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.ArrayList;
29 
30 import java.lang.reflect.Method;
31 import java.lang.reflect.Modifier;
32 
33 import javax.swing.JPanel;
34 import javax.swing.JScrollPane;
35 import javax.swing.JList;
36 import javax.swing.JTable;
37 import javax.swing.table.AbstractTableModel;
38 import javax.swing.JLabel;
39 import java.awt.BorderLayout;
40 import java.net.URL;
41 import java.net.URLClassLoader;
42 import java.net.MalformedURLException;
43 
44 import com.sun.star.script.framework.container.ScriptEntry;
45 import org.openoffice.idesupport.MethodFinder;
46 import org.openoffice.idesupport.ExtensionFinder;
47 import org.openoffice.idesupport.JavaFinder;
48 
49 public class MethodPanel extends JPanel {
50 
51     private File basedir;
52     private Vector classpath;
53     private final static String FIRST_PARAM =
54         "drafts.com.sun.star.script.framework.runtime.XScriptContext";
55 
56     // private JTable table;
57     // private MethodTableModel model;
58     private JList list;
59     private ScriptEntry[] values;
60 
MethodPanel(File basedir, Vector classpath, String language)61     public MethodPanel(File basedir, Vector classpath, String language) {
62         this.basedir = basedir;
63         this.classpath = classpath;
64 
65         initValues(language);
66         initUI();
67     }
68 
reload(File basedir, Vector classpath, String language)69     public void reload(File basedir, Vector classpath, String language) {
70         this.basedir = basedir;
71         this.classpath = classpath;
72 
73         initValues(language);
74         list.setListData(values);
75     }
76 
getSelectedEntries()77     public ScriptEntry[] getSelectedEntries() {
78         Object[] selections = list.getSelectedValues();
79         ScriptEntry[] entries = new ScriptEntry[selections.length];
80 
81         for (int i = 0; i < selections.length; i++) {
82             entries[i] = (ScriptEntry)selections[i];
83         }
84 
85         return entries;
86     }
87 
initUI()88     private void initUI() {
89         JLabel label = new JLabel("Available Methods:");
90         // table = new JTable(model);
91         list = new JList(values);
92 
93         JScrollPane pane = new JScrollPane(list);
94         label.setLabelFor(pane);
95 
96         BorderLayout layout = new BorderLayout();
97         setLayout(layout);
98         layout.setVgap(5);
99 
100         add(label, BorderLayout.NORTH);
101         add(pane, BorderLayout.CENTER);
102     }
103 
initValues(String language)104     private void initValues(String language) {
105         MethodFinder finder;
106 
107         if (language == null)
108             finder = JavaFinder.getInstance(classpath);
109         else if (language.toLowerCase().equals("beanshell"))
110             finder = new ExtensionFinder(language, new String[] {".bsh"});
111         else
112             finder = JavaFinder.getInstance(classpath);
113 
114         values = finder.findMethods(basedir);
115     }
116 
117     /*
118     private class MethodTableModel extends AbstractTableModel {
119         final String[] columnNames = {"Method",
120                                       "Language"};
121 
122         private Vector methods;
123         private int nextRow;
124 
125         public MethodTableModel() {
126             methods = new Vector(11);
127         }
128 
129         public int getColumnCount() {
130             return columnNames.length;
131         }
132 
133         public int getRowCount() {
134             return methods.size();
135         }
136 
137         public String getColumnName(int col) {
138             return columnNames[col];
139         }
140 
141         public void add(ScriptEntry entry) {
142             methods.addElement(entry);
143             fireTableRowsInserted(nextRow, nextRow);
144             nextRow++;
145         }
146 
147         public void remove(int row) {
148             methods.removeElementAt(row);
149             fireTableRowsDeleted(row, row);
150             nextRow--;
151         }
152 
153         public void removeAll() {
154             methods.removeAllElements();
155             fireTableRowsDeleted(0, nextRow);
156             nextRow = 0;
157         }
158 
159         public Object getValueAt(int row) {
160             return methods.elementAt(row);
161         }
162 
163         public Object getValueAt(int row, int col) {
164             String result = "";
165             ScriptEntry entry;
166 
167             entry = (ScriptEntry)methods.elementAt(row);
168 
169             if (col == 0)
170                 result = entry.getLanguageName();
171             else if (col == 1)
172                 result = entry.getLanguage();
173 
174             return result;
175         }
176 
177         public boolean isCellEditable(int row, int col) {
178             return false;
179         }
180     }
181     */
182 }
183