View Javadoc
1 package xdoclet.gui.swing; 2 3 import org.apache.commons.logging.LogFactory; 4 5 import xdoclet.beans.BeanContextSupportEx; 6 import xdoclet.beans.Invocation; 7 import xdoclet.gui.BeanContextTreeModel; 8 9 import javax.swing.JOptionPane; 10 import javax.swing.JPanel; 11 import javax.swing.JScrollPane; 12 import javax.swing.JTree; 13 import javax.swing.tree.TreePath; 14 import javax.swing.event.*; 15 16 import java.awt.CardLayout; 17 import java.awt.GridLayout; 18 19 import java.util.Collection; 20 import java.util.HashMap; 21 import java.util.Map; 22 23 /*** 24 * Configuration panel for XDoclet. This panel allows the same kind of 25 * configuration that is possible to do in a classical Ant script. 26 * 27 * @author <a href="mailto:aslak.hellesoy at bekk.no">Aslak Hellesøy</a> 28 * @version $Revision: 1.7 $ 29 */ 30 public class BeanContextConfigurationPanel extends JPanel { 31 private final BeanContextSupportEx _xdocletContainer; 32 private final CardLayout _cards = new CardLayout(); 33 private final JPanel _propertySheets = new JPanel(_cards); 34 private final Map _propertySheetMap = new HashMap(); 35 private Object _bean = null; 36 37 /*** 38 * @param xdocletContainer a container for multiple XDoclet instances. 39 */ 40 public BeanContextConfigurationPanel(BeanContextSupportEx xdocletContainer) { 41 _xdocletContainer = xdocletContainer; 42 43 setLayout(new GridLayout(1, 0)); 44 45 BeanContextTreeModel treeModel = new BeanContextTreeModel(_xdocletContainer); 46 final JTree tree = new JTree(treeModel); 47 48 // Add a renderer that renders nodes nicely. 49 tree.setCellRenderer(new BeanTreeCellRenderer()); 50 51 // Add a listener that listens for tree selections and 52 // displays appropriate property sheets. 53 tree.addTreeSelectionListener(new TreeSelectionListener() { 54 /*** 55 * When a node (bean) is selected we want to display editors for 56 * all properties. We're using BeanInfo to figure out what editors 57 * to display. 58 * 59 * @param e 60 */ 61 public void valueChanged(TreeSelectionEvent e) { 62 _bean = e.getPath().getLastPathComponent(); 63 64 // Show the property sheet for that bean 65 PropertySheet propertySheet = getPropertySheet(_bean); 66 67 showPropertySheet(propertySheet); 68 69 // Find the methods that can create new elements. These 70 // should either be non-argument methods, or methods 71 // taking arguments where all allowed argument values are known. 72 // Collection invocations = getInvocations(); 73 } 74 }); 75 76 // Automatically select new nodes. Expansion is handled automatically. 77 treeModel.addTreeModelListener( new TreeModelListener() { 78 public void treeNodesChanged(TreeModelEvent tme) { 79 } 80 81 public void treeNodesInserted(final TreeModelEvent tme) { 82 // There will only be one inserted child 83 Object child = tme.getChildren()[0]; 84 final TreePath childPath = tme.getTreePath().pathByAddingChild( child ); 85 tree.getSelectionModel().setSelectionPath(childPath); 86 } 87 88 public void treeNodesRemoved(TreeModelEvent e) { 89 } 90 91 public void treeStructureChanged(TreeModelEvent e) { 92 } 93 }); 94 95 JScrollPane scroll = new JScrollPane(tree); 96 97 add(scroll); 98 99 add(_propertySheets); 100 } 101 102 public void createChildInSelectedNode() { 103 if ( _bean instanceof BeanContextSupportEx ) { 104 try { 105 BeanContextSupportEx bean = (BeanContextSupportEx) _bean; 106 // Create a collection of invocations to display 107 Collection invocations = bean.getInvocations(); 108 Invocation invocation = (Invocation) JOptionPane.showInputDialog(this, "New Element", "New Element", 109 JOptionPane.QUESTION_MESSAGE, null, invocations.toArray(), null); 110 111 if (invocation != null) { 112 invocation.invoke(); 113 } 114 } catch (Exception e) { 115 LogFactory.getLog(BeanContextConfigurationPanel.class).error(e.getMessage(), e); 116 JOptionPane.showMessageDialog(this, "Failed to create element", "Error", JOptionPane.ERROR_MESSAGE); 117 } 118 } else { 119 JOptionPane.showMessageDialog(this, "Can't create subelement here", "Error", JOptionPane.INFORMATION_MESSAGE); 120 } 121 } 122 123 /*** 124 * Callback method from PropertySheetCommander. Called when a TreeNode 125 * is selected and no PropertySheet has yet been added for this node. 126 * 127 * @param propertySheet 128 */ 129 private void addPropertySheet(PropertySheet propertySheet) { 130 // JOptionPane.showMessageDialog(null, "Adding propertySheet: " + propertySheet.getName() ); 131 _propertySheets.add(propertySheet.getName(), propertySheet); 132 } 133 134 private void showPropertySheet(PropertySheet propertySheet) { 135 // JOptionPane.showMessageDialog(null, "Showing propertySheet: " + propertySheet.getName() ); 136 _cards.show(_propertySheets, propertySheet.getName()); 137 } 138 139 /*** 140 * Gets a PropertySheet for a Bean, and lazily creates it if it doesn't 141 * exist. 142 * @param bean 143 * @return 144 */ 145 private PropertySheet getPropertySheet(Object bean) { 146 PropertySheet propertySheet = (PropertySheet) _propertySheetMap.get(bean); 147 148 if (propertySheet == null) { 149 propertySheet = new PropertySheet(bean); 150 _propertySheetMap.put(bean, propertySheet); 151 addPropertySheet(propertySheet); 152 } 153 154 return propertySheet; 155 } 156 }

This page was automatically generated by Maven