1 11 package org.eclipse.debug.internal.ui; 12 13 14 import org.eclipse.jface.dialogs.IDialogConstants; 15 import org.eclipse.jface.preference.IPreferenceNode; 16 import org.eclipse.jface.preference.IPreferencePage; 17 import org.eclipse.jface.preference.PreferenceDialog; 18 import org.eclipse.jface.preference.PreferenceManager; 19 import org.eclipse.jface.preference.PreferenceNode; 20 import org.eclipse.jface.resource.JFaceResources; 21 import org.eclipse.jface.util.Assert; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.custom.BusyIndicator; 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.Composite; 29 import org.eclipse.swt.widgets.Group; 30 import org.eclipse.swt.widgets.Label; 31 import org.eclipse.swt.widgets.Text; 32 33 36 public class SWTUtil { 37 38 41 public static int getButtonWidthHint(Button button) { 42 button.setFont(JFaceResources.getDialogFont()); 43 PixelConverter converter= new PixelConverter(button); 44 int widthHint= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 45 return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); 46 } 47 48 55 public static void setButtonDimensionHint(Button button) { 56 Assert.isNotNull(button); 57 Object gd= button.getLayoutData(); 58 if (gd instanceof GridData) { 59 ((GridData)gd).widthHint= getButtonWidthHint(button); 60 ((GridData)gd).horizontalAlignment = GridData.FILL; 61 } 62 } 63 64 65 75 public static Button createPushButton(Composite parent, String label, Image image) { 76 Button button = new Button(parent, SWT.PUSH); 77 button.setFont(parent.getFont()); 78 if (image != null) { 79 button.setImage(image); 80 } 81 if (label != null) { 82 button.setText(label); 83 } 84 GridData gd = new GridData(); 85 button.setLayoutData(gd); 86 SWTUtil.setButtonDimensionHint(button); 87 return button; 88 } 89 90 99 public static Button createRadioButton(Composite parent, String label) { 100 Button button = new Button(parent, SWT.RADIO); 101 button.setFont(parent.getFont()); 102 if (label != null) { 103 button.setText(label); 104 } 105 GridData gd = new GridData(); 106 button.setLayoutData(gd); 107 SWTUtil.setButtonDimensionHint(button); 108 return button; 109 } 110 111 120 public static Label createLabel(Composite parent, String text, int hspan) { 121 Label l = new Label(parent, SWT.NONE); 122 l.setFont(parent.getFont()); 123 l.setText(text); 124 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 125 gd.horizontalSpan = hspan; 126 l.setLayoutData(gd); 127 return l; 128 } 129 130 138 public static Text createSingleText(Composite parent, int hspan) { 139 Text t = new Text(parent, SWT.SINGLE | SWT.BORDER); 140 t.setFont(parent.getFont()); 141 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 142 gd.horizontalSpan = hspan; 143 t.setLayoutData(gd); 144 return t; 145 } 146 147 158 public static Group createGroup(Composite parent, String text, int columns, int hspan, int fill) { 159 Group g = new Group(parent, SWT.NONE); 160 g.setLayout(new GridLayout(columns, false)); 161 g.setText(text); 162 g.setFont(parent.getFont()); 163 GridData gd = new GridData(fill); 164 gd.horizontalSpan = hspan; 165 g.setLayoutData(gd); 166 return g; 167 } 168 169 175 public static void showPreferencePage(String id, IPreferencePage page) { 176 final IPreferenceNode targetNode = new PreferenceNode(id, page); 177 PreferenceManager manager = new PreferenceManager(); 178 manager.addToRoot(targetNode); 179 final PreferenceDialog dialog = new PreferenceDialog(DebugUIPlugin.getShell(), manager); 180 BusyIndicator.showWhile(DebugUIPlugin.getStandardDisplay(), new Runnable () { 181 public void run() { 182 dialog.create(); 183 dialog.setMessage(targetNode.getLabelText()); 184 dialog.open(); 185 } 186 }); 187 } 188 } 189 | Popular Tags |