1 12 package org.eclipse.jface.preference; 13 14 import org.eclipse.jface.dialogs.IDialogConstants; 15 import org.eclipse.jface.resource.JFaceResources; 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.events.DisposeEvent; 19 import org.eclipse.swt.events.DisposeListener; 20 import org.eclipse.swt.events.SelectionAdapter; 21 import org.eclipse.swt.events.SelectionEvent; 22 import org.eclipse.swt.graphics.Point; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.widgets.Button; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Shell; 27 28 35 public abstract class StringButtonFieldEditor extends StringFieldEditor { 36 37 41 private Button changeButton; 42 43 46 private String changeButtonText; 47 48 51 protected StringButtonFieldEditor() { 52 } 53 54 61 protected StringButtonFieldEditor(String name, String labelText, 62 Composite parent) { 63 init(name, labelText); 64 createControl(parent); 65 } 66 67 70 protected void adjustForNumColumns(int numColumns) { 71 ((GridData) getTextControl().getLayoutData()).horizontalSpan = numColumns - 2; 72 } 73 74 85 protected abstract String changePressed(); 86 87 90 protected void doFillIntoGrid(Composite parent, int numColumns) { 91 super.doFillIntoGrid(parent, numColumns - 1); 92 changeButton = getChangeControl(parent); 93 GridData gd = new GridData(); 94 gd.horizontalAlignment = GridData.FILL; 95 int widthHint = convertHorizontalDLUsToPixels(changeButton, 96 IDialogConstants.BUTTON_WIDTH); 97 gd.widthHint = Math.max(widthHint, changeButton.computeSize( 98 SWT.DEFAULT, SWT.DEFAULT, true).x); 99 changeButton.setLayoutData(gd); 100 } 101 102 107 protected Button getChangeControl(Composite parent) { 108 if (changeButton == null) { 109 changeButton = new Button(parent, SWT.PUSH); 110 if (changeButtonText == null) { 111 changeButtonText = JFaceResources.getString("openChange"); } 113 changeButton.setText(changeButtonText); 114 changeButton.setFont(parent.getFont()); 115 changeButton.addSelectionListener(new SelectionAdapter() { 116 public void widgetSelected(SelectionEvent evt) { 117 String newValue = changePressed(); 118 if (newValue != null) { 119 setStringValue(newValue); 120 } 121 } 122 }); 123 changeButton.addDisposeListener(new DisposeListener() { 124 public void widgetDisposed(DisposeEvent event) { 125 changeButton = null; 126 } 127 }); 128 } else { 129 checkParent(changeButton, parent); 130 } 131 return changeButton; 132 } 133 134 137 public int getNumberOfControls() { 138 return 3; 139 } 140 141 146 protected Shell getShell() { 147 if (changeButton == null) { 148 return null; 149 } 150 return changeButton.getShell(); 151 } 152 153 158 public void setChangeButtonText(String text) { 159 Assert.isNotNull(text); 160 changeButtonText = text; 161 if (changeButton != null) { 162 changeButton.setText(text); 163 Point prefSize = changeButton.computeSize(SWT.DEFAULT, SWT.DEFAULT); 164 GridData data = (GridData)changeButton.getLayoutData(); 165 data.widthHint = Math.max(SWT.DEFAULT, prefSize.x); 166 } 167 } 168 169 172 public void setEnabled(boolean enabled, Composite parent) { 173 super.setEnabled(enabled, parent); 174 if (changeButton != null) { 175 changeButton.setEnabled(enabled); 176 } 177 } 178 179 } 180 | Popular Tags |