lttng: Help stabilize some TimeGraphs tests
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / colors / ColorSettingsManager.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 * Bernd Hufmann - Updated to use RGB for the tick color
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.ui.views.colors;
15
16 import java.io.File;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20
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;
25
26 /**
27 * Static class for managing color settings.
28 *
29 * @version 1.0
30 * @author Patrick Tasse
31 *
32 */
33 public class ColorSettingsManager {
34
35 // The color settings file name
36 private static final String COLOR_SETTINGS_FILE_NAME = "color_settings.xml"; //$NON-NLS-1$
37
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();
41
42 /*
43 * Legacy path to the XML definitions file (in Linux Tools)
44 * TODO Remove once we feel the transition phase is over.
45 */
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);
50
51 // The default color setting
52 private static final ColorSetting DEFAULT_COLOR_SETTING = new ColorSetting(
53 null,
54 null,
55 null,
56 null);
57
58 /**
59 * Special value for priority if unknown.
60 */
61 public static final int PRIORITY_NONE = Integer.MAX_VALUE;
62
63 // The stored color settings
64 private static ColorSetting[] fColorSettings;
65
66 static {
67 File defaultFile = new File(COLOR_SETTINGS_PATH_NAME);
68 /*
69 * If there is no file at the expected location, check the legacy
70 * location instead.
71 */
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);
78 }
79 }
80 }
81 fColorSettings = ColorSettingsXML.load(COLOR_SETTINGS_PATH_NAME);
82 }
83
84 // The listener list
85 private static List<IColorSettingsListener> fListeners = new ArrayList<>();
86
87 /**
88 * Returns an array of color settings.
89 *
90 * @return an array of color settings.
91 */
92 public static ColorSetting[] getColorSettings() {
93 return (fColorSettings != null) ? Arrays.copyOf(fColorSettings, fColorSettings.length) : null;
94 }
95
96 /**
97 * Sets the array of color settings.
98 *
99 * @param colorSettings A array of color settings to set
100 */
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);
105 }
106 fireColorSettingsChanged();
107 }
108
109 /**
110 * Gets the color settings that matches the filter for given event.
111 *
112 * @param event
113 * The event to check
114 *
115 * @return color settings defined for filter if found else default color
116 * settings
117 */
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)) {
123 return colorSetting;
124 }
125 }
126 return DEFAULT_COLOR_SETTING;
127 }
128
129 /**
130 * Gets the color settings priority for the given event.
131 *
132 * @param event A event the event to check
133 * @return the priority defined for the filter else PRIORITY_NONE
134 */
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)) {
140 return i;
141 }
142 }
143 return PRIORITY_NONE;
144 }
145
146 /**
147 * Returns the color settings based the priority.
148 *
149 * @param priority A priority (index) of color settings
150 * @return the color settings defined for the priority else default color settings
151 */
152 public static ColorSetting getColorSetting(int priority) {
153 if (priority < fColorSettings.length) {
154 return fColorSettings[priority];
155 }
156 return DEFAULT_COLOR_SETTING;
157 }
158
159 /**
160 * Adds a color settings listener.
161 *
162 * @param listener A listener to add.
163 */
164 public static void addColorSettingsListener(IColorSettingsListener listener) {
165 if (! fListeners.contains(listener)) {
166 fListeners.add(listener);
167 }
168 }
169
170 /**
171 * Removes a color settings listener.
172 *
173 * @param listener A listener to remove.
174 */
175 public static void removeColorSettingsListener(IColorSettingsListener listener) {
176 fListeners.remove(listener);
177 }
178
179 // Notify listeners
180 private static void fireColorSettingsChanged() {
181 for (IColorSettingsListener listener : fListeners) {
182 listener.colorSettingsChanged(fColorSettings);
183 }
184 }
185 }
This page took 0.073594 seconds and 5 git commands to generate.