1 19 20 package org.netbeans.modules.java.platform.wizard; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.KeyEvent ; 24 import java.beans.*; 25 import java.util.*; 26 import java.awt.*; 27 import java.io.File ; 28 import javax.swing.KeyStroke ; 29 import javax.swing.event.*; 30 import javax.swing.*; 31 import javax.swing.filechooser.FileFilter ; 32 import javax.swing.filechooser.FileSystemView ; 33 import javax.swing.filechooser.FileView ; 34 import javax.accessibility.Accessible ; 35 import javax.accessibility.AccessibleContext ; 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 51 public class LocationChooser extends javax.swing.JFileChooser 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 getActionMap().put("cancel", 80 new AbstractAction() { 81 public void actionPerformed(ActionEvent 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 (""); File 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 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 { 156 157 public boolean accept(File f) { 158 return f.isDirectory(); 159 } 160 161 public String 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 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 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 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 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 settings) { 259 ((LocationChooser)this.getComponent()).store((WizardDescriptor)settings); 260 } 261 262 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 c, java.awt.Graphics 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 { 331 332 private static final Icon BADGE = new ImageIcon(Utilities.loadImage("org/netbeans/modules/java/platform/resources/platformBadge.gif")); private static final Icon EMPTY = new ImageIcon(Utilities.loadImage("org/netbeans/modules/java/platform/resources/empty.gif")); 335 private FileSystemView fsv; 336 private Icon lastOriginal; 337 private Icon lastMerged; 338 private PlatformInstall platformInstall; 339 340 public PlatformFileView( FileSystemView fsv) { 341 this.fsv = fsv; 342 } 343 344 public Icon getIcon(File _f) { 345 File f = FileUtil.normalizeFile(_f); 346 Icon original = fsv.getSystemIcon(f); 347 if (original == null) { 348 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 f ) { 374 FileObject fo = (f != null) ? convertToValidDir(f) : null; 375 if (fo != null) { 376 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 f) { 393 FileObject fo; 394 File testFile = new File ( f.getPath() ); 395 if ( testFile == null || testFile.getParent() == null ) { 396 return null; 399 } 400 401 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 |