KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > specifier > dialogs > ChooserDialog


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.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.util.ArrayList JavaDoc;
38 import java.util.Properties JavaDoc;
39
40 import javax.swing.JButton JavaDoc;
41 import javax.swing.JDialog JavaDoc;
42 import javax.swing.JOptionPane JavaDoc;
43 import javax.swing.JPanel JavaDoc;
44
45 import com.genimen.djeneric.language.Messages;
46 import com.genimen.djeneric.repository.DjExtent;
47 import com.genimen.djeneric.repository.DjObject;
48 import com.genimen.djeneric.repository.DjSession;
49 import com.genimen.djeneric.tools.common.DjenericTool;
50 import com.genimen.djeneric.tools.specifier.editor.ChooserPanel;
51 import com.genimen.djeneric.ui.Util;
52 import com.genimen.djeneric.util.DjLogger;
53
54 public class ChooserDialog extends JDialog JavaDoc
55 {
56   private static final long serialVersionUID = 1L;
57   JPanel JavaDoc panel1 = new JPanel JavaDoc();
58   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
59   ChooserPanel _chooserPanel;
60   JPanel JavaDoc _buttonPanel = new JPanel JavaDoc();
61   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
62   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
63   JButton JavaDoc _butOk = new JButton JavaDoc();
64   JButton JavaDoc _butCancel = new JButton JavaDoc();
65   boolean _wasCancelled = true;
66   DjObject _selectedObject = null;
67   boolean _required;
68   boolean _valueWasSelected = false;
69   JButton JavaDoc _butNone = new JButton JavaDoc();
70
71   public ChooserDialog(Frame JavaDoc frame, DjSession session, DjExtent extent, ArrayList JavaDoc excludeTheseProperties, String JavaDoc title,
72                        boolean required, int x, int y)
73   {
74     super(frame, (title == null ? Messages.getString("ChooserDialog.Choose", extent.getNameSingular()) : title), true);
75     try
76     {
77       _required = required;
78       _chooserPanel = new ChooserPanel(session, extent)
79       {
80         private static final long serialVersionUID = 1L;
81
82         public void acceptHit()
83         {
84           _butOk_actionPerformed(null);
85         }
86
87         public void escapeHit()
88         {
89           _butCancel_actionPerformed(null);
90         }
91       };
92
93       _chooserPanel.hideDefaultOkCCancelButtons(true);
94
95       jbInit();
96       _butNone.setVisible(!required);
97
98       if (_chooserPanel.getQueryableProperties().length > 5)
99       {
100         int w = _chooserPanel.getPreferredSize().width * 2 + 20;
101         int h = _chooserPanel.getPreferredSize().height / 2 + 40;
102         _chooserPanel.setPreferredSize(new Dimension JavaDoc(w, h));
103       }
104
105       int calculatedWidth = Math.max(_chooserPanel.getPreferredSize().width + 10, panel1.getPreferredSize().width + 10);
106       int calculatedHeight = _chooserPanel.getPreferredSize().height + _buttonPanel.getPreferredSize().height + 50;
107
108       Dimension JavaDoc dim = getStoredDimension(calculatedWidth, calculatedHeight);
109
110       setSize(dim);
111       Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
112       Dimension JavaDoc frameSize = getSize();
113
114       if (x != 0)
115       {
116         if (x + frameSize.width > screenSize.width) x = screenSize.width - frameSize.width;
117         if (y + frameSize.height > screenSize.height) y = screenSize.height - frameSize.height;
118         if (x < 0) x = 0;
119         if (y < 0) y = 0;
120         setLocation(x, y);
121       }
122       else
123       {
124         setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
125       }
126       setVisible(true);
127     }
128     catch (Exception JavaDoc ex)
129     {
130       DjLogger.log(ex);
131     }
132   }
133
134   private Dimension JavaDoc getStoredDimension(int calculatedWidth, int calculatedHeight)
135   {
136     int width = calculatedWidth;
137     int height = calculatedHeight;
138
139     Properties JavaDoc props = getProperties();
140     if (props != null)
141     {
142       String JavaDoc chooserId = getChooserId();
143       width = Integer.parseInt(props.getProperty(chooserId + ".width", "" + calculatedWidth));
144       height = Integer.parseInt(props.getProperty(chooserId + ".height", "" + calculatedWidth));
145       if (width < calculatedWidth) width = calculatedWidth;
146       if (height < calculatedHeight) height = calculatedHeight;
147     }
148
149     return new Dimension JavaDoc(width, height);
150   }
151
152   private Properties JavaDoc getProperties()
153   {
154     Frame JavaDoc frame = Util.findActiveFrame();
155     if (frame instanceof DjenericTool)
156     {
157       DjenericTool tool = (DjenericTool) frame;
158       return tool.getProperties();
159     }
160     return null;
161   }
162
163   private String JavaDoc getChooserId()
164   {
165     String JavaDoc title = getTitle();
166     StringBuffer JavaDoc id = new StringBuffer JavaDoc(50);
167     for (int i = 0; i < title.length(); i++)
168       if (Character.isJavaIdentifierPart(title.charAt(i))) id.append(title.charAt(i));
169     if (id.length() == 0) id.append("genericchooser");
170     return id.toString();
171   }
172
173   void jbInit() throws Exception JavaDoc
174   {
175     panel1.setLayout(borderLayout1);
176     _buttonPanel.setLayout(borderLayout2);
177     _butOk.setText(Messages.getString("global.Ok"));
178     _butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
179     {
180       public void actionPerformed(ActionEvent JavaDoc e)
181       {
182         _butOk_actionPerformed(e);
183       }
184     });
185     _butCancel.setText(Messages.getString("global.Cancel"));
186     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
187     {
188       public void actionPerformed(ActionEvent JavaDoc e)
189       {
190         _butCancel_actionPerformed(e);
191       }
192     });
193     _butNone.setOpaque(true);
194     _butNone.setText(Messages.getString("global.None"));
195     _butNone.addActionListener(new java.awt.event.ActionListener JavaDoc()
196     {
197       public void actionPerformed(ActionEvent JavaDoc e)
198       {
199         _butNone_actionPerformed(e);
200       }
201     });
202     getContentPane().add(panel1);
203
204     jPanel3.add(_butCancel, null);
205     jPanel3.add(_butNone, null);
206     jPanel3.add(_butOk, null);
207     _buttonPanel.add(jPanel3, BorderLayout.EAST);
208
209     panel1.add(_buttonPanel, BorderLayout.SOUTH);
210     panel1.add(_chooserPanel, BorderLayout.CENTER);
211     Util.sizeButtons(jPanel3);
212   }
213
214   public boolean wasCancelled()
215   {
216     return _wasCancelled;
217   }
218
219   void _butOk_actionPerformed(ActionEvent JavaDoc e)
220   {
221     try
222     {
223       _selectedObject = _chooserPanel.getSelectedObject();
224       if (_selectedObject != null)
225       {
226         _wasCancelled = false;
227         _valueWasSelected = true;
228
229         storeDimensions();
230         setVisible(false);
231       }
232     }
233     catch (Exception JavaDoc x)
234     {
235       JOptionPane.showMessageDialog(this, x.getMessage(), Messages.getString("global.InvalidEntry"),
236                                     JOptionPane.WARNING_MESSAGE);
237     }
238   }
239
240   private void storeDimensions()
241   {
242     Properties JavaDoc props = getProperties();
243     if (props != null)
244     {
245       String JavaDoc chooserId = getChooserId();
246       props.setProperty(chooserId + ".width", "" + getWidth());
247       props.setProperty(chooserId + ".height", "" + getHeight());
248     }
249   }
250
251   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
252   {
253     _valueWasSelected = false;
254     storeDimensions();
255     setVisible(false);
256
257   }
258
259   public DjObject getSelectedObject()
260   {
261     if (_valueWasSelected) return _selectedObject;
262     else return null;
263   }
264
265   void _butNone_actionPerformed(ActionEvent JavaDoc e)
266   {
267     _valueWasSelected = false;
268     _selectedObject = null;
269     _wasCancelled = false;
270     setVisible(false);
271   }
272
273 }
Popular Tags