Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 37e0ecf78c2b3dc10f7a3a899fd20b8bbc246c5a (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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*****************************************************************************
 * Copyright (c) 2009 CEA LIST.
 * 
 * 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.customization.palette.dialog;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.gef.palette.PaletteEntry;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.ui.palette.PaletteCustomizer;
import org.eclipse.gmf.runtime.gef.ui.palette.customize.PaletteCustomizerDialogEx;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.ICheckStateProvider;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.papyrus.infra.core.sasheditor.editor.ISashWindowsContainer;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.Messages;
import org.eclipse.papyrus.uml.diagram.common.part.PaletteUtil;
import org.eclipse.papyrus.uml.diagram.common.part.PapyrusPaletteCustomizer;
import org.eclipse.papyrus.uml.diagram.common.part.PapyrusPalettePreferences;
import org.eclipse.papyrus.uml.diagram.common.service.PapyrusPaletteService;
import org.eclipse.papyrus.uml.diagram.common.service.PapyrusPaletteService.ExtendedProviderDescriptor;
import org.eclipse.papyrus.uml.diagram.common.service.PapyrusPaletteService.LocalProviderDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;

/**
 * specific dialog window for the customisation of the palette
 */
public class PapyrusPaletteCustomizerDialog extends PaletteCustomizerDialogEx implements IPreferenceChangeListener {

	/** new local palette icon */
	private static final String NEW_LOCAL_DESC_IMAGE = "/icons/local_desc_new.gif";

	/** delete palette icon */
	private static final String DELETE_LOCAL_DESC_IMAGE = "/icons/local_desc_destroy.gif";

	/** edit palette icon */
	private static final String EDIT_LOCAL_DESC_IMAGE = "/icons/local_desc_edit.gif";

	/** path to the local descriptor icon */
	protected final String LOCAL_DESCRIPTOR = "/icons/local_desc.gif";

	/** path to the plugin descriptor icon */
	protected final String PLUGIN_DESCRIPTOR = "/icons/plugin_desc.gif";

	/** path to the plugin descriptor icon */
	protected final String EXTENDED_PLUGIN_DESCRIPTOR = "/icons/extended_plugin_desc.gif";

	/** viewer for the available tools */
	protected TreeViewer availableToolsTreeViewer;

	/** table viewed by the availablePalettesTreeViewer */
	protected Table availablePalettesTable;

	/** viewer for the available palettes */
	protected CheckboxTableViewer availablePalettesTableViewer;

	/** label provider for palette provider */
	protected PaletteLabelProvider providersLabelProvider;

	/** add local palette button */
	protected Button newPaletteButton;

	/** delete local palette button */
	protected Button deletePaletteButton;

	/** edit local palette button */
	protected Button editPaletteButton;

	/**
	 * Creates a new PapyrusPaletteCustomizerDialog
	 * 
	 * @param shell
	 *        the shell that hosts the dialog window
	 * @param customizer
	 *        the customizer used to customize the palette
	 * @param root
	 *        the root of the palette
	 */
	public PapyrusPaletteCustomizerDialog(Shell shell, PaletteCustomizer customizer, PaletteRoot root) {
		super(shell, customizer, root);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int open() {
		IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(Activator.ID);
		prefs.addPreferenceChangeListener(this);
		return super.open();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean close() {
		IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(Activator.ID);
		prefs.removePreferenceChangeListener(this);
		return super.close();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		// RS: does not call super, as the composite should not be drawn like they are in parent
		Composite mainComposite = createMainComposite(parent);

		final Control availableToolsComposite = createAvailablePalettesViewer(mainComposite);
		FormData data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.top = new FormAttachment(0, 0);
		data.bottom = new FormAttachment(100, 0);
		// data.right = new FormAttachment(40, 0);
		availableToolsComposite.setLayoutData(data);

		// final Control displayButtons = createDisplayButtons(mainComposite);
		// data = new FormData();
		// data.left = new FormAttachment(availableToolsComposite, 0);
		// // data.width = 30;
		// data.top = new FormAttachment(40, 0);
		// data.bottom = new FormAttachment(100, 0);
		// displayButtons.setLayoutData(data);

		final Label nameLabel = new Label(mainComposite, SWT.NONE);
		nameLabel.setText(Messages.Palette_Viewer);
		data = new FormData();
		data.left = new FormAttachment(availableToolsComposite, ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(0, 0);
		nameLabel.setLayoutData(data);

		// Create the tree
		Control outline = createOutline(mainComposite);
		data = new FormData();
		data.left = new FormAttachment(availableToolsComposite, ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(nameLabel, 0);
		data.bottom = new FormAttachment(100, 0);
		// data.right = new FormAttachment(90, 0);
		outline.setLayoutData(data);

		// Create the panel where the properties of the selected palette entry will
		// be shown
		Control properties = createPropertiesPanel(mainComposite);
		data = new FormData();
		data.left = new FormAttachment(outline, ITabbedPropertyConstants.HSPACE);
		data.right = new FormAttachment(100, 0);
		data.top = new FormAttachment(0, 0);
		data.bottom = new FormAttachment(100, 0);
		properties.setLayoutData(data);

		// add listeners
		ISelectionChangedListener listener = createSelectionChangedListener();
		if(listener != null) {
			availablePalettesTableViewer.addSelectionChangedListener(listener);
		}
		return mainComposite;
	}

	protected ISelectionChangedListener createSelectionChangedListener() {
		return new ISelectionChangedListener() {

			/**
			 * {@inheritDoc}
			 */
			public void selectionChanged(SelectionChangedEvent event) {
				// retrieve element selected
				Object selectedElement = ((IStructuredSelection)event.getSelection()).getFirstElement();
				if(selectedElement instanceof PapyrusPaletteService.ExtendedProviderDescriptor) {
					PapyrusPaletteService.ProviderDescriptor descriptor = (PapyrusPaletteService.ProviderDescriptor)selectedElement;
					deletePaletteButton.setEnabled(true);
					editPaletteButton.setEnabled(true);
				} else if(selectedElement instanceof PapyrusPaletteService.LocalProviderDescriptor) {
					PapyrusPaletteService.LocalProviderDescriptor descriptor = (PapyrusPaletteService.LocalProviderDescriptor)selectedElement;
					deletePaletteButton.setEnabled(true);
					editPaletteButton.setEnabled(true);
				} else {
					deletePaletteButton.setEnabled(false);
					editPaletteButton.setEnabled(false);
				}

				//				if(selectedElement instanceof PapyrusPaletteService.LocalProviderDescriptor) {
				//					deletePaletteButton.setEnabled(true);
				//					// check if the palette is in good configuration to be edited...
				//					if(PaletteUtil.areRequiredProfileApplied(getActiveSashPage(), (PapyrusPaletteService.LocalProviderDescriptor)selectedElement)) {
				//						editPaletteButton.setEnabled(true);
				//						editPaletteButton.setToolTipText(Messages.PapyrusPaletteCustomizerDialog_EditButtonTooltip_LocalPaletteSelected);
				//					} else {
				//						editPaletteButton.setEnabled(false);
				//						editPaletteButton.setToolTipText(Messages.PapyrusPaletteCustomizerDialog_EditButtonTooltip_MissingProfile);
				//					}
				//				} else {
				//					deletePaletteButton.setEnabled(false);
				//					editPaletteButton.setEnabled(false);
				//					editPaletteButton.setToolTipText(Messages.PapyrusPaletteCustomizerDialog_EditButtonTooltip_LocalPaletteNotSelected);
				//				}
			}
		};
	}

	protected void addEntry(MouseEvent e) {
		ISelection selection = availableToolsTreeViewer.getSelection();
		PaletteEntry entry = null;
		if(selection instanceof IStructuredSelection) {
			Object firstElement = ((IStructuredSelection)selection).getFirstElement();
			if(!(firstElement instanceof PaletteEntry)) {
				return;
			} else {
				entry = ((PaletteEntry)firstElement);
			}
		}
		// sets the active entry, to have the page useful for saving modification
		setActiveEntry(entry);
		// add entry to the selected container
		// retrieve the selection in the palette viewer
		PaletteEntry destContainer = getSelectedPaletteEntry();
		((PapyrusPaletteCustomizer)getCustomizer()).storePaletteState(entry);
		if(destContainer instanceof PaletteContainer) {
			PaletteContainer oldParent = entry.getParent();

			// checks if the parent is already changed. If not, must store the standard parent (the
			// one given by the factory)
			PaletteContainer stdParent = ((PapyrusPaletteCustomizer)getCustomizer()).getChangedParents().get(entry);
			if(stdParent == null) {
				((PapyrusPaletteCustomizer)getCustomizer()).getChangedParents().put(entry, entry.getParent());
			}
			oldParent.remove(entry);
			entry.setParent((PaletteContainer)destContainer);
			((PaletteContainer)destContainer).add(entry);
		} else {
			// add it to the palette root
			PaletteRoot root = (PaletteRoot)PaletteUtil.getRoot(entry);
			if(root != null) {
				PaletteContainer oldParent = entry.getParent();
				PaletteContainer stdParent = ((PapyrusPaletteCustomizer)getCustomizer()).getChangedParents().get(entry);
				if(stdParent == null) {
					((PapyrusPaletteCustomizer)getCustomizer()).getChangedParents().put(entry, stdParent);
				}
				oldParent.remove(entry);
				entry.setParent(root);
				root.add(entry);
			}
		}
	}

	/**
	 * Creates the available palettes viewer part of the dialog.
	 * 
	 * @param container
	 *        The Composite within which the viewer has to be created
	 * @return The newly created Control that has the viewer
	 */
	protected Control createAvailablePalettesViewer(Composite container) {
		// Create the Composite that will contain the available tools
		Composite composite = new Composite(container, SWT.NONE);
		composite.setFont(container.getFont());
		GridLayout layout = new GridLayout(4, false);
		layout.horizontalSpacing = 0;
		layout.verticalSpacing = 0;
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		composite.setLayout(layout);

		final Label nameLabel = new Label(composite, SWT.NONE);
		nameLabel.setText(Messages.Available_Palettes);
		GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
		nameLabel.setLayoutData(data);

		newPaletteButton = new Button(composite, SWT.NONE);
		newPaletteButton.setImage(Activator.getImage(NEW_LOCAL_DESC_IMAGE));
		newPaletteButton.setToolTipText(Messages.Dialog_Create_Palette_Tooltip);
		newPaletteButton.addMouseListener(new MouseListener() {

			/**
			 * {@inheritDoc}
			 */
			public void mouseUp(MouseEvent e) {
				createNewLocalPalette();
			}

			/**
			 * {@inheritDoc}
			 */
			public void mouseDown(MouseEvent e) {

			}

			/**
			 * {@inheritDoc}
			 */
			public void mouseDoubleClick(MouseEvent e) {

			}
		});
		data = new GridData(SWT.CENTER, SWT.CENTER, false, false);
		newPaletteButton.setLayoutData(data);

		editPaletteButton = new Button(composite, SWT.NONE);
		editPaletteButton.setImage(Activator.getImage(EDIT_LOCAL_DESC_IMAGE));
		editPaletteButton.setToolTipText(Messages.Dialog_Edit_Palette_Tooltip);
		editPaletteButton.addMouseListener(new MouseListener() {

			/**
			 * {@inheritDoc}
			 */
			public void mouseUp(MouseEvent e) {
				if(availablePalettesTableViewer == null) {
					return;
				}

				IStructuredSelection selection = (IStructuredSelection)availablePalettesTableViewer.getSelection();
				if(selection != null) {
					if((selection.getFirstElement() instanceof PapyrusPaletteService.LocalProviderDescriptor)) {
						editLocalPalette((PapyrusPaletteService.LocalProviderDescriptor)selection.getFirstElement());
					} else if(selection.getFirstElement() instanceof PapyrusPaletteService.ExtendedProviderDescriptor) {
						editExtendedPalette((PapyrusPaletteService.ExtendedProviderDescriptor)selection.getFirstElement());
					}
				}
			}

			/**
			 * {@inheritDoc}
			 */
			public void mouseDown(MouseEvent e) {

			}

			/**
			 * {@inheritDoc}
			 */
			public void mouseDoubleClick(MouseEvent e) {

			}
		});
		data = new GridData(SWT.CENTER, SWT.CENTER, false, false);
		editPaletteButton.setLayoutData(data);
		editPaletteButton.setEnabled(false);

		deletePaletteButton = new Button(composite, SWT.NONE);
		deletePaletteButton.setImage(Activator.getImage(DELETE_LOCAL_DESC_IMAGE));
		deletePaletteButton.setToolTipText(Messages.Dialog_Delete_Palette_Tooltip);
		deletePaletteButton.addMouseListener(new MouseListener() {

			/**
			 * {@inheritDoc}
			 */
			public void mouseUp(MouseEvent e) {
				restoreExtendedPaletteToDefault();
			}

			/**
			 * {@inheritDoc}
			 */
			public void mouseDown(MouseEvent e) {

			}

			/**
			 * {@inheritDoc}
			 */
			public void mouseDoubleClick(MouseEvent e) {

			}
		});
		data = new GridData(SWT.CENTER, SWT.CENTER, false, false);
		deletePaletteButton.setLayoutData(data);
		deletePaletteButton.setEnabled(false);

		availablePalettesTable = new Table(composite, SWT.BORDER | SWT.CHECK);
		availablePalettesTable.setFont(composite.getFont());
		availablePalettesTable.addSelectionListener(new SelectionListener() {

			/**
			 * {@inheritDoc}
			 */
			public void widgetSelected(SelectionEvent e) {
				if(e.detail == SWT.CHECK) {
					TableItem item = (TableItem)e.item;
					// one item was checked => display/hide the given provider
					changeProviderVisibility((PapyrusPaletteService.ProviderDescriptor)item.getData(), item.getChecked());
				}
			}

			/**
			 * {@inheritDoc}
			 */
			public void widgetDefaultSelected(SelectionEvent e) {
				// does nothing
			}
		});
		data = new GridData(GridData.FILL_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL);
		data.horizontalSpan = 4;
		data.widthHint = 185;
		// Make the tree this tall even when there is nothing in it. This will keep the
		// dialog from shrinking to an unusually small size.
		data.heightHint = 200;
		availablePalettesTable.setLayoutData(data);
		availablePalettesTableViewer = new CheckboxTableViewer(availablePalettesTable);
		AvailablePalettesCheckStateProvider availablePalettesCheckStateProvider = new AvailablePalettesCheckStateProvider();
		availablePalettesTableViewer.setCheckStateProvider(availablePalettesCheckStateProvider);
		availablePalettesTableViewer.setContentProvider(new PalettesTableContentProvider(availablePalettesTableViewer));
		providersLabelProvider = new PaletteLabelProvider(availablePalettesTableViewer);
		availablePalettesTableViewer.setLabelProvider(providersLabelProvider);
		availablePalettesTableViewer.setInput(PapyrusPaletteService.getInstance());
		availablePalettesTableViewer.addDoubleClickListener(new IDoubleClickListener() {

			public void doubleClick(DoubleClickEvent event) {
				// get selection. if local palette: open the wizard to edit this local palette
				IStructuredSelection selection = (IStructuredSelection)event.getSelection();
				if(selection.getFirstElement() instanceof LocalProviderDescriptor) {
					editLocalPalette((PapyrusPaletteService.LocalProviderDescriptor)selection.getFirstElement());
				} else if(selection.getFirstElement() instanceof ExtendedProviderDescriptor) {
					editExtendedPalette((PapyrusPaletteService.ExtendedProviderDescriptor)selection.getFirstElement());
				}
			}
		});

		return composite;
	}

	/**
	 * Launch the wizard for the palette creation
	 * 
	 * @param shell
	 *        the shell where to display the wizard
	 */
	protected void createNewLocalPalette() {
		NewLocalPaletteWizard wizard = new NewLocalPaletteWizard(getActiveSashPage(), getCustomizer());
		WizardDialog wizardDialog = new WizardDialog(new Shell(), wizard);
		wizardDialog.open();
	}

	/**
	 * Deletes the current selected local palette
	 */
	protected void deleteLocalPalette() {
		IStructuredSelection selection = (IStructuredSelection)availablePalettesTableViewer.getSelection();
		if(selection == null || !(selection.getFirstElement() instanceof PapyrusPaletteService.LocalProviderDescriptor)) {
			MessageDialog.openError(getShell(), Messages.Dialog_Not_Local_Palette_Title, Messages.Dialog_Not_Local_Palette_Message);
		} else {
			PapyrusPaletteService.LocalProviderDescriptor descriptor = ((PapyrusPaletteService.LocalProviderDescriptor)selection.getFirstElement());
			String id = descriptor.getContributionID();
			PapyrusPalettePreferences.deleteLocalPalette(id);
		}
	}

	/**
	 * Reset the current selected extended palette to the initial configurtion in the plugin
	 */
	protected void restoreExtendedPaletteToDefault() {
		IStructuredSelection selection = (IStructuredSelection)availablePalettesTableViewer.getSelection();
		if(selection == null || !(selection.getFirstElement() instanceof PapyrusPaletteService.ExtendedProviderDescriptor)) {
			MessageDialog.openError(getShell(), Messages.Dialog_Not_Local_Palette_Title, Messages.Dialog_Not_Local_Palette_Message);
		} else {
			PapyrusPaletteService.ExtendedProviderDescriptor descriptor = ((PapyrusPaletteService.ExtendedProviderDescriptor)selection.getFirstElement());
			String id = descriptor.getContributionID();
			PapyrusPalettePreferences.unregisterLocalRedefinition(id);
		}
	}

	/**
	 * Edits the current selected local palette
	 */
	protected void editLocalPalette(PapyrusPaletteService.LocalProviderDescriptor descriptor) {
		UpdateLocalPaletteWizard wizard = new UpdateLocalPaletteWizard(getActiveSashPage(), descriptor, getCustomizer());
		WizardDialog wizardDialog = new WizardDialog(new Shell(), wizard);
		wizardDialog.open();
	}

	/**
	 * @param firstElement
	 */
	protected void editExtendedPalette(ExtendedProviderDescriptor descriptor) {
		// check the file in plugin state area.
		String contributionID = descriptor.getContributionID();
		String paletteRedefinition = PapyrusPalettePreferences.getPaletteRedefinition(contributionID);

		if(paletteRedefinition == null) {
			// create a local redefinition of this palette contribution
			PapyrusPalettePreferences.createPaletteRedefinition(descriptor);
		}


		UpdateExtendedPaletteWizard wizard = new UpdateExtendedPaletteWizard(getActiveSashPage(), descriptor, getCustomizer());
		WizardDialog wizardDialog = new WizardDialog(new Shell(), wizard);
		wizardDialog.open();
	}


	/**
	 * Changes the visibility of the given provider
	 * 
	 * @param descriptor
	 *        the provider to hide/show
	 * @param isChecked
	 *        <code>true</code> if the descriptor should be visible
	 */
	protected void changeProviderVisibility(PapyrusPaletteService.ProviderDescriptor descriptor, boolean isChecked) {
		PapyrusPalettePreferences.changePaletteVisibility(descriptor.getContributionID(), getActiveSashPage().getClass().getName(), isChecked);
	}

	/**
	 * Creates the main composite for the dialog area
	 * 
	 * @param parent
	 *        the parent of the createrd composite
	 * @return the newly created Composite
	 */
	protected Composite createMainComposite(Composite parent) {
		// dialog window
		// create a composite with standard margins and spacing
		Composite composite = new Composite(parent, SWT.NONE);
		FormLayout layout = new FormLayout();
		layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
		layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
		composite.setLayout(layout);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		applyDialogFont(composite);
		return composite;
	}

	/**
	 * Content provider for available tools viewer
	 */
	public class PalettesTableContentProvider implements IStructuredContentProvider {

		/** the palette root */
		private PapyrusPaletteService paletteService;

		/** tree viewer to fill */
		private final TableViewer viewer;

		/**
		 * Constructor
		 * 
		 * @param tableViewer
		 *        The TableViewer whose ContentProvider this PaletteTreeProvider is
		 */
		public PalettesTableContentProvider(TableViewer tableViewer) {
			this.viewer = tableViewer;
		}

		/**
		 * {@inheritDoc}
		 */
		public void dispose() {
			paletteService = null;
		}

		/**
		 * {@inheritDoc}
		 */
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			if(newInput != null) {
				paletteService = (PapyrusPaletteService)newInput;
			}
		}

		/**
		 * {@inheritDoc}
		 */
		public Object[] getElements(Object inputElement) {
			if(inputElement instanceof PapyrusPaletteService) {
				List<PapyrusPaletteService.ProviderDescriptor> providers = ((PapyrusPaletteService)inputElement).getContributingProviders(getActiveSashPage(), getPaletteRoot());

				return providers.toArray();
			}
			return null;
		}
	}

	/**
	 * Returns the current active sash page
	 * 
	 * @return the current active sash page
	 */
	protected IEditorPart getActiveSashPage() {
		// Lookup ServiceRegistry
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IEditorPart editorPart = page.getActiveEditor();
		assert editorPart != null;
		ISashWindowsContainer sashWindowsContainer = (ISashWindowsContainer)editorPart.getAdapter(ISashWindowsContainer.class);
		if(sashWindowsContainer != null) {
			return sashWindowsContainer.getActiveEditor();
		}
		return null;
	}

	/**
	 * provider in charge of the check boxes in the available palettes table viewer
	 */
	protected class AvailablePalettesCheckStateProvider implements ICheckStateProvider {

		/**
		 * {@inheritDoc}
		 */
		public boolean isChecked(Object element) {
			if(element instanceof PapyrusPaletteService.ProviderDescriptor) {
				return !PapyrusPalettePreferences.getHiddenPalettes(getActiveSashPage()).contains(((PapyrusPaletteService.ProviderDescriptor)element).getContributionID());
			}
			return false;
		}

		/**
		 * {@inheritDoc}
		 */
		public boolean isGrayed(Object element) {
			return false;
		}

	}

	/**
	 * Label provider for available tools viewer
	 */
	protected class PaletteLabelProvider implements ILabelProvider {

		/**
		 * Creates a new PaletteLabelProvider.
		 * 
		 * @param viewer
		 *        the table viewer where the labels are displayed
		 */
		public PaletteLabelProvider(TableViewer viewer) {
		}

		private Map<ImageDescriptor, Image> imageCache = new HashMap<ImageDescriptor, Image>();

		/**
		 * {@inheritDoc}
		 */
		public Image getImage(Object element) {
			if(element instanceof PapyrusPaletteService.LocalProviderDescriptor) {
				return Activator.getImage(LOCAL_DESCRIPTOR);
			} else if(element instanceof PapyrusPaletteService.ExtendedProviderDescriptor) {
				// icon should be decorated if it is already defined in a local way or not.
				return Activator.getImage(EXTENDED_PLUGIN_DESCRIPTOR);
			} else if(element instanceof PapyrusPaletteService.ProviderDescriptor) {
				return Activator.getImage(PLUGIN_DESCRIPTOR);
			}
			return Activator.getImage(Activator.DEFAULT_IMAGE);
		}

		/**
		 * {@inheritDoc}
		 */
		public String getText(Object element) {
			if(element instanceof PapyrusPaletteService.ProviderDescriptor) {
				return ((PapyrusPaletteService.ProviderDescriptor)element).getContributionName();
			}
			return "<undefined>";
		}

		/**
		 * {@inheritDoc}
		 */
		public void addListener(ILabelProviderListener listener) {
		}

		/**
		 * {@inheritDoc}
		 */
		public void dispose() {
			Iterator<Image> images = imageCache.values().iterator();
			while(images.hasNext()) {
				images.next().dispose();
			}
			imageCache = null;
		}

		/**
		 * {@inheritDoc}
		 */
		public boolean isLabelProperty(Object element, String property) {
			return false;
		}

		/**
		 * {@inheritDoc}
		 */
		public void removeListener(ILabelProviderListener listener) {
		}

	}

	/**
	 * {@inheritDoc}
	 */
	public void preferenceChange(PreferenceChangeEvent event) {
		String id = event.getKey();
		if(PapyrusPalettePreferences.PALETTE_CUSTOMIZATIONS_ID.equals(id) || PapyrusPalettePreferences.PALETTE_LOCAL_DEFINITIONS.equals(id)) {
			// refresh available palette table viewer
			availablePalettesTableViewer.setInput(PapyrusPaletteService.getInstance());
		}

	}

}

Back to the top