Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a6d5a7d99fb80dc00fb19244aaff633c25e45377 (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
/*****************************************************************************
 * 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.view;


import java.util.Collection;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.papyrus.infra.ui.util.PapyrusImageUtils;
import org.eclipse.papyrus.infra.widgets.toolbox.notification.ICallBack;
import org.eclipse.papyrus.infra.widgets.toolbox.notification.ICompositeCreator;
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.utils.PapyrusControlsFactory;
import org.eclipse.papyrus.infra.widgets.toolbox.utils.ToolbooxImageUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.part.ViewPart;


/**
 * This view displays a list of notifications for users
 */

public class PapyrusNotificationView extends ViewPart implements ICallBack {

	/**
	 * The ID of the view as specified by the extension.
	 */
	public static final String ID = "org.eclipse.papyrus.infra.widgets.toolbox.notification.view.PapyrusNotificationView";

	private Action closeAllAction;

	private Action collapseAllAction;

	private Action allDefaultAction;

	private boolean isCollapse = true;

	private ScrolledForm form;

	private FormToolkit toolkit = PapyrusToolkit.INSTANCE;

	/**
	 * The constructor.
	 */
	public PapyrusNotificationView() {
	}

	/**
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
	@Override
	public void createPartControl(Composite parent) {
		setTitleImage(PapyrusImageUtils.getDefaultIcon());
		createContent(parent);
		makeActions();
		hookContextMenu();
		hookDoubleClickAction();
		contributeToActionBars();
	}

	/**
	 * Add a composite in the view
	 *
	 * @param creator
	 *            , the composite creator
	 * @param collection
	 *            , the collection of actions
	 * @return the composite
	 */
	public AbstractInsideComposite setComposite(final ICompositeCreator creator, Collection<NotificationRunnable> collection) {
		MessageComposite composite = new MessageComposite(this, form, toolkit, collection) {

			@Override
			protected Control doCreateContents(FormToolkit toolkit, Composite compo) {
				Composite composite = creator.createComposite(compo, toolkit);
				setCompositeCreated(composite);
				return composite;
			}

		};
		manageComposite(composite);
		return composite;
	}

	/**
	 * Add a composite in the view
	 *
	 * @param creator
	 *            , the composite creator
	 * @param messageTitle
	 *            , the message in the section
	 * @param collection
	 *            , the collection of actions
	 * @return the composite
	 */
	public AbstractInsideComposite setComposite(final ICompositeCreator creator, final String messageTitle, Collection<NotificationRunnable> runnables) {
		return setComposite(creator, messageTitle, runnables, null);
	}

	/**
	 * Add a composite in the view
	 *
	 * @param creator
	 *            , the composite creator
	 * @param messageTitle
	 *            , the message in the section
	 * @param collection
	 *            , the collection of actions
	 * @return the composite
	 */
	public AbstractInsideComposite setComposite(final ICompositeCreator creator, final String messageTitle, Collection<NotificationRunnable> runnables, final Type type) {
		MessageComposite composite = new MessageComposite(this, form, toolkit, runnables) {

			@Override
			protected Control doCreateContents(FormToolkit toolkit, Composite composite) {
				Composite compo = null;
				if (type != null) {
					compo = PapyrusControlsFactory.createCompositeWithType(Display.getDefault().getActiveShell(), toolkit, composite, type, null, null, false, creator, context);
				} else {
					compo = creator.createComposite(composite, toolkit);
					setCompositeCreated(compo);
				}
				return compo;
			}

			@Override
			protected String getSectionName() {
				if (messageTitle == null) {
					return super.getSectionName();
				} else {
					return messageTitle;
				}
			}
		};
		manageComposite(composite);
		return composite;
	}

	/**
	 * Add a composite in the view
	 *
	 * @param message
	 *            , the message in the composite
	 * @param title
	 *            , the title of the section
	 * @param runnables
	 *            , the actions
	 * @return
	 */
	public AbstractInsideComposite setMessage(String message, final String title, Collection<NotificationRunnable> runnables) {
		MessageComposite compo = new MessageComposite(this, form, toolkit, runnables) {

			@Override
			protected String getSectionName() {
				if (title == null) {
					return super.getSectionName();
				} else {
					return title;
				}
			}
		};
		compo.setText(message);
		manageComposite(compo);
		return compo;
	}

	/**
	 * Add a message in the view
	 *
	 * @param message
	 *            , the message to display
	 */
	public void setMessage(String message) {
		setMessage(message, null);
	}

	/**
	 * Add a message with a collection of actions
	 *
	 * @param message
	 *            , the message to display
	 * @param runnables
	 *            , a collection of actions
	 */
	public void setMessage(String message, Collection<NotificationRunnable> runnables) {
		MessageComposite compo = new MessageComposite(this, form, toolkit, runnables);
		compo.setText(message);
		manageComposite(compo);
	}

	private void manageComposite(AbstractInsideComposite compo) {
		if (form.getBody().getChildren().length > 1) {
			Control previous = form.getBody().getChildren()[form.getBody().getChildren().length - 2];
			manageComposite(compo, previous);
		} else {
			manageComposite(compo, null);
		}

	}

	private void manageComposite(Control created, Control previous) {
		FormData data = new FormData();
		if (previous != null) {
			data.bottom = new FormAttachment(previous, -5);
		} else {
			data.bottom = new FormAttachment(98, -5);
		}
		data.left = new FormAttachment(0, 5);

		data.right = new FormAttachment(100, -5);
		created.setLayoutData(data);
		form.reflow(true);
		if (created instanceof AbstractInsideComposite) {
			AbstractInsideComposite inside = (AbstractInsideComposite) created;
			inside.setPrevious(previous);
		}
		if (previous instanceof AbstractInsideComposite) {
			AbstractInsideComposite inside = (AbstractInsideComposite) previous;
			inside.setAfter(created);
		}
		created.setFocus();
	}

	private void createContent(Composite parent) {
		parent.setLayout(new FillLayout(SWT.VERTICAL));
		form = toolkit.createScrolledForm(parent);
		form.getBody().setLayout(new FormLayout());
		form.setShowFocusedControl(true);
	}

	private void hookContextMenu() {
		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {

			public void menuAboutToShow(IMenuManager manager) {
				PapyrusNotificationView.this.fillContextMenu(manager);
			}
		});
	}

	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}

	private void fillLocalPullDown(IMenuManager manager) {
		manager.add(allDefaultAction);
		manager.add(closeAllAction);
		manager.add(collapseAllAction);
		manager.add(new Separator());
	}

	private void fillContextMenu(IMenuManager manager) {
	}

	private void fillLocalToolBar(IToolBarManager manager) {
		manager.add(allDefaultAction);
		manager.add(closeAllAction);
		manager.add(collapseAllAction);
	}

	private void makeActions() {
		allDefaultAction = new Action() {

			@Override
			public void run() {
				for (Control c : form.getBody().getChildren()) {
					if (c instanceof AbstractInsideComposite) {
						AbstractInsideComposite compo = (AbstractInsideComposite) c;
						compo.runDefault();
					}
				}
				form.getBody().layout(true, true);
				form.layout(true, true);
			}
		};
		allDefaultAction.setText("Run all default action");
		allDefaultAction.setToolTipText("Run all default action");
		allDefaultAction.setImageDescriptor(ToolbooxImageUtils.getImageDescriptor(org.eclipse.papyrus.infra.widgets.toolbox.utils.ISharedImages.IMG_RUN));
		closeAllAction = new Action() {

			@Override
			public void run() {
				for (Control c : form.getBody().getChildren()) {
					c.dispose();
				}
				form.getBody().layout(true, true);
				form.layout(true, true);
			}
		};
		closeAllAction.setText("Close All");
		closeAllAction.setToolTipText("Close all the messages");
		closeAllAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_ETOOL_DELETE));
		collapseAllAction = new Action() {

			@Override
			public void run() {
				for (Control c : form.getBody().getChildren()) {
					if (c instanceof AbstractInsideComposite) {
						AbstractInsideComposite compo = (AbstractInsideComposite) c;
						if (isCollapse) {
							compo.collapse();
						} else {
							compo.expand();
						}
					}
				}
				isCollapse = !isCollapse;
				if (isCollapse) {
					collapseAllAction.setText("Collapse all the messages");
					collapseAllAction.setToolTipText("Collapse all the messages");
				} else {
					collapseAllAction.setText("Expand all the messages");
					collapseAllAction.setToolTipText("Expand all the messages");
				}
				form.getBody().layout(true, true);
				form.layout(true, true);
			}
		};
		collapseAllAction.setText("Collapse all the messages");
		collapseAllAction.setToolTipText("Collapse all the messages");
		collapseAllAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_ELCL_COLLAPSEALL));
	}

	private void hookDoubleClickAction() {
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	@Override
	public void setFocus() {
		form.setFocus();
	}

	/*
	 * (non-Javadoc)
	 * this method update the after and previous attributes to update the layout
	 *
	 * @see org.eclipse.papyrus.infra.widgets.toolbox.notification.view.ICloseCallBack#closed(java.lang.Object)
	 */
	public void callBack(Object element) {
		destroy(element);
	}

	/**
	 * This method destroy the object if it is contained in the view
	 *
	 * @param element
	 */
	protected void destroy(Object element) {
		if (element instanceof AbstractInsideComposite) {
			AbstractInsideComposite inside = (AbstractInsideComposite) element;

			if (inside != null) {
				Control previous = inside.getPrevious();
				Control after = inside.getAfter();

				if ((after != null) && !(after.isDisposed())
						&& ((previous == null) || !(previous.isDisposed()))) {

					// previous can be null but can not be disposed when not null
					// after can not be null nor disposed
					manageComposite(after, previous);

				} else if ((previous instanceof AbstractInsideComposite) && !(previous.isDisposed())) {
					AbstractInsideComposite compo = (AbstractInsideComposite) previous;
					compo.setAfter(null);
					// FormData data = new FormData();
					// data.bottom = new FormAttachment(previous, -5);
					// data.left = new FormAttachment(0, 5);
					// data.right = new FormAttachment(100, -5);
					// compo.setLayoutData(data);
					// form.reflow(true);
				}
			}

			if (!inside.isDisposed()) {
				inside.dispose();
			}

			form.getBody().layout(true, true);
			form.layout(true, true);
		}
	}
}

Back to the top