KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > dialogs > ExtentSelectorDialog


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.modeler.dialogs;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Dimension JavaDoc;
34 import java.awt.Frame JavaDoc;
35 import java.awt.Toolkit JavaDoc;
36 import java.awt.event.ActionEvent JavaDoc;
37 import java.awt.event.KeyEvent JavaDoc;
38 import java.awt.event.MouseEvent JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Arrays JavaDoc;
41
42 import javax.swing.DefaultComboBoxModel JavaDoc;
43 import javax.swing.JButton JavaDoc;
44 import javax.swing.JCheckBox JavaDoc;
45 import javax.swing.JComboBox JavaDoc;
46 import javax.swing.JDialog JavaDoc;
47 import javax.swing.JLabel JavaDoc;
48 import javax.swing.JList JavaDoc;
49 import javax.swing.JPanel JavaDoc;
50 import javax.swing.JScrollPane JavaDoc;
51 import javax.swing.ListModel JavaDoc;
52 import javax.swing.ListSelectionModel JavaDoc;
53
54 import com.genimen.djeneric.language.Messages;
55 import com.genimen.djeneric.repository.DjExtent;
56 import com.genimen.djeneric.repository.DjPackage;
57 import com.genimen.djeneric.repository.DjRelation;
58 import com.genimen.djeneric.ui.Util;
59 import com.genimen.djeneric.util.DjLogger;
60
61 public class ExtentSelectorDialog extends JDialog JavaDoc
62 {
63   private static final long serialVersionUID = 1L;
64   JPanel JavaDoc panel1 = new JPanel JavaDoc();
65   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
66   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
67   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
68   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
69   JButton JavaDoc _butOk = new JButton JavaDoc();
70   JButton JavaDoc _butCancel = new JButton JavaDoc();
71   JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
72   JList JavaDoc _lstExtents = new JList JavaDoc();
73   boolean _canceled = true;
74   JCheckBox JavaDoc _chkSelectionRelated = new JCheckBox JavaDoc();
75   DjExtent[] _extents;
76   DjExtent[] _selection;
77   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
78   JLabel JavaDoc _lblPackage = new JLabel JavaDoc();
79   JComboBox JavaDoc _cbbPackage = new JComboBox JavaDoc();
80   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
81   DjPackage _defaultPackage = new DjPackage(Messages
82                                                      .getString("ExtentSelectorDialog.DefaultPackage"));
83   DjPackage _allPackages = new DjPackage(Messages.getString("ExtentSelectorDialog.AllPackages"));
84
85   public ExtentSelectorDialog(Frame JavaDoc frame, String JavaDoc title, DjPackage[] packages, DjExtent[] extents, DjExtent[] selected)
86   {
87     super(frame, title, true);
88     try
89     {
90       _extents = extents;
91
92       setupSelection(extents, selected);
93
94       jbInit();
95
96       ArrayList JavaDoc packs = new ArrayList JavaDoc();
97       packs.add(_allPackages);
98       packs.add(_defaultPackage);
99       packs.addAll(Arrays.asList(packages));
100
101       _cbbPackage.setModel(new DefaultComboBoxModel JavaDoc(packs.toArray()));
102
103       updateList();
104
105       pack();
106       Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
107       Dimension JavaDoc frameSize = getSize();
108       setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
109
110       _lstExtents.requestFocus();
111     }
112     catch (Exception JavaDoc ex)
113     {
114       DjLogger.log(ex);
115     }
116   }
117
118   public void setShowSelectionRelated(boolean b)
119   {
120     _chkSelectionRelated.setVisible(b);
121   }
122
123   private void addChoosableIfNotAdded(ArrayList JavaDoc choosable, ArrayList JavaDoc lst, DjExtent ext)
124   {
125     if (choosable.contains(ext) && !lst.contains(ext)) lst.add(ext);
126   }
127
128   private void setupSelection(DjExtent[] extents, DjExtent[] selected)
129   {
130     ArrayList JavaDoc result = new ArrayList JavaDoc();
131
132     ArrayList JavaDoc choosable = new ArrayList JavaDoc();
133     for (int i = 0; i < extents.length; i++)
134       choosable.add(extents[i]);
135
136     for (int i = 0; i < selected.length; i++)
137     {
138       addChoosableIfNotAdded(choosable, result, selected[i].getSuper());
139
140       DjExtent[] specs = selected[i].getSpecializations();
141       for (int s = 0; s < specs.length; s++)
142         addChoosableIfNotAdded(choosable, result, specs[s]);
143
144       DjRelation[] rels = selected[i].getDetailRelations();
145       for (int s = 0; s < rels.length; s++)
146         addChoosableIfNotAdded(choosable, result, rels[s].getDetailExtent());
147
148       rels = selected[i].getMasterRelations();
149       for (int s = 0; s < rels.length; s++)
150         addChoosableIfNotAdded(choosable, result, rels[s].getMasterExtent());
151     }
152
153     _selection = (DjExtent[]) result.toArray(new DjExtent[0]);
154   }
155
156   private void updateList()
157   {
158
159     ArrayList JavaDoc filtered = new ArrayList JavaDoc();
160
161     if (_chkSelectionRelated.isSelected()) filtered.addAll(Arrays.asList(_selection));
162     else filtered.addAll(Arrays.asList(_extents));
163
164     DjPackage thisPackage = (DjPackage) _cbbPackage.getSelectedItem();
165     if (thisPackage != _allPackages)
166     {
167       if (thisPackage == _defaultPackage) filterPackages(filtered, null);
168       else filterPackages(filtered, thisPackage);
169     }
170
171     _lstExtents.setListData(filtered.toArray());
172
173     if (_lstExtents.getModel().getSize() > 0) _lstExtents.setSelectedIndex(0);
174
175   }
176
177   private void filterPackages(ArrayList JavaDoc filtered, DjPackage pack)
178   {
179     int i = 0;
180     while (i < filtered.size())
181     {
182       DjExtent ext = (DjExtent) filtered.get(i);
183
184       if (ext.getPackage() != pack) filtered.remove(i);
185       else i++;
186     }
187   }
188
189   void jbInit() throws Exception JavaDoc
190   {
191     panel1.setLayout(borderLayout1);
192     jPanel1.setLayout(borderLayout2);
193
194     _butOk.setRequestFocusEnabled(false);
195     _butOk.setText(Messages.getString("global.Ok"));
196     _butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
197     {
198       public void actionPerformed(ActionEvent JavaDoc e)
199       {
200         _butOk_actionPerformed(e);
201       }
202     });
203     _butCancel.setRequestFocusEnabled(false);
204     _butCancel.setText(Messages.getString("global.Cancel"));
205     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
206     {
207       public void actionPerformed(ActionEvent JavaDoc e)
208       {
209         _butCancel_actionPerformed(e);
210       }
211     });
212     _lstExtents.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
213     {
214       public void mouseClicked(MouseEvent JavaDoc e)
215       {
216         _lstExtents_mouseClicked(e);
217       }
218     });
219     _lstExtents.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
220     {
221       public void keyPressed(KeyEvent JavaDoc e)
222       {
223         _lstExtents_keyPressed(e);
224       }
225     });
226     _chkSelectionRelated.setText(Messages.getString("ExtentSelector.SelectionRelated"));
227     _chkSelectionRelated.setToolTipText(Messages.getString("ExtentSelector.SelectionRelatedTooltip"));
228     _chkSelectionRelated.addActionListener(new java.awt.event.ActionListener JavaDoc()
229     {
230       public void actionPerformed(ActionEvent JavaDoc e)
231       {
232         _chkSelectionRelated_actionPerformed(e);
233       }
234     });
235     jPanel2.setLayout(borderLayout3);
236     _lblPackage.setText(" Package ");
237     _cbbPackage.addActionListener(new java.awt.event.ActionListener JavaDoc()
238     {
239       public void actionPerformed(ActionEvent JavaDoc e)
240       {
241         _cbbPackage_actionPerformed(e);
242       }
243     });
244     _lstExtents.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
245     getContentPane().add(panel1);
246     panel1.add(jPanel1, BorderLayout.SOUTH);
247     jPanel1.add(jPanel3, BorderLayout.EAST);
248     jPanel3.add(_butCancel, null);
249     jPanel3.add(_butOk, null);
250     jPanel1.add(_chkSelectionRelated, BorderLayout.WEST);
251     panel1.add(jScrollPane1, BorderLayout.CENTER);
252     panel1.add(jPanel2, BorderLayout.NORTH);
253     jPanel2.add(_lblPackage, BorderLayout.WEST);
254     jScrollPane1.getViewport().add(_lstExtents, null);
255     jPanel2.add(_cbbPackage, BorderLayout.CENTER);
256     Util.sizeButtons(jPanel3);
257   }
258
259   void _butOk_actionPerformed(ActionEvent JavaDoc e)
260   {
261     _canceled = false;
262     setVisible(false);
263   }
264
265   public boolean wasCancelled()
266   {
267     return _canceled;
268   }
269
270   public DjExtent[] getSelectedExtents()
271   {
272     ArrayList JavaDoc result = new ArrayList JavaDoc();
273     ListModel JavaDoc mdl = _lstExtents.getModel();
274     for (int i = 0; i < mdl.getSize(); i++)
275     {
276       if (_lstExtents.isSelectedIndex(i)) result.add(mdl.getElementAt(i));
277     }
278     return (DjExtent[]) result.toArray(new DjExtent[0]);
279   }
280
281   public DjExtent[] getUnSelectedExtents()
282   {
283     ArrayList JavaDoc result = new ArrayList JavaDoc();
284     ListModel JavaDoc mdl = _lstExtents.getModel();
285     for (int i = 0; i < mdl.getSize(); i++)
286     {
287       if (!_lstExtents.isSelectedIndex(i)) result.add(mdl.getElementAt(i));
288     }
289     return (DjExtent[]) result.toArray(new DjExtent[0]);
290   }
291
292   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
293   {
294     setVisible(false);
295   }
296
297   void _lstExtents_mouseClicked(MouseEvent JavaDoc e)
298   {
299     if (e.getClickCount() > 1) _butOk_actionPerformed(null);
300   }
301
302   void handleEnterEscape(KeyEvent JavaDoc e)
303   {
304     if (e.getKeyCode() == KeyEvent.VK_ENTER) _butOk_actionPerformed(null);
305     if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null);
306   }
307
308   void _lstExtents_keyPressed(KeyEvent JavaDoc e)
309   {
310     handleEnterEscape(e);
311   }
312
313   void _chkSelectionRelated_actionPerformed(ActionEvent JavaDoc e)
314   {
315     updateList();
316   }
317
318   void _cbbPackage_actionPerformed(ActionEvent JavaDoc e)
319   {
320     updateList();
321
322   }
323
324   public void setMultiSelect(boolean b)
325   {
326     if (b) _lstExtents.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
327     else _lstExtents.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
328   }
329
330 }
331
Popular Tags