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