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