xref: /AOO41X/main/scripting/java/org/openoffice/idesupport/ui/ScriptPanel.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package org.openoffice.idesupport.ui;
29 
30 import java.io.File;
31 import java.util.Vector;
32 import java.util.Enumeration;
33 
34 import java.awt.BorderLayout;
35 import java.awt.event.FocusEvent;
36 import java.awt.event.FocusAdapter;
37 
38 import javax.swing.JPanel;
39 import javax.swing.JScrollPane;
40 import javax.swing.JLabel;
41 import javax.swing.JTextField;
42 import javax.swing.JTable;
43 import javax.swing.DefaultCellEditor;
44 import javax.swing.table.TableCellEditor;
45 import javax.swing.table.TableColumn;
46 import javax.swing.table.AbstractTableModel;
47 
48 import com.sun.star.script.framework.container.ScriptEntry;
49 
50 public class ScriptPanel extends JPanel {
51     private ScriptTableModel model;
52     private JTable table;
53 
54     public ScriptPanel(ScriptEntry[] scripts) {
55         model = new ScriptTableModel(scripts);
56         initUI();
57     }
58 
59     public void reload(ScriptEntry[] entries) {
60         model.removeAll();
61         addScriptEntries(entries);
62     }
63 
64     public void addScriptEntries(ScriptEntry[] entries) {
65         for (int i = 0; i < entries.length; i++) {
66             ScriptEntry entry;
67 
68             try {
69                 entry = (ScriptEntry) entries[i].clone();
70             }
71             catch (CloneNotSupportedException cnse) {
72                 entry = new ScriptEntry(entries[i].getLanguage(),
73                                         entries[i].getLanguageName(),
74                                         entries[i].getLogicalName(),
75                                         entries[i].getLocation());
76             }
77 
78             model.add(entry);
79         }
80     }
81 
82     public void removeSelectedRows() {
83         int[] selections = table.getSelectedRows();
84 
85         for (int i = selections.length - 1; i >= 0; i--) {
86             model.remove(selections[i]);
87         }
88     }
89 
90     public void removeAllRows() {
91         model.removeAll();
92     }
93 
94     public Enumeration getScriptEntries() {
95         return model.getScriptEntries();
96     }
97 
98     private void initUI() {
99         table = new JTable(model);
100         TableColumn column = table.getColumnModel().getColumn(1);
101         column.setCellEditor(new DefaultCellEditor(new JTextField()));
102 
103         table.addFocusListener(new FocusAdapter() {
104             public void focusLost(FocusEvent evt) {
105                 tableFocusLost(evt);
106             }
107         });
108 
109         JScrollPane pane = new JScrollPane(table);
110         JLabel label = new JLabel("Scripts:");
111         label.setLabelFor(pane);
112 
113         BorderLayout layout = new BorderLayout();
114         setLayout(layout);
115         layout.setVgap(5);
116         add(label, BorderLayout.NORTH);
117         add(pane, BorderLayout.CENTER);
118     }
119 
120     private void tableFocusLost(FocusEvent evt) {
121         TableCellEditor editor = table.getCellEditor();
122         if (editor != null) {
123             Object value = editor.getCellEditorValue();
124             if (value != null)
125                 model.setValueAt(value,
126                                  table.getEditingRow(), table.getEditingColumn());
127         }
128     }
129 
130     private class ScriptTableModel extends AbstractTableModel {
131         final String[] columnNames = {"Exported Method",
132                                       "Script Name"};
133 
134         private Vector scripts;
135         private int nextRow;
136 
137         public ScriptTableModel(ScriptEntry[] entries) {
138             scripts = new Vector(entries.length + 11);
139             for (int i = 0; i < entries.length; i++) {
140                 scripts.addElement(entries[i]);
141             }
142             nextRow = entries.length;
143         }
144 
145         public int getColumnCount() {
146             return columnNames.length;
147         }
148 
149         public int getRowCount() {
150             return scripts.size();
151         }
152 
153         public String getColumnName(int col) {
154             return columnNames[col];
155         }
156 
157         public void add(ScriptEntry entry) {
158             scripts.addElement(entry);
159             fireTableRowsInserted(nextRow, nextRow);
160             nextRow++;
161         }
162 
163         public void remove(int row) {
164             scripts.removeElementAt(row);
165             fireTableRowsDeleted(row, row);
166             nextRow--;
167         }
168 
169         public void removeAll() {
170             scripts.removeAllElements();
171             fireTableRowsDeleted(0, nextRow);
172             nextRow = 0;
173         }
174 
175         public Enumeration getScriptEntries() {
176             return scripts.elements();
177         }
178 
179         public Object getValueAt(int row, int col) {
180             String result = "";
181             ScriptEntry entry;
182 
183             entry = (ScriptEntry)scripts.elementAt(row);
184 
185             if (col == 0)
186                 result = entry.getLanguageName();
187             else if (col == 1)
188                 result = entry.getLogicalName();
189 
190             return result;
191         }
192 
193         public boolean isCellEditable(int row, int col) {
194             if (col == 0)
195                 return false;
196             else
197                 return true;
198         }
199 
200         public void setValueAt(Object value, int row, int col) {
201             ScriptEntry entry = (ScriptEntry)scripts.elementAt(row);
202             entry.setLogicalName((String)value);
203             fireTableCellUpdated(row, col);
204         }
205     }
206 }
207