001: package com.xoetrope.carousel.services;
002:
003: import java.io.File;
004:
005: import java.awt.BorderLayout;
006: import java.awt.Component;
007: import java.awt.Container;
008: import java.awt.Dimension;
009: import javax.swing.BorderFactory;
010: import javax.swing.JPanel;
011: import javax.swing.JScrollPane;
012: import javax.swing.JSplitPane;
013: import javax.swing.JTree;
014: import javax.swing.border.EmptyBorder;
015: import javax.swing.tree.DefaultMutableTreeNode;
016: import javax.swing.tree.TreePath;
017: import net.xoetrope.editor.project.XEditorProject;
018: import net.xoetrope.editor.XEditorUtilities;
019: import com.xoetrope.carousel.services.dialog.InputDialog;
020: import com.xoetrope.carousel.services.events.RouteChangeListener;
021: import net.xoetrope.xml.XmlElement;
022: import net.xoetrope.editor.ui.KalideoscopeToolbar;
023: import net.xoetrope.xui.data.XBaseModel;
024:
025: /**
026: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
027: * the GNU Public License (GPL), please see license.txt for more details. If
028: * you make commercial use of this software you must purchase a commercial
029: * license from Xoetrope.</p>
030: * <p> $Revision: 1.13 $</p>
031: */
032: public class RouteEditor extends JPanel implements RouteChangeListener {
033: private XEditorProject currentProject;
034: private Container toolbar;
035: private JTree routesTree;
036: private JPanel editPanel;
037: private JSplitPane splitPane;
038: private JScrollPane treePane;
039: private DefaultMutableTreeNode topNode, clientNode, serverNode;
040: private RouteDataEditor routeDataEditor;
041: private RouteManager routeManager;
042:
043: public RouteEditor(XEditorProject proj) {
044: // ModelManager.getInstance();
045: setLayout(new BorderLayout());
046: currentProject = proj;
047:
048: //add( createToolbar(), BorderLayout.NORTH );
049: treePane = new JScrollPane(routesTree);
050: treePane.setBorder(BorderFactory.createEmptyBorder());
051: treePane.setViewportBorder(BorderFactory.createEmptyBorder());
052:
053: //Mode explorerMode = WindowManager.getDefault().findMode( "explorer" );
054: int preferredWidth = 200;
055: //if ( explorerMode != null )
056: // preferredWidth = explorerMode.getSelectedTopComponent().getWidth();
057: treePane.setPreferredSize(new Dimension(preferredWidth, 200));
058:
059: JPanel toolsPnl = new JPanel(new BorderLayout());
060: KalideoscopeToolbar visualizerToolbar = new KalideoscopeToolbar();
061: KalideoscopeToolbar layerToolbar = new KalideoscopeToolbar();
062: toolsPnl.add(visualizerToolbar, BorderLayout.NORTH);
063: toolsPnl.add(layerToolbar, BorderLayout.SOUTH);
064: routeDataEditor = new RouteDataEditor(currentProject, this ,
065: visualizerToolbar, layerToolbar);
066: routeDataEditor.setRouteChangeListener(this );
067:
068: routeManager = (RouteManager) currentProject
069: .getObject(ProjectRouteManager.ROUTE_MGR_LOOKUP);
070: //routeManager.loadRoutesFromFile( currentProject );
071: routeDataEditor.loadCustomRouteFile();
072: createTree();
073:
074: JPanel rightPane = new JPanel();
075: rightPane.setLayout(new BorderLayout());
076: rightPane.setBorder(BorderFactory.createEmptyBorder());
077:
078: rightPane.add(toolsPnl, BorderLayout.NORTH);
079: rightPane.add(routeDataEditor, BorderLayout.CENTER);
080:
081: splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
082: treePane, rightPane);
083: splitPane.setDividerLocation(0.3);
084: splitPane.setDividerSize(6);
085: splitPane.setBorder(BorderFactory.createEmptyBorder());
086: add(splitPane, BorderLayout.CENTER);
087: }
088:
089: private Component createToolbar() {
090: setLayout(new BorderLayout());
091: toolbar = new JPanel(new BorderLayout());
092: return toolbar;
093: }
094:
095: public void createTree() {
096: topNode = new DefaultMutableTreeNode("Routes");
097: routesTree = new JTree(topNode);
098: routesTree.setEditable(true);
099: routesTree.setBorder(new EmptyBorder(0, 0, 0, 0));
100:
101: treePane.setViewportView(routesTree);
102: routeDataEditor.setRouteTree(routesTree);
103:
104: buildTree(topNode, null);
105: expandRoot();
106: }
107:
108: private void expandRoot() {
109: TreePath path = new TreePath(topNode);
110: routesTree.expandPath(path);
111: routesTree.repaint();
112: }
113:
114: private void buildTree(DefaultMutableTreeNode parent, XmlElement ele) {
115: clientNode = new DefaultMutableTreeNode("Client");
116: serverNode = new DefaultMutableTreeNode("Server");
117: parent.add(clientNode);
118: parent.add(serverNode);
119: XBaseModel serverMdl = routeManager
120: .getRoutes(RouteDataEditor.SIDE_SERVER);
121: buildRouteTree(serverMdl, serverNode);
122: XBaseModel clientMdl = routeManager
123: .getRoutes(RouteDataEditor.SIDE_CLIENT);
124: buildRouteTree(clientMdl, clientNode);
125: }
126:
127: private void buildRouteTree(XBaseModel targetMdl,
128: DefaultMutableTreeNode targetNod) {
129: for (int i = 0; i < targetMdl.getNumChildren(); i++) {
130: XBaseModel child = (XBaseModel) targetMdl.get(i);
131: DefaultMutableTreeNode nodRoute = new DefaultMutableTreeNode(
132: child.getId());
133: targetNod.add(nodRoute);
134: for (int layer = 0; layer < child.getNumChildren(); layer++) {
135: XBaseModel layerMdl = (XBaseModel) child.get(layer);
136: DefaultMutableTreeNode nodLayer = new DefaultMutableTreeNode(
137: layerMdl.getId());
138: nodRoute.add(nodLayer);
139: }
140: }
141: }
142:
143: private ModelManager getModelManager() {
144: RouteManager routeMgr = (RouteManager) currentProject
145: .getObject(ProjectRouteManager.ROUTE_MGR_LOOKUP);
146: return routeMgr.getModelMgr();
147: }
148:
149: public void save() {
150: /*String filename = getModelManager().getRoutingFileName();
151: if ( filename != null ) {
152: routeManager.saveRoutesFile( currentProject, filename );
153: routeManager.saveCustomRouteDefs( currentProject, routeDataEditor.getCustomRouteFile() );
154: }*/
155: }
156:
157: public void setRoutesFile() {
158: String ret = InputDialog.showDialog(
159: "Please enter the name of the routes file...",
160: "Routes file", getModelManager().getRoutingFileName());
161: if (ret != null) {
162: getModelManager().setRoutingFileName(ret.toString());
163: XEditorUtilities.copyFile(currentProject, null,
164: "template_routes.xml", currentProject.getPath()
165: + File.separator + "resources"
166: + File.separator + ret, false);
167: }
168: }
169:
170: /**
171: * Refill the routes tree
172: */
173: public void routesUpdated() {
174: routesTree.removeAll();
175: buildTree(topNode, null);
176: }
177:
178: }
|