001: package org.drools.eclipse.editors;
002:
003: /*
004: * Copyright 2006 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.lang.reflect.InvocationTargetException;
020:
021: import org.drools.eclipse.DroolsEclipsePlugin;
022: import org.drools.eclipse.editors.rete.ReteViewer;
023: import org.drools.eclipse.editors.rete.model.ReteGraph;
024: import org.eclipse.core.runtime.IProgressMonitor;
025: import org.eclipse.core.runtime.IStatus;
026: import org.eclipse.core.runtime.Status;
027: import org.eclipse.gef.editparts.ZoomManager;
028: import org.eclipse.gef.ui.actions.ZoomComboContributionItem;
029: import org.eclipse.jface.dialogs.ErrorDialog;
030: import org.eclipse.jface.dialogs.MessageDialog;
031: import org.eclipse.jface.operation.IRunnableWithProgress;
032: import org.eclipse.jface.text.DocumentEvent;
033: import org.eclipse.jface.text.IDocumentListener;
034: import org.eclipse.ui.IEditorInput;
035: import org.eclipse.ui.IEditorSite;
036: import org.eclipse.ui.PartInitException;
037: import org.eclipse.ui.forms.editor.FormEditor;
038:
039: /**
040: * This is a multi table editor wrapper for both the text editor and the RETE
041: * viewer.
042: *
043: * @author Kris
044: * @author Ahti Kitsik
045: */
046: public class DRLRuleEditor2 extends FormEditor {
047:
048: private DRLRuleEditor textEditor;
049:
050: private ReteViewer reteViewer;
051:
052: private ZoomComboContributionItem zitem;
053:
054: private ZoomInAction2 zoomIn;
055: private ZoomOutAction2 zoomOut;
056:
057: protected ReteGraph graph;
058:
059: /* (non-Javadoc)
060: * @see org.eclipse.ui.forms.editor.FormEditor#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
061: */
062: public void init(IEditorSite site, IEditorInput input)
063: throws PartInitException {
064: super .init(site, input);
065: setPartName(input.getName());
066: }
067:
068: /**
069: * Adds Text Editor for rules and Rete graph viewer
070: *
071: * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
072: */
073: protected void addPages() {
074: try {
075: textEditor = new DRLRuleEditor() {
076: public void close(boolean save) {
077: super .close(save);
078: DRLRuleEditor2.this .close(save);
079: }
080:
081: protected void setPartName(String partName) {
082: super .setPartName(partName);
083: DRLRuleEditor2.this .setPartName(partName);
084: }
085: };
086:
087: reteViewer = new ReteViewer(textEditor);
088:
089: int text = addPage(textEditor, getEditorInput());
090:
091: int rete = addPage(reteViewer, getEditorInput());
092:
093: setPageText(text, "Text Editor");
094: setPageText(rete, "Rete Tree");
095:
096: textEditor.getDocumentProvider().getDocument(
097: getEditorInput()).addDocumentListener(
098: new IDocumentListener() {
099:
100: public void documentAboutToBeChanged(
101: DocumentEvent event) {
102: }
103:
104: public void documentChanged(DocumentEvent event) {
105: reteViewer.fireDocumentChanged();
106: }
107:
108: });
109:
110: } catch (PartInitException e) {
111: DroolsEclipsePlugin.log(e);
112: }
113: }
114:
115: /* (non-Javadoc)
116: * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
117: */
118: public void doSave(IProgressMonitor monitor) {
119: textEditor.doSave(monitor);
120: setInput(getEditorInput());
121: }
122:
123: /* (non-Javadoc)
124: * @see org.eclipse.ui.part.EditorPart#doSaveAs()
125: */
126: public void doSaveAs() {
127: textEditor.doSaveAs();
128: }
129:
130: /* (non-Javadoc)
131: * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
132: */
133: public boolean isSaveAsAllowed() {
134: return textEditor.isSaveAsAllowed();
135: }
136:
137: /* (non-Javadoc)
138: * @see org.eclipse.ui.part.MultiPageEditorPart#getAdapter(java.lang.Class)
139: */
140: public Object getAdapter(Class adapter) {
141: if (adapter == ZoomManager.class) {
142:
143: if (getActiveEditor() instanceof ReteViewer) {
144: return reteViewer.getAdapter(adapter);
145: } else if (getActiveEditor() instanceof DRLRuleEditor) {
146: return null;
147: }
148:
149: } else if (adapter == ZoomInAction2.class) {
150: return zoomIn;
151: } else if (adapter == ZoomOutAction2.class) {
152: return zoomOut;
153: } else if (adapter == ZoomComboContributionItem.class) {
154: return zitem;
155: }
156:
157: return textEditor.getAdapter(adapter);
158: }
159:
160: /**
161: * Updates ZoomManagers for contributed actions.
162: */
163: private void updateZoomItems() {
164: updateZoomIn();
165:
166: updateZoomOut();
167:
168: updateZoomCombo();
169:
170: }
171:
172: private void updateZoomCombo() {
173: if (zitem != null) {
174: zitem.setZoomManager(getZoomManager());
175: }
176: }
177:
178: private void updateZoomIn() {
179: boolean zoomActive = getZoomManager() != null;
180: if (zoomIn != null) {
181: zoomIn.setZoomManager(getZoomManager());
182: zoomIn.setEnabled(zoomActive);
183: }
184: }
185:
186: private void updateZoomOut() {
187: boolean zoomActive = getZoomManager() != null;
188: if (zoomOut != null) {
189: zoomOut.setZoomManager(getZoomManager());
190: zoomOut.setEnabled(zoomActive);
191: }
192: }
193:
194: /**
195: * Sets ZoomComboContributionItem to be used for updating it's
196: * ZoomManager when multipage tab is switched.
197: *
198: * @param zitem contribution item
199: */
200: public void setZoomComboContributionItem(
201: ZoomComboContributionItem zitem) {
202: this .zitem = zitem;
203: updateZoomCombo();
204: }
205:
206: private ZoomManager getZoomManager() {
207: return (ZoomManager) getAdapter(ZoomManager.class);
208: }
209:
210: /**
211: * Sets ZoomOutAction2 to be used for updating it's
212: * ZoomManager when multipage tab is switched.
213: *
214: * @param zoomOutAction zoom action
215: */
216: public void setZoomOutAction(ZoomOutAction2 zoomOutAction) {
217: this .zoomOut = zoomOutAction;
218: updateZoomOut();
219: }
220:
221: /**
222: * Sets ZoomInAction to be used for updating it's
223: * ZoomManager when multipage tab is switched.
224: * @param zoomInAction zoom action
225: */
226: public void setZoomInAction(ZoomInAction2 zoomInAction) {
227: this .zoomIn = zoomInAction;
228: updateZoomIn();
229: }
230:
231: public void setFocus() {
232: if (getActivePage() == 1) {
233: boolean reteFailed = false;
234: graph = null;
235: try {
236: final String contents = textEditor
237: .getDocumentProvider().getDocument(
238: getEditorInput()).get();
239: final IRunnableWithProgress runnable = new IRunnableWithProgress() {
240:
241: public void run(IProgressMonitor monitor)
242: throws InvocationTargetException,
243: InterruptedException {
244: try {
245: graph = reteViewer.loadReteModel(monitor,
246: contents);
247: } catch (Throwable e) {
248: if (e instanceof InvocationTargetException) {
249: throw (InvocationTargetException) e;
250: } else if (e instanceof InterruptedException) {
251: throw (InterruptedException) e;
252: }
253: throw new InvocationTargetException(e);
254: }
255:
256: }
257:
258: };
259:
260: getEditorSite().getWorkbenchWindow().getWorkbench()
261: .getProgressService().busyCursorWhile(runnable);
262:
263: reteViewer.drawGraph(graph);
264:
265: } catch (InvocationTargetException e) {
266: handleError(e);
267: reteFailed = true;
268: } catch (InterruptedException e) {
269: MessageDialog.openError(getSite().getShell(),
270: "Rete Tree Error!",
271: "Rete Tree Calculation Cancelled!");
272: reteFailed = true;
273: } catch (Throwable t) {
274: handleError(t);
275: reteFailed = true;
276: }
277: if (reteFailed) {
278: setActivePage(0);
279: }
280: }
281:
282: super .setFocus();
283: updateZoomItems();
284:
285: }
286:
287: private void handleError(Throwable t) {
288: DroolsEclipsePlugin.log(t);
289: Throwable cause = t.getCause();
290: if (cause == null) {
291: cause = t;
292: }
293: String message = cause.getClass().getName() + ": "
294: + cause.getMessage();
295: if (message == null || message.length() == 0) {
296: message = "Uncategorized Error!";
297: }
298: IStatus status = new Status(IStatus.ERROR, DroolsEclipsePlugin
299: .getUniqueIdentifier(), -1, message, null);
300: ErrorDialog.openError(getSite().getShell(),
301: "Rete Tree Build Error!", "Rete Tree Build Error!",
302: status);
303:
304: }
305:
306: /**
307: * Increasing visibility to allow switching tabs by page index
308: */
309: public void setActivePage(int pageIndex) {
310: super.setActivePage(pageIndex);
311: }
312:
313: }
|