001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */
024: package com.bostechcorp.cbesb.ui.ide.wizards;
025:
026: import java.io.File;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.util.ArrayList;
030: import java.util.EventListener;
031: import java.util.List;
032:
033: import org.eclipse.core.resources.IFile;
034: import org.eclipse.core.resources.ResourcesPlugin;
035: import org.eclipse.core.runtime.IPath;
036: import org.eclipse.jface.viewers.IStructuredSelection;
037: import org.eclipse.swt.SWT;
038: import org.eclipse.swt.layout.GridData;
039: import org.eclipse.swt.widgets.Button;
040: import org.eclipse.swt.widgets.Combo;
041: import org.eclipse.swt.widgets.Composite;
042: import org.eclipse.swt.widgets.Event;
043: import org.eclipse.swt.widgets.Label;
044: import org.eclipse.ui.IWorkbench;
045: import org.eclipse.ui.IWorkbenchPage;
046: import org.eclipse.ui.IWorkbenchWindow;
047: import org.eclipse.ui.PartInitException;
048: import org.eclipse.ui.ide.IDE;
049:
050: import com.bostechcorp.cbesb.common.i18n.I18N;
051: import com.bostechcorp.cbesb.common.i18n.Messages;
052: import com.bostechcorp.cbesb.common.util.FileUtil;
053: import com.bostechcorp.cbesb.common.util.project.ProjectUtil;
054:
055: public class X12VariantCreationWizardPage extends
056: WizardNewFileCreationPage implements EventListener {
057: protected IWorkbench workbench;
058:
059: // widgets
060: private Button openFileCheckbox;
061:
062: public static Combo versionChoice;
063:
064: private Label label;
065:
066: private IPath projectPath;
067:
068: private IPath rootLoc;
069:
070: static String ext = ".zip";
071:
072: static String start = "v";
073:
074: static String end_letter = "v";
075:
076: static String defaultName = "x12Var";
077:
078: static String extension = "vcx";
079:
080: static String pathOfFolder;
081:
082: static String MESSAGE = "messages";
083:
084: static String SEGMENT = "segments";
085:
086: static String DATATYPE = "datatypes.mdl";
087:
088: static String newVarName = "";
089:
090: static String version = "";
091:
092: // the root path for the zip files
093: public static String base_version_zip_path = null;
094:
095: public String projectName = null;
096:
097: // constants
098: private static int nameCounter = 1;
099:
100: /**
101: * Creates the page for the readme creation wizard.
102: *
103: * @param workbench
104: * the workbench on which the page should be created
105: * @param selection
106: * the current selection
107: */
108: public X12VariantCreationWizardPage(String name,
109: IWorkbench workbench, IStructuredSelection selection) {
110: super (name, selection); //$NON-NLS-1$
111: this .setTitle(I18N.getString(Messages.IDE_NEW_X12FILE));// "Create New
112: // X12
113: // Variant");
114: // //$NON-NLS-1$
115: this .setDescription(I18N
116: .getString(Messages.IDE_NEW_X12FILE_DESC));// "Create
117: // a
118: // new
119: // x12
120: // variant");
121: // //$NON-NLS-1$
122: this .workbench = workbench;
123:
124: /*
125: * super(name, workbench, selection);
126: * this.setTitle(I18N.getString(Messages.IDE_NEW_X12FILE));
127: * this.setDescription(I18N.getString(Messages.IDE_NEW_X12FILE_DESC));
128: * setDefaultName("x12Var"); setExtension("vcx");
129: * setVariant_name("x12"); setZipsource("src/formats/x12");
130: */
131:
132: }
133:
134: /**
135: * The <code>MessageFormatCreationWizardPage</code> implementation of this
136: * <code>WizardNewFileCreationPage</code> method generates sample headings
137: * for sections and subsections in the newly-created Readme file according
138: * to the selections of self's checkbox widgets
139: */
140: protected InputStream getInitialContents() {
141: return null;
142: }
143:
144: /**
145: * (non-Javadoc) Method declared on WizardNewFileCreationPage.
146: */
147: protected String getNewFileLabel() {
148:
149: return I18N.getString(Messages.IDE_X12_FILE_NAME);// "X12 variant
150: // name";
151: // //$NON-NLS-1$
152: }
153:
154: public void handleEvent(Event event) {
155: rootLoc = ResourcesPlugin.getWorkspace().getRoot()
156: .getLocation();
157: pathOfFolder = null;
158:
159: try {
160: projectPath = this .getContainerFullPath();
161: String path1 = projectPath.toString().substring(1,
162: projectPath.toString().length());
163: if (path1.contains("/"))
164: projectName = path1.substring(0, path1.indexOf("/"));
165: else
166: projectName = path1;
167: pathOfFolder = rootLoc.toString()
168: + "/"
169: + projectPath.toString().substring(1,
170: projectPath.toOSString().length());
171:
172: // Clear the Combo
173: versionChoice.removeAll();
174:
175: String dir = pathOfFolder;
176: // set the project name
177:
178: List fileList = listBaseVesionFiles(base_version_zip_path,
179: ext, start);
180: if (!fileList.isEmpty()) {
181: Object[] objectList = fileList.toArray();
182: for (int i = 0; i < objectList.length; i++)
183: versionChoice.add(objectList[i].toString()
184: .substring(
185: 0,
186: objectList[i].toString().indexOf(
187: ".")), i);
188: }
189: versionChoice.setText(versionChoice.getItem(0));
190: version = versionChoice.getText();
191:
192: // newX12Name = getFileName().substring(0,
193: // getFileName().indexOf("."));
194: newVarName = getFileName();
195: } catch (Exception e) {
196: //TODO
197: }
198: setPageComplete(validatePage(this .getContainerFullPath()
199: .toString(), "src/formats/x12"));
200:
201: }
202:
203: // List all the available version within a certain project
204: public List listBaseVesionFiles(String d, String e, String s) {
205:
206: File dir = new File(d);
207:
208: String[] list = dir.list();
209: List finalList = new ArrayList();
210:
211: if (list.length == 0)
212: return finalList;
213:
214: for (int i = 0; i < list.length; i++) {
215: String fileName = list[i].toString();
216: if (fileName.endsWith(e) && fileName.startsWith(s)) {
217:
218: finalList.add(list[i]);
219: }
220: }
221: return finalList;
222: }
223:
224: /**
225: * (non-Javadoc) Method declared on IDialogPage.
226: */
227: public void createControl(Composite parent) {
228: // inherit default container and name specification widgets
229: super .createControl(parent);
230: Composite composite = (Composite) getControl();
231:
232: this .setFileName(defaultName, extension); //$NON-NLS-1$ //$NON-NLS-2$
233:
234: // open file for editing checkbox
235: openFileCheckbox = new Button(composite, SWT.CHECK);
236: openFileCheckbox.setText(I18N
237: .getString(Messages.IDE_OPEN_FILE_EDITOR));// "Open
238: // file
239: // for
240: // editing
241: // when
242: // done");
243: // //$NON-NLS-1$
244: openFileCheckbox.setSelection(true);
245:
246: label = new Label(composite, SWT.NONE);
247: final GridData gridData = new GridData();
248: gridData.horizontalSpan = 5;
249: label.setLayoutData(gridData);
250: label.setText(I18N.getString(Messages.IDE_VER_CHOOSE));
251:
252: // Combo for MDL version choice
253: versionChoice = new Combo(composite, SWT.READ_ONLY);
254: versionChoice.setLayoutData(new GridData(
255: GridData.HORIZONTAL_ALIGN_FILL));
256:
257: // Add listener to Combo
258:
259: rootLoc = ResourcesPlugin.getWorkspace().getRoot()
260: .getLocation();
261: pathOfFolder = null;
262:
263: try {
264: projectPath = this .getContainerFullPath();
265:
266: String path1 = projectPath.toString().substring(1,
267: projectPath.toString().length());
268: if (path1.contains("/"))
269: projectName = path1.substring(0, path1.indexOf("/"));
270: else
271: projectName = path1;
272: pathOfFolder = rootLoc.toString()
273: + "/"
274: + projectPath.toString().substring(1,
275: projectPath.toOSString().length());
276:
277: // Clear the Combo
278: versionChoice.removeAll();
279: String dir = pathOfFolder;
280: String current_project_name = null;
281: // fetch the current project name
282:
283: String split = projectPath.toString().substring(
284: projectPath.toString().indexOf("/") + 1,
285: projectPath.toString().length());
286: if (split.contains("/"))
287: current_project_name = split.substring(0, split
288: .indexOf("/"));
289:
290: else
291: current_project_name = split;
292: base_version_zip_path = ProjectUtil
293: .getX12ZipPathForUI(rootLoc.toString() + "/"
294: + current_project_name);
295:
296: List fileList = listBaseVesionFiles(base_version_zip_path,
297: ext, start);
298: if (!fileList.isEmpty()) {
299: Object[] objectList = fileList.toArray();
300: for (int i = 0; i < objectList.length; i++)
301: versionChoice.add(objectList[i].toString()
302: .substring(
303: 0,
304: objectList[i].toString().indexOf(
305: ".")), i);
306: }
307: versionChoice.setText(versionChoice.getItem(0));
308: version = versionChoice.getText();
309:
310: // newX12Name = getFileName().substring(0,
311: // getFileName().indexOf("."));
312: newVarName = getFileName();
313: } catch (Exception ev) {
314: //TODO
315: }
316:
317: setPageComplete((validatePage(this .getContainerFullPath()
318: .toString(), "src/formats/x12")));
319:
320: }
321:
322: /**
323: * Creates a new file resource as requested by the user. If everything is OK
324: * then answer true. If not, false will cause the dialog to stay open.
325: *
326: * @return whether creation was successful
327: * @see MessageFormatCreationWizard#performFinish()
328: */
329: public boolean finish() {
330: // create the new file resource
331: if (versionChoice.getText().equals("")) {
332: return false;
333:
334: }
335:
336: IFile newFile = createNewFile(extension);
337: if (newFile == null)
338: return false; // ie.- creation was unsuccessful
339:
340: // Since the file resource was created fine, open it for editing
341: // if requested by the user
342: try {
343: if (openFileCheckbox.getSelection()) {
344: IWorkbenchWindow dwindow = workbench
345: .getActiveWorkbenchWindow();
346: IWorkbenchPage page = dwindow.getActivePage();
347: if (page != null) {
348: IDE.openEditor(page, newFile, true);
349: }
350: }
351: } catch (PartInitException e) {
352: e.printStackTrace();
353: return false;
354: }
355: nameCounter++;
356: return true;
357: }
358:
359: // After creating a .vcx file, we need to create some other folders.
360: public void dispose() {
361:
362: // Unzip
363: File file1 = new File(pathOfFolder + "/" + start + "_"
364: + versionChoice.getText() + ext);
365: File file2 = new File(pathOfFolder);
366: try {
367: File file = FileUtil.unpackArchive(file1, file2);
368:
369: } catch (IOException e) {
370: e.printStackTrace();
371: }
372: // Create a folder for this x12 variant
373: String newVariantPath = pathOfFolder + "/"
374: + versionChoice.getText();
375: boolean success = (new File(newVariantPath
376: + "/"
377: + getFileName()
378: .substring(0, getFileName().indexOf(".")))
379: .mkdir());
380: if (!success)
381: return;
382: // Move the files
383:
384: // File (or directory) to be moved
385: File message = new File(newVariantPath + "/" + MESSAGE);
386: File segment = new File(newVariantPath + "/" + SEGMENT);
387: File datatype = new File(newVariantPath + "/" + DATATYPE);
388: // Destination directory
389: File dir_message = new File(newVariantPath
390: + "/"
391: + getFileName()
392: .substring(0, getFileName().indexOf(".")));
393: File dir_segment = new File(newVariantPath
394: + "/"
395: + getFileName()
396: .substring(0, getFileName().indexOf(".")));
397: File dir_datatype = new File(newVariantPath
398: + "/"
399: + getFileName()
400: .substring(0, getFileName().indexOf(".")));
401: // Move file to new directory
402: boolean success1 = message.renameTo(new File(dir_message,
403: message.getName()));
404: boolean success2 = segment.renameTo(new File(dir_segment,
405: segment.getName()));
406: boolean success3 = datatype.renameTo(new File(dir_datatype,
407: datatype.getName()));
408:
409: }
410:
411: public String getProjectName() {
412: return projectName;
413: }
414:
415: public void setProjectName(String projectName) {
416: this.projectName = projectName;
417: }
418:
419: }
|