Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: befc6d9d3e6ecdc86b278e6a194d3728648c26a1 (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
/*******************************************************************************
 * Copyright (c) 2018 THALES GLOBAL SERVICES.
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.sirius.diagram.ui.tools.internal.menu;

import java.net.URISyntaxException;
import java.text.MessageFormat;
import java.util.Optional;

import org.eclipse.sirius.diagram.ui.provider.Messages;

/**
 * Class inspired from {@link java.net.URI} but simpler. It is used to define the location of an element.
 * 
 * @author <a href="mailto:laurent.redor@obeo.fr">Laurent Redor</a>
 */
public class LocationURI {
    /**
     * The scheme to use to locate the element in the contextual menu.
     */
    public static final String MENU_SCHEME = "menu"; //$NON-NLS-1$

    /**
     * The scheme to use to locate the element in the tabbar.
     */
    public static final String TABBAR_SCHEME = "tabbar"; //$NON-NLS-1$

    /**
     * The separator used to define 2 locations in one LocationURI (one for menu scheme and one for tabbar scheme).
     */
    public static final String LOCATION_SEPARATOR = "|"; //$NON-NLS-1$

    /**
     * The separator after the scheme and before the id of the menu.
     */
    public static final String SEPARATOR = ":"; //$NON-NLS-1$

    /**
     * Id used to represent the global contextual menu.
     */
    public static final String ROOT_MENU_ID = "root"; //$NON-NLS-1$

    /**
     * The string form of this URI.
     */
    private String string;

    /**
     * The menuId, if any, extracted from the string.
     */
    private String menuId;

    /**
     * The tabbarId, if any, extracted from the string.
     */
    private String tabbarId;

    /**
     * Constructs an URI by parsing the given string.
     * 
     * @param locationURI
     *            The string to be parsed into a URI
     * @throws URISyntaxException
     *             In case of a wrong syntax of the locationURI
     */
    public LocationURI(String locationURI) throws URISyntaxException {
        this.string = locationURI;
        parse();
    }

    /**
     * Return the menu id of this {@link LocationURI} if this {@link LocationURI} concerns the menu.
     * 
     * @return an optional menu id
     */
    public Optional<String> getMenuId() {
        return Optional.ofNullable(menuId);
    }

    /**
     * Return the tabbar id of this {@link LocationURI} if this {@link LocationURI} concerns the tabbar.
     * 
     * @return an optional tabbar id
     */

    public Optional<String> getTabbarId() {
        return Optional.ofNullable(tabbarId);
    }

    private void parse() throws URISyntaxException {
        if (string.trim().length() == 0) {
            throw new URISyntaxException(string, MessageFormat.format(Messages.LocationURI_ParsePb_Blank, MENU_SCHEME, TABBAR_SCHEME));
        }
        String[] locationsURI = string.split("\\" + LOCATION_SEPARATOR); //$NON-NLS-1$
        if (locationsURI.length > 2) {
            throw new URISyntaxException(string, MessageFormat.format(Messages.LocationURI_ParsePb_MoreThanTwoLocations, LOCATION_SEPARATOR, locationsURI.length));
        }
        for (int i = 0; i < locationsURI.length; i++) {
            String trimmedURI = locationsURI[i].trim();
            if (trimmedURI.startsWith(MENU_SCHEME)) {
                parseMenuURI(trimmedURI);
            } else if (trimmedURI.startsWith(TABBAR_SCHEME)) {
                parseTabbarURI(trimmedURI);
            } else {
                throw new URISyntaxException(string, MessageFormat.format(Messages.LocationURI_ParsePb_WrongScheme, MENU_SCHEME, TABBAR_SCHEME));
            }
        }
    }

    private void parseMenuURI(String menuURI) throws URISyntaxException {
        if (menuId != null) {
            throw new URISyntaxException(string, MessageFormat.format(Messages.LocationURI_ParsePb_OnlyOneLocationURIPerScheme, MENU_SCHEME));
        }
        if (menuURI.substring(MENU_SCHEME.length()).trim().startsWith(SEPARATOR)) {
            String id = menuURI.substring(MENU_SCHEME.length()).trim().substring(SEPARATOR.length()).trim();
            if (id.length() == 0) {
                throw new URISyntaxException(string, MessageFormat.format(Messages.LocationURI_ParsePb_NoId, MENU_SCHEME + SEPARATOR));
            }
            menuId = id;
        } else if (menuURI.length() == MENU_SCHEME.length()) {
            menuId = ROOT_MENU_ID;
        } else {
            throw new URISyntaxException(string, MessageFormat.format(Messages.LocationURI_ParsePb_WrongFormat, MENU_SCHEME + SEPARATOR + "menuId", menuURI)); //$NON-NLS-1$
        }
    }

    private void parseTabbarURI(String tabbarURI) throws URISyntaxException {
        if (tabbarId != null) {
            throw new URISyntaxException(string, MessageFormat.format(Messages.LocationURI_ParsePb_OnlyOneLocationURIPerScheme, TABBAR_SCHEME));
        }
        if (tabbarURI.substring(TABBAR_SCHEME.length()).trim().startsWith(SEPARATOR)) {
            String id = tabbarURI.substring(TABBAR_SCHEME.length()).trim().substring(SEPARATOR.length()).trim();
            if (id.length() == 0) {
                throw new URISyntaxException(string, MessageFormat.format(Messages.LocationURI_ParsePb_NoId, TABBAR_SCHEME + SEPARATOR));
            }
            tabbarId = id;
        } else {
            throw new URISyntaxException(string, MessageFormat.format(Messages.LocationURI_ParsePb_WrongFormat, TABBAR_SCHEME + SEPARATOR + "tabbarId", tabbarURI)); //$NON-NLS-1$
        }
    }
}

Back to the top