xref: /AOO41X/main/scripting/java/org/openoffice/idesupport/ui/ScriptPanel.java (revision cd519653a6b6a9e2ff38774da567b1ae7cbeddbb)
1*cd519653SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cd519653SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cd519653SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cd519653SAndrew Rist  * distributed with this work for additional information
6*cd519653SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cd519653SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cd519653SAndrew Rist  * "License"); you may not use this file except in compliance
9*cd519653SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*cd519653SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*cd519653SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cd519653SAndrew Rist  * software distributed under the License is distributed on an
15*cd519653SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cd519653SAndrew Rist  * KIND, either express or implied.  See the License for the
17*cd519653SAndrew Rist  * specific language governing permissions and limitations
18*cd519653SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*cd519653SAndrew Rist  *************************************************************/
21*cd519653SAndrew Rist 
22*cd519653SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package org.openoffice.idesupport.ui;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.File;
27cdf0e10cSrcweir import java.util.Vector;
28cdf0e10cSrcweir import java.util.Enumeration;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import java.awt.BorderLayout;
31cdf0e10cSrcweir import java.awt.event.FocusEvent;
32cdf0e10cSrcweir import java.awt.event.FocusAdapter;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import javax.swing.JPanel;
35cdf0e10cSrcweir import javax.swing.JScrollPane;
36cdf0e10cSrcweir import javax.swing.JLabel;
37cdf0e10cSrcweir import javax.swing.JTextField;
38cdf0e10cSrcweir import javax.swing.JTable;
39cdf0e10cSrcweir import javax.swing.DefaultCellEditor;
40cdf0e10cSrcweir import javax.swing.table.TableCellEditor;
41cdf0e10cSrcweir import javax.swing.table.TableColumn;
42cdf0e10cSrcweir import javax.swing.table.AbstractTableModel;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir import com.sun.star.script.framework.container.ScriptEntry;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir public class ScriptPanel extends JPanel {
47cdf0e10cSrcweir     private ScriptTableModel model;
48cdf0e10cSrcweir     private JTable table;
49cdf0e10cSrcweir 
ScriptPanel(ScriptEntry[] scripts)50cdf0e10cSrcweir     public ScriptPanel(ScriptEntry[] scripts) {
51cdf0e10cSrcweir         model = new ScriptTableModel(scripts);
52cdf0e10cSrcweir         initUI();
53cdf0e10cSrcweir     }
54cdf0e10cSrcweir 
reload(ScriptEntry[] entries)55cdf0e10cSrcweir     public void reload(ScriptEntry[] entries) {
56cdf0e10cSrcweir         model.removeAll();
57cdf0e10cSrcweir         addScriptEntries(entries);
58cdf0e10cSrcweir     }
59cdf0e10cSrcweir 
addScriptEntries(ScriptEntry[] entries)60cdf0e10cSrcweir     public void addScriptEntries(ScriptEntry[] entries) {
61cdf0e10cSrcweir         for (int i = 0; i < entries.length; i++) {
62cdf0e10cSrcweir             ScriptEntry entry;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir             try {
65cdf0e10cSrcweir                 entry = (ScriptEntry) entries[i].clone();
66cdf0e10cSrcweir             }
67cdf0e10cSrcweir             catch (CloneNotSupportedException cnse) {
68cdf0e10cSrcweir                 entry = new ScriptEntry(entries[i].getLanguage(),
69cdf0e10cSrcweir                                         entries[i].getLanguageName(),
70cdf0e10cSrcweir                                         entries[i].getLogicalName(),
71cdf0e10cSrcweir                                         entries[i].getLocation());
72cdf0e10cSrcweir             }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir             model.add(entry);
75cdf0e10cSrcweir         }
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
removeSelectedRows()78cdf0e10cSrcweir     public void removeSelectedRows() {
79cdf0e10cSrcweir         int[] selections = table.getSelectedRows();
80cdf0e10cSrcweir 
81cdf0e10cSrcweir         for (int i = selections.length - 1; i >= 0; i--) {
82cdf0e10cSrcweir             model.remove(selections[i]);
83cdf0e10cSrcweir         }
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir 
removeAllRows()86cdf0e10cSrcweir     public void removeAllRows() {
87cdf0e10cSrcweir         model.removeAll();
88cdf0e10cSrcweir     }
89cdf0e10cSrcweir 
getScriptEntries()90cdf0e10cSrcweir     public Enumeration getScriptEntries() {
91cdf0e10cSrcweir         return model.getScriptEntries();
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir 
initUI()94cdf0e10cSrcweir     private void initUI() {
95cdf0e10cSrcweir         table = new JTable(model);
96cdf0e10cSrcweir         TableColumn column = table.getColumnModel().getColumn(1);
97cdf0e10cSrcweir         column.setCellEditor(new DefaultCellEditor(new JTextField()));
98cdf0e10cSrcweir 
99cdf0e10cSrcweir         table.addFocusListener(new FocusAdapter() {
100cdf0e10cSrcweir             public void focusLost(FocusEvent evt) {
101cdf0e10cSrcweir                 tableFocusLost(evt);
102cdf0e10cSrcweir             }
103cdf0e10cSrcweir         });
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         JScrollPane pane = new JScrollPane(table);
106cdf0e10cSrcweir         JLabel label = new JLabel("Scripts:");
107cdf0e10cSrcweir         label.setLabelFor(pane);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         BorderLayout layout = new BorderLayout();
110cdf0e10cSrcweir         setLayout(layout);
111cdf0e10cSrcweir         layout.setVgap(5);
112cdf0e10cSrcweir         add(label, BorderLayout.NORTH);
113cdf0e10cSrcweir         add(pane, BorderLayout.CENTER);
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir 
tableFocusLost(FocusEvent evt)116cdf0e10cSrcweir     private void tableFocusLost(FocusEvent evt) {
117cdf0e10cSrcweir         TableCellEditor editor = table.getCellEditor();
118cdf0e10cSrcweir         if (editor != null) {
119cdf0e10cSrcweir             Object value = editor.getCellEditorValue();
120cdf0e10cSrcweir             if (value != null)
121cdf0e10cSrcweir                 model.setValueAt(value,
122cdf0e10cSrcweir                                  table.getEditingRow(), table.getEditingColumn());
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     private class ScriptTableModel extends AbstractTableModel {
127cdf0e10cSrcweir         final String[] columnNames = {"Exported Method",
128cdf0e10cSrcweir                                       "Script Name"};
129cdf0e10cSrcweir 
130cdf0e10cSrcweir         private Vector scripts;
131cdf0e10cSrcweir         private int nextRow;
132cdf0e10cSrcweir 
ScriptTableModel(ScriptEntry[] entries)133cdf0e10cSrcweir         public ScriptTableModel(ScriptEntry[] entries) {
134cdf0e10cSrcweir             scripts = new Vector(entries.length + 11);
135cdf0e10cSrcweir             for (int i = 0; i < entries.length; i++) {
136cdf0e10cSrcweir                 scripts.addElement(entries[i]);
137cdf0e10cSrcweir             }
138cdf0e10cSrcweir             nextRow = entries.length;
139cdf0e10cSrcweir         }
140cdf0e10cSrcweir 
getColumnCount()141cdf0e10cSrcweir         public int getColumnCount() {
142cdf0e10cSrcweir             return columnNames.length;
143cdf0e10cSrcweir         }
144cdf0e10cSrcweir 
getRowCount()145cdf0e10cSrcweir         public int getRowCount() {
146cdf0e10cSrcweir             return scripts.size();
147cdf0e10cSrcweir         }
148cdf0e10cSrcweir 
getColumnName(int col)149cdf0e10cSrcweir         public String getColumnName(int col) {
150cdf0e10cSrcweir             return columnNames[col];
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir 
add(ScriptEntry entry)153cdf0e10cSrcweir         public void add(ScriptEntry entry) {
154cdf0e10cSrcweir             scripts.addElement(entry);
155cdf0e10cSrcweir             fireTableRowsInserted(nextRow, nextRow);
156cdf0e10cSrcweir             nextRow++;
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir 
remove(int row)159cdf0e10cSrcweir         public void remove(int row) {
160cdf0e10cSrcweir             scripts.removeElementAt(row);
161cdf0e10cSrcweir             fireTableRowsDeleted(row, row);
162cdf0e10cSrcweir             nextRow--;
163cdf0e10cSrcweir         }
164cdf0e10cSrcweir 
removeAll()165cdf0e10cSrcweir         public void removeAll() {
166cdf0e10cSrcweir             scripts.removeAllElements();
167cdf0e10cSrcweir             fireTableRowsDeleted(0, nextRow);
168cdf0e10cSrcweir             nextRow = 0;
169cdf0e10cSrcweir         }
170cdf0e10cSrcweir 
getScriptEntries()171cdf0e10cSrcweir         public Enumeration getScriptEntries() {
172cdf0e10cSrcweir             return scripts.elements();
173cdf0e10cSrcweir         }
174cdf0e10cSrcweir 
getValueAt(int row, int col)175cdf0e10cSrcweir         public Object getValueAt(int row, int col) {
176cdf0e10cSrcweir             String result = "";
177cdf0e10cSrcweir             ScriptEntry entry;
178cdf0e10cSrcweir 
179cdf0e10cSrcweir             entry = (ScriptEntry)scripts.elementAt(row);
180cdf0e10cSrcweir 
181cdf0e10cSrcweir             if (col == 0)
182cdf0e10cSrcweir                 result = entry.getLanguageName();
183cdf0e10cSrcweir             else if (col == 1)
184cdf0e10cSrcweir                 result = entry.getLogicalName();
185cdf0e10cSrcweir 
186cdf0e10cSrcweir             return result;
187cdf0e10cSrcweir         }
188cdf0e10cSrcweir 
isCellEditable(int row, int col)189cdf0e10cSrcweir         public boolean isCellEditable(int row, int col) {
190cdf0e10cSrcweir             if (col == 0)
191cdf0e10cSrcweir                 return false;
192cdf0e10cSrcweir             else
193cdf0e10cSrcweir                 return true;
194cdf0e10cSrcweir         }
195cdf0e10cSrcweir 
setValueAt(Object value, int row, int col)196cdf0e10cSrcweir         public void setValueAt(Object value, int row, int col) {
197cdf0e10cSrcweir             ScriptEntry entry = (ScriptEntry)scripts.elementAt(row);
198cdf0e10cSrcweir             entry.setLogicalName((String)value);
199cdf0e10cSrcweir             fireTableCellUpdated(row, col);
200cdf0e10cSrcweir         }
201cdf0e10cSrcweir     }
202cdf0e10cSrcweir }
203