1 19 20 package org.netbeans.modules.xml.schema.ui.retriever; 21 22 import java.awt.Component ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.net.URI ; 26 import java.net.URISyntaxException ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Collections ; 30 import java.util.List ; 31 import java.util.NoSuchElementException ; 32 import java.util.Set ; 33 import javax.swing.JComponent ; 34 import javax.swing.event.ChangeListener ; 35 import org.netbeans.api.project.Project; 36 import org.netbeans.api.project.ProjectUtils; 37 import org.netbeans.api.project.SourceGroup; 38 import org.netbeans.api.project.Sources; 39 import org.netbeans.modules.xml.retriever.catalog.Utilities.DocumentTypesEnum; 40 import org.netbeans.modules.xml.retriever.IConstants; 41 import org.netbeans.modules.xml.retriever.ImportDirectory; 42 import org.netbeans.modules.xml.retriever.RetrieveEntry; 43 import org.netbeans.modules.xml.retriever.RetrieverEngine; 44 import org.netbeans.modules.xml.xam.ui.ProjectConstants; 45 import org.netbeans.spi.project.ui.templates.support.Templates; 46 import org.openide.WizardDescriptor; 47 import org.openide.filesystems.FileObject; 48 import org.openide.filesystems.FileUtil; 49 import org.openide.loaders.DataFolder; 50 import org.openide.loaders.TemplateWizard; 51 import org.openide.util.NbBundle; 52 53 54 public final class RetrieveXMLResourceWizardIterator implements TemplateWizard.Iterator { 55 56 private static final long serialVersionUID = 1L; 57 private int index; 58 59 private WizardDescriptor wizard; 60 private WizardDescriptor.Panel[] panels; 61 62 66 private WizardDescriptor.Panel[] getPanels() { 67 if (panels == null) { 68 panels = new WizardDescriptor.Panel[] { 69 new RetrieveXMLResourceWizardPanel1() 70 }; 71 String [] steps = createSteps(); 72 for (int i = 0; i < panels.length; i++) { 73 Component c = panels[i].getComponent(); 74 if (steps[i] == null) { 75 steps[i] = c.getName(); 79 } 80 if (c instanceof JComponent ) { JComponent jc = (JComponent ) c; 82 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); 84 jc.putClientProperty("WizardPanel_contentData", steps); 86 jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); 88 jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE); 90 jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE); 92 } 93 } 94 } 95 return panels; 96 } 97 98 public Set instantiate(TemplateWizard wizard) throws IOException { 99 100 RetrieveXMLResourceVisualPanel1.SourceType srcType = (RetrieveXMLResourceVisualPanel1.SourceType) wizard.getProperty(IConstants.SOURCE_LOCATION_TYPE_KEY); 101 if(srcType == RetrieveXMLResourceVisualPanel1.SourceType.LOCAL_FILE){ 102 return instantiateLocalFile(); 103 } 104 if(srcType == RetrieveXMLResourceVisualPanel1.SourceType.URL_ADDR){ 105 return instantiateURL(); 106 } 107 return Collections.singleton(wizard.getTargetFolder()); 108 } 109 110 private Set instantiateLocalFile() throws IOException { 111 URI getFileURI = null; 112 try { 113 getFileURI = new URI ((String ) wizard.getProperty(IConstants.SOURCE_LOCATION_KEY)); 114 } catch (URISyntaxException ex) { 115 throw new IOException (ex.getMessage()); 116 } 117 File storedFile = (File ) wizard.getProperty(IConstants.TARGET_FILE_KEY); 118 boolean overwriteFiles = ((Boolean )wizard.getProperty(IConstants.OVERWRITE_FILES)).booleanValue(); 119 new ImportDirectory(new File (getFileURI), storedFile, overwriteFiles); 120 if (storedFile == null) { 121 storedFile = new File (System.getProperty("user.dir")); 123 } 124 FileObject fobj = FileUtil.toFileObject(FileUtil.normalizeFile(storedFile)); 127 if(fobj == null) 128 return Collections.EMPTY_SET; 129 return Collections.singleton(fobj); 130 } 131 132 private Set instantiateURL() throws IOException { 133 File saveRootFile = (File ) wizard.getProperty(IConstants.TARGET_FILE_KEY); 134 String sourceURL = (String ) wizard.getProperty(IConstants.SOURCE_LOCATION_KEY); 135 File selectedSaveRootFolder = (File ) wizard.getProperty(IConstants.USER_SELECTED_SAVE_ROOT); 136 boolean overwriteFiles = ((Boolean )wizard.getProperty(IConstants.OVERWRITE_FILES)).booleanValue(); 137 RetrieverEngine instance = new RetrieverEngine(selectedSaveRootFolder); 138 RetrieveEntry rent = null; 139 if(((Boolean ) wizard.getProperty(IConstants.RETRIVE_CLOSURE_KEY)).booleanValue()) 140 rent = new RetrieveEntry(null, sourceURL, null, null, DocumentTypesEnum.schema, true); 141 else 142 rent = new RetrieveEntry(null, sourceURL, null, saveRootFile, DocumentTypesEnum.schema, false); 143 instance.addResourceToRetrieve(rent); 144 instance.setFileOverwrite(overwriteFiles); 145 instance.start(); 146 if (saveRootFile == null) { 147 saveRootFile = new File (System.getProperty("user.dir")); 149 } 150 FileObject fobj = FileUtil.toFileObject(FileUtil.normalizeFile(saveRootFile)); 153 if(fobj == null) 154 return Collections.EMPTY_SET; 155 return Collections.singleton(fobj); 156 } 157 158 public void initialize(TemplateWizard wizard) { 159 160 Project currentProject = Templates.getProject(wizard); 162 Sources sources = ProjectUtils.getSources(currentProject); 163 List <SourceGroup> roots = new ArrayList <SourceGroup>(); 164 SourceGroup[] javaRoots = 165 sources.getSourceGroups(ProjectConstants.JAVA_SOURCES_TYPE); 166 roots.addAll(Arrays.asList(javaRoots)); 167 if (roots.isEmpty()) { 168 SourceGroup[] sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC); 169 roots.addAll(Arrays.asList(sourceGroups)); 170 } 171 DataFolder folder = DataFolder.findFolder(roots.get(0).getRootFolder()); 172 DataFolder projectFolder = 173 DataFolder.findFolder(currentProject.getProjectDirectory()); 174 try { 175 if (wizard.getTargetFolder().equals(projectFolder)) { 176 wizard.setTargetFolder(folder); 177 } 178 } catch (IOException ioe) { 179 wizard.setTargetFolder(folder); 180 } 181 182 wizard.putProperty(IConstants.THIS_WIZARD_ITERATOR_KEY, this); 183 wizard.putProperty(IConstants.CURRENT_PROJECT_KEY, currentProject); 184 FileObject currentFolder = Templates.getTargetFolder(wizard); 185 wizard.putProperty(IConstants.CURRENT_FOLDER_KEY, currentFolder); 186 this.wizard = wizard; 187 } 188 189 public void uninitialize(TemplateWizard wizard) { 190 panels = null; 193 } 194 195 public WizardDescriptor.Panel current() { 196 return getPanels()[index]; 197 } 198 199 public String name() { 200 return index + 1 + ". from " + getPanels().length; 201 } 202 203 public boolean hasNext() { 204 return index < getPanels().length - 1; 205 } 206 207 public boolean hasPrevious() { 208 return index > 0; 209 } 210 211 public void nextPanel() { 212 if (!hasNext()) { 213 throw new NoSuchElementException (); 214 } 215 index++; 216 } 217 218 public void previousPanel() { 219 if (!hasPrevious()) { 220 throw new NoSuchElementException (); 221 } 222 index--; 223 } 224 225 public void addChangeListener(ChangeListener l) {} 227 public void removeChangeListener(ChangeListener l) {} 228 229 259 260 private String [] createSteps() { 265 String [] beforeSteps = null; 266 Object prop = wizard.getProperty("WizardPanel_contentData"); 267 if (prop != null && prop instanceof String []) { 268 beforeSteps = (String []) prop; 269 } 270 271 if (beforeSteps == null) { 272 beforeSteps = new String [0]; 273 } 274 275 String [] res = new String [(beforeSteps.length - 1) + panels.length]; 276 for (int i = 0; i < res.length; i++) { 277 if (i < (beforeSteps.length - 1)) { 278 res[i] = beforeSteps[i]; 279 } else { 280 res[i] = panels[i - beforeSteps.length + 1].getComponent().getName(); 281 } 282 } 283 return res; 284 } 285 286 } 287 288 | Popular Tags |