[431785] Correct synchronization mechanism
[EMFCompare2.git] / plugins / org.eclipse.emf.compare.rcp.ui / src / org / eclipse / emf / compare / rcp / ui / internal / structuremergeviewer / actions / FilterAction.java
blob4a90b0c417d2dc6059c300d2c123624e251c24ad
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Obeo.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Obeo - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.emf.compare.rcp.ui.internal.structuremergeviewer.actions;
13 import com.google.common.collect.Sets;
15 import java.util.Set;
17 import org.eclipse.core.runtime.preferences.InstanceScope;
18 import org.eclipse.emf.compare.rcp.ui.EMFCompareRCPUIPlugin;
19 import org.eclipse.emf.compare.rcp.ui.internal.EMFCompareRCPUIMessages;
20 import org.eclipse.emf.compare.rcp.ui.internal.preferences.FiltersPreferencePage;
21 import org.eclipse.emf.compare.rcp.ui.internal.structuremergeviewer.actions.ui.SynchronizerDialog;
22 import org.eclipse.emf.compare.rcp.ui.internal.structuremergeviewer.filters.StructureMergeViewerFilter;
23 import org.eclipse.emf.compare.rcp.ui.internal.structuremergeviewer.filters.impl.DifferenceFilterManager;
24 import org.eclipse.emf.compare.rcp.ui.structuremergeviewer.filters.IDifferenceFilter;
25 import org.eclipse.jface.action.Action;
26 import org.eclipse.jface.action.IAction;
27 import org.eclipse.jface.dialogs.IDialogConstants;
28 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
29 import org.eclipse.swt.widgets.Event;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.ui.preferences.ScopedPreferenceStore;
32 import org.osgi.service.prefs.Preferences;
34 /**
35 * These will be the actual actions displayed in the filter menu. Their sole purpose is to provide a Predicate
36 * to the structure viewer's filter.
37 * <p>
38 * Do note that each distinct {@link FilterAction} in the {@link FilterActionMenu filter menu} is considered
39 * as an "exclude" filter, and that they are OR'ed together (thus, any element must <b>not</b> meet the
40 * selected filters' criteria in order to be displayed).
41 * </p>
43 * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
44 * @since 4.0
46 public class FilterAction extends Action {
48 /** The filter associated with this action. */
49 private final IDifferenceFilter filter;
51 /** The Filter that will be modified by the action. */
52 private final StructureMergeViewerFilter structureMergeViewerFilter;
54 /** Preferences holding the value of the synchronization behavior of filters. */
55 private final Preferences preferences;
57 /** {@link DifferenceFilterManager}. */
58 private final DifferenceFilterManager filterManager;
60 /**
61 * The "default" constructor for this action.
63 * @param text
64 * Will be used as the action's tooltip.
65 * @param structureMergeViewerFilter
66 * The viewer filter that this action will need to update.
67 * @param filter
68 * The filter associated with this action.
70 public FilterAction(String text, StructureMergeViewerFilter structureMergeViewerFilter,
71 IDifferenceFilter filter) {
72 super(text, IAction.AS_CHECK_BOX);
73 this.structureMergeViewerFilter = structureMergeViewerFilter;
74 this.filter = filter;
75 this.preferences = EMFCompareRCPUIPlugin.getDefault().getEMFCompareUIPreferences();
76 this.filterManager = EMFCompareRCPUIPlugin.getDefault().getDifferenceFilterManager();
79 /**
80 * {@inheritDoc}
82 @Override
83 public void runWithEvent(Event event) {
84 if (isChecked()) {
85 structureMergeViewerFilter.addFilter(filter);
86 } else {
87 structureMergeViewerFilter.removeFilter(filter);
89 handleSynchronization(event);
92 /**
93 * Handle synchronization with {@link DifferenceFilterManager}.
95 * @param event
96 * Event.
98 private void handleSynchronization(Event event) {
99 String sync = preferences.get(FiltersPreferencePage.SYNCHRONIZATION_BEHAVIOR,
100 MessageDialogWithToggle.PROMPT);
101 final Shell shell = event.display.getActiveShell();
102 if (MessageDialogWithToggle.PROMPT.equals(sync) && shell != null) {
103 shell.getDisplay().asyncExec(new SynchronizationBehaviorRunnable(shell));
104 } else if (MessageDialogWithToggle.ALWAYS.equals(sync)) {
105 synchonizeFilters();
110 * Synchronizes UI filter selection with the preferences.
112 private void synchonizeFilters() {
113 Set<IDifferenceFilter> byDefaultFilters = Sets.newLinkedHashSet(filterManager
114 .getCurrentByDefaultFilters());
115 //Add newly activated filters
116 for (IDifferenceFilter activeFilter : structureMergeViewerFilter.getSelectedDifferenceFilters()) {
117 byDefaultFilters.add(activeFilter);
119 //Remove deactivated filters
120 for (IDifferenceFilter toDeactivateFilter : structureMergeViewerFilter
121 .getUnSelectedDifferenceFilters()) {
122 byDefaultFilters.remove(toDeactivateFilter);
124 filterManager.setCurrentByDefaultFilters(byDefaultFilters);
129 * Runnable in charge of synchronizing selection in UI with the {@link DifferenceFilterManager}.
131 * @author <a href="mailto:arthur.daussy@obeo.fr">Arthur Daussy</a>
133 private final class SynchronizationBehaviorRunnable implements Runnable {
134 /** Shell of this runnable. */
135 private final Shell shell;
138 * Constructor.
140 * @param shell
141 * Shell for this runnable.
143 private SynchronizationBehaviorRunnable(Shell shell) {
144 this.shell = shell;
148 * {@inheritDoc} Does not use InstanceScope#Instance for compatibility issues with Helios.
150 @SuppressWarnings("deprecation")
151 public void run() {
152 MessageDialogWithToggle dialog = new SynchronizerDialog(shell, EMFCompareRCPUIMessages
153 .getString("FilterAction.synchronization.dialog.title"), //$NON-NLS-1$
154 EMFCompareRCPUIMessages.getString("FilterAction.synchronization.dialog.message"), //$NON-NLS-1$
155 FiltersPreferencePage.PAGE_ID);
157 dialog.setPrefKey(FiltersPreferencePage.SYNCHRONIZATION_BEHAVIOR);
158 dialog.setPrefStore(new ScopedPreferenceStore(new InstanceScope(),
159 EMFCompareRCPUIPlugin.PLUGIN_ID));
160 if (dialog.open() == IDialogConstants.YES_ID) {
161 synchonizeFilters();