1 11 package org.eclipse.debug.internal.ui; 12 13 14 import org.eclipse.core.runtime.Assert; 15 import org.eclipse.jface.dialogs.IDialogConstants; 16 import org.eclipse.jface.resource.JFaceResources; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.custom.CLabel; 19 import org.eclipse.swt.graphics.Font; 20 import org.eclipse.swt.graphics.Image; 21 import org.eclipse.swt.layout.GridData; 22 import org.eclipse.swt.layout.GridLayout; 23 import org.eclipse.swt.widgets.Button; 24 import org.eclipse.swt.widgets.Combo; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Group; 27 import org.eclipse.swt.widgets.Label; 28 import org.eclipse.swt.widgets.Layout; 29 import org.eclipse.swt.widgets.Text; 30 import org.eclipse.ui.dialogs.PreferencesUtil; 31 32 35 public class SWTFactory { 36 37 40 public static int getButtonWidthHint(Button button) { 41 button.setFont(JFaceResources.getDialogFont()); 42 PixelConverter converter= new PixelConverter(button); 43 int widthHint= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 44 return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); 45 } 46 47 54 public static void setButtonDimensionHint(Button button) { 55 Assert.isNotNull(button); 56 Object gd= button.getLayoutData(); 57 if (gd instanceof GridData) { 58 ((GridData)gd).widthHint= getButtonWidthHint(button); 59 ((GridData)gd).horizontalAlignment = GridData.FILL; 60 } 61 } 62 63 73 public static Button createCheckButton(Composite parent, String label, Image image, boolean checked, int hspan) { 74 Button button = new Button(parent, SWT.CHECK); 75 button.setFont(parent.getFont()); 76 button.setSelection(checked); 77 if(image != null) { 78 button.setImage(image); 79 } 80 if(label != null) { 81 button.setText(label); 82 } 83 GridData gd = new GridData(); 84 gd.horizontalSpan = hspan; 85 button.setLayoutData(gd); 86 setButtonDimensionHint(button); 87 return button; 88 } 89 90 100 public static Button createPushButton(Composite parent, String label, Image image) { 101 Button button = new Button(parent, SWT.PUSH); 102 button.setFont(parent.getFont()); 103 if (image != null) { 104 button.setImage(image); 105 } 106 if (label != null) { 107 button.setText(label); 108 } 109 GridData gd = new GridData(); 110 button.setLayoutData(gd); 111 SWTFactory.setButtonDimensionHint(button); 112 return button; 113 } 114 115 124 public static Button createRadioButton(Composite parent, String label) { 125 Button button = new Button(parent, SWT.RADIO); 126 button.setFont(parent.getFont()); 127 if (label != null) { 128 button.setText(label); 129 } 130 GridData gd = new GridData(); 131 button.setLayoutData(gd); 132 SWTFactory.setButtonDimensionHint(button); 133 return button; 134 } 135 136 145 public static Label createLabel(Composite parent, String text, int hspan) { 146 Label l = new Label(parent, SWT.NONE); 147 l.setFont(parent.getFont()); 148 l.setText(text); 149 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 150 gd.horizontalSpan = hspan; 151 gd.grabExcessHorizontalSpace = false; 152 l.setLayoutData(gd); 153 return l; 154 } 155 156 165 public static Label createLabel(Composite parent, String text, Font font, int hspan) { 166 Label l = new Label(parent, SWT.NONE); 167 l.setFont(font); 168 l.setText(text); 169 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 170 gd.horizontalSpan = hspan; 171 l.setLayoutData(gd); 172 return l; 173 } 174 175 184 public static Label createWrapLabel(Composite parent, String text, int hspan, int wrapwidth) { 185 Label l = new Label(parent, SWT.NONE | SWT.WRAP); 186 l.setFont(parent.getFont()); 187 l.setText(text); 188 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 189 gd.horizontalSpan = hspan; 190 gd.widthHint = wrapwidth; 191 l.setLayoutData(gd); 192 return l; 193 } 194 195 205 public static CLabel createWrapCLabel(Composite parent, String text, Image image, int hspan, int wrapwidth) { 206 CLabel label = new CLabel(parent, SWT.NONE | SWT.WRAP); 207 label.setFont(parent.getFont()); 208 if(text != null) { 209 label.setText(text); 210 } 211 if(image != null) { 212 label.setImage(image); 213 } 214 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 215 gd.horizontalSpan = hspan; 216 gd.widthHint = wrapwidth; 217 label.setLayoutData(gd); 218 return label; 219 } 220 221 229 public static Label createWrapLabel(Composite parent, String text, int hspan) { 230 Label l = new Label(parent, SWT.NONE | SWT.WRAP); 231 l.setFont(parent.getFont()); 232 l.setText(text); 233 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 234 gd.horizontalSpan = hspan; 235 l.setLayoutData(gd); 236 return l; 237 } 238 239 247 public static Text createSingleText(Composite parent, int hspan) { 248 Text t = new Text(parent, SWT.SINGLE | SWT.BORDER); 249 t.setFont(parent.getFont()); 250 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 251 gd.horizontalSpan = hspan; 252 t.setLayoutData(gd); 253 return t; 254 } 255 256 265 public static Text createText(Composite parent, int style, int hspan, int fill) { 266 Text t = new Text(parent, style); 267 t.setFont(parent.getFont()); 268 GridData gd = new GridData(fill); 269 gd.horizontalSpan = hspan; 270 t.setLayoutData(gd); 271 return t; 272 } 273 274 282 public static Text createText(Composite parent, int style, int hspan) { 283 Text t = new Text(parent, style); 284 t.setFont(parent.getFont()); 285 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 286 gd.horizontalSpan = hspan; 287 t.setLayoutData(gd); 288 return t; 289 } 290 291 302 public static Text createText(Composite parent, int style, int hspan, int width, int height, int fill) { 303 Text t = new Text(parent, style); 304 t.setFont(parent.getFont()); 305 GridData gd = new GridData(fill); 306 gd.horizontalSpan = hspan; 307 gd.widthHint = width; 308 gd.heightHint = height; 309 t.setLayoutData(gd); 310 return t; 311 } 312 313 324 public static Group createGroup(Composite parent, String text, int columns, int hspan, int fill) { 325 Group g = new Group(parent, SWT.NONE); 326 g.setLayout(new GridLayout(columns, false)); 327 g.setText(text); 328 g.setFont(parent.getFont()); 329 GridData gd = new GridData(fill); 330 gd.horizontalSpan = hspan; 331 g.setLayoutData(gd); 332 return g; 333 } 334 335 344 public static Composite createComposite(Composite parent, Font font, int columns, int hspan, int fill) { 345 Composite g = new Composite(parent, SWT.NONE); 346 g.setLayout(new GridLayout(columns, false)); 347 g.setFont(font); 348 GridData gd = new GridData(fill); 349 gd.horizontalSpan = hspan; 350 g.setLayoutData(gd); 351 return g; 352 } 353 354 364 public static Composite createComposite(Composite parent, int columns, int hspan, int fill) { 365 Composite g = new Composite(parent, SWT.NONE); 366 g.setLayout(new GridLayout(columns, false)); 367 g.setFont(parent.getFont()); 368 GridData gd = new GridData(fill); 369 gd.horizontalSpan = hspan; 370 g.setLayoutData(gd); 371 return g; 372 } 373 374 383 public static void createVerticalSpacer(Composite parent, int numlines) { 384 Label lbl = new Label(parent, SWT.NONE); 385 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 386 Layout layout = parent.getLayout(); 387 if(layout instanceof GridLayout) { 388 gd.horizontalSpan = ((GridLayout)parent.getLayout()).numColumns; 389 } 390 gd.heightHint = numlines; 391 lbl.setLayoutData(gd); 392 } 393 394 400 public static void createHorizontalSpacer(Composite comp, int numlines) { 401 Label lbl = new Label(comp, SWT.NONE); 402 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 403 gd.horizontalSpan = numlines; 404 lbl.setLayoutData(gd); 405 } 406 407 417 public static Composite createComposite(Composite parent, Font font, int columns, int hspan, int fill, int marginwidth, int marginheight) { 418 Composite g = new Composite(parent, SWT.NONE); 419 GridLayout layout = new GridLayout(columns, false); 420 layout.marginWidth = marginwidth; 421 layout.marginHeight = marginheight; 422 g.setLayout(layout); 423 g.setFont(font); 424 GridData gd = new GridData(fill); 425 gd.horizontalSpan = hspan; 426 g.setLayoutData(gd); 427 return g; 428 } 429 430 441 public static Combo createCombo(Composite parent, int style, int hspan, int fill, String [] items) { 442 Combo c = new Combo(parent, style); 443 c.setFont(parent.getFont()); 444 GridData gd = new GridData(fill); 445 gd.horizontalSpan = hspan; 446 c.setLayoutData(gd); 447 c.setItems(items); 448 c.select(0); 449 return c; 450 } 451 452 461 public static Combo createCombo(Composite parent, int style, int hspan, String [] items) { 462 Combo c = new Combo(parent, style); 463 c.setFont(parent.getFont()); 464 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 465 gd.horizontalSpan = hspan; 466 c.setLayoutData(gd); 467 c.setItems(items); 468 c.select(0); 469 return c; 470 } 471 472 478 public static void showPreferencePage(String id) { 479 PreferencesUtil.createPreferenceDialogOn(DebugUIPlugin.getShell(), id, new String [] {id}, null).open(); 480 } 481 } 482 | Popular Tags |