|
| 1 | +package projections.Tools.UserEvents; |
| 2 | + |
| 3 | +import projections.gui.*; |
| 4 | + |
| 5 | +import javax.swing.JFrame; |
| 6 | +import javax.swing.JTable; |
| 7 | +import javax.swing.JScrollPane; |
| 8 | +import javax.swing.JPanel; |
| 9 | +import javax.swing.JButton; |
| 10 | +import javax.swing.table.TableColumn; |
| 11 | + |
| 12 | +import java.awt.BorderLayout; |
| 13 | +import java.awt.FlowLayout; |
| 14 | +import java.awt.Color; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +public class ChooseUserEntriesWindow extends JFrame { |
| 21 | + |
| 22 | + private int myRun = 0; |
| 23 | + |
| 24 | + final private ColorUpdateNotifier gw; |
| 25 | + final private EntryMethodVisibility data; |
| 26 | + private List<List> tabledata; |
| 27 | + final private Map<Integer, String> entryNames; |
| 28 | + final private GenericGraphColorer colorer; |
| 29 | + final private String[] eventNames; |
| 30 | + final private String option; |
| 31 | + |
| 32 | + public ChooseUserEntriesWindow(EntryMethodVisibility data, ColorUpdateNotifier gw, GenericGraphColorer colorer, Map<Integer, String> entryNames, String option) { |
| 33 | + this.data = data; |
| 34 | + this.gw = gw; |
| 35 | + this.colorer = colorer; |
| 36 | + this.entryNames = entryNames; |
| 37 | + this.eventNames = null; |
| 38 | + this.option = option; |
| 39 | + createLayout(); |
| 40 | + } |
| 41 | + |
| 42 | + ChooseUserEntriesWindow(EntryMethodVisibility data, ColorUpdateNotifier gw, GenericGraphColorer colorer, Map<Integer, String> entryNames, String option, String[] eventNames) { |
| 43 | + this.data = data; |
| 44 | + this.gw = gw; |
| 45 | + this.colorer = colorer; |
| 46 | + this.entryNames = entryNames; |
| 47 | + this.eventNames = eventNames; |
| 48 | + this.option = option; |
| 49 | + createLayout(); |
| 50 | + } |
| 51 | + |
| 52 | + private void createLayout() { |
| 53 | + setTitle("Choose which User " + option + "s are displayed"); |
| 54 | + List<String> columnNames = new ArrayList<>(4); |
| 55 | + columnNames.add("Visible"); |
| 56 | + columnNames.add("User " + option + " Name"); |
| 57 | + columnNames.add("ID"); |
| 58 | + columnNames.add("Color"); |
| 59 | + |
| 60 | + tabledata = new ArrayList<>(); |
| 61 | + |
| 62 | + makeTableData(); |
| 63 | + |
| 64 | + final MyTableModel tableModel = new MyTableModel(tabledata, columnNames, data, true); |
| 65 | + |
| 66 | + JTable table = new JTable(tableModel); |
| 67 | + initColumnSizes(table); |
| 68 | + |
| 69 | + table.setDefaultRenderer(ClickableColorBox.class, new ColorRenderer()); |
| 70 | + table.setDefaultEditor(ClickableColorBox.class, new ColorEditor()); |
| 71 | + table.setAutoCreateRowSorter(true); |
| 72 | + |
| 73 | + JScrollPane scroller = new JScrollPane(table); |
| 74 | + scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); |
| 75 | + |
| 76 | + JPanel p = new JPanel(); |
| 77 | + p.setLayout(new BorderLayout()); |
| 78 | + |
| 79 | + JPanel buttonPanel = new JPanel(); |
| 80 | + buttonPanel.setLayout(new FlowLayout()); |
| 81 | + JButton checkAll = new JButton("Make All Visible"); |
| 82 | + checkAll.addActionListener(actionEvent -> { |
| 83 | + changeVisibility(true, tableModel); |
| 84 | + tableModel.fireTableDataChanged(); |
| 85 | + data.displayMustBeRedrawn(); |
| 86 | + }); |
| 87 | + JButton uncheckAll = new JButton("Hide All"); |
| 88 | + uncheckAll.addActionListener(actionEvent -> { |
| 89 | + changeVisibility(false, tableModel); |
| 90 | + tableModel.fireTableDataChanged(); |
| 91 | + data.displayMustBeRedrawn(); |
| 92 | + }); |
| 93 | + buttonPanel.add(checkAll); |
| 94 | + buttonPanel.add(uncheckAll); |
| 95 | + p.add(buttonPanel, BorderLayout.NORTH); |
| 96 | + p.add(scroller, BorderLayout.CENTER); |
| 97 | + this.setContentPane(p); |
| 98 | + |
| 99 | + pack(); |
| 100 | + setSize(800, 400); |
| 101 | + setVisible(true); |
| 102 | + } |
| 103 | + |
| 104 | + private void makeTableData() { |
| 105 | + tabledata.clear(); |
| 106 | + for (Integer id : entryNames.keySet()) { |
| 107 | + int currIndex = id; |
| 108 | + if (eventNames != null) { |
| 109 | + String currName = entryNames.get(id); |
| 110 | + for (int i = 0; i < eventNames.length; i++) { |
| 111 | + if (currName.equals(eventNames[i])) { |
| 112 | + currIndex = i; |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + List<Object> tableRow = new ArrayList<>(4); |
| 118 | + tableRow.add(data.entryIsVisibleID(currIndex)); |
| 119 | + if (eventNames != null) |
| 120 | + tableRow.add(eventNames[currIndex]); |
| 121 | + else |
| 122 | + tableRow.add(entryNames.get(currIndex)); |
| 123 | + tableRow.add(currIndex); |
| 124 | + tableRow.add(new ClickableColorBox(currIndex, (Color) colorer.getColorMap()[currIndex], myRun, gw, colorer)); |
| 125 | + tabledata.add(tableRow); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private void initColumnSizes(JTable table) { |
| 130 | + TableColumn column; |
| 131 | + |
| 132 | + column = table.getColumnModel().getColumn(0); |
| 133 | + column.setPreferredWidth(70); |
| 134 | + |
| 135 | + column = table.getColumnModel().getColumn(1); |
| 136 | + column.setPreferredWidth(680); |
| 137 | + |
| 138 | + column = table.getColumnModel().getColumn(2); |
| 139 | + column.setPreferredWidth(50); |
| 140 | + } |
| 141 | + |
| 142 | + private void changeVisibility(boolean visible, MyTableModel tableModel) { |
| 143 | + for (List<Object> v : tabledata) { |
| 144 | + Integer id = (Integer) v.get(2); |
| 145 | + if (visible) |
| 146 | + data.makeEntryVisibleID(id); |
| 147 | + else |
| 148 | + data.makeEntryInvisibleID(id); |
| 149 | + } |
| 150 | + for (List<Object> v : tabledata) { |
| 151 | + v.set(0, visible); |
| 152 | + } |
| 153 | + tableModel.fireTableDataChanged(); |
| 154 | + data.displayMustBeRedrawn(); |
| 155 | + } |
| 156 | +} |
0 commit comments