- Timestamp:
- 07/14/13 23:20:04 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/gui/controls/EventControl.java
r100 r101 1 1 package com.framsticks.gui.controls; 2 2 3 import java.awt.Dimension;4 3 import java.awt.event.ActionEvent; 5 4 import java.awt.event.ActionListener; 6 7 import javax.swing.JButton; 8 5 import java.text.SimpleDateFormat; 6 import java.util.ArrayList; 7 import java.util.Date; 8 import java.util.List; 9 10 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; 9 18 import com.framsticks.params.types.EventParam; 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; 24 25 import static com.framsticks.core.TreeOperations.*; 10 26 11 27 /** … … 13 29 */ 14 30 @SuppressWarnings("serial") 15 public class EventControl extends Control {31 public class EventControl extends HistoryControl { 16 32 // private static final Logger log = LogManager.getLogger(EventControl.class.getName()); 17 33 18 protected final JButton button; 19 boolean subscribed = true; 34 @SuppressWarnings("rawtypes") 35 protected final SideNoteKey<EventListener> listenerKey = SideNoteKey.make(EventListener.class); 36 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; 20 43 21 44 public EventControl(final EventParam eventParam) { 22 45 super(eventParam); 23 46 24 button = new JButton("subscribe"); 25 this.add(button); 26 this.setMaximumSize(new Dimension(Integer.MAX_VALUE, Control.LINE_HEIGHT)); 27 28 button.addActionListener(new ActionListener() { 47 mainButton.setText("Subscribe"); 48 mainButton.setName("subscription"); 49 50 tableModel = new TableModel(); 51 resultsTable.setModel(tableModel); 52 53 mainButton.addActionListener(new ActionListener() { 29 54 @Override 30 55 public void actionPerformed(ActionEvent e) { 31 if (subscribed) { 32 //panel.getCurrentTreeNode().unsubscribe(eventParam); 33 } else { 34 //panel.getCurrentTreeNode().subscribe(eventParam); 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; 35 63 } 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 36 90 } 37 91 }); 38 92 93 updateFoldState(); 94 Gui.setupTitledControl(this, controlRow, resultsScrollPane); 39 95 } 40 96 41 97 @Override 42 98 protected void updateEnabled(boolean enabled) { 43 button.setEnabled(enabled); 44 } 45 46 /* 47 @Override 48 public void refresh() { 49 subscribed = panel.getCurrentTreeNode().hasSubscribed((EventParam)param); 50 button.setText(subscribed ? "unsubscribe" : "subscribe"); 51 this.revalidate(); 52 } 53 */ 99 mainButton.setEnabled(enabled); 100 } 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 110 @Override 111 protected void refreshTable() { 112 History history = getSideNote(assureCurrentPath(), historyKey); 113 tableModel.entries = history != null ? history.entries : null; 114 tableModel.refreshAll(); 115 } 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 } 54 200 }
Note: See TracChangeset
for help on using the changeset viewer.