1 26 27 package org.objectweb.util.browser.gui.lib; 28 29 30 import org.objectweb.util.browser.api.Entry; 31 import org.objectweb.util.browser.api.Wrapper; 32 import org.objectweb.util.browser.gui.api.TreeChooserObserver; 33 import org.objectweb.util.browser.gui.api.ValidateReport; 34 import org.objectweb.util.browser.core.common.DynamicTree; 35 36 37 import javax.swing.JScrollPane ; 38 import javax.swing.JTextField ; 39 import javax.swing.JLabel ; 40 import javax.swing.JPanel ; 41 import javax.swing.JButton ; 42 import javax.swing.AbstractAction ; 43 import javax.swing.Box ; 44 import javax.swing.SwingConstants ; 45 import javax.swing.JOptionPane ; 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 DynamicTree tree_; 74 75 76 protected JTextField textField_; 77 78 79 protected Object object_; 80 81 82 protected TreeChooserObserver treeChooserObserver_ = null; 83 84 90 95 public TreeChooserBox(String label, DynamicTree tree) { 96 this(label,tree,true); 97 } 98 99 105 public TreeChooserBox(String label, DynamicTree tree, boolean isMandatory) { 106 super(isMandatory); 107 label_ = label; 108 tree_ = tree; 109 tree_.setPopupEnabled(false); 110 tree_.setDragAndDropEnabled(false); 111 add(Box.createHorizontalGlue()); 112 JLabel browserLabel = new JLabel (label + ": ", SwingConstants.RIGHT); 113 browserLabel.setAlignmentX(Component.RIGHT_ALIGNMENT); 114 browserLabel.setAlignmentY(Component.CENTER_ALIGNMENT); 115 add(browserLabel); 116 add(Box.createHorizontalStrut(5)); 117 textField_ = new JTextField (); 118 textField_.setEditable(false); 119 textField_.setPreferredSize(new Dimension (200, 20)); 120 textField_.setMaximumSize(new Dimension (200, 20)); 121 add(textField_); 122 add(Box.createHorizontalStrut(5)); 123 browseButton_ = new JButton (new BrowseAction(this)); 124 browseButton_.setPreferredSize(new Dimension (20, 20)); 125 browseButton_.setMaximumSize(new Dimension (20, 20)); 126 add(browseButton_); 127 } 128 129 135 139 public ValidateReport validateBox(){ 140 if(isMandatory_){ 141 if(getObject()==null) 142 return new DefaultValidateReport(false,"The \"" + label_ + "\" value is mandatory"); 143 } 144 return new DefaultValidateReport(); 145 } 146 147 150 public void preInitialize(){ 151 if(tree_!=null){ 152 tree_.setPopupEnabled(false); 153 tree_.setDragAndDropEnabled(false); 154 } 155 } 156 157 160 public void postInitialize(){ 161 if(tree_!=null){ 162 tree_.setPopupEnabled(true); 163 tree_.setDragAndDropEnabled(true); 164 } 165 } 166 167 173 174 public Object getObject() { 175 if(object_!=null && Wrapper.class.isAssignableFrom(object_.getClass())) 176 return ((Wrapper)object_).getWrapped(); 177 return object_; 178 } 179 180 181 public void setTreeChooserObserver(TreeChooserObserver treeChooserObserver) { 182 treeChooserObserver_ = treeChooserObserver; 183 } 184 185 188 protected class BrowseAction extends AbstractAction { 189 190 191 protected Component parent_; 192 193 196 protected BrowseAction(Component parent) { 197 super("...", null); 198 parent_ = parent; 199 } 200 201 public void actionPerformed(java.awt.event.ActionEvent ae) { 202 if(treeChooserObserver_ != null) { 203 tree_ = treeChooserObserver_.refresh(); 204 tree_.setPopupEnabled(false); 205 } 206 JPanel treePanel = new JPanel (); 207 treePanel.setLayout(new GridLayout (1, 0)); 208 treePanel.setPreferredSize(new Dimension (450, 350)); 209 treePanel.add(new JScrollPane (tree_)); 210 int result = JOptionPane.showOptionDialog((Component )ae.getSource(), treePanel, "Select an object", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null); 211 if (result == 0) { 212 Entry entry = tree_.getSelectedEntry(); 213 if (entry != null) { 214 Object o = entry.getValue(); 215 if (o != null) { 216 object_ = o; 217 textField_.setText(entry.getName().toString()); 218 } 219 } 220 } 221 } 222 223 } 224 225 } 226 | Popular Tags |