Skip to main content
summaryrefslogtreecommitdiffstats
blob: b787e0cdf8aed583ddc0d7ac2c59b39b9ae2a313 (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
/*******************************************************************************
 * Copyright (c) 2010 Tasktop Technologies and others.
 * 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:
 *     Tasktop Technologies - initial API and implementation
 *     Itema AS - bug 330064 notification filtering and model persistence
 *     Itema AS - bug 331424 handle default event-sink action associations
 *******************************************************************************/

package org.eclipse.mylyn.internal.commons.ui.notifications;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.core.runtime.Assert;
import org.eclipse.ui.IMemento;

/**
 * @author Steffen Pingel
 * @author Torkild U. Resheim
 * @deprecated use classes in the <code>org.eclipse.mylyn.commons.notifications.core</code> bundle instead
 */
@Deprecated
public class NotificationModel {

	private boolean dirty;

	private Map<String, NotificationHandler> handlerByEventId;

	public NotificationModel(IMemento memento) {
		initialize(memento);
	}

	void initialize(IMemento memento) {
		this.handlerByEventId = new HashMap<String, NotificationHandler>();
		// We need the handlerByEventId map to be populated early
		for (NotificationCategory category : getCategories()) {
			for (NotificationEvent event : category.getEvents()) {
				getOrCreateNotificationHandler(event);
			}
		}
		if (memento != null) {
			load(memento);
		}
	}

	public Collection<NotificationCategory> getCategories() {
		return NotificationsExtensionReader.getCategories();
	}

	public NotificationHandler getNotificationHandler(String eventId) {
		return handlerByEventId.get(eventId);
	}

	public NotificationHandler getOrCreateNotificationHandler(NotificationEvent event) {
		NotificationHandler handler = getNotificationHandler(event.getId());
		if (handler == null) {
			handler = new NotificationHandler(event, getActions(event));
			handlerByEventId.put(event.getId(), handler);
		}
		return handler;
	}

	private List<NotificationAction> getActions(NotificationEvent event) {
		List<NotificationSinkDescriptor> descriptors = NotificationsExtensionReader.getSinks();
		List<NotificationAction> actions = new ArrayList<NotificationAction>(descriptors.size());
		for (NotificationSinkDescriptor descriptor : descriptors) {
			NotificationAction action = new NotificationAction(descriptor);
			if (event.defaultHandledBySink(descriptor.getId())) {
				action.setSelected(true);
			}
			actions.add(action);
		}
		return actions;
	}

	public boolean isDirty() {
		return dirty;
	}

	public boolean isSelected(NotificationEvent event) {
		NotificationHandler handler = getOrCreateNotificationHandler(event);
		for (NotificationAction action : handler.getActions()) {
			if (action.isSelected()) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Stores the selected state of events and sinks.
	 * 
	 * @param memento
	 *            the memento to store in.
	 */
	public void save(IMemento memento) {
		for (Entry<String, NotificationHandler> entry : handlerByEventId.entrySet()) {
			IMemento event = memento.createChild("event"); //$NON-NLS-1$
			event.putString("id", entry.getKey()); //$NON-NLS-1$
			List<NotificationAction> actions = entry.getValue().getActions();
			for (NotificationAction notificationAction : actions) {
				IMemento action = event.createChild("action"); //$NON-NLS-1$
				action.putBoolean("selected", notificationAction.isSelected()); //$NON-NLS-1$
				action.putString("sink", notificationAction.getSinkDescriptor().getId()); //$NON-NLS-1$
			}
		}
		setDirty(false);
	}

	/**
	 * Updates the notification model with selected states from the memento instance.
	 * 
	 * @param memento
	 */
	private void load(IMemento memento) {
		Assert.isNotNull(memento);
		for (IMemento mEvent : memento.getChildren("event")) { //$NON-NLS-1$
			for (NotificationCategory category : getCategories()) {
				for (NotificationEvent event : category.getEvents()) {
					if (event.getId().equals(mEvent.getString("id"))) { //$NON-NLS-1$
						NotificationHandler handler = getOrCreateNotificationHandler(event);
						List<NotificationAction> actions = handler.getActions();
						for (NotificationAction notificationAction : actions) {
							IMemento[] mActions = mEvent.getChildren("action"); //$NON-NLS-1$
							for (IMemento mAction : mActions) {
								if (notificationAction.getSinkDescriptor().getId().equals(mAction.getString("sink"))) { //$NON-NLS-1$
									notificationAction.setSelected(mAction.getBoolean("selected")); //$NON-NLS-1$
								}
							}
						}
					}
				}
			}
		}
	}

	public void setDirty(boolean dirty) {
		this.dirty = dirty;
	}

	/**
	 * Updates the state of the notification handlers depending on their child notification action states.
	 */
	@Deprecated
	public void updateStates() {
		Collection<NotificationHandler> handlers = handlerByEventId.values();
		for (NotificationHandler notificationHandler : handlers) {
			List<NotificationAction> actions = notificationHandler.getActions();
			boolean selected = false;
			for (NotificationAction notificationAction : actions) {
				if (notificationAction.isSelected()) {
					selected = true;
					break;
				}
			}
			notificationHandler.getEvent().setSelected(selected);
		}
	}

	public void setNotificationHandler(String eventId, NotificationHandler handler) {
		handlerByEventId.put(eventId, handler);
		setDirty(true);
	}

}

Back to the top