KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > specifier > editor > ChooserPanel


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 modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes 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,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.specifier.editor;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Color JavaDoc;
34 import java.awt.FlowLayout JavaDoc;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.KeyEvent JavaDoc;
37 import java.awt.event.MouseEvent JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Iterator JavaDoc;
40
41 import javax.swing.DefaultListModel JavaDoc;
42 import javax.swing.JButton JavaDoc;
43 import javax.swing.JLabel JavaDoc;
44 import javax.swing.JList JavaDoc;
45 import javax.swing.JPanel JavaDoc;
46 import javax.swing.JScrollPane JavaDoc;
47 import javax.swing.ListSelectionModel JavaDoc;
48
49 import com.genimen.djeneric.language.Messages;
50 import com.genimen.djeneric.repository.DjCursor;
51 import com.genimen.djeneric.repository.DjExtent;
52 import com.genimen.djeneric.repository.DjList;
53 import com.genimen.djeneric.repository.DjObject;
54 import com.genimen.djeneric.repository.DjOql;
55 import com.genimen.djeneric.repository.DjProperty;
56 import com.genimen.djeneric.repository.DjSession;
57 import com.genimen.djeneric.repository.exceptions.DjenericException;
58 import com.genimen.djeneric.tools.scriptengine.core.util.Variable;
59 import com.genimen.djeneric.tools.specifier.base.BasePanel;
60 import com.genimen.djeneric.tools.specifier.base.FilterPanel;
61 import com.genimen.djeneric.tools.specifier.interfaces.Chooser;
62 import com.genimen.djeneric.tools.specifier.interfaces.EditorEventListener;
63 import com.genimen.djeneric.tools.specifier.interfaces.SpecifierPanelContainer;
64 import com.genimen.djeneric.ui.Util;
65 import com.genimen.djeneric.util.DjLogger;
66
67 public class ChooserPanel extends BasePanel implements Chooser
68 {
69   private static final long serialVersionUID = 1L;
70   public static final int MAX_RESULT_SIZE = 200;
71
72   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
73   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
74   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
75   JPanel JavaDoc _buttonPanel = new JPanel JavaDoc();
76   JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
77   JList JavaDoc _lstResults = new JList JavaDoc();
78   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
79   BorderLayout JavaDoc borderLayout4 = new BorderLayout JavaDoc();
80   JPanel JavaDoc _buttonPanelRight = new JPanel JavaDoc();
81   FlowLayout JavaDoc flowLayout1 = new FlowLayout JavaDoc();
82   JButton JavaDoc _butSelect = new JButton JavaDoc();
83   JButton JavaDoc _butCancel = new JButton JavaDoc();
84   BorderLayout JavaDoc borderLayout5 = new BorderLayout JavaDoc();
85   FilterPanel _propertyPanel;
86   DjExtent _extent;
87   DjSession _session;
88   ArrayList JavaDoc _editorEventListeners = new ArrayList JavaDoc();
89   String JavaDoc _eventContext = null;
90   SpecifierPanelContainer _editorContainer;
91   Variable _destVariable = null;
92   DjObject _selectedObject = null;
93   JLabel JavaDoc _edtStatus = new JLabel JavaDoc();
94   FlowLayout JavaDoc flowLayout2 = new FlowLayout JavaDoc();
95   JButton JavaDoc _butRequery = new JButton JavaDoc();
96   JPanel JavaDoc _middleButtonPanel = new JPanel JavaDoc();
97   boolean _disableDefaultAccept = false;
98
99   public ChooserPanel()
100   {
101   }
102
103   public ChooserPanel(DjSession session, DjExtent extent)
104   {
105     construct(null, null, session, extent, null);
106   }
107
108   public void construct(SpecifierPanelContainer editorContainer, DjOql restrictingOql, DjSession session,
109                         DjExtent extent, ArrayList JavaDoc excludeTheseProperties)
110   {
111     try
112     {
113       _editorContainer = editorContainer;
114       _extent = extent;
115       _session = session;
116       _propertyPanel = new FilterPanel(restrictingOql, session, extent, excludeTheseProperties)
117       {
118         private static final long serialVersionUID = 1L;
119
120         protected void enterHit()
121         {
122           ChooserPanel.this.enterHit();
123         }
124
125         protected void acceptHit()
126         {
127           ChooserPanel.this.acceptHit();
128         }
129
130         protected void escapeHit()
131         {
132           ChooserPanel.this.escapeHit();
133         }
134       };
135       jbInit();
136
137       _lstResults.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
138       {
139         public void keyPressed(KeyEvent JavaDoc e)
140         {
141           handleListKeyPressed(e);
142         }
143       });
144     }
145     catch (Exception JavaDoc x)
146     {
147       DjLogger.log(x);
148     }
149   }
150
151   void jbInit() throws Exception JavaDoc
152   {
153     this.setLayout(borderLayout1);
154     jPanel2.setLayout(borderLayout2);
155     _buttonPanel.setLayout(borderLayout4);
156     _buttonPanelRight.setLayout(flowLayout1);
157     _butSelect.setText(Messages.getString("ChooserPanel.Select"));
158     _butSelect.addActionListener(new java.awt.event.ActionListener JavaDoc()
159     {
160       public void actionPerformed(ActionEvent JavaDoc e)
161       {
162         _butSelect_actionPerformed(e);
163       }
164     });
165     _butCancel.setText(Messages.getString("global.Cancel"));
166     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
167     {
168       public void actionPerformed(ActionEvent JavaDoc e)
169       {
170         _butCancel_actionPerformed(e);
171       }
172     });
173     _lstResults.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
174     {
175       public void mouseClicked(MouseEvent JavaDoc e)
176       {
177         _lstResults_mouseClicked(e);
178       }
179     });
180     _edtStatus.setForeground(Color.red);
181     _edtStatus.setText("");
182
183     flowLayout2.setAlignment(FlowLayout.RIGHT);
184     _butRequery.setText(Messages.getString("global.Query"));
185     _butRequery.addActionListener(new java.awt.event.ActionListener JavaDoc()
186     {
187       public void actionPerformed(ActionEvent JavaDoc e)
188       {
189         _butRequery_actionPerformed(e);
190       }
191     });
192     _middleButtonPanel.setLayout(flowLayout2);
193     _lstResults.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
194     jScrollPane1.getViewport().add(_lstResults, null);
195
196     jPanel2.add(jScrollPane1, BorderLayout.CENTER);
197     jPanel2.add(_buttonPanel, BorderLayout.SOUTH);
198
199     _buttonPanel.add(_buttonPanelRight, BorderLayout.EAST);
200     _buttonPanelRight.add(_butCancel, null);
201     _buttonPanelRight.add(_butSelect, null);
202     jPanel2.add(_middleButtonPanel, BorderLayout.NORTH);
203     _middleButtonPanel.add(_butRequery, null);
204
205     this.add(_propertyPanel, BorderLayout.NORTH);
206     this.add(jPanel2, BorderLayout.CENTER);
207     this.add(_edtStatus, BorderLayout.SOUTH);
208     Util.sizeButtons(_buttonPanelRight);
209   }
210
211   void _butRequery_actionPerformed(ActionEvent JavaDoc e)
212   {
213
214     try
215     {
216       requery();
217     }
218     catch (DjenericException e1)
219     {
220       setStatusMessage(e1);
221     }
222   }
223
224   public void requestFocus()
225   {
226     _propertyPanel.requestFocus();
227   }
228
229   protected void requery() throws DjenericException
230   {
231     DjOql oql = _propertyPanel.getOql();
232     DjCursor lst = _session.getObjectsCursor(oql);
233     DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
234
235     DjList result = new DjList();
236
237     DjObject obj = lst.getNext();
238
239     for (int i = 0; i < MAX_RESULT_SIZE && obj != null; i++)
240     {
241       result.add(obj);
242       obj = lst.getNext();
243     }
244     lst.close();
245     result.sort();
246
247     Iterator JavaDoc it = result.iterator();
248     while (it.hasNext())
249       model.addElement(it.next());
250
251     _lstResults.setModel(model);
252
253     if (model.size() > 0) _lstResults.setSelectedIndex(0);
254     if (model.size() == MAX_RESULT_SIZE) setStatusMessage(Messages.getString("ChooserPanel.MoreThan", String
255         .valueOf(MAX_RESULT_SIZE), _extent.getNamePlural()), false);
256     else if (model.size() == 0) setStatusMessage(Messages.getString("ChooserPanel.No", _extent.getNameSingular()),
257                                                  false);
258     else setStatusMessage(Messages.getString("global.Ok"), true);
259
260     if (model.size() != 0) _lstResults.requestFocus();
261   }
262
263   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
264   {
265     try
266     {
267       if (close()) fireEvent("cancel");
268     }
269     catch (DjenericException dje)
270     {
271       setStatusMessage(dje.getMessage(), false);
272     }
273   }
274
275   void _butSelect_actionPerformed(ActionEvent JavaDoc e)
276   {
277     try
278     {
279       select();
280       if (close()) fireEvent("accept");
281     }
282     catch (DjenericException dje)
283     {
284       setStatusMessage(dje);
285     }
286   }
287
288   protected void cleanup() throws DjenericException
289   {
290     fireEvent("close");
291     if (_editorContainer != null)
292     {
293       _editorContainer.notifyClosed(this);
294     }
295   }
296
297   protected void select() throws DjenericException
298   {
299
300     if (_lstResults.getSelectedIndex() == -1)
301     {
302       throw new DjenericException(Messages.getString("ChooserPanel.PleaseSelect", _extent.getNameSingular()));
303     }
304     fireEvent("validate");
305
306     _selectedObject = (DjObject) _lstResults.getSelectedValue();
307     if (_destVariable != null)
308     {
309       _destVariable.setObject(_selectedObject);
310     }
311   }
312
313   void _lstResults_mouseClicked(MouseEvent JavaDoc e)
314   {
315     if (!_disableDefaultAccept && e.getClickCount() > 1) acceptHit();
316   }
317
318   public void hideDefaultOkCCancelButtons(boolean hide)
319   {
320     _butSelect.setVisible(!hide);
321     _butCancel.setVisible(!hide);
322   }
323
324   public void registerEvent(String JavaDoc eventName, String JavaDoc eventTitle, String JavaDoc eventOption)
325   {
326     hideDefaultOkCCancelButtons(true);
327     JButton JavaDoc newButton = new JButton JavaDoc();
328
329     newButton.setText(eventTitle);
330     newButton.setActionCommand(eventName + "," + eventOption);
331     newButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
332     {
333       public void actionPerformed(ActionEvent JavaDoc e)
334       {
335         fireCustomEvent(e);
336       }
337     });
338     _buttonPanelRight.add(newButton, null);
339     Util.sizeButtons(_buttonPanelRight);
340     _disableDefaultAccept = true;
341   }
342
343   protected void fireCustomEvent(ActionEvent JavaDoc e)
344   {
345     String JavaDoc event = e.getActionCommand();
346     String JavaDoc option = "none";
347     int idx = event.indexOf(",");
348     if (idx != -1)
349     {
350       option = event.substring(idx + 1);
351       event = event.substring(0, idx);
352     }
353
354     try
355     {
356       if ("accept".equals(option))
357       {
358         select();
359         close();
360         fireEvent(event);
361       }
362       else if ("cancel".equals(option))
363       {
364         close();
365         fireEvent(event);
366       }
367       else fireEvent(event);
368     }
369     catch (Exception JavaDoc x)
370     {
371       setStatusMessage(x);
372     }
373
374   }
375
376   public void setStatusMessage(String JavaDoc msg, boolean isInformative)
377   {
378     if (_editorContainer != null) _editorContainer.setStatusMessage(msg, isInformative);
379     else if (!isInformative) _edtStatus.setText(msg);
380     else _edtStatus.setText("");
381   }
382
383   public DjProperty[] getQueryableProperties()
384   {
385     return _propertyPanel.getQueryableProperties();
386   }
387
388   protected void fireEvent(String JavaDoc eventName) throws DjenericException
389   {
390     String JavaDoc fullEventName;
391
392     if (_eventContext == null) fullEventName = eventName;
393     else fullEventName = _eventContext + "." + eventName;
394
395     for (int i = 0; i < _editorEventListeners.size(); i++)
396     {
397       EditorEventListener eel = (EditorEventListener) _editorEventListeners.get(i);
398       eel.handleEvent(fullEventName);
399     }
400   }
401
402   public void addEditorEventListener(EditorEventListener lsnr)
403   {
404     _editorEventListeners.add(lsnr);
405   }
406
407   public void removeEditorEventListener(EditorEventListener lsnr)
408   {
409     _editorEventListeners.remove(lsnr);
410   }
411
412   public String JavaDoc getEventContext()
413   {
414     return _eventContext;
415   }
416
417   public void setEventContext(String JavaDoc string)
418   {
419     _eventContext = string;
420   }
421
422   public void setDestinationVariable(Variable destVariable)
423   {
424     _destVariable = destVariable;
425   }
426
427   public DjObject getSelectedObject()
428   {
429     if (_selectedObject == null) return (DjObject) _lstResults.getSelectedValue();
430     return _selectedObject;
431   }
432
433   protected void enterHit()
434   {
435     _butRequery_actionPerformed(null);
436   }
437
438   protected void acceptHit()
439   {
440     _butSelect_actionPerformed(null);
441   }
442
443   protected void escapeHit()
444   {
445     _butCancel_actionPerformed(null);
446   }
447
448   void handleListKeyPressed(KeyEvent JavaDoc e)
449   {
450     if (!_disableDefaultAccept && e.getKeyCode() == KeyEvent.VK_ENTER && e.getModifiers() == 0) acceptHit();
451     if (e.getKeyCode() == KeyEvent.VK_ESCAPE) escapeHit();
452   }
453
454 }
Popular Tags