KickJava   Java API By Example, From Geeks To Geeks.

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


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
31 package com.genimen.djeneric.tools.modeler.userperspective;
32
33 import java.awt.BorderLayout JavaDoc;
34 import java.awt.Component JavaDoc;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.MouseEvent JavaDoc;
37 import java.util.ArrayList JavaDoc;
38
39 import javax.swing.DefaultListModel JavaDoc;
40 import javax.swing.JButton JavaDoc;
41 import javax.swing.JLabel JavaDoc;
42 import javax.swing.JList JavaDoc;
43 import javax.swing.JOptionPane JavaDoc;
44 import javax.swing.JPanel JavaDoc;
45 import javax.swing.JScrollPane JavaDoc;
46 import javax.swing.ListSelectionModel JavaDoc;
47
48 import com.genimen.djeneric.language.Messages;
49 import com.genimen.djeneric.structure.ScriptDefinition;
50 import com.genimen.djeneric.tools.modeler.ModelEditor;
51 import com.genimen.djeneric.ui.DjCloseable;
52 import com.genimen.djeneric.ui.DjCloseableCloser;
53 import com.genimen.djeneric.ui.DjTabbedPane;
54 import com.genimen.djeneric.ui.DjVerticalFlowLayout;
55 import com.genimen.djeneric.util.DjLogger;
56
57 public class ScriptEditor extends JPanel JavaDoc implements DjCloseableCloser
58 {
59   private static final long serialVersionUID = 1L;
60   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
61   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
62   JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
63   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
64   JList JavaDoc _lstScripts = new JList JavaDoc();
65   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
66   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
67   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
68   DjVerticalFlowLayout flowLayout1 = new DjVerticalFlowLayout();
69   JButton JavaDoc _butEdit = new JButton JavaDoc();
70   JButton JavaDoc _butCreate = new JButton JavaDoc();
71   JButton JavaDoc _butDelete = new JButton JavaDoc();
72   DjTabbedPane _tabs = new DjTabbedPane();
73   ViewEditor _viewEditor;
74   ModelEditor _editor;
75   JLabel JavaDoc jLabel1 = new JLabel JavaDoc();
76   JButton JavaDoc _butMoveUp = new JButton JavaDoc();
77   JButton JavaDoc _butMoveDown = new JButton JavaDoc();
78
79   public ScriptEditor()
80   {
81     try
82     {
83       jbInit();
84     }
85     catch (Exception JavaDoc ex)
86     {
87       DjLogger.log(ex);
88     }
89   }
90
91   public void setEditorReferences(ViewEditor viewEditor)
92   {
93     _editor = viewEditor.getModelEditor();
94     _viewEditor = viewEditor;
95   }
96
97   void jbInit() throws Exception JavaDoc
98   {
99     this.setLayout(borderLayout1);
100     jPanel2.setLayout(borderLayout2);
101     jPanel3.setLayout(borderLayout3);
102     jPanel4.setLayout(flowLayout1);
103     _butEdit.setText(Messages.getString("global.Edit"));
104     _butEdit.addActionListener(new java.awt.event.ActionListener JavaDoc()
105     {
106       public void actionPerformed(ActionEvent JavaDoc e)
107       {
108         _butEdit_actionPerformed(e);
109       }
110     });
111     _butCreate.setText(Messages.getString("global.Create"));
112     _butCreate.addActionListener(new java.awt.event.ActionListener JavaDoc()
113     {
114       public void actionPerformed(ActionEvent JavaDoc e)
115       {
116         _butCreate_actionPerformed(e);
117       }
118     });
119     _butDelete.setText(Messages.getString("global.Delete"));
120     _butDelete.addActionListener(new java.awt.event.ActionListener JavaDoc()
121     {
122       public void actionPerformed(ActionEvent JavaDoc e)
123       {
124         _butDelete_actionPerformed(e);
125       }
126     });
127     _lstScripts.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
128     {
129       public void mouseClicked(MouseEvent JavaDoc e)
130       {
131         _lstScripts_mouseClicked(e);
132       }
133     });
134     _lstScripts.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
135     _butMoveDown.setText(Messages.getString("global.MoveDown"));
136     _butMoveDown.addActionListener(new java.awt.event.ActionListener JavaDoc()
137     {
138       public void actionPerformed(ActionEvent JavaDoc e)
139       {
140         _butMoveDown_actionPerformed(e);
141       }
142     });
143     _butMoveUp.setText(Messages.getString("global.MoveUp"));
144     jLabel1.setText(" ");
145     _butMoveUp.addActionListener(new java.awt.event.ActionListener JavaDoc()
146     {
147       public void actionPerformed(ActionEvent JavaDoc e)
148       {
149         _butMoveUp_actionPerformed(e);
150       }
151     });
152     jPanel4.add(_butMoveUp, null);
153     jPanel4.add(_butMoveDown, null);
154     jPanel4.add(jLabel1, null);
155     jPanel4.add(_butEdit, null);
156     jPanel4.add(_butCreate, null);
157     jPanel4.add(_butDelete, null);
158     this.add(jPanel2, BorderLayout.NORTH);
159     jPanel2.add(jScrollPane1, BorderLayout.CENTER);
160     jPanel2.add(jPanel3, BorderLayout.EAST);
161     jPanel3.add(jPanel4, BorderLayout.SOUTH);
162     jScrollPane1.getViewport().add(_lstScripts, null);
163     this.add(_tabs, BorderLayout.CENTER);
164   }
165
166   void _butEdit_actionPerformed(ActionEvent JavaDoc e)
167   {
168     editSelectedScript();
169   }
170
171   private void editSelectedScript()
172   {
173     if (_lstScripts.getModel().getSize() > 0)
174     {
175       ScriptDefinition script = (ScriptDefinition) _lstScripts.getSelectedValue();
176       if (script != null)
177       {
178         openEditor(script);
179       }
180     }
181   }
182
183   void _butCreate_actionPerformed(ActionEvent JavaDoc e)
184   {
185     ScriptDefinition sd = new ScriptDefinition(Messages.getString("ScriptEditor.NewScript"),
186         "script NewScript \"New Script\"\n" + "{\n" + " firstAction;\n" + "}\n" + "\n" + "constructor\n" + "{\n"
187             + "}\n" + "\n" + "controller\n" + "{\n" + " * -> traceEvents;\n" + "}\n" + "\n" + "action traceEvents\n"
188             + "{\n" + " trace(\"event: \" + $event);\n" + "}\n" + "\n" + "action firstAction \"First Action\"\n"
189             + "{\n" + "}\n\n" + "action lastAction \"Last Action\"\n" + "{\n" + " commit;\n" + " terminate script;\n"
190             + "}\n");
191
192     _viewEditor.addScript(sd, _lstScripts.getSelectedIndex() + 1);
193     openEditor(sd);
194     synchronize();
195     _lstScripts.setSelectedValue(sd, true);
196   }
197
198   protected void synchronize()
199   {
200     ScriptDefinition curr = null;
201
202     if (_lstScripts.getModel().getSize() > 0)
203     {
204       curr = (ScriptDefinition) _lstScripts.getSelectedValue();
205     }
206
207     DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
208     ScriptDefinition[] defs = _viewEditor.getScripts();
209     int idx = 0;
210     for (int i = 0; i < defs.length; i++)
211     {
212       model.addElement(defs[i]);
213       if (defs[i] == curr) idx = i;
214     }
215     _lstScripts.setModel(model);
216     _lstScripts.setSelectedIndex(idx);
217
218   }
219
220   protected void openEditor(ScriptDefinition sd)
221   {
222     ScriptEditorPanel sep = findScriptEditor(sd);
223     if (sep == null)
224     {
225       sep = new ScriptEditorPanel(sd, this, _editor);
226       _tabs.addTab(sd.toString(), ModelEditor.getImageIcon("script.gif"), sep);
227       _tabs.validate();
228     }
229     _tabs.setSelectedComponent(sep);
230     sep.requestFocus();
231   }
232
233   protected boolean isOkToDelete(ScriptDefinition script)
234   {
235     int result = JOptionPane.showOptionDialog(this, Messages.getString("ScriptEditor.DeleteScript", script.getTitle()),
236                                               Messages.getString("ScriptEditor.DeleteScript1"),
237                                               JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null,
238                                               new String JavaDoc[]{Messages.getString("global.Delete"),
239                                                   Messages.getString("global.Cancel")}, null);
240     return result == 0;
241   }
242
243   protected ScriptEditorPanel findScriptEditor(ScriptDefinition sd)
244   {
245     for (int i = 0; i < _tabs.getComponentCount(); i++)
246     {
247       if (_tabs.getComponent(i) instanceof ScriptEditorPanel)
248       {
249         ScriptEditorPanel sep = (ScriptEditorPanel) _tabs.getComponent(i);
250         if (sep.getScriptDefinition() == sd)
251         {
252           return sep;
253         }
254       }
255     }
256     return null;
257   }
258
259   void _butDelete_actionPerformed(ActionEvent JavaDoc e)
260   {
261     ScriptDefinition script = (ScriptDefinition) _lstScripts.getSelectedValue();
262     if (script != null)
263     {
264       if (isOkToDelete(script))
265       {
266         script.setMarkedForDelete(true);
267         _viewEditor.deleteScript(script);
268
269         ScriptEditorPanel sep = findScriptEditor(script);
270         if (sep != null) _tabs.remove(sep);
271         synchronize();
272       }
273     }
274   }
275
276   public void closeCloseable(DjCloseable closeable)
277   {
278     _tabs.remove((Component JavaDoc) closeable);
279     synchronize();
280   }
281
282   void _lstScripts_mouseClicked(MouseEvent JavaDoc e)
283   {
284     if (e.getClickCount() > 1) editSelectedScript();
285
286   }
287
288   void _butMoveUp_actionPerformed(ActionEvent JavaDoc e)
289   {
290     if (_lstScripts.getModel().getSize() > 0)
291     {
292       int idx = _lstScripts.getSelectedIndex();
293       if (idx > 0)
294       {
295         ArrayList JavaDoc lst = _viewEditor.getScriptsList();
296         Object JavaDoc o = lst.get(idx - 1);
297         lst.set(idx - 1, lst.get(idx));
298         lst.set(idx, o);
299         synchronize();
300       }
301     }
302   }
303
304   void _butMoveDown_actionPerformed(ActionEvent JavaDoc e)
305   {
306     if (_lstScripts.getModel().getSize() > 0)
307     {
308       int idx = _lstScripts.getSelectedIndex();
309       if (idx < _lstScripts.getModel().getSize() - 1)
310       {
311         ArrayList JavaDoc lst = _viewEditor.getScriptsList();
312         Object JavaDoc o = lst.get(idx + 1);
313         lst.set(idx + 1, lst.get(idx));
314         lst.set(idx, o);
315         synchronize();
316       }
317     }
318   }
319
320   public boolean hasEditorsOpen()
321   {
322     return _tabs.getComponentCount() > 0;
323   }
324 }
Popular Tags