001: package com.xoetrope.carousel.services;
002:
003: import com.xoetrope.carousel.services.IServiceEditor;
004:
005: import java.awt.BorderLayout;
006: import java.awt.Color;
007: import java.awt.Dimension;
008: import java.awt.event.ActionEvent;
009: import java.awt.event.ActionListener;
010: import java.awt.event.ItemEvent;
011: import java.awt.event.ItemListener;
012: import java.awt.event.MouseEvent;
013: import java.awt.event.MouseListener;
014: import java.util.EventObject;
015:
016: import javax.swing.BorderFactory;
017: import javax.swing.DefaultCellEditor;
018: import javax.swing.JButton;
019: import javax.swing.JComboBox;
020: import javax.swing.JMenuItem;
021: import javax.swing.JPanel;
022: import javax.swing.JPopupMenu;
023: import javax.swing.JScrollPane;
024: import javax.swing.JTable;
025: import javax.swing.JTree;
026: import javax.swing.JTextField;
027: import javax.swing.event.TableModelEvent;
028: import javax.swing.event.TableModelListener;
029: import javax.swing.event.TreeModelEvent;
030: import javax.swing.event.TreeModelListener;
031: import javax.swing.event.TreeSelectionEvent;
032: import javax.swing.event.TreeSelectionListener;
033: import javax.swing.table.DefaultTableModel;
034: import javax.swing.table.TableColumn;
035: import javax.swing.tree.DefaultMutableTreeNode;
036: import javax.swing.tree.DefaultTreeModel;
037: import javax.swing.tree.TreePath;
038: import javax.swing.tree.DefaultTreeCellEditor;
039: import javax.swing.tree.DefaultTreeCellRenderer;
040: import javax.swing.tree.TreeCellEditor;
041:
042: import net.xoetrope.xui.XProjectManager;
043: import net.xoetrope.editor.project.XEditorProject;
044:
045: import net.xoetrope.editor.ui.KalideoscopeToolbar;
046: import javax.swing.JOptionPane;
047: import net.xoetrope.xui.data.XBaseModel;
048:
049: /**
050: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
051: * the GNU Public License (GPL), please see license.txt for more details. If
052: * you make commercial use of this software you must purchase a commercial
053: * license from Xoetrope.</p>
054: * <p> $Revision: 1.12 $</p>
055: */
056: public class ServiceDataEditor extends JPanel implements
057: ActionListener, TreeSelectionListener, ItemListener,
058: MouseListener, TreeModelListener, TableModelListener {
059: private JTree servicesTree;
060: private String selectedServiceName;
061: private JPopupMenu mnuServices, mnuDelete;
062: private JPanel pnlList;
063: private DefaultTableModel listTableModel;
064: private JTable tblListItems;
065: private JButton btnNewListItem, btnRemoveListItem,
066: btnRenameListItem;
067: private JComboBox routesCombo;
068: //private JTextField serverEdit;
069:
070: public static final String MNU_NEW_SERVICE = "New service";
071: public static final String MNU_DELETE_SERVICE = "Delete service";
072:
073: private KalideoscopeToolbar servicesToolbar;
074: private RouteManager routeManager;
075: private XEditorProject currentProject;
076: private IServiceEditor serviceEditor;
077:
078: public static final int LEVEL_BASE = 1;
079: public static final int LEVEL_SPLIT = 2;
080: public static final int LEVEL_SERVICE = 3;
081:
082: private int selectedSide = 0;
083:
084: private boolean routesLoading = false;
085:
086: public ServiceDataEditor(IServiceEditor editor, XEditorProject proj) {
087: super (new BorderLayout());
088: currentProject = proj;
089: routeManager = (RouteManager) RouteManager
090: .setupRouteManager(XProjectManager.getCurrentProject());
091: routeManager.loadRoutesFromFile(currentProject);
092: serviceEditor = editor;
093: createPopups();
094: add(createArgsListPanel(), BorderLayout.CENTER);
095: setToolbarEnabled(LEVEL_BASE);
096: }
097:
098: public void setServiceTree(JTree tree) {
099: servicesTree = tree;
100: servicesTree.getModel().addTreeModelListener(this );
101: servicesTree.addTreeSelectionListener(this );
102: servicesTree.addMouseListener(this );
103: CustomCellEditor cedit = new CustomCellEditor(
104: servicesTree,
105: (DefaultTreeCellRenderer) servicesTree
106: .getCellRenderer(),
107: (TreeCellEditor) new DefaultCellEditor(new JTextField()));
108: servicesTree.setCellEditor(cedit);
109: }
110:
111: private JPanel createArgsListPanel() {
112: pnlList = new JPanel(new BorderLayout());
113: // JPanel tempPanel = new JPanel( new GridLayout(3, 1) );
114:
115: listTableModel = new DefaultTableModel();
116: listTableModel.addColumn("Name");
117: listTableModel.addColumn("Usage");
118: listTableModel.addColumn("Type");
119: listTableModel.addColumn("Mandatory");
120: tblListItems = new JTable(listTableModel);
121: tblListItems.setBackground(Color.white);
122: tblListItems.getModel().addTableModelListener(this );
123: tblListItems.setBorder(BorderFactory.createEmptyBorder());
124:
125: TableColumn usageColumn = tblListItems.getColumnModel()
126: .getColumn(1);
127: JComboBox usageCombo = new JComboBox();
128: usageCombo.addItem("input");
129: usageCombo.addItem("output");
130: usageCombo.setEnabled(false);
131: usageColumn.setCellEditor(new DefaultCellEditor(usageCombo));
132:
133: TableColumn typeColumn = tblListItems.getColumnModel()
134: .getColumn(2);
135: JComboBox typeCombo = new JComboBox();
136: typeCombo.addItem("double");
137: typeCombo.addItem("int");
138: typeCombo.addItem("String");
139: typeColumn.setCellEditor(new DefaultCellEditor(typeCombo));
140:
141: TableColumn mandatoryColumn = tblListItems.getColumnModel()
142: .getColumn(3);
143: JComboBox mandatoryCombo = new JComboBox();
144: mandatoryCombo.addItem("true");
145: mandatoryCombo.addItem("false");
146: mandatoryColumn.setCellEditor(new DefaultCellEditor(
147: mandatoryCombo));
148:
149: JScrollPane listPane = new JScrollPane(tblListItems);
150: listPane.setBorder(BorderFactory.createEmptyBorder());
151:
152: servicesToolbar = new KalideoscopeToolbar();
153:
154: servicesToolbar.addTool(this , "refresh.gif", "Refresh",
155: "Refresh this view of the services");
156: servicesToolbar.addSeparator();
157: btnNewListItem = servicesToolbar.addTool(this ,
158: "newService.gif", MNU_NEW_SERVICE, "Add a new service");
159: btnRemoveListItem = servicesToolbar.addTool(this ,
160: "removeService.gif", MNU_DELETE_SERVICE,
161: "Remove the selected service");
162: servicesToolbar.addSeparator();
163: btnRenameListItem = servicesToolbar.addTool(this ,
164: "renameService.gif", "Rename",
165: "Rename the selected service");
166: servicesToolbar.addSeparator();
167: servicesToolbar.addLabel("Route ");
168: routesCombo = servicesToolbar
169: .addComboTool(this ,
170: "Select the route over which this service will be invoked");
171: routesCombo.addItemListener(this );
172: servicesToolbar.addSeparator();
173: //servicesToolbar.addLabel( "Server " );
174: //serverEdit = servicesToolbar.addEditTool( this, "Select the server end-point class that implments this service" );
175: //serverEdit.setMaximumSize( new Dimension( 400, 20 ));
176: //serverEdit.setPreferredSize( new Dimension( 300, 20 ));
177: routesCombo.setMaximumSize(new Dimension(250, 20));
178: routesCombo.setPreferredSize(new Dimension(250, 20));
179: addRoutes();
180:
181: pnlList.add(servicesToolbar, BorderLayout.NORTH);
182: //pnlList.add( listPane, BorderLayout.CENTER );
183: pnlList.setVisible(true);
184: return pnlList;
185: }
186:
187: private void createPopups() {
188: mnuServices = new JPopupMenu();
189: mnuServices.add(createMenuItem(MNU_NEW_SERVICE));
190:
191: mnuDelete = new JPopupMenu();
192: mnuDelete.add(createMenuItem(MNU_DELETE_SERVICE));
193: }
194:
195: private JMenuItem createMenuItem(String caption) {
196: JMenuItem item = new JMenuItem(caption);
197: item.addActionListener(this );
198: return item;
199: }
200:
201: private ServiceManager getServiceManager() {
202: return (ServiceManager) currentProject
203: .getObject(ProjectRouteManager.SERVICE_MGR_LOOKUP);
204: }
205:
206: private void setProjectModified() {
207: currentProject.setModified(true);
208: }
209:
210: public void actionPerformed(ActionEvent evt) {
211: if (evt.getActionCommand().compareTo(MNU_NEW_SERVICE) == 0) {
212: getServiceManager().addService("new service", selectedSide);
213: insertChildNode("new service");
214: setProjectModified();
215: } else if (evt.getActionCommand().compareTo(MNU_DELETE_SERVICE) == 0) {
216: getServiceManager().removeService(selectedServiceName,
217: selectedSide);
218: selectedServiceName = null;
219: serviceEditor.createTree();
220: setProjectModified();
221: setToolbarEnabled(LEVEL_BASE);
222: } else if (evt.getActionCommand().compareTo("Refresh") == 0) {
223: selectedServiceName = null;
224: serviceEditor.createTree();
225: } else if (evt.getSource() == btnNewListItem) {
226: String[] rowData = { "", "", "" };
227: listTableModel.addRow(rowData);
228: setProjectModified();
229: } else if (evt.getSource() == btnRenameListItem) {
230: String name = JOptionPane.showInputDialog(this ,
231: "Rename the service: " + selectedServiceName,
232: "Enter the new service name",
233: JOptionPane.OK_CANCEL_OPTION);
234: if (name != null) {
235: renameService(selectedServiceName, name);
236: selectedServiceName = name;
237: }
238: } else if (evt.getSource() == btnRemoveListItem) {
239: listTableModel.removeRow(tblListItems.getSelectedRow());
240: setProjectModified();
241: }
242: }
243:
244: private void getSide() {
245: TreePath path = servicesTree.getSelectionPath();
246: String side = path.getLastPathComponent().toString();
247: while ((side.compareTo("Client") != 0)
248: && (side.compareTo("Server") != 0)) {
249: path = path.getParentPath();
250: side = path.getLastPathComponent().toString();
251: }
252: if (side.compareTo("Client") == 0) {
253: selectedSide = ServiceManager.SIDE_CLIENT;
254: } else {
255: selectedSide = ServiceManager.SIDE_SERVER;
256: }
257: }
258:
259: private void insertChildNode(String text) {
260: DefaultTreeModel treemodel = (DefaultTreeModel) servicesTree
261: .getModel();
262: DefaultMutableTreeNode nodParent = (DefaultMutableTreeNode) servicesTree
263: .getSelectionPath().getLastPathComponent();
264: DefaultMutableTreeNode nodChild = new DefaultMutableTreeNode(
265: text);
266: treemodel.insertNodeInto(nodChild, nodParent, nodParent
267: .getChildCount());
268: XBaseModel newMdl = (XBaseModel) getServiceManager()
269: .getServices(selectedSide).get(text);
270: newMdl.setTagName("service");
271: }
272:
273: public void valueChanged(TreeSelectionEvent evt) {
274: TreePath[] path = evt.getPaths();
275: routesLoading = true;
276: clearRoutes();
277: switch (evt.getPath().getPathCount()) {
278: case LEVEL_SPLIT:
279: getSide();
280: break;
281: case LEVEL_SERVICE:
282: getSide();
283: addRoutes();
284: routesLoading = false;
285: selectedServiceName = evt.getPath().getLastPathComponent()
286: .toString();
287: routesCombo.setSelectedItem(getServiceManager()
288: .getRouteName(selectedServiceName, selectedSide));
289: break;
290: }
291: setToolbarEnabled(evt.getPath().getPathCount());
292: }
293:
294: private void setToolbarEnabled(int level) {
295: btnNewListItem.setEnabled(false);
296: btnRemoveListItem.setEnabled(false);
297: routesCombo.setEnabled(false);
298: btnRenameListItem.setEnabled(false);
299: switch (level) {
300: case LEVEL_BASE:
301: break;
302: case LEVEL_SPLIT:
303: btnNewListItem.setEnabled(true);
304: break;
305: case LEVEL_SERVICE:
306: btnRemoveListItem.setEnabled(true);
307: routesCombo.setEnabled(true);
308: btnRenameListItem.setEnabled(true);
309: break;
310: }
311: }
312:
313: public void itemStateChanged(ItemEvent evt) {
314: if (selectedServiceName != null && (!routesLoading)) {
315: getServiceManager().setRouteName(selectedServiceName,
316: (String) routesCombo.getSelectedItem(),
317: selectedSide);
318: setProjectModified();
319: }
320: }
321:
322: public void mouseExited(MouseEvent evt) {
323: }
324:
325: public void mouseEntered(MouseEvent evt) {
326: }
327:
328: public void mouseReleased(MouseEvent evt) {
329: }
330:
331: public void mouseClicked(MouseEvent evt) {
332: }
333:
334: public void mousePressed(MouseEvent evt) {
335: if ((evt.getButton() == evt.BUTTON3)
336: && (servicesTree.getSelectionPath() != null)) {
337: switch (servicesTree.getSelectionPath().getPathCount()) {
338: case LEVEL_SPLIT:
339: mnuServices.show(servicesTree, evt.getX(), evt.getY());
340: break;
341: case LEVEL_SERVICE:
342: mnuDelete.show(servicesTree, evt.getX(), evt.getY());
343: break;
344: }
345: }
346: }
347:
348: public void treeStructureChanged(TreeModelEvent evt) {
349: }
350:
351: public void treeNodesRemoved(TreeModelEvent evt) {
352: }
353:
354: public void treeNodesInserted(TreeModelEvent evt) {
355: }
356:
357: public void treeNodesChanged(TreeModelEvent evt) {
358: if (evt.getChildren() == null) {
359: } else {
360: String currentNode = evt.getChildren()[0].toString();
361: switch (servicesTree.getSelectionPath().getPathCount()) {
362: case LEVEL_SERVICE:
363: renameService(selectedServiceName, currentNode);
364: selectedServiceName = currentNode;
365: break;
366: }
367: }
368: }
369:
370: public void tableChanged(TableModelEvent evt) {
371: saveList();
372: }
373:
374: /**
375: * Fill the table with details of the sevice.
376: */
377: private void populateList() {
378: listTableModel.removeTableModelListener(this );
379: for (int i = listTableModel.getRowCount() - 1; i >= 0; i--)
380: listTableModel.removeRow(i);
381:
382: /*Hashtable table = getServiceManager().getServices();
383: XmlElement eleService = ( XmlElement )table.get( selectedServiceName );
384: routesCombo.setSelectedItem( eleService.getAttribute( "route" ));
385: Enumeration e = eleService.getChildren().elements();
386: while ( e.hasMoreElements() ) {
387: XmlElement arg = ( XmlElement )e.nextElement();
388: String isMandatory = arg.getAttribute( "mandatory" );
389: if (( isMandatory == null ) || ( isMandatory.length() == 0 ))
390: isMandatory = "false";
391: String usage = "input";
392: String name = arg.getAttribute( "name" );
393: if ( arg.getName ().equals( "return" )) {
394: usage = "output";
395: name = "-";
396: isMandatory = "true";
397: }
398: String[] row = { name, usage, arg.getAttribute( "type" ), isMandatory };
399: listTableModel.addRow( row );
400: }*/
401: XBaseModel mdl = getServiceManager().getServices(
402: ServiceManager.SIDE_CLIENT);
403: for (int i = 0; i < mdl.getNumChildren(); i++) {
404: XBaseModel child = (XBaseModel) mdl.get(i);
405: }
406: listTableModel.addTableModelListener(this );
407: }
408:
409: /**
410: * Saves the argument list for a service in the services hashtable in the
411: * XLibPlugServiceManager.
412: */
413: private void saveList() {
414: /*Hashtable table = getServiceManager().getServices();
415: XmlElement eleService = ( XmlElement )table.get( selectedServiceName );
416:
417: NanoXmlElement newService = new NanoXmlElement( "service" );
418: newService.setAttribute( "name", eleService.getAttribute( "name" ) );
419:
420: for ( int i = 0; i < tblListItems.getModel().getRowCount(); i++ ) {
421: NanoXmlElement arg = new NanoXmlElement( "arg" );
422: arg.setAttribute( "name", tblListItems.getModel().getValueAt( i, 0 ).toString() );
423: String usage= tblListItems.getModel().getValueAt( i, 1 ).toString();
424: arg.setAttribute( "type", tblListItems.getModel().getValueAt( i, 2 ).toString() );
425: String mandatory = tblListItems.getModel().getValueAt( i, 3 ).toString();
426: if ( mandatory.equals( "true" ) && !usage.equals( "output" ))
427: arg.setAttribute( "mandatory", "true" );
428: newService.addChild( arg );
429: }*/
430:
431: //table.put( selectedServiceName, newService );
432: }
433:
434: /**
435: * Rename a service
436: */
437: private void renameService(String selectedService, String newName) {
438: XBaseModel servicesMdl = getServiceManager().getServices(
439: selectedSide);
440: XBaseModel selectedMdl = (XBaseModel) servicesMdl
441: .get(selectedService);
442: selectedMdl.setId(newName);
443: serviceEditor.createTree();
444: setProjectModified();
445: /*XmlElement ele = ( XmlElement )servicesTable.remove( selectedService );
446: ele.setAttribute( "id", newName );
447: servicesTable.put( newName, ele );
448: selectedServiceName = newName;
449: DefaultMutableTreeNode nod = ( DefaultMutableTreeNode )servicesTree.getSelectionPath().getLastPathComponent();
450: serviceEditor.createTree();
451: */
452: }
453:
454: /**
455: * Populate the list of routes
456: */
457: private void addRoutes() {
458: XBaseModel routesMdl = routeManager.getRoutes(selectedSide);
459: for (int i = 0; i < routesMdl.getNumChildren(); i++) {
460: XBaseModel child = (XBaseModel) routesMdl.get(i);
461: routesCombo.addItem(child.getId());
462: }
463: }
464:
465: private void clearRoutes() {
466: routesCombo.removeAllItems();
467: }
468:
469: class CustomCellEditor extends DefaultTreeCellEditor {
470:
471: public CustomCellEditor(JTree tree,
472: DefaultTreeCellRenderer renderer, TreeCellEditor editor) {
473: super (tree, renderer, editor);
474: }
475:
476: protected boolean canEditImmediately(EventObject event) {
477: int level = servicesTree.getSelectionPath().getPathCount();
478: if (level == LEVEL_SERVICE)
479: return true;
480: else
481: return false;
482: }
483: }
484:
485: }
|