Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bdbecf8d15e529b173bb6acc74fe1bc7a46e7ef3 (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*****************************************************************************
 * Copyright (c) 2010 ATOS ORIGIN.
 *    
 * 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:
 *  Tristan Faure (ATOS ORIGIN INTEGRATION) tristan.faure@atosorigin.com - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.widgets.toolbox.notification.builders;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.papyrus.infra.widgets.toolbox.notification.IBuilder;
import org.eclipse.papyrus.infra.widgets.toolbox.notification.ICompositeCreator;
import org.eclipse.papyrus.infra.widgets.toolbox.notification.INotification;
import org.eclipse.papyrus.infra.widgets.toolbox.notification.NotificationRunnable;
import org.eclipse.papyrus.infra.widgets.toolbox.notification.PapyrusToolkit;
import org.eclipse.papyrus.infra.widgets.toolbox.notification.Type;
import org.eclipse.papyrus.infra.widgets.toolbox.notification.popups.PopupNotification;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.widgets.FormToolkit;


/**
 * A class creating a notification,
 * the run method launch the message according to the value of the attributes
 * 
 * @author tristan faure
 * 
 */
public class NotificationBuilder {

	private FormToolkit toolkit = PapyrusToolkit.INSTANCE;

	/** The parameters of the notification with the corresponding values */
	protected Map<String, Object> parameters = new HashMap<String, Object>();



	/** The builders creating the notification */
	protected static Map<Class<? extends IBuilder>, IBuilder> builders = getBuilders();

	/** asynchronous, determines if the message needs or not to be synchronous with the notification */
	static String ASYNCHRONOUS = "asynchronous";

	/** a message displayed in the notification */
	static String MESSAGE = "message";

	/** a composite creator for the element */
	static String COMPOSITE = "composite";

	/** a default action in the notification */
	static String ACTION = "default_action";

	/** a delay to display if it is a temporary notification */
	static String DELAY = "delay";

	/** determines if the notification is temporary */
	static String TEMPORARY = "temporary";

	/** a title displayed in the notification */
	static String TITLE = "title";

	/** determines if there is html content in the notification */
	static String HTML = "html";

	/** determines the type according to {@link Type} */
	static String TYPE = "type";

	/** an image displayed generally at the left of the notification */
	static String IMAGE = "image";

	/**
	 * Determine a specific builder class, if it is filled, it is forced to it
	 */
	protected Class<? extends IBuilder> builderClass;


	/**
	 * Returns the ibuilders able to create notifications
	 * 
	 * @return the list of {@link IBuilder}
	 */
	private static Map<Class<? extends IBuilder>, IBuilder> getBuilders() {
		Map<Class<? extends IBuilder>, IBuilder> result = new HashMap<Class<? extends IBuilder>, IBuilder>();
		IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.papyrus.infra.widgets.toolbox.papyrusNotificationBuilder");
		for(IConfigurationElement e : elements) {
			IBuilder instance;
			try {
				instance = (IBuilder)e.createExecutableExtension("builder");
				result.put(instance.getClass(), instance);
			} catch (CoreException e1) {
			}
		}
		return result;
	}

	/**
	 * Set a message for the notification
	 * 
	 * @param message
	 *        , the message to display
	 * @return this
	 */
	public NotificationBuilder setMessage(String message) {
		parameters.put(MESSAGE, message);
		return this;
	}

	/**
	 * Determines if the notification is asynchronous (don't force the user to read the notification immediately)
	 * 
	 * @param asynchronous
	 *        , true if it asynchronous
	 * @return this
	 */
	public NotificationBuilder setAsynchronous(boolean asynchronous) {
		parameters.put(ASYNCHRONOUS, asynchronous);
		return this;
	}

	/**
	 * Set a default action for the notification
	 * 
	 * @param runnable
	 *        , a runnable triggered when default action of the notification is selected
	 *        The first action added is the default One
	 * @return this
	 */
	@SuppressWarnings("unchecked")
	public NotificationBuilder addAction(NotificationRunnable runnable) {
		Collection<NotificationRunnable> runnables = (Collection<NotificationRunnable>)parameters.get(ACTION);
		if(runnables == null) {
			runnables = new LinkedList<NotificationRunnable>();
			parameters.put(ACTION, runnables);
		}
		runnables.add(runnable);
		return this;
	}

	/**
	 * Set a composite creator, able to fill a notification
	 * 
	 * @param creator
	 *        , the composite creator
	 * @return this
	 */
	public NotificationBuilder setComposite(ICompositeCreator creator) {
		parameters.put(COMPOSITE, creator);
		return this;
	}

	/**
	 * Set a delay if the notification is temporary
	 * 
	 * @param delayMs
	 *        , the delay in ms for visibility
	 * @return this
	 */
	public NotificationBuilder setDelay(long delayMs) {
		parameters.put(DELAY, delayMs);
		return this;
	}

	/**
	 * Set true if the notification is temporary
	 * 
	 * @param temporary
	 * @return this
	 */
	public NotificationBuilder setTemporary(boolean temporary) {
		parameters.put(TEMPORARY, temporary);
		return this;
	}

	/**
	 * Set a title for the notification
	 * 
	 * @param title
	 *        , the title
	 * @return this
	 */
	public NotificationBuilder setTitle(String title) {
		parameters.put(TITLE, title);
		return this;
	}

	/**
	 * Set if the notification has to understand HTML
	 * 
	 * @param useHTML
	 * @return this
	 */
	public NotificationBuilder setHTML(boolean useHTML) {
		parameters.put(HTML, useHTML);
		return this;
	}

	/**
	 * Set the type of the notification according to {@link Type}
	 * 
	 * @param type
	 *        , the desired type
	 * @return this
	 */
	public NotificationBuilder setType(Type type) {
		parameters.put(TYPE, type);
		return this;
	}

	/**
	 * Set an image for the notification
	 * 
	 * @param image
	 *        , the desired image
	 * @return this
	 */
	public NotificationBuilder setImage(Image image) {
		parameters.put(IMAGE, image);
		return this;
	}

	/**
	 * Force a builder class
	 * 
	 * @param builderClass
	 *        , a class which inherits from {@link IBuilder}
	 * @return this
	 */
	public NotificationBuilder setBuilderClass(Class<? extends IBuilder> builderClass) {
		this.builderClass = builderClass;
		return this;
	}

	/**
	 * Allows the developer to use a specific parameter
	 * 
	 * @param name
	 *        , the key of the parameter
	 * @param value
	 *        , the value
	 * @return this
	 */
	public NotificationBuilder setParameter(String name, Object value) {
		parameters.put(name, value);
		return this;
	}

	/**
	 * Creates a notification according to different parameters
	 */
	public INotification run() {
		Set<IBuilder> copy = null;
		if(builderClass != null) {
			copy = new HashSet<IBuilder>();
			try {
				copy.add(builderClass.newInstance());
			} catch (InstantiationException e) {
			} catch (IllegalAccessException e) {
			}
		} else {
			copy = new HashSet<IBuilder>(builders.values());
		}
		for(Iterator<IBuilder> i = copy.iterator(); i.hasNext();) {
			IBuilder b = i.next();
			for(String string : parameters.keySet()) {
				if(!b.accept(string, parameters.get(string))) {
					i.remove();
					break;
				}
			}
		}
		INotification result = null;
		PropertyWrapper wrapper = new PropertyWrapper(parameters);
		if(copy.size() >= 1) {
			result = copy.iterator().next().build(wrapper, toolkit);
			// default case : the popup
		} else if(copy.isEmpty()) {
			result = builders.get(PopupBuilder.class).build(wrapper, toolkit);
		}
		return result;
	}

	/**
	 * Creates a notification builder already configured to display an information builder
	 * 
	 * @return a notification builder
	 */
	public static NotificationBuilder createInformationBuilder() {
		NotificationBuilder builder = new NotificationBuilder();
		return builder;
	}

	/**
	 * Creates a notification builder already configured to display an asynchronous popup
	 * 
	 * @param text
	 *        , the text to display
	 * @return a notification builder
	 */
	public static NotificationBuilder createAsyncPopup(String text) {
		return new NotificationBuilder().setAsynchronous(true).setTemporary(true).setMessage(text).setDelay(2000);
	}

	/**
	 * Creates a notification builder already configured to display an asynchronous popup with a specified title
	 * 
	 * @param text
	 *        , the text to display
	 * @param title
	 *        , the title of the popup
	 * @return a notification builder
	 */
	public static NotificationBuilder createAsyncPopup(String title, String text) {
		return new NotificationBuilder().setAsynchronous(true).setTemporary(true).setMessage(text).setTitle(title).setDelay(2000);
	}

	/**
	 * Creates a notification builder already configured to display an information popup
	 * 
	 * @param text
	 *        , the text to display
	 * @return a notification builder
	 */
	public static NotificationBuilder createInfoPopup(String text) {
		return new NotificationBuilder().setAsynchronous(false).setTemporary(false).setMessage(text).setType(Type.INFO);
	}

	/**
	 * Creates a notification builder already configured to display an warning popup
	 * 
	 * @param text
	 *        , the text to display
	 * @return a notification builder
	 */
	public static NotificationBuilder createWarningPopup(String text) {
		return new NotificationBuilder().setAsynchronous(false).setTemporary(false).setMessage(text).setType(Type.WARNING);
	}

	/**
	 * Creates a notification builder already configured to display a popup with question icon
	 * 
	 * @param text
	 *        , the text to display
	 * @return a notification builder
	 */
	public static NotificationBuilder createQuestionPopup(String text) {
		return new NotificationBuilder().setAsynchronous(false).setTemporary(false).setMessage(text).setType(Type.QUESTION);
	}

	/**
	 * Creates a notification builder already configured to display a popup with error icon
	 * 
	 * @param text
	 *        , the text to display
	 * @return a notification builder
	 */
	public static NotificationBuilder createErrorPopup(String text) {
		return new NotificationBuilder().setAsynchronous(false).setTemporary(false).setMessage(text).setType(Type.ERROR);
	}

	/**
	 * Creates a notification builder already configured to display a yes no question
	 * 
	 * @param yes
	 *        , the action to launch if yes is selected
	 * @param no
	 *        , the action to launch if no is selected
	 * @return a notification builder
	 */
	public static NotificationBuilder createYesNo(String message, final Runnable yes, final Runnable no) {
		return new NotificationBuilder().setType(Type.QUESTION).setAsynchronous(false).setTemporary(false).setMessage(message).addAction(new NotificationRunnable() {

			public void run(IContext context) {
				if(yes != null) {
					context.put(IContext.ACTION_ID, SWT.YES);
					yes.run();
				}
			}

			public String getLabel() {
				return "Yes";
			}
		}).addAction(new NotificationRunnable() {

			public void run(IContext context) {
				if(no != null) {
					context.put(IContext.ACTION_ID, SWT.NO);
					no.run();
				}
			}

			public String getLabel() {
				return "No";
			}
		});
	}

	/**
	 * Creates a notification builder already configured to display a yes no question, no runnables are necesary as the user just want the
	 * PopupNotification result
	 * This NotificationRunnable is not intended to be changed to an asynchronous notification for example
	 * When the run method is called use getRsult method in {@link PopupNotification} and test if the value is SWT.YES or SWT.NO
	 * @param message
	 *        , the message to display
	 * 
	 * @return a notification builder
	 */
	public static NotificationBuilder createYesNo(String message) {
		return new NotificationBuilder().setType(Type.QUESTION).setAsynchronous(false).setTemporary(false).setMessage(message).addAction(new NotificationRunnable() {

			public void run(IContext context) {
				context.put(IContext.ACTION_ID, SWT.YES);
			}

			public String getLabel() {
				return "Yes";
			}
		}).addAction(new NotificationRunnable() {

			public void run(IContext context) {
				context.put(IContext.ACTION_ID, SWT.NO);
			}

			public String getLabel() {
				return "No";
			}
		});
	}

	/**
	 * Creates a notification builder already configured to display a yes no question
	 * 
	 * @param yes
	 *        , the action to launch if yes is selected
	 * @param no
	 *        , the action to launch if no is selected
	 * @return a notification builder
	 */
	public static NotificationBuilder createYesNo(String message, final NotificationRunnable yes, final NotificationRunnable no) {
		return new NotificationBuilder().setType(Type.QUESTION).setAsynchronous(false).setTemporary(false).setMessage(message).addAction(new NotificationRunnable() {

			public void run(IContext context) {
				if(yes != null) {
					context.put(IContext.ACTION_ID, SWT.YES);
					yes.run(context);
				}
			}

			public String getLabel() {
				return "Yes";
			}
		}).addAction(new NotificationRunnable() {

			public void run(IContext context) {
				if(no != null) {
					context.put(IContext.ACTION_ID, SWT.NO);
					no.run(context);
				}
			}

			public String getLabel() {
				return "No";
			}
		});
	}

	/**
	 * Return the system image according to the imageID
	 * 
	 * @param imageID
	 * @param shell
	 * @return
	 */
	public static Image getSWTImage(final int imageID, Shell shell) {
		final Display display;
		if(shell == null || shell.isDisposed()) {
			display = Display.getCurrent();
			// The dialog should be always instantiated in UI thread.
			// However it was possible to instantiate it in other threads
			// (the code worked in most cases) so the assertion covers
			// only the failing scenario. See bug 107082 for details.
			Assert.isNotNull(display, "The dialog should be created in UI thread"); //$NON-NLS-1$
		} else {
			display = shell.getDisplay();
		}

		final Image[] image = new Image[1];
		display.syncExec(new Runnable() {

			public void run() {
				image[0] = display.getSystemImage(imageID);
			}
		});

		return image[0];
	}
}

Back to the top