1 11 package org.eclipse.ui.internal.cheatsheets.views; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 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 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 viewItemList; 48 49 private CheatSheetViewer viewer; 50 51 public CheatSheetPage(CheatSheet cheatSheet, ArrayList 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()"); 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(): "); IntroItem intro = new IntroItem(this, cheatSheet.getIntroItem(), 69 introColor, viewer); 70 CheatSheetStopWatch 71 .printLapTime( 72 "CheatSheetPage.createPart()", "Time in CheatSheetPage.createInfoArea() after new IntroItem(): "); 74 intro.setBold(true); 75 viewItemList.add(intro); 76 77 CheatSheetStopWatch 78 .printLapTime( 79 "CheatSheetPage.createInfoArea()", "Time in CheatSheetPage.createPart() before add loop: "); ArrayList 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(): "); } 95 96 private Color getInactiveColor2() { 97 return inactiveColor2; 98 } 99 100 private Color getInactiveColor1() { 101 return inactiveColor1; 102 } 103 104 111 protected String 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 rgb = FormColors.blend(rgb, white, 20); 155 if (FormColors.testTwoPrimaryColors(rgb, 245, 256)) { 157 rgb = FormColors.blend(rgb, black, 80); 159 } else if (FormColors.testAnyPrimaryColor(rgb, 170, 191)) { 160 rgb = FormColors.blend(rgb, white, 85); 162 } else if (FormColors.testAnyPrimaryColor(rgb, 190, 215)) { 163 rgb = FormColors.blend(rgb, white, 90); 165 } 166 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 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 rgb = toolkit.getColors().getSystemColor(SWT.COLOR_LIST_SELECTION); 178 rgb = FormColors.blend(rgb, white, 30); 180 if (FormColors.testTwoPrimaryColors(rgb, 200, 221)) 183 rgb = FormColors.blend(rgb, black, 80); 184 else if (FormColors.testTwoPrimaryColors(rgb, 222, 241)) 187 rgb = FormColors.blend(rgb, black, 60); 188 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 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 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 |