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.
82 * @param A event the event to check
84 * @return color settings defined for filter if found else default color settings
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
)) {
93 return DEFAULT_COLOR_SETTING
;
97 * Gets the color settings priority for the given event.
99 * @param event A event the event to check
100 * @return the priority defined for the filter else PRIORITY_NONE
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
)) {
109 return PRIORITY_NONE
;
113 * Returns the color settings based the priority.
115 * @param priority A priority (index) of color settings
116 * @return the color settings defined for the priority else default color settings
118 public static ColorSetting
getColorSetting(int priority
) {
119 if (priority
< fColorSettings
.length
) {
120 return fColorSettings
[priority
];
122 return DEFAULT_COLOR_SETTING
;
126 * Adds a color settings listener.
128 * @param listener A listener to add.
130 public static void addColorSettingsListener(IColorSettingsListener listener
) {
131 if (! fListeners
.contains(listener
)) {
132 fListeners
.add(listener
);
137 * Removes a color settings listener.
139 * @param listener A listener to remove.
141 public static void removeColorSettingsListener(IColorSettingsListener listener
) {
142 fListeners
.remove(listener
);
146 private static void fireColorSettingsChanged() {
147 for (IColorSettingsListener listener
: fListeners
) {
148 listener
.colorSettingsChanged(fColorSettings
);