001: package org.openwfe.gpe.ui;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.IOException;
006: import java.io.InputStream;
007: import java.io.ObjectInputStream;
008: import java.io.ObjectOutputStream;
009: import java.io.OutputStream;
010: import java.util.EventObject;
011:
012: import org.eclipse.swt.SWT;
013:
014: import org.eclipse.core.resources.IFile;
015: import org.eclipse.core.resources.IMarker;
016: import org.eclipse.core.resources.IWorkspace;
017: import org.eclipse.core.resources.ResourcesPlugin;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.IProgressMonitor;
021: import org.eclipse.jface.action.IAction;
022: import org.eclipse.jface.dialogs.ProgressMonitorDialog;
023: import org.eclipse.ui.IEditorInput;
024: import org.eclipse.ui.IEditorPart;
025: import org.eclipse.ui.IFileEditorInput;
026: import org.eclipse.ui.IWorkbenchPart;
027: import org.eclipse.ui.actions.WorkspaceModifyOperation;
028: import org.eclipse.ui.dialogs.SaveAsDialog;
029: import org.eclipse.ui.part.FileEditorInput;
030: import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
031:
032: import org.eclipse.gef.ContextMenuProvider;
033: import org.eclipse.gef.DefaultEditDomain;
034: import org.eclipse.gef.EditPart;
035: import org.eclipse.gef.KeyHandler;
036: import org.eclipse.gef.KeyStroke;
037: import org.eclipse.gef.commands.CommandStack;
038: import org.eclipse.gef.dnd.TemplateTransferDragSourceListener;
039: import org.eclipse.gef.editparts.ScalableRootEditPart;
040: import org.eclipse.gef.palette.PaletteRoot;
041: import org.eclipse.gef.tools.ConnectionCreationTool;
042: import org.eclipse.gef.ui.actions.ActionRegistry;
043: import org.eclipse.gef.ui.actions.DirectEditAction;
044: import org.eclipse.gef.ui.actions.GEFActionConstants;
045: import org.eclipse.gef.ui.parts.GraphicalEditorWithPalette;
046: import org.eclipse.gef.ui.parts.GraphicalViewerKeyHandler;
047:
048: import org.openwfe.gpe.FlowEditorPaletteFactory;
049: import org.openwfe.gpe.actions.FlowContextMenuProvider;
050: import org.openwfe.gpe.model.ActivityDiagram;
051: import org.openwfe.gpe.parts.FlowElementPartFactory;
052:
053: /**
054: *
055: * @author helena
056: * This class creates the new editor window with the palette and the outline
057: */
058: public class FlowEditor extends GraphicalEditorWithPalette {
059:
060: ActivityDiagram diagram;
061: private PaletteRoot root;
062: private KeyHandler sharedKeyHandler;
063: private boolean savePreviouslyNeeded = false;
064: CommandStack stack = null;
065: EditPart viewer = null;
066: ActionRegistry registry = null;
067:
068: public FlowEditor() {
069: DefaultEditDomain defaultEditDomain = new DefaultEditDomain(
070: this );
071: defaultEditDomain.setActiveTool(new ConnectionCreationTool());
072: setEditDomain(defaultEditDomain);
073: }
074:
075: /**
076: * @see org.eclipse.gef.commands.CommandStackListener#commandStackChanged(java.util.EventObject)
077: */
078: public void commandStackChanged(EventObject event) {
079: if (isDirty()) {
080: if (!savePreviouslyNeeded()) {
081: setSavePreviouslyNeeded(true);
082: firePropertyChange(IEditorPart.PROP_DIRTY);
083: }
084: } else {
085: setSavePreviouslyNeeded(false);
086: firePropertyChange(IEditorPart.PROP_DIRTY);
087: }
088: super .commandStackChanged(event);
089: }
090:
091: /**
092: * @see org.eclipse.gef.ui.parts.GraphicalEditor#createActions()
093: */
094: protected void createActions() {
095: super .createActions();
096: ActionRegistry registry = getActionRegistry();
097: IAction action;
098:
099: action = new DirectEditAction((IWorkbenchPart) this );
100: registry.registerAction(action);
101: getSelectionActions().add(action.getId());
102: }
103:
104: /**
105: * Creates an appropriate output stream and writes the activity diagram out to this stream.
106: * @param os the base output stream
107: * @throws IOException
108: */
109: protected void createOutputStream(OutputStream os)
110: throws IOException {
111: ObjectOutputStream out = new ObjectOutputStream(os);
112: out.writeObject(diagram);
113: out.close();
114: }
115:
116: /**
117: * @see org.eclipse.gef.ui.parts.GraphicalEditor#configureGraphicalViewer()
118: */
119: protected void configureGraphicalViewer() {
120: super .configureGraphicalViewer();
121: getGraphicalViewer()
122: .setRootEditPart(new ScalableRootEditPart());
123: getGraphicalViewer().setEditPartFactory(
124: new FlowElementPartFactory());
125: getGraphicalViewer().setKeyHandler(
126: new GraphicalViewerKeyHandler(getGraphicalViewer())
127: .setParent(getCommonKeyHandler()));
128:
129: ContextMenuProvider provider = new FlowContextMenuProvider(
130: getGraphicalViewer(), getActionRegistry());
131: getGraphicalViewer().setContextMenu(provider);
132: getSite().registerContextMenu(
133: "org.openwfe.gpe.editor.contextmenu", //$NON-NLS-1$
134: provider, getGraphicalViewer());
135:
136: }
137:
138: /**
139: * @see org.eclipse.gef.ui.parts.GraphicalEditor#initializeGraphicalViewer()
140: */
141: protected void initializeGraphicalViewer() {
142: getGraphicalViewer().setContents(diagram);
143: //getGraphicalViewer().addDropTargetListener(
144: //new FlowTemplateTransferDropTargetListener(getGraphicalViewer()));
145:
146: }
147:
148: /**
149: * @see org.eclipse.gef.ui.parts.GraphicalEditorWithPalette#initializePaletteViewer()
150: */
151: protected void initializePaletteViewer() {
152: super .initializePaletteViewer();
153: getPaletteViewer().addDragSourceListener(
154: new TemplateTransferDragSourceListener(
155: getPaletteViewer()));
156: }
157:
158: /**
159: * @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor)
160: */
161: public void doSave(IProgressMonitor monitor) {
162: try {
163: ByteArrayOutputStream out = new ByteArrayOutputStream();
164: createOutputStream(out);
165: IFile file = ((IFileEditorInput) getEditorInput())
166: .getFile();
167: file.setContents(
168: new ByteArrayInputStream(out.toByteArray()), true,
169: false, monitor);
170: out.close();
171: getCommandStack().markSaveLocation();
172: } catch (Exception e) {
173: e.printStackTrace();
174: }
175: }
176:
177: /**
178: * @see org.eclipse.ui.ISaveablePart#doSaveAs()
179: */
180: public void doSaveAs() {
181: SaveAsDialog dialog = new SaveAsDialog(getSite()
182: .getWorkbenchWindow().getShell());
183: dialog.setOriginalFile(((IFileEditorInput) getEditorInput())
184: .getFile());
185: dialog.open();
186: IPath path = dialog.getResult();
187:
188: if (path == null)
189: return;
190:
191: IWorkspace workspace = ResourcesPlugin.getWorkspace();
192: final IFile file = workspace.getRoot().getFile(path);
193:
194: WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
195: public void execute(final IProgressMonitor monitor)
196: throws CoreException {
197: try {
198: ByteArrayOutputStream out = new ByteArrayOutputStream();
199: createOutputStream(out);
200: file.create(new ByteArrayInputStream(out
201: .toByteArray()), true, monitor);
202: out.close();
203: } catch (Exception e) {
204: e.printStackTrace();
205: }
206: }
207: };
208:
209: try {
210: new ProgressMonitorDialog(getSite().getWorkbenchWindow()
211: .getShell()).run(false, true, op);
212: setInput(new FileEditorInput((IFile) file));
213: getCommandStack().markSaveLocation();
214: } catch (Exception e) {
215: e.printStackTrace();
216: }
217: }
218:
219: protected KeyHandler getCommonKeyHandler() {
220: if (sharedKeyHandler == null) {
221: sharedKeyHandler = new KeyHandler();
222: sharedKeyHandler.put(KeyStroke.getPressed(SWT.DEL, 127, 0),
223: getActionRegistry().getAction(
224: GEFActionConstants.DELETE));
225: sharedKeyHandler.put(KeyStroke.getPressed(SWT.F2, 0),
226: getActionRegistry().getAction(
227: GEFActionConstants.DIRECT_EDIT));
228: }
229: return sharedKeyHandler;
230: }
231:
232: /**
233: * @see org.eclipse.gef.ui.parts.GraphicalEditorWithPalette#getPaletteRoot()
234: */
235: protected PaletteRoot getPaletteRoot() {
236: if (root == null)
237: root = FlowEditorPaletteFactory.createPalette();
238: return root;
239: }
240:
241: public void gotoMarker(IMarker marker) {
242: }
243:
244: /**
245: * @see org.eclipse.ui.ISaveablePart#isDirty()
246: */
247: public boolean isDirty() {
248: return isSaveOnCloseNeeded();
249: }
250:
251: /**
252: * @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
253: */
254: public boolean isSaveAsAllowed() {
255: return true;
256: }
257:
258: /**
259: * @see org.eclipse.ui.ISaveablePart#isSaveOnCloseNeeded()
260: */
261: public boolean isSaveOnCloseNeeded() {
262: return getCommandStack().isDirty();
263: }
264:
265: private boolean savePreviouslyNeeded() {
266: return savePreviouslyNeeded;
267: }
268:
269: /**
270: * @see org.eclipse.ui.part.EditorPart#setInput(org.eclipse.ui.IEditorInput)
271: */
272: protected void setInput(IEditorInput input) {
273: super .setInput(input);
274:
275: IFile file = ((IFileEditorInput) input).getFile();
276: try {
277: InputStream is = file.getContents(false);
278: ObjectInputStream ois = new ObjectInputStream(is);
279: diagram = (ActivityDiagram) ois.readObject();
280: ois.close();
281: } catch (Exception e) {
282: //This is just an example. All exceptions caught here.
283: e.printStackTrace();
284: diagram = new ActivityDiagram();
285: }
286: }
287:
288: private void setSavePreviouslyNeeded(boolean value) {
289: savePreviouslyNeeded = value;
290: }
291:
292: public DiagramOutlinePage createContentOutlinePage() {
293: if (getGraphicalViewer() != null
294: && getGraphicalViewer().getContents() != null
295: && getActionRegistry() != null) {
296: DiagramOutlinePage page = new DiagramOutlinePage(
297: getCommandStack(), getGraphicalViewer()
298: .getContents(), getActionRegistry());
299: return page;
300: }
301: return null;
302: }
303:
304: public Object getAdapter(Class aClass) {
305: if (aClass == CommandStack.class) {
306: return getCommandStack();
307: } else if (aClass == IContentOutlinePage.class) {
308: return createContentOutlinePage();
309: }
310: return super .getAdapter(aClass);
311: }
312:
313: /**
314: * @return Returns the registry.
315: */
316: public ActionRegistry getRegistry() {
317: return registry;
318: }
319:
320: /**
321: * @return Returns the stack.
322: */
323: public CommandStack getStack() {
324: return stack;
325: }
326:
327: /**
328: * @return Returns the viewer.
329: */
330: public EditPart getViewer() {
331: return viewer;
332: }
333: }
|