1 19 20 package org.netbeans.modules.project.ui; 21 22 import java.awt.Component ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import javax.swing.event.ChangeEvent ; 28 import javax.swing.event.ChangeListener ; 29 import org.netbeans.api.project.Project; 30 import org.netbeans.api.project.SourceGroup; 31 import org.netbeans.spi.project.ui.templates.support.Templates; 32 import org.openide.WizardDescriptor; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.util.HelpCtx; 36 import org.openide.util.NbBundle; 37 38 42 final class SimpleTargetChooserPanel implements WizardDescriptor.Panel, ChangeListener { 43 44 private final List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 45 private SimpleTargetChooserPanelGUI gui; 46 47 private Project project; 48 private SourceGroup[] folders; 49 private WizardDescriptor.Panel bottomPanel; 50 private WizardDescriptor wizard; 51 private boolean isFolder; 52 53 SimpleTargetChooserPanel( Project project, SourceGroup[] folders, WizardDescriptor.Panel bottomPanel, boolean isFolder ) { 54 this.folders = folders; 55 this.project = project; 56 this.bottomPanel = bottomPanel; 57 if ( bottomPanel != null ) { 58 bottomPanel.addChangeListener( this ); 59 } 60 this.isFolder = isFolder; 61 this.gui = null; 62 } 63 64 public Component getComponent() { 65 if (gui == null) { 66 gui = new SimpleTargetChooserPanelGUI( project, folders, bottomPanel == null ? null : bottomPanel.getComponent(), isFolder ); 67 gui.addChangeListener(this); 68 } 69 return gui; 70 } 71 72 public HelpCtx getHelp() { 73 if ( bottomPanel != null ) { 74 HelpCtx bottomHelp = bottomPanel.getHelp(); 75 if ( bottomHelp != null ) { 76 return bottomHelp; 77 } 78 } 79 80 return null; 82 83 } 84 85 public boolean isValid() { 86 boolean ok = ( gui != null && gui.getTargetName() != null && 87 ( bottomPanel == null || bottomPanel.isValid() ) ); 88 89 if (!ok) { 90 return false; 91 } 92 93 FileObject template = Templates.getTemplate( wizard ); 95 96 String errorMessage = ProjectUtilities.canUseFileName (gui.getTargetGroup().getRootFolder(), gui.getTargetFolder(), gui.getTargetName(), template.getExt (), isFolder); 97 wizard.putProperty ("WizardPanel_errorMessage", errorMessage); 99 return errorMessage == null; 100 } 101 102 public synchronized void addChangeListener(ChangeListener l) { 103 listeners.add(l); 104 } 105 106 public synchronized void removeChangeListener(ChangeListener l) { 107 listeners.remove(l); 108 } 109 110 private void fireChange() { 111 ChangeEvent e = new ChangeEvent (this); 112 List <ChangeListener > templist; 113 synchronized (this) { 114 templist = new ArrayList <ChangeListener > (listeners); 115 } 116 for (ChangeListener l: templist) { 117 l.stateChanged(e); 118 } 119 } 120 121 public void readSettings( Object settings ) { 122 123 wizard = (WizardDescriptor)settings; 124 125 if ( gui == null ) { 126 getComponent(); 127 } 128 129 FileObject preselectedTarget = Templates.getTargetFolder( wizard ); 131 String targetName = Templates.getTargetName( wizard ); 133 gui.initValues( Templates.getTemplate( wizard ), preselectedTarget, targetName ); 135 136 Object substitute = gui.getClientProperty ("NewFileWizard_Title"); if (substitute != null) { 140 wizard.putProperty ("NewFileWizard_Title", substitute); } 142 143 wizard.putProperty ("WizardPanel_contentData", new String [] { NbBundle.getBundle (SimpleTargetChooserPanel.class).getString ("LBL_TemplatesPanel_Name"), NbBundle.getBundle (SimpleTargetChooserPanel.class).getString ("LBL_SimpleTargetChooserPanel_Name")}); 147 if ( bottomPanel != null ) { 148 bottomPanel.readSettings( settings ); 149 } 150 } 151 152 public void storeSettings(Object settings) { 153 if ( WizardDescriptor.PREVIOUS_OPTION.equals( ((WizardDescriptor)settings).getValue() ) ) { 154 return; 155 } 156 if( isValid() ) { 157 if ( bottomPanel != null ) { 158 bottomPanel.storeSettings( settings ); 159 } 160 161 FileObject template = Templates.getTemplate( wizard ); 162 163 String name = gui.getTargetName (); 164 if (name.indexOf ('/') > 0) { name = name.substring (name.lastIndexOf ('/') + 1); 166 } 167 168 Templates.setTargetFolder( (WizardDescriptor)settings, getTargetFolderFromGUI () ); 169 Templates.setTargetName( (WizardDescriptor)settings, name ); 170 } 171 ((WizardDescriptor)settings).putProperty ("NewFileWizard_Title", null); } 173 174 public void stateChanged(ChangeEvent e) { 175 fireChange(); 176 } 177 178 private FileObject getTargetFolderFromGUI () { 179 FileObject rootFolder = gui.getTargetGroup().getRootFolder(); 180 String folderName = gui.getTargetFolder(); 181 String newObject = gui.getTargetName (); 182 183 if (newObject.indexOf ('/') > 0) { String path = newObject.substring (0, newObject.lastIndexOf ('/')); folderName = folderName == null || "".equals (folderName) ? path : folderName + '/' + path; } 187 188 FileObject targetFolder; 189 if ( folderName == null ) { 190 targetFolder = rootFolder; 191 } 192 else { 193 targetFolder = rootFolder.getFileObject( folderName ); 194 } 195 196 if ( targetFolder == null ) { 197 try { 199 targetFolder = FileUtil.createFolder( rootFolder, folderName ); 200 } catch (IOException ioe) { 201 } 204 } 205 206 return targetFolder; 207 } 208 } 209 | Popular Tags |