KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > swing > gui > lib > TreeChooserBox


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2000-2004 INRIA - 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, Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.util.explorer.swing.gui.lib;
28
29 /** The java API's imports */
30 import javax.swing.JScrollPane JavaDoc;
31 import javax.swing.JTextField JavaDoc;
32 import javax.swing.JLabel JavaDoc;
33 import javax.swing.JPanel JavaDoc;
34 import javax.swing.JButton JavaDoc;
35 import javax.swing.AbstractAction JavaDoc;
36 import javax.swing.Box JavaDoc;
37 import javax.swing.SwingConstants JavaDoc;
38 import javax.swing.JOptionPane JavaDoc;
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 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 component. */
73     protected org.objectweb.fractal.api.Component component_;
74     
75     /** The Tree interface of the Tree component. */
76     protected Tree treeItf_;
77
78     /** The Explorer interface of the Tree component. */
79     protected Explorer explorerItf_;
80     
81     /** */
82     protected JTextField JavaDoc textField_;
83
84     /** The choosen object */
85     protected Object JavaDoc object_;
86
87     /** The Observer. */
88     protected TreeChooserObserver treeChooserObserver_ = null;
89
90     //==================================================================
91
//
92
// Constructors.
93
//
94
//==================================================================
95

96     /**
97      * Default constructor
98      * @param label The label of the box
99      * @param tree The DynamicTree to use
100      */

101     public TreeChooserBox(String JavaDoc label, org.objectweb.fractal.api.Component treeComponent) {
102         this(label,treeComponent,true);
103     }
104     
105     /**
106      * Constructor which specifies if the value is mandatory or not
107      * @param label The label of the box
108      * @param tree The DynamicTree to use
109      * @param isMandatory If the value is mandatory : TRUE, else : FALSE
110      */

111     public TreeChooserBox(String JavaDoc 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 JavaDoc explorerLabel = new JLabel JavaDoc(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 JavaDoc();
128         textField_.setEditable(false);
129         textField_.setPreferredSize(new Dimension JavaDoc(200, 20));
130         textField_.setMaximumSize(new Dimension JavaDoc(200, 20));
131         add(textField_);
132         add(Box.createHorizontalStrut(5));
133         browseButton_ = new JButton JavaDoc(new BrowseAction(this));
134         browseButton_.setPreferredSize(new Dimension JavaDoc(20, 20));
135         browseButton_.setMaximumSize(new Dimension JavaDoc(20, 20));
136         add(browseButton_);
137     }
138
139     // ==================================================================
140
//
141
// Internal methods.
142
//
143
// ==================================================================
144

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     // ==================================================================
158
//
159
// Public methods for ElementBox.
160
//
161
// ==================================================================
162

163     /**
164      * Validates the content of the ElementBox. Indicates if the different value are acceptables or not.
165      * @return The corresponding ValidateReport.
166      */

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     /**
176      * Invokes just before displaying.
177      */

178     public void preInitialize(){
179         if(explorerItf_!=null){
180             explorerItf_.setPopupEnabled(false);
181             explorerItf_.setDragAndDropEnabled(false);
182         }
183     }
184     
185     /**
186      * Invokes just before removing.
187      */

188     public void postInitialize(){
189         if(explorerItf_!=null){
190             explorerItf_.setPopupEnabled(true);
191             explorerItf_.setDragAndDropEnabled(true);
192         }
193     }
194     
195     // ==================================================================
196
//
197
// Public methods.
198
//
199
// ==================================================================
200

201     /** Returns the chosen Object */
202     public Object JavaDoc getObject() {
203         return object_;
204     }
205
206     /** Fixes the TreeChooserObserver */
207     public void setTreeChooserObserver(TreeChooserObserver treeChooserObserver) {
208         treeChooserObserver_ = treeChooserObserver;
209     }
210
211     /**
212      * Action which
213      */

214     protected class BrowseAction extends AbstractAction JavaDoc {
215
216         /** The parent of the panel */
217         protected Component JavaDoc parent_;
218
219         /**
220          * Default constructor
221          */

222         protected BrowseAction(Component JavaDoc parent) {
223             super("...", null);
224             parent_ = parent;
225         }
226
227         public void actionPerformed(java.awt.event.ActionEvent JavaDoc ae) {
228             if(treeChooserObserver_ != null) {
229                 initAttributes(treeChooserObserver_.refresh());
230                 explorerItf_.setPopupEnabled(false);
231             }
232             JPanel JavaDoc treePanel = new JPanel JavaDoc();
233             treePanel.setLayout(new GridLayout JavaDoc(1, 0));
234             treePanel.setPreferredSize(new Dimension JavaDoc(450, 350));
235             treePanel.add(new JScrollPane JavaDoc(explorerItf_.getTree()));
236             int result = JOptionPane.showOptionDialog((Component JavaDoc)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 JavaDoc 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