1 26 27 package org.objectweb.util.explorer.swing.gui.lib; 28 29 30 import javax.swing.JScrollPane ; 31 import javax.swing.JTextField ; 32 import javax.swing.JLabel ; 33 import javax.swing.JPanel ; 34 import javax.swing.JButton ; 35 import javax.swing.AbstractAction ; 36 import javax.swing.Box ; 37 import javax.swing.SwingConstants ; 38 import javax.swing.JOptionPane ; 39 40 import org.objectweb.fractal.api.NoSuchInterfaceException; 41 import org.objectweb.util.explorer.api.Entry; 42 import org.objectweb.util.explorer.api.Tree; 43 import org.objectweb.util.explorer.swing.api.Explorer; 44 import org.objectweb.util.explorer.swing.gui.api.TreeChooserObserver; 45 import org.objectweb.util.explorer.swing.gui.api.ValidateReport; 46 47 import java.awt.Component ; 48 import java.awt.Dimension ; 49 import java.awt.GridLayout ; 50 51 57 public class TreeChooserBox 58 extends AbstractElementBox { 59 60 66 67 protected String label_; 68 69 70 protected JButton browseButton_; 71 72 73 protected org.objectweb.fractal.api.Component component_; 74 75 76 protected Tree treeItf_; 77 78 79 protected Explorer explorerItf_; 80 81 82 protected JTextField textField_; 83 84 85 protected Object object_; 86 87 88 protected TreeChooserObserver treeChooserObserver_ = null; 89 90 96 101 public TreeChooserBox(String label, org.objectweb.fractal.api.Component treeComponent) { 102 this(label,treeComponent,true); 103 } 104 105 111 public TreeChooserBox(String label, org.objectweb.fractal.api.Component treeComponent, boolean isMandatory) { 112 super(isMandatory); 113 label_ = label; 114 115 initAttributes(treeComponent); 116 117 if(explorerItf_!=null){ 118 explorerItf_.setPopupEnabled(false); 119 explorerItf_.setDragAndDropEnabled(false); 120 } 121 add(Box.createHorizontalGlue()); 122 JLabel explorerLabel = new JLabel (label + ": ", SwingConstants.RIGHT); 123 explorerLabel.setAlignmentX(Component.RIGHT_ALIGNMENT); 124 explorerLabel.setAlignmentY(Component.CENTER_ALIGNMENT); 125 add(explorerLabel); 126 add(Box.createHorizontalStrut(5)); 127 textField_ = new JTextField (); 128 textField_.setEditable(false); 129 textField_.setPreferredSize(new Dimension (200, 20)); 130 textField_.setMaximumSize(new Dimension (200, 20)); 131 add(textField_); 132 add(Box.createHorizontalStrut(5)); 133 browseButton_ = new JButton (new BrowseAction(this)); 134 browseButton_.setPreferredSize(new Dimension (20, 20)); 135 browseButton_.setMaximumSize(new Dimension (20, 20)); 136 add(browseButton_); 137 } 138 139 145 protected void initAttributes(org.objectweb.fractal.api.Component treeComponent){ 146 component_ = treeComponent; 147 if(component_!=null){ 148 try { 149 treeItf_ = (Tree)component_.getFcInterface(Tree.TREE); 150 explorerItf_ = (Explorer)component_.getFcInterface(Explorer.EXPLORER); 151 } catch (NoSuchInterfaceException e) { 152 e.printStackTrace(); 153 } 154 } 155 } 156 157 163 167 public ValidateReport validateBox(){ 168 if(isMandatory_){ 169 if(getObject()==null) 170 return new DefaultValidateReport(false,"The \"" + label_ + "\" value is mandatory"); 171 } 172 return new DefaultValidateReport(); 173 } 174 175 178 public void preInitialize(){ 179 if(explorerItf_!=null){ 180 explorerItf_.setPopupEnabled(false); 181 explorerItf_.setDragAndDropEnabled(false); 182 } 183 } 184 185 188 public void postInitialize(){ 189 if(explorerItf_!=null){ 190 explorerItf_.setPopupEnabled(true); 191 explorerItf_.setDragAndDropEnabled(true); 192 } 193 } 194 195 201 202 public Object getObject() { 203 return object_; 204 } 205 206 207 public void setTreeChooserObserver(TreeChooserObserver treeChooserObserver) { 208 treeChooserObserver_ = treeChooserObserver; 209 } 210 211 214 protected class BrowseAction extends AbstractAction { 215 216 217 protected Component parent_; 218 219 222 protected BrowseAction(Component parent) { 223 super("...", null); 224 parent_ = parent; 225 } 226 227 public void actionPerformed(java.awt.event.ActionEvent ae) { 228 if(treeChooserObserver_ != null) { 229 initAttributes(treeChooserObserver_.refresh()); 230 explorerItf_.setPopupEnabled(false); 231 } 232 JPanel treePanel = new JPanel (); 233 treePanel.setLayout(new GridLayout (1, 0)); 234 treePanel.setPreferredSize(new Dimension (450, 350)); 235 treePanel.add(new JScrollPane (explorerItf_.getTree())); 236 int result = JOptionPane.showOptionDialog((Component )ae.getSource(), treePanel, "Select an object", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null); 237 if (result == 0) { 238 Entry entry = treeItf_.getSelectedEntry(); 239 if (entry != null) { 240 Object o = entry.getValue(); 241 if (o != null) { 242 object_ = o; 243 textField_.setText(entry.getName().toString()); 244 } 245 } 246 247 } 248 } 249 250 } 251 252 } 253 | Popular Tags |