Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ba95ca7a7be70298460b36b2402acc954ee2c8b8 (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
/**
 * 
 */
package org.eclipse.papyrus.sasheditor.ui.views;

import java.util.ArrayList;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.papyrus.sasheditor.editor.IPage;
import org.eclipse.papyrus.sasheditor.editor.IPageChangedListener;
import org.eclipse.papyrus.sasheditor.editor.ISashWindowsContainer;
import org.eclipse.papyrus.sasheditor.editor.ISashWindowsContainerChangedListener;
import org.eclipse.papyrus.sasheditor.editor.SashWindowsEventsProvider;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;


/**
 * The model (from MVC) storing data to be shown by the ui.
 * Data owned :
 * <ul>
 * <li>selected element</li>
 * <li>current page (tabfolder)</li>
 * <li>current folder</li>
 * <li>current eclipse editor</li>
 * </ul>
 * 
 * A faire:
 * - ecouter les evenements sash
 * - ecouter les evenements Eclipse selectionChanged
 * - envoyer evenement qd une valeur a change
 * - creer la classe ModelEvent indiquant le type d'evenement ?
 * - creer l'interface IModelEventListener permettant d'ecouter les events du model.
 * Q: une methode par event, ou alors un type dans l'event ?
 * - Completer la classe View avec la UI
 * - Completer ViewPart pour mettre le model et la UI en relation.
 * 
 * Regarder les exemples dans SashWindowsViewOrig
 * 
 * @author cedric dumoulin
 * 
 */
public class SashWindowsViewModel {



	/**
	 * Interface implemented by classes wishing to be called when a change happen in the model.
	 * 
	 * @author cedric dumoulin
	 * 
	 */
	public interface IModelChangedListener {

		/**
		 * This method is called when a value has changed in the model.
		 * 
		 * @param changedModel
		 *        The model that has changed.
		 */
		public void modelChanged(SashWindowsViewModel changedModel);

	}

	/**
	 * List of listeners on model changed events.
	 */
	private ListenersManager listenersManager = new ListenersManager();


	/** Object providing events on SashWindows life cycle. */
	private SashWindowsEventsProvider sashWindowEventsManager;

	/** Current editor shown by the view, or null */
	private IEditorPart currentEditor = null;

	/** The last selected element, or null */
	private Object selectedElement = null;

	/**
	 * Listener on Eclipse selection changed.
	 */
	private ISelectionChangedListener selectionChangedListener = new ISelectionChangedListener() {

		public void selectionChanged(SelectionChangedEvent event) {
			System.out.println(SashWindowsViewModel.this.getClass().getSimpleName() + ".selectionChanged(" + event + ")");

			SashWindowsViewModel.this.selectionChanged(event.getSelection());
		}
	};

	/**
	 * Listener listening on pageChanged event.
	 */
	private IPageChangedListener pageChangedListener = new IPageChangedListener() {

		public void pageChanged(IPage newPage) {
			System.out.println(SashWindowsViewModel.this.getClass().getSimpleName() + ".pageChanged(" + newPage + ")");
			selectionChanged();
		}
	};

	/**
	 * Listener listening on containerChanged event.
	 */
	private ISashWindowsContainerChangedListener containerChangedListener = new ISashWindowsContainerChangedListener() {

		public void sashWindowsContainerChanged(ISashWindowsContainer newContainer)
			{
			// Get the active editor, which should be the one owning the new page
			// Set it to null if the container is null.
			System.out.println(SashWindowsViewModel.this.getClass().getSimpleName() + ".sashWindowsContainerChanged(" + newContainer + ")");

			reconnectSelectionListener();
		}
	};



	/**
	 * 
	 * Constructor.
	 * 
	 * @param workbenchPage
	 */
	public SashWindowsViewModel(IWorkbenchPage workbenchPage) {
		sashWindowEventsManager = new SashWindowsEventsProvider(workbenchPage);

		activateIncomingEventListeners();
	}

	/**
	 * Dispose this model.
	 * All resources are cleaned. The model should not be used again.
	 */
	public void dispose() {
		deactivateIncomingEventListeners();
		sashWindowEventsManager.dispose();
		listenersManager.clear();
	}


	/**
	 * Get the listener manager allowing to register / remove listeners.
	 * 
	 * @return the listenersManager
	 */
	public ListenersManager getListenersManager() {
		return listenersManager;
	}

	/**
	 * Activate listeners on incoming events.
	 */
	private void activateIncomingEventListeners() {
		sashWindowEventsManager.addPageChangedListener(pageChangedListener);
		sashWindowEventsManager.addSashWindowsContainerChangedListener(containerChangedListener);
		reconnectSelectionListener();
	}

	/**
	 * Activate listeners on incoming events.
	 */
	private void deactivateIncomingEventListeners() {
		disconnectSelectionListener();
		sashWindowEventsManager.removePageChangedListener(pageChangedListener);
		sashWindowEventsManager.removeSashWindowsContainerChangedListener(containerChangedListener);
	}

	/**
	 * Connect the selection listener to the current IEditor (Eclipse editor).
	 * Remove the listener from the previous editor if it is attached.
	 */
	private void reconnectSelectionListener() {
		// Remove listening on all editor
		if(currentEditor != null) {
			currentEditor.getSite().getSelectionProvider().removeSelectionChangedListener(selectionChangedListener);
		}

		currentEditor = sashWindowEventsManager.activeSashWindowsContainerOwner();
		if(currentEditor != null) {
			currentEditor.getSite().getSelectionProvider().addSelectionChangedListener(selectionChangedListener);
		}
	}

	/**
	 * Remove the listener from the previous editor if it is attached.
	 */
	private void disconnectSelectionListener() {
		// Remove listening on all editor
		if(currentEditor != null) {
			currentEditor.getSite().getSelectionProvider().removeSelectionChangedListener(selectionChangedListener);
		}
	}

	/**
	 * Trigger the event to registered listeners
	 */
	private void fireChangedEvent() {
		listenersManager.fireChangedEvent(this);
	}

	/**
	 * This method is called when the selection has changed.
	 * This method lookup the selection.
	 * TODO Hook this to Container changed instead of selection changed.
	 */
	private void selectionChanged() {
		IEditorPart activeEditor = sashWindowEventsManager.activeSashWindowsContainerOwner();
		// Check if there is an active editor.
		if(activeEditor == null) {
			selectionChanged(null);
			return;
		}

		// Set the active selection
		ISelection selection = activeEditor.getSite().getSelectionProvider().getSelection();
		selectionChanged(selection);
	}

	/**
	 * The selection has changed.
	 * 
	 * @param selection
	 */
	protected void selectionChanged(ISelection selection) {

		if(selection instanceof StructuredSelection) {
			StructuredSelection structuredSelection = (StructuredSelection)selection;
			selectedElement = structuredSelection.getFirstElement();
		} else {
			selectedElement = selection;
		}

		// Fire changed event.
		fireChangedEvent();
	}


	/**
	 * Get the currently first selected element.
	 * 
	 * @return The currently first selected element, or null if none is selected.
	 */
	public Object getSelectedElement() {
		return selectedElement;
	}

	/**
	 * Get the currently selected SashWindows Page
	 * 
	 * @return The currently selected SashWindows Page, or null if none is selected.
	 */
	public IPage getSelectedSashWindowsPage() {
		return sashWindowEventsManager.activeSashWindowsPage();
	}

	//	/**
	//	 * Get the currently selected SashWindows Folder 
	//	 * @return The currently selected SashWindows Page, or null if none is selected.
	//	 */
	//	public IFolder getSelectedSashWindowsFolder() 
	//	{
	//		return sashWindowEventsManager.SashWindowsFolder();		
	//	}

	/**
	 * Get the currently selected SashWindows Page (IE.
	 * 
	 * @return The currently selected SashWindows Page, or null if none is selected.
	 */
	public IEditorPart getSelectedIEditorPart() {
		return sashWindowEventsManager.activeSashWindowsContainerOwner();

	}

	/**
	 * Class used to register Listener and to send events to these listeners.
	 * 
	 * @author cedric dumoulin
	 * 
	 */
	public class ListenersManager extends ArrayList<IModelChangedListener> {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		/**
		 * Add a listener that will be notified when the fireChangedEvent() method is called.
		 * 
		 * @param listener
		 */
		public void addModelChangedListener(IModelChangedListener listener) {
			add(listener);
		}

		/**
		 * Remove a listener.
		 * 
		 * @param listener
		 */
		public void removeModelChangedListener(IModelChangedListener listener) {
			remove(listener);
		}

		/**
		 * Fire the event to all registered listeners.
		 */
		public void fireChangedEvent(SashWindowsViewModel changedModel) {
			for(IModelChangedListener listener : this) {
				listener.modelChanged(changedModel);
			}
		}
	}
}

Back to the top