KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > presentations > defaultpresentation > DefaultThemeListener


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.presentations.defaultpresentation;
12
13 import org.eclipse.jface.resource.JFaceResources;
14 import org.eclipse.swt.graphics.Color;
15 import org.eclipse.swt.graphics.Font;
16 import org.eclipse.swt.graphics.RGB;
17 import org.eclipse.ui.internal.IWorkbenchThemeConstants;
18 import org.eclipse.ui.internal.preferences.AbstractPropertyListener;
19 import org.eclipse.ui.internal.preferences.IPropertyMap;
20 import org.eclipse.ui.internal.preferences.PropertyUtil;
21 import org.eclipse.ui.internal.themes.LightColorFactory;
22 import org.eclipse.ui.presentations.StackPresentation;
23
24 /**
25  * @since 3.1
26  */

27 public class DefaultThemeListener extends AbstractPropertyListener {
28
29     private DefaultTabFolder folder;
30     private IPropertyMap theme;
31     
32     public DefaultThemeListener(DefaultTabFolder folder, IPropertyMap theme) {
33         this.folder = folder;
34         this.theme = theme;
35     }
36     
37     private Color getColor(String JavaDoc id, Color defaultValue) {
38         Color value = (Color)theme.getValue(id, Color.class);
39         if (value == null) {
40             value = defaultValue;
41         }
42         
43         return value;
44     }
45     
46     private int getInt(String JavaDoc id, int defaultValue) {
47         Integer JavaDoc result = ((Integer JavaDoc)theme.getValue(id, Integer JavaDoc.class));
48         
49         if (result == null) {
50             return defaultValue;
51         }
52         
53         return result.intValue();
54     }
55     
56     private boolean getBoolean(String JavaDoc id, boolean defaultValue) {
57         Boolean JavaDoc result = ((Boolean JavaDoc)theme.getValue(id, Boolean JavaDoc.class));
58         
59         if (result == null) {
60             return defaultValue;
61         }
62         
63         return result.booleanValue();
64     }
65
66     /*
67      * Update the ACTIVE_TAB_HIGHLIGHT_START color in the color registry.
68      * Return true if we're using highlights on tabs, false otherwise.
69      * The highlight color is computed based on the ACTIVE_TAB_BG_START.
70      * We need to do this here, in the ThemeListener, so that we can catch
71      * the change to the ACTIVE_TAB_BG_START begin color and update the
72      * highlight color appropriately.
73      * @return boolean use highlight color
74      */

75     private boolean updateHighlightColor() {
76         if(! useHighlight())
77             return false;
78         //get newTabBegin from theme, not from ColorRegistry, which may not have been updated yet
79
RGB newTabBegin = getColor(IWorkbenchThemeConstants.ACTIVE_TAB_BG_START, null).getRGB();
80         RGB newHighlight = LightColorFactory.createHighlightStartColor(newTabBegin);
81         //Registry handles lifecycle of colors so no leakage and if RGB s.equals then no change
82
JFaceResources.getColorRegistry().put(IWorkbenchThemeConstants.ACTIVE_TAB_HIGHLIGHT_START, newHighlight);
83         return true;
84     }
85     
86     private boolean useHighlight() {
87         return PropertyUtil.get(
88                 this.theme,
89                 IWorkbenchThemeConstants.ACTIVE_TAB_HIGHLIGHT,
90                 false);
91     }
92     
93     public void update() {
94         Color[] activeFocusBackgroundColors = updateHighlightColor()
95             ? new Color[] {
96                 getColor(IWorkbenchThemeConstants.ACTIVE_TAB_BG_START, null),
97                 getColor(IWorkbenchThemeConstants.ACTIVE_TAB_BG_END, null),
98                 JFaceResources.getColorRegistry().get(IWorkbenchThemeConstants.ACTIVE_TAB_HIGHLIGHT_START)
99                 }
100             : new Color[] {
101                 getColor(IWorkbenchThemeConstants.ACTIVE_TAB_BG_START, null),
102                 getColor(IWorkbenchThemeConstants.ACTIVE_TAB_BG_END, null)
103                 };
104         
105         folder.setColors(new DefaultTabFolderColors(
106                 getColor(IWorkbenchThemeConstants.ACTIVE_TAB_TEXT_COLOR, null),
107                 activeFocusBackgroundColors,
108                 new int[] {
109                         getInt(IWorkbenchThemeConstants.ACTIVE_TAB_PERCENT, 0) },
110                         getBoolean(IWorkbenchThemeConstants.ACTIVE_TAB_VERTICAL, true)),
111                         StackPresentation.AS_ACTIVE_FOCUS, true);
112         
113         folder.setColors(new DefaultTabFolderColors(
114                 getColor(IWorkbenchThemeConstants.ACTIVE_NOFOCUS_TAB_TEXT_COLOR, null),
115                 new Color[] {
116                         getColor(IWorkbenchThemeConstants.ACTIVE_NOFOCUS_TAB_BG_START, null),
117                         getColor(IWorkbenchThemeConstants.ACTIVE_NOFOCUS_TAB_BG_END, null)
118                         },
119                 new int[] {
120                         getInt(IWorkbenchThemeConstants.ACTIVE_NOFOCUS_TAB_PERCENT, 0) },
121                         getBoolean(IWorkbenchThemeConstants.ACTIVE_NOFOCUS_TAB_VERTICAL, true)),
122                         StackPresentation.AS_ACTIVE_FOCUS, false);
123         
124         folder.setColors(new DefaultTabFolderColors(
125                 getColor(IWorkbenchThemeConstants.INACTIVE_TAB_TEXT_COLOR, null),
126                 new Color[] {
127                         getColor(IWorkbenchThemeConstants.INACTIVE_TAB_BG_START, null),
128                         getColor(IWorkbenchThemeConstants.INACTIVE_TAB_BG_END, null) },
129                 new int[] {
130                         getInt(IWorkbenchThemeConstants.INACTIVE_TAB_PERCENT, 0) },
131                         getBoolean(IWorkbenchThemeConstants.INACTIVE_TAB_VERTICAL, true)),
132                         StackPresentation.AS_INACTIVE);
133         
134         folder.setColors(new DefaultTabFolderColors(
135                 getColor(IWorkbenchThemeConstants.INACTIVE_TAB_TEXT_COLOR, null),
136                 new Color[] {
137                         getColor(IWorkbenchThemeConstants.INACTIVE_TAB_BG_START, null) },
138                 new int[0],
139                 true), StackPresentation.AS_ACTIVE_NOFOCUS);
140         
141         folder.setFont((Font)theme.getValue(IWorkbenchThemeConstants.TAB_TEXT_FONT, Font.class));
142     }
143
144 }
145
Popular Tags