KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > userperspective > EditorEditor


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 modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes 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,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.modeler.userperspective;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Component JavaDoc;
34 import java.awt.Dimension JavaDoc;
35 import java.awt.FlowLayout JavaDoc;
36 import java.awt.event.ActionEvent JavaDoc;
37 import java.awt.event.MouseEvent JavaDoc;
38
39 import javax.swing.DefaultListModel JavaDoc;
40 import javax.swing.JButton JavaDoc;
41 import javax.swing.JList JavaDoc;
42 import javax.swing.JOptionPane JavaDoc;
43 import javax.swing.JPanel JavaDoc;
44 import javax.swing.JScrollPane JavaDoc;
45 import javax.swing.tree.DefaultTreeModel JavaDoc;
46
47 import com.genimen.djeneric.language.Messages;
48 import com.genimen.djeneric.structure.EditorDefinition;
49 import com.genimen.djeneric.structure.ExtentUsage;
50 import com.genimen.djeneric.tools.modeler.ModelEditor;
51 import com.genimen.djeneric.tools.modeler.dialogs.CreateEditorDialog;
52 import com.genimen.djeneric.ui.DjCloseable;
53 import com.genimen.djeneric.ui.DjCloseableCloser;
54 import com.genimen.djeneric.ui.DjTabbedPane;
55 import com.genimen.djeneric.ui.DjVerticalFlowLayout;
56 import com.genimen.djeneric.util.DjLogger;
57
58 /**
59  * Description of the Class
60  *
61  *@author Wido Riezebos
62  *@created 5 juni 2002
63  */

64 public class EditorEditor extends JPanel JavaDoc implements DjCloseableCloser
65 {
66   private static final long serialVersionUID = 1L;
67   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
68   ExtentUsageNode _root;
69   DefaultTreeModel JavaDoc _treeModel;
70
71   ViewEditor _viewEditor;
72   ModelEditor _editor;
73   JPanel JavaDoc jPanel10 = new JPanel JavaDoc();
74   BorderLayout JavaDoc borderLayout5 = new BorderLayout JavaDoc();
75   PropertyUsageTableModel _emptyModel = new PropertyUsageTableModel(null);
76   JPanel JavaDoc jPanel15 = new JPanel JavaDoc();
77   BorderLayout JavaDoc borderLayout7 = new BorderLayout JavaDoc();
78   JScrollPane JavaDoc jScrollPane2 = new JScrollPane JavaDoc();
79   JList JavaDoc _lstEditors = new JList JavaDoc();
80   JPanel JavaDoc jPanel18 = new JPanel JavaDoc();
81   BorderLayout JavaDoc borderLayout12 = new BorderLayout JavaDoc();
82   JPanel JavaDoc jPanel19 = new JPanel JavaDoc();
83   JButton JavaDoc _butNewEditor = new JButton JavaDoc();
84   DjVerticalFlowLayout flowLayout1 = new DjVerticalFlowLayout();
85   JButton JavaDoc _butDeleteEditor = new JButton JavaDoc();
86   DjTabbedPane _tabs = new DjTabbedPane();
87   JButton JavaDoc _butEdit = new JButton JavaDoc();
88   JButton JavaDoc _butCopy = new JButton JavaDoc();
89
90   /**
91    * Constructor for the EditorEditor object
92    */

93   public EditorEditor()
94   {
95     try
96     {
97       jbInit();
98     }
99     catch (Exception JavaDoc ex)
100     {
101       JOptionPane.showMessageDialog(this, ex.getMessage());
102       DjLogger.log(ex);
103     }
104   }
105
106   /**
107    * Sets the editorReferences of the EditorEditor object
108    *
109    *@param navigatorEditor The new editorReferences value
110    *@param editor The new editorReferences value
111    */

112   public void setEditorReferences(ViewEditor viewEditor)
113   {
114     _editor = viewEditor.getModelEditor();
115     _viewEditor = viewEditor;
116   }
117
118   public void synchronize()
119   {
120     synchronize(null);
121   }
122
123   public void synchronize(EditorDefinition editorToSelect)
124   {
125     EditorDefinition[] editorDefs = _viewEditor.getEditors();
126     DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
127     int idx = -1;
128     if (editorToSelect == null)
129     {
130       idx = _lstEditors.getSelectedIndex();
131     }
132
133     for (int i = 0; i < editorDefs.length; i++)
134     {
135       if (editorDefs[i] == editorToSelect) idx = i;
136       model.addElement(editorDefs[i]);
137     }
138     _lstEditors.setModel(model);
139
140     if (idx >= model.getSize()) idx = model.getSize() - 1;
141
142     if (idx != -1)
143     {
144       _lstEditors.setSelectedIndex(idx);
145       _lstEditors.ensureIndexIsVisible(idx);
146     }
147     else if (editorDefs.length > 0) _lstEditors.setSelectedIndex(0);
148   }
149
150   public void clear()
151   {
152     _lstEditors.setModel(new DefaultListModel JavaDoc());
153   }
154
155   void jbInit() throws Exception JavaDoc
156   {
157     this.setLayout(borderLayout1);
158     jPanel10.setLayout(borderLayout5);
159     jPanel15.setLayout(borderLayout7);
160     jScrollPane2.setAutoscrolls(true);
161     jScrollPane2.setPreferredSize(new Dimension JavaDoc(260, 100));
162     jPanel18.setLayout(borderLayout12);
163     _butNewEditor.setText(Messages.getString("global.new"));
164     _butNewEditor.addActionListener(new java.awt.event.ActionListener JavaDoc()
165     {
166       public void actionPerformed(ActionEvent JavaDoc e)
167       {
168         _butNewEditor_actionPerformed(e);
169       }
170     });
171     jPanel19.setLayout(flowLayout1);
172     flowLayout1.setAlignment(FlowLayout.LEFT);
173     flowLayout1.setVgap(0);
174     flowLayout1.setHorizontalFill(true);
175     _butDeleteEditor.setText(Messages.getString("global.Delete"));
176     _butDeleteEditor.addActionListener(new java.awt.event.ActionListener JavaDoc()
177     {
178       public void actionPerformed(ActionEvent JavaDoc e)
179       {
180         _butDeleteEditor_actionPerformed(e);
181       }
182     });
183     _butEdit.setText(Messages.getString("global.Edit"));
184     _butEdit.addActionListener(new java.awt.event.ActionListener JavaDoc()
185     {
186       public void actionPerformed(ActionEvent JavaDoc e)
187       {
188         _butEdit_actionPerformed(e);
189       }
190     });
191     _butCopy.setText(Messages.getString("EditorEditor.Copy"));
192     _butCopy.addActionListener(new java.awt.event.ActionListener JavaDoc()
193     {
194       public void actionPerformed(ActionEvent JavaDoc e)
195       {
196         _butCopy_actionPerformed(e);
197       }
198     });
199     _lstEditors.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
200     {
201       public void mouseClicked(MouseEvent JavaDoc e)
202       {
203         _lstEditors_mouseClicked(e);
204       }
205     });
206     this.add(jPanel10, BorderLayout.NORTH);
207     jPanel10.add(jPanel15, BorderLayout.NORTH);
208     jPanel15.add(jPanel18, BorderLayout.EAST);
209     jPanel18.add(jPanel19, BorderLayout.SOUTH);
210     jPanel19.add(_butEdit, null);
211     jPanel19.add(_butNewEditor, null);
212     jPanel19.add(_butCopy, null);
213     jPanel19.add(_butDeleteEditor, null);
214     jPanel15.add(jScrollPane2, BorderLayout.CENTER);
215     this.add(_tabs, BorderLayout.CENTER);
216     jScrollPane2.getViewport().add(_lstEditors, null);
217   }
218
219   void _butNewEditor_actionPerformed(ActionEvent JavaDoc e)
220   {
221     CreateEditorDialog ced = new CreateEditorDialog(_editor, _viewEditor.getEditors());
222     if (!ced.wasCanceled())
223     {
224       ExtentUsage newUsage = new ExtentUsage(ced.getExtent());
225       newUsage.setMultiRow(false);
226
227       EditorDefinition newEditor = new EditorDefinition(ced.getEditorName(), newUsage);
228       _viewEditor.addEditor(newEditor);
229       synchronize(newEditor);
230
231       EditorEditorPanel editorEditor = editEditor(newEditor);
232       editorEditor.addAllProperties();
233     }
234   }
235
236   boolean isOkToDelete(EditorDefinition editor)
237   {
238     int result = JOptionPane.showOptionDialog(this, Messages.getString("EditorEditor.DeleteEditor", editor.getName()),
239                                               Messages.getString("EditorEditor.DeleteEditor1"),
240                                               JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null,
241                                               new String JavaDoc[]{Messages.getString("global.Delete"),
242                                                   Messages.getString("global.Cancel")}, null);
243     return result == 0;
244   }
245
246   void _butDeleteEditor_actionPerformed(ActionEvent JavaDoc e)
247   {
248     EditorDefinition editor = (EditorDefinition) _lstEditors.getSelectedValue();
249     if (editor != null)
250     {
251       if (isOkToDelete(editor))
252       {
253         EditorEditorPanel ep = getEditor(editor);
254         if (ep != null) _tabs.remove(ep);
255
256         editor.setMarkedForDelete(true);
257         _viewEditor.deleteEditor(editor);
258         synchronize();
259       }
260     }
261   }
262
263   protected EditorEditorPanel getEditor(EditorDefinition editorDef)
264   {
265     for (int i = 0; i < _tabs.getTabCount(); i++)
266     {
267       Component JavaDoc c = _tabs.getComponent(i);
268       if (c instanceof EditorEditorPanel)
269       {
270         EditorEditorPanel ep = (EditorEditorPanel) c;
271         if (ep.getCurrentEditorDefinition() == editorDef)
272         {
273           return ep;
274         }
275       }
276     }
277     return null;
278
279   }
280
281   void _butEdit_actionPerformed(ActionEvent JavaDoc e)
282   {
283     EditorDefinition editor = (EditorDefinition) _lstEditors.getSelectedValue();
284
285     // make sure the editor is valid
286
editor.removeInvalidExtents();
287     editEditor(editor);
288   }
289
290   protected EditorEditorPanel editEditor(EditorDefinition editor)
291   {
292     EditorEditorPanel editorEditor = null;
293
294     if (editor != null)
295     {
296       if (editor.getUsages() == null)
297       {
298         JOptionPane.showMessageDialog(this, Messages.getString("ModelEditor.EditorInvalid"));
299         return null;
300       }
301       else
302       {
303         editorEditor = getEditor(editor);
304         if (editorEditor == null)
305         {
306           editorEditor = new EditorEditorPanel(this, _viewEditor, editor);
307           _tabs.addTab(editor.getName(), ModelEditor.getImageIcon("editor.gif"), editorEditor);
308         }
309         _tabs.setSelectedComponent(editorEditor);
310       }
311     }
312     return editorEditor;
313   }
314
315   public void closeCloseable(DjCloseable edt)
316   {
317     _tabs.remove((Component JavaDoc) edt);
318     synchronize();
319   }
320
321   void _butCopy_actionPerformed(ActionEvent JavaDoc e)
322   {
323     EditorDefinition editor = (EditorDefinition) _lstEditors.getSelectedValue();
324     if (editor != null)
325     {
326       EditorDefinition newEditor;
327       newEditor = (EditorDefinition) editor.clone();
328       newEditor.setName(Messages.getString("global.CopyOf", editor.getName()));
329       _viewEditor.addEditor(newEditor);
330       synchronize(newEditor);
331     }
332
333   }
334
335   void _lstEditors_mouseClicked(MouseEvent JavaDoc e)
336   {
337     if (e.getClickCount() > 1) _butEdit_actionPerformed(null);
338
339   }
340
341   public boolean hasEditorsOpen()
342   {
343     return _tabs.getComponentCount() > 0;
344   }
345 }
Popular Tags