Fix for bug 380944 (color mapping in ColorView/TimeChartView)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / colors / ColorSettingsManager.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2012 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.linuxtools.tmf.ui.views.colors;
15
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.List;
19
20 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
21 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.widgets.Display;
24
25 /**
26 * Static class for managing color settings.
27 *
28 * @version 1.0
29 * @author Patrick Tasse
30 *
31 */
32 public class ColorSettingsManager {
33
34 // The color settings file name
35 private static final String COLOR_SETTINGS_FILE_NAME = "color_settings.xml"; //$NON-NLS-1$
36
37 // The path for the color settings file
38 private static final String COLOR_SETTINGS_PATH_NAME =
39 Activator.getDefault().getStateLocation().addTrailingSeparator().append(COLOR_SETTINGS_FILE_NAME).toString();
40
41 // The default color setting
42 private static final ColorSetting DEFAULT_COLOR_SETTING = new ColorSetting(
43 Display.getDefault().getSystemColor(SWT.COLOR_LIST_FOREGROUND).getRGB(),
44 Display.getDefault().getSystemColor(SWT.COLOR_LIST_BACKGROUND).getRGB(),
45 Display.getDefault().getSystemColor(SWT.COLOR_LIST_FOREGROUND).getRGB(),
46 null);
47
48 /**
49 * Special value for priority if unknown.
50 */
51 public static final int PRIORITY_NONE = Integer.MAX_VALUE;
52
53 // The stored color settings
54 private static ColorSetting[] fColorSettings = ColorSettingsXML.load(COLOR_SETTINGS_PATH_NAME);
55
56 // The listener list
57 private static List<IColorSettingsListener> fListeners = new ArrayList<IColorSettingsListener>();
58
59 /**
60 * Returns an array of color settings.
61 *
62 * @return an array of color settings.
63 */
64 public static ColorSetting[] getColorSettings() {
65 return (fColorSettings != null) ? Arrays.copyOf(fColorSettings, fColorSettings.length) : null;
66 }
67
68 /**
69 * Sets the array of color settings.
70 *
71 * @param colorSettings A array of color settings to set
72 */
73 public static void setColorSettings(ColorSetting[] colorSettings) {
74 fColorSettings = (colorSettings != null) ? Arrays.copyOf(colorSettings, colorSettings.length) : null;
75 ColorSettingsXML.save(COLOR_SETTINGS_PATH_NAME, fColorSettings);
76 fireColorSettingsChanged();
77 }
78
79 /**
80 * Gets the color settings that matches the filter for given event.
81 *
82 * @param A event the event to check
83 *
84 * @return color settings defined for filter if found else default color settings
85 */
86 public static ColorSetting getColorSetting(ITmfEvent event) {
87 for (int i = 0; i < fColorSettings.length; i++) {
88 ColorSetting colorSetting = fColorSettings[i];
89 if (colorSetting.getFilter() != null && colorSetting.getFilter().matches(event)) {
90 return colorSetting;
91 }
92 }
93 return DEFAULT_COLOR_SETTING;
94 }
95
96 /**
97 * Gets the color settings priority for the given event.
98 *
99 * @param event A event the event to check
100 * @return the priority defined for the filter else PRIORITY_NONE
101 */
102 public static int getColorSettingPriority(ITmfEvent event) {
103 for (int i = 0; i < fColorSettings.length; i++) {
104 ColorSetting colorSetting = fColorSettings[i];
105 if (colorSetting.getFilter() != null && colorSetting.getFilter().matches(event)) {
106 return i;
107 }
108 }
109 return PRIORITY_NONE;
110 }
111
112 /**
113 * Returns the color settings based the priority.
114 *
115 * @param priority A priority (index) of color settings
116 * @return the color settings defined for the priority else default color settings
117 */
118 public static ColorSetting getColorSetting(int priority) {
119 if (priority < fColorSettings.length) {
120 return fColorSettings[priority];
121 }
122 return DEFAULT_COLOR_SETTING;
123 }
124
125 /**
126 * Adds a color settings listener.
127 *
128 * @param listener A listener to add.
129 */
130 public static void addColorSettingsListener(IColorSettingsListener listener) {
131 if (! fListeners.contains(listener)) {
132 fListeners.add(listener);
133 }
134 }
135
136 /**
137 * Removes a color settings listener.
138 *
139 * @param listener A listener to remove.
140 */
141 public static void removeColorSettingsListener(IColorSettingsListener listener) {
142 fListeners.remove(listener);
143 }
144
145 // Notify listeners
146 private static void fireColorSettingsChanged() {
147 for (IColorSettingsListener listener : fListeners) {
148 listener.colorSettingsChanged(fColorSettings);
149 }
150 }
151 }
This page took 0.038045 seconds and 6 git commands to generate.