1 /*******************************************************************************
2 * Copyright (c) 2010, 2012 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 * Bernd Hufmann - Updated to use RGB for the tick color
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.colors
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Arrays
;
18 import java
.util
.List
;
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
;
26 * Static class for managing color settings.
29 * @author Patrick Tasse
32 public class ColorSettingsManager
{
34 // The color settings file name
35 private static final String COLOR_SETTINGS_FILE_NAME
= "color_settings.xml"; //$NON-NLS-1$
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();
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(),
49 * Special value for priority if unknown.
51 public static final int PRIORITY_NONE
= Integer
.MAX_VALUE
;
53 // The stored color settings
54 private static ColorSetting
[] fColorSettings
= ColorSettingsXML
.load(COLOR_SETTINGS_PATH_NAME
);
57 private static List
<IColorSettingsListener
> fListeners
= new ArrayList
<IColorSettingsListener
>();
60 * Returns an array of color settings.
62 * @return an array of color settings.
64 public static ColorSetting
[] getColorSettings() {
65 return (fColorSettings
!= null) ? Arrays
.copyOf(fColorSettings
, fColorSettings
.length
) : null;
69 * Sets the array of color settings.
71 * @param colorSettings A array of color settings to set
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();
80 * Gets the color settings that matches the filter for given event.
85 * @return color settings defined for filter if found else default color
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
)) {
95 return DEFAULT_COLOR_SETTING
;
99 * Gets the color settings priority for the given event.
101 * @param event A event the event to check
102 * @return the priority defined for the filter else PRIORITY_NONE
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
)) {
111 return PRIORITY_NONE
;
115 * Returns the color settings based the priority.
117 * @param priority A priority (index) of color settings
118 * @return the color settings defined for the priority else default color settings
120 public static ColorSetting
getColorSetting(int priority
) {
121 if (priority
< fColorSettings
.length
) {
122 return fColorSettings
[priority
];
124 return DEFAULT_COLOR_SETTING
;
128 * Adds a color settings listener.
130 * @param listener A listener to add.
132 public static void addColorSettingsListener(IColorSettingsListener listener
) {
133 if (! fListeners
.contains(listener
)) {
134 fListeners
.add(listener
);
139 * Removes a color settings listener.
141 * @param listener A listener to remove.
143 public static void removeColorSettingsListener(IColorSettingsListener listener
) {
144 fListeners
.remove(listener
);
148 private static void fireColorSettingsChanged() {
149 for (IColorSettingsListener listener
: fListeners
) {
150 listener
.colorSettingsChanged(fColorSettings
);