Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b06e6c52c2f5bf8e8ae9c9e492ebb649afd98c00 (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
/*******************************************************************************
 * Copyright (c) 2015 Obeo.
 * 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.editor.tabbar;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.sirius.common.tools.api.util.EclipseUtil;
import org.eclipse.sirius.common.tools.api.util.StringUtil;
import org.eclipse.sirius.diagram.ui.provider.DiagramUIPlugin;
import org.eclipse.sirius.diagram.ui.provider.Messages;
import org.eclipse.sirius.diagram.ui.tools.api.editor.tabbar.ITabbarContributor;
import org.eclipse.sirius.viewpoint.SiriusPlugin;

/**
 * This implementation of {@link ITabbarContributorProvider} looks for extension
 * in the extension point.
 * 
 * @author Florian Barbin
 *
 */
public class ExtensionPointTabbarContributorProvider implements ITabbarContributorProvider {

    /**
     * The tabbar extension point ID.
     */
    public static final String EXTENSION_ID = "org.eclipse.sirius.diagram.ui.tabbarContributor"; //$NON-NLS-1$

    private List<ITabbarContributor> loadedExtensions;

    @Override
    public boolean hasContributor() {
        if (loadedExtensions == null) {
            loadExtensions();
        }
        return !loadedExtensions.isEmpty();
    }

    @Override
    public ITabbarContributor getContributor() {
        return getContributor(null);
    }

    @Override
    public ITabbarContributor getContributor(ISelection selection) {
        if (loadedExtensions == null) {
            loadExtensions();
        }
        for (ITabbarContributor extension : loadedExtensions) {
            if (extension.accept(selection)) {
                return extension;
            }
        }
        return null;
    }

    private void loadExtensions() {
        this.loadedExtensions = new ArrayList<ITabbarContributor>();

        IConfigurationElement[] configurationElements = EclipseUtil.getConfigurationElementsFor(EXTENSION_ID);
        for (IConfigurationElement configurationElement : configurationElements) {
            try {
                Object contribution = configurationElement.createExecutableExtension("class"); //$NON-NLS-1$
                if (contribution instanceof ITabbarContributor) {
                    loadedExtensions.add((ITabbarContributor) contribution);
                }
            } catch (CoreException e) {
                String extensionId = null;
                Object parent = configurationElement.getParent();
                if (parent instanceof IExtension) {
                    extensionId = ((IExtension) parent).getUniqueIdentifier();
                }
                String message = StringUtil.EMPTY_STRING;
                if (extensionId != null) {
                    message = MessageFormat.format(Messages.DefaultTabbarContributorProvider_contributionError_withId, extensionId);
                } else {
                    message = Messages.DefaultTabbarContributorProvider_contributionError;
                }
                SiriusPlugin.getDefault().getLog().log(new Status(IStatus.WARNING, DiagramUIPlugin.ID, message, e));
            }
        }
    }
}

Back to the top