001: package com.xoetrope.carousel.visualizer;
002:
003: import java.awt.BorderLayout;
004: import java.awt.CardLayout;
005: import java.awt.Component;
006: import java.awt.Dimension;
007: import java.awt.FlowLayout;
008: import java.awt.Point;
009: import java.awt.Rectangle;
010: import java.awt.event.ActionEvent;
011: import java.awt.event.ActionListener;
012: import java.awt.event.MouseEvent;
013: import java.awt.event.MouseListener;
014: import javax.swing.JButton;
015: import javax.swing.JComboBox;
016: import javax.swing.JDialog;
017: import javax.swing.JFrame;
018: import javax.swing.JLabel;
019: import javax.swing.JMenuItem;
020: import javax.swing.JPanel;
021: import javax.swing.JPopupMenu;
022: import javax.swing.JScrollPane;
023: import javax.swing.JSpinner;
024: import javax.swing.JSplitPane;
025: import javax.swing.JTabbedPane;
026: import javax.swing.JTextField;
027: import javax.swing.BorderFactory;
028: import javax.swing.border.TitledBorder;
029: import javax.swing.event.TreeSelectionEvent;
030: import javax.swing.event.TreeSelectionListener;
031: import javax.swing.tree.TreePath;
032: import javax.swing.JOptionPane;
033:
034: import net.xoetrope.editor.project.XEditorProject;
035: import net.xoetrope.editor.project.XEditorProjectManager;
036: import net.xoetrope.editor.project.pages.IEditorUtility;
037:
038: import net.xoetrope.xui.XProjectManager;
039: import net.xoetrope.xui.data.XBaseModel;
040: import net.xoetrope.xui.data.XModel;
041: import net.xoetrope.editor.ui.KalideoscopeToolbar;
042:
043: import java.beans.PropertyChangeEvent;
044: import java.awt.event.ActionEvent;
045:
046: /**
047: * The main panel of the data model visualization.
048: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
049: * the GNU Public License (GPL), please see license.txt for more details. If
050: * you make commercial use of this software you must purchase a commercial
051: * license from Xoetrope.</p>
052: * <p> $Revision: 1.18 $</p>
053: */
054: public class ModelVisualiserPanel extends JPanel implements
055: TreeSelectionListener, MouseListener {
056: private NewNodeDialog newNodeDialog;
057: private JTabbedPane tabbedPane;
058: private JPanel leftPane;
059: private JPanel rightPane;
060: private JPanel visualizerPanel;
061: private ModelVisualiserTree visualizerTreel;
062: private JSplitPane splitPane = null;
063: private KalideoscopeToolbar visualizerToolbar;
064: private KalideoscopeToolbar treeToolbar;
065: private XModel rootModel;
066:
067: private XStructureTable structureTable;
068:
069: // debugging interface
070: private VisualiserDebuggerModel debuggerModel;
071: private VisualiserDebuggerEngine debuggerEngine;
072:
073: private boolean debugView;
074: private String projectPath;
075: private XEditorProject currentProject;
076: private JButton editAttributeBtn, copyAttrToClipboardBtn;
077: private JButton addAttributeBtn, deleteAttributeBtn;
078: private JButton refreshBtn, copyNodeToClipboardBtn;
079: private JButton deleteNodeBtn, renameNodeBtn, addNodeBtn;
080:
081: private ActionListener leftPaneListener, rightPaneListener;
082:
083: public ModelVisualiserPanel(int preferredWidth,
084: VisualiserDebuggerEngine dEngine) {
085: debuggerEngine = dEngine;
086: currentProject = (XEditorProject) XEditorProjectManager
087: .getCurrentProject();
088: debugView = (debuggerEngine != null);
089:
090: leftPaneListener = new LeftPaneActionListener();
091: rightPaneListener = new RightPaneActionListener();
092:
093: // left pane
094: leftPane = new JPanel();
095: leftPane.setLayout(new BorderLayout());
096: leftPane.setBorder(BorderFactory.createEmptyBorder());
097:
098: visualizerTreel = new ModelVisualiserTree(this );
099: visualizerTreel.addTreeSelectionListener(this );
100: visualizerTreel.expandRoot();
101: visualizerTreel.getTreeComponent().addMouseListener(this );
102: visualizerTreel.setPreferredSize(new Dimension(preferredWidth,
103: 200));
104: visualizerTreel.setMinimumSize(new Dimension(
105: preferredWidth / 2, 100));
106:
107: treeToolbar = new KalideoscopeToolbar();
108: refreshBtn = treeToolbar.addTool(leftPaneListener,
109: "refresh.gif", "Refresh", "Refresh the model");
110: treeToolbar.addSeparator();
111: addNodeBtn = treeToolbar.addTool(leftPaneListener,
112: "newModelNode.gif", "Add...",
113: "Add a new node to the model");
114: deleteNodeBtn = treeToolbar.addTool(leftPaneListener,
115: "deleteModelNode.gif", "Delete",
116: "Delete the selected node from the model");
117: deleteNodeBtn.setEnabled(false);
118: treeToolbar.addSeparator();
119: renameNodeBtn = treeToolbar.addTool(leftPaneListener,
120: "renameModelNode.gif", "Rename...",
121: "Rename the model node");
122: renameNodeBtn.setEnabled(false);
123: treeToolbar.addSeparator();
124: copyNodeToClipboardBtn = treeToolbar.addTool(leftPaneListener,
125: "modelPathToClipboard.gif", "Copy path to clipboard",
126: "Copy the model path to the clipboard");
127: treeToolbar.addSeparator();
128:
129: leftPane.add(treeToolbar, BorderLayout.NORTH);
130: leftPane.add(visualizerTreel, BorderLayout.CENTER);
131:
132: // right pane
133: rightPane = new JPanel();
134: rightPane.setLayout(new BorderLayout());
135: rightPane.setBorder(BorderFactory.createEmptyBorder());
136:
137: tabbedPane = new JTabbedPane();
138: visualizerPanel = new JPanel();
139: visualizerPanel.setLayout(new BorderLayout());
140:
141: structureTable = new XStructureTable();
142:
143: visualizerToolbar = new KalideoscopeToolbar();
144: editAttributeBtn = visualizerToolbar.addTool(rightPaneListener,
145: "editModelNode.gif", "Edit...", "Edit the attribute");
146: editAttributeBtn.setEnabled(false);
147: if (!debugView) {
148: visualizerToolbar.addSeparator();
149: addAttributeBtn = visualizerToolbar.addTool(
150: rightPaneListener, "addAttribute.gif",
151: "Add Attribute...",
152: "Add a new attribute to the model node");
153: addAttributeBtn.setEnabled(false);
154: deleteAttributeBtn = visualizerToolbar
155: .addTool(rightPaneListener, "deleteAttribute.gif",
156: "Delete Attribute",
157: "Delete the selected attribute from the model node");
158: deleteAttributeBtn.setEnabled(false);
159: }
160: visualizerToolbar.addSeparator();
161: copyAttrToClipboardBtn = visualizerToolbar.addTool(
162: rightPaneListener, "modelPathToClipboard.gif",
163: "Copy path to clipboard",
164: "Copy the model path to the clipboard");
165: copyAttrToClipboardBtn.setEnabled(false);
166: rightPane.add(visualizerToolbar, BorderLayout.NORTH);
167:
168: //JScrollPane scrollPane = new JScrollPane( valuesTable );
169: JScrollPane scrollPane = new JScrollPane(structureTable);
170: scrollPane.setBorder(BorderFactory.createEmptyBorder());
171: rightPane.add(scrollPane, BorderLayout.CENTER);
172:
173: tabbedPane.add("structure", rightPane);
174: tabbedPane.add("visualizer", visualizerPanel);
175:
176: splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
177: leftPane, tabbedPane);
178: splitPane.setOneTouchExpandable(true);
179: splitPane.setDividerSize(6);
180: splitPane.setUI(new javax.swing.plaf.basic.BasicSplitPaneUI());
181: splitPane.setBorder(BorderFactory.createEmptyBorder());
182: setLayout(new BorderLayout());
183: add(splitPane, BorderLayout.CENTER);
184: splitPane.setDividerLocation(0.3);
185:
186: if (debugView)
187: debuggerEngine.setupDebuggerInterface(this );
188:
189: newNodeDialog = new NewNodeDialog(null);
190: }
191:
192: public ModelVisualiserPanel(int preferredWidth) {
193: this (preferredWidth, null);
194: }
195:
196: public boolean debugView() {
197: return debugView;
198: }
199:
200: /**
201: * Set the root mode node displayed by this visualizer
202: * @param root the root of the node hierarchy
203: */
204: public void setRootModel(XModel root) {
205: rootModel = root;
206: structureTable.setModelNode(root);
207: refresh(null);
208: }
209:
210: /**
211: * Set a message in an empty tree
212: * @param message the new message text
213: */
214: public void addMessageNode(String message) {
215: visualizerTreel.addMessageNode(message);
216: }
217:
218: /**
219: * Set listener for the tree
220: * @param the listener
221: */
222: public void setModelTreeListener(ModelTreeListener mtl) {
223: visualizerTreel.setModelTreeListner(mtl);
224: }
225:
226: /**
227: * Set the selected path in the tree
228: * @param the selection path
229: */
230: public void setSelectedPath(String path) {
231: visualizerTreel.setSelectedPath(path);
232: }
233:
234: /**
235: * Get the splitpane
236: * @return the split pane
237: */
238: protected JSplitPane getSplitPane() {
239: return splitPane;
240: }
241:
242: /**
243: * Get the model tree
244: * @return the tree
245: */
246: public ModelVisualiserTree getModelVisualiserTree() {
247: return visualizerTreel;
248: }
249:
250: /**
251: * Update the table following a tree selection change
252: */
253: public void valueChanged(TreeSelectionEvent evt) {
254: if (rootModel == null)
255: return;
256: String path = getTreePath();
257:
258: // Clear the table
259: structureTable.setModelNode(null);
260:
261: // enable/disable buttons, leave the buttons disabled
262: // for database table model node
263: boolean dbTableNodeSelected = isDbTableNodeSelected();
264: boolean buttState = (path.length() > 0)
265: && (!dbTableNodeSelected);
266: if (addNodeBtn != null)
267: addNodeBtn.setEnabled(!dbTableNodeSelected);
268: if (deleteNodeBtn != null)
269: deleteNodeBtn.setEnabled(path.length() > 0);
270: if (renameNodeBtn != null)
271: renameNodeBtn.setEnabled(buttState);
272: if (addAttributeBtn != null)
273: addAttributeBtn.setEnabled(buttState);
274:
275: // Don't fill an empty table!
276: XModel modelNode = null;
277: if (path.length() > 0) {
278: try {
279:
280: modelNode = (XModel) rootModel.get(path);
281: if (modelNode == null)
282: return;
283:
284: } catch (ClassCastException ex) {
285: ex.printStackTrace();
286: }
287:
288: visualizerPanel.removeAll();
289: Component comp = XModelVisualizerFactory.getVisualizer(
290: modelNode, this );
291: visualizerPanel.add(comp, BorderLayout.CENTER);
292: visualizerPanel.repaint();
293:
294: structureTable.setModelNode(modelNode);
295: }
296: }
297:
298: /**
299: * Get the path of the selected treenode by looping the selected path array
300: * and placing a '/' between each item
301: * @return the path to the selected XModel
302: */
303: protected String getTreePath() {
304: String modelName = null;
305: TreePath selectedPath = visualizerTreel.getSelectionPath();
306: if (selectedPath != null) {
307: Object path[] = selectedPath.getPath();
308:
309: modelName = "";
310: for (int i = 1; i < path.length; i++) {
311: String pathEle = path[i].toString();
312: pathEle = pathEle.substring(0, pathEle.indexOf(": "));
313: String value = path[i].toString();
314: value = value.substring(value.indexOf(": ") + 2, value
315: .length());
316: //if (( value.length() > 0 ) && ( value.compareTo( "null" ) != 0 ) && ( value.indexOf( "DatabaseTableModel" ) == -1 ) )
317: //pathEle += "@value=[" + value + "]";
318: modelName += "/" + pathEle;
319: }
320: }
321: if (modelName == null)
322: return "";
323: else if (modelName.trim().compareTo("") != 0)
324: return modelName.substring(1);
325: else
326: return "";
327: }
328:
329: /**
330: * Centre the panel on screen.
331: */
332: public void centerScreen(Rectangle parentRect) {
333: double x = parentRect.getWidth() - getSize().getWidth();
334: double y = parentRect.getHeight() - getSize().getHeight();
335: setLocation((int) x, (int) y);
336: }
337:
338: /**
339: * left tree actions listener
340: */
341: private class LeftPaneActionListener implements ActionListener {
342: public void actionPerformed(ActionEvent ae) {
343: String path = visualizerTreel.getStrippedPath();
344: String cmd = ae.getActionCommand();
345: if (cmd.equals("Refresh")) {
346: visualizerTreel.refresh();
347: } else if (cmd.equals("Add...")) {
348: if (newNodeDialog.showDialog(path)) {
349: if (newNodeDialog.getType() == NewNodeDialog.TABLEMODEL) {
350: int ncols = newNodeDialog.getNumCols();
351: int nrows = newNodeDialog.getNumRows();
352: String name = newNodeDialog.getTableName();
353: addTable(path, name, nrows, ncols);
354: } else if (newNodeDialog.getType() == NewNodeDialog.BASEMODEL) {
355: String name = newNodeDialog.getSingleNodeName();
356: addNode(path, name);
357: path += ("/" + name);
358: }
359: }
360: } else if (cmd.equals("Delete")) {
361: int res = JOptionPane.showConfirmDialog(leftPane,
362: "Are you sure you want to delete: " + path,
363: "Delete node", JOptionPane.OK_CANCEL_OPTION,
364: JOptionPane.WARNING_MESSAGE);
365: if (res == JOptionPane.OK_OPTION)
366: deleteNode(path);
367: } else if (cmd.equals("Rename...")) {
368: String name = JOptionPane.showInputDialog(leftPane,
369: "Rename: " + path,
370: "Enter the new name for the node",
371: JOptionPane.QUESTION_MESSAGE);
372: if (name == null)
373: return;
374: renameNode(path, name);
375: } else if (cmd.equals("Copy path to clipboard")) {
376: copyNodePathToClipboard();
377: return;
378: } else
379: return;
380:
381: // Fall through, including the 'refresh' command
382: refresh(path);
383: }
384: }
385:
386: /**
387: * right table actions listener
388: */
389: private class RightPaneActionListener implements ActionListener {
390: public void actionPerformed(ActionEvent ae) {
391: String cmd = ae.getActionCommand();
392: String path = visualizerTreel.getStrippedPath();
393:
394: if (cmd.equals("Edit...")) {
395: //int idx = valuesTable.getSelectedRow();
396: int idx = structureTable.getSelectedRow();
397: if (idx >= 0) {
398: //String attribName = ( String ) listTableModel.getValueAt( idx, 0 );
399: String attribName = (String) structureTable
400: .getValueAt(idx, 0);
401: String value = JOptionPane.showInputDialog(
402: tabbedPane, "Value for: " + path + "@"
403: + attribName,
404: "Enter the new node value",
405: JOptionPane.QUESTION_MESSAGE);
406: if (value == null)
407: return;
408: setNodeValue(path, attribName, value);
409: } else
410: JOptionPane
411: .showMessageDialog(
412: tabbedPane,
413: "Please select the attribute you wish to edit.",
414: "Cannot edit the node value",
415: JOptionPane.WARNING_MESSAGE);
416: } else if (cmd.equals("Add Attribute...")) {
417: String name = JOptionPane.showInputDialog(tabbedPane,
418: "Add attribute: " + path,
419: "Enter the new attribute name",
420: JOptionPane.OK_CANCEL_OPTION);
421: if (name == null)
422: return;
423: addAttribute(path, name);
424: } else if (cmd.equals("Delete Attribute")) {
425: //String attribName = (String)listTableModel.getValueAt( valuesTable.getSelectedRow(), 0 );
426: int idx = structureTable.getSelectedRow();
427: String attribName = (String) structureTable.getValueAt(
428: idx, 0);
429: int res = JOptionPane.showConfirmDialog(tabbedPane,
430: "Are you sure you want to delete the attribute: "
431: + attribName, "Delete attribute",
432: JOptionPane.OK_CANCEL_OPTION,
433: JOptionPane.WARNING_MESSAGE);
434: if (res == JOptionPane.OK_OPTION)
435: deleteAttribute(path, attribName);
436: else
437: return;
438: } else if (cmd.equals("Copy path to clipboard")) {
439: copyAttrPathToClipboard();
440: return;
441: } else
442: return;
443:
444: // Fall through, including the 'refresh' command
445: refresh(path);
446: }
447: }
448:
449: /**
450: * The mouse was clicked on this component
451: * @param me the mouse event
452: */
453: public void mouseClicked(MouseEvent me) {
454: }
455:
456: private void popupTriggered(MouseEvent me) {
457: if (me.isPopupTrigger()) {
458: JPopupMenu popupMenu = new JPopupMenu("Refresh");
459: if (visualizerTreel.getSelectionPath() != null) {
460: JMenuItem mi = new JMenuItem("Refresh");
461: mi.addActionListener(leftPaneListener);
462: popupMenu.add(mi);
463:
464: popupMenu.addSeparator();
465:
466: mi = new JMenuItem("Add...");
467: mi.addActionListener(leftPaneListener);
468: popupMenu.add(mi);
469:
470: if (visualizerTreel.getSelectionPath().getPathCount() > 0) {
471: mi = new JMenuItem("Delete");
472: mi.addActionListener(leftPaneListener);
473: popupMenu.add(mi);
474:
475: popupMenu.addSeparator();
476:
477: mi = new JMenuItem("Rename...");
478: mi.addActionListener(leftPaneListener);
479: popupMenu.add(mi);
480:
481: popupMenu.addSeparator();
482:
483: mi = new JMenuItem("Copy path to clipboard");
484: mi.addActionListener(leftPaneListener);
485: popupMenu.add(mi);
486: }
487: Point pt = me.getPoint();
488: Point vp = visualizerTreel.getLocation();
489: popupMenu.show(this , pt.x + vp.x, pt.y + vp.y);
490: }
491: }
492: }
493:
494: /**
495: * The mouse button was pressed while over this component
496: * @param me the mouse event
497: */
498: public void mousePressed(MouseEvent me) {
499: popupTriggered(me);
500: }
501:
502: /**
503: * Responds to a mouse button release by poping up a context menu for the component
504: * @param me
505: */
506: public void mouseReleased(MouseEvent me) {
507: popupTriggered(me);
508: }
509:
510: /**
511: * The mouse entered this component's area
512: * @param me the mouse event
513: */
514: public void mouseEntered(MouseEvent me) {
515: }
516:
517: /**
518: * The mouse exited this component's area
519: * @param me the mouse event
520: */
521: public void mouseExited(MouseEvent me) {
522: }
523:
524: /**
525: * Refresh the model representation
526: * @param selectedPath the selected path in the tree or null for no sleection
527: */
528: synchronized public void refresh(String selectedPath) {
529: if (debugView && (debuggerModel == null))
530: debuggerEngine.setupDebuggerInterface(this );
531: if (debugView)
532: visualizerTreel.setRootModel(debuggerModel);
533: else
534: visualizerTreel.setRootModel(XProjectManager
535: .getCurrentProject().getModel());
536:
537: visualizerTreel.removeTreeSelectionListener(this );
538: visualizerTreel.removeMouseListener(this );
539: visualizerTreel.createTreeComp(null);
540: visualizerTreel.addTreeSelectionListener(this );
541: visualizerTreel.addMouseListener(this );
542: if (selectedPath != null) {
543: visualizerTreel.setSelectedPath(selectedPath);
544: }
545: }
546:
547: /**
548: * Copies selected node path to the clipboard
549: */
550: public void copyNodePathToClipboard() {
551: String selection = visualizerTreel.getStrippedPath();
552: if (selection != null) {
553: IEditorUtility editorUtility = currentProject
554: .getEditorUtility();
555: editorUtility.copyToClipboard(selection);
556: }
557: }
558:
559: /**
560: * Copies selected attribute path to the clipboard
561: */
562:
563: public void copyAttrPathToClipboard() {
564: String selection = visualizerTreel.getStrippedPath();
565: int row = structureTable.getSelectedRow();
566: if (row != -1) {
567: //String attribute = (String)valuesTable.getValueAt( row, 0 );
568: String attribute = (String) structureTable.getValueAt(row,
569: 0);
570: if (attribute != null) {
571: selection += "/" + attribute;
572: IEditorUtility editorUtility = currentProject
573: .getEditorUtility();
574: editorUtility.copyToClipboard(selection);
575: }
576: }
577: }
578:
579: /**
580: * Determines whether selected node is a database table node
581: */
582: protected boolean isDbTableNodeSelected() {
583: String selection = visualizerTreel.getStrippedPath();
584: XModel model = (XModel) rootModel.get(selection);
585: return XModelVisualizerFactory.isDbTableModel(model);
586: }
587:
588: /**
589: * Add an attribute to the model
590: * @param path the selected path
591: * @param attribName the new attribute name
592: */
593: public void addAttribute(String path, String attribName) {
594: if (path.length() > 0) {
595: XModel model = (XModel) rootModel.get(path);
596: model.set("@" + attribName, "");
597: currentProject.setModified(true);
598: }
599: }
600:
601: /**
602: * Delete the selected attribute from the model
603: */
604: public void deleteAttribute(String path, String attribName) {
605: if (path.length() > 0) {
606: if (attribName.equals("value") || attribName.equals("id")) {
607: JOptionPane.showMessageDialog(tabbedPane, attribName
608: + " is a required attribute ",
609: "Cannot delete the attribute",
610: JOptionPane.WARNING_MESSAGE);
611: return;
612: }
613:
614: XModel node = (XModel) rootModel.get(path);
615: if (node instanceof XBaseModel) {
616: if (attribName != null) {
617: int idx = node.getAttribute(attribName);
618: ((XBaseModel) node).setAttribValue(idx, null, null);
619: currentProject.setModified(true);
620: }
621: }
622: }
623: }
624:
625: /**
626: * Add a new node to the model
627: * @param path the path of the parent node
628: * @param name the name/ID of the new node
629: */
630: public void addNode(String path, String name) {
631: XModel model = (XModel) (path.length() > 0 ? rootModel
632: .get(path) : rootModel);
633: XModel newNode = (XModel) model.append(name);
634: newNode.setTagName("data");
635: currentProject.setModified(true);
636: }
637:
638: /**
639: * Add a new table model to the model
640: * @param path the path of the parent node
641: * @param name the name/ID of the new node
642: */
643: public void addTable(String path, String name, int nrows, int ncols) {
644: XModel model = (XModel) (path.length() > 0 ? rootModel
645: .get(path) : rootModel);
646: //root
647: XModel tableNode = (XModel) model.append(name);
648: tableNode.setTagName("table");
649: //header
650: XModel headerNode = (XModel) tableNode.append(name + " header");
651: headerNode.setTagName("th");
652: for (int i = 1; i <= ncols; i++) {
653: XModel columnNode = (XModel) headerNode.append(String
654: .valueOf(i));
655: columnNode.set("col " + i);
656: columnNode.setTagName("data");
657: }
658: //rows
659: for (int i = 1; i <= nrows; i++) {
660: XModel rowNode = (XModel) tableNode.append(String
661: .valueOf(i));
662: rowNode.setTagName("tr");
663: rowNode.set("");
664: //ceils
665: for (int j = 1; j <= ncols; j++) {
666: XModel ceilNode = (XModel) rowNode.append(String
667: .valueOf(j));
668: ceilNode.setTagName("td");
669: ceilNode.set("");
670: }
671: }
672: currentProject.setModified(true);
673: }
674:
675: /**
676: * Deletes selected node from the model
677: * @param path the path of the model node
678: */
679: public void deleteNode(String path) {
680: if (path.length() > 0) {
681: int pos = path.lastIndexOf('/');
682: if (pos > 0) {
683: XModel model = (XModel) rootModel.get(path.substring(0,
684: pos));
685: if (model instanceof XBaseModel)
686: ((XBaseModel) model).removeChild(path
687: .substring(pos + 1));
688: else if (model instanceof VisualiserDebuggerModel)
689: ((VisualiserDebuggerModel) model).removeChild(path
690: .substring(pos + 1));
691: } else {
692: if (rootModel instanceof XBaseModel) {
693: ((XBaseModel) rootModel).removeChild(path);
694: } else if (rootModel instanceof VisualiserDebuggerModel) {
695: ((VisualiserDebuggerModel) rootModel)
696: .removeChild(path);
697: }
698: }
699: currentProject.setModified(true);
700: }
701: }
702:
703: /**
704: * Reset the node's ID
705: * @param path the path of the parent node
706: * @param name the name/ID of the new node
707: */
708: public void renameNode(String path, String name) {
709: if (path == null)
710: return;
711: XModel model = (XModel) rootModel.get(path);
712: model.setAttribValue(XBaseModel.ID_ATTRIBUTE, name);
713: currentProject.setModified(true);
714: }
715:
716: /**
717: * Reset the node's value
718: * @param path the path of the parent node
719: * @param name the name/ID of the new node
720: */
721: public void setNodeValue(String path, String attribName,
722: String value) {
723: if (path.length() > 0) {
724: XModel model = (XModel) rootModel.get(path);
725: int attribIdx = model.getAttribute(attribName);
726: model.setAttribValue(attribIdx, value);
727: if (model.getAttribValue(attribIdx) == value)
728: currentProject.setModified(true);
729: }
730: }
731:
732: public VisualiserDebuggerModel getDebuggerModel() {
733: return debuggerModel;
734: }
735:
736: public void setDebuggerModel(VisualiserDebuggerModel dModel) {
737: debuggerModel = dModel;
738: }
739:
740: public XModel getRootModel() {
741: return rootModel;
742: }
743:
744: public void setProjectPath(String ppath) {
745: projectPath = ppath;
746: }
747:
748: public String getProjectPath() {
749: return projectPath;
750: }
751:
752: /**
753: * This method gets called when a bound property is changed
754: */
755: public void propertyChange(PropertyChangeEvent evt) {
756: }
757:
758: class NewNodeDialog extends JDialog {
759: static final int BASEMODEL = 1;
760: static final int TABLEMODEL = 2;
761:
762: private int type;
763: private JPanel topPanel, centerPanel, bottomPanel;
764: private JPanel baseModelPanel, tableModelPanel;
765: private JTextField baseModelName;
766: private JSpinner spinnCols, spinnRows;
767: private JLabel tableNameLbl, singleNodeLbl;
768: private JTextField tableModelName;
769: private JComboBox typeCombo;
770: private JButton buttOk, buttCancel;
771: private String[] types = { "single node", "table model" };
772: private CardLayout cardLayout;
773: private boolean okPressed;
774: private String path;
775:
776: NewNodeDialog(JFrame parent) {
777: super (parent);
778: path = "";
779: okPressed = false;
780: type = BASEMODEL;
781: setModal(true);
782: setLayout(new BorderLayout());
783:
784: createBaseModelPanel();
785: createTableModelPanel();
786:
787: typeCombo = new JComboBox(types);
788: typeCombo.addActionListener(new ActionListener() {
789: public void actionPerformed(ActionEvent e) {
790: String type = typeCombo.getSelectedItem()
791: .toString();
792: if (type.equals("single node"))
793: showBaseModel();
794: else if (type.equals("table model"))
795: showTableModel();
796: }
797: });
798:
799: topPanel = new JPanel();
800: topPanel.setLayout(new BorderLayout());
801: TitledBorder tb1 = BorderFactory
802: .createTitledBorder("choose model type");
803: typeCombo.setBorder(tb1);
804: topPanel.add(typeCombo, BorderLayout.CENTER);
805: add(topPanel, BorderLayout.NORTH);
806:
807: cardLayout = new CardLayout();
808: centerPanel = new JPanel();
809: centerPanel.setLayout(cardLayout);
810: centerPanel.add(baseModelPanel, "baseModel");
811: centerPanel.add(tableModelPanel, "tableModel");
812: add(centerPanel, BorderLayout.CENTER);
813:
814: Dimension buttSize = new Dimension(100, 20);
815: buttCancel = new JButton("cancel");
816: buttCancel.setSize(buttSize);
817: buttCancel.setPreferredSize(buttSize);
818: buttCancel.addActionListener(new ActionListener() {
819: public void actionPerformed(ActionEvent e) {
820: okPressed = false;
821: setVisible(false);
822: }
823: });
824: buttOk = new JButton("ok");
825: buttOk.setSize(buttSize);
826: buttOk.setPreferredSize(buttSize);
827:
828: buttOk.addActionListener(new ActionListener() {
829: public void actionPerformed(ActionEvent e) {
830: if (modelIdSpecified()) {
831: okPressed = true;
832: setVisible(false);
833: } else {
834: JOptionPane.showMessageDialog(
835: NewNodeDialog.this ,
836: "Please enter the model ID");
837: }
838: }
839: });
840:
841: bottomPanel = new JPanel();
842: bottomPanel.setLayout(new FlowLayout());
843: bottomPanel.add(buttCancel);
844: bottomPanel.add(buttOk);
845: add(bottomPanel, BorderLayout.SOUTH);
846:
847: pack();
848: setLocation();
849: }
850:
851: private boolean modelIdSpecified() {
852: return ((type == BASEMODEL)
853: && !"".equals(baseModelName.getText()) || (type == TABLEMODEL)
854: && !"".equals(tableModelName));
855: }
856:
857: String getTableName() {
858: return tableModelName.getText();
859: }
860:
861: int getNumCols() {
862: return (new Integer(spinnCols.getValue().toString()))
863: .intValue();
864: }
865:
866: int getNumRows() {
867: return (new Integer(spinnRows.getValue().toString()))
868: .intValue();
869: }
870:
871: public String getSingleNodeName() {
872: return baseModelName.getText();
873: }
874:
875: public boolean showDialog(String p) {
876: path = p;
877: okPressed = false;
878: repaint();
879: pack();
880: setVisible(true);
881: return okPressed;
882: }
883:
884: int getType() {
885: return type;
886: }
887:
888: private void showBaseModel() {
889: type = BASEMODEL;
890: cardLayout.show(centerPanel, "baseModel");
891: }
892:
893: private void showTableModel() {
894: type = TABLEMODEL;
895: cardLayout.show(centerPanel, "tableModel");
896: }
897:
898: private void createBaseModelPanel() {
899: Dimension d = new Dimension(100, 20);
900: baseModelName = new JTextField();
901: baseModelName.setSize(d);
902: baseModelName.setPreferredSize(d);
903: baseModelName.setMaximumSize(d);
904: baseModelPanel = new JPanel();
905: baseModelPanel.setLayout(new FlowLayout());
906: TitledBorder tb = BorderFactory
907: .createTitledBorder("add node");
908: baseModelPanel.setBorder(tb);
909: singleNodeLbl = new JLabel("model id: ");
910: baseModelPanel.add(singleNodeLbl);
911: baseModelPanel.add(baseModelName);
912: pack();
913: repaint();
914: }
915:
916: private void createTableModelPanel() {
917: Dimension nameSize = new Dimension(100, 20);
918: Dimension spinnSize = new Dimension(30, 20);
919: tableModelPanel = new JPanel();
920: tableModelPanel.setLayout(new FlowLayout());
921: TitledBorder tb = BorderFactory
922: .createTitledBorder("add table");
923: tableModelPanel.setBorder(tb);
924: spinnCols = new JSpinner();
925: spinnCols.setSize(spinnSize);
926: spinnCols.setPreferredSize(spinnSize);
927: spinnRows = new JSpinner();
928: spinnRows.setSize(spinnSize);
929: spinnRows.setPreferredSize(spinnSize);
930: tableModelName = new JTextField();
931: tableModelName.setSize(nameSize);
932: tableModelName.setPreferredSize(nameSize);
933: tableModelName.setMinimumSize(nameSize);
934: tableNameLbl = new JLabel("model id: ");
935: tableModelPanel.add(tableNameLbl);
936: tableModelPanel.add(tableModelName);
937: tableModelPanel.add(new JLabel("columns"));
938: tableModelPanel.add(spinnCols);
939: tableModelPanel.add(new JLabel("rows"));
940: tableModelPanel.add(spinnRows);
941: pack();
942: repaint();
943: }
944:
945: private void setLocation() {
946: int px = ModelVisualiserPanel.this .getLocation().x;
947: int py = ModelVisualiserPanel.this .getLocation().y;
948: int pw = ModelVisualiserPanel.this .getSize().width;
949: int ph = ModelVisualiserPanel.this .getSize().height;
950: int w = getSize().width;
951: int h = getSize().height;
952: int x = px + (pw - w) / 2;
953: int y = py + (ph - h) / 2;
954: //setLocation( x, y );
955: setLocation(500, 720);
956: }
957:
958: }
959:
960: }
|