Merge branch 'master' into lttng_2_0_control_dev
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / colors / ColorSettingsManager.java
1 /*******************************************************************************
2 * Copyright (c) 2010 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 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.views.colors;
14
15 import java.util.ArrayList;
16 import java.util.Arrays;
17
18 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
19 import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;
20 import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.widgets.TraceColorScheme;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.widgets.Display;
23
24 public class ColorSettingsManager {
25
26 private static final String COLOR_SETTINGS_FILE_NAME = "color_settings.xml"; //$NON-NLS-1$
27 private static final String COLOR_SETTINGS_PATH_NAME =
28 TmfUiPlugin.getDefault().getStateLocation().addTrailingSeparator().append(COLOR_SETTINGS_FILE_NAME).toString();
29 private static final ColorSetting DEFAULT_COLOR_SETTING = new ColorSetting(
30 Display.getDefault().getSystemColor(SWT.COLOR_LIST_FOREGROUND).getRGB(),
31 Display.getDefault().getSystemColor(SWT.COLOR_LIST_BACKGROUND).getRGB(),
32 TraceColorScheme.BLACK_STATE,
33 null);
34 public static final int PRIORITY_NONE = Integer.MAX_VALUE;
35
36 private static ColorSetting[] fColorSettings = ColorSettingsXML.load(COLOR_SETTINGS_PATH_NAME);
37 private static ArrayList<IColorSettingsListener> fListeners = new ArrayList<IColorSettingsListener>();
38
39 public static ColorSetting[] getColorSettings() {
40 return (fColorSettings != null) ? Arrays.copyOf(fColorSettings, fColorSettings.length) : null;
41 }
42
43 public static void setColorSettings(ColorSetting[] colorSettings) {
44 fColorSettings = (colorSettings != null) ? Arrays.copyOf(colorSettings, colorSettings.length) : null;
45 ColorSettingsXML.save(COLOR_SETTINGS_PATH_NAME, fColorSettings);
46 fireColorSettingsChanged();
47 }
48
49 public static ColorSetting getColorSetting(ITmfEvent event) {
50 for (int i = 0; i < fColorSettings.length; i++) {
51 ColorSetting colorSetting = fColorSettings[i];
52 if (colorSetting.getFilter() != null && colorSetting.getFilter().matches(event)) {
53 return colorSetting;
54 }
55 }
56 return DEFAULT_COLOR_SETTING;
57 }
58
59 public static int getColorSettingPriority(ITmfEvent event) {
60 for (int i = 0; i < fColorSettings.length; i++) {
61 ColorSetting colorSetting = fColorSettings[i];
62 if (colorSetting.getFilter() != null && colorSetting.getFilter().matches(event)) {
63 return i;
64 }
65 }
66 return PRIORITY_NONE;
67 }
68
69 public static ColorSetting getColorSetting(int priority) {
70 if (priority < fColorSettings.length) {
71 return fColorSettings[priority];
72 }
73 return DEFAULT_COLOR_SETTING;
74 }
75
76 public static void addColorSettingsListener(IColorSettingsListener listener) {
77 if (! fListeners.contains(listener)) {
78 fListeners.add(listener);
79 }
80 }
81
82 public static void removeColorSettingsListener(IColorSettingsListener listener) {
83 fListeners.remove(listener);
84 }
85
86 private static void fireColorSettingsChanged() {
87 for (IColorSettingsListener listener : fListeners) {
88 listener.colorSettingsChanged(fColorSettings);
89 }
90 }
91 }
This page took 0.034268 seconds and 6 git commands to generate.