KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.event.MouseEvent JavaDoc;
38 import java.awt.event.WindowEvent JavaDoc;
39
40 import javax.swing.DefaultComboBoxModel JavaDoc;
41 import javax.swing.JButton JavaDoc;
42 import javax.swing.JDialog JavaDoc;
43 import javax.swing.JLabel JavaDoc;
44 import javax.swing.JList JavaDoc;
45 import javax.swing.JOptionPane JavaDoc;
46 import javax.swing.JPanel JavaDoc;
47 import javax.swing.JScrollPane JavaDoc;
48 import javax.swing.JTextField JavaDoc;
49 import javax.swing.event.ListSelectionEvent JavaDoc;
50
51 import com.genimen.djeneric.language.Messages;
52 import com.genimen.djeneric.repository.DjModelView;
53 import com.genimen.djeneric.tools.modeler.ModelEditor;
54 import com.genimen.djeneric.ui.Util;
55
56 public class SaveAsDialog extends JDialog JavaDoc
57 {
58   private static final long serialVersionUID = 1L;
59   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
60   JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
61   JList JavaDoc _lstViews = new JList JavaDoc();
62   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
63   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
64   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
65   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
66   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
67   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
68   JButton JavaDoc _butOk = new JButton JavaDoc();
69   JButton JavaDoc _butCancel = new JButton JavaDoc();
70   JLabel JavaDoc jLabel1 = new JLabel JavaDoc();
71
72   boolean _canceled = true;
73   DjModelView[] _allViews;
74   DjModelView _modelView;
75   JPanel JavaDoc jPanel5 = new JPanel JavaDoc();
76   BorderLayout JavaDoc borderLayout4 = new BorderLayout JavaDoc();
77   JTextField JavaDoc _edtFileName = new JTextField JavaDoc();
78   BorderLayout JavaDoc borderLayout5 = new BorderLayout JavaDoc();
79
80   public SaveAsDialog(ModelEditor editor, DjModelView modelView) throws Exception JavaDoc
81   {
82     super(editor, Messages.getString("SaveAsDialog.SaveView"), true);
83     _modelView = modelView;
84     jbInit();
85
86     _allViews = editor.getPersistenceManager().getViews();
87
88     _lstViews.setModel(new DefaultComboBoxModel JavaDoc(_allViews));
89     _edtFileName.setText(modelView.getCode());
90     checkOkButtonEnabled();
91     setSize(290, 230);
92
93     Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
94     Dimension JavaDoc frameSize = getSize();
95     setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
96   }
97
98   public void execute() throws Exception JavaDoc
99   {
100     setVisible(true);
101     if (_canceled) throw new Exception JavaDoc(Messages.getString("SaveAsDialog.SaveAborted"));
102   }
103
104   void jbInit() throws Exception JavaDoc
105   {
106     this.getContentPane().setLayout(borderLayout1);
107     jPanel1.setLayout(borderLayout2);
108     jPanel3.setLayout(borderLayout3);
109     _butOk.setEnabled(false);
110     _butOk.setText(Messages.getString("global.Ok"));
111     _butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
112     {
113       public void actionPerformed(ActionEvent JavaDoc e)
114       {
115         _butOk_actionPerformed(e);
116       }
117     });
118     _butCancel.setText(Messages.getString("global.Cancel"));
119     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
120     {
121       public void actionPerformed(ActionEvent JavaDoc e)
122       {
123         _butCancel_actionPerformed(e);
124       }
125     });
126     jPanel2.setLayout(borderLayout4);
127     jLabel1.setText(" " + Messages.getString("SaveAsDialog.SaveAs") + " ");
128     _lstViews.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc()
129     {
130       public void valueChanged(ListSelectionEvent JavaDoc e)
131       {
132         _lstViews_valueChanged(e);
133       }
134     });
135     _edtFileName.setPreferredSize(new Dimension JavaDoc(230, 21));
136     _edtFileName.setToolTipText("");
137     _edtFileName.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
138     {
139       public void keyPressed(KeyEvent JavaDoc e)
140       {
141         _edtFileName_keyPressed(e);
142       }
143     });
144     jPanel5.setLayout(borderLayout5);
145     _lstViews.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
146     {
147       public void mouseClicked(MouseEvent JavaDoc e)
148       {
149         _lstViews_mouseClicked(e);
150       }
151     });
152     _lstViews.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
153     {
154       public void keyPressed(KeyEvent JavaDoc e)
155       {
156         _lstViews_keyPressed(e);
157       }
158     });
159     this.addWindowListener(new java.awt.event.WindowAdapter JavaDoc()
160     {
161       public void windowOpened(WindowEvent JavaDoc e)
162       {
163         this_windowOpened(e);
164       }
165     });
166     this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
167     this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
168     jPanel1.add(jPanel2, BorderLayout.CENTER);
169     jPanel2.add(jLabel1, BorderLayout.WEST);
170     jPanel2.add(jPanel5, BorderLayout.CENTER);
171     jPanel5.add(_edtFileName, BorderLayout.CENTER);
172     jPanel1.add(jPanel3, BorderLayout.SOUTH);
173     jPanel3.add(jPanel4, BorderLayout.EAST);
174     jPanel4.add(_butCancel, null);
175     jPanel4.add(_butOk, null);
176     jScrollPane1.getViewport().add(_lstViews, null);
177     Util.sizeButtons(jPanel4);
178   }
179
180   void _lstViews_valueChanged(ListSelectionEvent JavaDoc e)
181   {
182     if (_lstViews.getSelectedIndex() == -1) return;
183
184     _edtFileName.setText(_lstViews.getSelectedValue().toString());
185     checkOkButtonEnabled();
186   }
187
188   void _butOk_actionPerformed(ActionEvent JavaDoc e)
189   {
190     String JavaDoc fn = getFileName();
191     if (fn.trim().length() == 0) return;
192
193     String JavaDoc definition = _modelView.getDefinition();
194
195     for (int i = 0; i < _allViews.length; i++)
196     {
197       if (fn.equalsIgnoreCase(_allViews[i].getCode()))
198       {
199         int result = JOptionPane.showOptionDialog(this, Messages.getString("SaveAsDialog.Overwrite1", fn), Messages
200             .getString("global.Sure2Overwrite", fn), JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null,
201                                                   new String JavaDoc[]{Messages.getString("global.Overwrite"),
202                                                       Messages.getString("global.Cancel")}, null);
203         if (result != 0) return;
204         _modelView = _allViews[i];
205         break;
206       }
207     }
208     _canceled = false;
209     _modelView.setCode(getFileName().trim());
210     _modelView.setDefinition(definition);
211     setVisible(false);
212   }
213
214   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
215   {
216     setVisible(false);
217   }
218
219   protected String JavaDoc getFileName()
220   {
221     return _edtFileName.getText().trim();
222   }
223
224   public DjModelView getModelView()
225   {
226     return _modelView;
227   }
228
229   void checkOkButtonEnabled()
230   {
231     _butOk.setEnabled(_edtFileName.getText().trim().length() != 0);
232   }
233
234   void _edtFileName_keyPressed(KeyEvent JavaDoc e)
235   {
236     checkOkButtonEnabled();
237     if (e.getKeyCode() == KeyEvent.VK_ENTER) _butOk_actionPerformed(null);
238     if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null);
239   }
240
241   void _lstViews_mouseClicked(MouseEvent JavaDoc e)
242   {
243     if (e.getClickCount() > 1) _butOk_actionPerformed(null);
244   }
245
246   void _lstViews_keyPressed(KeyEvent JavaDoc e)
247   {
248     if (e.getKeyCode() == KeyEvent.VK_ENTER) _butOk_actionPerformed(null);
249     if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null);
250   }
251
252   void this_windowOpened(WindowEvent JavaDoc e)
253   {
254     _edtFileName.requestFocus();
255     _edtFileName.selectAll();
256   }
257
258   public void setViewName(String JavaDoc openedViewName)
259   {
260     _edtFileName.setText(openedViewName);
261
262   }
263 }
Popular Tags