1 12 package org.eclipse.ui.internal.forms.widgets; 13 14 import com.ibm.icu.text.BreakIterator; 15 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.custom.ScrolledComposite; 18 import org.eclipse.swt.events.MouseEvent; 19 import org.eclipse.swt.graphics.Device; 20 import org.eclipse.swt.graphics.Font; 21 import org.eclipse.swt.graphics.FontData; 22 import org.eclipse.swt.graphics.FontMetrics; 23 import org.eclipse.swt.graphics.GC; 24 import org.eclipse.swt.graphics.Image; 25 import org.eclipse.swt.graphics.ImageData; 26 import org.eclipse.swt.graphics.Point; 27 import org.eclipse.swt.graphics.Rectangle; 28 import org.eclipse.swt.layout.GridData; 29 import org.eclipse.swt.widgets.Combo; 30 import org.eclipse.swt.widgets.Composite; 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.swt.widgets.Display; 33 import org.eclipse.swt.widgets.Label; 34 import org.eclipse.swt.widgets.Layout; 35 import org.eclipse.swt.widgets.ScrollBar; 36 import org.eclipse.swt.widgets.Text; 37 import org.eclipse.ui.forms.widgets.ColumnLayout; 38 import org.eclipse.ui.forms.widgets.Form; 39 import org.eclipse.ui.forms.widgets.FormToolkit; 40 import org.eclipse.ui.forms.widgets.ILayoutExtension; 41 42 public class FormUtil { 43 public static final String PLUGIN_ID = "org.eclipse.ui.forms"; 45 static final int H_SCROLL_INCREMENT = 5; 46 47 static final int V_SCROLL_INCREMENT = 64; 48 49 public static final String DEBUG = PLUGIN_ID + "/debug"; 51 public static final String DEBUG_TEXT = DEBUG + "/text"; public static final String DEBUG_TEXTSIZE = DEBUG + "/textsize"; 54 public static final String DEBUG_FOCUS = DEBUG + "/focus"; 56 public static final String FOCUS_SCROLLING = "focusScrolling"; 58 public static final String IGNORE_BODY = "__ignore_body__"; 60 public static Text createText(Composite parent, String label, 61 FormToolkit factory) { 62 return createText(parent, label, factory, 1); 63 } 64 65 public static Text createText(Composite parent, String label, 66 FormToolkit factory, int span) { 67 factory.createLabel(parent, label); 68 Text text = factory.createText(parent, ""); int hfill = span == 1 ? GridData.FILL_HORIZONTAL 70 : GridData.HORIZONTAL_ALIGN_FILL; 71 GridData gd = new GridData(hfill | GridData.VERTICAL_ALIGN_CENTER); 72 gd.horizontalSpan = span; 73 text.setLayoutData(gd); 74 return text; 75 } 76 77 public static Text createText(Composite parent, String label, 78 FormToolkit factory, int span, int style) { 79 Label l = factory.createLabel(parent, label); 80 if ((style & SWT.MULTI) != 0) { 81 GridData gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING); 82 l.setLayoutData(gd); 83 } 84 Text text = factory.createText(parent, "", style); int hfill = span == 1 ? GridData.FILL_HORIZONTAL 86 : GridData.HORIZONTAL_ALIGN_FILL; 87 GridData gd = new GridData(hfill | GridData.VERTICAL_ALIGN_CENTER); 88 gd.horizontalSpan = span; 89 text.setLayoutData(gd); 90 return text; 91 } 92 93 public static Text createText(Composite parent, FormToolkit factory, 94 int span) { 95 Text text = factory.createText(parent, ""); int hfill = span == 1 ? GridData.FILL_HORIZONTAL 97 : GridData.HORIZONTAL_ALIGN_FILL; 98 GridData gd = new GridData(hfill | GridData.VERTICAL_ALIGN_CENTER); 99 gd.horizontalSpan = span; 100 text.setLayoutData(gd); 101 return text; 102 } 103 104 public static int computeMinimumWidth(GC gc, String text) { 105 BreakIterator wb = BreakIterator.getWordInstance(); 106 wb.setText(text); 107 int last = 0; 108 109 int width = 0; 110 111 for (int loc = wb.first(); loc != BreakIterator.DONE; loc = wb.next()) { 112 String word = text.substring(last, loc); 113 Point extent = gc.textExtent(word); 114 width = Math.max(width, extent.x); 115 last = loc; 116 } 117 String lastWord = text.substring(last); 118 Point extent = gc.textExtent(lastWord); 119 width = Math.max(width, extent.x); 120 return width; 121 } 122 123 public static Point computeWrapSize(GC gc, String text, int wHint) { 124 BreakIterator wb = BreakIterator.getWordInstance(); 125 wb.setText(text); 126 FontMetrics fm = gc.getFontMetrics(); 127 int lineHeight = fm.getHeight(); 128 129 int saved = 0; 130 int last = 0; 131 int height = lineHeight; 132 int maxWidth = 0; 133 134 for (int loc = wb.first(); loc != BreakIterator.DONE; loc = wb.next()) { 135 String word = text.substring(saved, loc); 136 Point extent = gc.textExtent(word); 137 if (extent.x > wHint) { 138 saved = last; 140 height += extent.y; 141 } else { 142 maxWidth = Math.max(maxWidth, extent.x); 143 } 144 last = loc; 145 } 146 return new Point(maxWidth, height); 147 } 148 149 public static void paintWrapText(GC gc, String text, Rectangle bounds) { 150 paintWrapText(gc, text, bounds, false); 151 } 152 153 public static void paintWrapText(GC gc, String text, Rectangle bounds, 154 boolean underline) { 155 BreakIterator wb = BreakIterator.getWordInstance(); 156 wb.setText(text); 157 FontMetrics fm = gc.getFontMetrics(); 158 int lineHeight = fm.getHeight(); 159 int descent = fm.getDescent(); 160 161 int saved = 0; 162 int last = 0; 163 int y = bounds.y; 164 int width = bounds.width; 165 166 for (int loc = wb.first(); loc != BreakIterator.DONE; loc = wb.next()) { 167 String line = text.substring(saved, loc); 168 Point extent = gc.textExtent(line); 169 170 if (extent.x > width) { 171 String prevLine = text.substring(saved, last); 173 gc.drawText(prevLine, bounds.x, y, true); 174 if (underline) { 175 Point prevExtent = gc.textExtent(prevLine); 176 int lineY = y + lineHeight - descent + 1; 177 gc 178 .drawLine(bounds.x, lineY, bounds.x + prevExtent.x, 179 lineY); 180 } 181 182 saved = last; 183 y += lineHeight; 184 } 185 last = loc; 186 } 187 String lastLine = text.substring(saved, last); 189 gc.drawText(lastLine, bounds.x, y, true); 190 if (underline) { 191 int lineY = y + lineHeight - descent + 1; 192 Point lastExtent = gc.textExtent(lastLine); 193 gc.drawLine(bounds.x, lineY, bounds.x + lastExtent.x, lineY); 194 } 195 } 196 197 public static ScrolledComposite getScrolledComposite(Control c) { 198 Composite parent = c.getParent(); 199 200 while (parent != null) { 201 if (parent instanceof ScrolledComposite) { 202 return (ScrolledComposite) parent; 203 } 204 parent = parent.getParent(); 205 } 206 return null; 207 } 208 209 public static void ensureVisible(Control c) { 210 ScrolledComposite scomp = getScrolledComposite(c); 211 if (scomp != null) { 212 Object data = scomp.getData(FOCUS_SCROLLING); 213 if (data == null || !data.equals(Boolean.FALSE)) 214 FormUtil.ensureVisible(scomp, c); 215 } 216 } 217 218 public static void ensureVisible(ScrolledComposite scomp, Control control) { 219 Point controlSize = control.getSize(); 220 Point controlOrigin = getControlLocation(scomp, control); 221 ensureVisible(scomp, controlOrigin, controlSize); 222 } 223 224 public static void ensureVisible(ScrolledComposite scomp, 225 Point controlOrigin, Point controlSize) { 226 Rectangle area = scomp.getClientArea(); 227 Point scompOrigin = scomp.getOrigin(); 228 229 int x = scompOrigin.x; 230 int y = scompOrigin.y; 231 232 if (controlSize.x < area.width 235 && (controlOrigin.x + controlSize.x > scompOrigin.x 236 + area.width)) { 237 x = controlOrigin.x + controlSize.x - area.width; 238 } 239 if (controlOrigin.x < x) { 242 if (controlSize.x < area.width) 243 x = controlOrigin.x + controlSize.x - area.width; 244 else 245 x = controlOrigin.x; 246 } 247 if (controlSize.y < area.height 249 && (controlOrigin.y + controlSize.y > scompOrigin.y 250 + area.height)) { 251 y = controlOrigin.y + controlSize.y - area.height; 252 } 253 if (controlOrigin.y < y) { 256 if (controlSize.y < area.height) 257 y = controlOrigin.y + controlSize.y - area.height; 258 else 259 y = controlOrigin.y; 260 } 261 262 if (scompOrigin.x != x || scompOrigin.y != y) { 263 scomp.setOrigin(x, y); 265 } 266 } 267 268 public static void ensureVisible(ScrolledComposite scomp, Control control, 269 MouseEvent e) { 270 Point controlOrigin = getControlLocation(scomp, control); 271 int rX = controlOrigin.x + e.x; 272 int rY = controlOrigin.y + e.y; 273 Rectangle area = scomp.getClientArea(); 274 Point scompOrigin = scomp.getOrigin(); 275 276 int x = scompOrigin.x; 277 int y = scompOrigin.y; 278 281 if (rX > scompOrigin.x + area.width) { 283 x = rX - area.width; 284 } 285 else if (rX < x) { 287 x = rX; 288 } 289 if (rY > scompOrigin.y + area.height) { 291 y = rY - area.height; 292 } 293 else if (rY < y) { 295 y = rY; 296 } 297 298 if (scompOrigin.x != x || scompOrigin.y != y) { 299 scomp.setOrigin(x, y); 301 } 302 } 303 304 public static Point getControlLocation(ScrolledComposite scomp, 305 Control control) { 306 int x = 0; 307 int y = 0; 308 Control content = scomp.getContent(); 309 Control currentControl = control; 310 for (;;) { 311 if (currentControl == content) 312 break; 313 Point location = currentControl.getLocation(); 314 x += location.x; 319 y += location.y; 320 currentControl = currentControl.getParent(); 321 } 322 return new Point(x, y); 323 } 324 325 static void scrollVertical(ScrolledComposite scomp, boolean up) { 326 scroll(scomp, 0, up ? -V_SCROLL_INCREMENT : V_SCROLL_INCREMENT); 327 } 328 329 static void scrollHorizontal(ScrolledComposite scomp, boolean left) { 330 scroll(scomp, left ? -H_SCROLL_INCREMENT : H_SCROLL_INCREMENT, 0); 331 } 332 333 static void scrollPage(ScrolledComposite scomp, boolean up) { 334 Rectangle clientArea = scomp.getClientArea(); 335 int increment = up ? -clientArea.height : clientArea.height; 336 scroll(scomp, 0, increment); 337 } 338 339 static void scroll(ScrolledComposite scomp, int xoffset, int yoffset) { 340 Point origin = scomp.getOrigin(); 341 Point contentSize = scomp.getContent().getSize(); 342 int xorigin = origin.x + xoffset; 343 int yorigin = origin.y + yoffset; 344 xorigin = Math.max(xorigin, 0); 345 xorigin = Math.min(xorigin, contentSize.x - 1); 346 yorigin = Math.max(yorigin, 0); 347 yorigin = Math.min(yorigin, contentSize.y - 1); 348 scomp.setOrigin(xorigin, yorigin); 349 } 350 351 public static void updatePageIncrement(ScrolledComposite scomp) { 352 ScrollBar vbar = scomp.getVerticalBar(); 353 if (vbar != null) { 354 Rectangle clientArea = scomp.getClientArea(); 355 int increment = clientArea.height - 5; 356 vbar.setPageIncrement(increment); 357 } 358 } 359 360 public static void processKey(int keyCode, Control c) { 361 ScrolledComposite scomp = FormUtil.getScrolledComposite(c); 362 if (scomp != null) { 363 if (c instanceof Combo) 364 return; 365 switch (keyCode) { 366 case SWT.ARROW_DOWN: 367 if (scomp.getData("novarrows") == null) FormUtil.scrollVertical(scomp, false); 369 break; 370 case SWT.ARROW_UP: 371 if (scomp.getData("novarrows") == null) FormUtil.scrollVertical(scomp, true); 373 break; 374 case SWT.ARROW_LEFT: 375 FormUtil.scrollHorizontal(scomp, true); 376 break; 377 case SWT.ARROW_RIGHT: 378 FormUtil.scrollHorizontal(scomp, false); 379 break; 380 case SWT.PAGE_UP: 381 FormUtil.scrollPage(scomp, true); 382 break; 383 case SWT.PAGE_DOWN: 384 FormUtil.scrollPage(scomp, false); 385 break; 386 } 387 } 388 } 389 390 static boolean isWrapControl(Control c) { 391 if ((c.getStyle() & SWT.WRAP) != 0) 392 return true; 393 if (c instanceof Composite) { 394 return ((Composite) c).getLayout() instanceof ILayoutExtension; 395 } 396 return false; 397 } 398 399 public static int getWidthHint(int wHint, Control c) { 400 boolean wrap = isWrapControl(c); 401 return wrap ? wHint : SWT.DEFAULT; 402 } 403 404 public static int getHeightHint(int hHint, Control c) { 405 if (c instanceof Composite) { 406 Layout layout = ((Composite) c).getLayout(); 407 if (layout instanceof ColumnLayout) 408 return hHint; 409 } 410 return SWT.DEFAULT; 411 } 412 413 public static int computeMinimumWidth(Control c, boolean changed) { 414 if (c instanceof Composite) { 415 Layout layout = ((Composite) c).getLayout(); 416 if (layout instanceof ILayoutExtension) 417 return ((ILayoutExtension) layout).computeMinimumWidth( 418 (Composite) c, changed); 419 } 420 return c.computeSize(FormUtil.getWidthHint(5, c), SWT.DEFAULT, changed).x; 421 } 422 423 public static int computeMaximumWidth(Control c, boolean changed) { 424 if (c instanceof Composite) { 425 Layout layout = ((Composite) c).getLayout(); 426 if (layout instanceof ILayoutExtension) 427 return ((ILayoutExtension) layout).computeMaximumWidth( 428 (Composite) c, changed); 429 } 430 return c.computeSize(SWT.DEFAULT, SWT.DEFAULT, changed).x; 431 } 432 433 public static Form getForm(Control c) { 434 Composite parent = c.getParent(); 435 while (parent != null) { 436 if (parent instanceof Form) { 437 return (Form) parent; 438 } 439 parent = parent.getParent(); 440 } 441 return null; 442 } 443 444 public static Image createAlphaMashImage(Device device, Image srcImage) { 445 Rectangle bounds = srcImage.getBounds(); 446 int alpha = 0; 447 int calpha = 0; 448 ImageData data = srcImage.getImageData(); 449 for (int i = 0; i < bounds.height; i++) { 454 alpha = calpha; 456 for (int j = 0; j < bounds.width; j++) { 457 data.setAlpha(j, i, alpha); 459 alpha = alpha == 255 ? 0 : 255; 460 } 461 calpha = calpha == 255 ? 0 : 255; 462 } 463 return new Image(device, data); 464 } 465 466 public static Font createBoldFont(Display display, Font regularFont) { 467 FontData[] fontDatas = regularFont.getFontData(); 468 for (int i = 0; i < fontDatas.length; i++) { 469 fontDatas[i].setStyle(fontDatas[i].getStyle() | SWT.BOLD); 470 } 471 return new Font(display, fontDatas); 472 } 473 474 public static boolean mnemonicMatch(String text, char key) { 475 char mnemonic = findMnemonic(text); 476 if (mnemonic == '\0') 477 return false; 478 return Character.toUpperCase(key) == Character.toUpperCase(mnemonic); 479 } 480 481 private static char findMnemonic(String string) { 482 int index = 0; 483 int length = string.length(); 484 do { 485 while (index < length && string.charAt(index) != '&') 486 index++; 487 if (++index >= length) 488 return '\0'; 489 if (string.charAt(index) != '&') 490 return string.charAt(index); 491 index++; 492 } while (index < length); 493 return '\0'; 494 } 495 496 public static void setFocusScrollingEnabled(Control c, boolean enabled) { 497 ScrolledComposite scomp = null; 498 499 if (c instanceof ScrolledComposite) 500 scomp = (ScrolledComposite)c; 501 else 502 scomp = getScrolledComposite(c); 503 if (scomp!=null) 504 scomp.setData(FormUtil.FOCUS_SCROLLING, enabled?null:Boolean.FALSE); 505 } 506 507 public static void setAntialias(GC gc, int style) { 508 if (!gc.getAdvanced()) { 509 gc.setAdvanced(true); 510 if (!gc.getAdvanced()) 511 return; 512 } 513 gc.setAntialias(style); 514 } 515 } 516
| Popular Tags
|