Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b27aaafc54aa432761eb686ab027461ea73f1e25 (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
/*******************************************************************************
 * Copyright (c) 2008, 2009 THALES GLOBAL SERVICES.
 * 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:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.sirius.diagram.ui.tools.internal.views.providers.filters;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.sirius.diagram.DDiagram;
import org.eclipse.sirius.diagram.DiagramPackage;
import org.eclipse.sirius.diagram.description.filter.FilterDescription;
import org.eclipse.ui.PlatformUI;

/**
 * An adapter to listen layer activation change.
 * 
 * @author mchauvin
 */
public class FiltersActivationAdapter extends AdapterImpl {

    /** The structured viewer to update. */
    private StructuredViewer viewer;

    /**
     * Set the viewer.
     * 
     * @param viewer
     *            the viewer to update when the model change
     */
    public void setViewer(final Viewer viewer) {
        if (viewer instanceof StructuredViewer) {
            this.viewer = (StructuredViewer) viewer;
        }
    }

    private void update(final DDiagram diagram, final FilterDescription filter, final boolean activate) {
        PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
            public void run() {
                if (viewer != null) {
                    viewer.update(filter, null);
                }
            }
        });
    }

    private void update(final DDiagram notifier, final Collection<FilterDescription> filters, final boolean b) {
        PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
            public void run() {
                if (viewer != null && filters != null) {
                    for (FilterDescription filter : filters) {
                        viewer.update(filter, null);
                    }
                }
            }
        });
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.eclipse.emf.common.notify.impl.AdapterImpl#notifyChanged(org.eclipse.emf.common.notify.Notification)
     */
    @Override
    public void notifyChanged(final Notification msg) {
        final Object notifier = msg.getNotifier();
        if (notifier instanceof DDiagram) {
            final int featureID = msg.getFeatureID(DDiagram.class);
            if (featureID == DiagramPackage.DDIAGRAM__ACTIVATED_FILTERS) {

                switch (msg.getEventType()) {

                case Notification.ADD:
                    final FilterDescription filterToAdd = (FilterDescription) msg.getNewValue();
                    update((DDiagram) notifier, filterToAdd, true);
                    break;
                case Notification.ADD_MANY:
                    @SuppressWarnings("unchecked")
                    final Collection<FilterDescription> filtersToAdd = (Collection<FilterDescription>) msg.getNewValue();
                    update((DDiagram) notifier, filtersToAdd, true);
                    break;
                case Notification.REMOVE:
                    final FilterDescription filterToRemove = (FilterDescription) msg.getOldValue();
                    update((DDiagram) notifier, filterToRemove, false);
                    break;
                case Notification.REMOVE_MANY:
                    @SuppressWarnings("unchecked")
                    final Collection<FilterDescription> filtersToRemove = (Collection<FilterDescription>) msg.getOldValue();
                    update((DDiagram) notifier, filtersToRemove, false);
                    break;
                default:
                    break;
                }
            }
        }
    }
}

Back to the top