Commit | Line | Data |
---|---|---|
be222f56 PT |
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 event | |
83 | * The event to check | |
84 | * | |
85 | * @return color settings defined for filter if found else default color | |
86 | * settings | |
87 | */ | |
88 | public static ColorSetting getColorSetting(ITmfEvent event) { | |
89 | for (int i = 0; i < fColorSettings.length; i++) { | |
90 | ColorSetting colorSetting = fColorSettings[i]; | |
91 | if (colorSetting.getFilter() != null && colorSetting.getFilter().matches(event)) { | |
92 | return colorSetting; | |
93 | } | |
94 | } | |
95 | return DEFAULT_COLOR_SETTING; | |
96 | } | |
97 | ||
98 | /** | |
99 | * Gets the color settings priority for the given event. | |
100 | * | |
101 | * @param event A event the event to check | |
102 | * @return the priority defined for the filter else PRIORITY_NONE | |
103 | */ | |
104 | public static int getColorSettingPriority(ITmfEvent event) { | |
105 | for (int i = 0; i < fColorSettings.length; i++) { | |
106 | ColorSetting colorSetting = fColorSettings[i]; | |
107 | if (colorSetting.getFilter() != null && colorSetting.getFilter().matches(event)) { | |
108 | return i; | |
109 | } | |
110 | } | |
111 | return PRIORITY_NONE; | |
112 | } | |
113 | ||
114 | /** | |
115 | * Returns the color settings based the priority. | |
116 | * | |
117 | * @param priority A priority (index) of color settings | |
118 | * @return the color settings defined for the priority else default color settings | |
119 | */ | |
120 | public static ColorSetting getColorSetting(int priority) { | |
121 | if (priority < fColorSettings.length) { | |
122 | return fColorSettings[priority]; | |
123 | } | |
124 | return DEFAULT_COLOR_SETTING; | |
125 | } | |
126 | ||
127 | /** | |
128 | * Adds a color settings listener. | |
129 | * | |
130 | * @param listener A listener to add. | |
131 | */ | |
132 | public static void addColorSettingsListener(IColorSettingsListener listener) { | |
133 | if (! fListeners.contains(listener)) { | |
134 | fListeners.add(listener); | |
135 | } | |
136 | } | |
137 | ||
138 | /** | |
139 | * Removes a color settings listener. | |
140 | * | |
141 | * @param listener A listener to remove. | |
142 | */ | |
143 | public static void removeColorSettingsListener(IColorSettingsListener listener) { | |
144 | fListeners.remove(listener); | |
145 | } | |
146 | ||
147 | // Notify listeners | |
148 | private static void fireColorSettingsChanged() { | |
149 | for (IColorSettingsListener listener : fListeners) { | |
150 | listener.colorSettingsChanged(fColorSettings); | |
151 | } | |
152 | } | |
153 | } |