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
.tracecompass
.internal
.tmf
.ui
.Activator
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.ITmfFilter
;
27 * Static class for managing color settings.
30 * @author Patrick Tasse
33 public class ColorSettingsManager
{
35 // The color settings file name
36 private static final String COLOR_SETTINGS_FILE_NAME
= "color_settings.xml"; //$NON-NLS-1$
38 // The path for the color settings file
39 private static final String COLOR_SETTINGS_PATH_NAME
=
40 Activator
.getDefault().getStateLocation().addTrailingSeparator().append(COLOR_SETTINGS_FILE_NAME
).toString();
43 * Legacy path to the XML definitions file (in Linux Tools)
44 * TODO Remove once we feel the transition phase is over.
46 private static final IPath COLOR_SETTINGS_PATH_NAME_LEGACY
=
47 Activator
.getDefault().getStateLocation().removeLastSegments(1)
48 .append("org.eclipse.linuxtools.tmf.ui") //$NON-NLS-1$
49 .append(COLOR_SETTINGS_FILE_NAME
);
51 // The default color setting
52 private static final ColorSetting DEFAULT_COLOR_SETTING
= new ColorSetting(
59 * Special value for priority if unknown.
61 public static final int PRIORITY_NONE
= Integer
.MAX_VALUE
;
63 // The stored color settings
64 private static ColorSetting
[] fColorSettings
;
67 File defaultFile
= new File(COLOR_SETTINGS_PATH_NAME
);
69 * If there is no file at the expected location, check the legacy
72 if (!defaultFile
.exists()) {
73 File legacyFileCore
= COLOR_SETTINGS_PATH_NAME_LEGACY
.toFile();
74 if (legacyFileCore
.exists()) {
75 ColorSetting
[] colorSettings
= ColorSettingsXML
.load(COLOR_SETTINGS_PATH_NAME_LEGACY
.toString());
76 if (colorSettings
!= null) {
77 ColorSettingsXML
.save(COLOR_SETTINGS_PATH_NAME
, colorSettings
);
81 fColorSettings
= ColorSettingsXML
.load(COLOR_SETTINGS_PATH_NAME
);
85 private static List
<IColorSettingsListener
> fListeners
= new ArrayList
<>();
88 * Returns an array of color settings.
90 * @return an array of color settings.
92 public static ColorSetting
[] getColorSettings() {
93 return (fColorSettings
!= null) ? Arrays
.copyOf(fColorSettings
, fColorSettings
.length
) : null;
97 * Sets the array of color settings.
99 * @param colorSettings A array of color settings to set
101 public static void setColorSettings(ColorSetting
[] colorSettings
) {
102 fColorSettings
= (colorSettings
!= null) ? Arrays
.copyOf(colorSettings
, colorSettings
.length
) : null;
103 if (fColorSettings
!= null) {
104 ColorSettingsXML
.save(COLOR_SETTINGS_PATH_NAME
, fColorSettings
);
106 fireColorSettingsChanged();
110 * Gets the color settings that matches the filter for given event.
115 * @return color settings defined for filter if found else default color
118 public static ColorSetting
getColorSetting(ITmfEvent event
) {
119 for (int i
= 0; i
< fColorSettings
.length
; i
++) {
120 ColorSetting colorSetting
= fColorSettings
[i
];
121 ITmfFilter filter
= colorSetting
.getFilter();
122 if (filter
!= null && filter
.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 ITmfFilter filter
= colorSetting
.getFilter();
139 if (filter
!= null && filter
.matches(event
)) {
143 return PRIORITY_NONE
;
147 * Returns the color settings based the priority.
149 * @param priority A priority (index) of color settings
150 * @return the color settings defined for the priority else default color settings
152 public static ColorSetting
getColorSetting(int priority
) {
153 if (priority
< fColorSettings
.length
) {
154 return fColorSettings
[priority
];
156 return DEFAULT_COLOR_SETTING
;
160 * Adds a color settings listener.
162 * @param listener A listener to add.
164 public static void addColorSettingsListener(IColorSettingsListener listener
) {
165 if (! fListeners
.contains(listener
)) {
166 fListeners
.add(listener
);
171 * Removes a color settings listener.
173 * @param listener A listener to remove.
175 public static void removeColorSettingsListener(IColorSettingsListener listener
) {
176 fListeners
.remove(listener
);
180 private static void fireColorSettingsChanged() {
181 for (IColorSettingsListener listener
: fListeners
) {
182 listener
.colorSettingsChanged(fColorSettings
);