tmf: Fix workspace backwards compatibility of filters, colors and XML
[deliverable/tracecompass.git] / 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.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;
26
27 /**
28 * Static class for managing color settings.
29 *
30 * @version 1.0
31 * @author Patrick Tasse
32 *
33 */
34 public class ColorSettingsManager {
35
36 // The color settings file name
37 private static final String COLOR_SETTINGS_FILE_NAME = "color_settings.xml"; //$NON-NLS-1$
38
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();
42
43 /*
44 * Legacy path to the XML definitions file (in Linux Tools)
45 * TODO Remove once we feel the transition phase is over.
46 */
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);
51
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(),
57 null);
58
59 /**
60 * Special value for priority if unknown.
61 */
62 public static final int PRIORITY_NONE = Integer.MAX_VALUE;
63
64 // The stored color settings
65 private static ColorSetting[] fColorSettings;
66
67 static {
68 File defaultFile = new File(COLOR_SETTINGS_PATH_NAME);
69 /*
70 * If there is no file at the expected location, check the legacy
71 * location instead.
72 */
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);
79 }
80 }
81 }
82 fColorSettings = ColorSettingsXML.load(COLOR_SETTINGS_PATH_NAME);
83 }
84
85 // The listener list
86 private static List<IColorSettingsListener> fListeners = new ArrayList<>();
87
88 /**
89 * Returns an array of color settings.
90 *
91 * @return an array of color settings.
92 */
93 public static ColorSetting[] getColorSettings() {
94 return (fColorSettings != null) ? Arrays.copyOf(fColorSettings, fColorSettings.length) : null;
95 }
96
97 /**
98 * Sets the array of color settings.
99 *
100 * @param colorSettings A array of color settings to set
101 */
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);
106 }
107 fireColorSettingsChanged();
108 }
109
110 /**
111 * Gets the color settings that matches the filter for given event.
112 *
113 * @param event
114 * The event to check
115 *
116 * @return color settings defined for filter if found else default color
117 * settings
118 */
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)) {
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 if (colorSetting.getFilter() != null && colorSetting.getFilter().matches(event)) {
139 return i;
140 }
141 }
142 return PRIORITY_NONE;
143 }
144
145 /**
146 * Returns the color settings based the priority.
147 *
148 * @param priority A priority (index) of color settings
149 * @return the color settings defined for the priority else default color settings
150 */
151 public static ColorSetting getColorSetting(int priority) {
152 if (priority < fColorSettings.length) {
153 return fColorSettings[priority];
154 }
155 return DEFAULT_COLOR_SETTING;
156 }
157
158 /**
159 * Adds a color settings listener.
160 *
161 * @param listener A listener to add.
162 */
163 public static void addColorSettingsListener(IColorSettingsListener listener) {
164 if (! fListeners.contains(listener)) {
165 fListeners.add(listener);
166 }
167 }
168
169 /**
170 * Removes a color settings listener.
171 *
172 * @param listener A listener to remove.
173 */
174 public static void removeColorSettingsListener(IColorSettingsListener listener) {
175 fListeners.remove(listener);
176 }
177
178 // Notify listeners
179 private static void fireColorSettingsChanged() {
180 for (IColorSettingsListener listener : fListeners) {
181 listener.colorSettingsChanged(fColorSettings);
182 }
183 }
184 }
This page took 0.036158 seconds and 6 git commands to generate.