PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/encoder.coco.ui/src/org/netbeans/modules/encoder/coco/ui/wizard/RetrieveCopybookWizardIterator.java

https://bitbucket.org/rsaqc/netbeans-soa
Java | 221 lines | 151 code | 20 blank | 50 comment | 23 complexity | 7cb80c48cbdd73de7b1a07c34e3e9f53 MD5 | raw file
  1. /*
  2. * The contents of this file are subject to the terms of the Common Development
  3. * and Distribution License (the License). You may not use this file except in
  4. * compliance with the License.
  5. *
  6. * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
  7. * or http://www.netbeans.org/cddl.txt.
  8. *
  9. * When distributing Covered Code, include this CDDL Header Notice in each file
  10. * and include the License file at http://www.netbeans.org/cddl.txt.
  11. * If applicable, add the following below the CDDL Header, with the fields
  12. * enclosed by brackets [] replaced by your own identifying information:
  13. * "Portions Copyrighted [year] [name of copyright owner]"
  14. *
  15. * Portions Copyrighted 2007 Sun Microsystems, Inc.
  16. */
  17. package org.netbeans.modules.encoder.coco.ui.wizard;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.OutputStream;
  24. import java.net.URL;
  25. import java.util.HashSet;
  26. import java.util.NoSuchElementException;
  27. import java.util.Set;
  28. import javax.swing.JComponent;
  29. import javax.swing.event.ChangeListener;
  30. import org.netbeans.modules.encoder.coco.ui.CocoEncodingConst;
  31. import org.netbeans.spi.project.ui.templates.support.Templates;
  32. import org.openide.WizardDescriptor;
  33. import org.openide.WizardDescriptor.Panel;
  34. import org.openide.cookies.EditCookie;
  35. import org.openide.filesystems.FileObject;
  36. import org.openide.filesystems.FileUtil;
  37. import org.openide.loaders.DataObject;
  38. import org.openide.loaders.TemplateWizard;
  39. /**
  40. * The wizard iterator for retrieving external COBOL Copybooks into current
  41. * project.
  42. *
  43. * @author Jun Xu
  44. */
  45. public class RetrieveCopybookWizardIterator implements TemplateWizard.Iterator {
  46. private WizardDescriptor.Panel[] mPanels;
  47. private int mIndex;
  48. public RetrieveCopybookWizardIterator() {
  49. }
  50. /**
  51. * Instantiates the template using information provided by the wizard.
  52. * If instantiation fails then wizard remains open to enable correct values.
  53. * @param wiz - the wizard.
  54. * @return set of data objects that have been created (should contain
  55. * at least one).
  56. * @throws java.io.IOException - if the instantiation fails.
  57. */
  58. public Set<DataObject> instantiate(TemplateWizard wiz)
  59. throws IOException {
  60. try {
  61. PropertyValue sourceType =
  62. (PropertyValue) wiz.getProperty(PropertyKey.SOURCE_TYPE);
  63. String sourceLocation =
  64. (String) wiz.getProperty(PropertyKey.SOURCE_LOCATION);
  65. //File targetFolder = new File(
  66. // (String) wiz.getProperty(PropertyKey.TARGET_FOLDER));
  67. boolean overwrite = (Boolean) wiz.getProperty(PropertyKey.OVERWRITE_EXIST);
  68. FileObject dir = Templates.getTargetFolder(wiz);
  69. String targetFileName;
  70. InputStream inStream;
  71. if (PropertyValue.FROM_URL.equals(sourceType)) {
  72. URL sourceURL = new URL(sourceLocation);
  73. targetFileName = new File(sourceURL.getPath()).getName();
  74. inStream = sourceURL.openStream();
  75. } else if (PropertyValue.FROM_FILE.equals(sourceType)) {
  76. File sourceFile = new File(sourceLocation);
  77. targetFileName = sourceFile.getName();
  78. inStream = new FileInputStream(sourceFile);
  79. } else {
  80. throw new IllegalArgumentException(
  81. "Unknown source type: " + sourceType);
  82. }
  83. FileObject fObj = FileObjectUtil.createFileObject(dir,
  84. targetFileName, CocoEncodingConst.DEFAULT_COBOL_EXT, overwrite);
  85. importOneFile(inStream, FileUtil.toFile(fObj));
  86. inStream.close();
  87. DataObject dObj = DataObject.find(fObj);
  88. if (dObj != null) {
  89. // open it in editor
  90. EditCookie edit = (EditCookie) dObj.getCookie(EditCookie.class);
  91. if (edit != null) {
  92. edit.edit();
  93. }
  94. }
  95. Set<DataObject> dObjs = new HashSet<DataObject>(1);
  96. dObjs.add(dObj);
  97. return dObjs;
  98. } catch (IOException e) {
  99. throw new IOException("IOException: " + e.toString());
  100. }
  101. }
  102. public void initialize(TemplateWizard wiz) {
  103. if (mPanels == null) {
  104. mPanels = new WizardDescriptor.Panel[]{
  105. new RetrieveCopybookWizardPanel()};
  106. }
  107. String steps[] = createSteps(wiz);
  108. // int delta = steps.length - mPanels.length;
  109. for (int i = 0; i < mPanels.length; i++) {
  110. if (mPanels[i].getComponent() instanceof JComponent) {
  111. JComponent jc = (JComponent) mPanels[i].getComponent();
  112. //How does this index match the steps? Somehow it just works.
  113. jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i));
  114. // Sets steps names for a panel
  115. jc.putClientProperty("WizardPanel_contentData", steps);
  116. // Turn on subtitle creation on each step
  117. jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE);
  118. // Show steps on the left side with the image on the background
  119. jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE);
  120. // Turn on numbering of all steps
  121. jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE);
  122. }
  123. }
  124. mIndex = 0;
  125. wiz.putProperty(PropertyKey.CURRENT_PROJECT, Templates.getProject(wiz));
  126. wiz.putProperty(PropertyKey.CURRENT_FOLDER, Templates.getTargetFolder(wiz));
  127. }
  128. public void uninitialize(TemplateWizard wiz) {
  129. mPanels = null;
  130. }
  131. @SuppressWarnings("unchecked")
  132. public Panel<WizardDescriptor> current() {
  133. return mPanels[mIndex];
  134. }
  135. public String name() {
  136. return mIndex + 1 + ". from " + mPanels.length;
  137. }
  138. public boolean hasNext() {
  139. return mIndex < mPanels.length - 1;
  140. }
  141. public boolean hasPrevious() {
  142. return mIndex > 0;
  143. }
  144. public void nextPanel() {
  145. if (!hasNext()) {
  146. throw new NoSuchElementException();
  147. }
  148. mIndex++;
  149. }
  150. public void previousPanel() {
  151. if (!hasPrevious()) {
  152. throw new NoSuchElementException();
  153. }
  154. mIndex--;
  155. }
  156. public void addChangeListener(ChangeListener l) {
  157. }
  158. public void removeChangeListener(ChangeListener l) {
  159. }
  160. /*
  161. * This code is a bit odd to be here. If someone does not know how wizard
  162. * framework works internally, how would he/she write the following code?
  163. * And why the last one of the beforeSteps is always "..."?
  164. */
  165. private String[] createSteps(WizardDescriptor wizard) {
  166. String[] beforeSteps = null;
  167. Object prop = wizard.getProperty("WizardPanel_contentData");
  168. if (prop != null && prop instanceof String[]) {
  169. beforeSteps = (String[]) prop;
  170. }
  171. if (beforeSteps == null) {
  172. beforeSteps = new String[0];
  173. }
  174. String[] res = new String[beforeSteps.length - 1 + mPanels.length];
  175. for (int i = 0; i < res.length; i++) {
  176. if (i < beforeSteps.length - 1) {
  177. res[i] = beforeSteps[i];
  178. } else {
  179. res[i] = mPanels[i - beforeSteps.length + 1].getComponent().getName();
  180. }
  181. }
  182. return res;
  183. }
  184. /**
  185. * Imports one file from the source InputStream into targetFile.
  186. * @param in source InputStream object.
  187. * @param targetFile target File.
  188. * @throws java.io.IOException --
  189. */
  190. private void importOneFile(InputStream in, File targetFile)
  191. throws IOException {
  192. byte[] buf = new byte[1024];
  193. int numOfBytesActuallyRead;
  194. OutputStream out = new FileOutputStream(targetFile);
  195. while ((numOfBytesActuallyRead = in.read(buf)) >= 0) {
  196. out.write(buf, 0, numOfBytesActuallyRead);
  197. }
  198. out.flush();
  199. out.close();
  200. }
  201. }