001: /*******************************************************************************
002: * Copyright (c) 2004, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.presentations.r33;
011:
012: import org.eclipse.jface.resource.JFaceResources;
013: import org.eclipse.swt.graphics.Color;
014: import org.eclipse.swt.graphics.Font;
015: import org.eclipse.swt.graphics.RGB;
016: import org.eclipse.ui.internal.IWorkbenchThemeConstants;
017: import org.eclipse.ui.internal.preferences.AbstractPropertyListener;
018: import org.eclipse.ui.internal.preferences.IPropertyMap;
019: import org.eclipse.ui.internal.preferences.PropertyUtil;
020: import org.eclipse.ui.internal.themes.LightColorFactory;
021: import org.eclipse.ui.presentations.StackPresentation;
022:
023: /**
024: * @since 3.1
025: */
026: public class DefaultThemeListener extends AbstractPropertyListener {
027:
028: private DefaultTabFolder folder;
029: private IPropertyMap theme;
030:
031: public DefaultThemeListener(DefaultTabFolder folder,
032: IPropertyMap theme) {
033: this .folder = folder;
034: this .theme = theme;
035: }
036:
037: private Color getColor(String id, Color defaultValue) {
038: Color value = (Color) theme.getValue(id, Color.class);
039: if (value == null) {
040: value = defaultValue;
041: }
042:
043: return value;
044: }
045:
046: private int getInt(String id, int defaultValue) {
047: Integer result = ((Integer) theme.getValue(id, Integer.class));
048:
049: if (result == null) {
050: return defaultValue;
051: }
052:
053: return result.intValue();
054: }
055:
056: private boolean getBoolean(String id, boolean defaultValue) {
057: Boolean result = ((Boolean) theme.getValue(id, Boolean.class));
058:
059: if (result == null) {
060: return defaultValue;
061: }
062:
063: return result.booleanValue();
064: }
065:
066: /*
067: * Update the ACTIVE_TAB_HIGHLIGHT_START color in the color registry.
068: * Return true if we're using highlights on tabs, false otherwise.
069: * The highlight color is computed based on the ACTIVE_TAB_BG_START.
070: * We need to do this here, in the ThemeListener, so that we can catch
071: * the change to the ACTIVE_TAB_BG_START begin color and update the
072: * highlight color appropriately.
073: * @return boolean use highlight color
074: */
075: private boolean updateHighlightColor() {
076: if (!useHighlight())
077: return false;
078: //get newTabBegin from theme, not from ColorRegistry, which may not have been updated yet
079: RGB newTabBegin = getColor(
080: IWorkbenchThemeConstants.ACTIVE_TAB_BG_START, null)
081: .getRGB();
082: RGB newHighlight = LightColorFactory
083: .createHighlightStartColor(newTabBegin);
084: //Registry handles lifecycle of colors so no leakage and if RGB s.equals then no change
085: JFaceResources.getColorRegistry().put(
086: IWorkbenchThemeConstants.ACTIVE_TAB_HIGHLIGHT_START,
087: newHighlight);
088: return true;
089: }
090:
091: private boolean useHighlight() {
092: return PropertyUtil.get(this .theme,
093: IWorkbenchThemeConstants.ACTIVE_TAB_HIGHLIGHT, false);
094: }
095:
096: public void update() {
097: Color[] activeFocusBackgroundColors = updateHighlightColor() ? new Color[] {
098: getColor(IWorkbenchThemeConstants.ACTIVE_TAB_BG_START,
099: null),
100: getColor(IWorkbenchThemeConstants.ACTIVE_TAB_BG_END,
101: null),
102: JFaceResources
103: .getColorRegistry()
104: .get(
105: IWorkbenchThemeConstants.ACTIVE_TAB_HIGHLIGHT_START) }
106: : new Color[] {
107: getColor(
108: IWorkbenchThemeConstants.ACTIVE_TAB_BG_START,
109: null),
110: getColor(
111: IWorkbenchThemeConstants.ACTIVE_TAB_BG_END,
112: null) };
113:
114: folder
115: .setColors(
116: new DefaultTabFolderColors(
117: getColor(
118: IWorkbenchThemeConstants.ACTIVE_TAB_TEXT_COLOR,
119: null),
120: activeFocusBackgroundColors,
121: new int[] { getInt(
122: IWorkbenchThemeConstants.ACTIVE_TAB_PERCENT,
123: 0) },
124: getBoolean(
125: IWorkbenchThemeConstants.ACTIVE_TAB_VERTICAL,
126: true)),
127: StackPresentation.AS_ACTIVE_FOCUS, true);
128:
129: folder
130: .setColors(
131: new DefaultTabFolderColors(
132: getColor(
133: IWorkbenchThemeConstants.ACTIVE_NOFOCUS_TAB_TEXT_COLOR,
134: null),
135: new Color[] {
136: getColor(
137: IWorkbenchThemeConstants.ACTIVE_NOFOCUS_TAB_BG_START,
138: null),
139: getColor(
140: IWorkbenchThemeConstants.ACTIVE_NOFOCUS_TAB_BG_END,
141: null) },
142: new int[] { getInt(
143: IWorkbenchThemeConstants.ACTIVE_NOFOCUS_TAB_PERCENT,
144: 0) },
145: getBoolean(
146: IWorkbenchThemeConstants.ACTIVE_NOFOCUS_TAB_VERTICAL,
147: true)),
148: StackPresentation.AS_ACTIVE_FOCUS, false);
149:
150: folder
151: .setColors(
152: new DefaultTabFolderColors(
153: getColor(
154: IWorkbenchThemeConstants.INACTIVE_TAB_TEXT_COLOR,
155: null),
156: new Color[] {
157: getColor(
158: IWorkbenchThemeConstants.INACTIVE_TAB_BG_START,
159: null),
160: getColor(
161: IWorkbenchThemeConstants.INACTIVE_TAB_BG_END,
162: null) },
163: new int[] { getInt(
164: IWorkbenchThemeConstants.INACTIVE_TAB_PERCENT,
165: 0) },
166: getBoolean(
167: IWorkbenchThemeConstants.INACTIVE_TAB_VERTICAL,
168: true)),
169: StackPresentation.AS_INACTIVE);
170:
171: folder
172: .setColors(
173: new DefaultTabFolderColors(
174: getColor(
175: IWorkbenchThemeConstants.INACTIVE_TAB_TEXT_COLOR,
176: null),
177: new Color[] { getColor(
178: IWorkbenchThemeConstants.INACTIVE_TAB_BG_START,
179: null) }, new int[0], true),
180: StackPresentation.AS_ACTIVE_NOFOCUS);
181:
182: folder.setFont((Font) theme.getValue(
183: IWorkbenchThemeConstants.TAB_TEXT_FONT, Font.class));
184: }
185:
186: }
|