KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > extenteditor > MappingChooser


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.extenteditor;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Color JavaDoc;
34 import java.awt.Dimension JavaDoc;
35 import java.awt.FlowLayout JavaDoc;
36 import java.awt.Frame JavaDoc;
37 import java.awt.Toolkit JavaDoc;
38 import java.awt.event.ActionEvent JavaDoc;
39 import java.awt.event.KeyEvent JavaDoc;
40 import java.awt.event.KeyListener JavaDoc;
41 import java.util.Vector JavaDoc;
42
43 import javax.swing.DefaultComboBoxModel JavaDoc;
44 import javax.swing.JButton JavaDoc;
45 import javax.swing.JComboBox JavaDoc;
46 import javax.swing.JDialog JavaDoc;
47 import javax.swing.JLabel JavaDoc;
48 import javax.swing.JPanel JavaDoc;
49 import javax.swing.border.Border JavaDoc;
50 import javax.swing.border.EtchedBorder JavaDoc;
51
52 import com.genimen.djeneric.language.Messages;
53 import com.genimen.djeneric.repository.DjExtent;
54 import com.genimen.djeneric.repository.DjPersistenceManager;
55 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
56 import com.genimen.djeneric.ui.DjFocusMeLater;
57 import com.genimen.djeneric.ui.Util;
58 import com.genimen.djeneric.util.DjLogger;
59
60 public class MappingChooser extends JDialog JavaDoc implements KeyListener JavaDoc
61 {
62   private static final long serialVersionUID = 1L;
63   JPanel JavaDoc panel1 = new JPanel JavaDoc();
64   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
65   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
66   JPanel JavaDoc jPanel2 = 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
72   ColumnMapping[] mapping = new ColumnMapping[]{
73       new ColumnMapping(DjPersistenceManager.MAPPING_OBJECT_ID, "Object ID"),
74       new ColumnMapping(DjPersistenceManager.MAPPING_STR, "String250"),
75       new ColumnMapping(DjPersistenceManager.MAPPING_TXT, "String4000"),
76       new ColumnMapping(DjPersistenceManager.MAPPING_NUM, "Numeric"),
77       new ColumnMapping(DjPersistenceManager.MAPPING_DAT, "Timestamp"),
78       new ColumnMapping(DjPersistenceManager.MAPPING_REL, "<Relation>"),
79       new ColumnMapping(DjPersistenceManager.MAPPING_LNG, "byte[]")};
80
81   ColumnMapping[] mappingWithoutId = new ColumnMapping[mapping.length - 1];
82
83   JComboBox JavaDoc _cbbType = new JComboBox JavaDoc(mapping);
84   JComboBox JavaDoc _cbbNum = new JComboBox JavaDoc();
85   FlowLayout JavaDoc flowLayout1 = new FlowLayout JavaDoc();
86
87   String JavaDoc _initialValue;
88   int _initialNum;
89   boolean _wasCancelled = true;
90   DjExtent _extent;
91   FlowLayout JavaDoc flowLayout2 = new FlowLayout JavaDoc();
92   Border JavaDoc border1;
93   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
94   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
95   JLabel JavaDoc jLabel1 = new JLabel JavaDoc();
96   JLabel JavaDoc _lblPropertyName = new JLabel JavaDoc();
97
98   public MappingChooser(Frame JavaDoc frame, String JavaDoc title, String JavaDoc initalValue, DjExtent extent, String JavaDoc colName)
99   {
100     super(frame, title, true);
101     try
102     {
103       System.arraycopy(mapping, 1, mappingWithoutId, 0, mapping.length - 1);
104
105       _extent = extent;
106
107       if (initalValue.equalsIgnoreCase(DjPersistenceManager.MAPPING_OBJECT_ID))
108       {
109         _initialValue = initalValue;
110         _initialNum = -1;
111       }
112       else
113       {
114         _initialValue = initalValue.substring(0, 3);
115         _initialNum = Integer.parseInt(initalValue.substring(3));
116         try
117         {
118           _extent.getPropertyByMapping(DjPersistenceManager.MAPPING_OBJECT_ID);
119           // Defined by other property
120
_cbbType.setModel(new DefaultComboBoxModel JavaDoc(mappingWithoutId));
121         }
122         catch (Exception JavaDoc x)
123         {
124           DjLogger.log(x);
125         }
126       }
127
128       jbInit();
129       _lblPropertyName.setText(colName);
130
131       ColumnMapping curMap = new ColumnMapping(_initialValue, _initialValue);
132       for (int i = 0; i < _cbbType.getModel().getSize(); i++)
133       {
134         if (_cbbType.getModel().getElementAt(i).equals(curMap))
135         {
136           _cbbType.setSelectedIndex(i);
137           break;
138         }
139       }
140
141       _cbbNum.setSelectedItem(new Integer JavaDoc(_initialNum));
142       pack();
143
144       Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
145       Dimension JavaDoc frameSize = getSize();
146       setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
147       DjFocusMeLater.getNeededComponent(_cbbType).addKeyListener(this);
148       DjFocusMeLater.getNeededComponent(_cbbNum).addKeyListener(this);
149       addKeyListener(this);
150       setVisible(true);
151     }
152     catch (Exception JavaDoc ex)
153     {
154       DjLogger.log(ex);
155     }
156   }
157
158   void jbInit() throws Exception JavaDoc
159   {
160     border1 = new EtchedBorder JavaDoc(EtchedBorder.RAISED, Color.white, new Color JavaDoc(142, 142, 142));
161     panel1.setLayout(borderLayout1);
162     jPanel2.setLayout(borderLayout2);
163     _butOk.setText(Messages.getString("global.Ok"));
164     _butOk.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
165     {
166       public void keyPressed(KeyEvent JavaDoc e)
167       {
168         _cbbType_keyPressed(e);
169       }
170     });
171     _butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
172     {
173       public void actionPerformed(ActionEvent JavaDoc e)
174       {
175         _butOk_actionPerformed(e);
176       }
177     });
178     _butCancel.setText(Messages.getString("global.Cancel"));
179     _butCancel.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
180     {
181       public void keyPressed(KeyEvent JavaDoc e)
182       {
183         _cbbType_keyPressed(e);
184       }
185     });
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     jPanel1.setLayout(flowLayout1);
194     _cbbType.setPreferredSize(new Dimension JavaDoc(100, 23));
195     _cbbType.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
196     {
197       public void keyPressed(KeyEvent JavaDoc e)
198       {
199         _cbbType_keyPressed(e);
200       }
201     });
202     _cbbType.addActionListener(new java.awt.event.ActionListener JavaDoc()
203     {
204       public void actionPerformed(ActionEvent JavaDoc e)
205       {
206         _cbbType_actionPerformed(e);
207       }
208     });
209     _cbbNum.setPreferredSize(new Dimension JavaDoc(80, 23));
210     _cbbNum.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
211     {
212       public void keyPressed(KeyEvent JavaDoc e)
213       {
214         _cbbType_keyPressed(e);
215       }
216     });
217     jPanel3.setLayout(flowLayout2);
218     flowLayout2.setAlignment(FlowLayout.RIGHT);
219     jPanel1.setAlignmentX((float) 0.0);
220     jPanel1.setAlignmentY((float) 0.0);
221     jPanel4.setLayout(borderLayout3);
222     jLabel1.setText(Messages.getString("MappingChooser.MappingFor") + " ");
223     _lblPropertyName.setText(Messages.getString("global.Property"));
224     getContentPane().add(panel1);
225     panel1.add(jPanel1, BorderLayout.CENTER);
226     jPanel1.add(_cbbType, null);
227     jPanel1.add(_cbbNum, null);
228     panel1.add(jPanel2, BorderLayout.SOUTH);
229     jPanel2.add(jPanel3, BorderLayout.CENTER);
230     jPanel3.add(_butCancel, null);
231     jPanel3.add(_butOk, null);
232     panel1.add(jPanel4, BorderLayout.NORTH);
233     jPanel4.add(jLabel1, BorderLayout.WEST);
234     jPanel4.add(_lblPropertyName, BorderLayout.CENTER);
235     Util.sizeButtons(jPanel3);
236   }
237
238   void _butOk_actionPerformed(ActionEvent JavaDoc e)
239   {
240     _wasCancelled = false;
241     setVisible(false);
242   }
243
244   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
245   {
246     setVisible(false);
247   }
248
249   public String JavaDoc getMapping()
250   {
251     String JavaDoc num = "";
252     if (_cbbNum.isEnabled())
253     {
254       if (_cbbNum.getSelectedItem() == null) return null;
255       String JavaDoc n = _cbbNum.getSelectedItem().toString();
256       if (n.length() == 1) n = "0" + n;
257       num += n;
258     }
259     ColumnMapping map = (ColumnMapping) _cbbType.getSelectedItem();
260     return map.getValue() + num;
261   }
262
263   public boolean isCancelled()
264   {
265     return _wasCancelled;
266   }
267
268   private boolean propertyIsMapped(DjExtent extent, String JavaDoc map)
269   {
270     try
271     {
272       extent.getPropertyByMapping(map);
273       return true;
274     }
275     catch (ObjectNotDefinedException ond)
276     {
277       // ignore
278
}
279     DjExtent[] specializations = extent.getSpecializations();
280     for (int i = 0; i < specializations.length; i++)
281     {
282       if (propertyIsMapped(specializations[i], map)) return true;
283     }
284     return false;
285   }
286
287   void _cbbType_actionPerformed(ActionEvent JavaDoc e)
288   {
289     ColumnMapping mapping = (ColumnMapping) _cbbType.getSelectedItem();
290     String JavaDoc val = mapping.getValue();
291     if (val.equals(DjPersistenceManager.MAPPING_OBJECT_ID)) _cbbNum.setEnabled(false);
292     else
293     {
294       _cbbNum.setEnabled(true);
295       Vector JavaDoc v = new Vector JavaDoc();
296
297       int sz = DjPersistenceManager.getMaxMappingCount(val);
298
299       for (int i = 1; i <= sz; i++)
300       {
301         String JavaDoc n = "" + i;
302         if (n.length() == 1) n = "0" + n;
303         String JavaDoc map = val + n;
304         if ((i == _initialNum) && val.equals(_initialValue))
305         {
306           v.add(new Integer JavaDoc(i));
307         }
308         else
309         {
310           if (!propertyIsMapped(_extent, map)) v.add(new Integer JavaDoc(i));
311         }
312       }
313       _cbbNum.setModel(new DefaultComboBoxModel JavaDoc(v));
314       if (val.equals(_initialValue))
315       {
316         _cbbNum.setSelectedItem(new Integer JavaDoc(_initialNum));
317       }
318     }
319   }
320
321   public void keyTyped(KeyEvent JavaDoc e)
322   {
323   }
324
325   public void keyPressed(KeyEvent JavaDoc e)
326   {
327     if (_cbbNum.isPopupVisible()) return;
328     if (_cbbType.isPopupVisible()) return;
329
330     if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
331     {
332       _butCancel_actionPerformed(null);
333     }
334
335     if (e.getKeyCode() == KeyEvent.VK_ENTER)
336     {
337       _butOk_actionPerformed(null);
338     }
339   }
340
341   public void keyReleased(KeyEvent JavaDoc e)
342   {
343   }
344
345   void _cbbType_keyPressed(KeyEvent JavaDoc e)
346   {
347     if (_cbbNum.isPopupVisible() || _cbbType.isPopupVisible()) return;
348
349     if (e.getKeyCode() == KeyEvent.VK_ENTER) _butOk_actionPerformed(null);
350     if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null);
351   }
352 }
353
354 class ColumnMapping
355 {
356
357   String JavaDoc _value;
358   String JavaDoc _title;
359
360   public ColumnMapping(String JavaDoc value, String JavaDoc title)
361   {
362     _value = value;
363     _title = title;
364   }
365
366   public String JavaDoc toString()
367   {
368     return _title;
369   }
370
371   public String JavaDoc getValue()
372   {
373     return _value;
374   }
375
376   public boolean equals(Object JavaDoc o)
377   {
378     if (!(o instanceof ColumnMapping))
379     {
380       return false;
381     }
382     if ((_value == null) || (o == null)) return false;
383
384     return _value.equals(((ColumnMapping) o)._value);
385   }
386 }
Popular Tags