KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > scripting > ScriptManager


1 /*
2
3 The contents of this file are subject to the Mozilla Public License Version 1.1
4 (the "License") you may not use this file except in compliance with the License.
5
6 You may obtain a copy of the License at http://www.mozilla.org/MPL/
7
8 Software distributed under the License is distributed on an "AS IS" basis,
9 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10 for the specific language governing rights and limitations under the License.
11
12 The Original Code is "BshInterpreter plugin for The Columba Project"
13
14 The Initial Developer of the Original Code is Celso Pinto
15 Portions created by Celso Pinto are Copyright (C) 2005.
16 Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
17
18 All Rights Reserved.
19
20 */

21 package org.columba.core.gui.scripting;
22
23 import java.awt.BorderLayout JavaDoc;
24 import java.awt.Color JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.FlowLayout JavaDoc;
27 import java.awt.Frame JavaDoc;
28 import java.awt.GridLayout JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32
33 import javax.swing.BorderFactory JavaDoc;
34 import javax.swing.JButton JavaDoc;
35 import javax.swing.JDialog JavaDoc;
36 import javax.swing.JOptionPane JavaDoc;
37 import javax.swing.JPanel JavaDoc;
38 import javax.swing.JScrollPane JavaDoc;
39 import javax.swing.JTabbedPane JavaDoc;
40 import javax.swing.JTable JavaDoc;
41 import javax.swing.ListSelectionModel JavaDoc;
42 import javax.swing.SwingConstants JavaDoc;
43 import javax.swing.SwingUtilities JavaDoc;
44 import javax.swing.event.ListSelectionEvent JavaDoc;
45 import javax.swing.event.ListSelectionListener JavaDoc;
46 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
47 import javax.swing.table.TableColumnModel JavaDoc;
48 import javax.swing.table.TableModel JavaDoc;
49
50 import org.columba.core.gui.base.ButtonWithMnemonic;
51 import org.columba.core.gui.base.SingleSideEtchedBorder;
52 import org.columba.core.scripting.ScriptLogger;
53 import org.columba.core.scripting.model.ColumbaScript;
54
55 /**
56     @author Celso Pinto (cpinto@yimports.com)
57  */

58 public class ScriptManager
59     extends JDialog JavaDoc
60     implements ActionListener JavaDoc
61 {
62
63     private static final Logger JavaDoc LOG = Logger.getLogger(ScriptManager.class.getName());
64
65
66     /*TODO move resources to a resource file */
67     private static final String JavaDoc
68         RES_WINDOW_TITLE = "Macros",
69         RES_EDIT_BUTTON = "&Edit",
70         RES_REFRESH_BUTTON = "&Refresh",
71         RES_REMOVE_BUTTON = "Re&move",
72         RES_CLOSE_BUTTON = "&Close",
73         RES_LOG_DETAILS_BUTTON = "&Details...",
74         RES_CLEAR_LOG_BUTTON = "C&lear",
75         RES_TAB_SCRIPTS = "Scripts",
76         RES_TAB_LOG = "Log",
77         RES_REMOVE_MESSAGE = "The selected scripts will be removed from disk.\nContinue?",
78         RES_WARNING_DIALOG_TITLE = "Remove scripts";
79
80     private static final String JavaDoc
81         EDIT_SCRIPT_ACTION = "edit",
82         REMOVE_SCRIPT_ACTION = "remove",
83         REFRESH_LIST_ACTION = "refresh",
84         CLOSE_ACTION = "close",
85         LOG_DETAILS_ACTION = "log_details",
86         CLEAR_LOG_ACTION = "clear_log";
87
88     private JTable JavaDoc
89         scriptsList,
90         logList;
91
92     private JButton JavaDoc
93         editScriptButton,
94         removeScriptButton,
95         refreshListButton,
96         logDetailsButton,
97         clearLogButton;
98
99
100     private ScriptManagerDocument document;
101
102     public ScriptManager(Frame JavaDoc parent, ScriptManagerDocument doc)
103     {
104         super(parent, RES_WINDOW_TITLE, true);
105         document = doc;
106
107         initGui();
108         setLocationRelativeTo(getParent());
109
110     }
111
112     private void initGui()
113     {
114
115         JPanel JavaDoc bottomPanel = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.RIGHT));
116         bottomPanel.setBorder(new SingleSideEtchedBorder(SwingConstants.TOP));
117         bottomPanel.add(createDialogActionsPanel());
118
119         getContentPane().setLayout(new BorderLayout JavaDoc());
120         JTabbedPane JavaDoc tabs = new JTabbedPane JavaDoc();
121         tabs.add(RES_TAB_SCRIPTS, createScriptsPanel());
122         tabs.add(RES_TAB_LOG, createLogPanel());
123         getContentPane().add(tabs, BorderLayout.CENTER);
124         getContentPane().add(bottomPanel, BorderLayout.SOUTH);
125
126         setButtonStatus();
127
128         pack();
129
130     }
131
132     private JPanel JavaDoc createLogPanel()
133     {
134         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc(10, 0));
135         panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
136         panel.add(createLogList(), BorderLayout.CENTER);
137         panel.add(createLogListActionsPanel(), BorderLayout.EAST);
138         return panel;
139     }
140
141     private JPanel JavaDoc createLogListActionsPanel()
142     {
143         JPanel JavaDoc panel = new JPanel JavaDoc(new GridLayout JavaDoc(2, 1, 0, 5));
144
145         logDetailsButton = new ButtonWithMnemonic(RES_LOG_DETAILS_BUTTON);
146         clearLogButton = new ButtonWithMnemonic(RES_CLEAR_LOG_BUTTON);
147
148         logDetailsButton.setActionCommand(LOG_DETAILS_ACTION);
149         clearLogButton.setActionCommand(CLEAR_LOG_ACTION);
150
151         logDetailsButton.addActionListener(this);
152         clearLogButton.addActionListener(this);
153
154         panel.add(logDetailsButton);
155         panel.add(clearLogButton);
156
157         JPanel JavaDoc container = new JPanel JavaDoc(new BorderLayout JavaDoc());
158         container.add(panel, BorderLayout.NORTH);
159         return container;
160
161     }
162
163     
164     private JPanel JavaDoc createLogList()
165     {
166         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc());
167
168         logList = new JTable JavaDoc(new ScriptLogTableModel());
169
170         logList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
171         logList.setShowGrid(true);
172         logList.sizeColumnsToFit(JTable.AUTO_RESIZE_ALL_COLUMNS);
173
174         logList.getSelectionModel()
175             .addListSelectionListener(new ListSelectionListener JavaDoc()
176             {
177                 public void valueChanged(ListSelectionEvent JavaDoc e)
178                 {
179                     if (e.getValueIsAdjusting()) return;
180
181                     setButtonStatus();
182                 }
183
184             });
185
186         TableColumnModel JavaDoc tcm = logList.getColumnModel();
187         tcm.getColumn(ScriptLogTableModel.MESSAGE_COLUMN).setPreferredWidth(400);
188         tcm.getColumn(ScriptLogTableModel.MESSAGE_COLUMN)
189             .setCellRenderer(new DefaultTableCellRenderer JavaDoc()
190             {
191                 protected void setValue(Object JavaDoc value)
192                 {
193                     setText(((ScriptLogger.LogEntry) value).getMessage());
194                 }
195             });
196
197         JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(logList);
198         scrollPane.setPreferredSize(new Dimension JavaDoc(450, 200));
199         scrollPane.getViewport().setBackground(Color.white);
200         panel.add(scrollPane, BorderLayout.CENTER);
201
202         return panel;
203     }
204
205     private JPanel JavaDoc createScriptsPanel()
206     {
207
208         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc(10, 0));
209         panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
210         panel.add(createScriptList(), BorderLayout.CENTER);
211         panel.add(createScriptActionsPanel(), BorderLayout.EAST);
212
213         return panel;
214
215     }
216
217     
218     private JPanel JavaDoc createScriptList()
219     {
220         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc());
221
222         scriptsList = new JTable JavaDoc(new ScriptsTableModel(document));
223
224         scriptsList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
225         scriptsList.setShowGrid(false);
226         scriptsList.sizeColumnsToFit(JTable.AUTO_RESIZE_NEXT_COLUMN);
227
228         scriptsList.getSelectionModel()
229             .addListSelectionListener(new ListSelectionListener JavaDoc()
230             {
231                 public void valueChanged(ListSelectionEvent JavaDoc e)
232                 {
233                     if (e.getValueIsAdjusting()) return;
234
235                     setButtonStatus();
236                 }
237             });
238
239         TableColumnModel JavaDoc tcm = scriptsList.getColumnModel();
240         tcm.getColumn(ScriptsTableModel.NAME_COLUMN).setPreferredWidth(100);
241         tcm.getColumn(ScriptsTableModel.NAME_COLUMN)
242             .setCellRenderer(new DefaultTableCellRenderer JavaDoc()
243             {
244                 protected void setValue(Object JavaDoc value)
245                 {
246                     setText(((ColumbaScript) value).getName());
247                 }
248             });
249
250         tcm.getColumn(ScriptsTableModel.AUTHOR_COLUMN).setPreferredWidth(100);
251         tcm.getColumn(ScriptsTableModel.DESCRIPTION_COLUMN).setPreferredWidth(250);
252
253         JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(scriptsList);
254         scrollPane.setPreferredSize(new Dimension JavaDoc(450, 200));
255         scrollPane.getViewport().setBackground(Color.white);
256         panel.add(scrollPane, BorderLayout.CENTER);
257
258         return panel;
259     }
260
261     private JPanel JavaDoc createScriptActionsPanel()
262     {
263
264         JPanel JavaDoc scriptActionsPanel = new JPanel JavaDoc(new GridLayout JavaDoc(3, 1, 0, 5));
265
266         editScriptButton = new ButtonWithMnemonic(RES_EDIT_BUTTON);
267         removeScriptButton = new ButtonWithMnemonic(RES_REMOVE_BUTTON);
268         refreshListButton = new ButtonWithMnemonic(RES_REFRESH_BUTTON);
269
270         editScriptButton.setActionCommand(EDIT_SCRIPT_ACTION);
271         removeScriptButton.setActionCommand(REMOVE_SCRIPT_ACTION);
272         refreshListButton.setActionCommand(REFRESH_LIST_ACTION);
273
274         editScriptButton.addActionListener(this);
275         removeScriptButton.addActionListener(this);
276         refreshListButton.addActionListener(this);
277
278         scriptActionsPanel.add(editScriptButton);
279         scriptActionsPanel.add(removeScriptButton);
280         scriptActionsPanel.add(refreshListButton);
281
282         JPanel JavaDoc container = new JPanel JavaDoc(new BorderLayout JavaDoc());
283         container.add(scriptActionsPanel, BorderLayout.NORTH);
284
285         return container;
286     }
287
288     private void setButtonStatus()
289     {
290         SwingUtilities.invokeLater(new Runnable JavaDoc()
291             {
292                 public void run()
293                 {
294
295                     boolean
296                         scriptSelected = scriptsList.getSelectedRowCount() > 0,
297                         hasLogMessages = logList.getRowCount() > 0,
298                         logMessageSelected = logList.getSelectedRow() > -1;
299
300                     editScriptButton.setEnabled(scriptSelected);
301                     removeScriptButton.setEnabled(scriptSelected);
302                     refreshListButton.setEnabled(scriptSelected);
303
304                     if (logMessageSelected)
305                     {
306                         ScriptLogger.LogEntry entry =
307                             (ScriptLogger.LogEntry)logList.getModel().getValueAt(logList.getSelectedRow(),
308                                                                                 ScriptLogTableModel.MESSAGE_COLUMN);
309
310                         logDetailsButton.setEnabled(entry.getDetails() != null &&
311                                                     entry.getDetails().length() > 0);
312                     }
313                     else
314                     {
315                         logDetailsButton.setEnabled(false);
316                     }
317
318                     clearLogButton.setEnabled(hasLogMessages);
319
320                 }
321             });
322
323
324     }
325
326     private JPanel JavaDoc createDialogActionsPanel()
327     {
328
329         JButton JavaDoc closeButton = new ButtonWithMnemonic(RES_CLOSE_BUTTON);
330         JPanel JavaDoc actionsPanel = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.RIGHT));
331
332         closeButton.setActionCommand(CLOSE_ACTION);
333         closeButton.addActionListener(this);
334         actionsPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
335         actionsPanel.add(closeButton);
336
337         return actionsPanel;
338
339     }
340
341     public void actionPerformed(ActionEvent JavaDoc event)
342     {
343         String JavaDoc command = event.getActionCommand();
344
345         if (command.equals(CLOSE_ACTION))
346         {
347             close();
348         }
349         else if (command.equals(REMOVE_SCRIPT_ACTION))
350         {
351
352             if (JOptionPane.showConfirmDialog(this,
353                     RES_REMOVE_MESSAGE,
354                     RES_WARNING_DIALOG_TITLE,
355                     JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
356             {
357                 return;
358             }
359
360             int[] selectedIndexes = scriptsList.getSelectedRows();
361             TableModel JavaDoc model = scriptsList.getModel();
362
363             ColumbaScript[] scripts = new ColumbaScript[selectedIndexes.length];
364             for (int i = 0; i < selectedIndexes.length; i++)
365             {
366                 scripts[i] = (ColumbaScript) model.getValueAt( selectedIndexes[i],
367                                                                 ScriptsTableModel.NAME_COLUMN);
368             }
369             document.removeScript(scripts);
370         }
371         else if (command.equals(REFRESH_LIST_ACTION))
372         {
373             document.refreshScriptList();
374         }
375         else if (command.equals(EDIT_SCRIPT_ACTION))
376         {
377             JOptionPane.showMessageDialog(this,
378                 "Ha ha, wouldn't it be really sweet if this were implemented?",
379                 "TODO",
380                 JOptionPane.INFORMATION_MESSAGE);
381         }
382         else if (command.equals(LOG_DETAILS_ACTION))
383         {
384             ScriptLogger.LogEntry entry =
385                 (ScriptLogger.LogEntry)logList.getModel().getValueAt(logList.getSelectedRow(),
386                                                                     ScriptLogTableModel.MESSAGE_COLUMN);
387
388             new MessageDetailsDialog(this,entry).setVisible(true);
389
390         }
391         else if (command.equals(CLEAR_LOG_ACTION))
392         {
393             ((ScriptLogTableModel) logList.getModel()).clearLog();
394         }
395         else
396         {
397             LOG.warning("Not handling: " + event.getActionCommand());
398         }
399
400         setButtonStatus();
401     }
402
403     private void close()
404     {
405
406         ((ScriptLogTableModel) logList.getModel()).dispose();
407
408         setVisible(false);
409         dispose();
410     }
411
412 }
413
Popular Tags