1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Patrick Tasse - Initial API and implementation
11 * Bernd Hufmann - Updated to use RGB for the tick color
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.views
.colors
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Arrays
;
19 import java
.util
.List
;
21 import org
.eclipse
.core
.runtime
.IPath
;
22 import org
.eclipse
.swt
.SWT
;
23 import org
.eclipse
.swt
.widgets
.Display
;
24 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
28 * Static class for managing color settings.
31 * @author Patrick Tasse
34 public class ColorSettingsManager
{
36 // The color settings file name
37 private static final String COLOR_SETTINGS_FILE_NAME
= "color_settings.xml"; //$NON-NLS-1$
39 // The path for the color settings file
40 private static final String COLOR_SETTINGS_PATH_NAME
=
41 Activator
.getDefault().getStateLocation().addTrailingSeparator().append(COLOR_SETTINGS_FILE_NAME
).toString();
44 * Legacy path to the XML definitions file (in Linux Tools)
45 * TODO Remove once we feel the transition phase is over.
47 private static final IPath COLOR_SETTINGS_PATH_NAME_LEGACY
=
48 Activator
.getDefault().getStateLocation().removeLastSegments(1)
49 .append("org.eclipse.linuxtools.tmf.ui") //$NON-NLS-1$
50 .append(COLOR_SETTINGS_FILE_NAME
);
52 // The default color setting
53 private static final ColorSetting DEFAULT_COLOR_SETTING
= new ColorSetting(
54 Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_FOREGROUND
).getRGB(),
55 Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_BACKGROUND
).getRGB(),
56 Display
.getDefault().getSystemColor(SWT
.COLOR_LIST_FOREGROUND
).getRGB(),
60 * Special value for priority if unknown.
62 public static final int PRIORITY_NONE
= Integer
.MAX_VALUE
;
64 // The stored color settings
65 private static ColorSetting
[] fColorSettings
;
68 File defaultFile
= new File(COLOR_SETTINGS_PATH_NAME
);
70 * If there is no file at the expected location, check the legacy
73 if (!defaultFile
.exists()) {
74 File legacyFileCore
= COLOR_SETTINGS_PATH_NAME_LEGACY
.toFile();
75 if (legacyFileCore
.exists()) {
76 ColorSetting
[] colorSettings
= ColorSettingsXML
.load(COLOR_SETTINGS_PATH_NAME_LEGACY
.toString());
77 if (colorSettings
!= null) {
78 ColorSettingsXML
.save(COLOR_SETTINGS_PATH_NAME
, colorSettings
);
82 fColorSettings
= ColorSettingsXML
.load(COLOR_SETTINGS_PATH_NAME
);
86 private static List
<IColorSettingsListener
> fListeners
= new ArrayList
<>();
89 * Returns an array of color settings.
91 * @return an array of color settings.
93 public static ColorSetting
[] getColorSettings() {
94 return (fColorSettings
!= null) ? Arrays
.copyOf(fColorSettings
, fColorSettings
.length
) : null;
98 * Sets the array of color settings.
100 * @param colorSettings A array of color settings to set
102 public static void setColorSettings(ColorSetting
[] colorSettings
) {
103 fColorSettings
= (colorSettings
!= null) ? Arrays
.copyOf(colorSettings
, colorSettings
.length
) : null;
104 if (fColorSettings
!= null) {
105 ColorSettingsXML
.save(COLOR_SETTINGS_PATH_NAME
, fColorSettings
);
107 fireColorSettingsChanged();
111 * Gets the color settings that matches the filter for given event.
116 * @return color settings defined for filter if found else default color
119 public static ColorSetting
getColorSetting(ITmfEvent event
) {
120 for (int i
= 0; i
< fColorSettings
.length
; i
++) {
121 ColorSetting colorSetting
= fColorSettings
[i
];
122 if (colorSetting
.getFilter() != null && colorSetting
.getFilter().matches(event
)) {
126 return DEFAULT_COLOR_SETTING
;
130 * Gets the color settings priority for the given event.
132 * @param event A event the event to check
133 * @return the priority defined for the filter else PRIORITY_NONE
135 public static int getColorSettingPriority(ITmfEvent event
) {
136 for (int i
= 0; i
< fColorSettings
.length
; i
++) {
137 ColorSetting colorSetting
= fColorSettings
[i
];
138 if (colorSetting
.getFilter() != null && colorSetting
.getFilter().matches(event
)) {
142 return PRIORITY_NONE
;
146 * Returns the color settings based the priority.
148 * @param priority A priority (index) of color settings
149 * @return the color settings defined for the priority else default color settings
151 public static ColorSetting
getColorSetting(int priority
) {
152 if (priority
< fColorSettings
.length
) {
153 return fColorSettings
[priority
];
155 return DEFAULT_COLOR_SETTING
;
159 * Adds a color settings listener.
161 * @param listener A listener to add.
163 public static void addColorSettingsListener(IColorSettingsListener listener
) {
164 if (! fListeners
.contains(listener
)) {
165 fListeners
.add(listener
);
170 * Removes a color settings listener.
172 * @param listener A listener to remove.
174 public static void removeColorSettingsListener(IColorSettingsListener listener
) {
175 fListeners
.remove(listener
);
179 private static void fireColorSettingsChanged() {
180 for (IColorSettingsListener listener
: fListeners
) {
181 listener
.colorSettingsChanged(fColorSettings
);