1 19 20 package org.netbeans.modules.project.ui; 21 22 import java.awt.*; 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import javax.swing.event.ChangeEvent ; 27 import javax.swing.event.ChangeListener ; 28 29 import org.openide.ErrorManager; 30 import org.openide.WizardDescriptor; 31 import org.openide.filesystems.FileObject; 32 import org.openide.loaders.DataFolder; 33 import org.openide.loaders.DataObject; 34 import org.openide.loaders.DataObjectNotFoundException; 35 import org.openide.loaders.TemplateWizard; 36 import org.openide.nodes.Children; 37 import org.openide.nodes.FilterNode; 38 import org.openide.nodes.Node; 39 import org.openide.util.*; 40 41 45 public class TemplatesPanel implements WizardDescriptor.Panel { 46 47 private ArrayList <ChangeListener > listeners; 48 private TemplatesPanelGUI panel; 49 private WarmupJob warmUp; 50 private boolean warmUpActive; 51 private boolean needsReselect = false; 53 54 public TemplatesPanel() { 55 } 56 57 public void readSettings (Object settings) { 58 TemplateWizard wd = (TemplateWizard) settings; 59 wd.putProperty ("WizardPanel_contentSelectedIndex", new Integer (0)); wd.putProperty ("WizardPanel_contentData", new String [] { NbBundle.getBundle (TemplatesPanel.class).getString ("LBL_TemplatesPanel_Name"), NbBundle.getBundle (TemplatesPanel.class).getString ("LBL_TemplatesPanel_Dots")}); FileObject templatesFolder = (FileObject) wd.getProperty (TemplatesPanelGUI.TEMPLATES_FOLDER); 64 65 String preselectedCategory = (String )wd.getProperty( "PRESELECT_CATEGORY" ); 67 if ( templatesFolder != null && templatesFolder.isFolder() && 68 ( wd.getTemplate() == null || preselectedCategory != null || needsReselect ) ) { 69 70 String preselectedTemplate = (String )wd.getProperty( "PRESELECT_TEMPLATE" ); 71 String template; 72 String selectedCategory = OpenProjectListSettings.getInstance().getLastSelectedProjectCategory (); 73 String selectedTemplate = OpenProjectListSettings.getInstance().getLastSelectedProjectType (); 74 75 if (preselectedTemplate == null) { 76 template = preselectedCategory != null ? null : selectedTemplate; 77 } else { 78 template = preselectedCategory != null ? preselectedTemplate : selectedTemplate; 79 } 80 81 TemplatesPanelGUI p = (TemplatesPanelGUI) this.getComponent(); 82 if (isWarmUpActive()) { 83 WarmupJob wup = getWarmUp(); 84 wup.setTemplatesFolder (templatesFolder); 85 wup.setSelectedCategory( preselectedCategory != null ? preselectedCategory : selectedCategory ); 86 wup.setSelectedTemplate( template ); 87 } 88 else { 89 p.setTemplatesFolder(templatesFolder); 90 p.setSelectedCategoryByName (preselectedCategory != null ? preselectedCategory : selectedCategory); 91 p.setSelectedTemplateByName (template); 92 } 93 94 } 95 ((WizardDescriptor)settings).putProperty ("NewProjectWizard_Title", null); } 98 99 public void storeSettings (Object settings) { 100 TemplateWizard wd = (TemplateWizard) settings; 101 102 String preselectedCategory = (String )wd.getProperty( "PRESELECT_CATEGORY" ); 104 105 TemplatesPanelGUI gui = (TemplatesPanelGUI)this.getComponent(); 106 FileObject fo = gui.getSelectedTemplate(); 107 if (fo != null) { 108 try { 109 wd.setTemplate (DataObject.find(fo)); 110 } catch (DataObjectNotFoundException e) { 111 ErrorManager.getDefault().notify(e); 112 } 113 } 114 if ( preselectedCategory == null ) { 115 116 String path = gui.getSelectedCategoryName(); 117 if (path != null) { 118 OpenProjectListSettings.getInstance().setLastSelectedProjectCategory(path); 119 } 120 path = gui.getSelectedTemplateName(); 121 if (path != null) { 122 OpenProjectListSettings.getInstance().setLastSelectedProjectType (path); 123 } 124 needsReselect = false; 125 } 126 else { 127 needsReselect = true; 128 } 129 } 130 131 public synchronized void addChangeListener(ChangeListener l) { 132 if (this.listeners == null) { 133 this.listeners = new ArrayList <ChangeListener > (); 134 } 135 this.listeners.add (l); 136 } 137 138 public synchronized void removeChangeListener(ChangeListener l) { 139 if (this.listeners == null) { 140 return; 141 } 142 this.listeners.remove (l); 143 } 144 145 public boolean isValid() { 146 return ((TemplatesPanelGUI)this.getComponent()).getSelectedTemplate() != null; 147 } 148 149 public HelpCtx getHelp() { 150 return new HelpCtx( TemplatesPanel.class ); 151 } 152 153 public synchronized Component getComponent() { 154 if (this.panel == null) { 155 TemplatesPanelGUI.Builder firer = new Builder(); 156 this.panel = new TemplatesPanelGUI (firer); 157 Utilities.attachInitJob (panel, getWarmUp()); 158 this.warmUpActive = true; 159 this.panel.setName (NbBundle.getBundle (TemplatesPanel.class).getString ("LBL_TemplatesPanel_Name")); } 161 return this.panel; 162 } 163 164 165 private synchronized WarmupJob getWarmUp () { 166 if (this.warmUp == null) { 167 this.warmUp = new WarmupJob(); 168 } 169 return this.warmUp; 170 } 171 172 private synchronized boolean isWarmUpActive () { 173 return warmUpActive; 174 } 175 176 private static class CategoriesChildren extends Children.Keys<DataObject> { 177 178 private DataFolder root; 179 180 public CategoriesChildren (DataFolder folder) { 181 this.root = folder; 182 } 183 184 protected void addNotify () { 185 setKeys(root.getChildren()); 186 } 187 188 protected void removeNotify () { 189 this.setKeys(new DataObject[0]); 190 } 191 192 protected Node[] createNodes(DataObject dobj) { 193 if (dobj instanceof DataFolder) { 194 DataFolder folder = (DataFolder) dobj; 195 int type = 0; for (DataObject child : folder.getChildren()) { 197 type = 1; 198 if (child.getPrimaryFile().isFolder()) { 199 type = 2; break; 201 } 202 } 203 if (type == 1) { 204 return new Node[] { 205 new FilterNode(dobj.getNodeDelegate(), Children.LEAF) 206 }; 207 } else if (type == 2) { 208 return new Node[] { 209 new FilterNode(dobj.getNodeDelegate(), new CategoriesChildren((DataFolder)dobj)) 210 }; 211 } 212 } 213 return new Node[0]; 214 } 215 } 216 217 private static class TemplateChildren extends Children.Keys<DataObject> { 218 219 private DataFolder folder; 220 221 public TemplateChildren (DataFolder folder) { 222 this.folder = folder; 223 } 224 225 protected void addNotify () { 226 this.setKeys (this.folder.getChildren ()); 227 } 228 229 protected void removeNotify () { 230 this.setKeys(new DataObject[0]); 231 } 232 233 protected Node[] createNodes(DataObject dobj) { 234 if (dobj.isTemplate()) { 235 return new Node[] { 236 new FilterNode(dobj.getNodeDelegate(), Children.LEAF) 237 }; 238 } else { 239 return new Node[0]; 240 } 241 } 242 243 } 244 245 private class WarmupJob implements AsyncGUIJob { 246 247 private FileObject templatesFolder; 248 private String category; 249 private String template; 250 251 public void construct () { 252 panel.warmUp (this.templatesFolder); 253 } 254 255 public void finished () { 256 Cursor cursor = null; 257 try { 258 cursor = panel.getCursor(); 259 panel.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 260 panel.doFinished (this.templatesFolder, this.category, this.template); 261 } finally { 262 if (cursor != null) { 263 panel.setCursor (cursor); 264 } 265 synchronized(TemplatesPanel.this) { 266 warmUpActive = false; 267 } 268 } 269 } 270 271 void setTemplatesFolder (FileObject fo) { 272 this.templatesFolder = fo; 273 } 274 275 void setSelectedCategory (String s) { 276 this.category = s; 277 } 278 279 void setSelectedTemplate (String s) { 280 this.template = s; 281 } 282 } 283 284 private class Builder implements TemplatesPanelGUI.Builder { 285 286 public org.openide.nodes.Children createCategoriesChildren (DataFolder folder) { 287 assert folder != null : "Folder cannot be null."; return new CategoriesChildren (folder); 289 } 290 291 public org.openide.nodes.Children createTemplatesChildren(DataFolder folder) { 292 return new TemplateChildren (folder); 293 } 294 295 296 public String getCategoriesName() { 297 return NbBundle.getMessage(TemplatesPanel.class,"CTL_Categories"); 298 } 299 300 301 public String getTemplatesName() { 302 return NbBundle.getMessage(TemplatesPanel.class,"CTL_Projects"); 303 } 304 305 public void fireChange() { 306 Iterator it = null; 307 synchronized (this) { 308 if (listeners == null) { 309 return; 310 } 311 it = ((ArrayList )listeners.clone()).iterator(); 312 } 313 ChangeEvent event = new ChangeEvent (this); 314 while (it.hasNext ()) { 315 ((ChangeListener )it.next()).stateChanged(event); 316 } 317 } 318 319 } 320 } 321 | Popular Tags |