001: /**
002: *
003: */package newprocess.diagram.cust.annotations.wizards;
004:
005: import newprocess.diagram.cust.annotations.commands.WriteCommand;
006: import newprocess.diagram.cust.annotations.utils.URIConvertUtil;
007:
008: import org.eclipse.core.resources.IFile;
009: import org.eclipse.gef.EditPart;
010: import org.eclipse.gmf.runtime.diagram.ui.editpolicies.DecorationEditPolicy;
011: import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
012: import org.eclipse.jface.dialogs.MessageDialog;
013: import org.eclipse.jface.resource.ImageDescriptor;
014: import org.eclipse.jface.viewers.IStructuredSelection;
015: import org.eclipse.swt.widgets.Listener;
016: import org.eclipse.ui.IWorkbench;
017: import org.eclipse.ui.IWorkbenchPage;
018: import org.eclipse.ui.IWorkbenchWindow;
019: import org.eclipse.ui.PartInitException;
020: import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
021: import org.eclipse.ui.ide.IDE;
022: import org.eclipse.ui.plugin.AbstractUIPlugin;
023: import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
024:
025: /**
026: * @author sh
027: *
028: */
029: public abstract class AbstractAnnotationWizard extends
030: BasicNewResourceWizard implements Listener {
031:
032: protected WizardNewFileCreationPage mainPage;
033: protected String title = null;
034: protected String fileName = null;
035: protected String windowTitle = null;
036: protected String plugin = null;
037: protected String icon = null;
038: protected String annotationType = null;
039: protected String projectName = null;
040: protected String path = null;
041:
042: /**
043: * Method declared on IWizard.
044: */
045: public void addPages() {
046: super .addPages();
047: mainPage = new NewFileCreationPage("newFilePage1",
048: getSelection(), annotationType);
049: mainPage.setTitle(title);
050: mainPage.setFileName(fileName);
051: addPage(mainPage);
052: }
053:
054: /**
055: *
056: */
057: @Override
058: public void init(IWorkbench workbench,
059: IStructuredSelection currentSelection) {
060: super .init(workbench, currentSelection);
061: setWindowTitle(windowTitle);
062: setNeedsProgressMonitor(true);
063: }
064:
065: /**
066: * Method declared on BasicNewResourceWizard.
067: */
068: @Override
069: protected void initializeDefaultPageImageDescriptor() {
070: super .initializeDefaultPageImageDescriptor();
071: ImageDescriptor desc = AbstractUIPlugin
072: .imageDescriptorFromPlugin(plugin, icon);
073: setDefaultPageImageDescriptor(desc);
074: getNextPage(mainPage);
075: }
076:
077: /*
078: * Method for the Wizard finish.
079: * Before the external editor opens the file,
080: * a SetValueCommand is executed to write the object.
081: * returns FALSE if no file has been created
082: * returns TRUE if the new file has been created successf
083: */
084: @Override
085: public boolean performFinish() {
086: // creates the file at the given location
087: IFile file = mainPage.createNewFile();
088: if (file == null)
089: return false;
090:
091: // get the relative path for the created file
092: URIConvertUtil uriUtil = new URIConvertUtil();
093: String path = uriUtil.getPathForFile(file, projectName);
094:
095: selectAndReveal(file);
096:
097: // get the selected object
098: IStructuredSelection selection = getSelection();
099: Object object = selection.getFirstElement();
100: if (object == null) {
101: MessageDialog.openError(mainPage.getShell(), "Error",
102: "saving Object failed");
103: return false;
104: }
105:
106: // write input to the model
107: EditPart selectedEditPart = (EditPart) object;
108: boolean ret = writeUri(selectedEditPart, path);
109: if (ret == false)
110: return false;
111:
112: // notify the decorator
113: notifyDecorator(selectedEditPart);
114:
115: // Open editor on new file.
116: openEditor(file);
117: return true;
118: }
119:
120: /**
121: * open the editor on new file
122: */
123: private void openEditor(IFile file) {
124: IWorkbenchWindow dw = getWorkbench().getActiveWorkbenchWindow();
125: try {
126: if (dw != null) {
127: IWorkbenchPage page = dw.getActivePage();
128: if (page != null) {
129: IDE.openEditor(page, file, true);
130: }
131: }
132: } catch (PartInitException e) {
133: MessageDialog.openError(dw.getShell(), "Error",
134: "file could not be opened");
135: }
136: }
137:
138: /**
139: * notify the decorator
140: */
141: private void notifyDecorator(EditPart selectedEditPart) {
142: Object editPolicy = selectedEditPart
143: .getEditPolicy(EditPolicyRoles.DECORATION_ROLE);
144: if (editPolicy instanceof DecorationEditPolicy) {
145: ((DecorationEditPolicy) editPolicy).refresh();
146: }
147: }
148:
149: /**
150: * write uri of the file to the model
151: */
152: private boolean writeUri(EditPart selectedEditPart, String uri) {
153: // write input to the model
154: WriteCommand writeCommand = new WriteCommand();
155: writeCommand.executeWriteCommand(selectedEditPart, uri);
156:
157: /*
158: // check if object stored successfully
159: if(!writeCommand.containsObject(selectedEditPart, uri.toString())) {
160: MessageDialog.openError(mainPage.getShell(), "Error", "saving Object failed");
161: return false;
162: }
163: */
164: return true;
165: }
166: }
|