View Javadoc

1   package org.eparapher.rcp.editors;
2   
3   import org.eclipse.core.resources.IMarker;
4   import org.eclipse.core.resources.IResourceChangeEvent;
5   import org.eclipse.core.resources.IResourceChangeListener;
6   import org.eclipse.core.resources.ResourcesPlugin;
7   import org.eclipse.core.runtime.IProgressMonitor;
8   import org.eclipse.jface.dialogs.ErrorDialog;
9   import org.eclipse.swt.graphics.Font;
10  import org.eclipse.swt.widgets.Composite;
11  import org.eclipse.swt.widgets.Display;
12  import org.eclipse.ui.IEditorInput;
13  import org.eclipse.ui.IEditorPart;
14  import org.eclipse.ui.IEditorSite;
15  import org.eclipse.ui.IFileEditorInput;
16  import org.eclipse.ui.IPathEditorInput;
17  import org.eclipse.ui.IWorkbenchPage;
18  import org.eclipse.ui.PartInitException;
19  import org.eclipse.ui.editors.text.TextEditor;
20  import org.eclipse.ui.forms.widgets.Form;
21  import org.eclipse.ui.ide.IDE;
22  import org.eclipse.ui.part.MultiPageEditorPart;
23  /**
24   * A basic editor that use a multi-page editor.
25   * This editor has 2 pages:
26   * <ul>
27   * <li>page 0 contains a nested text editor.
28   * <li>page 1 allows you to view signature information.
29   * </ul>
30   */
31  public class BasicMultiPageEditor extends MultiPageEditorPart implements IResourceChangeListener{
32  
33  	/** The text editor used in page 0. */
34  	private TextEditor editor;
35  
36  	/** The font chosen in page 1. */
37  	private Font font;
38  	
39  	SignatureTabEditor ftesignature;
40  	
41  	/**
42  	 * Creates a multi-page editor example.
43  	 */
44  	public BasicMultiPageEditor() {
45  		super();
46  		ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
47  	}
48  
49  	/**
50  	 * Creates page 0 of the multi-page editor,
51  	 * which contains a text editor.
52  	 */
53  	void createEditableTab() {
54  		try {
55  			editor = new SimpleEditor();
56  			int index = addPage(editor, getEditorInput());
57  			setPageText(index, "File Content");
58  		} catch (PartInitException e) {
59  			ErrorDialog.openError(
60  				getSite().getShell(),
61  				"Error creating nested text editor",
62  				null,
63  				e.getStatus());
64  		}
65  	}
66  	/**
67  	 * Creates page 1 of the multi-page editor,
68  	 * which allows you to change the font used in page 2.
69  	 */
70  	void createSignatureTab() {
71  		ftesignature = new SignatureTabEditor();
72  		Composite composite = ftesignature.createSignatureForm(getContainer());
73  			
74  		int index = addPage(composite);
75  		setPageText(index, "Signature info");
76  	}
77  	
78  	/**
79  	 * Creates the pages of the multi-page editor.
80  	 */
81  	protected void createPages() {
82  		createEditableTab();
83  		createSignatureTab();
84  		//setTitle("toto");
85  	}
86  	/**
87  	 * The <code>MultiPageEditorPart</code> implementation of this 
88  	 * <code>IWorkbenchPart</code> method disposes all nested editors.
89  	 * Subclasses may extend.
90  	 */
91  	public void dispose() {
92  		ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
93  		ftesignature.dispose();
94  		super.dispose();
95  	}
96  	/**
97  	 * Saves the multi-page editor's document.
98  	 */
99  	public void doSave(IProgressMonitor monitor) {
100 		getEditor(0).doSave(monitor);
101 	}
102 	/**
103 	 * Saves the multi-page editor's document as another file.
104 	 * Also updates the text for page 0's tab, and updates this multi-page editor's input
105 	 * to correspond to the nested editor's.
106 	 */
107 	public void doSaveAs() {
108 		IEditorPart editor = getEditor(0);
109 		editor.doSaveAs();
110 		setPageText(0, editor.getTitle());
111 		setInput(editor.getEditorInput());
112 	}
113 	/* (non-Javadoc)
114 	 * Method declared on IEditorPart
115 	 */
116 	public void gotoMarker(IMarker marker) {
117 		setActivePage(0);
118 		IDE.gotoMarker(getEditor(0), marker);
119 	}
120 	/**
121 	 * The <code>MultiPageEditorExample</code> implementation of this method
122 	 * checks that the input is an instance of <code>IFileEditorInput</code>.
123 	 */
124 	public void init(IEditorSite site, IEditorInput editorInput)
125 		throws PartInitException {
126 		if (!(editorInput instanceof FileEditorInput))
127 			throw new PartInitException("Invalid Input: Must be FileEditorInput");
128 		/* Setting title*/
129 		if (editorInput instanceof IPathEditorInput) {
130 			IPathEditorInput pathInput = (IPathEditorInput) editorInput;
131 			setPartName(pathInput.getName());
132 		}
133 		super.init(site, editorInput);
134 	}
135 	/* (non-Javadoc)
136 	 * Method declared on IEditorPart.
137 	 */
138 	public boolean isSaveAsAllowed() {
139 		return true;
140 	}
141 	/**
142 	 * Calculates the contents of page 2 when the it is activated.
143 	 */
144 	protected void pageChange(int newPageIndex) {
145 		super.pageChange(newPageIndex);
146 		if (newPageIndex == 2) {
147 			//sortWords();
148 		}
149 	}
150 	/**
151 	 * Closes all project files on project close.
152 	 */
153 	public void resourceChanged(final IResourceChangeEvent event){
154 		if(event.getType() == IResourceChangeEvent.PRE_CLOSE){
155 			Display.getDefault().asyncExec(new Runnable(){
156 				public void run(){
157 					IWorkbenchPage[] pages = getSite().getWorkbenchWindow().getPages();
158 					for (int i = 0; i<pages.length; i++){
159 						if(((FileEditorInput)editor.getEditorInput()).getPath().equals(event.getResource())){
160 							IEditorPart editorPart = pages[i].findEditor(editor.getEditorInput());
161 							pages[i].closeEditor(editorPart,true);
162 						}
163 					}
164 				}            
165 			});
166 		}
167 	}	
168 
169 }