001: package com.xoetrope.carousel.services;
002:
003: import com.xoetrope.carousel.services.*;
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.tree.DefaultMutableTreeNode;
015: import javax.swing.tree.TreePath;
016:
017: import net.xoetrope.editor.project.XEditorProject;
018: import net.xoetrope.xml.XmlElement;
019: import net.xoetrope.xui.data.XBaseModel;
020: import net.xoetrope.xui.helper.Constants;
021:
022: /**
023: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
024: * the GNU Public License (GPL), please see license.txt for more details. If
025: * you make commercial use of this software you must purchase a commercial
026: * license from Xoetrope.</p>
027: * <p> $Revision: 1.6 $</p>
028: */
029: public class ServiceEditor extends JPanel implements IServiceEditor {
030: private XEditorProject currentProject;
031: private Container toolbar;
032: private JTree servicesTree;
033: private JPanel editPanel;
034: private JSplitPane splitPane;
035: private JScrollPane servicesPane;
036: private DefaultMutableTreeNode topNode, clientNode, serverNode;
037: private ServiceDataEditor serviceDataEditor;
038:
039: public ServiceEditor(XEditorProject proj) {
040: currentProject = proj;
041: //setName( "Services (" + currentProject.getProjectTitle() + ")" );
042:
043: add(createToolbar(), BorderLayout.NORTH);
044:
045: servicesPane = new JScrollPane(servicesTree);
046: servicesPane.setBorder(BorderFactory.createEmptyBorder());
047: servicesPane.setViewportBorder(BorderFactory
048: .createEmptyBorder());
049: int preferredWidth = 200;
050: // Mode explorerMode = WindowManager.getDefault().findMode( "explorer" );
051: //if ( explorerMode != null )
052: // preferredWidth = explorerMode.getSelectedTopComponent().getWidth();
053: servicesPane
054: .setPreferredSize(new Dimension(preferredWidth, 200));
055: serviceDataEditor = new ServiceDataEditor(this , proj);
056: getServiceManager().loadServicesFromFile(currentProject);
057: createTree();
058: editPanel = new JPanel(new BorderLayout());
059: editPanel.setBorder(BorderFactory.createEmptyBorder());
060: editPanel.add(serviceDataEditor);
061: splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
062: servicesPane, editPanel);
063: splitPane.setDividerSize(6);
064: splitPane.setDividerLocation(0.3);
065: splitPane.setBorder(BorderFactory.createEmptyBorder());
066: add(splitPane, BorderLayout.CENTER);
067: }
068:
069: private ServiceManager getServiceManager() {
070: return (ServiceManager) currentProject
071: .getObject(ProjectRouteManager.SERVICE_MGR_LOOKUP);
072: }
073:
074: private Component createToolbar() {
075: setLayout(new BorderLayout());
076: toolbar = new JPanel(new BorderLayout());
077: return toolbar;
078: }
079:
080: public void createTree() {
081: topNode = new DefaultMutableTreeNode(Constants.SERVICES);
082: servicesTree = new JTree(topNode);
083: servicesTree.setEditable(true);
084: servicesTree.setBorder(BorderFactory.createEmptyBorder());
085:
086: servicesPane.setViewportView(servicesTree);
087: serviceDataEditor.setServiceTree(servicesTree);
088:
089: buildTree(topNode, null);
090: expandRoot();
091: }
092:
093: private void buildTree(DefaultMutableTreeNode parent, XmlElement ele) {
094: clientNode = new DefaultMutableTreeNode(Constants.CLIENT);
095: serverNode = new DefaultMutableTreeNode(Constants.SERVER);
096: parent.add(clientNode);
097: parent.add(serverNode);
098:
099: XBaseModel serviceMdl = getServiceManager().getServices(
100: ServiceManager.SIDE_CLIENT);
101: buildServiceTree(clientNode, serviceMdl);
102:
103: serviceMdl = getServiceManager().getServices(
104: ServiceManager.SIDE_SERVER);
105: buildServiceTree(serverNode, serviceMdl);
106: }
107:
108: private void buildServiceTree(DefaultMutableTreeNode parentNode,
109: XBaseModel serviceMdl) {
110: for (int i = 0; i < serviceMdl.getNumChildren(); i++) {
111: XBaseModel child = (XBaseModel) serviceMdl.get(i);
112: DefaultMutableTreeNode nodService = new DefaultMutableTreeNode(
113: child.getId());
114: parentNode.add(nodService);
115: }
116: }
117:
118: public void save() {
119: String filename = getModelManager().getServicesFileName();
120: if (filename != null)
121: getServiceManager().saveServices(currentProject);
122: }
123:
124: private ModelManager getModelManager() {
125: RouteManager routeMgr = (RouteManager) currentProject
126: .getObject(ProjectRouteManager.ROUTE_MGR_LOOKUP);
127: return routeMgr.getModelMgr();
128: }
129:
130: private void expandRoot() {
131: TreePath path = new TreePath(topNode);
132: servicesTree.expandPath(path);
133: servicesTree.repaint();
134: }
135:
136: /*public String preferredId()
137: {
138: return "XuiProServiceEditor";
139: }*/
140:
141: /**
142: * @todo When the class has stabilized remove this method and retest the persistence
143: * @return
144: */
145: /*public int getPersistenceType()
146: {
147: return PERSISTENCE_NEVER;
148: }*/
149: }
|