1 /******************************************************************************* 2 * Copyright (c) 2007, 2009 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.pde.api.tools.ui.internal; 15 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.IAdaptable; 19 import org.eclipse.jface.dialogs.IDialogConstants; 20 import org.eclipse.jface.layout.PixelConverter; 21 import org.eclipse.jface.resource.JFaceResources; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.graphics.Font; 24 import org.eclipse.swt.graphics.Image; 25 import org.eclipse.swt.layout.GridData; 26 import org.eclipse.swt.layout.GridLayout; 27 import org.eclipse.swt.widgets.Button; 28 import org.eclipse.swt.widgets.Combo; 29 import org.eclipse.swt.widgets.Composite; 30 import org.eclipse.swt.widgets.Group; 31 import org.eclipse.swt.widgets.Label; 32 import org.eclipse.swt.widgets.Layout; 33 import org.eclipse.swt.widgets.Link; 34 import org.eclipse.swt.widgets.Shell; 35 import org.eclipse.swt.widgets.Text; 36 import org.eclipse.ui.dialogs.PreferencesUtil; 37 import org.eclipse.ui.forms.widgets.ExpandableComposite; 38 import org.eclipse.ui.forms.widgets.SharedScrolledComposite; 39 40 /** 41 * Factory class to create some SWT resources. 42 * 43 * @since 1.0.0 44 */ 45 public class SWTFactory { 46 47 /** 48 * Returns a width hint for a button control. 49 */ getButtonWidthHint(Button button)50 public static int getButtonWidthHint(Button button) { 51 button.setFont(JFaceResources.getDialogFont()); 52 PixelConverter converter= new PixelConverter(button); 53 int widthHint = converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 54 return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); 55 } 56 57 /** 58 * Sets width and height hint for the button control. 59 * <b>Note:</b> This is a NOP if the button's layout data is not 60 * an instance of <code>GridData</code>. 61 * 62 * @param the button for which to set the dimension hint 63 */ setButtonDimensionHint(Button button)64 public static void setButtonDimensionHint(Button button) { 65 Assert.isNotNull(button); 66 Object gd = button.getLayoutData(); 67 if (gd instanceof GridData) { 68 ((GridData)gd).widthHint = getButtonWidthHint(button); 69 ((GridData)gd).horizontalAlignment = GridData.FILL; 70 } 71 } 72 73 /** 74 * Creates a check box button using the parents' font 75 * 76 * @param parent the parent to add the button to 77 * @param label the label for the button 78 * @param image the image for the button 79 * @param checked the initial checked state of the button 80 * @param hspan the horizontal span to take up in the parent composite 81 * @return a new checked button set to the initial checked state 82 */ createCheckButton(Composite parent, String label, Image image, boolean checked, int hspan)83 public static Button createCheckButton(Composite parent, String label, Image image, boolean checked, int hspan) { 84 Button button = new Button(parent, SWT.CHECK); 85 button.setFont(parent.getFont()); 86 button.setSelection(checked); 87 if(image != null) { 88 button.setImage(image); 89 } 90 if(label != null) { 91 button.setText(label); 92 } 93 GridData gd = new GridData(); 94 gd.horizontalSpan = hspan; 95 button.setLayoutData(gd); 96 return button; 97 } 98 99 /** 100 * Creates and returns a new push button with the given 101 * label and/or image. 102 * 103 * @param parent parent control 104 * @param label button label or <code>null</code> 105 * @param image image of <code>null</code> 106 * @return a new push button 107 */ createPushButton(Composite parent, String label, Image image)108 public static Button createPushButton(Composite parent, String label, Image image) { 109 Button button = new Button(parent, SWT.PUSH); 110 button.setFont(parent.getFont()); 111 if (image != null) { 112 button.setImage(image); 113 } 114 if (label != null) { 115 button.setText(label); 116 } 117 GridData gd = new GridData(); 118 button.setLayoutData(gd); 119 SWTFactory.setButtonDimensionHint(button); 120 return button; 121 } 122 123 /** 124 * Creates and returns a new push button with the given 125 * label and/or image. 126 * 127 * @param parent parent control 128 * @param label button label or <code>null</code> 129 * @param image image of <code>null</code> 130 * @param fill the alignment for the new button 131 * @return a new push button 132 */ createPushButton(Composite parent, String label, Image image, int fill)133 public static Button createPushButton(Composite parent, String label, Image image, int fill) { 134 Button button = new Button(parent, SWT.PUSH); 135 button.setFont(parent.getFont()); 136 if (image != null) { 137 button.setImage(image); 138 } 139 if (label != null) { 140 button.setText(label); 141 } 142 GridData gd = new GridData(fill); 143 button.setLayoutData(gd); 144 SWTFactory.setButtonDimensionHint(button); 145 return button; 146 } 147 148 /** 149 * Creates and returns a new radio button with the given 150 * label. 151 * 152 * @param parent parent control 153 * @param label button label or <code>null</code> 154 * @return a new radio button 155 */ createRadioButton(Composite parent, String label)156 public static Button createRadioButton(Composite parent, String label) { 157 Button button = new Button(parent, SWT.RADIO); 158 button.setFont(parent.getFont()); 159 if (label != null) { 160 button.setText(label); 161 } 162 GridData gd = new GridData(); 163 button.setLayoutData(gd); 164 SWTFactory.setButtonDimensionHint(button); 165 return button; 166 } 167 168 /** 169 * Creates a new label widget 170 * 171 * @param parent the parent composite to add this label widget to 172 * @param text the text for the label 173 * @param hspan the horizontal span to take up in the parent composite 174 * @return the new label 175 */ createLabel(Composite parent, String text, int hspan)176 public static Label createLabel(Composite parent, String text, int hspan) { 177 Label l = new Label(parent, SWT.NONE); 178 l.setFont(parent.getFont()); 179 if(text != null) { 180 l.setText(text); 181 } 182 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 183 gd.horizontalSpan = hspan; 184 gd.grabExcessHorizontalSpace = false; 185 l.setLayoutData(gd); 186 return l; 187 } 188 189 /** 190 * Creates a new label widget 191 * 192 * @param parent the parent composite to add this label widget to 193 * @param text the text for the label 194 * @param font the font for the label 195 * @param hspan the horizontal span to take up in the parent composite 196 * @return the new label 197 */ createLabel(Composite parent, String text, Font font, int hspan)198 public static Label createLabel(Composite parent, String text, Font font, int hspan) { 199 Label l = new Label(parent, SWT.NONE); 200 l.setFont(font); 201 if(text != null) { 202 l.setText(text); 203 } 204 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 205 gd.horizontalSpan = hspan; 206 l.setLayoutData(gd); 207 return l; 208 } 209 210 /** 211 * Creates a new link widget 212 * 213 * @param parent the parent composite to add this label widget to 214 * @param text the text for the label 215 * @param font the font for the label 216 * @param hspan the horizontal span to take up in the parent composite 217 * @return the new label 218 */ createLink(Composite parent, String text, Font font, int hspan)219 public static Link createLink(Composite parent, String text, Font font, int hspan) { 220 Link l = new Link(parent, SWT.UNDERLINE_LINK); 221 l.setFont(font); 222 l.setText(text); 223 GridData gd = new GridData(GridData.BEGINNING, GridData.VERTICAL_ALIGN_CENTER, true, false, hspan, 1); 224 l.setLayoutData(gd); 225 return l; 226 } 227 createLink(Composite parent, String text, Font font, int hspan, int fill)228 public static Link createLink(Composite parent, String text, Font font, int hspan, int fill) { 229 Link l = new Link(parent, SWT.UNDERLINE_LINK); 230 l.setFont(font); 231 l.setText(text); 232 GridData gd = new GridData(fill); 233 gd.horizontalSpan = hspan; 234 l.setLayoutData(gd); 235 return l; 236 } 237 238 239 /** 240 * Creates a wrapping label 241 * 242 * @param parent the parent composite to add this label to 243 * @param text the text to be displayed in the label 244 * @param hspan the horizontal span that label should take up in the parent composite 245 * @param wrapwidth the width hint that the label should wrap at 246 * @return a new label that wraps at a specified width 247 */ createWrapLabel(Composite parent, String text, int hspan, int wrapwidth)248 public static Label createWrapLabel(Composite parent, String text, int hspan, int wrapwidth) { 249 Label l = new Label(parent, SWT.NONE | SWT.WRAP); 250 l.setFont(parent.getFont()); 251 if(text != null) { 252 l.setText(text); 253 } 254 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 255 gd.horizontalSpan = hspan; 256 gd.widthHint = wrapwidth; 257 l.setLayoutData(gd); 258 return l; 259 } 260 261 /** 262 * Creates a wrapping label 263 * 264 * @param parent the parent composite to add this label to 265 * @param text the text to be displayed in the label 266 * @param hspan the horizontal span that label should take up in the parent composite 267 * @return a new label that wraps at a specified width 268 */ createWrapLabel(Composite parent, String text, int hspan)269 public static Label createWrapLabel(Composite parent, String text, int hspan) { 270 Label l = new Label(parent, SWT.NONE | SWT.WRAP); 271 l.setFont(parent.getFont()); 272 if(text != null) { 273 l.setText(text); 274 } 275 GridData gd = new GridData(GridData.BEGINNING); 276 gd.horizontalSpan = hspan; 277 l.setLayoutData(gd); 278 return l; 279 } 280 281 /** 282 * Creates a scrolled composite 283 * 284 * @param parent the parent to add to 285 * @param columns the number of columns for the composite 286 * @param hspan the horizontal span to take up in the parent 287 * @param marginwidth the width of the margins 288 * @param marginheight the height of the margins 289 * @return a new scrolled composite 290 */ createScrolledComposite(Composite parent, int columns, int hspan, int marginwidth, int marginheight)291 public static SharedScrolledComposite createScrolledComposite(Composite parent, int columns, int hspan, int marginwidth, int marginheight) { 292 SharedScrolledComposite comp = new SharedScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL) {}; 293 GridLayout layout = new GridLayout(columns, false); 294 layout.marginHeight = marginheight; 295 layout.marginWidth = marginwidth; 296 comp.setLayout(layout); 297 GridData gd = new GridData(GridData.FILL_BOTH); 298 gd.horizontalSpan = hspan; 299 comp.setLayoutData(gd); 300 comp.setExpandHorizontal(true); 301 comp.setExpandVertical(true); 302 return comp; 303 } 304 305 /** 306 * Creates an ExpandibleComposite widget 307 * 308 * @param parent the parent to add this widget to 309 * @param style the style for ExpandibleComposite expanding handle, and layout 310 * @param label the label for the widget 311 * @param hspan how many columns to span in the parent 312 * @param fill the fill style for the widget 313 * Can be one of <code>GridData.FILL_HORIZONAL</code>, <code>GridData.FILL_BOTH</code> or <code>GridData.FILL_VERTICAL</code> 314 * @return a new ExpandibleComposite widget 315 */ createExpandibleComposite(Composite parent, String label, int hspan, int fill)316 public static ExpandableComposite createExpandibleComposite(Composite parent, String label, int hspan, int fill) { 317 ExpandableComposite ex = new ExpandableComposite(parent, SWT.NONE, ExpandableComposite.TWISTIE | ExpandableComposite.CLIENT_INDENT); 318 ex.setText(label); 319 ex.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DIALOG_FONT)); 320 GridData gd = new GridData(fill); 321 gd.horizontalSpan = hspan; 322 gd.grabExcessHorizontalSpace = true; 323 ex.setLayoutData(gd); 324 return ex; 325 } 326 327 /** 328 * Creates a new text widget 329 * 330 * @param parent the parent composite to add this text widget to 331 * @param hspan the horizontal span to take up on the parent composite 332 * @return the new text widget 333 * 334 */ createSingleText(Composite parent, int hspan)335 public static Text createSingleText(Composite parent, int hspan) { 336 Text t = new Text(parent, SWT.SINGLE | SWT.BORDER); 337 t.setFont(parent.getFont()); 338 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 339 gd.horizontalSpan = hspan; 340 t.setLayoutData(gd); 341 return t; 342 } 343 344 /** 345 * Creates a new text widget 346 * 347 * @param parent the parent composite to add this text widget to 348 * @param style the style bits for the text widget 349 * @param hspan the horizontal span to take up on the parent composite 350 * @param fill the fill for the grid layout 351 * @return the new text widget 352 */ createText(Composite parent, int style, int hspan, int fill)353 public static Text createText(Composite parent, int style, int hspan, int fill) { 354 Text t = new Text(parent, style); 355 t.setFont(parent.getFont()); 356 GridData gd = new GridData(fill); 357 gd.horizontalSpan = hspan; 358 t.setLayoutData(gd); 359 return t; 360 } 361 362 /** 363 * Creates a new text widget 364 * 365 * @param parent the parent composite to add this text widget to 366 * @param style the style bits for the text widget 367 * @param hspan the horizontal span to take up on the parent composite 368 * @return the new text widget 369 */ createText(Composite parent, int style, int hspan)370 public static Text createText(Composite parent, int style, int hspan) { 371 Text t = new Text(parent, style); 372 t.setFont(parent.getFont()); 373 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 374 gd.horizontalSpan = hspan; 375 t.setLayoutData(gd); 376 return t; 377 } 378 379 /** 380 * Creates a new text widget 381 * 382 * @param parent the parent composite to add this text widget to 383 * @param style the style bits for the text widget 384 * @param hspan the horizontal span to take up on the parent composite 385 * @param width the desired width of the text widget 386 * @param height the desired height of the text widget 387 * @param fill the fill style for the widget 388 * @return the new text widget 389 */ createText(Composite parent, int style, int hspan, int width, int height, int fill)390 public static Text createText(Composite parent, int style, int hspan, int width, int height, int fill) { 391 Text t = new Text(parent, style); 392 t.setFont(parent.getFont()); 393 GridData gd = new GridData(fill); 394 gd.horizontalSpan = hspan; 395 gd.widthHint = width; 396 gd.heightHint = height; 397 t.setLayoutData(gd); 398 return t; 399 } 400 401 /** 402 * Creates a Group widget 403 * 404 * @param parent the parent composite to add this group to 405 * @param text the text for the heading of the group 406 * @param columns the number of columns within the group 407 * @param hspan the horizontal span the group should take up on the parent 408 * @param fill the style for how this composite should fill into its parent 409 * @return the new group 410 */ createGroup(Composite parent, String text, int columns, int hspan, int fill)411 public static Group createGroup(Composite parent, String text, int columns, int hspan, int fill) { 412 Group g = new Group(parent, SWT.NONE); 413 g.setLayout(new GridLayout(columns, false)); 414 g.setText(text); 415 g.setFont(parent.getFont()); 416 GridData gd = new GridData(fill); 417 gd.horizontalSpan = hspan; 418 g.setLayoutData(gd); 419 return g; 420 } 421 422 /** 423 * Creates a composite that uses the parent's font and has a grid layout 424 * 425 * @param parent the parent to add the composite to 426 * @param columns the number of columns the composite should have 427 * @param hspan the horizontal span the new composite should take up in the parent 428 * @param fill the fill style of the composite {@link GridData} 429 * @return a new composite with a grid layout 430 */ createComposite(Composite parent, int columns, int hspan, int fill)431 public static Composite createComposite(Composite parent, int columns, int hspan, int fill) { 432 Composite g = new Composite(parent, SWT.NONE); 433 g.setLayout(new GridLayout(columns, false)); 434 g.setFont(parent.getFont()); 435 GridData gd = new GridData(fill); 436 gd.horizontalSpan = hspan; 437 gd.grabExcessHorizontalSpace = true; 438 g.setLayoutData(gd); 439 return g; 440 } 441 442 /** 443 * Creates a composite that uses the parent's font and has a grid layout 444 * 445 * @param parent the parent to add the composite to 446 * @param font the font to use for this composite 447 * @param columns the number of columns the composite should have 448 * @param hspan the horizontal span the new composite should take up in the parent 449 * @param fill the fill style of the composite {@link GridData} 450 * @return a new composite with a grid layout 451 */ createComposite(Composite parent, Font font, int columns, int hspan, int fill)452 public static Composite createComposite(Composite parent, Font font, int columns, int hspan, int fill) { 453 Composite g = new Composite(parent, SWT.NONE); 454 g.setLayout(new GridLayout(columns, false)); 455 g.setFont(font); 456 GridData gd = new GridData(fill); 457 gd.horizontalSpan = hspan; 458 gd.grabExcessHorizontalSpace = true; 459 g.setLayoutData(gd); 460 return g; 461 } 462 463 /** 464 * Creates a composite that uses the parent's font and has a grid layout 465 * 466 * @param parent the parent to add the composite to 467 * @param columns the number of columns the composite should have 468 * @param hspan the horizontal span the new composite should take up in the parent 469 * @param fill the fill style of the composite {@link GridData} 470 * @param style the style of the composite 471 * @return a new composite with a grid layout 472 */ createComposite(Composite parent, int columns, int hspan, int fill, int style)473 public static Composite createComposite(Composite parent, int columns, int hspan, int fill, int style) { 474 Composite g = new Composite(parent, style); 475 g.setLayout(new GridLayout(columns, false)); 476 g.setFont(parent.getFont()); 477 GridData gd = new GridData(fill); 478 gd.horizontalSpan = hspan; 479 gd.grabExcessHorizontalSpace = true; 480 g.setLayoutData(gd); 481 return g; 482 } 483 484 /** 485 * Creates a vertical spacer for separating components. If applied to a 486 * <code>GridLayout</code>, this method will automatically span all of the columns of the parent 487 * to make vertical space 488 * 489 * @param parent the parent composite to add this spacer to 490 * @param numlines the number of vertical lines to make as space 491 */ createVerticalSpacer(Composite parent, int numlines)492 public static void createVerticalSpacer(Composite parent, int numlines) { 493 Label lbl = new Label(parent, SWT.NONE); 494 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 495 Layout layout = parent.getLayout(); 496 if(layout instanceof GridLayout) { 497 gd.horizontalSpan = ((GridLayout)parent.getLayout()).numColumns; 498 } 499 gd.heightHint = numlines; 500 lbl.setLayoutData(gd); 501 } 502 503 /** 504 * Creates a horizontal spacer for separating components 505 * 506 * @param comp 507 * @param numlines 508 */ createHorizontalSpacer(Composite comp, int numlines)509 public static void createHorizontalSpacer(Composite comp, int numlines) { 510 Label lbl = new Label(comp, SWT.NONE); 511 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 512 gd.horizontalSpan = numlines; 513 lbl.setLayoutData(gd); 514 } 515 516 /** 517 * Creates a Composite widget 518 * 519 * @param parent the parent composite to add this composite to 520 * @param columns the number of columns within the composite 521 * @param hspan the horizontal span the composite should take up on the parent 522 * @param fill the style for how this composite should fill into its parent 523 * @param marginwidth the width of the margin to place on the sides of the composite (default is 5, specified by GridLayout) 524 * @param marginheight the height of the margin to place o the top and bottom of the composite 525 * @return the new composite 526 */ createComposite(Composite parent, int columns, int hspan, int fill, int marginwidth, int marginheight)527 public static Composite createComposite(Composite parent, int columns, int hspan, int fill, int marginwidth, int marginheight) { 528 Composite g = new Composite(parent, SWT.NONE); 529 GridLayout layout = new GridLayout(columns, false); 530 layout.marginWidth = marginwidth; 531 layout.marginHeight = marginheight; 532 g.setLayout(layout); 533 g.setFont(parent.getFont()); 534 GridData gd = new GridData(fill); 535 gd.horizontalSpan = hspan; 536 g.setLayoutData(gd); 537 return g; 538 } 539 540 /** 541 * This method is used to make a combo box 542 * 543 * @param parent the parent composite to add the new combo to 544 * @param style the style for the Combo 545 * @param hspan the horizontal span to take up on the parent composite 546 * @param fill how the combo will fill into the composite 547 * Can be one of <code>GridData.FILL_HORIZONAL</code>, <code>GridData.FILL_BOTH</code> or <code>GridData.FILL_VERTICAL</code> 548 * @param items the item to put into the combo 549 * @return a new Combo instance 550 */ createCombo(Composite parent, int style, int hspan, int fill, String[] items)551 public static Combo createCombo(Composite parent, int style, int hspan, int fill, String[] items) { 552 Combo c = new Combo(parent, style); 553 c.setFont(parent.getFont()); 554 GridData gd = new GridData(fill); 555 gd.horizontalSpan = hspan; 556 c.setLayoutData(gd); 557 if (items != null){ 558 c.setItems(items); 559 } 560 c.select(0); 561 return c; 562 } 563 564 /** 565 * This method is used to make a combo box with a default fill style of GridData.FILL_HORIZONTAL 566 * 567 * @param parent the parent composite to add the new combo to 568 * @param style the style for the Combo 569 * @param hspan the horizontal span to take up on the parent composite 570 * @param items the item to put into the combo 571 * @return a new Combo instance 572 */ createCombo(Composite parent, int style, int hspan, String[] items)573 public static Combo createCombo(Composite parent, int style, int hspan, String[] items) { 574 Combo c = new Combo(parent, style); 575 c.setFont(parent.getFont()); 576 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 577 gd.horizontalSpan = hspan; 578 c.setLayoutData(gd); 579 if (items != null){ 580 c.setItems(items); 581 } 582 c.select(0); 583 return c; 584 } 585 586 /** 587 * This method allows us to open the preference dialog on the specific page, in this case the perspective page 588 * 589 * @param shell 590 * @param id the id of preference page to show 591 * @param page the actual page to show 592 */ showPreferencePage(Shell shell, String id, Object data)593 public static void showPreferencePage(Shell shell, String id, Object data) { 594 PreferencesUtil.createPreferenceDialogOn(shell, id, new String[] {id}, data).open(); 595 } 596 597 /** 598 * This method allows us to open a property page in the default platform dialog 599 * @param shell 600 * @param id 601 * @param element 602 * @param data 603 */ showPropertiesDialog(Shell shell, String id, IAdaptable element, Object data)604 public static void showPropertiesDialog(Shell shell, String id, IAdaptable element, Object data) { 605 PreferencesUtil.createPropertyDialogOn(shell, element, id, new String[] {id}, data).open(); 606 } 607 } 608