001: package newprocess.diagram.part;
002:
003: import java.io.IOException;
004: import java.util.Collections;
005: import java.util.LinkedList;
006: import java.util.List;
007:
008: import newprocess.diagram.edit.parts.ProcessEditPart;
009:
010: import org.eclipse.core.commands.ExecutionException;
011: import org.eclipse.core.commands.operations.OperationHistoryFactory;
012: import org.eclipse.core.resources.IContainer;
013: import org.eclipse.core.resources.IFile;
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IAdaptable;
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.core.runtime.NullProgressMonitor;
018: import org.eclipse.core.runtime.Path;
019: import org.eclipse.emf.common.util.URI;
020: import org.eclipse.emf.ecore.EObject;
021: import org.eclipse.emf.ecore.resource.Resource;
022: import org.eclipse.emf.ecore.resource.ResourceSet;
023: import org.eclipse.emf.ecore.util.FeatureMap;
024: import org.eclipse.emf.edit.provider.IWrapperItemProvider;
025: import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
026: import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
027: import org.eclipse.emf.transaction.TransactionalEditingDomain;
028: import org.eclipse.gmf.runtime.common.core.command.CommandResult;
029: import org.eclipse.gmf.runtime.diagram.core.services.ViewService;
030: import org.eclipse.gmf.runtime.diagram.core.services.view.CreateDiagramViewOperation;
031: import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
032: import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
033: import org.eclipse.gmf.runtime.notation.Diagram;
034: import org.eclipse.jface.viewers.ISelectionChangedListener;
035: import org.eclipse.jface.viewers.IStructuredSelection;
036: import org.eclipse.jface.viewers.SelectionChangedEvent;
037: import org.eclipse.jface.viewers.StructuredSelection;
038: import org.eclipse.jface.viewers.TreeViewer;
039: import org.eclipse.jface.wizard.Wizard;
040: import org.eclipse.jface.wizard.WizardPage;
041: import org.eclipse.swt.SWT;
042: import org.eclipse.swt.layout.GridData;
043: import org.eclipse.swt.layout.GridLayout;
044: import org.eclipse.swt.widgets.Composite;
045: import org.eclipse.swt.widgets.Label;
046: import org.eclipse.ui.IWorkbenchPage;
047: import org.eclipse.ui.PartInitException;
048: import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
049:
050: /**
051: * @generated
052: */
053: public class New_processNewDiagramFileWizard extends Wizard {
054:
055: /**
056: * @generated
057: */
058: private TransactionalEditingDomain myEditingDomain;
059:
060: /**
061: * @generated
062: */
063: private WizardNewFileCreationPage myFileCreationPage;
064:
065: /**
066: * @generated
067: */
068: private IFile mySelectedModelFile;
069:
070: /**
071: * @generated
072: */
073: private IWorkbenchPage myWorkbenchPage;
074:
075: /**
076: * @generated
077: */
078: private IStructuredSelection mySelection;
079:
080: /**
081: * @generated
082: */
083: private EObject myDiagramRoot;
084:
085: /**
086: * @generated
087: */
088: public New_processNewDiagramFileWizard(IFile selectedModelFile,
089: IWorkbenchPage workbenchPage,
090: IStructuredSelection selection, EObject diagramRoot,
091: TransactionalEditingDomain editingDomain) {
092: assert selectedModelFile != null : "Null selectedModelFile in New_processNewDiagramFileWizard constructor"; //$NON-NLS-1$
093: assert workbenchPage != null : "Null workbenchPage in New_processNewDiagramFileWizard constructor"; //$NON-NLS-1$
094: assert selection != null : "Null selection in New_processNewDiagramFileWizard constructor"; //$NON-NLS-1$
095: assert diagramRoot != null : "Null diagramRoot in New_processNewDiagramFileWizard constructor"; //$NON-NLS-1$
096: assert editingDomain != null : "Null editingDomain in New_processNewDiagramFileWizard constructor"; //$NON-NLS-1$
097:
098: mySelectedModelFile = selectedModelFile;
099: myWorkbenchPage = workbenchPage;
100: mySelection = selection;
101: myDiagramRoot = diagramRoot;
102: myEditingDomain = editingDomain;
103: }
104:
105: /**
106: * @generated
107: */
108: public void addPages() {
109: myFileCreationPage = new WizardNewFileCreationPage(
110: "Initialize new Ecore diagram file", mySelection) {
111:
112: public void createControl(Composite parent) {
113: super .createControl(parent);
114:
115: IContainer parentContainer = mySelectedModelFile
116: .getParent();
117: String originalFileName = mySelectedModelFile
118: .getProjectRelativePath().removeFileExtension()
119: .lastSegment();
120: String fileExtension = ".concept_diagram"; //$NON-NLS-1$
121: String fileName = originalFileName + fileExtension;
122: for (int i = 1; parentContainer.getFile(
123: new Path(fileName)).exists(); i++) {
124: fileName = originalFileName + i + fileExtension;
125: }
126: setFileName(fileName);
127: }
128:
129: };
130: myFileCreationPage.setTitle("Diagram file");
131: myFileCreationPage
132: .setDescription("Create new diagram based on "
133: + ProcessEditPart.MODEL_ID + " model content");
134: addPage(myFileCreationPage);
135: addPage(new RootElementSelectorPage());
136: }
137:
138: /**
139: * @generated
140: */
141: public boolean performFinish() {
142: IFile diagramFile = myFileCreationPage.createNewFile();
143: try {
144: diagramFile.setCharset("UTF-8", new NullProgressMonitor()); //$NON-NLS-1$
145: } catch (CoreException e) {
146: New_processDiagramEditorPlugin.getInstance().logError(
147: "Unable to set charset for diagram file", e); //$NON-NLS-1$
148: }
149:
150: ResourceSet resourceSet = myEditingDomain.getResourceSet();
151: final Resource diagramResource = resourceSet.createResource(URI
152: .createPlatformResourceURI(diagramFile.getFullPath()
153: .toString(), true));
154:
155: List affectedFiles = new LinkedList();
156: affectedFiles.add(mySelectedModelFile);
157: affectedFiles.add(diagramFile);
158:
159: AbstractTransactionalCommand command = new AbstractTransactionalCommand(
160: myEditingDomain,
161: "Initializing diagram contents", affectedFiles) { //$NON-NLS-1$
162: protected CommandResult doExecuteWithResult(
163: IProgressMonitor monitor, IAdaptable info)
164: throws ExecutionException {
165: int diagramVID = New_processVisualIDRegistry
166: .getDiagramVisualID(myDiagramRoot);
167: if (diagramVID != ProcessEditPart.VISUAL_ID) {
168: return CommandResult
169: .newErrorCommandResult("Incorrect model object stored as a root resource object"); //$NON-NLS-1$
170: }
171: Diagram diagram = ViewService
172: .createDiagram(
173: myDiagramRoot,
174: ProcessEditPart.MODEL_ID,
175: New_processDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
176: diagramResource.getContents().add(diagram);
177: return CommandResult.newOKCommandResult();
178: }
179: };
180:
181: try {
182: OperationHistoryFactory.getOperationHistory().execute(
183: command, new NullProgressMonitor(), null);
184: diagramResource.save(Collections.EMPTY_MAP);
185: New_processDiagramEditorUtil.openDiagram(diagramResource);
186: } catch (ExecutionException e) {
187: New_processDiagramEditorPlugin.getInstance().logError(
188: "Unable to create model and diagram", e); //$NON-NLS-1$
189: } catch (IOException ex) {
190: New_processDiagramEditorPlugin
191: .getInstance()
192: .logError(
193: "Save operation failed for: " + diagramFile.getFullPath().toString(), ex); //$NON-NLS-1$
194: } catch (PartInitException ex) {
195: New_processDiagramEditorPlugin.getInstance().logError(
196: "Unable to open editor", ex); //$NON-NLS-1$
197: }
198: return true;
199: }
200:
201: /**
202: * @generated
203: */
204: private class RootElementSelectorPage extends WizardPage implements
205: ISelectionChangedListener {
206:
207: /**
208: * @generated
209: */
210: protected RootElementSelectorPage() {
211: super ("Select diagram root element");
212: setTitle("Diagram root element");
213: setDescription("Select semantic model element to be depicted on diagram");
214: }
215:
216: /**
217: * @generated
218: */
219: public void createControl(Composite parent) {
220: initializeDialogUnits(parent);
221: Composite topLevel = new Composite(parent, SWT.NONE);
222: topLevel.setLayout(new GridLayout());
223: topLevel.setLayoutData(new GridData(
224: GridData.VERTICAL_ALIGN_FILL
225: | GridData.HORIZONTAL_ALIGN_FILL));
226: topLevel.setFont(parent.getFont());
227: setControl(topLevel);
228: createModelBrowser(topLevel);
229: setPageComplete(validatePage());
230: }
231:
232: /**
233: * @generated
234: */
235: private void createModelBrowser(Composite parent) {
236: Composite panel = new Composite(parent, SWT.NONE);
237: panel.setLayoutData(new GridData(GridData.FILL_BOTH));
238: GridLayout layout = new GridLayout();
239: layout.marginWidth = 0;
240: panel.setLayout(layout);
241:
242: Label label = new Label(panel, SWT.NONE);
243: label.setText("Select diagram root element:");
244: label.setLayoutData(new GridData(
245: GridData.HORIZONTAL_ALIGN_BEGINNING));
246:
247: TreeViewer treeViewer = new TreeViewer(panel, SWT.SINGLE
248: | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
249: GridData layoutData = new GridData(GridData.FILL_BOTH);
250: layoutData.heightHint = 300;
251: layoutData.widthHint = 300;
252: treeViewer.getTree().setLayoutData(layoutData);
253: treeViewer
254: .setContentProvider(new AdapterFactoryContentProvider(
255: New_processDiagramEditorPlugin
256: .getInstance()
257: .getItemProvidersAdapterFactory()));
258: treeViewer
259: .setLabelProvider(new AdapterFactoryLabelProvider(
260: New_processDiagramEditorPlugin
261: .getInstance()
262: .getItemProvidersAdapterFactory()));
263: treeViewer.setInput(myDiagramRoot.eResource());
264: treeViewer.setSelection(new StructuredSelection(
265: myDiagramRoot));
266: treeViewer.addSelectionChangedListener(this );
267: }
268:
269: /**
270: * @generated
271: */
272: public void selectionChanged(SelectionChangedEvent event) {
273: myDiagramRoot = null;
274: if (event.getSelection() instanceof IStructuredSelection) {
275: IStructuredSelection selection = (IStructuredSelection) event
276: .getSelection();
277: if (selection.size() == 1) {
278: Object selectedElement = selection
279: .getFirstElement();
280: if (selectedElement instanceof IWrapperItemProvider) {
281: selectedElement = ((IWrapperItemProvider) selectedElement)
282: .getValue();
283: }
284: if (selectedElement instanceof FeatureMap.Entry) {
285: selectedElement = ((FeatureMap.Entry) selectedElement)
286: .getValue();
287: }
288: if (selectedElement instanceof EObject) {
289: myDiagramRoot = (EObject) selectedElement;
290: }
291: }
292: }
293: setPageComplete(validatePage());
294: }
295:
296: /**
297: * @generated
298: */
299: private boolean validatePage() {
300: if (myDiagramRoot == null) {
301: setErrorMessage("No diagram root element selected");
302: return false;
303: }
304: boolean result = ViewService
305: .getInstance()
306: .provides(
307: new CreateDiagramViewOperation(
308: new EObjectAdapter(myDiagramRoot),
309: ProcessEditPart.MODEL_ID,
310: New_processDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT));
311: setErrorMessage(result ? null
312: : "Invalid diagram root element was selected");
313: return result;
314: }
315:
316: }
317: }
|