KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > gui > lib > TreeChooserBox


1 /*===========================================================================
2
3 ObjectWeb Naming Context Framework
4 Copyright (C) 2002 USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy.
23 Contributor(s): ______________________________________.
24
25 ===========================================================================*/

26
27 package org.objectweb.util.browser.gui.lib;
28
29 /** The browser's imports */
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 /** The java API's imports */
37 import javax.swing.JScrollPane JavaDoc;
38 import javax.swing.JTextField JavaDoc;
39 import javax.swing.JLabel JavaDoc;
40 import javax.swing.JPanel JavaDoc;
41 import javax.swing.JButton JavaDoc;
42 import javax.swing.AbstractAction JavaDoc;
43 import javax.swing.Box JavaDoc;
44 import javax.swing.SwingConstants JavaDoc;
45 import javax.swing.JOptionPane JavaDoc;
46
47 import java.awt.Component JavaDoc;
48 import java.awt.Dimension JavaDoc;
49 import java.awt.GridLayout JavaDoc;
50
51 /**
52  * This class represents the panel which allows to choose an element in the tree.
53  *
54  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>
55  * @version 0.1
56  */

57 public class TreeChooserBox
58     extends AbstractElementBox {
59
60     //==================================================================
61
//
62
// Internal state.
63
//
64
//==================================================================
65

66     /** The value of the label */
67     protected String JavaDoc label_;
68
69     /** The JButton used to browse the tree */
70     protected JButton JavaDoc browseButton_;
71
72     /** The Tree */
73     protected DynamicTree tree_;
74
75     /** */
76     protected JTextField JavaDoc textField_;
77
78     /** The choosen object */
79     protected Object JavaDoc object_;
80
81     /** The Observer. */
82     protected TreeChooserObserver treeChooserObserver_ = null;
83
84     //==================================================================
85
//
86
// Constructors.
87
//
88
//==================================================================
89

90     /**
91      * Default constructor
92      * @param label The label of the box
93      * @param tree The DynamicTree to use
94      */

95     public TreeChooserBox(String JavaDoc label, DynamicTree tree) {
96         this(label,tree,true);
97     }
98     
99     /**
100      * Constructor which specifies if the value is mandatory or not
101      * @param label The label of the box
102      * @param tree The DynamicTree to use
103      * @param isMandatory If the value is mandatory : TRUE, else : FALSE
104      */

105     public TreeChooserBox(String JavaDoc 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 JavaDoc browserLabel = new JLabel JavaDoc(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 JavaDoc();
118         textField_.setEditable(false);
119         textField_.setPreferredSize(new Dimension JavaDoc(200, 20));
120         textField_.setMaximumSize(new Dimension JavaDoc(200, 20));
121         add(textField_);
122         add(Box.createHorizontalStrut(5));
123         browseButton_ = new JButton JavaDoc(new BrowseAction(this));
124         browseButton_.setPreferredSize(new Dimension JavaDoc(20, 20));
125         browseButton_.setMaximumSize(new Dimension JavaDoc(20, 20));
126         add(browseButton_);
127     }
128
129     // ==================================================================
130
//
131
// Public methods for ElementBox.
132
//
133
// ==================================================================
134

135     /**
136      * Validates the content of the ElementBox. Indicates if the different value are acceptables or not.
137      * @return The corresponding ValidateReport.
138      */

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     /**
148      * Invokes just before displaying.
149      */

150     public void preInitialize(){
151         if(tree_!=null){
152             tree_.setPopupEnabled(false);
153             tree_.setDragAndDropEnabled(false);
154         }
155     }
156     
157     /**
158      * Invokes just before removing.
159      */

160     public void postInitialize(){
161         if(tree_!=null){
162             tree_.setPopupEnabled(true);
163             tree_.setDragAndDropEnabled(true);
164         }
165     }
166     
167     // ==================================================================
168
//
169
// Public methods.
170
//
171
// ==================================================================
172

173     /** Returns the chosen Object */
174     public Object JavaDoc getObject() {
175         if(object_!=null && Wrapper.class.isAssignableFrom(object_.getClass()))
176             return ((Wrapper)object_).getWrapped();
177         return object_;
178     }
179
180     /** Fixes the TreeChooserObserver */
181     public void setTreeChooserObserver(TreeChooserObserver treeChooserObserver) {
182         treeChooserObserver_ = treeChooserObserver;
183     }
184
185     /**
186      * Action which
187      */

188     protected class BrowseAction extends AbstractAction JavaDoc {
189
190         /** The parent of the panel */
191         protected Component JavaDoc parent_;
192
193         /**
194          * Default constructor
195          */

196         protected BrowseAction(Component JavaDoc parent) {
197             super("...", null);
198             parent_ = parent;
199         }
200
201         public void actionPerformed(java.awt.event.ActionEvent JavaDoc ae) {
202             if(treeChooserObserver_ != null) {
203                 tree_ = treeChooserObserver_.refresh();
204                 tree_.setPopupEnabled(false);
205             }
206             JPanel JavaDoc treePanel = new JPanel JavaDoc();
207             treePanel.setLayout(new GridLayout JavaDoc(1, 0));
208             treePanel.setPreferredSize(new Dimension JavaDoc(450, 350));
209             treePanel.add(new JScrollPane JavaDoc(tree_));
210             int result = JOptionPane.showOptionDialog((Component JavaDoc)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 JavaDoc 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