KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > views > CheatSheetPage


1 /*******************************************************************************
2  * Copyright (c) 2002, 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.cheatsheets.views;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.graphics.Color;
20 import org.eclipse.swt.graphics.RGB;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.swt.widgets.Menu;
24 import org.eclipse.swt.widgets.MenuItem;
25 import org.eclipse.ui.forms.FormColors;
26 import org.eclipse.ui.forms.HyperlinkGroup;
27 import org.eclipse.ui.forms.HyperlinkSettings;
28 import org.eclipse.ui.forms.widgets.FormToolkit;
29 import org.eclipse.ui.forms.widgets.ScrolledForm;
30 import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
31 import org.eclipse.ui.internal.cheatsheets.CheatSheetStopWatch;
32 import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
33 import org.eclipse.ui.internal.cheatsheets.Messages;
34 import org.eclipse.ui.internal.cheatsheets.actions.IMenuContributor;
35 import org.eclipse.ui.internal.cheatsheets.data.CheatSheet;
36
37 public class CheatSheetPage extends Page implements IMenuContributor {
38     // Colors
39
private Color introColor;
40     private Color activeColor;
41
42     private Color inactiveColor1;
43     private Color inactiveColor2;
44
45     private CheatSheet cheatSheet;
46
47     private ArrayList JavaDoc viewItemList;
48
49     private CheatSheetViewer viewer;
50
51     public CheatSheetPage(CheatSheet cheatSheet, ArrayList JavaDoc viewItemList,
52             CheatSheetViewer cheatSheetViewer) {
53         super();
54         this.cheatSheet = cheatSheet;
55         this.viewItemList = viewItemList;
56         this.viewer = cheatSheetViewer;
57     }
58
59     public void createPart(Composite parent) {
60         CheatSheetStopWatch.startStopWatch("CheatSheetPage.createPart()"); //$NON-NLS-1$
61
super.createPart(parent);
62         HyperlinkGroup hyperlinkGroup = toolkit.getHyperlinkGroup();
63         hyperlinkGroup.setHyperlinkUnderlineMode(HyperlinkSettings.UNDERLINE_HOVER);
64         
65         CheatSheetStopWatch
66                 .printLapTime(
67                         "CheatSheetPage.createPart()", "Time in CheatSheetPage.createInfoArea() after super.createInfoArea(): "); //$NON-NLS-1$ //$NON-NLS-2$
68
IntroItem intro = new IntroItem(this, cheatSheet.getIntroItem(),
69                 introColor, viewer);
70         CheatSheetStopWatch
71                 .printLapTime(
72                         "CheatSheetPage.createPart()", "Time in CheatSheetPage.createInfoArea() after new IntroItem(): "); //$NON-NLS-1$ //$NON-NLS-2$
73

74         intro.setBold(true);
75         viewItemList.add(intro);
76
77         CheatSheetStopWatch
78                 .printLapTime(
79                         "CheatSheetPage.createInfoArea()", "Time in CheatSheetPage.createPart() before add loop: "); //$NON-NLS-1$ //$NON-NLS-2$
80
// Get the content info from the parser. This makes up all items except
81
// the intro item.
82
ArrayList JavaDoc items = cheatSheet.getItems();
83         for (int i = 0; i < items.size(); i++) {
84             Color color = (i % 2) == 0 ? getInactiveColor1() : getInactiveColor2();
85
86             CoreItem coreItem = new CoreItem(this,
87                     (org.eclipse.ui.internal.cheatsheets.data.Item) items
88                             .get(i), color, viewer);
89             viewItemList.add(coreItem);
90         }
91         CheatSheetStopWatch
92                 .printLapTime(
93                         "CheatSheetPage.createInfoArea()", "Time in CheatSheetPage.createPart(): "); //$NON-NLS-1$ //$NON-NLS-2$
94
}
95
96     private Color getInactiveColor2() {
97         return inactiveColor2;
98     }
99
100     private Color getInactiveColor1() {
101         return inactiveColor1;
102     }
103
104     /**
105      * Creates the cheatsheet's title areawhich will consists of a title and
106      * image.
107      *
108      * @param parent
109      * the SWT parent for the title area composite
110      */

111     protected String JavaDoc getTitle() {
112         if (cheatSheet != null && cheatSheet.getTitle() != null)
113             return cheatSheet.getTitle();
114         return ICheatSheetResource.EMPTY_STRING;
115     }
116
117     public void dispose() {
118         super.dispose();
119
120         if (getInactiveColor1() != null)
121             getInactiveColor1().dispose();
122         if (getInactiveColor2() != null)
123             getInactiveColor2().dispose();
124
125         if (activeColor != null)
126             activeColor.dispose();
127         
128         if (introColor != null)
129             introColor.dispose();
130         inactiveColor1 = null;
131         inactiveColor2 = null;
132         activeColor = null;
133         introColor = null;
134     }
135
136     protected void init(Display display) {
137         super.init(display);
138         computeColors(display);
139     }
140
141     private void computeColors(Display display) {
142         RGB rgb;
143         RGB white = new RGB(255, 255, 255);
144         RGB black = new RGB(0, 0, 0);
145         
146         if (isReverseVideo()) {
147             computeReverseVideoColors(display);
148             return;
149         }
150
151         if (toolkit.getColors().isWhiteBackground()) {
152             rgb = toolkit.getColors().getSystemColor(SWT.COLOR_LIST_SELECTION);
153             // active color is selection + 80% white
154
rgb = FormColors.blend(rgb, white, 20);
155             // test for bounds
156
if (FormColors.testTwoPrimaryColors(rgb, 245, 256)) {
157                 // too bright - tone down 20%
158
rgb = FormColors.blend(rgb, black, 80);
159             } else if (FormColors.testAnyPrimaryColor(rgb, 170, 191)) {
160                 // too dark - brighten 15%
161
rgb = FormColors.blend(rgb, white, 85);
162             } else if (FormColors.testAnyPrimaryColor(rgb, 190, 215)) {
163                 // too dark - brighten 10%
164
rgb = FormColors.blend(rgb, white, 90);
165             }
166             // final check - if gray
167
if (Math.abs(rgb.blue-rgb.green) <5 &&
168                     Math.abs(rgb.blue-rgb.red)<5 &&
169                     Math.abs(rgb.green-rgb.red)<5) {
170                 // blend with blue
171
rgb = FormColors.blend(rgb, new RGB(100, 100, 255), 90);
172             }
173             introColor = new Color(display, rgb);
174             inactiveColor2 = new Color(display, rgb);
175         } else {
176             // colored background
177
rgb = toolkit.getColors().getSystemColor(SWT.COLOR_LIST_SELECTION);
178             // active color is selection + 70% white
179
rgb = FormColors.blend(rgb, white, 30);
180             // If these values are in the range of 201 to 220, then decrease
181
// white by 20%
182
if (FormColors.testTwoPrimaryColors(rgb, 200, 221))
183                 rgb = FormColors.blend(rgb, black, 80);
184             // If these values are in the range of 221 to 240, then decrease
185
// white by 40%
186
else if (FormColors.testTwoPrimaryColors(rgb, 222, 241))
187                 rgb = FormColors.blend(rgb, black, 60);
188             // If these values are in the range of 241 to 255, then decrease
189
// white by 70%
190
else if (FormColors.testTwoPrimaryColors(rgb, 240, 256))
191                 rgb = FormColors.blend(rgb, black, 30);
192             introColor = new Color(display, rgb);
193             inactiveColor2 = new Color(display, rgb);
194         }
195         rgb = inactiveColor2.getRGB();
196         rgb = FormColors.blend(rgb, backgroundColor.getRGB(), 40);
197         inactiveColor1 = new Color(display, rgb);
198         activeColor = new Color(display, backgroundColor.getRGB());
199     }
200
201     private void computeReverseVideoColors(Display display) {
202         Color background = toolkit.getColors().getBackground();
203         RGB white = new RGB(255, 255, 255);
204         // Create new colors, they will get disposed
205
RGB rgb = background.getRGB();
206         activeColor = new Color(display, rgb );
207         rgb = FormColors.blend(rgb, white, 85);
208         inactiveColor1 = new Color(display, rgb);
209         rgb = FormColors.blend(rgb, white, 85);
210         inactiveColor2 = new Color(display, rgb );
211         introColor = new Color(display, rgb );
212     }
213     
214     private boolean isReverseVideo() {
215         Color bg = toolkit.getColors().getBackground();
216         return ((bg.getBlue() + bg.getRed() + bg.getGreen()) < 380);
217     }
218
219     public void initialized() {
220         for (Iterator JavaDoc iter = viewItemList.iterator(); iter.hasNext();) {
221             ViewItem item = (ViewItem) iter.next();
222             item.initialized();
223         }
224     }
225
226     public Color getActiveColor() {
227         return activeColor;
228     }
229
230     public ScrolledForm getForm() {
231         return form;
232     }
233
234     public FormToolkit getToolkit() {
235         return toolkit;
236     }
237     
238     private int contributeRestartItem(Menu menu, int index) {
239         MenuItem item = new MenuItem(menu, SWT.PUSH, index++);
240         item.setText(Messages.RESTART_MENU);
241         item.setImage(CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_RETURN));
242         
243         item.addSelectionListener(new SelectionAdapter() {
244             public void widgetSelected(SelectionEvent e) {
245                 viewer.restart();
246             }
247         });
248         return index;
249     }
250
251     public int contributeToViewMenu(Menu menu, int index) {
252         return contributeRestartItem(menu, index);
253     }
254 }
255
Popular Tags