KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > specifier > components > DjChooserField


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.specifier.components;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Dimension JavaDoc;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36 import java.awt.event.FocusListener JavaDoc;
37 import java.awt.event.KeyEvent JavaDoc;
38 import java.awt.event.MouseEvent JavaDoc;
39 import java.util.ArrayList JavaDoc;
40
41 import javax.swing.AbstractAction JavaDoc;
42 import javax.swing.JButton JavaDoc;
43 import javax.swing.JComponent JavaDoc;
44 import javax.swing.JDialog JavaDoc;
45 import javax.swing.JPanel JavaDoc;
46 import javax.swing.JTextField JavaDoc;
47 import javax.swing.KeyStroke JavaDoc;
48
49 import com.genimen.djeneric.repository.DjExtent;
50 import com.genimen.djeneric.repository.DjSession;
51 import com.genimen.djeneric.repository.exceptions.DjenericException;
52 import com.genimen.djeneric.tools.specifier.dialogs.ChooserDialog;
53 import com.genimen.djeneric.tools.specifier.interfaces.ObjectViewer;
54 import com.genimen.djeneric.ui.Util;
55 import com.genimen.djeneric.util.DjLogger;
56
57 public class DjChooserField extends JPanel JavaDoc implements DjBindable
58 {
59   private static final long serialVersionUID = 1L;
60   private BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
61   private JTextField JavaDoc _displayField = new JTextField JavaDoc();
62   private JButton JavaDoc _butChoose = new JButton JavaDoc();
63   private DjExtent _extent;
64   private DjSession _session;
65   private BindingMediator _mediator = null;
66   private Object JavaDoc _dispayedValue = null;
67   private String JavaDoc _dialogTitle = null;
68   private boolean _required = true;
69
70   ArrayList JavaDoc _listeners = new ArrayList JavaDoc();
71
72   static boolean _onJdk13 = System.getProperty("java.specification.version").startsWith("1.3");
73
74   public DjChooserField(DjSession session, DjExtent extent) throws DjenericException
75   {
76     this(null, null, session, extent);
77   }
78
79   public DjChooserField(ObjectViewer viewer, String JavaDoc propertyName, DjSession session, DjExtent extent)
80       throws DjenericException
81   {
82     if (viewer != null) _mediator = new BindingMediator(this, viewer, propertyName);
83     _session = session;
84     _extent = extent;
85
86     if (!_onJdk13)
87     {
88       setFocusable(false);
89       _displayField.setFocusable(false);
90     }
91
92     setLayout(borderLayout1);
93     _butChoose.setPreferredSize(new Dimension JavaDoc(21, 21));
94     _butChoose.setText("...");
95     _butChoose.addActionListener(new ChooserField__butChoose_actionAdapter(this));
96     add(_displayField, BorderLayout.CENTER);
97     add(_butChoose, BorderLayout.EAST);
98     _displayField.setEditable(false);
99
100     _displayField.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0));
101
102     _displayField.registerKeyboardAction(new KickButtonAction(), KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
103                                          JComponent.WHEN_FOCUSED);
104
105     _displayField.registerKeyboardAction(new KickButtonAction(), KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0),
106                                          JComponent.WHEN_FOCUSED);
107
108     _displayField.addMouseListener(new ChooserField__displayField_mouseAdapter(this));
109   }
110
111   public boolean isComponentWritable()
112   {
113     return _mediator.isComponentWritable();
114   }
115
116   public void setLookupExtent(DjExtent extent)
117   {
118     _extent = extent;
119   }
120
121   public void clear()
122   {
123     _displayField.setText("");
124   }
125
126   public void apply()
127   {
128     // No need to apply (again); this is done the moment the button is clicked
129
// and it's dialog closed
130
}
131
132   public void setPropertyName(String JavaDoc propertyName) throws DjenericException
133   {
134     if (_mediator != null) _mediator.setPropertyName(propertyName);
135   }
136
137   public void setViewer(ObjectViewer viewer)
138   {
139     if (_mediator != null) _mediator.setViewer(viewer);
140   }
141
142   public void synchronize() throws DjenericException
143   {
144     if (_mediator != null)
145     {
146       _displayField.setText(_mediator.getPropertyValueString());
147       _dispayedValue = _mediator.getPropertyValue();
148     }
149   }
150
151   void _butChoose_actionPerformed(ActionEvent JavaDoc e)
152   {
153     showChooseDialog(e);
154   }
155
156   public void showChooseDialog(ActionEvent JavaDoc e)
157   {
158     try
159     {
160       int x = 0;
161       int y = 0;
162       JDialog JavaDoc parent = (JDialog JavaDoc) Util.findParent(this, JDialog JavaDoc.class);
163       if (parent != null)
164       {
165         x = parent.getX() + 30;
166         y = parent.getY() + 30;
167       }
168
169       ChooserDialog cd = new ChooserDialog(Util.findFrame(this), _session, _extent, null, _dialogTitle, _required, x, y);
170       if (!cd.wasCancelled())
171       {
172         Object JavaDoc o = cd.getSelectedObject();
173         if (_mediator != null) _mediator.setValue(o);
174         setDisplayedValue(o);
175       }
176       for (int i = 0; i < _listeners.size(); i++)
177       {
178         ActionListener JavaDoc al = (ActionListener JavaDoc) _listeners.get(i);
179         al.actionPerformed(e);
180       }
181     }
182     catch (DjenericException x)
183     {
184       DjLogger.log(x);
185     }
186   }
187
188   public boolean isEditable()
189   {
190     return _butChoose.isEnabled();
191   }
192
193   public boolean isEnabled()
194   {
195     return _butChoose.isEnabled();
196   }
197
198   public void requestFocus()
199   {
200     _butChoose.requestFocus();
201   }
202
203   public void setEditable(boolean b)
204   {
205     _butChoose.setEnabled(b);
206   }
207
208   public void setEnabled(boolean b)
209   {
210     _butChoose.setEnabled(b);
211   }
212
213   public void setSession(DjSession session)
214   {
215     _session = session;
216   }
217
218   public void setDisplayedValue(Object JavaDoc value)
219   {
220     _dispayedValue = value;
221     if (value != null) _displayField.setText(value.toString());
222     else _displayField.setText("");
223   }
224
225   public JTextField JavaDoc getDisplayField()
226   {
227     return _displayField;
228   }
229
230   public JTextField JavaDoc getEditor()
231   {
232     return _displayField;
233   }
234
235   public Object JavaDoc getDisplayedValue()
236   {
237     return _dispayedValue;
238   }
239
240   public String JavaDoc getPropertyName()
241   {
242     return _mediator.getPropertyName();
243   }
244
245   public JComponent JavaDoc getFocussableComponent()
246   {
247     return _butChoose;
248   }
249
250   public void addActionListener(ActionListener JavaDoc l)
251   {
252     _listeners.add(l);
253   }
254
255   public void removeActionListener(ActionListener JavaDoc l)
256   {
257     _listeners.remove(l);
258   }
259
260   private class KickButtonAction extends AbstractAction JavaDoc
261   {
262     private static final long serialVersionUID = 1L;
263
264     public void actionPerformed(ActionEvent JavaDoc e)
265     {
266       _butChoose_actionPerformed(e);
267     }
268   }
269
270   public void setDialogTitle(String JavaDoc title)
271   {
272     _dialogTitle = title;
273   }
274
275   public boolean isRequired()
276   {
277     return _required;
278   }
279
280   public void setRequired(boolean b)
281   {
282     _required = b;
283   }
284
285   public synchronized void addFocusListener(FocusListener JavaDoc l)
286   {
287     if (_butChoose != null) _butChoose.addFocusListener(l);
288   }
289
290   public void _displayField_mouseClicked(MouseEvent JavaDoc e)
291   {
292     if (e.getClickCount() > 1) showChooseDialog(new ActionEvent JavaDoc(this, 0, ""));
293
294   }
295 }
296
297 class ChooserField__butChoose_actionAdapter implements java.awt.event.ActionListener JavaDoc
298 {
299   DjChooserField adaptee;
300
301   ChooserField__butChoose_actionAdapter(DjChooserField adaptee)
302   {
303     this.adaptee = adaptee;
304   }
305
306   public void actionPerformed(ActionEvent JavaDoc e)
307   {
308     adaptee._butChoose_actionPerformed(e);
309   }
310 }
311
312 class ChooserField__displayField_mouseAdapter extends java.awt.event.MouseAdapter JavaDoc
313 {
314   DjChooserField adaptee;
315
316   ChooserField__displayField_mouseAdapter(DjChooserField adaptee)
317   {
318     this.adaptee = adaptee;
319   }
320
321   public void mouseClicked(MouseEvent JavaDoc e)
322   {
323     adaptee._displayField_mouseClicked(e);
324   }
325 }
Popular Tags