1 11 package org.eclipse.jdt.internal.debug.ui; 12 13 14 import org.eclipse.jface.dialogs.IDialogConstants; 15 import org.eclipse.jface.resource.JFaceResources; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.graphics.Font; 18 import org.eclipse.swt.graphics.Image; 19 import org.eclipse.swt.layout.GridData; 20 import org.eclipse.swt.layout.GridLayout; 21 import org.eclipse.swt.widgets.Button; 22 import org.eclipse.swt.widgets.Combo; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.swt.widgets.Group; 25 import org.eclipse.swt.widgets.Label; 26 import org.eclipse.swt.widgets.Text; 27 import org.eclipse.ui.dialogs.PreferencesUtil; 28 import org.eclipse.ui.forms.widgets.ExpandableComposite; 29 30 34 public class SWTFactory { 35 36 39 public static int getButtonWidthHint(Button button) { 40 button.setFont(JFaceResources.getDialogFont()); 41 PixelConverter converter= new PixelConverter(button); 42 int widthHint= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 43 return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); 44 } 45 46 53 public static void setButtonDimensionHint(Button button) { 54 Object gd= button.getLayoutData(); 55 if (gd instanceof GridData) { 56 ((GridData)gd).widthHint= getButtonWidthHint(button); 57 ((GridData)gd).horizontalAlignment = GridData.FILL; 58 } 59 } 60 61 62 72 public static Button createPushButton(Composite parent, String label, Image image) { 73 Button button = new Button(parent, SWT.PUSH); 74 button.setFont(parent.getFont()); 75 if (image != null) { 76 button.setImage(image); 77 } 78 if (label != null) { 79 button.setText(label); 80 } 81 GridData gd = new GridData(); 82 button.setLayoutData(gd); 83 SWTFactory.setButtonDimensionHint(button); 84 return button; 85 } 86 87 98 public static Button createPushButton(Composite parent, String label, String tooltip, Image image) { 99 Button button = createPushButton(parent, label, image); 100 button.setToolTipText(tooltip); 101 return button; 102 } 103 104 113 public static Button createRadioButton(Composite parent, String label) { 114 Button button = new Button(parent, SWT.RADIO); 115 button.setFont(parent.getFont()); 116 if (label != null) { 117 button.setText(label); 118 } 119 GridData gd = new GridData(); 120 button.setLayoutData(gd); 121 SWTFactory.setButtonDimensionHint(button); 122 return button; 123 } 124 125 135 public static Button createRadioButton(Composite parent, String label, int hspan) { 136 Button button = new Button(parent, SWT.RADIO); 137 button.setFont(parent.getFont()); 138 if (label != null) { 139 button.setText(label); 140 } 141 GridData gd = new GridData(GridData.BEGINNING); 142 gd.horizontalSpan = hspan; 143 button.setLayoutData(gd); 144 SWTFactory.setButtonDimensionHint(button); 145 return button; 146 } 147 148 157 public static Button createCheckButton(Composite parent, String label, Image image, boolean checked, int hspan) { 158 Button button = new Button(parent, SWT.CHECK); 159 button.setFont(parent.getFont()); 160 button.setSelection(checked); 161 if(image != null) { 162 button.setImage(image); 163 } 164 if(label != null) { 165 button.setText(label); 166 } 167 GridData gd = new GridData(); 168 gd.horizontalSpan = hspan; 169 button.setLayoutData(gd); 170 setButtonDimensionHint(button); 171 return button; 172 } 173 174 181 public static Label createLabel(Composite parent, String text, Font font, int hspan) { 182 Label l = new Label(parent, SWT.NONE); 183 l.setFont(font); 184 l.setText(text); 185 GridData gd = new GridData(); 186 gd.horizontalSpan = hspan; 187 l.setLayoutData(gd); 188 return l; 189 } 190 191 198 public static Label createLabel(Composite parent, String text, int hspan) { 199 Label l = new Label(parent, SWT.NONE); 200 l.setFont(parent.getFont()); 201 l.setText(text); 202 GridData gd = new GridData(); 203 gd.horizontalSpan = hspan; 204 l.setLayoutData(gd); 205 return l; 206 } 207 208 216 public static Label createWrapLabel(Composite parent, String text, int hspan, int wrapwidth) { 217 Label l = new Label(parent, SWT.WRAP); 218 l.setFont(parent.getFont()); 219 l.setText(text); 220 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 221 gd.horizontalSpan = hspan; 222 gd.widthHint = wrapwidth; 223 l.setLayoutData(gd); 224 return l; 225 } 226 227 233 public static Text createSingleText(Composite parent, int hspan) { 234 Text t = new Text(parent, SWT.SINGLE | SWT.BORDER); 235 t.setFont(parent.getFont()); 236 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 237 gd.horizontalSpan = hspan; 238 t.setLayoutData(gd); 239 return t; 240 } 241 242 250 public static Text createText(Composite parent, int style, int hspan, String text) { 251 Text t = new Text(parent, style); 252 t.setFont(parent.getFont()); 253 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 254 gd.horizontalSpan = hspan; 255 t.setLayoutData(gd); 256 t.setText(text); 257 return t; 258 } 259 260 270 public static Group createGroup(Composite parent, String text, int columns, int hspan, int fill) { 271 Group g = new Group(parent, SWT.NONE); 272 g.setLayout(new GridLayout(columns, false)); 273 g.setText(text); 274 g.setFont(parent.getFont()); 275 GridData gd = new GridData(fill); 276 gd.horizontalSpan = hspan; 277 g.setLayoutData(gd); 278 return g; 279 } 280 281 285 public static void showPreferencePage(String id) { 286 PreferencesUtil.createPreferenceDialogOn(JDIDebugUIPlugin.getShell(), id, new String [] { id }, null).open(); 287 } 288 289 301 public static void showPreferencePage(String page_id, String [] page_filters) { 302 PreferencesUtil.createPreferenceDialogOn(JDIDebugUIPlugin.getShell(), page_id, page_filters, null).open(); 303 } 304 305 314 public static Composite createComposite(Composite parent, Font font, int columns, int hspan, int fill) { 315 Composite g = new Composite(parent, SWT.NONE); 316 g.setLayout(new GridLayout(columns, false)); 317 g.setFont(font); 318 GridData gd = new GridData(fill); 319 gd.horizontalSpan = hspan; 320 g.setLayoutData(gd); 321 return g; 322 } 323 324 335 public static Composite createComposite(Composite parent, Font font, int columns, int hspan, int fill, int marginwidth, int marginheight) { 336 Composite g = new Composite(parent, SWT.NONE); 337 GridLayout layout = new GridLayout(columns, false); 338 layout.marginWidth = marginwidth; 339 layout.marginHeight = marginheight; 340 g.setLayout(layout); 341 g.setFont(font); 342 GridData gd = new GridData(fill); 343 gd.horizontalSpan = hspan; 344 g.setLayoutData(gd); 345 return g; 346 } 347 348 353 public static void createVerticalSpacer(Composite comp, int numlines) { 354 Label lbl = new Label(comp, SWT.NONE); 355 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 356 gd.heightHint = numlines; 357 lbl.setLayoutData(gd); 358 } 359 360 365 public static void createHorizontalSpacer(Composite comp, int numlines) { 366 Label lbl = new Label(comp, SWT.NONE); 367 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 368 gd.horizontalSpan = numlines; 369 lbl.setLayoutData(gd); 370 } 371 372 382 public static Combo createCombo(Composite parent, int style, int hspan, int fill, String [] items) { 383 Combo c = new Combo(parent, style); 384 c.setFont(parent.getFont()); 385 GridData gd = new GridData(fill); 386 gd.horizontalSpan = hspan; 387 c.setLayoutData(gd); 388 c.setItems(items); 389 c.select(0); 390 return c; 391 } 392 393 401 public static Combo createCombo(Composite parent, int style, int hspan, String [] items) { 402 Combo c = new Combo(parent, style); 403 c.setFont(parent.getFont()); 404 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 405 gd.horizontalSpan = hspan; 406 c.setLayoutData(gd); 407 c.setItems(items); 408 c.select(0); 409 return c; 410 } 411 412 422 public static ExpandableComposite createExpandibleComposite(Composite parent, int style, String label, int hspan, int fill) { 423 ExpandableComposite ex = new ExpandableComposite(parent, SWT.NONE, style); 424 ex.setText(label); 425 ex.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DIALOG_FONT)); 426 GridData gd = new GridData(fill); 427 gd.horizontalSpan = hspan; 428 ex.setLayoutData(gd); 429 return ex; 430 } 431 432 } 433 | Popular Tags |