1cd519653SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3cd519653SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4cd519653SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5cd519653SAndrew Rist  * distributed with this work for additional information
6cd519653SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7cd519653SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8cd519653SAndrew Rist  * "License"); you may not use this file except in compliance
9cd519653SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cd519653SAndrew Rist  *
11cd519653SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cd519653SAndrew Rist  *
13cd519653SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14cd519653SAndrew Rist  * software distributed under the License is distributed on an
15cd519653SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16cd519653SAndrew Rist  * KIND, either express or implied.  See the License for the
17cd519653SAndrew Rist  * specific language governing permissions and limitations
18cd519653SAndrew Rist  * under the License.
19cd519653SAndrew Rist  *
20cd519653SAndrew Rist  *************************************************************/
21cd519653SAndrew Rist 
22cd519653SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package org.openoffice.idesupport.ui;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.File;
27cdf0e10cSrcweir import java.io.IOException;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import java.util.Vector;
30cdf0e10cSrcweir import java.util.Enumeration;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import javax.swing.JFrame;
33cdf0e10cSrcweir import javax.swing.JPanel;
34cdf0e10cSrcweir import javax.swing.JButton;
35cdf0e10cSrcweir import javax.swing.AbstractButton;
36cdf0e10cSrcweir import javax.swing.ImageIcon;
37cdf0e10cSrcweir import javax.swing.border.LineBorder;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir import java.awt.BorderLayout;
40cdf0e10cSrcweir import java.awt.GridBagLayout;
41cdf0e10cSrcweir import java.awt.GridBagConstraints;
42cdf0e10cSrcweir import java.awt.Insets;
43cdf0e10cSrcweir import java.awt.event.ActionEvent;
44cdf0e10cSrcweir import java.awt.event.ActionListener;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir import org.w3c.dom.Document;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir import com.sun.star.script.framework.container.ScriptEntry;
49cdf0e10cSrcweir import com.sun.star.script.framework.container.ParcelDescriptor;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir import org.openoffice.idesupport.zip.ParcelZipper;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir public class ConfigurePanel extends JPanel {
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     private File basedir;
56cdf0e10cSrcweir     private Vector classpath;
57cdf0e10cSrcweir     private ParcelDescriptor descriptor;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     private MethodPanel methodPanel;
60cdf0e10cSrcweir     private ScriptPanel scriptPanel;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     public static final String DIALOG_TITLE =
63cdf0e10cSrcweir         "Choose What to Export as Scripts";
64cdf0e10cSrcweir 
ConfigurePanel(String basedir, Vector classpath, ParcelDescriptor descriptor)65cdf0e10cSrcweir     public ConfigurePanel(String basedir, Vector classpath,
66cdf0e10cSrcweir         ParcelDescriptor descriptor) {
67cdf0e10cSrcweir 
68cdf0e10cSrcweir         this.basedir = new File(basedir);
69cdf0e10cSrcweir         this.classpath = classpath;
70cdf0e10cSrcweir         this.descriptor = descriptor;
71cdf0e10cSrcweir         initUI();
72cdf0e10cSrcweir     }
73cdf0e10cSrcweir 
ConfigurePanel(String basedir, Vector classpath)74cdf0e10cSrcweir     public ConfigurePanel(String basedir, Vector classpath)
75cdf0e10cSrcweir         throws IOException {
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         this.basedir = new File(basedir);
78cdf0e10cSrcweir         this.classpath = classpath;
79cdf0e10cSrcweir         this.descriptor = new ParcelDescriptor(new File(this.basedir,
80cdf0e10cSrcweir             ParcelZipper.PARCEL_DESCRIPTOR_XML));
81cdf0e10cSrcweir         initUI();
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir 
reload(String basedir, Vector classpath, ParcelDescriptor descriptor)84cdf0e10cSrcweir     public void reload(String basedir, Vector classpath,
85cdf0e10cSrcweir         ParcelDescriptor descriptor) {
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         if (basedir != null)
88cdf0e10cSrcweir             this.basedir = new File(basedir);
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         if (classpath != null)
91cdf0e10cSrcweir             this.classpath = classpath;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         if (descriptor != null) {
94*f1d3244eSDamjan Jovanovic             this.descriptor = descriptor;
95cdf0e10cSrcweir         }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir         methodPanel.reload(this.basedir, this.classpath,
98cdf0e10cSrcweir             descriptor.getLanguage());
99cdf0e10cSrcweir         scriptPanel.reload(descriptor.getScriptEntries());
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir 
reload(String basedir, Vector classpath)102cdf0e10cSrcweir     public void reload(String basedir, Vector classpath)
103cdf0e10cSrcweir         throws IOException {
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         if (basedir != null)
106cdf0e10cSrcweir             this.basedir = new File(basedir);
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         if (classpath != null)
109cdf0e10cSrcweir             this.classpath = classpath;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir         this.descriptor = new ParcelDescriptor(new File(this.basedir,
112cdf0e10cSrcweir             ParcelZipper.PARCEL_DESCRIPTOR_XML));
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         methodPanel.reload(this.basedir, this.classpath,
115cdf0e10cSrcweir             descriptor.getLanguage());
116cdf0e10cSrcweir         scriptPanel.reload(descriptor.getScriptEntries());
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
getConfiguration()119cdf0e10cSrcweir     public ParcelDescriptor getConfiguration() throws Exception {
120cdf0e10cSrcweir         Enumeration scripts = scriptPanel.getScriptEntries();
121cdf0e10cSrcweir         descriptor.setScriptEntries(scripts);
122cdf0e10cSrcweir         return descriptor;
123cdf0e10cSrcweir     }
124cdf0e10cSrcweir 
initUI()125cdf0e10cSrcweir     private void initUI() {
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         JPanel leftPanel = new JPanel();
128cdf0e10cSrcweir         JPanel methodButtons = initMethodButtons();
129cdf0e10cSrcweir         methodPanel = new MethodPanel(basedir, classpath, descriptor.getLanguage());
130cdf0e10cSrcweir 
131cdf0e10cSrcweir         leftPanel.setLayout(new BorderLayout());
132cdf0e10cSrcweir         leftPanel.add(methodPanel, BorderLayout.CENTER);
133cdf0e10cSrcweir 
134cdf0e10cSrcweir         JPanel rightPanel = new JPanel();
135cdf0e10cSrcweir         JPanel scriptButtons = initScriptButtons();
136cdf0e10cSrcweir         scriptPanel = new ScriptPanel(descriptor.getScriptEntries());
137cdf0e10cSrcweir 
138cdf0e10cSrcweir         rightPanel.setLayout(new BorderLayout());
139cdf0e10cSrcweir         rightPanel.add(scriptPanel, BorderLayout.CENTER);
140cdf0e10cSrcweir         rightPanel.add(scriptButtons, BorderLayout.SOUTH);
141cdf0e10cSrcweir 
142cdf0e10cSrcweir         setLayout(new GridBagLayout());
143cdf0e10cSrcweir         setPreferredSize(new java.awt.Dimension(700, 300));
144cdf0e10cSrcweir         setBorder(LineBorder.createBlackLineBorder());
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         GridBagConstraints gbc = new GridBagConstraints();
147cdf0e10cSrcweir         gbc.gridx = 0;
148cdf0e10cSrcweir         gbc.gridy = 0;
149cdf0e10cSrcweir         gbc.fill = java.awt.GridBagConstraints.BOTH;
150cdf0e10cSrcweir         gbc.ipadx = 40;
151cdf0e10cSrcweir         gbc.anchor = java.awt.GridBagConstraints.WEST;
152cdf0e10cSrcweir         gbc.insets = new Insets(10, 5, 5, 5);
153cdf0e10cSrcweir         gbc.weightx = 0.75;
154cdf0e10cSrcweir         add(leftPanel, gbc);
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         gbc = new java.awt.GridBagConstraints();
157cdf0e10cSrcweir         gbc.gridx = 1;
158cdf0e10cSrcweir         gbc.gridy = 0;
159cdf0e10cSrcweir         add(methodButtons, gbc);
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         gbc = new java.awt.GridBagConstraints();
162cdf0e10cSrcweir         gbc.gridx = 2;
163cdf0e10cSrcweir         gbc.gridy = 0;
164cdf0e10cSrcweir         gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
165cdf0e10cSrcweir         gbc.fill = java.awt.GridBagConstraints.BOTH;
166cdf0e10cSrcweir         gbc.anchor = java.awt.GridBagConstraints.EAST;
167cdf0e10cSrcweir         gbc.insets = new Insets(10, 5, 5, 5);
168cdf0e10cSrcweir         gbc.weightx = 1.0;
169cdf0e10cSrcweir         gbc.weighty = 1.0;
170cdf0e10cSrcweir         add(rightPanel, gbc);
171cdf0e10cSrcweir     }
172cdf0e10cSrcweir 
initMethodButtons()173cdf0e10cSrcweir     private JPanel initMethodButtons() {
174cdf0e10cSrcweir         JPanel panel = new JPanel();
175cdf0e10cSrcweir         panel.setLayout(new GridBagLayout());
176cdf0e10cSrcweir         ImageIcon icon = new ImageIcon(getClass().getResource("/org/openoffice/idesupport/ui/add.gif"));
177cdf0e10cSrcweir         JButton addButton = new JButton("Add", icon);
178cdf0e10cSrcweir         addButton.setHorizontalTextPosition(AbstractButton.LEFT);
179cdf0e10cSrcweir 
180cdf0e10cSrcweir         addButton.addActionListener(
181cdf0e10cSrcweir             new ActionListener() {
182cdf0e10cSrcweir                 public void actionPerformed(ActionEvent e) {
183cdf0e10cSrcweir                     scriptPanel.addScriptEntries(methodPanel.getSelectedEntries());
184cdf0e10cSrcweir                 }
185cdf0e10cSrcweir             }
186cdf0e10cSrcweir         );
187cdf0e10cSrcweir 
188cdf0e10cSrcweir         GridBagConstraints gbc = new java.awt.GridBagConstraints();
189cdf0e10cSrcweir         gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
190cdf0e10cSrcweir         gbc.fill = java.awt.GridBagConstraints.HORIZONTAL;
191cdf0e10cSrcweir         gbc.insets = new Insets(5, 5, 5, 5);
192cdf0e10cSrcweir         panel.add(addButton, gbc);
193cdf0e10cSrcweir 
194cdf0e10cSrcweir         JPanel dummyPanel = new JPanel();
195cdf0e10cSrcweir         gbc = new java.awt.GridBagConstraints();
196cdf0e10cSrcweir         gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
197cdf0e10cSrcweir         gbc.gridheight = java.awt.GridBagConstraints.REMAINDER;
198cdf0e10cSrcweir         gbc.fill = java.awt.GridBagConstraints.BOTH;
199cdf0e10cSrcweir         gbc.weightx = 1.0;
200cdf0e10cSrcweir         gbc.weighty = 1.0;
201cdf0e10cSrcweir         panel.add(dummyPanel, gbc);
202cdf0e10cSrcweir 
203cdf0e10cSrcweir         return panel;
204cdf0e10cSrcweir     }
205cdf0e10cSrcweir 
initScriptButtons()206cdf0e10cSrcweir     private JPanel initScriptButtons() {
207cdf0e10cSrcweir         JPanel panel = new JPanel();
208cdf0e10cSrcweir         JButton removeButton = new JButton("Remove");
209cdf0e10cSrcweir         JButton removeAllButton = new JButton("Remove All");
210cdf0e10cSrcweir 
211cdf0e10cSrcweir         removeButton.addActionListener(
212cdf0e10cSrcweir             new ActionListener() {
213cdf0e10cSrcweir                 public void actionPerformed(ActionEvent e) {
214cdf0e10cSrcweir                     scriptPanel.removeSelectedRows();
215cdf0e10cSrcweir                 }
216cdf0e10cSrcweir             }
217cdf0e10cSrcweir         );
218cdf0e10cSrcweir 
219cdf0e10cSrcweir         removeAllButton.addActionListener(
220cdf0e10cSrcweir             new ActionListener() {
221cdf0e10cSrcweir                 public void actionPerformed(ActionEvent e) {
222cdf0e10cSrcweir                     scriptPanel.removeAllRows();
223cdf0e10cSrcweir                 }
224cdf0e10cSrcweir             }
225cdf0e10cSrcweir         );
226cdf0e10cSrcweir 
227cdf0e10cSrcweir         panel.add(removeButton);
228cdf0e10cSrcweir         panel.add(removeAllButton);
229cdf0e10cSrcweir 
230cdf0e10cSrcweir         return panel;
231cdf0e10cSrcweir     }
232cdf0e10cSrcweir }
233