Commit | Line | Data |
---|---|---|
ca13a91c | 1 | /*******************************************************************************\r |
0edc9535 | 2 | * Copyright (c) 2010, 2012 Ericsson\r |
013a5f1c | 3 | *\r |
ca13a91c FC |
4 | * All rights reserved. This program and the accompanying materials are\r |
5 | * made available under the terms of the Eclipse Public License v1.0 which\r | |
6 | * accompanies this distribution, and is available at\r | |
7 | * http://www.eclipse.org/legal/epl-v10.html\r | |
013a5f1c | 8 | *\r |
ca13a91c FC |
9 | * Contributors:\r |
10 | * Patrick Tasse - Initial API and implementation\r | |
0edc9535 | 11 | * Bernd Hufmann - Updated to use RGB for the tick color\r |
ca13a91c FC |
12 | *******************************************************************************/\r |
13 | \r | |
14 | package org.eclipse.linuxtools.tmf.ui.views.colors;\r | |
15 | \r | |
16 | import java.util.ArrayList;\r | |
5a5c2fc7 | 17 | import java.util.Arrays;\r |
9fa32496 | 18 | import java.util.List;\r |
ca13a91c | 19 | \r |
8fd82db5 | 20 | import org.eclipse.linuxtools.internal.tmf.ui.Activator;\r |
72f1e62a | 21 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;\r |
ca13a91c FC |
22 | import org.eclipse.swt.SWT;\r |
23 | import org.eclipse.swt.widgets.Display;\r | |
24 | \r | |
0edc9535 BH |
25 | /**\r |
26 | * Static class for managing color settings.\r | |
013a5f1c | 27 | *\r |
0edc9535 BH |
28 | * @version 1.0\r |
29 | * @author Patrick Tasse\r | |
30 | *\r | |
31 | */\r | |
ca13a91c | 32 | public class ColorSettingsManager {\r |
013a5f1c | 33 | \r |
0edc9535 | 34 | // The color settings file name\r |
ca13a91c | 35 | private static final String COLOR_SETTINGS_FILE_NAME = "color_settings.xml"; //$NON-NLS-1$\r |
013a5f1c | 36 | \r |
0edc9535 | 37 | // The path for the color settings file\r |
ca13a91c | 38 | private static final String COLOR_SETTINGS_PATH_NAME =\r |
8fd82db5 | 39 | Activator.getDefault().getStateLocation().addTrailingSeparator().append(COLOR_SETTINGS_FILE_NAME).toString();\r |
0edc9535 BH |
40 | \r |
41 | // The default color setting\r | |
ca13a91c FC |
42 | private static final ColorSetting DEFAULT_COLOR_SETTING = new ColorSetting(\r |
43 | Display.getDefault().getSystemColor(SWT.COLOR_LIST_FOREGROUND).getRGB(),\r | |
44 | Display.getDefault().getSystemColor(SWT.COLOR_LIST_BACKGROUND).getRGB(),\r | |
0edc9535 | 45 | Display.getDefault().getSystemColor(SWT.COLOR_LIST_FOREGROUND).getRGB(),\r |
ca13a91c | 46 | null);\r |
0edc9535 BH |
47 | \r |
48 | /**\r | |
013a5f1c | 49 | * Special value for priority if unknown.\r |
0edc9535 | 50 | */\r |
ca13a91c | 51 | public static final int PRIORITY_NONE = Integer.MAX_VALUE;\r |
0edc9535 BH |
52 | \r |
53 | // The stored color settings\r | |
ca13a91c | 54 | private static ColorSetting[] fColorSettings = ColorSettingsXML.load(COLOR_SETTINGS_PATH_NAME);\r |
0edc9535 BH |
55 | \r |
56 | // The listener list\r | |
9fa32496 | 57 | private static List<IColorSettingsListener> fListeners = new ArrayList<IColorSettingsListener>();\r |
0edc9535 BH |
58 | \r |
59 | /**\r | |
60 | * Returns an array of color settings.\r | |
013a5f1c | 61 | *\r |
0edc9535 BH |
62 | * @return an array of color settings.\r |
63 | */\r | |
ca13a91c | 64 | public static ColorSetting[] getColorSettings() {\r |
5a5c2fc7 | 65 | return (fColorSettings != null) ? Arrays.copyOf(fColorSettings, fColorSettings.length) : null;\r |
ca13a91c | 66 | }\r |
013a5f1c | 67 | \r |
0edc9535 BH |
68 | /**\r |
69 | * Sets the array of color settings.\r | |
013a5f1c | 70 | *\r |
0edc9535 BH |
71 | * @param colorSettings A array of color settings to set\r |
72 | */\r | |
ca13a91c | 73 | public static void setColorSettings(ColorSetting[] colorSettings) {\r |
5a5c2fc7 | 74 | fColorSettings = (colorSettings != null) ? Arrays.copyOf(colorSettings, colorSettings.length) : null;\r |
ca13a91c FC |
75 | ColorSettingsXML.save(COLOR_SETTINGS_PATH_NAME, fColorSettings);\r |
76 | fireColorSettingsChanged();\r | |
77 | }\r | |
0edc9535 | 78 | \r |
013a5f1c AM |
79 | /**\r |
80 | * Gets the color settings that matches the filter for given event.\r | |
81 | *\r | |
82 | * @param event\r | |
83 | * The event to check\r | |
84 | *\r | |
85 | * @return color settings defined for filter if found else default color\r | |
86 | * settings\r | |
87 | */\r | |
72f1e62a | 88 | public static ColorSetting getColorSetting(ITmfEvent event) {\r |
ca13a91c FC |
89 | for (int i = 0; i < fColorSettings.length; i++) {\r |
90 | ColorSetting colorSetting = fColorSettings[i];\r | |
91 | if (colorSetting.getFilter() != null && colorSetting.getFilter().matches(event)) {\r | |
92 | return colorSetting;\r | |
93 | }\r | |
94 | }\r | |
95 | return DEFAULT_COLOR_SETTING;\r | |
96 | }\r | |
0edc9535 BH |
97 | \r |
98 | /**\r | |
99 | * Gets the color settings priority for the given event.\r | |
013a5f1c | 100 | *\r |
0edc9535 BH |
101 | * @param event A event the event to check\r |
102 | * @return the priority defined for the filter else PRIORITY_NONE\r | |
103 | */\r | |
72f1e62a | 104 | public static int getColorSettingPriority(ITmfEvent event) {\r |
ca13a91c FC |
105 | for (int i = 0; i < fColorSettings.length; i++) {\r |
106 | ColorSetting colorSetting = fColorSettings[i];\r | |
107 | if (colorSetting.getFilter() != null && colorSetting.getFilter().matches(event)) {\r | |
108 | return i;\r | |
109 | }\r | |
110 | }\r | |
111 | return PRIORITY_NONE;\r | |
112 | }\r | |
113 | \r | |
0edc9535 | 114 | /**\r |
013a5f1c AM |
115 | * Returns the color settings based the priority.\r |
116 | *\r | |
0edc9535 BH |
117 | * @param priority A priority (index) of color settings\r |
118 | * @return the color settings defined for the priority else default color settings\r | |
119 | */\r | |
ca13a91c FC |
120 | public static ColorSetting getColorSetting(int priority) {\r |
121 | if (priority < fColorSettings.length) {\r | |
122 | return fColorSettings[priority];\r | |
123 | }\r | |
013a5f1c | 124 | return DEFAULT_COLOR_SETTING;\r |
ca13a91c FC |
125 | }\r |
126 | \r | |
0edc9535 BH |
127 | /**\r |
128 | * Adds a color settings listener.\r | |
013a5f1c | 129 | *\r |
0edc9535 BH |
130 | * @param listener A listener to add.\r |
131 | */\r | |
ca13a91c FC |
132 | public static void addColorSettingsListener(IColorSettingsListener listener) {\r |
133 | if (! fListeners.contains(listener)) {\r | |
134 | fListeners.add(listener);\r | |
135 | }\r | |
136 | }\r | |
013a5f1c | 137 | \r |
0edc9535 BH |
138 | /**\r |
139 | * Removes a color settings listener.\r | |
013a5f1c | 140 | *\r |
0edc9535 BH |
141 | * @param listener A listener to remove.\r |
142 | */\r | |
ca13a91c FC |
143 | public static void removeColorSettingsListener(IColorSettingsListener listener) {\r |
144 | fListeners.remove(listener);\r | |
145 | }\r | |
013a5f1c | 146 | \r |
0edc9535 | 147 | // Notify listeners\r |
ca13a91c FC |
148 | private static void fireColorSettingsChanged() {\r |
149 | for (IColorSettingsListener listener : fListeners) {\r | |
150 | listener.colorSettingsChanged(fColorSettings);\r | |
151 | }\r | |
152 | }\r | |
153 | }\r |