001: package com.bostechcorp.cbesb.ui.variant.wizards;
002:
003: import java.io.FileOutputStream;
004: import java.util.Vector;
005:
006: import org.eclipse.core.runtime.CoreException;
007: import org.eclipse.jface.viewers.IStructuredSelection;
008: import org.eclipse.jface.wizard.Wizard;
009: import org.eclipse.swt.SWT;
010: import org.eclipse.swt.widgets.MessageBox;
011: import org.eclipse.swt.widgets.TreeItem;
012: import org.eclipse.ui.INewWizard;
013: import org.eclipse.ui.IWorkbench;
014: import org.eclipse.ui.IWorkbenchWizard;
015: import org.eclipse.ui.PlatformUI;
016:
017: import com.bostechcorp.cbesb.common.i18n.I18N;
018: import com.bostechcorp.cbesb.common.i18n.Message;
019: import com.bostechcorp.cbesb.common.i18n.Messages;
020: import com.bostechcorp.cbesb.common.mdl.IMDLDocument;
021: import com.bostechcorp.cbesb.common.mdl.IMessageDefinition;
022: import com.bostechcorp.cbesb.common.mdl.util.FormatDefUtil;
023: import com.bostechcorp.cbesb.ui.baseformat.editor.models.ModelFactory;
024: import com.bostechcorp.cbesb.ui.util.MsgUtil;
025: import com.bostechcorp.cbesb.ui.util.ProjectUtil;
026: import com.bostechcorp.cbesb.ui.variant.editor.Activator;
027: import com.bostechcorp.cbesb.ui.variant.editor.actions.Analyze;
028: import com.bostechcorp.cbesb.ui.variant.editor.editors.X12Editor;
029: import com.bostechcorp.cbesb.ui.variant.utils.FileChange;
030:
031: public class VariantNewWizard extends Wizard implements INewWizard {
032:
033: private IStructuredSelection selection;
034:
035: private IWorkbench workbench;
036:
037: private VariantNewWizardMainPage mainPage;
038:
039: private String wizardTitle = "";
040:
041: private String pageTitle = "";
042:
043: private String pageDescription = "";
044:
045: protected X12Editor editor;
046:
047: protected String var_type = "x12";
048:
049: public TreeItem clipboard = null;
050:
051: protected boolean message = true;
052:
053: protected String path = "";
054:
055: protected boolean copy = true;
056:
057: public VariantNewWizard(X12Editor editor, String wizardTitle,
058: String pageTitle, String pageDescription, String var_type,
059: boolean message, boolean copy, String path) {
060: super ();
061: this .editor = editor;
062: this .pageTitle = pageTitle;
063: this .pageDescription = pageDescription;
064: this .wizardTitle = wizardTitle;
065: this .var_type = var_type;
066: this .message = message;
067: this .copy = copy;
068: this .path = path;
069: }
070:
071: /**
072: * Adding the page to the wizard.
073: */
074: public void addPages() {
075: mainPage = new VariantNewWizardMainPage(workbench, selection,
076: pageTitle, pageDescription);
077: addPage(mainPage);
078: }
079:
080: /**
081: * We will accept the selection in the workbench to see if we can initialize
082: * from it.
083: *
084: * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
085: */
086: public void init(IWorkbench workbench,
087: IStructuredSelection selection) {
088: this .workbench = workbench;
089: this .selection = selection;
090: setWindowTitle(wizardTitle);
091: setDefaultPageImageDescriptor(Activator
092: .imageDescriptorFromPlugin(Activator.PLUGIN_ID,
093: "icons/full/wizban/openMessageFormatView.gif"));
094: }
095:
096: /**
097: * (non-Javadoc) Method declared on IWizard
098: */
099: public boolean performFinish() {
100: try {
101: doFinish();
102: } catch (Exception ex) {
103: ex.printStackTrace();
104: MsgUtil.warningMsg(ex.toString());
105:
106: }
107: return true;//thiredPage.finish();
108: }
109:
110: private void doFinish() throws CoreException {
111: String file_name = mainPage.getFilename();
112:
113: if (message)
114: creatMDLFile(file_name);
115: else
116: creatMDLFileSeg(file_name);
117: }
118:
119: private void creatMDLFile(String filename) {
120: if (filename.equals(""))
121: filename = "Muntitled.mdl";
122: if ((filename.endsWith(".mdl")) || (filename.endsWith(".MDL")))
123: filename = filename.substring(0, filename.indexOf("."));
124: Analyze analyzeLeft = (Analyze) ((X12Editor) editor)
125: .getAnalyzeLeft();
126: String mes_path = analyzeLeft.getMes_path();
127: String filepath = mes_path + "\\" + filename + ".mdl";
128:
129: // get the vector for the segments mdl files
130: Vector vector_mes = analyzeLeft.getMes_mdl();
131:
132: for (int i = 0; i < vector_mes.size(); i++) {
133: // If the new file already existed in the old vector
134: if (((TreeItem) vector_mes.get(i)).getText()
135: .equalsIgnoreCase(filename + ".mdl")) {
136: int open = MsgUtil.confirmMsg(I18N
137: .getString(Messages.OVERWRITE));
138: if (open == SWT.OK)
139: analyzeLeft.deleteFile(filepath);
140: else
141: return;
142: }
143: }
144:
145: analyzeLeft.addintoMes_mdl(filename);
146: analyzeLeft.createFile(filepath);
147: if (copy) {
148: IMDLDocument mdlDoc = FormatDefUtil
149: .getMdlDocFromFormatSpec("x12", path);
150: IMessageDefinition[] md = mdlDoc.getAllMessageDefinitions();
151: md[0].setName(filename);
152: //mdlDoc.setTargetNamespace(filename);
153:
154: try {
155: FileOutputStream fileOutputStream = new FileOutputStream(
156: filepath);
157: mdlDoc.serialize(fileOutputStream);
158: } catch (Exception ex) {
159: ex.printStackTrace();
160: MsgUtil.warningMsg(ex.toString());
161: return;
162: }
163: } else {
164: analyzeLeft.writeMDLHeader(filepath, filename, true);
165: }
166: ((X12Editor) editor).varMDLFile = true;
167: ((X12Editor) editor).executeOpenMDL(analyzeLeft.getNewItem());
168: ((X12Editor) editor).getLeftPanel().layout();
169: ((X12Editor) editor).currentMDLFile = ModelFactory.TYPE_VARIANT_MES;
170: ((X12Editor) editor).setBasefilename(filename + ".mdl");
171: Vector vec = ((X12Editor) editor).getAnalyze().getMes_mdl();
172: int index = vec.size() - 1;
173: ((X12Editor) editor).getLeftTree().setSelection(
174: (TreeItem) vec.get(index));
175: ((X12Editor) editor).writeIntoTaggedList();
176: ProjectUtil.refreshProject(((X12Editor) editor)
177: .getProjectName());
178: }
179:
180: public void creatMDLFileSeg(String filename) {
181: if (filename.equals(""))
182: filename = "untitled.mdl";
183: if ((filename.endsWith(".mdl")) || (filename.endsWith(".MDL")))
184: filename = filename.substring(0, filename.indexOf("."));
185: //filename = filename.toUpperCase();
186: Analyze analyzeLeft = (Analyze) ((X12Editor) editor)
187: .getAnalyzeLeft();
188: String seg_path = analyzeLeft.getSeg_path();
189: String filepath = seg_path + "\\" + filename + ".mdl";
190:
191: // get the vector for the segments mdl files
192: Vector vector_seg = analyzeLeft.getSeg_mdl();
193:
194: for (int i = 0; i < vector_seg.size(); i++) {
195: // If the new file already existed in the old vector
196: if (((TreeItem) vector_seg.get(i)).getText()
197: .equalsIgnoreCase(filename + ".mdl")) {
198: MessageBox dlg = new MessageBox(PlatformUI
199: .getWorkbench().getActiveWorkbenchWindow()
200: .getShell(), SWT.ICON_QUESTION | SWT.YES
201: | SWT.NO);
202: dlg.setMessage(new Message(Messages.OVERWRITE)
203: .getMessage());
204:
205: int open = dlg.open();
206: switch (open) {
207: case SWT.YES:
208: analyzeLeft.deleteFile(filepath);
209: break;
210: case SWT.NO:
211: return;
212:
213: }
214: break;
215: }
216: }
217:
218: analyzeLeft.addintoSeg_mdl(filename);
219: analyzeLeft.createFile(filepath);
220:
221: if (copy) {
222: IMDLDocument mdlDoc = FormatDefUtil
223: .getMdlDocFromFormatSpec("x12", path);
224:
225: IMDLDocument new_mdlDoc = FileChange.copyChangeDoc(mdlDoc,
226: filename, "x12");
227: try {
228: FileOutputStream fileOutputStream = new FileOutputStream(
229: filepath);
230: new_mdlDoc.serialize(fileOutputStream);
231: } catch (Exception ex) {
232: ex.printStackTrace();
233: }
234: } else {
235: analyzeLeft.writeMDLHeader(filepath, filename, false);
236: }
237:
238: ((X12Editor) editor).varMDLFile = true;
239: ((X12Editor) editor).executeOpenMDL(analyzeLeft.getNewItem());
240: ((X12Editor) editor).getLeftPanel().layout();
241: ((X12Editor) editor).currentMDLFile = ModelFactory.TYPE_VARIANT_SEG;
242: ((X12Editor) editor).setBasefilename(filename + ".mdl");
243: Vector vec = ((X12Editor) editor).getAnalyze().getSeg_mdl();
244: int index = vec.size() - 1;
245: ((X12Editor) editor).getLeftTree().setSelection(
246: ((TreeItem) vec.get(index)));
247:
248: ((X12Editor) editor).writeIntoTaggedList();
249: ProjectUtil.refreshProject(((X12Editor) editor)
250: .getProjectName());
251: }
252:
253: public void setClipboard(TreeItem clipboard) {
254: this .clipboard = clipboard;
255: }
256:
257: public TreeItem getClipboard() {
258: return this.clipboard;
259: }
260: }
|