[84] | 1 | package com.framsticks.gui.controls; |
---|
| 2 | |
---|
| 3 | import java.awt.event.ActionEvent; |
---|
| 4 | import java.awt.event.ActionListener; |
---|
[101] | 5 | import java.text.SimpleDateFormat; |
---|
| 6 | import java.util.ArrayList; |
---|
| 7 | import java.util.Date; |
---|
| 8 | import java.util.List; |
---|
[84] | 9 | |
---|
| 10 | |
---|
[101] | 11 | |
---|
| 12 | import com.framsticks.core.Path; |
---|
| 13 | import com.framsticks.core.SideNoteKey; |
---|
| 14 | import com.framsticks.gui.Frame; |
---|
| 15 | import com.framsticks.gui.Gui; |
---|
| 16 | import com.framsticks.gui.table.AbstractTableModel; |
---|
| 17 | import com.framsticks.params.EventListener; |
---|
[84] | 18 | import com.framsticks.params.types.EventParam; |
---|
[101] | 19 | import com.framsticks.util.FramsticksUnsupportedOperationException; |
---|
| 20 | import com.framsticks.util.dispatching.Dispatching; |
---|
| 21 | import com.framsticks.util.dispatching.FutureHandler; |
---|
| 22 | import com.framsticks.util.dispatching.RunAt; |
---|
| 23 | import com.framsticks.util.lang.Pair; |
---|
[84] | 24 | |
---|
[101] | 25 | import static com.framsticks.core.TreeOperations.*; |
---|
| 26 | |
---|
[84] | 27 | /** |
---|
| 28 | * @author Piotr Sniegowski |
---|
| 29 | */ |
---|
| 30 | @SuppressWarnings("serial") |
---|
[101] | 31 | public class EventControl extends HistoryControl { |
---|
[100] | 32 | // private static final Logger log = LogManager.getLogger(EventControl.class.getName()); |
---|
[84] | 33 | |
---|
[101] | 34 | @SuppressWarnings("rawtypes") |
---|
| 35 | protected final SideNoteKey<EventListener> listenerKey = SideNoteKey.make(EventListener.class); |
---|
[84] | 36 | |
---|
[101] | 37 | public static class History { |
---|
| 38 | protected final List<Pair<Date, Object>> entries = new ArrayList<>(); |
---|
| 39 | }; |
---|
| 40 | |
---|
| 41 | protected final SideNoteKey<History> historyKey = SideNoteKey.make(History.class); |
---|
| 42 | protected TableModel tableModel; |
---|
| 43 | |
---|
[84] | 44 | public EventControl(final EventParam eventParam) { |
---|
| 45 | super(eventParam); |
---|
| 46 | |
---|
[101] | 47 | mainButton.setText("Subscribe"); |
---|
| 48 | mainButton.setName("subscription"); |
---|
[84] | 49 | |
---|
[101] | 50 | tableModel = new TableModel(); |
---|
| 51 | resultsTable.setModel(tableModel); |
---|
| 52 | |
---|
| 53 | mainButton.addActionListener(new ActionListener() { |
---|
[84] | 54 | @Override |
---|
| 55 | public void actionPerformed(ActionEvent e) { |
---|
[101] | 56 | final Path path = assureCurrentPath(); |
---|
| 57 | EventListener<?> listener = getSideNote(path, listenerKey); |
---|
| 58 | if (listener != null) { |
---|
| 59 | removeSideNote(path, listenerKey); |
---|
| 60 | refreshState(); |
---|
| 61 | path.getTree().removeListener(path, getParam(), listener, FutureHandler.doNothing(Void.class, owner.getFrame())); |
---|
| 62 | return; |
---|
[84] | 63 | } |
---|
[101] | 64 | |
---|
| 65 | final EventListener<Object> newListener = new EventListener<Object>() { |
---|
| 66 | @Override |
---|
| 67 | public void action(final Object argument) { |
---|
| 68 | /** actions can be invoked from anywhere */ |
---|
| 69 | Dispatching.dispatchIfNotActive(owner.getFrame(), new RunAt<Frame>(owner.getFrame()) { |
---|
| 70 | |
---|
| 71 | @Override |
---|
| 72 | protected void runAt() { |
---|
| 73 | getOrCreateSideNote(path.getTree(), path.getTopObject(), historyKey).entries.add(Pair.make(new Date(), argument)); |
---|
| 74 | refreshTable(); |
---|
| 75 | } |
---|
| 76 | }); |
---|
| 77 | // owner.getFrame().getStatusBar().showInfo("event " + param + " happened: " + argument); |
---|
| 78 | } |
---|
| 79 | }; |
---|
| 80 | |
---|
| 81 | path.getTree().addListener(path, getParam(), newListener, Object.class, new FutureHandler<Void>(owner.getFrame()) { |
---|
| 82 | |
---|
| 83 | @Override |
---|
| 84 | protected void result(Void result) { |
---|
| 85 | putSideNote(path, listenerKey, newListener); |
---|
| 86 | refreshState(); |
---|
| 87 | } |
---|
| 88 | }); |
---|
| 89 | |
---|
[84] | 90 | } |
---|
| 91 | }); |
---|
| 92 | |
---|
[101] | 93 | updateFoldState(); |
---|
| 94 | Gui.setupTitledControl(this, controlRow, resultsScrollPane); |
---|
[84] | 95 | } |
---|
| 96 | |
---|
[98] | 97 | @Override |
---|
| 98 | protected void updateEnabled(boolean enabled) { |
---|
[101] | 99 | mainButton.setEnabled(enabled); |
---|
[98] | 100 | } |
---|
| 101 | |
---|
[101] | 102 | public boolean isListening() { |
---|
| 103 | return hasSideNote(getCurrentPath(), listenerKey); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | protected void refreshButtonState() { |
---|
| 107 | mainButton.setText(isListening() ? "Don't listen" : "Listen"); |
---|
| 108 | } |
---|
| 109 | |
---|
[84] | 110 | @Override |
---|
[101] | 111 | protected void refreshTable() { |
---|
| 112 | History history = getSideNote(assureCurrentPath(), historyKey); |
---|
| 113 | tableModel.entries = history != null ? history.entries : null; |
---|
| 114 | tableModel.refreshAll(); |
---|
[84] | 115 | } |
---|
[101] | 116 | |
---|
| 117 | @Override |
---|
| 118 | protected void clearTable() { |
---|
| 119 | History history = getSideNote(assureCurrentPath(), historyKey); |
---|
| 120 | if (history != null) { |
---|
| 121 | history.entries.clear(); |
---|
| 122 | } |
---|
| 123 | refreshTable(); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | public void refreshState() { |
---|
| 127 | refreshButtonState(); |
---|
| 128 | refreshTable(); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | @Override |
---|
| 132 | public EventParam getParam() { |
---|
| 133 | return (EventParam) param; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | public class TableModel extends AbstractTableModel { |
---|
| 138 | |
---|
| 139 | List<Pair<Date, Object>> entries; |
---|
| 140 | SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss:SSS"); |
---|
| 141 | |
---|
| 142 | @Override |
---|
| 143 | public Class<?> getColumnClass(int columnIndex) { |
---|
| 144 | return String.class; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | @Override |
---|
| 148 | public int getColumnCount() { |
---|
| 149 | return 2; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | @Override |
---|
| 153 | public String getColumnName(int columnIndex) { |
---|
| 154 | return columnIndex == 0 ? "Occured at" : "Argument"; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | @Override |
---|
| 158 | public int getRowCount() { |
---|
| 159 | if (entries == null) { |
---|
| 160 | return 0; |
---|
| 161 | } |
---|
| 162 | if (isFolded()) { |
---|
| 163 | return entries.isEmpty() ? 0 : 1; |
---|
| 164 | } |
---|
| 165 | return entries.size(); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | @Override |
---|
| 169 | public Object getValueAt(int rowIndex, int columnIndex) { |
---|
| 170 | if (entries == null) { |
---|
| 171 | return null; |
---|
| 172 | } |
---|
| 173 | Pair<Date, Object> entry; |
---|
| 174 | if (isFolded()) { |
---|
| 175 | if (rowIndex > 0) { |
---|
| 176 | return null; |
---|
| 177 | } |
---|
| 178 | if (entries.isEmpty()) { |
---|
| 179 | return null; |
---|
| 180 | } |
---|
| 181 | entry = entries.get(entries.size() - 1); |
---|
| 182 | } else { |
---|
| 183 | entry = entries.get(rowIndex); |
---|
| 184 | } |
---|
| 185 | return columnIndex == 0 ? format.format(entry.first) : entry.second; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | @Override |
---|
| 189 | public boolean isCellEditable(int rowIndex, int columnIndex) { |
---|
| 190 | return false; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | @Override |
---|
| 194 | public void setValueAt(Object value, int rowIndex, int columnIndex) { |
---|
| 195 | throw new FramsticksUnsupportedOperationException().msg("setting value in event history"); |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | |
---|
| 199 | } |
---|
[84] | 200 | } |
---|