1 | /* |
---|
2 | * This file is part of OpenModelica. |
---|
3 | * |
---|
4 | * Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC), |
---|
5 | * c/o Linköpings universitet, Department of Computer and Information Science, |
---|
6 | * SE-58183 Linköping, Sweden. |
---|
7 | * |
---|
8 | * All rights reserved. |
---|
9 | * |
---|
10 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR |
---|
11 | * THIS OSMC PUBLIC LICENSE (OSMC-PL). |
---|
12 | * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE |
---|
13 | * OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, ACCORDING TO RECIPIENTS CHOICE. |
---|
14 | * |
---|
15 | * The OpenModelica software and the Open Source Modelica |
---|
16 | * Consortium (OSMC) Public License (OSMC-PL) are obtained |
---|
17 | * from OSMC, either from the above address, |
---|
18 | * from the URLs: http://www.ida.liu.se/projects/OpenModelica or |
---|
19 | * http://www.openmodelica.org, and in the OpenModelica distribution. |
---|
20 | * GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html. |
---|
21 | * |
---|
22 | * This program is distributed WITHOUT ANY WARRANTY; without |
---|
23 | * even the implied warranty of MERCHANTABILITY or FITNESS |
---|
24 | * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH |
---|
25 | * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. |
---|
26 | * |
---|
27 | * See the full OSMC Public License conditions for more details. |
---|
28 | * |
---|
29 | * Contributors: |
---|
30 | * Wladimir Schamai, EADS Innovation Works 2009-2011 |
---|
31 | * Uwe Pohlmann, University of Paderborn 2009-2010 |
---|
32 | * Parham Vasaiely, EADS Innovation Works / Hamburg University of Applied Sciences 2009-2011 |
---|
33 | */ |
---|
34 | package org.modelica; |
---|
35 | import java.awt.GridLayout; |
---|
36 | import java.awt.Label; |
---|
37 | import java.awt.event.ActionEvent; |
---|
38 | import java.awt.event.ActionListener; |
---|
39 | import java.awt.event.KeyEvent; |
---|
40 | import java.awt.event.KeyListener; |
---|
41 | import java.util.ArrayList; |
---|
42 | |
---|
43 | import javax.swing.BoxLayout; |
---|
44 | import javax.swing.JButton; |
---|
45 | import javax.swing.JPanel; |
---|
46 | import javax.swing.JScrollPane; |
---|
47 | import javax.swing.JSplitPane; |
---|
48 | import javax.swing.JTextArea; |
---|
49 | import javax.swing.JTextField; |
---|
50 | |
---|
51 | // TODO: Auto-generated Javadoc |
---|
52 | /** |
---|
53 | * The Class OMCPane. |
---|
54 | */ |
---|
55 | public class OMCPane extends JPanel implements ActionListener, KeyListener |
---|
56 | { |
---|
57 | |
---|
58 | /** The Constant serialVersionUID. */ |
---|
59 | private static final long serialVersionUID = 1L; |
---|
60 | |
---|
61 | /** The j text field. */ |
---|
62 | private JTextField jTextField = null; |
---|
63 | |
---|
64 | /** The j button. */ |
---|
65 | private JButton jButton = null; |
---|
66 | |
---|
67 | /** The j demo button. */ |
---|
68 | private JButton jDemoButton = null; |
---|
69 | |
---|
70 | /** The j text area. */ |
---|
71 | private JTextArea jTextArea = null; |
---|
72 | |
---|
73 | /** The omc. */ |
---|
74 | private OMCProxy omc; |
---|
75 | |
---|
76 | /** The j panel1. */ |
---|
77 | private JPanel jPanel1 = null; |
---|
78 | |
---|
79 | /** The label. */ |
---|
80 | private Label label = null; |
---|
81 | |
---|
82 | /** The j split pane. */ |
---|
83 | private JSplitPane jSplitPane = null; |
---|
84 | |
---|
85 | /** The j scroll pane. */ |
---|
86 | private JScrollPane jScrollPane = null; |
---|
87 | |
---|
88 | /** The history. */ |
---|
89 | private ArrayList<String> history = new ArrayList<String>(); |
---|
90 | |
---|
91 | /** |
---|
92 | * This is the default constructor. |
---|
93 | */ |
---|
94 | public OMCPane() |
---|
95 | { |
---|
96 | super(new GridLayout(1,0)); |
---|
97 | initialize(); |
---|
98 | this.omc = new OMCProxy();; |
---|
99 | history.clear(); |
---|
100 | history.add(0, "getVersion()"); |
---|
101 | history.add(0, "loadModel(Modelica)"); |
---|
102 | history.add(0, "getClassNames()"); |
---|
103 | history.add(0, "getClassNames(...class name...)"); |
---|
104 | history.add(0, "loadFile(\"...file...\")"); |
---|
105 | history.add(0, "list()"); |
---|
106 | history.add(0, "list(...class name...)"); |
---|
107 | history.add(0, "simulate(...class name...)"); |
---|
108 | history.add(0, "plot(...variable name...)"); |
---|
109 | history.add(0, "plot({...var1, var2,...})"); |
---|
110 | history.add(0, "instantiateModel(...class name..."); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * This method initializes this. |
---|
115 | * |
---|
116 | * @return void |
---|
117 | */ |
---|
118 | private void initialize() |
---|
119 | { |
---|
120 | this.setSize(398, 260); |
---|
121 | this.add(getJSplitPane(), null); |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * This method initializes jTextField. |
---|
126 | * |
---|
127 | * @return javax.swing.JTextField |
---|
128 | */ |
---|
129 | private JTextField getJTextField() |
---|
130 | { |
---|
131 | if (jTextField == null) |
---|
132 | { |
---|
133 | jTextField = new JTextField(); |
---|
134 | jTextField.setName("Expression"); |
---|
135 | jTextField.setColumns(100); |
---|
136 | } |
---|
137 | jTextField.addKeyListener(this); |
---|
138 | return jTextField; |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * This method initializes jButton. |
---|
143 | * |
---|
144 | * @return javax.swing.JButton |
---|
145 | */ |
---|
146 | private JButton getJButton() |
---|
147 | { |
---|
148 | if (jButton == null) |
---|
149 | { |
---|
150 | jButton = new JButton(); |
---|
151 | jButton.setText("Send"); |
---|
152 | jButton.setActionCommand("send"); |
---|
153 | } |
---|
154 | jButton.addActionListener(this); |
---|
155 | return jButton; |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * This method initializes jButton. |
---|
160 | * |
---|
161 | * @return javax.swing.JButton |
---|
162 | */ |
---|
163 | private JButton getJDemoButton() |
---|
164 | { |
---|
165 | if (jDemoButton == null) |
---|
166 | { |
---|
167 | jDemoButton = new JButton(); |
---|
168 | jDemoButton.setText("Demo"); |
---|
169 | jDemoButton.setActionCommand("demo"); |
---|
170 | } |
---|
171 | jDemoButton.addActionListener(this); |
---|
172 | return jDemoButton; |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * This method initializes jTextArea. |
---|
177 | * |
---|
178 | * @return javax.swing.JTextArea |
---|
179 | */ |
---|
180 | private JTextArea getJTextArea() |
---|
181 | { |
---|
182 | if (jTextArea == null) |
---|
183 | { |
---|
184 | jTextArea = new JTextArea(); |
---|
185 | jTextArea.setColumns(100); |
---|
186 | jTextArea.setRows(20); |
---|
187 | } |
---|
188 | return jTextArea; |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * Execute command. |
---|
193 | * |
---|
194 | * @param command |
---|
195 | * the command |
---|
196 | */ |
---|
197 | public void executeCommand(String command) |
---|
198 | { |
---|
199 | System.out.println("Expression:" + command); |
---|
200 | if (command != null && |
---|
201 | command.length() > 0) |
---|
202 | { |
---|
203 | history.add(command); |
---|
204 | String result = ""; |
---|
205 | try |
---|
206 | { |
---|
207 | jTextArea.append("\nSending expression:" + command); |
---|
208 | result = omc.sendExpression(command); |
---|
209 | jTextArea.append("\nGot reply:" + result); |
---|
210 | } |
---|
211 | catch(Exception ex) |
---|
212 | { |
---|
213 | jTextArea.append( |
---|
214 | "\nError while sending expression: " + command + "\n"+ |
---|
215 | ex.getMessage()); |
---|
216 | } |
---|
217 | jTextField.setText(""); |
---|
218 | } |
---|
219 | else |
---|
220 | { |
---|
221 | jTextArea.append("\nNo expression sent because is empty"); |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | /* (non-Javadoc) |
---|
226 | * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) |
---|
227 | */ |
---|
228 | public void actionPerformed(ActionEvent e) |
---|
229 | { |
---|
230 | System.out.println("ActionCommand:" + e.getActionCommand()); |
---|
231 | if (e.getActionCommand().equals("send")) |
---|
232 | { |
---|
233 | executeCommand(jTextField.getText()); |
---|
234 | } |
---|
235 | if (e.getActionCommand().equals("demo")) |
---|
236 | { |
---|
237 | Thread t = new Thread() |
---|
238 | { |
---|
239 | public void run() |
---|
240 | { |
---|
241 | jTextArea.append("\n// ****************************** DEMO STARTS *************************************"); |
---|
242 | jTextArea.append("\n// ****************************** Starting BouncingBall DEMO **********************"); |
---|
243 | jTextArea.append("\n// send the BouncingBall model"); |
---|
244 | executeCommand("\n" + |
---|
245 | "model BouncingBall\n"+ |
---|
246 | " parameter Real e=0.7 \"coefficient of restitution\";\n" + |
---|
247 | " parameter Real g=9.81 \"gravity acceleration\";\n" + |
---|
248 | " Real h(start=1) \"height of ball\";\n" + |
---|
249 | " Real v \"velocity of ball\";\n" + |
---|
250 | " Boolean flying(start=true) \"true, if ball is flying\";\n" + |
---|
251 | " Boolean impact;\n" + |
---|
252 | " Real v_new;\n" + |
---|
253 | " Integer foo;\n" + |
---|
254 | "equation\n" + |
---|
255 | " impact = h <= 0.0;\n" + |
---|
256 | " foo = if impact then 1 else 2;\n" + |
---|
257 | " der(v) = if flying then -g else 0;\n" + |
---|
258 | " der(h) = v;\n" + |
---|
259 | " when {h <= 0.0 and v <= 0.0,impact} then\n" + |
---|
260 | " v_new = if edge(impact) then -e*pre(v) else 0;\n" + |
---|
261 | " flying = v_new > 0;\n" + |
---|
262 | " reinit(v, v_new);\n" + |
---|
263 | " end when;\n" + |
---|
264 | "end BouncingBall;\n"); |
---|
265 | jTextArea.repaint(); |
---|
266 | jTextArea.append("\n// see if there were any errors"); |
---|
267 | executeCommand("getErrorString()"); |
---|
268 | jTextArea.append("\n// instantiate the BouncingBall model"); |
---|
269 | executeCommand("instantiateModel(BouncingBall)"); executeCommand("getErrorString()"); |
---|
270 | jTextArea.append("\n// check the BouncingBall model"); |
---|
271 | executeCommand("instantiateModel(BouncingBall)"); executeCommand("getErrorString()"); |
---|
272 | jTextArea.append("\n// simulate the BouncingBall model. The files will be generated in the current directory."); |
---|
273 | executeCommand("simulate(BouncingBall,stopTime=3.5)"); executeCommand("getErrorString()"); |
---|
274 | jTextArea.append("\n// plot some of the BouncingBall model variables"); |
---|
275 | executeCommand("plot({h, impact, v})"); executeCommand("getErrorString()"); |
---|
276 | jTextArea.append("\n// ******************* BouncingBall DEMO DONE ****************"); |
---|
277 | jTextArea.append("\n// ******************* Starting dcmotor script DEMO *************"); |
---|
278 | executeCommand("readFile(\"sim_dcmotor.mos\")"); executeCommand("getErrorString()"); |
---|
279 | executeCommand("runScript(\"sim_dcmotor.mos\")"); executeCommand("getErrorString()"); |
---|
280 | jTextArea.append("\n// ****** DEMO OVER, see OpenModelica Users/System Guides for more API commands ********"); |
---|
281 | } |
---|
282 | }; |
---|
283 | t.start(); |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | * This method initializes jPanel1. |
---|
289 | * |
---|
290 | * @return javax.swing.JPanel |
---|
291 | */ |
---|
292 | private JPanel getJPanel1() { |
---|
293 | if (jPanel1 == null) { |
---|
294 | label = new Label(); |
---|
295 | label.setText("Expression:"); |
---|
296 | jPanel1 = new JPanel(); |
---|
297 | JPanel jPanelX = new JPanel(); |
---|
298 | jPanelX.setLayout(new BoxLayout(jPanelX, BoxLayout.X_AXIS)); |
---|
299 | jPanelX.add(label, null); |
---|
300 | jPanelX.add(getJTextField(), null); |
---|
301 | jPanelX.add(getJButton(), null); |
---|
302 | jPanelX.add(getJDemoButton(), null); |
---|
303 | JPanel jPanelY = new JPanel(); |
---|
304 | jPanelY.setLayout(new BoxLayout(jPanelY, BoxLayout.Y_AXIS)); |
---|
305 | jPanelY.add(jPanelX); |
---|
306 | Label helpLabel = new Label(); |
---|
307 | helpLabel.setText("Use <ENTER> or press Send to send the command, <Up>/<Down> keys for command history." + |
---|
308 | " Press Demo for a quick demonstration of available OMC commands."); |
---|
309 | jPanelY.add(helpLabel, null); |
---|
310 | jPanel1.add(jPanelY); |
---|
311 | } |
---|
312 | return jPanel1; |
---|
313 | } |
---|
314 | |
---|
315 | /** |
---|
316 | * This method initializes jSplitPane. |
---|
317 | * |
---|
318 | * @return javax.swing.JSplitPane |
---|
319 | */ |
---|
320 | private JSplitPane getJSplitPane() { |
---|
321 | if (jSplitPane == null) { |
---|
322 | jSplitPane = new JSplitPane(); |
---|
323 | jSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); |
---|
324 | jSplitPane.setBottomComponent(getJScrollPane()); |
---|
325 | jSplitPane.setTopComponent(getJPanel1()); |
---|
326 | } |
---|
327 | return jSplitPane; |
---|
328 | } |
---|
329 | |
---|
330 | /** |
---|
331 | * This method initializes jScrollPane. |
---|
332 | * |
---|
333 | * @return javax.swing.JScrollPane |
---|
334 | */ |
---|
335 | private JScrollPane getJScrollPane() { |
---|
336 | if (jScrollPane == null) { |
---|
337 | jScrollPane = new JScrollPane(); |
---|
338 | jScrollPane.setViewportView(getJTextArea()); |
---|
339 | } |
---|
340 | return jScrollPane; |
---|
341 | } |
---|
342 | |
---|
343 | /* (non-Javadoc) |
---|
344 | * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent) |
---|
345 | */ |
---|
346 | public void keyPressed(KeyEvent e) { |
---|
347 | // TODO Auto-generated method stub |
---|
348 | if (e.getKeyCode() == KeyEvent.VK_ENTER) |
---|
349 | { |
---|
350 | executeCommand(jTextField.getText()); |
---|
351 | } |
---|
352 | if (e.getKeyCode() == KeyEvent.VK_UP) |
---|
353 | { |
---|
354 | // set the last command in the text field |
---|
355 | jTextField.setText(history.get(history.size()-1)); |
---|
356 | // move the last command at the beginning! |
---|
357 | String tmp = history.get(history.size()-1); |
---|
358 | history.add(0, tmp); |
---|
359 | history.remove(history.size()-1); |
---|
360 | } |
---|
361 | if (e.getKeyCode() == KeyEvent.VK_DOWN) |
---|
362 | { |
---|
363 | // set the first command in the text field |
---|
364 | jTextField.setText(history.get(0)); |
---|
365 | // move the first command at the end! |
---|
366 | String tmp = history.get(0); |
---|
367 | history.add(history.size()-1, tmp); |
---|
368 | history.remove(0); |
---|
369 | } |
---|
370 | } |
---|
371 | |
---|
372 | /* (non-Javadoc) |
---|
373 | * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent) |
---|
374 | */ |
---|
375 | public void keyReleased(KeyEvent arg0) { |
---|
376 | // TODO Auto-generated method stub |
---|
377 | |
---|
378 | } |
---|
379 | |
---|
380 | /* (non-Javadoc) |
---|
381 | * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent) |
---|
382 | */ |
---|
383 | public void keyTyped(KeyEvent arg0) { |
---|
384 | // TODO Auto-generated method stub |
---|
385 | |
---|
386 | } |
---|
387 | |
---|
388 | } // @jve:decl-index=0:visual-constraint="10,10" |
---|