1 | package com.framsticks.gui; |
---|
2 | |
---|
3 | import com.framsticks.gui.windows.console.ConsoleFrame; |
---|
4 | import com.framsticks.util.swing.MenuConstructor; |
---|
5 | import org.apache.log4j.Logger; |
---|
6 | |
---|
7 | import javax.swing.*; |
---|
8 | import javax.swing.border.TitledBorder; |
---|
9 | import java.awt.*; |
---|
10 | import java.awt.event.ActionEvent; |
---|
11 | import java.awt.event.ActionListener; |
---|
12 | import java.awt.event.KeyEvent; |
---|
13 | import java.awt.event.WindowAdapter; |
---|
14 | import java.awt.event.WindowEvent; |
---|
15 | |
---|
16 | /** |
---|
17 | * Application Main Frame. |
---|
18 | */ |
---|
19 | @SuppressWarnings("serial") |
---|
20 | public class MainFrame extends Frame { |
---|
21 | |
---|
22 | private final static Logger log = Logger.getLogger(MainFrame.class.getName()); |
---|
23 | |
---|
24 | JButton start, stop, step; |
---|
25 | |
---|
26 | /** |
---|
27 | * Builds main frame. |
---|
28 | */ |
---|
29 | public MainFrame(final Browser browser) { |
---|
30 | super("framsticks", browser); |
---|
31 | log.debug("creating main frame"); |
---|
32 | |
---|
33 | } |
---|
34 | |
---|
35 | |
---|
36 | @Override |
---|
37 | public void configure() { |
---|
38 | super.configure(); |
---|
39 | |
---|
40 | new MenuConstructor(fileMenu) |
---|
41 | .add(null, new AbstractAction("Console") { |
---|
42 | @Override |
---|
43 | public void actionPerformed(ActionEvent actionEvent) { |
---|
44 | showConsoleFrame(); |
---|
45 | } |
---|
46 | }) |
---|
47 | .add(KeyStroke.getKeyStroke(KeyEvent.VK_G, ActionEvent.CTRL_MASK), new AbstractAction("Goto") { |
---|
48 | @Override |
---|
49 | public void actionPerformed(ActionEvent actionEvent) { |
---|
50 | browser.autoResolvePath("/simulator/genepools/groups/0", null); |
---|
51 | // browser.autoResolvePath("/simulator/populations/groups/0", null; |
---|
52 | // browser.autoResolvePath("/simulator/genepools/groups/0/genotypes", null); |
---|
53 | // browser.autoResolvePath("/simulator/populations/groups", null); |
---|
54 | } |
---|
55 | }) |
---|
56 | .add(null, new AbstractAction("Disconnect") { |
---|
57 | @Override |
---|
58 | public void actionPerformed(ActionEvent actionEvent) { |
---|
59 | disconnect(); |
---|
60 | } |
---|
61 | }) |
---|
62 | .add(null, new AbstractAction("Quit") { |
---|
63 | @Override |
---|
64 | public void actionPerformed(ActionEvent actionEvent) { |
---|
65 | } |
---|
66 | }) |
---|
67 | ; |
---|
68 | |
---|
69 | setFrameProperties(); |
---|
70 | } |
---|
71 | |
---|
72 | // private void onConnectionEstablished() { |
---|
73 | // assert isActive(); |
---|
74 | |
---|
75 | // addNodeActionToTreePopupMenu("Open in console", new NodeAction() { |
---|
76 | // @Override |
---|
77 | // public void actionPerformed(TreeNode treeNode) { |
---|
78 | // assert isActive(); |
---|
79 | // } |
---|
80 | // }); |
---|
81 | |
---|
82 | |
---|
83 | |
---|
84 | |
---|
85 | // // browser.addNodeActionToTreePopupMenu("Resolve recursively", new NodeAction() { |
---|
86 | // // @Override |
---|
87 | // // public void actionPerformed(TreeNode treeNode) { |
---|
88 | // // final Child child = treeNode.getChild(); |
---|
89 | // // //server.invokeLater(new Runnable() { |
---|
90 | // // @Override |
---|
91 | // // public void run() { |
---|
92 | // // resolveRecursively(child); |
---|
93 | // // } |
---|
94 | // // }); |
---|
95 | // // } |
---|
96 | // // }); |
---|
97 | |
---|
98 | |
---|
99 | // // addNodeActionToTreePopupMenu("Store in file", new NodeAction() { |
---|
100 | // // @Override |
---|
101 | // // public void actionPerformed(final TreeNode treeNode) { |
---|
102 | // // // final Node node = treeNode.getNode(); |
---|
103 | // // // server.invokeLater(new Runnable() { |
---|
104 | // // // @Override |
---|
105 | // // // public void run() { |
---|
106 | // // // try { |
---|
107 | // // // log.info("storing"); |
---|
108 | // // // //File file = new File(); |
---|
109 | // // // StoreStream stream = new StoreStream(file, server.getManager()); |
---|
110 | // // // stream.store(node); |
---|
111 | // // // } catch (FileNotFoundException e) { |
---|
112 | // // // e.printStackTrace(); |
---|
113 | // // // } |
---|
114 | // // // } |
---|
115 | // // // }); |
---|
116 | // // } |
---|
117 | // // }); |
---|
118 | |
---|
119 | |
---|
120 | // // arguments.forEach("resolve", new UnaryFunctor<Boolean, String>() { |
---|
121 | // // @Override |
---|
122 | // // public Boolean call(final String s) { |
---|
123 | // // server.getManager().resolvePath(s, new StateFunctor() { |
---|
124 | // // @Override |
---|
125 | // // public void call(Exception e) { |
---|
126 | // // if (e != null) { |
---|
127 | // // log.error("failed to resolve: " + s); |
---|
128 | // // return; |
---|
129 | // // } |
---|
130 | // // log.info("succeeded to resolve: " + s); |
---|
131 | // // } |
---|
132 | // // }); |
---|
133 | // // return true; |
---|
134 | // // } |
---|
135 | // // }); |
---|
136 | |
---|
137 | |
---|
138 | // // arguments.forEach("console", new UnaryFunctor<Boolean, String>() { |
---|
139 | // // @Override |
---|
140 | // // public Boolean call(String s) { |
---|
141 | // // showConsoleFrame().setCommandLine(s); |
---|
142 | // // return true; |
---|
143 | // // } |
---|
144 | // // }); |
---|
145 | // } |
---|
146 | |
---|
147 | public void setSimulationRunning(final boolean run) { |
---|
148 | assert isActive(); |
---|
149 | /* |
---|
150 | remoteInstance.invokeLater(new Runnable() { |
---|
151 | @Override |
---|
152 | public void run() { |
---|
153 | remoteInstance.setRunning(run); |
---|
154 | } |
---|
155 | }); |
---|
156 | */ |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * Creates panel with start, step, stop buttons. |
---|
161 | */ |
---|
162 | @Override |
---|
163 | protected JPanel createLeftTopPanel() { |
---|
164 | assert isActive(); |
---|
165 | Dimension buttonDimension = new Dimension(45, 45); |
---|
166 | |
---|
167 | JPanel panel = new JPanel(); |
---|
168 | |
---|
169 | stop = new JButton(ImageProvider.loadImage(ImageProvider.SIM_STOP)); |
---|
170 | stop.addActionListener(new ActionListener() { |
---|
171 | |
---|
172 | public void actionPerformed(ActionEvent e) { |
---|
173 | setSimulationRunning(false); |
---|
174 | } |
---|
175 | |
---|
176 | }); |
---|
177 | |
---|
178 | stop.setPreferredSize(buttonDimension); |
---|
179 | stop.setToolTipText("Simulation Stop"); |
---|
180 | stop.setEnabled(false); |
---|
181 | |
---|
182 | step = new JButton(ImageProvider.loadImage(ImageProvider.SIM_STEP)); |
---|
183 | /* |
---|
184 | step.addActionListener(new ActionListener() { |
---|
185 | |
---|
186 | public void actionPerformed(ActionEvent e) { |
---|
187 | connection.send(new CallQuery().setMethod("step").setPath("/simulator")); |
---|
188 | } |
---|
189 | |
---|
190 | }); |
---|
191 | */ |
---|
192 | |
---|
193 | step.setPreferredSize(buttonDimension); |
---|
194 | step.setToolTipText("Simulation Step"); |
---|
195 | |
---|
196 | start = new JButton(ImageProvider.loadImage(ImageProvider.SIM_START)); |
---|
197 | start.addActionListener(new ActionListener() { |
---|
198 | |
---|
199 | public void actionPerformed(ActionEvent e) { |
---|
200 | setSimulationRunning(true); |
---|
201 | } |
---|
202 | |
---|
203 | }); |
---|
204 | |
---|
205 | start.setPreferredSize(buttonDimension); |
---|
206 | start.setToolTipText("Start Simulation"); |
---|
207 | |
---|
208 | JPanel buttonPanel = new JPanel(); |
---|
209 | buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS)); |
---|
210 | buttonPanel.add(Box.createHorizontalStrut(5)); |
---|
211 | buttonPanel.add(stop); |
---|
212 | buttonPanel.add(Box.createHorizontalStrut(10)); |
---|
213 | buttonPanel.add(step); |
---|
214 | buttonPanel.add(Box.createHorizontalStrut(10)); |
---|
215 | buttonPanel.add(start); |
---|
216 | buttonPanel.add(Box.createHorizontalStrut(5)); |
---|
217 | |
---|
218 | buttonPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory |
---|
219 | .createRaisedBevelBorder(), "Simulation Control", |
---|
220 | TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION |
---|
221 | )); |
---|
222 | |
---|
223 | panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS)); |
---|
224 | panel.add(Box.createHorizontalGlue()); |
---|
225 | panel.add(buttonPanel); |
---|
226 | panel.add(Box.createHorizontalGlue()); |
---|
227 | return panel; |
---|
228 | } |
---|
229 | |
---|
230 | /** |
---|
231 | * Sets frame properties. |
---|
232 | */ |
---|
233 | private void setFrameProperties() { |
---|
234 | assert isActive(); |
---|
235 | swing.setMinimumSize(swing.getPreferredSize()); |
---|
236 | swing.setSize(swing.getPreferredSize()); |
---|
237 | swing.setMaximumSize(screenDimension); |
---|
238 | swing.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
---|
239 | |
---|
240 | swing.setIconImage(ImageProvider.loadImage(ImageProvider.LOGO) |
---|
241 | .getImage()); |
---|
242 | |
---|
243 | |
---|
244 | swing.addWindowListener(new WindowAdapter() { |
---|
245 | public void windowClosing(WindowEvent e) { |
---|
246 | assert isActive(); |
---|
247 | /* |
---|
248 | if (connection != null) { |
---|
249 | connection.close(); |
---|
250 | } |
---|
251 | */ |
---|
252 | onWindowClosing(); |
---|
253 | swing.dispose(); |
---|
254 | } |
---|
255 | }); |
---|
256 | } |
---|
257 | |
---|
258 | public ConsoleFrame showConsoleFrame() { |
---|
259 | assert isActive(); |
---|
260 | /* |
---|
261 | ConsoleFrame consoleFrame = new ConsoleFrame(connection); |
---|
262 | consoleFrame.show(MainFrame.this); |
---|
263 | return consoleFrame; |
---|
264 | */ |
---|
265 | return null; |
---|
266 | } |
---|
267 | |
---|
268 | /** |
---|
269 | * Closes connection with manager. |
---|
270 | */ |
---|
271 | private void disconnect() { |
---|
272 | assert isActive(); |
---|
273 | //remoteInstance.disconnect(); |
---|
274 | browser.clear(); |
---|
275 | onWindowClosing(); |
---|
276 | statusBar.setText("You are disconnected"); |
---|
277 | } |
---|
278 | |
---|
279 | /** |
---|
280 | * Method used while window is closing. |
---|
281 | */ |
---|
282 | private void onWindowClosing() { |
---|
283 | assert isActive(); |
---|
284 | |
---|
285 | } |
---|
286 | |
---|
287 | public void setRunningButtons(boolean running) { |
---|
288 | assert isActive(); |
---|
289 | step.setEnabled(!running); |
---|
290 | stop.setEnabled(running); |
---|
291 | start.setEnabled(!running); |
---|
292 | } |
---|
293 | |
---|
294 | } |
---|
295 | |
---|