xref: /AOO41X/main/scripting/java/org/openoffice/idesupport/ui/ConfigurePanel.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package org.openoffice.idesupport.ui;
29 
30 import java.io.File;
31 import java.io.IOException;
32 
33 import java.util.Vector;
34 import java.util.Enumeration;
35 
36 import javax.swing.JFrame;
37 import javax.swing.JPanel;
38 import javax.swing.JButton;
39 import javax.swing.AbstractButton;
40 import javax.swing.ImageIcon;
41 import javax.swing.border.LineBorder;
42 
43 import java.awt.BorderLayout;
44 import java.awt.GridBagLayout;
45 import java.awt.GridBagConstraints;
46 import java.awt.Insets;
47 import java.awt.event.ActionEvent;
48 import java.awt.event.ActionListener;
49 
50 import org.w3c.dom.Document;
51 
52 import com.sun.star.script.framework.container.ScriptEntry;
53 import com.sun.star.script.framework.container.ParcelDescriptor;
54 
55 import org.openoffice.idesupport.zip.ParcelZipper;
56 
57 public class ConfigurePanel extends JPanel {
58 
59     private File basedir;
60     private Vector classpath;
61     private ParcelDescriptor descriptor;
62 
63     private MethodPanel methodPanel;
64     private ScriptPanel scriptPanel;
65 
66     public static final String DIALOG_TITLE =
67         "Choose What to Export as Scripts";
68 
69     public ConfigurePanel(String basedir, Vector classpath,
70         ParcelDescriptor descriptor) {
71 
72         this.basedir = new File(basedir);
73         this.classpath = classpath;
74         this.descriptor = descriptor;
75         initUI();
76     }
77 
78     public ConfigurePanel(String basedir, Vector classpath)
79         throws IOException {
80 
81         this.basedir = new File(basedir);
82         this.classpath = classpath;
83         this.descriptor = new ParcelDescriptor(new File(this.basedir,
84             ParcelZipper.PARCEL_DESCRIPTOR_XML));
85         initUI();
86     }
87 
88     public void reload(String basedir, Vector classpath,
89         ParcelDescriptor descriptor) {
90 
91         if (basedir != null)
92             this.basedir = new File(basedir);
93 
94         if (classpath != null)
95             this.classpath = classpath;
96 
97         if (descriptor != null) {
98             descriptor = descriptor;
99         }
100 
101         methodPanel.reload(this.basedir, this.classpath,
102             descriptor.getLanguage());
103         scriptPanel.reload(descriptor.getScriptEntries());
104     }
105 
106     public void reload(String basedir, Vector classpath)
107         throws IOException {
108 
109         if (basedir != null)
110             this.basedir = new File(basedir);
111 
112         if (classpath != null)
113             this.classpath = classpath;
114 
115         this.descriptor = new ParcelDescriptor(new File(this.basedir,
116             ParcelZipper.PARCEL_DESCRIPTOR_XML));
117 
118         methodPanel.reload(this.basedir, this.classpath,
119             descriptor.getLanguage());
120         scriptPanel.reload(descriptor.getScriptEntries());
121     }
122 
123     public ParcelDescriptor getConfiguration() throws Exception {
124         Enumeration scripts = scriptPanel.getScriptEntries();
125         descriptor.setScriptEntries(scripts);
126         return descriptor;
127     }
128 
129     private void initUI() {
130 
131         JPanel leftPanel = new JPanel();
132         JPanel methodButtons = initMethodButtons();
133         methodPanel = new MethodPanel(basedir, classpath, descriptor.getLanguage());
134 
135         leftPanel.setLayout(new BorderLayout());
136         leftPanel.add(methodPanel, BorderLayout.CENTER);
137 
138         JPanel rightPanel = new JPanel();
139         JPanel scriptButtons = initScriptButtons();
140         scriptPanel = new ScriptPanel(descriptor.getScriptEntries());
141 
142         rightPanel.setLayout(new BorderLayout());
143         rightPanel.add(scriptPanel, BorderLayout.CENTER);
144         rightPanel.add(scriptButtons, BorderLayout.SOUTH);
145 
146         setLayout(new GridBagLayout());
147         setPreferredSize(new java.awt.Dimension(700, 300));
148         setBorder(LineBorder.createBlackLineBorder());
149 
150         GridBagConstraints gbc = new GridBagConstraints();
151         gbc.gridx = 0;
152         gbc.gridy = 0;
153         gbc.fill = java.awt.GridBagConstraints.BOTH;
154         gbc.ipadx = 40;
155         gbc.anchor = java.awt.GridBagConstraints.WEST;
156         gbc.insets = new Insets(10, 5, 5, 5);
157         gbc.weightx = 0.75;
158         add(leftPanel, gbc);
159 
160         gbc = new java.awt.GridBagConstraints();
161         gbc.gridx = 1;
162         gbc.gridy = 0;
163         add(methodButtons, gbc);
164 
165         gbc = new java.awt.GridBagConstraints();
166         gbc.gridx = 2;
167         gbc.gridy = 0;
168         gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
169         gbc.fill = java.awt.GridBagConstraints.BOTH;
170         gbc.anchor = java.awt.GridBagConstraints.EAST;
171         gbc.insets = new Insets(10, 5, 5, 5);
172         gbc.weightx = 1.0;
173         gbc.weighty = 1.0;
174         add(rightPanel, gbc);
175     }
176 
177     private JPanel initMethodButtons() {
178         JPanel panel = new JPanel();
179         panel.setLayout(new GridBagLayout());
180         ImageIcon icon = new ImageIcon(getClass().getResource("/org/openoffice/idesupport/ui/add.gif"));
181         JButton addButton = new JButton("Add", icon);
182         addButton.setHorizontalTextPosition(AbstractButton.LEFT);
183 
184         addButton.addActionListener(
185             new ActionListener() {
186                 public void actionPerformed(ActionEvent e) {
187                     scriptPanel.addScriptEntries(methodPanel.getSelectedEntries());
188                 }
189             }
190         );
191 
192         GridBagConstraints gbc = new java.awt.GridBagConstraints();
193         gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
194         gbc.fill = java.awt.GridBagConstraints.HORIZONTAL;
195         gbc.insets = new Insets(5, 5, 5, 5);
196         panel.add(addButton, gbc);
197 
198         JPanel dummyPanel = new JPanel();
199         gbc = new java.awt.GridBagConstraints();
200         gbc.gridwidth = java.awt.GridBagConstraints.REMAINDER;
201         gbc.gridheight = java.awt.GridBagConstraints.REMAINDER;
202         gbc.fill = java.awt.GridBagConstraints.BOTH;
203         gbc.weightx = 1.0;
204         gbc.weighty = 1.0;
205         panel.add(dummyPanel, gbc);
206 
207         return panel;
208     }
209 
210     private JPanel initScriptButtons() {
211         JPanel panel = new JPanel();
212         JButton removeButton = new JButton("Remove");
213         JButton removeAllButton = new JButton("Remove All");
214 
215         removeButton.addActionListener(
216             new ActionListener() {
217                 public void actionPerformed(ActionEvent e) {
218                     scriptPanel.removeSelectedRows();
219                 }
220             }
221         );
222 
223         removeAllButton.addActionListener(
224             new ActionListener() {
225                 public void actionPerformed(ActionEvent e) {
226                     scriptPanel.removeAllRows();
227                 }
228             }
229         );
230 
231         panel.add(removeButton);
232         panel.add(removeAllButton);
233 
234         return panel;
235     }
236 }
237