KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > autoupdate > SelectModulesPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.autoupdate;
21
22 import java.awt.Font JavaDoc;
23 import java.awt.KeyboardFocusManager JavaDoc;
24 import java.awt.Window JavaDoc;
25 import java.io.File JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import javax.swing.JFileChooser JavaDoc;
32 import javax.swing.DefaultListModel JavaDoc;
33 import javax.swing.DefaultListSelectionModel JavaDoc;
34
35 import org.openide.DialogDisplayer;
36 import org.openide.NotifyDescriptor;
37 import org.openide.util.NbBundle;
38 import org.openide.util.Utilities;
39 import org.openide.windows.WindowManager;
40
41 class SelectModulesPanel extends javax.swing.JPanel JavaDoc {
42
43     private static final javax.swing.filechooser.FileFilter JavaDoc NBM_FILE_FILTER = new NbmFileFilter();
44
45     /** The list model */
46     private DefaultListModel JavaDoc modulesModel;
47
48     /** Stores the last selected directory */
49     static File JavaDoc defaultDir = null;
50
51     private Wizard.Validator validator;
52
53     static final long serialVersionUID = 8279789280479374665L;
54     
55     /** Creates new form SelectModulesPanel */
56     public SelectModulesPanel(Wizard.Validator validator) {
57         putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(1)); // NOI18N
58
setName(getBundle("LAB_Select"));
59         initComponents();
60         
61         this.validator = validator;
62
63         getAccessibleContext().setAccessibleName(getBundle("LAB_Select"));
64         getAccessibleContext().setAccessibleDescription(getBundle("LAB_21") + getBundle("LAB_22"));
65         jTextArea.getAccessibleContext().setAccessibleDescription(getBundle("ACS_21"));
66         addButton.getAccessibleContext().setAccessibleDescription(getBundle("ACS_Add"));
67         removeButton.getAccessibleContext().setAccessibleDescription(getBundle("ACS_Remove"));
68         jList.getAccessibleContext().setAccessibleDescription(getBundle("ACS_InstallList"));
69         
70         reset();
71     }
72
73     /** Overload getPreffered size to get a bit bigger dialog */
74     public java.awt.Dimension JavaDoc getPreferredSize() {
75         return new java.awt.Dimension JavaDoc( 350, super.getPreferredSize().height );
76     }
77     
78     /** This method is called from within the constructor to
79      * initialize the form.
80      * WARNING: Do NOT modify this code. The content of this method is
81      * always regenerated by the Form Editor.
82      */

83     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
84
private void initComponents() {
85         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
86
87         jTextArea = new javax.swing.JTextArea JavaDoc();
88         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
89         jList = new javax.swing.JList JavaDoc();
90         addButton = new javax.swing.JButton JavaDoc();
91         removeButton = new javax.swing.JButton JavaDoc();
92         nextLabel = new javax.swing.JLabel JavaDoc();
93         listLabel = new javax.swing.JLabel JavaDoc();
94
95         setLayout(new java.awt.GridBagLayout JavaDoc());
96
97         setNextFocusableComponent(addButton);
98         jTextArea.setBackground(nextLabel.getBackground());
99         jTextArea.setFont(new Font JavaDoc("Dialog", Font.PLAIN, jTextArea.getFont().getSize()));
100         jTextArea.setLineWrap(true);
101         jTextArea.setText(getBundle("LAB_21"));
102         jTextArea.setWrapStyleWord(true);
103         jTextArea.setDisabledTextColor(java.awt.Color.black);
104         jTextArea.setEnabled(false);
105         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
106         gridBagConstraints.gridx = 0;
107         gridBagConstraints.gridy = 0;
108         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
109         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
110         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
111         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
112         add(jTextArea, gridBagConstraints);
113
114         jList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
115             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
116                 jListValueChanged(evt);
117             }
118         });
119
120         jScrollPane1.setViewportView(jList);
121
122         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
123         gridBagConstraints.gridx = 0;
124         gridBagConstraints.gridy = 2;
125         gridBagConstraints.gridheight = 2;
126         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
127         gridBagConstraints.weightx = 1.0;
128         gridBagConstraints.weighty = 1.0;
129         add(jScrollPane1, gridBagConstraints);
130
131         org.openide.awt.Mnemonics.setLocalizedText(addButton, getBundle("BTN_Add"));
132         addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
133             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
134                 addButtonActionPerformed(evt);
135             }
136         });
137
138         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
139         gridBagConstraints.gridx = 1;
140         gridBagConstraints.gridy = 2;
141         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
142         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
143         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 0);
144         add(addButton, gridBagConstraints);
145
146         org.openide.awt.Mnemonics.setLocalizedText(removeButton, getBundle("BTN_Remove"));
147         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
148             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
149                 removeButtonActionPerformed(evt);
150             }
151         });
152
153         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
154         gridBagConstraints.gridx = 1;
155         gridBagConstraints.gridy = 3;
156         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
157         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
158         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
159         add(removeButton, gridBagConstraints);
160
161         nextLabel.setLabelFor(this);
162         org.openide.awt.Mnemonics.setLocalizedText(nextLabel, getBundle("LAB_22"));
163         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
164         gridBagConstraints.gridx = 0;
165         gridBagConstraints.gridy = 4;
166         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
167         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
168         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
169         add(nextLabel, gridBagConstraints);
170
171         listLabel.setLabelFor(jList);
172         org.openide.awt.Mnemonics.setLocalizedText(listLabel, getBundle("LAB_ModulesList"));
173         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
174         gridBagConstraints.gridx = 0;
175         gridBagConstraints.gridy = 1;
176         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
177         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 2, 0);
178         add(listLabel, gridBagConstraints);
179
180     }
181     // </editor-fold>//GEN-END:initComponents
182

183     private void jListValueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_jListValueChanged
184
buttonsCheck();
185     }//GEN-LAST:event_jListValueChanged
186

187     private void removeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
188
Object JavaDoc[] sel = jList.getSelectedValues();
189         for (int i=0;i<sel.length;i++)
190             modulesModel.removeElement(sel[i]);
191         // to remove selection BUG #14750
192
// workaround, bug reported to BugParade
193
jList.setSelectionModel(new DefaultListSelectionModel JavaDoc());
194         
195         if ( modulesModel.getSize() == 0 )
196             validator.setValid( false );
197         buttonsCheck();
198     }//GEN-LAST:event_removeButtonActionPerformed
199

200     private void addButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
201
List JavaDoc files = selectNbmFiles();
202         
203         if ( files != null ) {
204             Iterator JavaDoc it = files.iterator();
205             for ( int i = 0; it.hasNext(); i++ ) {
206                 if ( i == 0 )
207                     validator.setValid( true );
208
209                 File JavaDoc fn = (File JavaDoc)it.next();
210                 if ( !selectedFile( fn ) ) {
211                     modulesModel.addElement( fn );
212                 }
213             }
214         }
215
216         buttonsCheck();
217     }//GEN-LAST:event_addButtonActionPerformed
218

219     // Variables declaration - do not modify//GEN-BEGIN:variables
220
private javax.swing.JButton JavaDoc addButton;
221     private javax.swing.JList JavaDoc jList;
222     private javax.swing.JScrollPane JavaDoc jScrollPane1;
223     private javax.swing.JTextArea JavaDoc jTextArea;
224     private javax.swing.JLabel JavaDoc listLabel;
225     private javax.swing.JLabel JavaDoc nextLabel;
226     private javax.swing.JButton JavaDoc removeButton;
227     // End of variables declaration//GEN-END:variables
228

229     private boolean selectedFile( File JavaDoc file ) {
230         for ( int i = 0; i < modulesModel.size(); i++ ) {
231             if ( ((File JavaDoc)modulesModel.elementAt(i)).compareTo( file ) == 0 )
232                 return true;
233         }
234
235         return false;
236     }
237     
238     File JavaDoc[] getFiles() {
239         File JavaDoc[] files = new File JavaDoc[ modulesModel.size() ];
240
241         for ( int i = 0; i < modulesModel.size(); i ++ ) {
242             files[i] = (File JavaDoc)modulesModel.elementAt( i );
243         }
244
245         return files;
246     }
247
248     /** Clears all modules in listbox, ceates new model,
249      * if the model does not exist yet
250      */

251     void reset() {
252         if ( modulesModel == null ) {
253             modulesModel = new DefaultListModel JavaDoc();
254             jList.setModel( modulesModel );
255         }
256         else {
257             modulesModel.removeAllElements();
258         }
259         buttonsCheck();
260     }
261     
262     private void buttonsCheck() {
263         if ( modulesModel.getSize() > 0 && jList.getSelectedIndex() != -1 )
264             removeButton.setEnabled(true);
265         else
266             removeButton.setEnabled(false);
267     }
268
269     private static class NbmFileFilter extends javax.swing.filechooser.FileFilter JavaDoc {
270         public boolean accept( File JavaDoc f ) {
271             return f.isDirectory() || f.getName().toLowerCase().endsWith( ".nbm" ); // NOI18N
272
}
273
274         public String JavaDoc getDescription() {
275             return getBundle( "CTL_FileFilterDescription" );
276         }
277     }
278     
279     static File JavaDoc getDefaultDir() {
280         File JavaDoc defDir = new File JavaDoc( System.getProperty("user.home") ); // NOI18N
281
try {
282             if ( Utilities.isUnix() )
283                 return defDir;
284             else if ( Utilities.isWindows() ) {
285                 do {
286                     defDir = defDir.getParentFile();
287                 } while ( defDir != null && defDir.getParentFile() != null );
288             }
289         }
290         catch (Exception JavaDoc ex) {
291             defDir = null;
292         }
293         return defDir;
294     }
295
296     static List JavaDoc selectNbmFiles() {
297         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
298         chooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
299         chooser.addChoosableFileFilter( NBM_FILE_FILTER );
300         chooser.setFileFilter(NBM_FILE_FILTER);
301         chooser.setMultiSelectionEnabled( true );
302         chooser.setFileHidingEnabled( false );
303         chooser.setDialogTitle(getBundle("CTL_FileChooser_Title"));
304         
305         if (defaultDir == null)
306             defaultDir = getDefaultDir();
307
308         if ( defaultDir != null) {
309             chooser.setCurrentDirectory( defaultDir );
310         }
311
312         List JavaDoc files = null;
313         
314         Window JavaDoc focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager ().getActiveWindow ();
315
316         if (chooser.showDialog (KeyboardFocusManager.getCurrentKeyboardFocusManager ().getActiveWindow (),
317                                getBundle("CTL_FileChooser_Approve_Button"))
318                 != JFileChooser.APPROVE_OPTION)
319             return null;
320
321         defaultDir = chooser.getCurrentDirectory();
322         
323         // #46353, recover focus after close the JFileChooser
324
if (focusOwner != null) {
325             focusOwner.toFront ();
326         }
327
328         return Arrays.asList(chooser.getSelectedFiles());
329     }
330     
331     private static String JavaDoc getBundle( String JavaDoc key ) {
332         return NbBundle.getMessage( SelectModulesPanel.class, key );
333     }
334 }
335
Popular Tags