KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > platform > wizard > LocationChooser


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.java.platform.wizard;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.KeyEvent JavaDoc;
24 import java.beans.*;
25 import java.util.*;
26 import java.awt.*;
27 import java.io.File JavaDoc;
28 import javax.swing.KeyStroke JavaDoc;
29 import javax.swing.event.*;
30 import javax.swing.*;
31 import javax.swing.filechooser.FileFilter JavaDoc;
32 import javax.swing.filechooser.FileSystemView JavaDoc;
33 import javax.swing.filechooser.FileView JavaDoc;
34 import javax.accessibility.Accessible JavaDoc;
35 import javax.accessibility.AccessibleContext JavaDoc;
36
37 import org.openide.*;
38 import org.openide.filesystems.*;
39 import org.openide.loaders.*;
40 import org.openide.util.HelpCtx;
41 import org.openide.util.NbBundle;
42
43 import org.netbeans.modules.java.platform.PlatformSettings;
44 import org.netbeans.spi.java.platform.PlatformInstall;
45 import org.openide.util.Utilities;
46
47 /**
48  *
49  * @author sd99038, Tomas Zezula
50  */

51 public class LocationChooser extends javax.swing.JFileChooser JavaDoc implements PropertyChangeListener {
52         
53
54     private static final Dimension PREFERRED_SIZE = new Dimension (500,340);
55     
56     private WizardDescriptor.InstantiatingIterator iterator;
57     private LocationChooser.Panel firer;
58     private PlatformFileView platformFileView;
59     private PlatformAccessory accessory;
60     
61
62     public LocationChooser (LocationChooser.Panel firer) {
63         super ();
64         this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
65         this.setName (NbBundle.getMessage(LocationChooser.class,"TXT_PlatformFolderTitle"));
66         this.setFileSelectionMode(DIRECTORIES_ONLY);
67         this.setMultiSelectionEnabled(false);
68         this.setControlButtonsAreShown(false);
69         this.accessory = new PlatformAccessory ();
70         this.setFileFilter (new PlatformFileFilter());
71         this.setAccessory (this.accessory);
72         this.firer = firer;
73         this.platformFileView = new PlatformFileView( this.getFileSystemView());
74         this.setFileView(this.platformFileView);
75         this.addPropertyChangeListener (this);
76         this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(LocationChooser.class,"AD_LocationChooser"));
77
78         //XXX JFileChooser workaround
79
getActionMap().put("cancel",
80                 new AbstractAction() {
81                     public void actionPerformed(ActionEvent JavaDoc e) {
82                         Container parent = LocationChooser.this.getParent();
83                         do {
84                             parent = parent.getParent();
85                         } while (parent != null && !(parent instanceof Window));
86                         if (parent != null) {
87                             ((Window)parent).setVisible(false);
88                         }
89         }});
90         getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put (KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
91         
92     }
93
94     
95     public Dimension getPreferredSize () {
96         return PREFERRED_SIZE;
97     }
98     
99
100
101     public void propertyChange(PropertyChangeEvent evt) {
102         if (SELECTED_FILE_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
103             this.iterator = null;
104             this.accessory.setType (""); //NOI18N
105
File JavaDoc file = this.getSelectedFile();
106             if (file != null) {
107                 file = FileUtil.normalizeFile(file);
108                 FileObject fo = FileUtil.toFileObject (FileUtil.normalizeFile(file));
109                 if (fo != null) {
110                     PlatformInstall install = this.platformFileView.getPlatformInstall();
111                     if (install != null && install.accept(fo)) {
112                         this.accessory.setType (install.getDisplayName());
113                         this.iterator = install.createIterator(fo);
114                     }
115                 }
116             }
117             this.firer.fireStateChanged();
118         }
119     }
120
121
122     private boolean valid () {
123         return this.getInstaller() != null;
124     }
125
126     private void read (WizardDescriptor settings) {
127         PlatformSettings ps = PlatformSettings.getDefault();
128         if (ps !=null) {
129             this.setCurrentDirectory(ps.getPlatformsFolder());
130         }
131     }
132
133     private void store (WizardDescriptor settings) {
134         File JavaDoc dir = this.getCurrentDirectory();
135         if (dir != null) {
136             PlatformSettings ps = PlatformSettings.getDefault();
137             if (ps != null) {
138                 ps.setPlatformsFolder(dir);
139             }
140         }
141     }
142
143     private WizardDescriptor.InstantiatingIterator getInstaller () {
144         return this.iterator;
145     }
146     
147     private void setPlatformInstall (PlatformInstall platformInstall) {
148         this.platformFileView.setPlatformInstall(platformInstall);
149     }
150     
151     private PlatformInstall getPlatformInstall () {
152         return this.platformFileView.getPlatformInstall ();
153     }
154     
155     private static class PlatformFileFilter extends FileFilter JavaDoc {
156
157         public boolean accept(File JavaDoc f) {
158             return f.isDirectory();
159         }
160
161         public String JavaDoc getDescription() {
162             return NbBundle.getMessage (LocationChooser.class,"TXT_PlatformFolder");
163         }
164     }
165
166     private static class PlatformAccessory extends JPanel {
167
168         private JTextField tf;
169
170         public PlatformAccessory () {
171             this.initComponents ();
172         }
173
174         private void setType (String JavaDoc type) {
175             this.tf.setText(type);
176         }
177         
178
179         private void initComponents () {
180             this.getAccessibleContext().setAccessibleName (NbBundle.getMessage(LocationChooser.class,"AN_LocationChooserAccessiory"));
181             this.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage(LocationChooser.class,"AD_LocationChooserAccessiory"));
182             GridBagLayout l = new GridBagLayout();
183             this.setLayout (l);
184             JLabel label = new JLabel (NbBundle.getMessage(LocationChooser.class,"TXT_PlatformType"));
185             label.setDisplayedMnemonic (NbBundle.getMessage(LocationChooser.class,"MNE_PlatformType").charAt(0));
186             GridBagConstraints c = new GridBagConstraints();
187             c.gridx = c.gridy = GridBagConstraints.RELATIVE;
188             c.gridwidth = GridBagConstraints.REMAINDER;
189             c.insets = new Insets (0,12,3,12);
190             c.anchor = GridBagConstraints.NORTHWEST;
191             l.setConstraints(label,c);
192             this.add (label);
193             this.tf = new JTextField();
194             this.tf.setColumns(15);
195             this.tf.setEditable(false);
196             this.tf.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(LocationChooser.class,"AD_PlatformType"));
197             c = new GridBagConstraints();
198             c.gridx = c.gridy = GridBagConstraints.RELATIVE;
199             c.gridwidth = GridBagConstraints.REMAINDER;
200             c.insets = new Insets (3,12,12,12);
201             c.anchor = GridBagConstraints.NORTHWEST;
202             c.fill = GridBagConstraints.HORIZONTAL;
203             c.weightx = 1.0;
204             l.setConstraints(this.tf,c);
205             this.add (tf);
206             label.setLabelFor (this.tf);
207             JPanel fill = new JPanel ();
208             c = new GridBagConstraints();
209             c.gridx = c.gridy = GridBagConstraints.RELATIVE;
210             c.gridwidth = GridBagConstraints.REMAINDER;
211             c.insets = new Insets (0,12,12,12);
212             c.anchor = GridBagConstraints.NORTHWEST;
213             c.fill = GridBagConstraints.BOTH;
214             c.weightx = c.weighty = 1.0;
215             l.setConstraints(fill,c);
216             this.add (fill);
217         }
218     }
219
220
221     /**
222      * Controller for the LocationChooser panel.
223      */

224     public static class Panel implements WizardDescriptor.Panel {
225             
226         LocationChooser component;
227         Collection listeners = new ArrayList();
228
229
230
231         public java.awt.Component JavaDoc getComponent() {
232             if (component == null) {
233                 this.component = new LocationChooser (this);
234             }
235             return component;
236         }
237         
238         public HelpCtx getHelp() {
239             return new HelpCtx (LocationChooser.class);
240         }
241         
242         public boolean isValid() {
243             return ((LocationChooser)this.getComponent()).valid();
244         }
245         
246         public void readSettings(Object JavaDoc settings) {
247             ((LocationChooser)this.getComponent()).read ((WizardDescriptor) settings);
248         }
249         
250         public void addChangeListener(ChangeListener l) {
251             listeners.add(l);
252         }
253
254         public void removeChangeListener(ChangeListener l) {
255             listeners.remove(l);
256         }
257         
258         public void storeSettings(Object JavaDoc settings) {
259             ((LocationChooser)this.getComponent()).store((WizardDescriptor)settings);
260         }
261         
262         /**
263          * Returns the currently selected installer.
264          */

265         WizardDescriptor.InstantiatingIterator getInstallerIterator() {
266             return ((LocationChooser)this.getComponent()).getInstaller ();
267         }
268         
269         void setPlatformInstall (PlatformInstall platformInstall) {
270             ((LocationChooser)this.getComponent ()).setPlatformInstall (platformInstall);
271         }
272         
273         PlatformInstall getPlatformInstall () {
274             return ((LocationChooser)this.getComponent ()).getPlatformInstall ();
275         }
276         
277         void fireStateChanged() {
278             ChangeListener[] ll;
279             synchronized (this) {
280                 if (listeners.isEmpty())
281                     return;
282                 ll = (ChangeListener[])listeners.toArray(new ChangeListener[0]);
283             }
284             ChangeEvent ev = new ChangeEvent(this);
285             for (int i = 0; i < ll.length; i++)
286                 ll[i].stateChanged(ev);
287         }
288
289     }
290     
291     private static class MergedIcon implements Icon {
292         
293         private Icon icon1;
294         private Icon icon2;
295         private int xMerge;
296         private int yMerge;
297         
298         MergedIcon( Icon icon1, Icon icon2, int xMerge, int yMerge ) {
299             
300             this.icon1 = icon1;
301             this.icon2 = icon2;
302             
303             if ( xMerge == -1 ) {
304                 xMerge = icon1.getIconWidth() - icon2.getIconWidth();
305             }
306             
307             if ( yMerge == -1 ) {
308                 yMerge = icon1.getIconHeight() - icon2.getIconHeight();
309             }
310             
311             this.xMerge = xMerge;
312             this.yMerge = yMerge;
313         }
314         
315         public int getIconHeight() {
316             return Math.max( icon1.getIconHeight(), yMerge + icon2.getIconHeight() );
317         }
318         
319         public int getIconWidth() {
320             return Math.max( icon1.getIconWidth(), yMerge + icon2.getIconWidth() );
321         }
322         
323         public void paintIcon(java.awt.Component JavaDoc c, java.awt.Graphics JavaDoc g, int x, int y) {
324             icon1.paintIcon( c, g, x, y );
325             icon2.paintIcon( c, g, x + xMerge, y + yMerge );
326         }
327         
328     }
329     
330     private static class PlatformFileView extends FileView JavaDoc {
331         
332         private static final Icon BADGE = new ImageIcon(Utilities.loadImage("org/netbeans/modules/java/platform/resources/platformBadge.gif")); // NOI18N
333
private static final Icon EMPTY = new ImageIcon(Utilities.loadImage("org/netbeans/modules/java/platform/resources/empty.gif")); // NOI18N
334

335         private FileSystemView JavaDoc fsv;
336         private Icon lastOriginal;
337         private Icon lastMerged;
338         private PlatformInstall platformInstall;
339         
340         public PlatformFileView( FileSystemView JavaDoc fsv) {
341             this.fsv = fsv;
342         }
343                 
344         public Icon getIcon(File JavaDoc _f) {
345             File JavaDoc f = FileUtil.normalizeFile(_f);
346             Icon original = fsv.getSystemIcon(f);
347             if (original == null) {
348                 // L&F (e.g. GTK) did not specify any icon.
349
original = EMPTY;
350             }
351             if ( isPlatformDir( f ) ) {
352                 if ( original.equals( lastOriginal ) ) {
353                     return lastMerged;
354                 }
355                 lastOriginal = original;
356                 lastMerged = new MergedIcon(original, BADGE, -1, -1);
357                 return lastMerged;
358             }
359             else {
360                 return original;
361             }
362         }
363         
364         public void setPlatformInstall (PlatformInstall platformInstall) {
365             this.platformInstall = platformInstall;
366         }
367         
368         public PlatformInstall getPlatformInstall () {
369             return this.platformInstall;
370         }
371         
372         
373         private boolean isPlatformDir ( File JavaDoc f ) {
374             FileObject fo = (f != null) ? convertToValidDir(f) : null;
375             if (fo != null) {
376                 //XXX: Workaround of /net folder on Unix, the folders in the root are not badged as platforms.
377
// User can still select them.
378
try {
379                     if (Utilities.isUnix() && (fo.getParent() == null || fo.getFileSystem().getRoot().equals(fo.getParent()))) {
380                         return false;
381                     }
382                 } catch (FileStateInvalidException e) {
383                     return false;
384                 }
385                 if (this.platformInstall.accept(fo)) {
386                     return true;
387                 }
388             }
389             return false;
390         }
391
392         private static FileObject convertToValidDir(File JavaDoc f) {
393             FileObject fo;
394             File JavaDoc testFile = new File JavaDoc( f.getPath() );
395             if ( testFile == null || testFile.getParent() == null ) {
396                 // BTW this means that roots of file systems can't be project
397
// directories.
398
return null;
399             }
400         
401             /**ATTENTION: on Windows may occure dir.isDirectory () == dir.isFile () == true then
402              * its used testFile instead of dir.
403             */

404             if ( !testFile.isDirectory() ) {
405                 return null;
406             }
407             
408             fo = FileUtil.toFileObject(FileUtil.normalizeFile(f));
409             return fo;
410         }
411     }
412 }
413
Popular Tags