blob: 993daafe95b3271896e22f533f1e4c88f6630e9f [file] [log] [blame]
/*******************************************************************************
* <copyright>
*
* Copyright (c) 2011, 2012 SAP AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* SAP AG - initial API, implementation and documentation
* mwenz - Bug 387971 - Features cant't be invoked from contextMenu
* pjpaulin - Bug 352120 - Changed to instantiate DiagramEditorImpl
* pjpaulin - Bug 352120 - Now uses IDiagramContainerUI interface
*
* </copyright>
*
*******************************************************************************/
package org.eclipse.graphiti.testtool.sketch.editors;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.graphiti.ui.editor.DiagramEditor;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.FileEditorInput;
public class MultiPageEditor extends FormEditor implements IResourceChangeListener, ISelectionListener {
/** The text editor used in page 0. */
private IEditorPart editor;
private TextEditor editor2;
/**
* Creates a multi-page editor example.
*/
public MultiPageEditor() {
super();
ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
}
/**
* Creates page 0 of the multi-page editor,
* which contains a text editor.
*/
void createPage0() {
try {
editor = new DiagramEditor();
int index = addPage(editor, getEditorInput());
setPageText(index, editor.getTitle());
} catch (PartInitException e) {
ErrorDialog.openError(
getSite().getShell(),
"Error creating nested text editor",
null,
e.getStatus());
}
}
/**
* Creates page 1 of the multi-page editor,
* which allows you to change the font used in page 2.
*/
void createPage1() {
try {
editor2 = new TextEditor();
int index = addPage(editor2, getEditorInput());
setPageText(index, editor2.getTitle());
} catch (PartInitException e) {
ErrorDialog.openError(getSite().getShell(), "Error creating nested text editor", null, e.getStatus());
}
}
/**
* Creates the pages of the multi-page editor.
*/
protected void addPages() {
createPage0();
createPage1();
}
/**
* The <code>MultiPageEditorPart</code> implementation of this
* <code>IWorkbenchPart</code> method disposes all nested editors.
* Subclasses may extend.
*/
public void dispose() {
ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
super.dispose();
}
/**
* Saves the multi-page editor's document.
*/
public void doSave(IProgressMonitor monitor) {
getActiveEditor().doSave(monitor);
}
/**
* Saves the multi-page editor's document as another file.
* Also updates the text for page 0's tab, and updates this multi-page editor's input
* to correspond to the nested editor's.
*/
public void doSaveAs() {
IEditorPart editor = getEditor(0);
editor.doSaveAs();
setPageText(0, editor.getTitle());
setInput(editor.getEditorInput());
}
/* (non-Javadoc)
* Method declared on IEditorPart
*/
public void gotoMarker(IMarker marker) {
setActivePage(0);
IDE.gotoMarker(getEditor(0), marker);
}
/**
* The <code>MultiPageEditorExample</code> implementation of this method
* checks that the input is an instance of <code>IFileEditorInput</code>.
*/
public void init(IEditorSite site, IEditorInput editorInput)
throws PartInitException {
if (!(editorInput instanceof IFileEditorInput))
throw new PartInitException("Invalid Input: Must be IFileEditorInput");
super.init(site, editorInput);
// Register this editor to get informed about selection changes (also
// inside editor) to update e.g. the registered actions (see Bug 387971)
getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(this);
}
/* (non-Javadoc)
* Method declared on IEditorPart.
*/
public boolean isSaveAsAllowed() {
return true;
}
/**
* Calculates the contents of page 2 when the it is activated.
*/
protected void pageChange(int newPageIndex) {
super.pageChange(newPageIndex);
}
/**
* Closes all project files on project close.
*/
public void resourceChanged(final IResourceChangeEvent event){
if(event.getType() == IResourceChangeEvent.PRE_CLOSE){
Display.getDefault().asyncExec(new Runnable(){
public void run(){
IWorkbenchPage[] pages = getSite().getWorkbenchWindow().getPages();
for (int i = 0; i<pages.length; i++){
if(((FileEditorInput)editor.getEditorInput()).getFile().getProject().equals(event.getResource())){
IEditorPart editorPart = pages[i].findEditor(editor.getEditorInput());
pages[i].closeEditor(editorPart,true);
}
}
}
});
}
}
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
// Propagate the selection changed event to all sub editors
int pageCount = getPageCount();
for (int i = 0; i < pageCount; i++) {
IEditorPart editor = getEditor(i);
if (editor instanceof ISelectionListener) {
((ISelectionListener) editor).selectionChanged(part, selection);
}
}
}
}