1 19 20 package org.netbeans.modules.apisupport.project.ui.platform; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.net.URI ; 27 import java.net.URL ; 28 import java.text.Collator ; 29 import java.util.Arrays ; 30 import java.util.Comparator ; 31 import java.util.Set ; 32 import java.util.TreeSet ; 33 import javax.swing.AbstractListModel ; 34 import javax.swing.ComboBoxModel ; 35 import javax.swing.DefaultListCellRenderer ; 36 import javax.swing.JComboBox ; 37 import javax.swing.JLabel ; 38 import javax.swing.JList ; 39 import javax.swing.ListCellRenderer ; 40 import javax.swing.ListSelectionModel ; 41 import javax.swing.MutableComboBoxModel ; 42 import javax.swing.UIManager ; 43 import javax.swing.plaf.UIResource ; 44 import org.netbeans.api.project.Project; 45 import org.netbeans.api.project.ui.OpenProjects; 46 import org.netbeans.modules.apisupport.project.ui.customizer.SuiteUtils; 47 import org.netbeans.modules.apisupport.project.universe.NbPlatform; 48 import org.netbeans.modules.apisupport.project.universe.ModuleEntry; 49 import org.openide.ErrorManager; 50 import org.openide.filesystems.FileUtil; 51 52 58 public final class PlatformComponentFactory { 59 60 private static final Color INVALID_PLAF_COLOR = UIManager.getColor("nb.errorForeground"); 62 63 private static Set <String > userSuites = new TreeSet (Collator.getInstance()); 64 65 private PlatformComponentFactory() { 66 } 68 69 73 public static JComboBox getNbPlatformsComboxBox() { 74 JComboBox plafComboBox = new JComboBox (new NbPlatformListModel()); 75 plafComboBox.setRenderer(new NbPlatformListRenderer()); 76 return plafComboBox; 77 } 78 79 83 public static JList getNbPlatformsList() { 84 JList plafList = new JList (new NbPlatformListModel()); 85 plafList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 86 plafList.setCellRenderer(new NbPlatformListRenderer()); 87 return plafList; 88 } 89 90 94 public static JComboBox getSuitesComboBox() { 95 MutableComboBoxModel model = new SuiteListModel(userSuites); 96 Project[] projects = OpenProjects.getDefault().getOpenProjects(); 97 for (int i = 0; i < projects.length; i++) { 98 String suiteDir = SuiteUtils.getSuiteDirectoryPath(projects[i]); 99 if (suiteDir != null) { 100 model.addElement(suiteDir); 101 } 102 } 103 JComboBox suiteCombo = new JComboBox (model); 104 if (model.getSize() > 0) { 105 suiteCombo.setSelectedIndex(0); 106 } 107 return suiteCombo; 108 } 109 110 115 public static void addUserSuite(String suiteDir) { 116 userSuites.add(suiteDir); 117 } 118 119 public static ListCellRenderer getURLListRenderer() { 120 return new URLListRenderer(); 121 } 122 123 129 private static class NbPlatformListRenderer extends JLabel implements ListCellRenderer , UIResource { 130 131 public NbPlatformListRenderer () { 132 setOpaque(true); 133 } 134 135 public Component getListCellRendererComponent(JList list, Object value, 136 int index, boolean isSelected, boolean cellHasFocus) { 137 setName("ComboBox.listRenderer"); 140 NbPlatform plaf = ((NbPlatform) value); 141 String text = plaf == null ? "" : plaf.getLabel(); setText(text); 144 145 if ( isSelected ) { 146 setBackground(list.getSelectionBackground()); 147 setForeground(list.getSelectionForeground()); 148 } 149 else { 150 setBackground(list.getBackground()); 151 setForeground(list.getForeground()); 152 } 153 154 if (plaf != null && !plaf.isValid()) { 155 setForeground(INVALID_PLAF_COLOR); 156 } 157 158 return this; 159 } 160 161 public String getName() { 163 String name = super.getName(); 164 return name == null ? "ComboBox.renderer" : name; } 166 167 } 168 169 174 public static class NbPlatformListModel extends AbstractListModel 175 implements ComboBoxModel { 176 177 private static NbPlatform[] getSortedPlatforms() { 178 Set _platforms = NbPlatform.getPlatforms(); 179 NbPlatform[] platforms = (NbPlatform[]) _platforms.toArray(new NbPlatform[_platforms.size()]); 180 Arrays.sort(platforms, new Comparator () { 181 public int compare(Object o1, Object o2) { 182 int res = Collator.getInstance().compare(((NbPlatform) o1).getLabel(), ((NbPlatform) o2).getLabel()); 183 if (res != 0) { 184 return res; 185 } else { 186 return System.identityHashCode(o1) - System.identityHashCode(o2); 187 } 188 } 189 }); 190 return platforms; 191 } 192 193 private NbPlatform[] nbPlafs; 194 private Object selectedPlaf; 195 196 public NbPlatformListModel() { 197 nbPlafs = getSortedPlatforms(); 198 if (nbPlafs.length > 0) { 199 selectedPlaf = nbPlafs[0]; 200 } 201 } 202 203 public int getSize() { 204 return nbPlafs.length; 205 } 206 207 public Object getElementAt(int index) { 208 return index < nbPlafs.length ? nbPlafs[index] : null; 209 } 210 211 public void setSelectedItem(Object plaf) { 212 assert plaf == null || plaf instanceof NbPlatform; 213 if (selectedPlaf != plaf) { 214 selectedPlaf = plaf; 215 fireContentsChanged(this, -1, -1); 216 } 217 } 218 219 public Object getSelectedItem() { 220 return selectedPlaf; 221 } 222 223 void removePlatform(NbPlatform plaf) { 224 try { 225 NbPlatform.removePlatform(plaf); 226 nbPlafs = getSortedPlatforms(); fireContentsChanged(this, 0, nbPlafs.length - 1); 228 } catch (IOException e) { 229 ErrorManager.getDefault().notify(ErrorManager.USER, e); 231 } 232 } 233 234 NbPlatform addPlatform(String id, String destdir, String label) { 235 try { 236 NbPlatform def = NbPlatform.getDefaultPlatform(); 237 NbPlatform plaf = def != null ? 238 NbPlatform.addPlatform(id, new File (destdir), def.getHarnessLocation(), label) : 239 NbPlatform.addPlatform(id, new File (destdir), label); 241 nbPlafs = getSortedPlatforms(); fireContentsChanged(this, 0, nbPlafs.length - 1); 243 return plaf; 244 } catch (IOException e) { 245 ErrorManager.getDefault().notify(ErrorManager.USER, e); 247 } 248 return null; 249 } 250 } 251 252 static class ModuleEntryListModel extends AbstractListModel { 253 254 private ModuleEntry[] mes; 255 256 ModuleEntryListModel(ModuleEntry[] mes) { 257 this.mes = mes; 258 } 259 260 public int getSize() { 261 return mes.length; 262 } 263 264 public Object getElementAt(int index) { 265 return mes[index].getLocalizedName(); 266 } 267 } 268 269 private static class SuiteListModel extends AbstractListModel 270 implements MutableComboBoxModel { 271 272 private Set <String > suites = new TreeSet (Collator.getInstance()); 273 private String selectedSuite; 274 275 SuiteListModel(Set <String > suites) { 276 this.suites.addAll(suites); 277 } 278 279 public void setSelectedItem(Object suite) { 280 if (suite == null) { 281 return; 282 } 283 if (selectedSuite != suite) { 284 selectedSuite = (String ) suite; 285 fireContentsChanged(this, -1, -1); 286 } 287 } 288 289 public Object getSelectedItem() { 290 return selectedSuite; 291 } 292 293 public int getSize() { 294 return suites.size(); 295 } 296 297 public Object getElementAt(int index) { 298 return suites.toArray()[index]; 299 } 300 301 public void addElement(Object obj) { 302 suites.add((String ) obj); 303 fireIntervalAdded(this, 0, suites.size()); 304 } 305 306 307 public void insertElementAt(Object obj, int index) { 308 assert false : "Who needs to insertElementAt?"; } 310 311 312 public void removeElement(Object obj) { 313 assert false : "Who needs to removeElement?"; } 315 316 317 public void removeElementAt(int index) { 318 assert false : "Who needs to call removeElementAt?"; } 320 } 321 322 326 static final class NbPlatformSourceRootsModel extends AbstractListModel { 327 328 private NbPlatform plaf; 329 private URL [] srcRoots; 330 331 NbPlatformSourceRootsModel(NbPlatform plaf) { 332 this.plaf = plaf; 333 this.srcRoots = plaf.getSourceRoots(); 334 } 335 336 public Object getElementAt(int index) { 337 return srcRoots[index]; 338 } 339 340 public int getSize() { 341 return srcRoots.length; 342 } 343 344 void removeSourceRoot(URL [] srcRootToRemove) { 345 try { 346 plaf.removeSourceRoots(srcRootToRemove); 347 this.srcRoots = plaf.getSourceRoots(); fireContentsChanged(this, 0, srcRootToRemove.length); 349 } catch (IOException e) { 350 ErrorManager.getDefault().notify(ErrorManager.USER, e); 352 } 353 } 354 355 void addSourceRoot(URL srcRootToAdd) { 356 try { 357 plaf.addSourceRoot(srcRootToAdd); 358 this.srcRoots = plaf.getSourceRoots(); fireContentsChanged(this, 0, srcRoots.length); 360 } catch (IOException e) { 361 ErrorManager.getDefault().notify(ErrorManager.USER, e); 363 } 364 } 365 366 void moveSourceRootsDown(int[] toMoveDown) { 367 try { 368 for (int i = 0; i < toMoveDown.length; i++) { 369 plaf.moveSourceRootDown(toMoveDown[i]); 370 } 371 this.srcRoots = plaf.getSourceRoots(); fireContentsChanged(this, 0, srcRoots.length); 373 } catch (IOException e) { 374 ErrorManager.getDefault().notify(ErrorManager.USER, e); 376 } 377 } 378 379 void moveSourceRootsUp(int[] toMoveUp) { 380 try { 381 for (int i = 0; i < toMoveUp.length; i++) { 382 plaf.moveSourceRootUp(toMoveUp[i]); 383 } 384 this.srcRoots = plaf.getSourceRoots(); fireContentsChanged(this, 0, srcRoots.length); 386 } catch (IOException e) { 387 ErrorManager.getDefault().notify(ErrorManager.USER, e); 389 } 390 } 391 } 392 393 397 static final class NbPlatformJavadocRootsModel extends AbstractListModel { 398 399 private NbPlatform plaf; 400 private URL [] javadocRoots; 401 402 NbPlatformJavadocRootsModel(NbPlatform plaf) { 403 this.plaf = plaf; 404 this.javadocRoots = plaf.getJavadocRoots(); 405 } 406 407 public Object getElementAt(int index) { 408 return javadocRoots[index]; 409 } 410 411 public int getSize() { 412 return javadocRoots.length; 413 } 414 415 void removeJavadocRoots(URL [] jdRootToRemove) { 416 try { 417 plaf.removeJavadocRoots(jdRootToRemove); 418 this.javadocRoots = plaf.getJavadocRoots(); fireContentsChanged(this, 0, javadocRoots.length); 420 } catch (IOException e) { 421 ErrorManager.getDefault().notify(ErrorManager.USER, e); 423 } 424 } 425 426 void addJavadocRoot(URL jdRootToAdd) { 427 try { 428 plaf.addJavadocRoot(jdRootToAdd); 429 this.javadocRoots = plaf.getJavadocRoots(); fireContentsChanged(this, 0, javadocRoots.length); 431 } catch (IOException e) { 432 ErrorManager.getDefault().notify(ErrorManager.USER, e); 434 } 435 } 436 437 void moveJavadocRootsDown(int[] toMoveDown) { 438 try { 439 for (int i = 0; i < toMoveDown.length; i++) { 440 plaf.moveJavadocRootDown(toMoveDown[i]); 441 } 442 this.javadocRoots = plaf.getJavadocRoots(); fireContentsChanged(this, 0, javadocRoots.length); 444 } catch (IOException e) { 445 ErrorManager.getDefault().notify(ErrorManager.USER, e); 447 } 448 } 449 450 void moveJavadocRootsUp(int[] toMoveUp) { 451 try { 452 for (int i = 0; i < toMoveUp.length; i++) { 453 plaf.moveJavadocRootUp(toMoveUp[i]); 454 } 455 this.javadocRoots = plaf.getJavadocRoots(); fireContentsChanged(this, 0, javadocRoots.length); 457 } catch (IOException e) { 458 ErrorManager.getDefault().notify(ErrorManager.USER, e); 460 } 461 } 462 } 463 464 469 static final class URLListRenderer extends DefaultListCellRenderer { 470 471 public Component getListCellRendererComponent(JList list, Object value, 472 int index, boolean isSelected, boolean cellHasFocus) { 473 URL u = (URL ) value; 474 String text = u.toExternalForm(); 475 if (u.getProtocol().equals("file")) { text = new File (URI.create(u.toExternalForm())).getAbsolutePath(); 477 } else if (u.getProtocol().equals("jar")) { URL baseU = FileUtil.getArchiveFile(u); 479 if (u.equals(FileUtil.getArchiveRoot(baseU)) && baseU.getProtocol().equals("file")) { text = new File (URI.create(baseU.toExternalForm())).getAbsolutePath(); 481 } 482 } 483 return super.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus); 484 } 485 } 486 487 } 488 | Popular Tags |