KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > connection > wizard > ClassPathListVisualPanel


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.mc4j.console.connection.wizard;
18
19 import org.openide.util.NbBundle;
20
21 import javax.swing.*;
22 import javax.swing.filechooser.FileFilter JavaDoc;
23 import java.awt.*;
24 import java.io.File JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * This visual panel lets you manage a list of class path entries
30  *
31  * @author Greg Hinkle (ghinkle@users.sourceforge.net), January 2002
32  * @version $Revision: 570 $($Author: ghinkl $ / $Date: 2006-04-12 15:14:16 -0400 (Wed, 12 Apr 2006) $)
33  */

34 public class ClassPathListVisualPanel extends JPanel {
35
36     /** The wizard panel descriptor associated with this GUI panel.
37      * If you need to fire state changes or something similar, you can
38      * use this handle to do so.
39      */

40     private final ClassPathListPanel panel;
41
42     private static File JavaDoc startingDirectory;
43
44     /** Create the wizard panel and set up some basic properties. */
45     public ClassPathListVisualPanel(ClassPathListPanel panel) {
46         this.panel = panel;
47         initComponents();
48
49         this.jListEntries.setModel(new DefaultListModel());
50         // Provide a name in the title bar.
51
setName(NbBundle.getMessage(ClassPathListVisualPanel.class, "TITLE_WizardPanel"));
52         /*
53         // Optional: provide a special description for this pane.
54         // You must have turned on WizardDescriptor.WizardPanel_helpDisplayed
55         // (see descriptor in standard iterator template for an example of this).
56         try {
57             putClientProperty("WizardPanel_helpURL", // NOI18N
58                 new URL("nbresloc:/org/mc4j/console/classpath/ClassPathListPanelVisualHelp.html")); // NOI18N
59         } catch (MalformedURLException mfue) {
60             throw new IllegalStateException(mfue.toString());
61         }
62          */

63
64         DefaultListModel model = new DefaultListModel();
65         File JavaDoc[] installedLibraries = panel.getInstalledLibraries();
66         if (installedLibraries != null) {
67             for (int i = 0; i < installedLibraries.length; i++) {
68                 File JavaDoc installedLibrary = installedLibraries[i];
69                 model.addElement(installedLibrary.getAbsolutePath());
70             }
71         }
72         this.jListExtraLibraries.setModel(model);
73     }
74
75     public void setFileList(List JavaDoc<File JavaDoc> fileList) {
76         ((DefaultListModel)this.jListEntries.getModel()).removeAllElements();
77         if (fileList != null) {
78             for (File JavaDoc file : fileList) {
79                 ((DefaultListModel) this.jListEntries.getModel()).addElement(file);
80             }
81         }
82     }
83
84     public List JavaDoc<File JavaDoc> getFileList() {
85         List JavaDoc<File JavaDoc> results = new ArrayList JavaDoc<File JavaDoc>();
86         int count = this.jListEntries.getModel().getSize();
87         for (int i = 0; i < count; i++) {
88             results.add((File JavaDoc) this.jListEntries.getModel().getElementAt(i));
89         }
90         return results;
91     }
92
93     public Dimension getPreferredSize() {
94         return new Dimension(600, 400);
95     }
96
97     /** This method is called from within the constructor to
98      * initialize the form.
99      * WARNING: Do NOT modify this code. The content of this method is
100      * always regenerated by the Form Editor.
101      */

102     private void initComponents() {//GEN-BEGIN:initComponents
103
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
104
105         jPanelTop = new javax.swing.JPanel JavaDoc();
106         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
107         jListEntries = new javax.swing.JList JavaDoc();
108         jButtonAdd = new javax.swing.JButton JavaDoc();
109         jButtonRemove = new javax.swing.JButton JavaDoc();
110         jPanelBottom = new javax.swing.JPanel JavaDoc();
111         jScrollPane2 = new javax.swing.JScrollPane JavaDoc();
112         jListExtraLibraries = new javax.swing.JList JavaDoc();
113
114         setLayout(new java.awt.GridLayout JavaDoc(2, 1, 0, 5));
115
116         jPanelTop.setLayout(new java.awt.GridBagLayout JavaDoc());
117
118         jPanelTop.setBorder(new javax.swing.border.TitledBorder JavaDoc("Custom classpath and server libraries"));
119         jScrollPane1.setPreferredSize(new java.awt.Dimension JavaDoc(259, 105));
120         jListEntries.setVisibleRowCount(6);
121         jScrollPane1.setViewportView(jListEntries);
122
123         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
124         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
125         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
126         jPanelTop.add(jScrollPane1, gridBagConstraints);
127
128         jButtonAdd.setText("Add...");
129         jButtonAdd.addActionListener(new java.awt.event.ActionListener JavaDoc() {
130             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
131                 jButtonAddActionPerformed(evt);
132             }
133         });
134
135         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
136         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
137         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
138         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 4, 4, 4);
139         jPanelTop.add(jButtonAdd, gridBagConstraints);
140
141         jButtonRemove.setText("Remove");
142         jButtonRemove.addActionListener(new java.awt.event.ActionListener JavaDoc() {
143             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
144                 jButtonRemoveActionPerformed(evt);
145             }
146         });
147
148         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
149         gridBagConstraints.gridx = 1;
150         gridBagConstraints.gridy = 1;
151         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
152         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
153         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 4, 4, 4);
154         jPanelTop.add(jButtonRemove, gridBagConstraints);
155
156         add(jPanelTop);
157
158         jPanelBottom.setLayout(new java.awt.GridBagLayout JavaDoc());
159
160         jPanelBottom.setBorder(new javax.swing.border.TitledBorder JavaDoc("Additional mc4jlib directory libraries"));
161         jScrollPane2.setPreferredSize(new java.awt.Dimension JavaDoc(350, 95));
162         jListExtraLibraries.setVisibleRowCount(5);
163         jListExtraLibraries.setEnabled(false);
164         jScrollPane2.setViewportView(jListExtraLibraries);
165
166         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
167         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
168         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
169         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
170         gridBagConstraints.insets = new java.awt.Insets JavaDoc(8, 8, 8, 8);
171         jPanelBottom.add(jScrollPane2, gridBagConstraints);
172
173         add(jPanelBottom);
174
175     }//GEN-END:initComponents
176

177     private void jButtonRemoveActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonRemoveActionPerformed
178
// Add your handling code here:
179
int index = this.jListEntries.getSelectedIndex();
180         ((DefaultListModel) this.jListEntries.getModel()).remove(index);
181     }//GEN-LAST:event_jButtonRemoveActionPerformed
182

183     private void jButtonAddActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonAddActionPerformed
184
// Add your handling code here:
185
JFileChooser chooser = new JFileChooser();
186         if (ClassPathListVisualPanel.startingDirectory != null)
187             chooser.setCurrentDirectory(ClassPathListVisualPanel.startingDirectory);
188         
189         chooser.setFileFilter(new FileFilter JavaDoc() {
190             public boolean accept(File JavaDoc path) {
191                 return (path.isDirectory() ||
192                     (path.getName().toLowerCase().endsWith(".jar")) ||
193                     (path.getName().toLowerCase().endsWith(".zip")));
194             }
195             public String JavaDoc getDescription() {
196                 return "Library files or class directories";
197             }
198         });
199         chooser.setMultiSelectionEnabled(true);
200         chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
201         int returnVal;
202         if ("Mac OS X".equals(System.getProperty ("os.name"))) {
203             chooser.setDialogTitle("Select additional libraries or directories");
204             returnVal = chooser.showOpenDialog(null);
205         } else {
206             returnVal = chooser.showDialog(null,"Select");
207         }
208         if (returnVal == JFileChooser.APPROVE_OPTION) {
209             File JavaDoc[] selectedFiles = chooser.getSelectedFiles();
210             for (int i = 0; i < selectedFiles.length; i++) {
211                 File JavaDoc selectedFile = selectedFiles[i];
212                 ((DefaultListModel)this.jListEntries.getModel()).addElement(selectedFile);
213             }
214
215             ClassPathListVisualPanel.startingDirectory = chooser.getCurrentDirectory();
216
217         }
218     }//GEN-LAST:event_jButtonAddActionPerformed
219

220     // Variables declaration - do not modify//GEN-BEGIN:variables
221
private javax.swing.JButton JavaDoc jButtonAdd;
222     private javax.swing.JButton JavaDoc jButtonRemove;
223     private javax.swing.JList JavaDoc jListEntries;
224     private javax.swing.JList JavaDoc jListExtraLibraries;
225     private javax.swing.JPanel JavaDoc jPanelBottom;
226     private javax.swing.JPanel JavaDoc jPanelTop;
227     private javax.swing.JScrollPane JavaDoc jScrollPane1;
228     private javax.swing.JScrollPane JavaDoc jScrollPane2;
229     // End of variables declaration//GEN-END:variables
230

231 }
232
Popular Tags