Removed
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
30 |
* If you wish your version of this file to be governed by only the CDDL |
31 |
* or only the GPL Version 2, indicate your decision by adding |
32 |
* "[Contributor] elects to include this software in this distribution |
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
34 |
* single choice of license, a recipient has the option to distribute |
35 |
* your version of this file under either the CDDL, the GPL Version 2 or |
36 |
* to extend the choice of license to its licensees as provided above. |
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
41 |
package org.netbeans.modules.mobility.cldcplatform.wizard; |
42 |
|
43 |
import java.awt.Container; |
44 |
import java.awt.Dimension; |
45 |
import java.awt.Window; |
46 |
import java.awt.event.ActionEvent; |
47 |
import java.awt.event.KeyEvent; |
48 |
import java.beans.PropertyChangeEvent; |
49 |
import java.beans.PropertyChangeListener; |
50 |
import java.io.File; |
51 |
import java.util.Collection; |
52 |
import java.util.Collections; |
53 |
import javax.swing.AbstractAction; |
54 |
import javax.swing.BorderFactory; |
55 |
import javax.swing.Icon; |
56 |
import javax.swing.ImageIcon; |
57 |
import javax.swing.JFileChooser; |
58 |
import javax.swing.KeyStroke; |
59 |
import javax.swing.filechooser.FileFilter; |
60 |
import javax.swing.filechooser.FileSystemView; |
61 |
import javax.swing.filechooser.FileView; |
62 |
import org.netbeans.modules.mobility.cldcplatform.J2MEPlatform; |
63 |
import org.netbeans.spi.java.platform.PlatformInstall; |
64 |
import org.netbeans.spi.mobility.cldcplatform.CustomCLDCPlatformConfigurator; |
65 |
import org.openide.WizardDescriptor; |
66 |
import org.openide.filesystems.FileObject; |
67 |
import org.openide.filesystems.FileStateInvalidException; |
68 |
import org.openide.filesystems.FileUtil; |
69 |
import org.openide.util.ChangeSupport; |
70 |
import org.openide.util.ImageUtilities; |
71 |
import org.openide.util.Lookup; |
72 |
import org.openide.util.NbBundle; |
73 |
import org.openide.util.Utilities; |
74 |
|
75 |
/** |
76 |
* Copied from Java Platform |
77 |
* @author sd99038, Tomas Zezula |
78 |
*/ |
79 |
public class LocationChooser extends JFileChooser implements PropertyChangeListener { |
80 |
|
81 |
private static final Dimension PREFERRED_SIZE = new Dimension(600, 340); |
82 |
private WizardDescriptor.InstantiatingIterator<WizardDescriptor> iterator; |
83 |
private final ChangeSupport cs = new ChangeSupport(this); |
84 |
private PlatformFileView platformFileView; |
85 |
|
86 |
public LocationChooser(File dir) { |
87 |
super(dir); |
88 |
this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); |
89 |
this.setName(NbBundle.getMessage(LocationChooser.class, "Title_FindPanel_Choose_To_Search")); |
90 |
this.setFileSelectionMode(DIRECTORIES_ONLY); |
91 |
this.setMultiSelectionEnabled(false); |
92 |
this.setControlButtonsAreShown(false); |
93 |
this.setFileFilter(new PlatformFileFilter()); |
94 |
this.platformFileView = new PlatformFileView(this.getFileSystemView()); |
95 |
this.setFileView(this.platformFileView); |
96 |
this.addPropertyChangeListener(this); |
97 |
this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(LocationChooser.class, "AD_LocationChooser")); |
98 |
|
99 |
//XXX JFileChooser workaround |
100 |
getActionMap().put("cancel", |
101 |
new AbstractAction() { |
102 |
|
103 |
public void actionPerformed(ActionEvent e) { |
104 |
Container parent = LocationChooser.this.getParent(); |
105 |
do { |
106 |
parent = parent.getParent(); |
107 |
} while (parent != null && !(parent instanceof Window)); |
108 |
if (parent != null) { |
109 |
((Window) parent).setVisible(false); |
110 |
} |
111 |
} |
112 |
}); |
113 |
getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel"); |
114 |
this.setBorder(null); |
115 |
} |
116 |
|
117 |
@Override |
118 |
public Dimension getPreferredSize() { |
119 |
return PREFERRED_SIZE; |
120 |
} |
121 |
|
122 |
public void propertyChange(PropertyChangeEvent evt) { |
123 |
if (SELECTED_FILE_CHANGED_PROPERTY.equals(evt.getPropertyName())) { |
124 |
this.iterator = null; |
125 |
File file = this.getSelectedFile(); |
126 |
if (file != null) { |
127 |
file = FileUtil.normalizeFile(file); |
128 |
FileObject fo = FileUtil.toFileObject(FileUtil.normalizeFile(file)); |
129 |
if (fo != null) { |
130 |
PlatformInstall install = this.platformFileView.getPlatformInstall(); |
131 |
if (install != null && install.accept(fo)) { |
132 |
this.iterator = install.createIterator(fo); |
133 |
} |
134 |
} |
135 |
} |
136 |
cs.fireChange(); |
137 |
} |
138 |
} |
139 |
|
140 |
private static class PlatformFileFilter extends FileFilter { |
141 |
|
142 |
public boolean accept(File f) { |
143 |
return f.isDirectory(); |
144 |
} |
145 |
|
146 |
public String getDescription() { |
147 |
return NbBundle.getMessage(LocationChooser.class, "TXT_PlatformFolder"); |
148 |
} |
149 |
} |
150 |
private static class MergedIcon implements Icon { |
151 |
|
152 |
private Icon icon1; |
153 |
private Icon icon2; |
154 |
private int xMerge; |
155 |
private int yMerge; |
156 |
|
157 |
MergedIcon(Icon icon1, Icon icon2, int xMerge, int yMerge) { |
158 |
|
159 |
this.icon1 = icon1; |
160 |
this.icon2 = icon2; |
161 |
|
162 |
if (xMerge == -1) { |
163 |
xMerge = icon1.getIconWidth() - icon2.getIconWidth(); |
164 |
} |
165 |
|
166 |
if (yMerge == -1) { |
167 |
yMerge = icon1.getIconHeight() - icon2.getIconHeight(); |
168 |
} |
169 |
|
170 |
this.xMerge = xMerge; |
171 |
this.yMerge = yMerge; |
172 |
} |
173 |
|
174 |
public int getIconHeight() { |
175 |
return Math.max(icon1.getIconHeight(), yMerge + icon2.getIconHeight()); |
176 |
} |
177 |
|
178 |
public int getIconWidth() { |
179 |
return Math.max(icon1.getIconWidth(), yMerge + icon2.getIconWidth()); |
180 |
} |
181 |
|
182 |
public void paintIcon(java.awt.Component c, java.awt.Graphics g, int x, int y) { |
183 |
icon1.paintIcon(c, g, x, y); |
184 |
icon2.paintIcon(c, g, x + xMerge, y + yMerge); |
185 |
} |
186 |
} |
187 |
|
188 |
private static class PlatformFileView extends FileView { |
189 |
|
190 |
private final Icon BADGE = new ImageIcon(ImageUtilities.loadImage("org/netbeans/modules/java/platform/resources/platformBadge.gif")); // NOI18N |
191 |
private final Icon EMPTY = new ImageIcon(ImageUtilities.loadImage("org/netbeans/modules/java/platform/resources/empty.gif")); // NOI18N |
192 |
private FileSystemView fsv; |
193 |
private Icon lastOriginal; |
194 |
private Icon lastMerged; |
195 |
private PlatformInstall platformInstall; |
196 |
|
197 |
public PlatformFileView(FileSystemView fsv) { |
198 |
this.fsv = fsv; |
199 |
} |
200 |
|
201 |
public |
202 |
@Override |
203 |
Icon getIcon( File _f) { |
204 |
File f = FileUtil.normalizeFile(_f); |
205 |
Icon original = fsv.getSystemIcon(f); |
206 |
if (original == null) { |
207 |
// L&F (e.g. GTK) did not specify any icon. |
208 |
original = EMPTY; |
209 |
} |
210 |
if (isPlatformDir(f)) { |
211 |
if (original.equals(lastOriginal)) { |
212 |
return lastMerged; |
213 |
} |
214 |
lastOriginal = original; |
215 |
lastMerged = new MergedIcon(original, BADGE, -1, -1); |
216 |
return lastMerged; |
217 |
} else { |
218 |
return original; |
219 |
} |
220 |
} |
221 |
|
222 |
public void setPlatformInstall(PlatformInstall platformInstall) { |
223 |
this.platformInstall = platformInstall; |
224 |
} |
225 |
|
226 |
public PlatformInstall getPlatformInstall() { |
227 |
return this.platformInstall; |
228 |
} |
229 |
|
230 |
private boolean isPlatformDir(File f) { |
231 |
//XXX: Workaround of hard NFS mounts on Solaris. |
232 |
final int osId = Utilities.getOperatingSystem(); |
233 |
if (osId == Utilities.OS_SOLARIS || osId == Utilities.OS_SUNOS) { |
234 |
return false; |
235 |
} |
236 |
FileObject fo = (f != null) ? convertToValidDir(f) : null; |
237 |
if (fo != null) { |
238 |
//XXX: Workaround of /net folder on Unix, the folders in the root are not badged as platforms. |
239 |
// User can still select them. |
240 |
try { |
241 |
if (Utilities.isUnix() && (fo.getParent() == null || fo.getFileSystem().getRoot().equals(fo.getParent()))) { |
242 |
return false; |
243 |
} |
244 |
} catch (FileStateInvalidException e) { |
245 |
return false; |
246 |
} |
247 |
return isPossibleJ2MEPlatform(FileUtil.toFile(fo)); |
248 |
} |
249 |
return false; |
250 |
} |
251 |
|
252 |
public boolean isPossibleJ2MEPlatform(final File directory) { |
253 |
Collection <? extends CustomCLDCPlatformConfigurator> customConfigurators = Lookup.getDefault().lookupAll(CustomCLDCPlatformConfigurator.class); |
254 |
for ( CustomCLDCPlatformConfigurator pc : customConfigurators ) |
255 |
if (pc.isPossiblePlatform(directory)) return true; |
256 |
final FileObject dir = FileUtil.toFileObject(directory); |
257 |
return dir != null && J2MEPlatform.findTool("emulator", Collections.singletonList(dir)) != null // NOI18N |
258 |
&& J2MEPlatform.findTool("preverify", Collections.singletonList(dir)) != null; //NOI18N |
259 |
} |
260 |
|
261 |
private static FileObject convertToValidDir(File f) { |
262 |
FileObject fo; |
263 |
File testFile = new File(f.getPath()); |
264 |
if (testFile == null || testFile.getParent() == null) { |
265 |
// BTW this means that roots of file systems can't be project |
266 |
// directories. |
267 |
return null; |
268 |
} |
269 |
|
270 |
/**ATTENTION: on Windows may occure dir.isDirectory () == dir.isFile () == true then |
271 |
* its used testFile instead of dir. |
272 |
*/ |
273 |
if (!testFile.isDirectory()) { |
274 |
return null; |
275 |
} |
276 |
|
277 |
fo = FileUtil.toFileObject(FileUtil.normalizeFile(f)); |
278 |
return fo; |
279 |
} |
280 |
} |
281 |
} |