1 11 package org.eclipse.help.ui.internal.views; 12 13 import org.eclipse.help.ui.internal.Messages; 14 import org.eclipse.jface.action.ContributionItem; 15 import org.eclipse.jface.action.IContributionItem; 16 import org.eclipse.jface.action.ToolBarManager; 17 import org.eclipse.jface.dialogs.DialogTray; 18 import org.eclipse.jface.dialogs.IDialogPage; 19 import org.eclipse.jface.dialogs.IPageChangeProvider; 20 import org.eclipse.jface.dialogs.IPageChangedListener; 21 import org.eclipse.jface.dialogs.PageChangedEvent; 22 import org.eclipse.jface.dialogs.TrayDialog; 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.graphics.Color; 25 import org.eclipse.swt.graphics.GC; 26 import org.eclipse.swt.graphics.Image; 27 import org.eclipse.swt.graphics.ImageData; 28 import org.eclipse.swt.graphics.PaletteData; 29 import org.eclipse.swt.graphics.Point; 30 import org.eclipse.swt.graphics.RGB; 31 import org.eclipse.swt.layout.GridData; 32 import org.eclipse.swt.layout.GridLayout; 33 import org.eclipse.swt.widgets.Composite; 34 import org.eclipse.swt.widgets.Control; 35 import org.eclipse.swt.widgets.Display; 36 import org.eclipse.swt.widgets.Event; 37 import org.eclipse.swt.widgets.Label; 38 import org.eclipse.swt.widgets.Listener; 39 import org.eclipse.swt.widgets.Shell; 40 import org.eclipse.swt.widgets.TabFolder; 41 import org.eclipse.swt.widgets.TabItem; 42 import org.eclipse.swt.widgets.ToolBar; 43 import org.eclipse.swt.widgets.ToolItem; 44 import org.eclipse.ui.PlatformUI; 45 import org.eclipse.ui.forms.HyperlinkGroup; 46 import org.eclipse.ui.forms.widgets.FormToolkit; 47 48 52 public class HelpTray extends DialogTray implements IPageChangedListener { 53 54 public static final int MINIMUM_HEIGHT = 450; 55 private static final int DEFAULT_WIDTH = 210; 56 57 private int originalHeight; 58 private int heightAdded; 59 60 private FormToolkit toolkit; 61 private ReusableHelpPart helpPart; 62 private Shell shell; 63 private IContributionItem closeAction; 64 private Image normal; 65 private Image hover; 66 67 70 private void createActions() { 71 createImages(); 72 closeAction = new ContributionItem() { 73 public void fill(ToolBar parent, int index) { 74 final ToolItem item = new ToolItem(parent, SWT.PUSH); 75 item.setImage(normal); 76 item.setHotImage(hover); 77 item.setToolTipText(Messages.ReusableHelpPart_closeAction_tooltip); 78 item.addListener(SWT.Selection, new Listener() { 79 public void handleEvent(Event event) { 80 TrayDialog dialog = (TrayDialog)shell.getData(); 82 dialog.closeTray(); 83 84 shell.setFocus(); 86 } 87 }); 88 } 89 }; 90 } 91 92 97 protected Control createContents(Composite parent) { 98 ensureMinimumHeight(parent.getShell()); 100 101 toolkit = new FormToolkit(parent.getDisplay()); 102 toolkit.getHyperlinkGroup().setHyperlinkUnderlineMode(HyperlinkGroup.UNDERLINE_HOVER); 103 toolkit.getColors().initializeSectionToolBarColors(); 104 Composite container = new Composite(parent, SWT.NONE); 105 GridLayout layout = new GridLayout(); 106 layout.marginWidth = layout.marginHeight = 0; 107 layout.verticalSpacing = 0; 108 container.setLayout(layout); 109 container.addListener(SWT.Dispose, new Listener() { 110 public void handleEvent(Event event) { 111 dispose(); 112 } 113 }); 114 115 ToolBarManager tbm = new ToolBarManager(SWT.FLAT); 116 tbm.createControl(container); 117 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_END); 118 gd.grabExcessHorizontalSpace = true; 119 tbm.getControl().setLayoutData(gd); 120 Label separator = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL); 121 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 122 gd.heightHint = 1; 123 separator.setLayoutData(gd); 124 helpPart = new ReusableHelpPart(PlatformUI.getWorkbench().getProgressService()); 125 helpPart.init(null, tbm, null, null); 126 helpPart.setDefaultContextHelpText(Messages.HelpView_defaultText); 127 helpPart.createControl(container, toolkit); 128 gd = new GridData(GridData.FILL_BOTH); 129 gd.widthHint = DEFAULT_WIDTH; 130 helpPart.getControl().setLayoutData(gd); 131 132 createActions(); 133 tbm.add(closeAction); 134 135 shell = parent.getShell(); 136 hookPageChangeListener(shell); 137 helpPart.getControl().addListener(SWT.Dispose, new Listener() { 138 public void handleEvent(Event event) { 139 unhookPageChangeListener(shell); 140 } 141 }); 142 143 return container; 144 } 145 146 149 private void createImages() { 150 Display display = Display.getCurrent(); 151 int[] shape = new int[] { 152 3, 3, 5, 3, 7, 5, 8, 5, 10, 3, 12, 3, 153 12, 5, 10, 7, 10, 8, 12,10, 12,12, 154 10,12, 8, 10, 7, 10, 5, 12, 3, 12, 155 3, 10, 5, 8, 5, 7, 3, 5 156 }; 157 158 161 Color border = display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW); 162 Color background = display.getSystemColor(SWT.COLOR_LIST_BACKGROUND); 163 Color backgroundHot = new Color(display, new RGB(252, 160, 160)); 164 Color transparent = display.getSystemColor(SWT.COLOR_MAGENTA); 165 166 PaletteData palette = new PaletteData(new RGB[] { transparent.getRGB(), border.getRGB(), background.getRGB(), backgroundHot.getRGB() }); 167 ImageData data = new ImageData(16, 16, 8, palette); 168 data.transparentPixel = 0; 169 170 normal = new Image(display, data); 171 normal.setBackground(transparent); 172 GC gc = new GC(normal); 173 gc.setBackground(background); 174 gc.fillPolygon(shape); 175 gc.setForeground(border); 176 gc.drawPolygon(shape); 177 gc.dispose(); 178 179 hover = new Image(display, data); 180 hover.setBackground(transparent); 181 gc = new GC(hover); 182 gc.setBackground(backgroundHot); 183 gc.fillPolygon(shape); 184 gc.setForeground(border); 185 gc.drawPolygon(shape); 186 gc.dispose(); 187 188 backgroundHot.dispose(); 189 } 190 191 194 private void dispose() { 195 normal.dispose(); 196 hover.dispose(); 197 toolkit.dispose(); 198 helpPart.dispose(); 199 200 204 shell.addListener(SWT.Resize, new Listener() { 205 public void handleEvent(Event event) { 206 shell.removeListener(SWT.Resize, this); 207 Point p = shell.getSize(); 208 if (heightAdded > 0 && p.y > originalHeight) { 209 p.y = Math.max(p.y - heightAdded, originalHeight); 210 shell.setSize(p); 211 } 212 } 213 }); 214 } 215 216 223 private void ensureMinimumHeight(Shell shell) { 224 Point p = shell.getSize(); 225 originalHeight = p.y; 226 if (p.y < MINIMUM_HEIGHT) { 227 heightAdded = MINIMUM_HEIGHT - p.y; 228 p.y = MINIMUM_HEIGHT; 229 shell.setSize(p); 230 } 231 else { 232 heightAdded = 0; 233 } 234 } 235 236 241 public ReusableHelpPart getHelpPart() { 242 return helpPart; 243 } 244 245 251 private void hookPageChangeListener(Composite parent) { 252 Object data = parent.getData(); 253 if (data instanceof IPageChangeProvider) { 254 ((IPageChangeProvider)data).addPageChangedListener(this); 255 } 256 } 257 258 266 public static boolean isAppropriateFor(Shell shell) { 267 if (shell != null && !shell.isDisposed() && shell.isVisible()) { 268 Object data = shell.getData(); 269 return (data instanceof TrayDialog && (shell.getSize().y >= MINIMUM_HEIGHT || (shell.getStyle() & SWT.RESIZE) != 0)); 270 } 271 return false; 272 } 273 274 280 public void pageChanged(PageChangedEvent event) { 281 Object page = event.getSelectedPage(); 282 Control c = null; 283 if (page instanceof IDialogPage) { 284 c = ((IDialogPage) page).getControl(); 285 } else { 286 c = shell.getDisplay().getFocusControl(); 287 if (c instanceof TabFolder) { 288 TabFolder folder = (TabFolder) c; 289 TabItem[] selection = folder.getSelection(); 290 if (selection.length == 1) { 291 c = selection[0].getControl(); 292 } 293 } 294 } 295 helpPart.update(null, null, c); 296 } 297 298 304 private void unhookPageChangeListener(Composite parent) { 305 Object data = parent.getData(); 306 if (data instanceof IPageChangeProvider) { 307 ((IPageChangeProvider)data).removePageChangedListener(this); 308 } 309 } 310 } 311 | Popular Tags |