1 19 20 package org.netbeans.modules.project.ui; 21 22 import java.awt.*; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.beans.PropertyChangeEvent ; 26 import java.beans.PropertyChangeListener ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Collections ; 30 import java.util.List ; 31 import javax.swing.DefaultComboBoxModel ; 32 import javax.swing.JLabel ; 33 import javax.swing.JList ; 34 import javax.swing.ListCellRenderer ; 35 import javax.swing.event.ChangeEvent ; 36 import javax.swing.event.ChangeListener ; 37 import javax.swing.plaf.UIResource ; 38 import org.netbeans.api.project.Project; 39 import org.netbeans.api.project.ProjectUtils; 40 import org.netbeans.api.project.ProjectInformation; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.Repository; 43 import org.openide.loaders.DataFolder; 44 import org.openide.loaders.DataObject; 45 import org.openide.loaders.DataShadow; 46 import org.openide.nodes.Children; 47 import org.openide.nodes.FilterNode; 48 import org.openide.nodes.Node; 49 import org.openide.util.AsyncGUIJob; 50 import org.openide.util.NbBundle; 51 import org.openide.util.Utilities; 52 53 56 57 61 final class TemplateChooserPanelGUI extends javax.swing.JPanel implements PropertyChangeListener , AsyncGUIJob { 62 63 64 private static final java.awt.Dimension PREF_DIM = new java.awt.Dimension (500, 340); 65 66 private final List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 68 69 private FileObject templatesFolder; 71 72 private TemplatesPanelGUI.Builder builder; 74 private Project project; 75 private String category; 76 private String template; 77 private boolean isWarmUp = true; 78 private ListCellRenderer projectCellRenderer; 79 private boolean firstTime = true; 80 81 public TemplateChooserPanelGUI() { 82 this.builder = new FileChooserBuilder (); 83 initComponents(); 84 setPreferredSize( PREF_DIM ); 85 setName (org.openide.util.NbBundle.getMessage(TemplateChooserPanelGUI.class, "LBL_TemplateChooserPanelGUI_Name")); projectCellRenderer = new ProjectCellRenderer (); 87 projectsComboBox.setRenderer (projectCellRenderer); 88 } 89 90 public void readValues (Project p, String category, String template) { 91 assert p != null : "Project can not be null"; boolean wf; 93 synchronized (this) { 94 this.project = p; 95 this.category = category; 96 this.template = template; 97 wf = this.isWarmUp; 98 } 99 if (!wf) { 100 this.selectProject ( project ); 101 ((TemplatesPanelGUI)this.templatesPanel).setSelectedCategoryByName (this.category); 102 ((TemplatesPanelGUI)this.templatesPanel).setSelectedTemplateByName (this.template); 103 } 104 } 105 106 108 private void initValues( Project p ) { 109 Project openProjects[] = OpenProjectList.getDefault().getOpenProjects(); 111 Arrays.sort( openProjects, OpenProjectList.PROJECT_BY_DISPLAYNAME ); 112 DefaultComboBoxModel projectsModel = new DefaultComboBoxModel ( openProjects ); 113 projectsComboBox.setModel( projectsModel ); 114 this.selectProject (p); 115 } 116 117 private void selectProject (Project p) { 118 if (p != null) { 119 DefaultComboBoxModel projectsModel = (DefaultComboBoxModel ) projectsComboBox.getModel (); 120 if ( projectsModel.getIndexOf( p ) == -1 ) { 121 projectsModel.insertElementAt( p, 0 ); 122 } 123 projectsComboBox.setSelectedItem( p ); 124 } 125 } 126 127 128 public synchronized void addChangeListener(ChangeListener l) { 129 listeners.add(l); 130 } 131 132 public synchronized void removeChangeListener(ChangeListener l) { 133 listeners.remove(l); 134 } 135 136 private void fireChange() { 137 ChangeEvent e = new ChangeEvent (this); 138 List <ChangeListener > templist; 139 synchronized (this) { 140 templist = new ArrayList <ChangeListener > (listeners); 141 } 142 for (ChangeListener l: templist) { 143 l.stateChanged(e); 144 } 145 } 146 147 148 public Project getProject() { 149 boolean wf; 150 synchronized (this) { 151 wf = isWarmUp; 152 } 153 if (wf) { 154 return this.project; 155 } 156 else { 157 return (Project)projectsComboBox.getSelectedItem(); 158 } 159 } 160 161 public FileObject getTemplate() { 162 return ((TemplatesPanelGUI)this.templatesPanel).getSelectedTemplate (); 163 } 164 165 public void propertyChange(PropertyChangeEvent evt) { 166 fireChange(); 167 } 168 169 170 public java.awt.Dimension getPreferredSize() { 171 return PREF_DIM; 172 } 173 174 public String getCategoryName () { 175 return ((TemplatesPanelGUI)this.templatesPanel).getSelectedCategoryName (); 176 } 177 178 public String getTemplateName () { 179 return ((TemplatesPanelGUI)this.templatesPanel).getSelectedTemplateName (); 180 } 181 182 public void setCategory (String category) { 183 ((TemplatesPanelGUI)this.templatesPanel).setSelectedCategoryByName (category); 184 } 185 186 public void addNotify () { 187 if (firstTime) { 188 Utilities.attachInitJob (this, this); 190 firstTime = false; 191 } 192 super.addNotify (); 193 } 194 195 200 private void initComponents() { 202 java.awt.GridBagConstraints gridBagConstraints; 203 204 jLabel1 = new javax.swing.JLabel (); 205 projectsComboBox = new javax.swing.JComboBox (); 206 templatesPanel = new TemplatesPanelGUI (this.builder); 207 208 setLayout(new java.awt.GridBagLayout ()); 209 210 jLabel1.setLabelFor(projectsComboBox); 211 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(TemplateChooserPanelGUI.class, "LBL_TemplateChooserPanelGUI_jLabel1")); gridBagConstraints = new java.awt.GridBagConstraints (); 213 gridBagConstraints.insets = new java.awt.Insets (0, 0, 13, 0); 214 add(jLabel1, gridBagConstraints); 215 jLabel1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(TemplateChooserPanelGUI.class, "ACSN_jLabel1")); jLabel1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplateChooserPanelGUI.class, "ACSD_jLabel1")); 218 gridBagConstraints = new java.awt.GridBagConstraints (); 219 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 220 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 221 gridBagConstraints.weightx = 1.0; 222 gridBagConstraints.insets = new java.awt.Insets (0, 6, 12, 0); 223 add(projectsComboBox, gridBagConstraints); 224 gridBagConstraints = new java.awt.GridBagConstraints (); 225 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 226 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 227 gridBagConstraints.weightx = 1.0; 228 gridBagConstraints.weighty = 1.0; 229 add(templatesPanel, gridBagConstraints); 230 } 232 233 private javax.swing.JLabel jLabel1; 235 private javax.swing.JComboBox projectsComboBox; 236 private javax.swing.JPanel templatesPanel; 237 239 241 private final class TemplateChildren extends Children.Keys<DataFolder> implements ActionListener { 242 243 private final DataFolder folder; 244 245 TemplateChildren(DataFolder folder) { 246 this.folder = folder; 247 } 248 249 protected void addNotify() { 250 super.addNotify(); 251 projectsComboBox.addActionListener( this ); 252 updateKeys(); 253 } 254 255 protected void removeNotify() { 256 setKeys(Collections.<DataFolder>emptySet()); 257 projectsComboBox.removeActionListener( this ); 258 super.removeNotify(); 259 } 260 261 private void updateKeys() { 262 List <DataFolder> l = new ArrayList <DataFolder>(); 263 for (DataObject d : folder.getChildren()) { 264 FileObject prim = d.getPrimaryFile(); 265 if ( acceptTemplate( d, prim ) ) { 266 if (hasChildren ((Project)projectsComboBox.getSelectedItem (), d)) { 268 l.add((DataFolder) d); 269 } 270 } 271 } 272 setKeys(l); 273 } 274 275 protected Node[] createNodes(DataFolder d) { 276 boolean haveChildren = false; 277 for (DataObject child : d.getChildren()) { 278 if ((child instanceof DataFolder) && !isTemplate(child)) { 279 haveChildren = true; 280 break; 281 } 282 } 283 if (!haveChildren) { 284 return new Node[] {new FilterNode(d.getNodeDelegate(), Children.LEAF )}; 285 } else { 286 return new Node[] {new FilterNode(d.getNodeDelegate(), new TemplateChildren(d))}; 287 } 288 } 289 290 public void actionPerformed (ActionEvent event) { 291 final String cat = getCategoryName (); 292 String template = ((TemplatesPanelGUI)TemplateChooserPanelGUI.this.templatesPanel).getSelectedTemplateName(); 293 this.setKeys(Collections.<DataFolder>emptySet()); 294 this.updateKeys (); 295 setCategory (cat); 296 ((TemplatesPanelGUI)TemplateChooserPanelGUI.this.templatesPanel).setSelectedTemplateByName(template); 297 } 298 299 300 316 317 319 private boolean acceptTemplate( DataObject d, FileObject primaryFile ) { 320 if (d instanceof DataFolder && !isTemplate((DataFolder)d)) { 321 Object o = primaryFile.getAttribute ("simple"); return o == null || Boolean.TRUE.equals (o); 323 } 324 return false; 325 } 326 327 } 328 329 330 private final class FileChildren extends Children.Keys<DataObject> { 331 332 private DataFolder root; 333 334 public FileChildren (DataFolder folder) { 335 this.root = folder; 336 assert this.root != null : "Root can not be null"; } 338 339 protected void addNotify () { 340 this.setKeys (this.root.getChildren()); 341 } 342 343 protected void removeNotify () { 344 this.setKeys (new DataObject[0]); 345 } 346 347 protected Node[] createNodes(DataObject dobj) { 348 if (isTemplate(dobj) && OpenProjectList.isRecommended(getProject(), dobj.getPrimaryFile())) { 349 if (dobj instanceof DataShadow) { 350 dobj = ((DataShadow)dobj).getOriginal(); 351 } 352 return new Node[] { new FilterNode(dobj.getNodeDelegate(), Children.LEAF) }; 353 } else { 354 return new Node[0]; 355 } 356 } 357 358 } 359 360 361 private final class FileChooserBuilder implements TemplatesPanelGUI.Builder { 362 363 public Children createCategoriesChildren(DataFolder folder) { 364 return new TemplateChildren (folder); 365 } 366 367 public Children createTemplatesChildren(DataFolder folder) { 368 return new FileChildren (folder); 369 } 370 371 public void fireChange() { 372 TemplateChooserPanelGUI.this.fireChange(); 373 } 374 375 public String getCategoriesName() { 376 return NbBundle.getMessage (TemplateChooserPanelGUI.class,"CTL_Categories"); 377 } 378 379 public String getTemplatesName() { 380 return NbBundle.getMessage (TemplateChooserPanelGUI.class,"CTL_Files"); 381 } 382 383 } 384 385 private static class ProjectCellRenderer extends JLabel implements ListCellRenderer , UIResource { 387 388 389 public ProjectCellRenderer() { 390 setOpaque(true); 391 } 392 393 394 public Component getListCellRendererComponent( 395 JList list, 396 Object value, 397 int index, 398 boolean isSelected, 399 boolean cellHasFocus) { 400 401 setName("ComboBox.listRenderer"); 404 if ( value instanceof Project ) { 405 ProjectInformation pi = ProjectUtils.getInformation((Project)value); 406 setText(pi.getDisplayName()); 407 setIcon(pi.getIcon()); 408 } 409 else { 410 setText( value == null ? "" : value.toString () ); setIcon( null ); 412 } 413 if ( isSelected ) { 414 setBackground(list.getSelectionBackground()); 415 setForeground(list.getSelectionForeground()); 416 } 417 else { 418 setBackground(list.getBackground()); 419 setForeground(list.getForeground()); 420 } 421 422 return this; 423 } 424 425 public String getName() { 427 String name = super.getName(); 428 return name == null ? "ComboBox.renderer" : name; } 430 431 } 432 433 434 private static boolean isTemplate (DataObject dobj) { 435 if (dobj.isTemplate()) 436 return true; 437 if (dobj instanceof DataShadow) { 438 return ((DataShadow)dobj).getOriginal().isTemplate(); 439 } 440 return false; 441 } 442 443 private boolean hasChildren (Project p, DataObject folder) { 444 if (!(folder instanceof DataFolder)) { 445 return false; 446 } 447 448 DataFolder f = (DataFolder) folder; 449 DataObject[] ch = f.getChildren (); 450 boolean ok = false; 451 for (int i = 0; i < ch.length; i++) { 452 if (isTemplate (ch[i]) && OpenProjectList.isRecommended(p, ch[i].getPrimaryFile ())) { 453 if (!(ch[i] instanceof DataShadow)) { 455 ok = true; 456 break; 457 } 458 } else if (ch[i] instanceof DataFolder && hasChildren (p, ch[i])) { 459 ok = true; 460 break; 461 } 462 } 463 return ok; 464 465 468 } 469 470 public void construct () { 471 this.templatesFolder = Repository.getDefault().getDefaultFileSystem().findResource("Templates"); 472 ((TemplatesPanelGUI)this.templatesPanel).warmUp(this.templatesFolder); 473 } 474 475 public void finished () { 476 Cursor cursor = null; 478 try { 479 Project p; 480 String c,t; 481 synchronized (this) { 482 p = this.project; 483 c = this.category; 484 t = this.template; 485 } 486 cursor = TemplateChooserPanelGUI.this.getCursor(); 487 TemplateChooserPanelGUI.this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 488 initValues( p ); 489 ((TemplatesPanelGUI)this.templatesPanel).doFinished (this.templatesFolder, c, t); 490 } finally { 491 synchronized (this) { 492 isWarmUp = false; 493 } 494 if (cursor != null) { 495 this.setCursor (cursor); 496 } 497 } 498 } 499 500 } 501 | Popular Tags |