Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2909455527660b82e83409711e1766de1003d003 (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
/**
 * 
 */
package org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.internal;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.util.EContentAdapter;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IContentChangedListener;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IContentChangedProvider;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IContentChangedListener.ContentEvent;
import org.eclipse.papyrus.infra.core.sashwindows.di.AbstractPanel;
import org.eclipse.papyrus.infra.core.sashwindows.di.PageRef;
import org.eclipse.papyrus.infra.core.sashwindows.di.SashModel;
import org.eclipse.papyrus.infra.core.sashwindows.di.SashWindowsMngr;
import org.eclipse.papyrus.infra.core.sashwindows.di.Window;


/**
 * A class listening on changes on the internal model and delivering events to registered listeners.
 * 
 * @author cedric dumoulin
 *
 */
public class ContentChangedEventProvider implements IContentChangedProvider {
	
	private List<IContentChangedListener> listeners;

	/** Is this mngr delivering events ? */
	private boolean isDeliverEnable = true;

	/** Last event stored when isDeliverEnable == false; */
	private ContentEvent storedEvent;

	private EMFAdapter emfAdapter = new EMFAdapter();

	/**
	 * The model firing events
	 */
	private SashModel diSashModel;
	
	/**
	 * 
	 * Constructor.
	 *
	 * @param diSashModel
	 */
	public ContentChangedEventProvider(SashModel diSashModel) {
		this.diSashModel = diSashModel;
		activate();
	}
	
	/**
	 * 
	 * Constructor.
	 *
	 * @param diSashModel
	 */
	public ContentChangedEventProvider(SashWindowsMngr sashWindowMngr) {
		this(sashWindowMngr.getSashModel());
	}
	
	/**
	 * Connect EMF changeListener to the {@link ContentChangeListenerManager}.
	 * Changes in the EMF di model will be fired by the ContentChangeListenerManager.
	 */
	protected void activate() {
		diSashModel.eAdapters().add(emfAdapter);
		setDeliver(true);
	}

	protected void deactivate() {
		setDeliver(false);
		diSashModel.eAdapters().remove(emfAdapter);
	}

	/**
	 * @return the isDeliverEnable
	 */
	protected boolean isDeliver() {
		return isDeliverEnable;
	}

	/**
	 * @param isDeliverEnable
	 *        the isDeliverEnable to set
	 */
	protected void setDeliver(boolean isDeliverEnable) {

		if(this.isDeliverEnable == isDeliverEnable)
			return;

		// Check if the old value is not delivering event
		if(!this.isDeliverEnable) {
			this.isDeliverEnable = true;
			// reenable events. Check if an event is stored
			if(storedEvent != null)
				fireContentChanged(storedEvent);

		} else
			this.isDeliverEnable = isDeliverEnable;

		storedEvent = null;
	}

	/**
	 * Add a listener listening on content changed. This listener will be
	 * notified each time the content change.
	 * 
	 * @param listener
	 */
	public void addListener(IContentChangedListener listener) {
		if(listeners == null)
			createListeners();

		// Check if already exists.
		if(listeners.contains(listener))
			return;

		listeners.add(listener);
	}

	/**
	 * Add a listener listening on content changed. This listener will be
	 * notified each time the content change.
	 * 
	 * @param listener
	 */
	public void removeListener(IContentChangedListener listener) {
		if(listeners == null)
			return;

		listeners.remove(listener);
	}

	/**
	 * Create the list of listeners.
	 */
	private void createListeners() {
		if(listeners == null)
			listeners = new ArrayList<IContentChangedListener>();

	}

	/**
	 * Fire the changed event.
	 * 
	 * @param event
	 */
	protected void fireContentChanged(ContentEvent event) {
		if(listeners == null)
			return;

		if(!isDeliverEnable) {
			storedEvent = event;
			return;
		}

		for(IContentChangedListener listener : listeners) {
			listener.contentChanged(event);
		}
	}

	/**
	 * Change event Adapter.
	 * Forward EMF changeEvent to the {@link ContentChangeListenerManager}.
	 * 
	 * @author cedric dumoulin
	 */
	public class EMFAdapter extends EContentAdapter {

		/**
		 * ContentProvider Model has changed.
		 * Changes includes : ADD, REMOVE and MOVE of elements
		 * 
		 * @see org.eclipse.emf.common.notify.impl.AdapterImpl#notifyChanged(org.eclipse.emf.common.notify.Notification)
		 * 
		 * @param msg
		 */
		@Override
		public void notifyChanged(Notification msg) {
			super.notifyChanged(msg);

			// Filter out notification of type RESOLVE
			// We don't need to be noti
			if(msg.getEventType() == Notification.RESOLVE)
				return;

			// W
			Object sender = msg.getNotifier();
			if(sender instanceof AbstractPanel || sender instanceof Window || sender instanceof PageRef)
				fireContentChanged(new ContentEvent(msg.getEventType(), sender, null));
		}
	}

}

Back to the top