source: java/main/src/main/java/com/framsticks/gui/controls/EventControl.java @ 105

Last change on this file since 105 was 105, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • import refactorization: move Tree, Path, etc.

from core to structure package

  • initial serialization implementation
  • improve PrimeExperiment? test
  • many organizational changes and convenience improvements

CHANGELOG:
Make registry in AbstractTree? final.

Move most classes from core to structure package.

Minor changes.

Switch names of Future and FutureHandler?.

Rename ExceptionResultHandler? to ExceptionHandler?.

Rename ExceptionHandler? to ExceptionDispatcherHandler?.

Fix bug in ParamCandidate? cache.

Add missing synchronization to the BufferedDispatcher?.

Develop @Serialized support.

Rework serialization further.

Add serialization/deserialization interface to ValueParam?.

Move getStorageType and isNumeric from Param down to params hierarchy.

Minor changes.

Improve param type induction.

Add TestSerializedClass? for testing new serialization.

Add info files gor GenePool? and Population.

Add standard.expt exemplary netfile.

Add type name field to PropertiesObject?.

Use PropertiesObject? for PropertiesAccess? instead of ordinary map.

Hide getFramsClass is several more places.

More unification accross FramsClass?, Access and Path.

Add ParamCollection?.

Simplify interface for getting params from FramsClass?, Access
or Path.

Make Access.call() interface variadic.

Add arguments(args) convenience wrapper around new Object[] {args}.

Upgrade to apache.commons.lang version 3.1

Minor improvement with Response constructors.

Develop proper result printing in ClientAtServer?.

Add experimentNetsave to PrimeExperiment?.

File size: 5.1 KB
Line 
1package com.framsticks.gui.controls;
2
3import java.awt.event.ActionEvent;
4import java.awt.event.ActionListener;
5import java.text.SimpleDateFormat;
6import java.util.ArrayList;
7import java.util.Date;
8import java.util.List;
9
10
11
12import com.framsticks.gui.Frame;
13import com.framsticks.gui.Gui;
14import com.framsticks.gui.table.AbstractTableModel;
15import com.framsticks.params.EventListener;
16import com.framsticks.params.types.EventParam;
17import com.framsticks.structure.Path;
18import com.framsticks.structure.SideNoteKey;
19import com.framsticks.util.FramsticksUnsupportedOperationException;
20import com.framsticks.util.dispatching.Dispatching;
21import com.framsticks.util.dispatching.Future;
22import com.framsticks.util.dispatching.RunAt;
23import com.framsticks.util.lang.Pair;
24
25import static com.framsticks.structure.TreeOperations.*;
26
27/**
28 * @author Piotr Sniegowski
29 */
30@SuppressWarnings("serial")
31public class EventControl extends HistoryControl {
32        // private static final Logger log = LogManager.getLogger(EventControl.class.getName());
33
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;
43
44        public EventControl(final EventParam eventParam) {
45                super(eventParam);
46
47                mainButton.setText("Subscribe");
48                mainButton.setName("subscription");
49
50                tableModel = new TableModel();
51                resultsTable.setModel(tableModel);
52
53                mainButton.addActionListener(new ActionListener() {
54                        @Override
55                        public void actionPerformed(ActionEvent e) {
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, Future.doNothing(Void.class, owner.getFrame()));
62                                        return;
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                                addListener(path, getParam(), newListener, Object.class, new Future<Void>(owner.getFrame()) {
82
83                                        @Override
84                                        protected void result(Void result) {
85                                                putSideNote(path, listenerKey, newListener);
86                                                refreshState();
87                                        }
88                                });
89
90                        }
91                });
92
93                updateFoldState();
94                Gui.setupTitledControl(this, controlRow, resultsScrollPane);
95        }
96
97        @Override
98        protected void updateEnabled(boolean enabled) {
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        }
200}
Note: See TracBrowser for help on using the repository browser.