KickJava   Java API By Example, From Geeks To Geeks.

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


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.Toolkit JavaDoc;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.FocusEvent JavaDoc;
37 import java.awt.event.KeyEvent JavaDoc;
38
39 import javax.swing.DefaultComboBoxModel JavaDoc;
40 import javax.swing.JButton JavaDoc;
41 import javax.swing.JCheckBox JavaDoc;
42 import javax.swing.JComboBox JavaDoc;
43 import javax.swing.JDialog JavaDoc;
44 import javax.swing.JLabel JavaDoc;
45 import javax.swing.JPanel JavaDoc;
46 import javax.swing.JTextField JavaDoc;
47
48 import com.genimen.djeneric.language.Messages;
49 import com.genimen.djeneric.repository.DjExtent;
50 import com.genimen.djeneric.repository.DjPersistenceManager;
51 import com.genimen.djeneric.repository.DjProperty;
52 import com.genimen.djeneric.repository.DjRelation;
53 import com.genimen.djeneric.tools.modeler.ModelEditor;
54 import com.genimen.djeneric.ui.DjVerticalFlowLayout;
55 import com.genimen.djeneric.ui.Util;
56 import com.genimen.djeneric.util.DjLogger;
57
58 public class CreateRelationDialog extends JDialog JavaDoc
59 {
60   private static final long serialVersionUID = 1L;
61   JPanel JavaDoc panel1 = new JPanel JavaDoc();
62   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
63   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
64   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
65   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
66   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
67   JButton JavaDoc _butOk = new JButton JavaDoc();
68   JButton JavaDoc _butCancel = new JButton JavaDoc();
69   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
70   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
71   JLabel JavaDoc jLabel1 = new JLabel JavaDoc();
72   JLabel JavaDoc jLabel2 = new JLabel JavaDoc();
73   DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout();
74   DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout();
75   JLabel JavaDoc jLabel3 = new JLabel JavaDoc();
76   JLabel JavaDoc jLabel4 = new JLabel JavaDoc();
77   JLabel JavaDoc jLabel5 = new JLabel JavaDoc();
78   JLabel JavaDoc _lblMaster = new JLabel JavaDoc();
79   JLabel JavaDoc _lblDetail = new JLabel JavaDoc();
80   JTextField JavaDoc _edtRelation = new JTextField JavaDoc();
81   JTextField JavaDoc _edtProperty = new JTextField JavaDoc();
82   JCheckBox JavaDoc _chkContains = new JCheckBox JavaDoc();
83   DjExtent _master;
84   DjExtent _detail;
85   String JavaDoc _mapping;
86   ModelEditor _editor;
87   JLabel JavaDoc jLabel6 = new JLabel JavaDoc();
88   JCheckBox JavaDoc _chkRequired = new JCheckBox JavaDoc();
89   JLabel JavaDoc _lblUnicity = new JLabel JavaDoc();
90   JComboBox JavaDoc _cbbUnicity = new JComboBox JavaDoc();
91
92   public CreateRelationDialog(ModelEditor editor, DjExtent master, DjExtent detail)
93   {
94     super(editor, Messages.getString("CreateRelationDialog.RelationProperties"), true);
95     try
96     {
97       _editor = editor;
98       _master = master;
99       _detail = detail;
100
101       jbInit();
102       _edtRelation.setText(_detail.getDefaultMasterRelationName());
103       _edtRelation.setText(fixRelationName());
104
105       String JavaDoc propName = master.getObjectType();
106       if (propName.length() > 1) propName = propName.substring(0, 1).toLowerCase() + propName.substring(1);
107       _edtProperty.setText(propName);
108       _edtProperty.setText(fixPropertyName());
109       _lblMaster.setText(master.getName());
110       _lblDetail.setText(detail.getName());
111       pack();
112
113       _mapping = _detail.findNextFreeMapping(DjPersistenceManager.MAPPING_REL);
114
115       _cbbUnicity.setModel(new DefaultComboBoxModel JavaDoc(new String JavaDoc[]{"1:n", "1:1"}));
116       if (_mapping == null)
117       {
118         editor.setStatusMessage(Messages.getString("CreateRelationDialog.NoMoreRels"), false);
119         return;
120       }
121       Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
122       Dimension JavaDoc frameSize = getSize();
123       setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
124
125       setVisible(true);
126     }
127     catch (Exception JavaDoc ex)
128     {
129       DjLogger.log(ex);
130     }
131   }
132
133   protected String JavaDoc fixRelationName()
134   {
135     String JavaDoc tryString;
136     int tryCount = 0;
137
138     do
139     {
140       tryString = _edtRelation.getText();
141       if (tryCount > 0) tryString += tryCount;
142       tryCount++;
143     }
144     while (_master.hasRelation(tryString));
145
146     if (tryString.length() > 1) tryString = tryString.toLowerCase().substring(0, 1) + tryString.substring(1);
147     return tryString;
148   }
149
150   protected String JavaDoc fixPropertyName()
151   {
152     String JavaDoc tryString;
153     int tryCount = 0;
154
155     do
156     {
157       tryString = _edtProperty.getText();
158       if (tryCount > 0) tryString += tryCount;
159       tryCount++;
160     }
161     while (_detail.hasProperty(tryString));
162
163     return tryString;
164   }
165
166   void jbInit() throws Exception JavaDoc
167   {
168     panel1.setLayout(borderLayout1);
169     this.getContentPane().setLayout(borderLayout2);
170     jPanel1.setLayout(borderLayout3);
171     _butOk.setText(Messages.getString("global.Ok"));
172     _butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
173     {
174       public void actionPerformed(ActionEvent JavaDoc e)
175       {
176         _butOk_actionPerformed(e);
177       }
178     });
179     _butCancel.setText(Messages.getString("global.Cancel"));
180     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
181     {
182       public void actionPerformed(ActionEvent JavaDoc e)
183       {
184         _butCancel_actionPerformed(e);
185       }
186     });
187     jLabel1.setText(Messages.getString("CreateRelationDialog.MasterExtent"));
188     jLabel2.setText(Messages.getString("CreateRelationDialog.DetailExtent"));
189     jPanel3.setLayout(verticalFlowLayout1);
190     jPanel4.setLayout(verticalFlowLayout2);
191     jLabel3.setText(Messages.getString("CreateRelationDialog.RelationName"));
192     jLabel4.setText(Messages.getString("CreateRelationDialog.DetailProperty"));
193     jLabel5.setText(Messages.getString("CreateRelationDialog.MasterContainsDetails"));
194     _lblMaster.setText(Messages.getString("global.Master"));
195     _lblDetail.setText(Messages.getString("CreateRelationDialog.detail"));
196     _edtRelation.setPreferredSize(new Dimension JavaDoc(200, 21));
197     _edtRelation.addFocusListener(new java.awt.event.FocusAdapter JavaDoc()
198     {
199       public void focusGained(FocusEvent JavaDoc e)
200       {
201         _edtRelation_focusGained(e);
202       }
203     });
204     _edtRelation.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
205     {
206       public void keyPressed(KeyEvent JavaDoc e)
207       {
208         handleEnterEscape(e);
209       }
210     });
211     _edtProperty.setPreferredSize(new Dimension JavaDoc(200, 21));
212     _edtProperty.addFocusListener(new java.awt.event.FocusAdapter JavaDoc()
213     {
214       public void focusGained(FocusEvent JavaDoc e)
215       {
216         _edtProperty_focusGained(e);
217       }
218     });
219     _edtProperty.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
220     {
221       public void keyPressed(KeyEvent JavaDoc e)
222       {
223         handleEnterEscape(e);
224       }
225     });
226     _chkContains.setSelected(true);
227     _chkContains.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
228     {
229       public void keyPressed(KeyEvent JavaDoc e)
230       {
231         handleEnterEscape(e);
232       }
233     });
234     jLabel6.setText(Messages.getString("global.Required"));
235     _chkRequired.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
236     {
237       public void keyPressed(KeyEvent JavaDoc e)
238       {
239         handleEnterEscape(e);
240       }
241     });
242
243     _chkRequired.setSelected(true);
244
245     _lblUnicity.setText(Messages.getString("RelationTableModel.Unicity"));
246     getContentPane().add(panel1, BorderLayout.CENTER);
247     this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
248     jPanel1.add(jPanel2, BorderLayout.EAST);
249     jPanel2.add(_butCancel, null);
250     jPanel2.add(_butOk, null);
251     panel1.add(jPanel3, BorderLayout.WEST);
252     jPanel3.add(jLabel1, null);
253     jPanel3.add(jLabel2, null);
254     jPanel3.add(jLabel3, null);
255     jPanel3.add(jLabel4, null);
256     jPanel3.add(jLabel5, null);
257     jPanel3.add(jLabel6, null);
258     jPanel3.add(_lblUnicity, null);
259     panel1.add(jPanel4, BorderLayout.CENTER);
260     jPanel4.add(_lblMaster, null);
261     jPanel4.add(_lblDetail, null);
262     jPanel4.add(_edtRelation, null);
263     jPanel4.add(_edtProperty, null);
264     jPanel4.add(_chkContains, null);
265     jPanel4.add(_chkRequired, null);
266     jPanel4.add(_cbbUnicity, null);
267     Util.sizeButtons(jPanel2);
268     Util.sizeLabels(jPanel3);
269     Util.sizeLabels(jPanel4);
270   }
271
272   void _butOk_actionPerformed(ActionEvent JavaDoc e)
273   {
274     DjRelation newRel = null;
275     DjProperty newCol = null;
276     try
277     {
278       _edtRelation.setText(fixRelationName());
279       _edtProperty.setText(fixPropertyName());
280
281       newCol = new DjProperty(_edtProperty.getText(), _mapping, _edtProperty.getText(), _master.getNameSingular(),
282           _master, _chkRequired.isSelected(), _detail.getPropertyCount() * 10, null, Messages
283               .getString("CreateRelationDialog.LinkTo", _master.getNameSingular()));
284       _detail.addProperty(newCol);
285       newRel = _editor.getPersistenceManager().createRelation(_edtRelation.getText(), _master, _detail, newCol,
286                                                               _chkContains.isSelected(), _master.getNamePlural());
287       newRel.setUnicity(_cbbUnicity.getSelectedIndex() == 0 ? DjRelation.ONE_TO_MANY : DjRelation.ONE_TO_ONE);
288       _detail.addMasterRelation(newRel);
289       _master.addDetailRelation(newRel);
290       _editor.validateModel(false);
291       setVisible(false);
292     }
293     catch (Exception JavaDoc x)
294     {
295       //undo any actions above
296
_detail.removeMasterRelation(newRel);
297       _master.removeDetailRelation(newRel);
298       _detail.removeProperty(newCol);
299       _editor.setStatusMessage(x);
300     }
301   }
302
303   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
304   {
305     setVisible(false);
306   }
307
308   void handleEnterEscape(KeyEvent JavaDoc e)
309   {
310     if (e.getKeyCode() == KeyEvent.VK_ENTER) _butOk_actionPerformed(null);
311     if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null);
312   }
313
314   void _edtRelation_focusGained(FocusEvent JavaDoc e)
315   {
316     _edtRelation.selectAll();
317
318   }
319
320   void _edtProperty_focusGained(FocusEvent JavaDoc e)
321   {
322     _edtProperty.selectAll();
323
324   }
325
326   void _chkRequired_keyTyped(KeyEvent JavaDoc e)
327   {
328
329   }
330
331   void _chkRequired_keyPressed(KeyEvent JavaDoc e)
332   {
333
334   }
335
336   void _chkRequired_keyReleased(KeyEvent JavaDoc e)
337   {
338
339   }
340 }
Popular Tags