001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.designstudio.editors.actionsequence;
014:
015: import org.eclipse.core.resources.IMarker;
016: import org.eclipse.core.resources.IResourceChangeEvent;
017: import org.eclipse.core.resources.IResourceChangeListener;
018: import org.eclipse.core.resources.ResourcesPlugin;
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.jface.dialogs.ErrorDialog;
021: import org.eclipse.jface.dialogs.MessageDialog;
022: import org.eclipse.swt.widgets.Display;
023: import org.eclipse.ui.IEditorPart;
024: import org.eclipse.ui.IWorkbenchPage;
025: import org.eclipse.ui.PartInitException;
026: import org.eclipse.ui.editors.text.TextEditor;
027: import org.eclipse.ui.forms.editor.FormEditor;
028: import org.eclipse.ui.forms.widgets.FormToolkit;
029: import org.eclipse.ui.ide.IDE;
030: import org.eclipse.ui.part.FileEditorInput;
031: import org.pentaho.designstudio.controls.ActionSequenceFormToolkit;
032: import org.pentaho.designstudio.controls.IXmlModificationListener;
033: import org.pentaho.designstudio.controls.XmlModificationEvent;
034: import org.pentaho.designstudio.editors.actionsequence.pages.HeaderPage;
035: import org.pentaho.designstudio.editors.actionsequence.pages.ProcessPage;
036: import org.pentaho.designstudio.editors.actionsequence.pages.RunPage;
037: import org.pentaho.designstudio.editors.actionsequence.pages.XMLPage;
038: import org.pentaho.designstudio.messages.Messages;
039:
040: /**
041: */
042: public class ActionSequenceEditor extends FormEditor implements
043: IResourceChangeListener, IXmlModificationListener {
044:
045: static final int XML_PAGE_INDEX = 2;
046: ProcessPage actionPage;
047: static final boolean isMacOS = System
048: .getProperty("os.name").startsWith("Mac"); //$NON-NLS-1$ //$NON-NLS-2$
049:
050: /** The text editor used */
051: private XMLPage editor;
052:
053: /**
054: * Creates a multi-page editor
055: */
056: public ActionSequenceEditor() {
057: super ();
058: ResourcesPlugin.getWorkspace().addResourceChangeListener(this );
059: }
060:
061: /**
062: * The <code>MultiPageEditorPart</code> implementation of this
063: * <code>IWorkbenchPart</code> method disposes all nested editors.
064: * Subclasses may extend.
065: */
066: public void dispose() {
067: ResourcesPlugin.getWorkspace().removeResourceChangeListener(
068: this );
069: super .dispose();
070: }
071:
072: /**
073: * Saves the multi-page editor's document.
074: */
075: public void doSave(IProgressMonitor monitor) {
076: debug(
077: Messages.getString("ASMultiPageEditor.DEBUG_MSG_SAVE"), ""); //$NON-NLS-1$ //$NON-NLS-2$
078: editor.doSave(monitor);
079: }
080:
081: public IEditorPart getEditor() {
082: return (editor);
083: }
084:
085: /**
086: * Saves the multi-page editor's document as another file.
087: * Also updates the text for page 0's tab, and updates this multi-page editor's input
088: * to correspond to the nested editor's.
089: */
090: public void doSaveAs() {
091: debug(
092: Messages
093: .getString("ASMultiPageEditor.DEBUG_MSG_SAVE_AS"), ""); //$NON-NLS-1$ //$NON-NLS-2$
094: editor.doSaveAs();
095: setPartName(editor.getEditorInput().getName());
096: setInput(editor.getEditorInput());
097:
098: }
099:
100: /* (non-Javadoc)
101: * Method declared on IEditorPart
102: */
103: public void gotoMarker(IMarker marker) {
104: setActivePage(0);
105: IDE.gotoMarker(editor, marker);
106: }
107:
108: protected void handlePropertyChange(int propertyId) {
109: // TODO Auto-generated method stub
110: super .handlePropertyChange(propertyId);
111: }
112:
113: /* (non-Javadoc)
114: * Method declared on IEditorPart.
115: */
116: public boolean isSaveAsAllowed() {
117: return true;
118: }
119:
120: /**
121: * Closes all project files on project close.
122: */
123: public void resourceChanged(final IResourceChangeEvent event) {
124: debug(
125: Messages
126: .getString("ASMultiPageEditor.DEBUG_MSG_RESOURCE_CHANGED"), event.toString()); //$NON-NLS-1$
127: if (event.getType() == IResourceChangeEvent.PRE_CLOSE) {
128: Display.getDefault().asyncExec(new Runnable() {
129: public void run() {
130: IWorkbenchPage[] pages = getSite()
131: .getWorkbenchWindow().getPages();
132: for (int i = 0; i < pages.length; i++) {
133: if (((FileEditorInput) editor.getEditorInput())
134: .getFile().getProject().equals(
135: event.getResource())) {
136: IEditorPart editorPart = pages[i]
137: .findEditor(editor.getEditorInput());
138: pages[i].closeEditor(editorPart, true);
139: }
140: }
141: }
142: });
143: }
144: }
145:
146: public void contentChanged(XmlModificationEvent event) {
147: editor.getXMLDocumentProvider().setChanged();
148: }
149:
150: protected void addPages() {
151:
152: editor = new XMLPage();
153:
154: try {
155: int pageIndex = addPage(editor, getEditorInput());
156: setPartName(editor.getEditorInput().getName());
157: setPageText(
158: pageIndex,
159: Messages
160: .getString("ASMultiPageEditor.UI_XML_SOURCE_TAB_LABEL")); //$NON-NLS-1$
161: } catch (PartInitException e) {
162: try {
163: int pageIndex = addPage(new TextEditor(),
164: getEditorInput());
165: setPartName(editor.getEditorInput().getName());
166: setPageText(
167: pageIndex,
168: Messages
169: .getString("ASMultiPageEditor.UI_XML_SOURCE_TAB_LABEL")); //$NON-NLS-1$
170: } catch (PartInitException e2) {
171: // TODO Auto-generated catch block
172: e2.printStackTrace();
173: }
174: }
175:
176: ActionSequenceDocumentProvider actionSequenceDocumentProvider = (ActionSequenceDocumentProvider) editor
177: .getXMLDocumentProvider();
178:
179: if (actionSequenceDocumentProvider.getDocument() != null) {
180: try {
181: addPage(0, new HeaderPage(this ,
182: (ActionSequenceDocumentProvider) editor
183: .getXMLDocumentProvider()));
184: actionPage = new ProcessPage(this ,
185: (ActionSequenceDocumentProvider) editor
186: .getXMLDocumentProvider());
187: addPage(1, actionPage);
188: addPage(3, new RunPage(this ,
189: (ActionSequenceDocumentProvider) editor
190: .getXMLDocumentProvider()));
191: } catch (PartInitException e) {
192: ErrorDialog
193: .openError(
194: getSite().getShell(),
195: Messages
196: .getString("ASMultiPageEditor.ERROR_0003_TEXT_EDITOR_CREATION"), null, e.getStatus()); //$NON-NLS-1$
197: }
198: }
199: }
200:
201: protected void pageChange(int newPageIndex) {
202: if (newPageIndex != getCurrentPage()) {
203: if (!editor.checkDocument()) {
204: setActivePage(getCurrentPage());
205: } else {
206: if (newPageIndex == XML_PAGE_INDEX) {
207: editor.refresh();
208: }
209: super .pageChange(newPageIndex);
210: }
211: } else {
212: super .pageChange(newPageIndex);
213: }
214: }
215:
216: void debug(String method, String text) {
217: System.out
218: .println("ASMultiPageEditor: " + method + " - " + text); //$NON-NLS-1$ //$NON-NLS-2$
219: }
220:
221: protected void createPages() {
222: super .createPages();
223: if (actionPage != null) {
224: if (((ActionSequenceDocumentProvider) editor
225: .getXMLDocumentProvider()).getActionSequence()
226: .getExecutableChildren().length > 0) {
227: setActivePage(1);
228: if (!actionPage.checkResourceReferences()) {
229: getContainer().getDisplay().asyncExec(
230: new Runnable() {
231: public void run() {
232: if (MessageDialog
233: .openQuestion(
234: getContainer()
235: .getShell(),
236: getEditorInput()
237: .getName(),
238: Messages
239: .getString("ASMultiPageEditor.UPDATE_FILE_FORMAT"))) { //$NON-NLS-1$
240: actionPage
241: .fixResourceReferences();
242: }
243: }
244: });
245: }
246: }
247: }
248: }
249:
250: protected FormToolkit createToolkit(Display display) {
251: super .createToolkit(display);
252: return new ActionSequenceFormToolkit(display);
253: }
254:
255: }
|