KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > platform > NbPlatformCustomizerSources


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.apisupport.project.ui.platform;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Locale JavaDoc;
26 import javax.swing.JFileChooser JavaDoc;
27 import javax.swing.JPanel JavaDoc;
28 import javax.swing.event.ListDataEvent JavaDoc;
29 import javax.swing.event.ListDataListener JavaDoc;
30 import javax.swing.event.ListSelectionEvent JavaDoc;
31 import javax.swing.event.ListSelectionListener JavaDoc;
32 import javax.swing.filechooser.FileFilter JavaDoc;
33 import org.netbeans.modules.apisupport.project.Util;
34 import org.netbeans.modules.apisupport.project.queries.GlobalSourceForBinaryImpl;
35 import org.netbeans.modules.apisupport.project.ui.ModuleUISettings;
36 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
37 import org.openide.DialogDisplayer;
38 import org.openide.NotifyDescriptor;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.util.NbBundle;
41
42 /**
43  * Represents <em>Sources</em> tab in the NetBeans platforms customizer.
44  *
45  * @author Martin Krauskopf
46  */

47 final class NbPlatformCustomizerSources extends JPanel JavaDoc {
48     
49     private NbPlatform plaf;
50     private PlatformComponentFactory.NbPlatformSourceRootsModel model;
51     private final ListListener listListener;
52     
53     /** Creates new form NbPlatformCustomizerModules */
54     NbPlatformCustomizerSources() {
55         initComponents();
56         initAccessibility();
57         this.listListener = new ListListener() {
58             void listChanged() {
59                 updateEnabled();
60             }
61         };
62         updateEnabled();
63     }
64     
65     public void addNotify() {
66         super.addNotify();
67         sourceList.addListSelectionListener(listListener);
68         sourceList.getModel().addListDataListener(listListener);
69     }
70     
71     public void removeNotify() {
72         sourceList.removeListSelectionListener(listListener);
73         sourceList.getModel().removeListDataListener(listListener);
74         super.removeNotify();
75     }
76     
77     void setPlatform(NbPlatform plaf) {
78         this.plaf = plaf;
79         this.model = new PlatformComponentFactory.NbPlatformSourceRootsModel(plaf);
80         sourceList.setModel(model);
81     }
82     
83     private void updateEnabled() {
84         // update buttons enability appropriately
85
removeButton.setEnabled(sourceList.getModel().getSize() > 0 && sourceList.getSelectedIndex() != -1);
86         moveUpButton.setEnabled(sourceList.getSelectionModel().getMinSelectionIndex() > 0);
87         moveDownButton.setEnabled(plaf != null &&
88                 sourceList.getSelectionModel().getMaxSelectionIndex() < plaf.getSourceRoots().length - 1);
89     }
90     
91     /** This method is called from within the constructor to
92      * initialize the form.
93      * WARNING: Do NOT modify this code. The content of this method is
94      * always regenerated by the Form Editor.
95      */

96     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
97
private void initComponents() {
98         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
99
100         sourceLabel = new javax.swing.JLabel JavaDoc();
101         sourceSP = new javax.swing.JScrollPane JavaDoc();
102         sourceList = new javax.swing.JList JavaDoc();
103         buttonPanel = new javax.swing.JPanel JavaDoc();
104         addFolderButton = new javax.swing.JButton JavaDoc();
105         removeButton = new javax.swing.JButton JavaDoc();
106         moveUpButton = new javax.swing.JButton JavaDoc();
107         moveDownButton = new javax.swing.JButton JavaDoc();
108
109         setLayout(new java.awt.GridBagLayout JavaDoc());
110
111         setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 12, 12));
112         sourceLabel.setLabelFor(sourceList);
113         org.openide.awt.Mnemonics.setLocalizedText(sourceLabel, org.openide.util.NbBundle.getMessage(NbPlatformCustomizerSources.class, "LBL_PlatformSources"));
114         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
115         gridBagConstraints.gridx = 0;
116         gridBagConstraints.gridy = 0;
117         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
118         add(sourceLabel, gridBagConstraints);
119
120         sourceList.setCellRenderer(PlatformComponentFactory.getURLListRenderer());
121         sourceSP.setViewportView(sourceList);
122
123         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
124         gridBagConstraints.gridx = 0;
125         gridBagConstraints.gridy = 1;
126         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
127         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
128         gridBagConstraints.weightx = 1.0;
129         gridBagConstraints.weighty = 1.0;
130         add(sourceSP, gridBagConstraints);
131
132         buttonPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
133
134         buttonPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 12, 0, 0));
135         org.openide.awt.Mnemonics.setLocalizedText(addFolderButton, org.openide.util.NbBundle.getMessage(NbPlatformCustomizerSources.class, "CTL_AddZipOrFolder"));
136         addFolderButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
137             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
138                 addZipOrFolder(evt);
139             }
140         });
141
142         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
143         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
144         buttonPanel.add(addFolderButton, gridBagConstraints);
145
146         org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getMessage(NbPlatformCustomizerSources.class, "CTL_Remove"));
147         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
148             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
149                 removeFolder(evt);
150             }
151         });
152
153         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
154         gridBagConstraints.gridx = 0;
155         gridBagConstraints.gridy = 1;
156         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
157         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 12, 0);
158         buttonPanel.add(removeButton, gridBagConstraints);
159
160         org.openide.awt.Mnemonics.setLocalizedText(moveUpButton, org.openide.util.NbBundle.getMessage(NbPlatformCustomizerSources.class, "CTL_MoveUp"));
161         moveUpButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
162             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
163                 moveUp(evt);
164             }
165         });
166
167         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
168         gridBagConstraints.gridx = 0;
169         gridBagConstraints.gridy = 2;
170         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
171         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 2, 0);
172         buttonPanel.add(moveUpButton, gridBagConstraints);
173
174         org.openide.awt.Mnemonics.setLocalizedText(moveDownButton, org.openide.util.NbBundle.getMessage(NbPlatformCustomizerSources.class, "CTL_MoveDown"));
175         moveDownButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
176             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
177                 moveDown(evt);
178             }
179         });
180
181         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
182         gridBagConstraints.gridx = 0;
183         gridBagConstraints.gridy = 3;
184         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
185         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
186         gridBagConstraints.weighty = 1.0;
187         buttonPanel.add(moveDownButton, gridBagConstraints);
188
189         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
190         gridBagConstraints.gridx = 1;
191         gridBagConstraints.gridy = 1;
192         gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
193         add(buttonPanel, gridBagConstraints);
194
195     }// </editor-fold>//GEN-END:initComponents
196

197     private void moveDown(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_moveDown
198
int[] selIndices = sourceList.getSelectedIndices();
199         model.moveSourceRootsDown(selIndices);
200         for (int i = 0; i < selIndices.length; i++) {
201             selIndices[i] = ++selIndices[i];
202         }
203         sourceList.setSelectedIndices(selIndices);
204     }//GEN-LAST:event_moveDown
205

206     private void moveUp(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_moveUp
207
int[] selIndices = sourceList.getSelectedIndices();
208         model.moveSourceRootsUp(selIndices);
209         for (int i = 0; i < selIndices.length; i++) {
210             selIndices[i] = --selIndices[i];
211         }
212         sourceList.setSelectedIndices(selIndices);
213     }//GEN-LAST:event_moveUp
214

215     private void removeFolder(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeFolder
216
Object JavaDoc[] selVals = sourceList.getSelectedValues();
217         int toSelect = sourceList.getSelectedIndex() - 1;
218         URL JavaDoc[] selURLs = new URL JavaDoc[selVals.length];
219         System.arraycopy(selVals, 0, selURLs, 0, selVals.length);
220         model.removeSourceRoot(selURLs);
221         sourceList.setSelectedIndex(toSelect);
222     }//GEN-LAST:event_removeFolder
223

224     private void addZipOrFolder(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addZipOrFolder
225
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc(ModuleUISettings.getDefault().getLastUsedNbPlatformLocation());
226         chooser.setAcceptAllFileFilterUsed(false);
227         chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
228         chooser.setFileFilter(new FileFilter JavaDoc() {
229             public boolean accept(File JavaDoc f) {
230                 return f.isDirectory() || isValidNbSourceRoot(f);
231             }
232             public String JavaDoc getDescription() {
233                 return getMessage("CTL_SourcesTab");
234             }
235         });
236         int ret = chooser.showOpenDialog(this);
237         if (ret == JFileChooser.APPROVE_OPTION) {
238             File JavaDoc file = FileUtil.normalizeFile(chooser.getSelectedFile());
239             if (!file.exists() || (file.isFile() && !isValidNbSourceRoot(file))) {
240                 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
241                         getMessage("MSG_NotValidNBSrcZIP")));
242             } else {
243                 ModuleUISettings.getDefault().setLastUsedNbPlatformLocation(file.getParentFile().getAbsolutePath());
244                 URL JavaDoc newUrl = Util.urlForDirOrJar(file);
245                 model.addSourceRoot(newUrl);
246                 sourceList.setSelectedValue(newUrl, true);
247             }
248         }
249     }//GEN-LAST:event_addZipOrFolder
250

251     private static boolean isValidNbSourceRoot(final File JavaDoc nbSrcRoot) {
252         boolean isValid = false;
253         String JavaDoc lcName = nbSrcRoot.getName().toLowerCase(Locale.US);
254         if (lcName.endsWith(".jar") || lcName.endsWith(".zip")) { // NOI18N
255
try {
256                 isValid = GlobalSourceForBinaryImpl.NetBeansSourcesParser.getInstance(nbSrcRoot) != null;
257             } catch (IOException JavaDoc ex) {
258                 // isValid = false
259
}
260         }
261         return isValid;
262     }
263     
264     // Variables declaration - do not modify//GEN-BEGIN:variables
265
private javax.swing.JButton JavaDoc addFolderButton;
266     private javax.swing.JPanel JavaDoc buttonPanel;
267     private javax.swing.JButton JavaDoc moveDownButton;
268     private javax.swing.JButton JavaDoc moveUpButton;
269     private javax.swing.JButton JavaDoc removeButton;
270     private javax.swing.JLabel JavaDoc sourceLabel;
271     private javax.swing.JList JavaDoc sourceList;
272     private javax.swing.JScrollPane JavaDoc sourceSP;
273     // End of variables declaration//GEN-END:variables
274

275     private void initAccessibility() {
276         addFolderButton.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_addFolderButton"));
277         sourceList.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_sourceList"));
278         moveDownButton.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_moveDownButton"));
279         moveUpButton.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_moveUpButton"));
280         removeButton.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_removeButton"));
281     }
282     
283     private String JavaDoc getMessage(String JavaDoc key) {
284         return NbBundle.getMessage(NbPlatformCustomizerSources.class, key);
285     }
286     
287     static abstract class ListListener implements ListDataListener JavaDoc, ListSelectionListener JavaDoc {
288         
289         public void intervalAdded(final ListDataEvent JavaDoc e) {
290             listChanged();
291         }
292         
293         public void intervalRemoved(final ListDataEvent JavaDoc e) {
294             listChanged();
295         }
296         
297         public void contentsChanged(final ListDataEvent JavaDoc e) {
298             listChanged();
299         }
300         
301         public void valueChanged(final ListSelectionEvent JavaDoc e) {
302             if (!e.getValueIsAdjusting()) {
303                 listChanged();
304             }
305         }
306         
307         abstract void listChanged();
308         
309     }
310     
311 }
312
Popular Tags