Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bd672bf94686b6e9915afeaadba9378ad98566e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*****************************************************************************
 * Copyright (c) 2009 CEA LIST & LIFL 
 *
 *    
 * 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:
 *  Cedric Dumoulin  Cedric.dumoulin@lifl.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.example.core.sashwindows.fulleditor.editor;


import java.io.IOException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.papyrus.example.core.sashwindows.fulleditor.texteditor.TextEditorPartModel;
import org.eclipse.papyrus.sasheditor.contentprovider.IContentChangedListener;
import org.eclipse.papyrus.sasheditor.contentprovider.IPageModel;
import org.eclipse.papyrus.sasheditor.contentprovider.ISashWindowsContentProvider;
import org.eclipse.papyrus.sasheditor.contentprovider.di.DiSashModelMngr;
import org.eclipse.papyrus.sasheditor.contentprovider.di.IPageModelFactory;
import org.eclipse.papyrus.sasheditor.editor.AbstractMultiPageSashEditor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IPropertyListener;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.ide.IDE;


/**
 * An example showing how to create a multi-page editor using sash windows.
 * This example start with one page, and provide actions to create new pages.
 * It can be used with the full sashWindow system (extends AbstractMultiPageSashEditor) or
 * with the original Eclipse MultiPageEditor (extends MultiPageEditor).
 * This editor use the EMF Di implementation of the ContentProvider.
 * To use one or the other, change the extended class.
 */
public class DiMultiTextEditor extends /* MultiPageEditor */AbstractMultiPageSashEditor {

	/** SashModelMngr to add pages */
	protected DiSashModelMngr sashModelMngr;

	/** Resource mngr to load and save model */
	protected ResourceMngr resourceMngr;

	/**
	 * A listener on model change events.
	 */
	IContentChangedListener contentChangedListener = new IContentChangedListener() {

		/**
		 * Called when the content is changed. RefreshTabs.
		 */
		public void contentChanged(ContentEvent event) {
			System.out.println("contentChanged()");
			markDirty();
			refreshTabs();
		}
	};

	/**
	 * The dirty flag.
	 */
	protected boolean isDirty = false;

	/**
	 * Listener on PROP_DIRTY event. Register the change.
	 */
	private IPropertyListener dirtyPropertyListener = new IPropertyListener() {

		public void propertyChanged(Object source, int propId) {
			if(propId == PROP_DIRTY)
				isDirty = true;
		}
	};

	/**
	 * Creates a multi-page editor example.
	 */
	public DiMultiTextEditor() {
		super();
		//		ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
		// Listen on dirty event.
		addPropertyListener(dirtyPropertyListener);
	}

	/**
	 * Mark the editor as dirty, and fire appropriate event.
	 */
	protected void markDirty() {
		isDirty = true;
		firePropertyChange(PROP_DIRTY);
	}

	/**
	 * Create and initialize the pageProvider.
	 */
	protected ISashWindowsContentProvider createPageProvider() {
		IPageModelFactory pageFactory = new SimplePageModelFactory();

		//		sashModelMngr = new DiSashModelMngr(pageFactory, resourceMngr.getDiResource() );
		sashModelMngr = new DiSashModelMngr(pageFactory);

		ISashWindowsContentProvider pageProvider = sashModelMngr.getISashWindowsContentProvider();

		// Adding requested pages
		pageProvider.addPage(new TextEditorPartModel());
		// Listen on contentProvider changes
		sashModelMngr.getSashModelContentChangedProvider().addListener(contentChangedListener);

		return pageProvider;
	}

	/**
	 * 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);
		sashModelMngr.getSashModelContentChangedProvider().removeListener(contentChangedListener);
		super.dispose();
	}

	/**
	 * Saves the multi-page editor's document.
	 */
	public void doSave(IProgressMonitor monitor) {

		IEditorPart editor = getActiveEditor();
		if(editor != null) {
			editor.doSave(monitor);
			// Reset dirty flag.
			isDirty = false;
		}

		//		try {
		//			resourceMngr.saveResource(monitor);
		//		} catch (IOException e) {
		//			e.printStackTrace();
		//		}
	}

	/**
	 * 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 = getActiveEditor();
		if(editor != null) {
			editor.doSaveAs();
			//			setPageText(0, editor.getTitle());
			setInput(editor.getEditorInput());

			// Reset dirty flag.
			isDirty = false;
		}
	}

	/*
	 * (non-Javadoc)
	 * Method declared on IEditorPart
	 */
	public void gotoMarker(IMarker marker) {
		//		setActivePage(0);
		IDE.gotoMarker(getActiveEditor(), 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);

		// Load model
		//		IFile file = ((IFileEditorInput) editorInput).getFile();
		//		resourceMngr = new ResourceMngr();
		//		resourceMngr.loadResource(file);
	}

	/*
	 * (non-Javadoc)
	 * Method declared on IEditorPart.
	 */
	public boolean isSaveAsAllowed() {
		return true;
	}

	@Override
	public boolean isDirty() {
		return isDirty;
	}

	/**
	 * Simple implementation of the factory.
	 * This factory return the object as is.
	 * 
	 * @author dumoulin
	 */
	public class SimplePageModelFactory implements IPageModelFactory {

		public IPageModel createIPageModel(Object pageIdentifier) {
			return (IPageModel)pageIdentifier;
		}

	}

	/**
	 * A class managing a Resource.
	 * 
	 * @author cedric dumoulin
	 */
	public class ResourceMngr {

		private static final String DI_FILE_EXTENSION = "di";

		private ResourceSet resourceSet;

		private Resource diResource;


		/**
		 * Constructor.
		 */
		public ResourceMngr() {
			createResourceSet();
		}

		/**
		 * Return the resource used for DI model.
		 * 
		 * @return
		 */
		public Resource getDiResource() {
			return diResource;
		}

		/**
		 * Create the resourceSet.
		 */
		protected void createResourceSet() {

			if(resourceSet != null)
				return;

			// Create di resource
			resourceSet = new ResourceSetImpl();

			// Register the default resource factory -- only needed for stand-alone!
			resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
					Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());

		}

		/**
		 * Load resource using name provided in file.
		 * 
		 * @param file
		 */
		public void loadResource(IFile file) {
			// Extract file name, without extension
			IPath fullPath = file.getFullPath().removeFileExtension();

			IPath filePath;
			filePath = fullPath.addFileExtension(DI_FILE_EXTENSION);

			URI uri = URI.createPlatformResourceURI(filePath.toString(), true);
			try {
				diResource = resourceSet.getResource(uri, true);
			} catch (Exception e) {
				//				diResource = resourceSet.createResource(uri);
				e.printStackTrace();
			}

		}

		/**
		 * Load resource using name provided in file.
		 * 
		 * @param file
		 */
		public void createResource(IFile file) {
			// Extract file name, without extension
			IPath fullPath = file.getFullPath().removeFileExtension();

			IPath filePath;
			filePath = fullPath.addFileExtension(DI_FILE_EXTENSION);

			URI uri = URI.createPlatformResourceURI(filePath.toString(), true);
			diResource = resourceSet.createResource(uri);

		}

		/**
		 * Save Resource associated to this manager.
		 * 
		 * @throws IOException
		 */
		public void saveResource() throws IOException {
			diResource.save(null);
		}

		public void saveResource(IProgressMonitor monitor) throws IOException {
			monitor.beginTask("Saving resources", 3);
			monitor.worked(1);
			saveResource();
			monitor.worked(1);
			monitor.done();
		}


	}
}

Back to the top