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.io.IOException;
28 
29 import java.util.Vector;
30 import java.util.Enumeration;
31 
32 import javax.swing.JFrame;
33 import javax.swing.JPanel;
34 import javax.swing.JButton;
35 import javax.swing.AbstractButton;
36 import javax.swing.ImageIcon;
37 import javax.swing.border.LineBorder;
38 
39 import java.awt.BorderLayout;
40 import java.awt.GridBagLayout;
41 import java.awt.GridBagConstraints;
42 import java.awt.Insets;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.ActionListener;
45 
46 import org.w3c.dom.Document;
47 
48 import com.sun.star.script.framework.container.ScriptEntry;
49 import com.sun.star.script.framework.container.ParcelDescriptor;
50 
51 import org.openoffice.idesupport.zip.ParcelZipper;
52 
53 public class ConfigurePanel extends JPanel {
54 
55     private File basedir;
56     private Vector classpath;
57     private ParcelDescriptor descriptor;
58 
59     private MethodPanel methodPanel;
60     private ScriptPanel scriptPanel;
61 
62     public static final String DIALOG_TITLE =
63         "Choose What to Export as Scripts";
64 
ConfigurePanel(String basedir, Vector classpath, ParcelDescriptor descriptor)65     public ConfigurePanel(String basedir, Vector classpath,
66         ParcelDescriptor descriptor) {
67 
68         this.basedir = new File(basedir);
69         this.classpath = classpath;
70         this.descriptor = descriptor;
71         initUI();
72     }
73 
ConfigurePanel(String basedir, Vector classpath)74     public ConfigurePanel(String basedir, Vector classpath)
75         throws IOException {
76 
77         this.basedir = new File(basedir);
78         this.classpath = classpath;
79         this.descriptor = new ParcelDescriptor(new File(this.basedir,
80             ParcelZipper.PARCEL_DESCRIPTOR_XML));
81         initUI();
82     }
83 
reload(String basedir, Vector classpath, ParcelDescriptor descriptor)84     public void reload(String basedir, Vector classpath,
85         ParcelDescriptor descriptor) {
86 
87         if (basedir != null)
88             this.basedir = new File(basedir);
89 
90         if (classpath != null)
91             this.classpath = classpath;
92 
93         if (descriptor != null) {
94             descriptor = descriptor;
95         }
96 
97         methodPanel.reload(this.basedir, this.classpath,
98             descriptor.getLanguage());
99         scriptPanel.reload(descriptor.getScriptEntries());
100     }
101 
reload(String basedir, Vector classpath)102     public void reload(String basedir, Vector classpath)
103         throws IOException {
104 
105         if (basedir != null)
106             this.basedir = new File(basedir);
107 
108         if (classpath != null)
109             this.classpath = classpath;
110 
111         this.descriptor = new ParcelDescriptor(new File(this.basedir,
112             ParcelZipper.PARCEL_DESCRIPTOR_XML));
113 
114         methodPanel.reload(this.basedir, this.classpath,
115             descriptor.getLanguage());
116         scriptPanel.reload(descriptor.getScriptEntries());
117     }
118 
getConfiguration()119     public ParcelDescriptor getConfiguration() throws Exception {
120         Enumeration scripts = scriptPanel.getScriptEntries();
121         descriptor.setScriptEntries(scripts);
122         return descriptor;
123     }
124 
initUI()125     private void initUI() {
126 
127         JPanel leftPanel = new JPanel();
128         JPanel methodButtons = initMethodButtons();
129         methodPanel = new MethodPanel(basedir, classpath, descriptor.getLanguage());
130 
131         leftPanel.setLayout(new BorderLayout());
132         leftPanel.add(methodPanel, BorderLayout.CENTER);
133 
134         JPanel rightPanel = new JPanel();
135         JPanel scriptButtons = initScriptButtons();
136         scriptPanel = new ScriptPanel(descriptor.getScriptEntries());
137 
138         rightPanel.setLayout(new BorderLayout());
139         rightPanel.add(scriptPanel, BorderLayout.CENTER);
140         rightPanel.add(scriptButtons, BorderLayout.SOUTH);
141 
142         setLayout(new GridBagLayout());
143         setPreferredSize(new java.awt.Dimension(700, 300));
144         setBorder(LineBorder.createBlackLineBorder());
145 
146         GridBagConstraints gbc = new GridBagConstraints();
147         gbc.gridx = 0;
148         gbc.gridy = 0;
149         gbc.fill = java.awt.GridBagConstraints.BOTH;
150         gbc.ipadx = 40;
151         gbc.anchor = java.awt.GridBagConstraints.WEST;
152         gbc.insets = new Insets(10, 5, 5, 5);
153         gbc.weightx = 0.75;
154         add(leftPanel, gbc);
155 
156         gbc = new java.awt.GridBagConstraints();
157         gbc.gridx = 1;
158         gbc.gridy = 0;
159         add(methodButtons, gbc);
160 
161         gbc = new java.awt.GridBagConstraints();
162         gbc.gridx = 2;
163         gbc.gridy = 0;
164         gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
165         gbc.fill = java.awt.GridBagConstraints.BOTH;
166         gbc.anchor = java.awt.GridBagConstraints.EAST;
167         gbc.insets = new Insets(10, 5, 5, 5);
168         gbc.weightx = 1.0;
169         gbc.weighty = 1.0;
170         add(rightPanel, gbc);
171     }
172 
initMethodButtons()173     private JPanel initMethodButtons() {
174         JPanel panel = new JPanel();
175         panel.setLayout(new GridBagLayout());
176         ImageIcon icon = new ImageIcon(getClass().getResource("/org/openoffice/idesupport/ui/add.gif"));
177         JButton addButton = new JButton("Add", icon);
178         addButton.setHorizontalTextPosition(AbstractButton.LEFT);
179 
180         addButton.addActionListener(
181             new ActionListener() {
182                 public void actionPerformed(ActionEvent e) {
183                     scriptPanel.addScriptEntries(methodPanel.getSelectedEntries());
184                 }
185             }
186         );
187 
188         GridBagConstraints gbc = new java.awt.GridBagConstraints();
189         gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
190         gbc.fill = java.awt.GridBagConstraints.HORIZONTAL;
191         gbc.insets = new Insets(5, 5, 5, 5);
192         panel.add(addButton, gbc);
193 
194         JPanel dummyPanel = new JPanel();
195         gbc = new java.awt.GridBagConstraints();
196         gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
197         gbc.gridheight = java.awt.GridBagConstraints.REMAINDER;
198         gbc.fill = java.awt.GridBagConstraints.BOTH;
199         gbc.weightx = 1.0;
200         gbc.weighty = 1.0;
201         panel.add(dummyPanel, gbc);
202 
203         return panel;
204     }
205 
initScriptButtons()206     private JPanel initScriptButtons() {
207         JPanel panel = new JPanel();
208         JButton removeButton = new JButton("Remove");
209         JButton removeAllButton = new JButton("Remove All");
210 
211         removeButton.addActionListener(
212             new ActionListener() {
213                 public void actionPerformed(ActionEvent e) {
214                     scriptPanel.removeSelectedRows();
215                 }
216             }
217         );
218 
219         removeAllButton.addActionListener(
220             new ActionListener() {
221                 public void actionPerformed(ActionEvent e) {
222                     scriptPanel.removeAllRows();
223                 }
224             }
225         );
226 
227         panel.add(removeButton);
228         panel.add(removeAllButton);
229 
230         return panel;
231     }
232 }
233