1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
package com.tbo;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class PortfolioAndRuleApplicationV6 {
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Portfolio and Rule Application V6");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(500, 300);
mainFrame.setLayout(new FlowLayout());
JButton choosePortfolioButton = new JButton("Choose Portfolios");
JButton chooseRuleButton = new JButton("Choose Rules");
mainFrame.add(choosePortfolioButton);
mainFrame.add(chooseRuleButton);
SelectionDialog<Portfolio, PortfolioToExclude> portfolioDialog =
new SelectionDialog<>(mainFrame, "Portfolio Selection",
() -> fetchPortfoliosFromDatabase(),
PortfolioToExclude::new);
SelectionDialog<Rule, RuleToExclude> ruleDialog =
new SelectionDialog<>(mainFrame, "Rule Selection",
() -> fetchRulesFromDatabase(),
RuleToExclude::new);
choosePortfolioButton.addActionListener(e -> portfolioDialog.showDialog());
chooseRuleButton.addActionListener(e -> ruleDialog.showDialog());
mainFrame.setVisible(true);
}
static List<Portfolio> fetchPortfoliosFromDatabase() {
List<Portfolio> portfolios = new ArrayList<>();
portfolios.add(new Portfolio("Portfolio Alpha", 1, false));
portfolios.add(new Portfolio("Portfolio Beta", 2, true));
portfolios.add(new Portfolio("Portfolio Gamma", 3, false));
return portfolios;
}
static List<Rule> fetchRulesFromDatabase() {
List<Rule> rules = new ArrayList<>();
rules.add(new Rule("Rule A", 1, false));
rules.add(new Rule("Rule B", 2, true));
rules.add(new Rule("Rule C", 3, false));
return rules;
}
static class SelectionDialog<T, U extends Excludable> {
private final JDialog dialog;
private final Map<U, JCheckBox> checkBoxMap = new HashMap<>();
private final List<U> allItemsToExclude;
private final JPanel checkBoxPanel;
private final Set<Integer> tempExcludedItems;
public SelectionDialog(Frame parentFrame, String title,
Supplier<List<T>> fetchFunction,
Function<T, U> mapper) {
allItemsToExclude = mapToExcludable(fetchFunction.get(), mapper);
tempExcludedItems = new HashSet<>();
dialog = new JDialog(parentFrame, title, Dialog.ModalityType.APPLICATION_MODAL);
dialog.setSize(300, 400);
dialog.setLayout(new BorderLayout());
JTextField filterField = new JTextField();
dialog.add(filterField, BorderLayout.NORTH);
checkBoxPanel = new JPanel();
checkBoxPanel.setLayout(new BoxLayout(checkBoxPanel, BoxLayout.Y_AXIS));
JScrollPane scrollPane = new JScrollPane(checkBoxPanel);
dialog.add(scrollPane, BorderLayout.CENTER);
JButton saveButton = new JButton("Save Selection");
dialog.add(saveButton, BorderLayout.SOUTH);
saveButton.addActionListener(e -> saveSelections());
filterField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
updateCheckBoxes(filterField.getText());
}
@Override
public void removeUpdate(DocumentEvent e) {
updateCheckBoxes(filterField.getText());
}
@Override
public void changedUpdate(DocumentEvent e) {
updateCheckBoxes(filterField.getText());
}
});
updateCheckBoxes("");
}
private void updateCheckBoxes(String filterText) {
String searchText = filterText.toLowerCase();
checkBoxPanel.removeAll();
for (U itemToExclude : allItemsToExclude) {
if (searchText.isEmpty() || itemToExclude.getLabel().toLowerCase().contains(searchText)) {
JCheckBox checkBox = checkBoxMap.computeIfAbsent(itemToExclude, k -> new JCheckBox(itemToExclude.getLabel(), itemToExclude.isChecked()));
checkBox.addActionListener(e -> handleCheckBoxAction(itemToExclude, checkBox.isSelected()));
checkBoxPanel.add(checkBox);
}
}
checkBoxPanel.revalidate();
checkBoxPanel.repaint();
}
public void showDialog() {
dialog.setVisible(true);
}
private void handleCheckBoxAction(U itemToExclude, boolean isSelected) {
if (isSelected) {
tempExcludedItems.add(itemToExclude.getId());
} else {
tempExcludedItems.remove(itemToExclude.getId());
}
}
private void saveSelections() {
// Logic to save selections
}
private List<U> mapToExcludable(List<T> items, Function<T, U> mapper) {
List<U> itemsToExclude = new ArrayList<>();
for (T item : items) {
itemsToExclude.add(mapper.apply(item));
}
return itemsToExclude;
}
}
interface Excludable {
int getId();
String getLabel();
boolean isChecked();
void setChecked(boolean isChecked);
}
static class Portfolio {
private final String label;
private final int id;
private final boolean isChecked;
public Portfolio(String label, int id, boolean isChecked) {
this.label = label;
this.id = id;
this.isChecked = isChecked;
}
// Getters pour label, id et isChecked
}
static class Rule {
private final String description;
private final int id;
private final boolean isChecked;
public Rule(String description, int id, boolean isChecked) {
this.description = description;
this.id = id;
this.isChecked = isChecked;
}
// Getters pour description, id et isChecked
}
static class PortfolioToExclude implements Excludable {
private final int id;
private final String label;
private boolean isChecked;
public PortfolioToExclude(int id, String label, boolean isChecked) {
this.id = id;
this.label = label;
this.isChecked = isChecked;
}
@Override
public int getId() {
return id;
}
@Override
public String getLabel() {
return label;
}
@Override
public boolean isChecked() {
return isChecked;
}
@Override
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
}
static class RuleToExclude implements Excludable {
private final int id;
private final String description;
private boolean isChecked;
public RuleToExclude(int id, String description, boolean isChecked) {
this.id = id;
this.description = description;
this.isChecked = isChecked;
}
@Override
public int getId() {
return id;
}
@Override
public String getLabel() {
return description;
}
@Override
public boolean isChecked() {
return isChecked;
}
@Override
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
}
} |
Partager