3 * Copyright (c) 2015 by Natalie Karl
4 * FH Hagenberg, Software Engineering
6 * Use is subject to license terms.
8 package isac.gui.mawen;
10 import static isac.gui.IGuiConstants.WINDOW_APPLICATION_INITIAL_SIZE_X;
11 import static isac.gui.IGuiConstants.WINDOW_APPLICATION_INITIAL_SIZE_Y;
12 import static isac.gui.IGuiConstants.WORKSHEET_DESKTOP_PANE_INITIAL_SIZE_X;
13 import static isac.gui.IGuiConstants.WORKSHEET_DESKTOP_PANE_INITIAL_SIZE_Y;
14 import static isac.gui.IGuiConstants.WORKSHEET_FRAME_INITIAL_OFFSET_Y;
15 import static isac.gui.IGuiConstants.WORKSHEET_FRAME_INITIAL_SIZE_X;
16 import isac.gui.IToWorksheetUser;
17 import isac.gui.Worksheet;
18 import isac.interfaces.ICalcIterator;
19 import isac.useractions.EUIElement;
20 import isac.useractions.IUIAction;
21 import isac.util.CalcChanged;
22 import isac.util.formulae.CalcFormula;
23 import isac.util.formulae.Position;
24 import isac.wsdialog.IWorksheetDialog;
26 import java.awt.BorderLayout;
27 import java.awt.Container;
28 import java.awt.Cursor;
29 import java.awt.Dimension;
30 import java.awt.event.WindowAdapter;
31 import java.awt.event.WindowEvent;
32 import java.awt.event.WindowListener;
33 import java.beans.PropertyVetoException;
34 import java.rmi.RemoteException;
35 import java.util.HashMap;
36 import java.util.Vector;
38 import javax.swing.JButton;
39 import javax.swing.JFrame;
40 import javax.swing.JDesktopPane;
41 import javax.swing.JInternalFrame;
42 import javax.swing.JMenuBar;
43 import javax.swing.plaf.basic.BasicInternalFrameUI;
44 import javax.swing.SwingUtilities;
46 import junit.framework.TestCase;
49 * @author Natalie Karl
51 * With a minimum of code present the terms of the example for reference
54 * <code>Worksheet</code> is launched by
55 * <code>WindowApplication#openNewWorksheet</code>, which involves a
56 * <code>IWorksheetDialog</code>, which in turn involves RMI (the
57 * <code>WorksheetDialog</code> runs in the <code>ObjectManager</code>).
59 * We don't want RMI in this TestCase and modify code of
60 * <code>WindowApplication#openNewWorksheet</code> such that RMI is
64 public class TestWorksheetForMawen extends TestCase implements IToWorksheetUser
67 private static JFrame frame_;
68 private JDesktopPane desktop_pane_;
69 protected static Worksheet currentSession;
71 @SuppressWarnings("unused") // is used below; deletion causes error
72 private HashMap<JButton, EUIElement> map_buttons_actions_;
73 @SuppressWarnings("unused") // is used below; deletion causes error
74 private HashMap<EUIElement, JButton> map_actions_buttons_;
75 private JMenuBar menu_bar_;
76 @SuppressWarnings("unused") // is used below; deletion causes error
77 private int num_browser_frames_;
78 @SuppressWarnings("unused") // is used below; deletion causes error
79 private boolean menu_separator_added_;
81 // from "implements IToWorksheetUser"
83 public void addToMenuBar(IUIAction action) {
87 public boolean removeFromMenuBar(IUIAction action) {
92 public void addCloseListener(Worksheet ws) {
96 public void removeCloseListener(Worksheet ws) {
99 // ======= copies from constructor of WindowApplication
100 private JFrame constructorWindowApplication() {
101 menu_separator_added_ = false;
103 frame_ = new JFrame();
104 frame_.setSize(new Dimension(WINDOW_APPLICATION_INITIAL_SIZE_X,
105 WINDOW_APPLICATION_INITIAL_SIZE_Y));
106 // locate frame in the middle of the screen
107 frame_.setLocationRelativeTo(null);
109 // initialize hashMaps
110 map_buttons_actions_ = new HashMap<JButton, EUIElement>();
111 map_actions_buttons_ = new HashMap<EUIElement, JButton>();
113 menu_bar_ = new JMenuBar();
114 frame_.setJMenuBar(menu_bar_);
115 num_browser_frames_ = 0;
117 this.createWorksheetPane();
118 frame_.add(desktop_pane_);
119 JFrame.setDefaultLookAndFeelDecorated(true);
121 frame_.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
122 frame_.setVisible(true);
126 // ------ methods from WindowApplication
127 private static WindowListener windowListener = new WindowAdapter() {
129 public void windowClosing(WindowEvent w) {
130 frame_.setVisible(false);
136 protected static void startApplication(/* final JFrame windowApplication */) {
137 SwingUtilities.invokeLater(new Runnable() { // Start the application
139 frame_.addWindowListener(windowListener);
144 public void createWorksheetPane() {
145 desktop_pane_ = new JDesktopPane();
146 Dimension d = new Dimension();
147 d.setSize(frame_.getWidth() * 0.60, 0.0);
148 desktop_pane_.setMinimumSize(d);
149 desktop_pane_.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
152 private void addInternalFrame(JInternalFrame frame) {
153 desktop_pane_.add(frame);
154 desktop_pane_.getDesktopManager().activateFrame(frame);
159 private JInternalFrame createInternalFrame(String title, boolean resizable,
160 boolean closable, boolean maximizable, boolean iconifiable) {
161 JInternalFrame internalFrame = new JInternalFrame(title, resizable,
162 closable, maximizable, iconifiable);
163 return internalFrame;
166 private void openNewWorksheet(IWorksheetDialog dialog)
167 throws RemoteException {
168 // InternalFrame, holding the Frames for the Worksheet and the
169 // corresponding CalcHeadPanel
170 JInternalFrame internalFrame = createInternalFrame("Worksheet", true,
172 internalFrame.setResizable(true);
173 internalFrame.setLayout(new BorderLayout());
175 // DesktopPane, added to internalFrame to be able to handle multiple
176 // InternalFrames in this InternalFrame
177 final JDesktopPane desktop = new JDesktopPane();
178 desktop.setSize(WORKSHEET_DESKTOP_PANE_INITIAL_SIZE_X,
179 WORKSHEET_DESKTOP_PANE_INITIAL_SIZE_Y);
181 desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
182 internalFrame.setContentPane(desktop); // , BorderLayout.CENTER);
184 frame_.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
186 Worksheet ws = new Worksheet((IToWorksheetUser) this, dialog, desktop);
187 // new InternalFrame for the Worksheet
188 JInternalFrame ws_frame = new JInternalFrame();
189 ((BasicInternalFrameUI) ws_frame.getUI()).setNorthPane(null);
190 ws_frame.setSize(WORKSHEET_FRAME_INITIAL_SIZE_X, frame_.getHeight()
191 - WORKSHEET_FRAME_INITIAL_OFFSET_Y);
194 c = ws_frame.getContentPane();
195 c.setLayout(new BorderLayout());
196 c.add(ws, BorderLayout.CENTER);
197 desktop.add(ws_frame);
199 ws_frame.setMaximum(true);
200 } catch (PropertyVetoException e) {
203 ws_frame.setFocusable(false);
204 ws_frame.setVisible(true);
206 internalFrame.addInternalFrameListener(ws);
208 // int width, height = 0;
209 // height = internalFrame.getPreferredSize().height;
210 // width = internalFrame.getPreferredSize().width;
211 // // make Frame a little smaller than the window
212 // height = frame_.getHeight() - WORKSHEET_FRAME_INITIAL_OFFSET_Y;
213 // width = width < 600 ? WORKSHEET_FRAME_INITIAL_SIZE_X : width;
215 // JInternalFrame[] frames = desktop_pane_.getAllFrames();
216 // int num_frames = frames.length - 4;
217 this.addInternalFrame(internalFrame);
218 // internalFrame.setLocation(num_frames * 20, num_frames * 20);
219 // internalFrame.setPreferredSize(new Dimension(width, height));
220 internalFrame.setLocation(20, 10);
221 internalFrame.setPreferredSize(new Dimension(500, 300));
222 internalFrame.pack();
223 internalFrame.setVisible(true);
225 frame_.setCursor(Cursor.getDefaultCursor());
228 /* protocol of building testWorksheetIsac (below): getFormulaeFromTo */
229 public void testModDialogIterator1() {
230 System.out.println("/--BEGIN isac.gui.mawen.testModDialogIterator1");
232 Vector<Integer> p = new Vector<Integer>(); p.add(1);
233 Position pos = new Position(p, "Res");
234 ICalcIterator from = new MockDialogIterator(pos);
235 p = new Vector<Integer>(); p.add(2);
236 pos = new Position(p, "Res");
237 ICalcIterator to = new MockDialogIterator(pos);
239 Vector<CalcFormula> fs = null;
240 try { // in the test NO rmi is involved
241 fs = from.getFormulaeFromTo(to, 0, false);
242 } catch (RemoteException e) {
245 assertEquals(fs.get(0).getPosition().toSMLString(), "([2],Res)");
247 // -------------------------------------------------------------------
248 p = new Vector<Integer>(); p.add(1);
249 pos = new Position(p, "Frm");
250 from = new MockDialogIterator(pos);
251 p = new Vector<Integer>(); p.add(2);
252 pos = new Position(p, "Res");
253 to = new MockDialogIterator(pos);
256 try { // in the test NO rmi is involved
257 fs = from.getFormulaeFromTo(to, 0, false);
258 } catch (RemoteException e) {
261 assertEquals(fs.get(0).getPosition().toSMLString(), "([1],Res)");
262 assertEquals(fs.get(1).getPosition().toSMLString(), "([2],Res)");
264 System.out.println("\\--END isac.gui.mawen.testModDialogIterator1");
267 /* protocol of building testWorksheetIsac (below): CalcChanged */
268 public void testModDialogIterator2() {
269 System.out.println("/--BEGIN isac.gui.mawen.testModDialogIterator2");
271 Vector<Integer> p = new Vector<Integer>();
272 Position pos = new Position(p, "Pbl");
273 ICalcIterator ci = new MockDialogIterator(pos);
275 CalcChanged e = new CalcChanged(4711, false, ci, ci, ci);
276 ICalcIterator from = e.getLastUnchangedFormula();
277 @SuppressWarnings("unused") // is used below; deletion causes error
278 ICalcIterator del = e.getLastDeletedFormula();
279 ICalcIterator to = e.getLastGeneratedFormula();
281 Vector<CalcFormula> fs = null;
283 fs = from.getFormulaeFromTo(to, 0, false);
284 } catch (RemoteException e1) {
285 // TODO Auto-generated catch block
286 e1.printStackTrace();
288 assertEquals(fs.size(), 1);
289 assertEquals(fs.get(0).getPosition().toSMLString(), "([],Pbl)");
290 // assertEquals(fs.get(0).getFormula().toSMLString(), "Simplify (1 + (2 * x * (y + 3)) / (4 * z * (y + 3)) + 5)");
291 assertEquals(fs.get(0).getFormula().toSMLString(), "1+((2*x*(y+3)+((y^2+6*y+9)/(y+3)))/(4*z*(y+3)))+(5::real)");
293 // -------------------------------------------------------------------
294 p = new Vector<Integer>(); p.add(1);
295 pos = new Position(p, "Res");
296 ICalcIterator unc = new MockDialogIterator(pos);
297 p = new Vector<Integer>(); p.add(2);
298 pos = new Position(p, "Res");
299 ICalcIterator gen = new MockDialogIterator(pos);
301 e = new CalcChanged(4711, false, unc, unc, gen);
302 from = e.getLastUnchangedFormula();
303 del = e.getLastDeletedFormula();
304 to = e.getLastGeneratedFormula();
308 fs = from.getFormulaeFromTo(to, 0, false);
309 } catch (RemoteException e1) {
310 e1.printStackTrace();
312 System.out.println(fs.size());
313 System.out.println(fs.get(0).getPosition().toSMLString());
314 System.out.println(fs.get(0).getFormula().toSMLString());
316 assertEquals(fs.size(), 1);
317 assertEquals(fs.get(0).getPosition().toSMLString(), "([2],Res)");
318 // assertEquals(fs.get(0).getFormula().toSMLString(), "1 + x / (2 * z) + 5");
319 assertEquals(fs.get(0).getFormula().toSMLString(), "1+(((2*x+1)*(y+3))/(4*z*(y+3)))+(5::real)");
322 System.out.println("\\--END isac.gui.mawen.testModDialogIterator2");
325 /* display example for reference in a Worksheet.
326 * THIS TEST WORKS ONLY IN DEBUG MODE, JUnit hangs here.
328 public void testWorksheetIsac() {
329 System.out.println("/--BEGIN isac.gui.mawen.testWorksheetIsac");
331 frame_ = constructorWindowApplication();
332 startApplication(/* windowApplication */);
333 MockWorksheetDialog wd = new MockWorksheetDialog();
336 this.openNewWorksheet(wd); // <<<<<<<-----------------------------------
338 } catch (RemoteException e) {
341 this.createWorksheetPane();
343 Position from; Position to;
344 //-->ISA: getFormulaeFromTo 1 ([],Pbl) ([],Pbl) 0 false;
345 from = new Position(); from.setKind("Pbl");
346 to = new Position(); to.setKind("Pbl");
349 } catch (InterruptedException e1) {
350 // TODO Auto-generated catch block
351 e1.printStackTrace();
353 wd.sendFormulaToWorksheet (from, to); // the root-node in CalcModelHierarchy
356 // } catch (InterruptedException e1) {
357 // // TODO Auto-generated catch block
358 // e1.printStackTrace();
360 // //-->ISA: getFormulaeFromTo 1 ([],Met) ([1],Frm) 0 false;
361 // from = new Position(); from.setKind("Met");
362 // to = new Position(); to.addInt("1"); to.setKind("Frm");
364 // Thread.sleep(100);
365 // } catch (InterruptedException e1) {
366 // // TODO Auto-generated catch block
367 // e1.printStackTrace();
369 // wd.sendFormulaToWorksheet (from, to);
371 // Thread.sleep(100);
372 // } catch (InterruptedException e1) {
373 // // TODO Auto-generated catch block
374 // e1.printStackTrace();
376 // //-->ISA: getFormulaeFromTo 1 ([1],Frm) ([1],Res) 0 false;
377 // from = new Position(); from.addInt("1"); from.setKind("Frm");
378 // to = new Position(); to.addInt("1"); to.setKind("Res");
381 // } catch (InterruptedException e1) {
382 // // TODO Auto-generated catch block
383 // e1.printStackTrace();
385 // wd.sendFormulaToWorksheet (from, to);
388 // } catch (InterruptedException e1) {
389 // // TODO Auto-generated catch block
390 // e1.printStackTrace();
392 // //-->ISA: getFormulaeFromTo 1 ([1],Res) ([2],Res) 0 false;
393 // from = new Position(); from.addInt("1"); from.setKind("Res");
394 // to = new Position(); to.addInt("2"); to.setKind("Res");
397 // } catch (InterruptedException e1) {
398 // // TODO Auto-generated catch block
399 // e1.printStackTrace();
401 // wd.sendFormulaToWorksheet (from, to);
404 // } catch (InterruptedException e1) {
405 // // TODO Auto-generated catch block
406 // e1.printStackTrace();
408 // //-->ISA: getFormulaeFromTo 1 ([2],Res) ([3],Res) 0 false;
409 // from = new Position(); from.addInt("2"); from.setKind("Res");
410 // to = new Position(); to.addInt("3"); to.setKind("Res");
413 // } catch (InterruptedException e1) {
414 // // TODO Auto-generated catch block
415 // e1.printStackTrace();
418 // wd.sendFormulaToWorksheet (from, to);
421 // } catch (InterruptedException e1) {
422 // // TODO Auto-generated catch block
423 // e1.printStackTrace();
425 // //ONLY SCEN 5-->ISA: getFormulaeFromTo 1 ([3],Res) ([4],Res) 0 false;
426 // from = new Position(); from.addInt("3"); from.setKind("Res");
427 // to = new Position(); to.addInt("4"); to.setKind("Res");
430 // } catch (InterruptedException e1) {
431 // // TODO Auto-generated catch block
432 // e1.printStackTrace();
434 // wd.sendFormulaToWorksheet (from, to);
437 // } catch (InterruptedException e1) {
438 // // TODO Auto-generated catch block
439 // e1.printStackTrace();
441 // from = new Position(); from.addInt("4"); from.setKind("Res");
442 // to = new Position(); to.addInt("4"); to.setKind("Res");
446 // } catch (InterruptedException e1) {
447 // // TODO Auto-generated catch block
448 // e1.printStackTrace();
450 // wd.sendFormulaToWorksheet (from, to);
451 //END ONLY SCEN 5-->ISA: getFormulaeFromTo 1 ([3],Res) ([4],Res) 0 false;
452 try { Thread.sleep(100000); } catch (InterruptedException e) { e.printStackTrace(); }
453 System.out.println("\\--END isac.gui.mawen.testWorksheetIsac");