001: package org.drools.eclipse.flow.common.editor;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.ObjectInputStream;
024: import java.io.ObjectOutputStream;
025: import java.io.OutputStream;
026: import java.util.EventObject;
027:
028: import org.drools.eclipse.DroolsEclipsePlugin;
029: import org.eclipse.core.resources.IFile;
030: import org.eclipse.core.resources.IWorkspace;
031: import org.eclipse.core.resources.ResourcesPlugin;
032: import org.eclipse.core.runtime.CoreException;
033: import org.eclipse.core.runtime.IPath;
034: import org.eclipse.core.runtime.IProgressMonitor;
035: import org.eclipse.draw2d.PositionConstants;
036: import org.eclipse.gef.ContextMenuProvider;
037: import org.eclipse.gef.DefaultEditDomain;
038: import org.eclipse.gef.EditPartFactory;
039: import org.eclipse.gef.KeyHandler;
040: import org.eclipse.gef.KeyStroke;
041: import org.eclipse.gef.editparts.ScalableRootEditPart;
042: import org.eclipse.gef.editparts.ZoomManager;
043: import org.eclipse.gef.palette.PaletteRoot;
044: import org.eclipse.gef.ui.actions.ActionRegistry;
045: import org.eclipse.gef.ui.actions.AlignmentAction;
046: import org.eclipse.gef.ui.actions.DirectEditAction;
047: import org.eclipse.gef.ui.actions.GEFActionConstants;
048: import org.eclipse.gef.ui.actions.ToggleGridAction;
049: import org.eclipse.gef.ui.parts.GraphicalEditorWithPalette;
050: import org.eclipse.gef.ui.parts.GraphicalViewerKeyHandler;
051: import org.eclipse.jface.action.IAction;
052: import org.eclipse.jface.dialogs.ProgressMonitorDialog;
053: import org.eclipse.swt.SWT;
054: import org.eclipse.ui.IEditorInput;
055: import org.eclipse.ui.IEditorPart;
056: import org.eclipse.ui.IFileEditorInput;
057: import org.eclipse.ui.IWorkbenchPart;
058: import org.eclipse.ui.actions.ActionFactory;
059: import org.eclipse.ui.actions.WorkspaceModifyOperation;
060: import org.eclipse.ui.dialogs.SaveAsDialog;
061: import org.eclipse.ui.part.FileEditorInput;
062: import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
063:
064: /**
065: * Abstract implementation of a graphical editor.
066: *
067: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
068: */
069: public abstract class GenericModelEditor extends
070: GraphicalEditorWithPalette {
071:
072: private Object model;
073: private boolean savePreviouslyNeeded = false;
074: private KeyHandler sharedKeyHandler;
075: private PaletteRoot root;
076: private OverviewOutlinePage overviewOutlinePage;
077:
078: public GenericModelEditor() {
079: setEditDomain(new DefaultEditDomain(this ));
080: }
081:
082: protected void setModel(Object model) {
083: this .model = model;
084: }
085:
086: protected Object getModel() {
087: return model;
088: }
089:
090: protected void createActions() {
091: super .createActions();
092: ActionRegistry registry = getActionRegistry();
093:
094: IAction action = new DirectEditAction((IWorkbenchPart) this );
095: registry.registerAction(action);
096: getSelectionActions().add(action.getId());
097:
098: action = new AlignmentAction((IWorkbenchPart) this ,
099: PositionConstants.LEFT);
100: registry.registerAction(action);
101: getSelectionActions().add(action.getId());
102:
103: action = new AlignmentAction((IWorkbenchPart) this ,
104: PositionConstants.CENTER);
105: registry.registerAction(action);
106: getSelectionActions().add(action.getId());
107:
108: action = new AlignmentAction((IWorkbenchPart) this ,
109: PositionConstants.RIGHT);
110: registry.registerAction(action);
111: getSelectionActions().add(action.getId());
112:
113: action = new AlignmentAction((IWorkbenchPart) this ,
114: PositionConstants.TOP);
115: registry.registerAction(action);
116: getSelectionActions().add(action.getId());
117:
118: action = new AlignmentAction((IWorkbenchPart) this ,
119: PositionConstants.MIDDLE);
120: registry.registerAction(action);
121: getSelectionActions().add(action.getId());
122:
123: action = new AlignmentAction((IWorkbenchPart) this ,
124: PositionConstants.BOTTOM);
125: registry.registerAction(action);
126: getSelectionActions().add(action.getId());
127: }
128:
129: public void commandStackChanged(EventObject event) {
130: if (isDirty()) {
131: if (!savePreviouslyNeeded()) {
132: setSavePreviouslyNeeded(true);
133: firePropertyChange(IEditorPart.PROP_DIRTY);
134: }
135: } else {
136: setSavePreviouslyNeeded(false);
137: firePropertyChange(IEditorPart.PROP_DIRTY);
138: }
139: super .commandStackChanged(event);
140: }
141:
142: protected void createOutputStream(OutputStream os)
143: throws IOException {
144: ObjectOutputStream out = new ObjectOutputStream(os);
145: out.writeObject(model);
146: out.close();
147: }
148:
149: protected void configureGraphicalViewer() {
150: super .configureGraphicalViewer();
151: getGraphicalViewer()
152: .setRootEditPart(new ScalableRootEditPart());
153: getGraphicalViewer()
154: .setEditPartFactory(createEditPartFactory());
155: getGraphicalViewer().setKeyHandler(
156: new GraphicalViewerKeyHandler(getGraphicalViewer())
157: .setParent(getCommonKeyHandler()));
158:
159: IAction showGrid = new ToggleGridAction(getGraphicalViewer());
160: getActionRegistry().registerAction(showGrid);
161:
162: ContextMenuProvider provider = new GenericContextMenuProvider(
163: getGraphicalViewer(), getActionRegistry());
164: getGraphicalViewer().setContextMenu(provider);
165: getSite().registerContextMenu(
166: "org.drools.eclipse.flow.editor.contextmenu", provider,
167: getGraphicalViewer());
168: }
169:
170: protected abstract EditPartFactory createEditPartFactory();
171:
172: protected void initializeGraphicalViewer() {
173: getGraphicalViewer().setContents(model);
174: }
175:
176: public void doSave(IProgressMonitor monitor) {
177: try {
178: ByteArrayOutputStream out = new ByteArrayOutputStream();
179: createOutputStream(out);
180: IFile file = ((IFileEditorInput) getEditorInput())
181: .getFile();
182: file.setContents(
183: new ByteArrayInputStream(out.toByteArray()), true,
184: false, monitor);
185: out.close();
186: getCommandStack().markSaveLocation();
187: } catch (Exception e) {
188: e.printStackTrace();
189: }
190: }
191:
192: public void doSaveAs() {
193: SaveAsDialog dialog = new SaveAsDialog(getSite()
194: .getWorkbenchWindow().getShell());
195: dialog.setOriginalFile(((IFileEditorInput) getEditorInput())
196: .getFile());
197: dialog.open();
198: IPath path = dialog.getResult();
199:
200: if (path == null) {
201: return;
202: }
203:
204: IWorkspace workspace = ResourcesPlugin.getWorkspace();
205: final IFile file = workspace.getRoot().getFile(path);
206:
207: WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
208: public void execute(final IProgressMonitor monitor)
209: throws CoreException {
210: try {
211: ByteArrayOutputStream out = new ByteArrayOutputStream();
212: createOutputStream(out);
213: file.create(new ByteArrayInputStream(out
214: .toByteArray()), true, monitor);
215: out.close();
216: } catch (Exception e) {
217: e.printStackTrace();
218: }
219: }
220: };
221:
222: try {
223: new ProgressMonitorDialog(getSite().getWorkbenchWindow()
224: .getShell()).run(false, true, op);
225: setInput(new FileEditorInput(file));
226: getCommandStack().markSaveLocation();
227: } catch (Exception e) {
228: e.printStackTrace();
229: }
230: }
231:
232: protected KeyHandler getCommonKeyHandler() {
233: if (sharedKeyHandler == null) {
234: sharedKeyHandler = new KeyHandler();
235: sharedKeyHandler.put(KeyStroke.getPressed(SWT.DEL, 127, 0),
236: getActionRegistry().getAction(
237: ActionFactory.DELETE.getId()));
238: sharedKeyHandler.put(KeyStroke.getPressed(SWT.F2, 0),
239: getActionRegistry().getAction(
240: GEFActionConstants.DIRECT_EDIT));
241: }
242: return sharedKeyHandler;
243: }
244:
245: public boolean isDirty() {
246: return isSaveOnCloseNeeded();
247: }
248:
249: public boolean isSaveAsAllowed() {
250: return true;
251: }
252:
253: public boolean isSaveOnCloseNeeded() {
254: return getCommandStack().isDirty();
255: }
256:
257: private boolean savePreviouslyNeeded() {
258: return savePreviouslyNeeded;
259: }
260:
261: private void setSavePreviouslyNeeded(boolean value) {
262: savePreviouslyNeeded = value;
263: }
264:
265: protected PaletteRoot getPaletteRoot() {
266: if (root == null) {
267: root = createPalette();
268: }
269: return root;
270: }
271:
272: protected abstract PaletteRoot createPalette();
273:
274: protected void setInput(IEditorInput input) {
275: super .setInput(input);
276:
277: IFile file = ((IFileEditorInput) input).getFile();
278: setPartName(file.getName());
279: try {
280: InputStream is = file.getContents(false);
281: createInputStream(is);
282: } catch (Exception e) {
283: DroolsEclipsePlugin.log(e);
284: model = createModel();
285: }
286: if (getGraphicalViewer() != null) {
287: initializeGraphicalViewer();
288: }
289: }
290:
291: protected void createInputStream(InputStream is) throws Exception {
292: ObjectInputStream ois = new ObjectInputStreamWithLoader(is,
293: getClass().getClassLoader());
294: model = ois.readObject();
295: ois.close();
296: }
297:
298: protected abstract Object createModel();
299:
300: public Object getAdapter(Class type) {
301: if (type == IContentOutlinePage.class) {
302: return getOverviewOutlinePage();
303: }
304: if (type == ZoomManager.class) {
305: return ((ScalableRootEditPart) getGraphicalViewer()
306: .getRootEditPart()).getZoomManager();
307: }
308: return super .getAdapter(type);
309: }
310:
311: protected OverviewOutlinePage getOverviewOutlinePage() {
312: if (null == overviewOutlinePage && null != getGraphicalViewer()) {
313: ScalableRootEditPart rootEditPart = (ScalableRootEditPart) getGraphicalViewer()
314: .getRootEditPart();
315: overviewOutlinePage = new OverviewOutlinePage(rootEditPart);
316: }
317: return overviewOutlinePage;
318: }
319: }
|