Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cf83e11fa2612522775e458653d5875845af1852 (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
/*******************************************************************************
 * Copyright (c) 2010, 2015 Ericsson
 *
 * 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:
 *   Patrick Tasse - Initial API and implementation
 *   Bernd Hufmann - Updated to use RGB for the tick color
 *******************************************************************************/

package org.eclipse.tracecompass.tmf.ui.views.colors;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.IPath;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.tracecompass.internal.tmf.ui.Activator;
import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;

/**
 * Static class for managing color settings.
 *
 * @version 1.0
 * @author Patrick Tasse
 *
 */
public class ColorSettingsManager {

    // The color settings file name
    private static final String COLOR_SETTINGS_FILE_NAME = "color_settings.xml"; //$NON-NLS-1$

    // The path for the color settings file
    private static final String COLOR_SETTINGS_PATH_NAME =
        Activator.getDefault().getStateLocation().addTrailingSeparator().append(COLOR_SETTINGS_FILE_NAME).toString();

    /*
     * Legacy path to the XML definitions file (in Linux Tools)
     *  TODO Remove once we feel the transition phase is over.
     */
    private static final IPath COLOR_SETTINGS_PATH_NAME_LEGACY =
            Activator.getDefault().getStateLocation().removeLastSegments(1)
                    .append("org.eclipse.linuxtools.tmf.ui") //$NON-NLS-1$
                    .append(COLOR_SETTINGS_FILE_NAME);

    // The default color setting
    private static final ColorSetting DEFAULT_COLOR_SETTING = new ColorSetting(
            Display.getDefault().getSystemColor(SWT.COLOR_LIST_FOREGROUND).getRGB(),
            Display.getDefault().getSystemColor(SWT.COLOR_LIST_BACKGROUND).getRGB(),
            Display.getDefault().getSystemColor(SWT.COLOR_LIST_FOREGROUND).getRGB(),
            null);

    /**
     * Special value for priority if unknown.
     */
    public static final int PRIORITY_NONE = Integer.MAX_VALUE;

    // The stored color settings
    private static ColorSetting[] fColorSettings;

    static {
        File defaultFile = new File(COLOR_SETTINGS_PATH_NAME);
        /*
         * If there is no file at the expected location, check the legacy
         * location instead.
         */
        if (!defaultFile.exists()) {
            File legacyFileCore = COLOR_SETTINGS_PATH_NAME_LEGACY.toFile();
            if (legacyFileCore.exists()) {
                ColorSetting[] colorSettings = ColorSettingsXML.load(COLOR_SETTINGS_PATH_NAME_LEGACY.toString());
                if (colorSettings != null) {
                    ColorSettingsXML.save(COLOR_SETTINGS_PATH_NAME, colorSettings);
                }
            }
        }
        fColorSettings = ColorSettingsXML.load(COLOR_SETTINGS_PATH_NAME);
    }

    // The listener list
    private static List<IColorSettingsListener> fListeners = new ArrayList<>();

    /**
     * Returns an array of color settings.
     *
     * @return an array of color settings.
     */
    public static ColorSetting[] getColorSettings() {
        return (fColorSettings != null) ? Arrays.copyOf(fColorSettings, fColorSettings.length) : null;
    }

    /**
     * Sets the array of color settings.
     *
     * @param colorSettings A array of color settings to set
     */
    public static void setColorSettings(ColorSetting[] colorSettings) {
        fColorSettings = (colorSettings != null) ? Arrays.copyOf(colorSettings, colorSettings.length) : null;
        if (fColorSettings != null) {
            ColorSettingsXML.save(COLOR_SETTINGS_PATH_NAME, fColorSettings);
        }
        fireColorSettingsChanged();
    }

        /**
     * Gets the color settings that matches the filter for given event.
     *
     * @param event
     *            The event to check
     *
     * @return color settings defined for filter if found else default color
     *         settings
     */
    public static ColorSetting getColorSetting(ITmfEvent event) {
        for (int i = 0; i < fColorSettings.length; i++) {
            ColorSetting colorSetting = fColorSettings[i];
            if (colorSetting.getFilter() != null && colorSetting.getFilter().matches(event)) {
                return colorSetting;
            }
        }
        return DEFAULT_COLOR_SETTING;
    }

    /**
     * Gets the color settings priority for the given event.
     *
     * @param event A event the event to check
     * @return the priority defined for the filter else PRIORITY_NONE
     */
    public static int getColorSettingPriority(ITmfEvent event) {
        for (int i = 0; i < fColorSettings.length; i++) {
            ColorSetting colorSetting = fColorSettings[i];
            if (colorSetting.getFilter() != null && colorSetting.getFilter().matches(event)) {
                return i;
            }
        }
        return PRIORITY_NONE;
    }

    /**
     * Returns the color settings based the priority.
     *
     * @param priority A priority (index) of color settings
     * @return the color settings defined for the priority else default color settings
     */
    public static ColorSetting getColorSetting(int priority) {
        if (priority < fColorSettings.length) {
            return fColorSettings[priority];
        }
        return DEFAULT_COLOR_SETTING;
    }

    /**
     * Adds a color settings listener.
     *
     * @param listener A listener to add.
     */
    public static void addColorSettingsListener(IColorSettingsListener listener) {
        if (! fListeners.contains(listener)) {
            fListeners.add(listener);
        }
    }

    /**
     * Removes a color settings listener.
     *
     * @param listener A listener to remove.
     */
    public static void removeColorSettingsListener(IColorSettingsListener listener) {
        fListeners.remove(listener);
    }

    // Notify listeners
    private static void fireColorSettingsChanged() {
        for (IColorSettingsListener listener : fListeners) {
            listener.colorSettingsChanged(fColorSettings);
        }
    }
}

Back to the top