KickJava   Java API By Example, From Geeks To Geeks.

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


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.KeyEvent JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Collections JavaDoc;
39
40 import javax.swing.DefaultComboBoxModel JavaDoc;
41 import javax.swing.JButton JavaDoc;
42 import javax.swing.JCheckBox JavaDoc;
43 import javax.swing.JComboBox JavaDoc;
44 import javax.swing.JDialog JavaDoc;
45 import javax.swing.JLabel JavaDoc;
46 import javax.swing.JOptionPane JavaDoc;
47 import javax.swing.JPanel JavaDoc;
48 import javax.swing.JTextField JavaDoc;
49
50 import com.genimen.djeneric.language.Messages;
51 import com.genimen.djeneric.repository.DjExtent;
52 import com.genimen.djeneric.structure.EditorDefinition;
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 import com.genimen.djeneric.util.DjStringComparator;
58
59 public class CreateEditorDialog extends JDialog JavaDoc
60 {
61   private static final long serialVersionUID = 1L;
62   JPanel JavaDoc panel1 = new JPanel JavaDoc();
63   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
64   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
65   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
66   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
67   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
68   JButton JavaDoc _butOk = new JButton JavaDoc();
69   JButton JavaDoc _butCancel = new JButton JavaDoc();
70   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
71   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
72   JLabel JavaDoc jLabel1 = new JLabel JavaDoc();
73   JLabel JavaDoc jLabel2 = new JLabel JavaDoc();
74   DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout();
75   DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout();
76   JTextField JavaDoc _edtEditorName = new JTextField JavaDoc();
77   DjExtent _master;
78   DjExtent _detail;
79   String JavaDoc _mapping;
80   ModelEditor _modelEditor;
81   JComboBox JavaDoc _cbbExtent = new JComboBox JavaDoc();
82   boolean _wasCanceled = true;
83   EditorDefinition[] _editorsDefined;
84   EditorDefinition _existingEditor;
85   JCheckBox JavaDoc _chkShowDiagOnly = new JCheckBox JavaDoc();
86
87   public CreateEditorDialog(ModelEditor editor, EditorDefinition[] editorsDefined)
88   {
89     super(editor, Messages.getString("CreateEditorDialog.NewEditor"), true);
90     try
91     {
92       _editorsDefined = editorsDefined;
93       _modelEditor = editor;
94
95       jbInit();
96
97       reloadExtentList();
98
99       if (_cbbExtent.getSelectedIndex() == -1) return;
100
101       setSize(new Dimension JavaDoc(300, 150));
102
103       Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
104       Dimension JavaDoc frameSize = getSize();
105       setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
106       setVisible(true);
107     }
108     catch (Exception JavaDoc ex)
109     {
110       DjLogger.log(ex);
111     }
112   }
113
114   void jbInit() throws Exception JavaDoc
115   {
116     panel1.setLayout(borderLayout1);
117     this.getContentPane().setLayout(borderLayout2);
118     jPanel1.setLayout(borderLayout3);
119     _butOk.setText(Messages.getString("global.Ok"));
120     _butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
121     {
122       public void actionPerformed(ActionEvent JavaDoc e)
123       {
124         _butOk_actionPerformed(e);
125       }
126     });
127     _butCancel.setText(Messages.getString("global.Cancel"));
128     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
129     {
130       public void actionPerformed(ActionEvent JavaDoc e)
131       {
132         _butCancel_actionPerformed(e);
133       }
134     });
135     jLabel1.setText(Messages.getString("global.Extent"));
136     jLabel2.setText(Messages.getString("global.Name"));
137     jPanel3.setLayout(verticalFlowLayout1);
138     jPanel4.setLayout(verticalFlowLayout2);
139     _edtEditorName.setPreferredSize(new Dimension JavaDoc(200, 21));
140     _edtEditorName.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
141     {
142       public void keyPressed(KeyEvent JavaDoc e)
143       {
144         handleEnterEscape(e);
145       }
146     });
147     _cbbExtent.setPreferredSize(new Dimension JavaDoc(240, 21));
148     _chkShowDiagOnly.setSelected(true);
149     _chkShowDiagOnly.setText(Messages.getString("ViewEditor.DiagramExtentsOnly"));
150     _chkShowDiagOnly.addActionListener(new java.awt.event.ActionListener JavaDoc()
151     {
152       public void actionPerformed(ActionEvent JavaDoc e)
153       {
154         _chkShowDiagOnly_actionPerformed(e);
155       }
156     });
157     getContentPane().add(panel1, BorderLayout.CENTER);
158     this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
159     jPanel1.add(jPanel2, BorderLayout.EAST);
160     jPanel2.add(_butCancel, null);
161     jPanel2.add(_butOk, null);
162     jPanel1.add(_chkShowDiagOnly, BorderLayout.NORTH);
163     panel1.add(jPanel3, BorderLayout.WEST);
164     jPanel3.add(jLabel1, null);
165     jPanel3.add(jLabel2, null);
166     panel1.add(jPanel4, BorderLayout.CENTER);
167     jPanel4.add(_cbbExtent, null);
168     jPanel4.add(_edtEditorName, null);
169     Util.sizeButtons(this);
170     Util.sizeLabels(jPanel3);
171   }
172
173   void _butOk_actionPerformed(ActionEvent JavaDoc e)
174   {
175     String JavaDoc name = _edtEditorName.getText().trim();
176     if (name.length() == 0 && _cbbExtent.getSelectedItem() != null)
177     {
178       name = _cbbExtent.getSelectedItem().toString() + "Editor";
179     }
180
181     for (int i = 0; i < _editorsDefined.length; i++)
182     {
183       if (_editorsDefined[i].getName().equals(name) && (_editorsDefined[i] != _existingEditor))
184       {
185         JOptionPane.showMessageDialog(this, Messages.getString("CreateEditorDialog.NameInUse", name), Messages
186             .getString("CreateEditorDialog.DuplicateName"), JOptionPane.WARNING_MESSAGE);
187         return;
188       }
189     }
190     _edtEditorName.setText(name);
191     _wasCanceled = false;
192     setVisible(false);
193   }
194
195   public boolean wasCanceled()
196   {
197     return _wasCanceled;
198   }
199
200   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
201   {
202     setVisible(false);
203   }
204
205   void handleEnterEscape(KeyEvent JavaDoc e)
206   {
207     if (e.getKeyCode() == KeyEvent.VK_ENTER) _butOk_actionPerformed(null);
208     if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null);
209   }
210
211   public String JavaDoc getEditorName()
212   {
213     return _edtEditorName.getText().trim();
214   }
215
216   public DjExtent getExtent()
217   {
218     return (DjExtent) _cbbExtent.getSelectedItem();
219   }
220
221   void _chkShowDiagOnly_actionPerformed(ActionEvent JavaDoc e)
222   {
223     reloadExtentList();
224   }
225
226   private void reloadExtentList()
227   {
228     DefaultComboBoxModel JavaDoc model;
229
230     ArrayList JavaDoc onDiag = _modelEditor.getExtentsOnDiagram();
231     Collections.sort(onDiag, new DjStringComparator(false));
232
233     if (_chkShowDiagOnly.isSelected()) model = new DefaultComboBoxModel JavaDoc(onDiag.toArray());
234     else model = new DefaultComboBoxModel JavaDoc(_modelEditor.getPersistenceManager().getExtentsSorted());
235
236     _cbbExtent.setModel(model);
237     if (model.getSize() > 0)
238     {
239       _cbbExtent.setSelectedIndex(0);
240     }
241   }
242 }
Popular Tags