1 11 package org.eclipse.debug.internal.ui.stringsubstitution; 12 13 import org.eclipse.core.variables.IDynamicVariable; 14 import org.eclipse.core.variables.IStringVariable; 15 import org.eclipse.core.variables.VariablesPlugin; 16 import org.eclipse.debug.internal.ui.DebugUIPlugin; 17 import org.eclipse.debug.internal.ui.DialogSettingsHelper; 18 import org.eclipse.debug.internal.ui.SWTUtil; 19 import org.eclipse.debug.internal.ui.preferences.StringVariablePreferencePage; 20 import org.eclipse.debug.ui.IDebugUIConstants; 21 import org.eclipse.jface.dialogs.IDialogConstants; 22 import org.eclipse.jface.dialogs.IDialogSettings; 23 import org.eclipse.jface.preference.IPreferenceNode; 24 import org.eclipse.jface.preference.PreferenceDialog; 25 import org.eclipse.jface.preference.PreferenceManager; 26 import org.eclipse.jface.preference.PreferenceNode; 27 import org.eclipse.jface.preference.PreferencePage; 28 import org.eclipse.swt.SWT; 29 import org.eclipse.swt.custom.BusyIndicator; 30 import org.eclipse.swt.events.SelectionAdapter; 31 import org.eclipse.swt.events.SelectionEvent; 32 import org.eclipse.swt.graphics.Point; 33 import org.eclipse.swt.layout.GridData; 34 import org.eclipse.swt.layout.GridLayout; 35 import org.eclipse.swt.widgets.Button; 36 import org.eclipse.swt.widgets.Composite; 37 import org.eclipse.swt.widgets.Control; 38 import org.eclipse.swt.widgets.Display; 39 import org.eclipse.swt.widgets.Label; 40 import org.eclipse.swt.widgets.Shell; 41 import org.eclipse.swt.widgets.Text; 42 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 43 44 50 public class StringVariableSelectionDialog extends ElementListSelectionDialog { 51 52 private Button fArgumentButton; 54 private Text fDescriptionText; 56 private Text fArgumentText; 58 private String fArgumentValue; 59 private Button fEditVariablesButton; 60 61 66 public StringVariableSelectionDialog(Shell parent) { 67 super(parent, new StringVariableLabelProvider()); 68 setShellStyle(getShellStyle() | SWT.RESIZE); 69 setTitle(StringSubstitutionMessages.getString("StringVariableSelectionDialog.2")); setMessage(StringSubstitutionMessages.getString("StringVariableSelectionDialog.3")); setMultipleSelection(false); 72 setElements(VariablesPlugin.getDefault().getStringVariableManager().getVariables()); 73 } 74 75 82 public String getVariableExpression() { 83 Object [] selected = getResult(); 84 if (selected != null && selected.length == 1) { 85 IStringVariable variable = (IStringVariable)selected[0]; 86 StringBuffer buffer = new StringBuffer (); 87 buffer.append("${"); buffer.append(variable.getName()); 89 if (fArgumentValue != null && fArgumentValue.length() > 0) { 90 buffer.append(":"); buffer.append(fArgumentValue); 92 } 93 buffer.append("}"); return buffer.toString(); 95 } 96 return null; 97 } 98 99 102 protected Control createDialogArea(Composite parent) { 103 Control control = super.createDialogArea(parent); 104 createArgumentArea((Composite)control); 105 return control; 106 } 107 108 114 private void createArgumentArea(Composite parent) { 115 Composite container = new Composite(parent, SWT.NONE); 116 GridLayout layout = new GridLayout(); 117 layout.numColumns = 2; 118 layout.makeColumnsEqualWidth = false; 119 layout.marginHeight = 0; 120 layout.marginWidth = 0; 121 container.setLayout(layout); 122 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 123 container.setLayoutData(gd); 124 container.setFont(parent.getFont()); 125 126 fEditVariablesButton = new Button(container, SWT.PUSH); 127 fEditVariablesButton.setFont(container.getFont()); 128 fEditVariablesButton.setText(StringSubstitutionMessages.getString("StringVariableSelectionDialog.0")); gd = new GridData(GridData.HORIZONTAL_ALIGN_END); 130 gd.horizontalSpan = 2; 131 fEditVariablesButton.setLayoutData(gd); 132 fEditVariablesButton.addSelectionListener(new SelectionAdapter() { 133 public void widgetSelected(SelectionEvent e) { 134 editVariables(); 135 } 136 }); 137 138 Label desc = new Label(container, SWT.NONE); 139 desc.setFont(parent.getFont()); 140 desc.setText(StringSubstitutionMessages.getString("StringVariableSelectionDialog.6")); gd = new GridData(GridData.FILL_HORIZONTAL); 142 gd.horizontalSpan = 2; 143 desc.setLayoutData(gd); 144 145 Composite args = new Composite(container, SWT.NONE); 146 layout = new GridLayout(2, false); 147 layout.marginHeight = 0; 148 layout.marginWidth = 0; 149 args.setLayout(layout); 150 gd = new GridData(GridData.FILL_HORIZONTAL); 151 gd.horizontalSpan = 2; 152 args.setLayoutData(gd); 153 args.setFont(container.getFont()); 154 155 fArgumentText = new Text(args, SWT.BORDER); 156 fArgumentText.setFont(container.getFont()); 157 gd = new GridData(GridData.FILL_HORIZONTAL); 158 fArgumentText.setLayoutData(gd); 159 160 fArgumentButton = new Button(args, SWT.PUSH); 161 fArgumentButton.setFont(parent.getFont()); 162 fArgumentButton.setText(StringSubstitutionMessages.getString("StringVariableSelectionDialog.7")); gd = new GridData(GridData.HORIZONTAL_ALIGN_END); 164 gd.widthHint = SWTUtil.getButtonWidthHint(fArgumentButton); 165 fArgumentButton.setLayoutData(gd); 166 fArgumentButton.addSelectionListener(new SelectionAdapter() { 167 public void widgetSelected(SelectionEvent e) { 168 configureArgument(); 169 } 170 }); 171 172 173 desc = new Label(container, SWT.NONE); 174 desc.setFont(parent.getFont()); 175 desc.setText(StringSubstitutionMessages.getString("StringVariableSelectionDialog.8")); gd = new GridData(GridData.FILL_HORIZONTAL); 177 gd.horizontalSpan = 2; 178 desc.setLayoutData(gd); 179 180 fDescriptionText = new Text(container, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL); 181 fDescriptionText.setFont(container.getFont()); 182 fDescriptionText.setEditable(false); 183 gd = new GridData(GridData.FILL_HORIZONTAL); 184 gd.horizontalSpan = 2; 185 gd.heightHint = 50; 186 fDescriptionText.setLayoutData(gd); 187 188 } 189 190 191 protected void editVariables() { 192 PreferencePage page = new StringVariablePreferencePage(); 193 page.setTitle(StringSubstitutionMessages.getString("StringVariableSelectionDialog.1")); final IPreferenceNode targetNode = new PreferenceNode("org.eclipse.debug.ui.StringVariablePreferencePage", page); 196 PreferenceManager manager = new PreferenceManager(); 197 manager.addToRoot(targetNode); 198 final PreferenceDialog dialog = new PreferenceDialog(getShell(), manager); 199 200 final Display display = DebugUIPlugin.getStandardDisplay(); 201 BusyIndicator.showWhile(display, new Runnable () { 202 public void run() { 203 dialog.create(); 204 dialog.setMessage(targetNode.getLabelText()); 205 if(dialog.open() == IDialogConstants.OK_ID) { 206 final IStringVariable[] elements = VariablesPlugin.getDefault().getStringVariableManager().getVariables(); 207 display.asyncExec(new Runnable () { 208 public void run() { 209 setListElements(elements); 210 } 211 }); 212 } 213 } 214 }); 215 } 216 217 220 protected void configureArgument() { 221 Object [] objects = getSelectedElements(); 222 IStringVariable variable = (IStringVariable)objects[0]; 223 IArgumentSelector selector = StringVariablePresentationManager.getDefault().getArgumentSelector(variable); 224 String value = selector.selectArgument(variable, getShell()); 225 if (value != null) { 226 fArgumentText.setText(value); 227 } 228 } 229 230 235 protected void handleSelectionChanged() { 236 super.handleSelectionChanged(); 237 Object [] objects = getSelectedElements(); 238 boolean buttonEnabled = false; 239 boolean argEnabled = false; 240 String text = null; 241 if (objects.length == 1) { 242 IStringVariable variable = (IStringVariable)objects[0]; 243 IArgumentSelector selector = StringVariablePresentationManager.getDefault().getArgumentSelector(variable); 244 if (variable instanceof IDynamicVariable) { 245 argEnabled = ((IDynamicVariable)variable).supportsArgument(); 246 } 247 buttonEnabled = argEnabled && selector != null; 248 text = variable.getDescription(); 249 } 250 if (text == null) { 251 text = ""; } 253 fArgumentText.setEnabled(argEnabled); 254 fArgumentButton.setEnabled(buttonEnabled); 255 fDescriptionText.setText(text); 256 } 257 258 261 protected void okPressed() { 262 fArgumentValue = fArgumentText.getText().trim(); 263 super.okPressed(); 264 } 265 266 protected IDialogSettings getDialogSettings() { 267 IDialogSettings settings = DebugUIPlugin.getDefault().getDialogSettings(); 268 IDialogSettings section = settings.getSection(getDialogSettingsSectionName()); 269 if (section == null) { 270 section = settings.addNewSection(getDialogSettingsSectionName()); 271 } 272 return section; 273 } 274 275 280 protected String getDialogSettingsSectionName() { 281 return IDebugUIConstants.PLUGIN_ID + ".STRING_VARIABLE_SELECTION_DIALOG_SECTION"; } 283 284 287 protected Point getInitialLocation(Point initialSize) { 288 Point initialLocation= DialogSettingsHelper.getInitialLocation(getDialogSettingsSectionName()); 289 if (initialLocation != null) { 290 return initialLocation; 291 } 292 return super.getInitialLocation(initialSize); 293 } 294 295 298 protected Point getInitialSize() { 299 Point size = super.getInitialSize(); 300 return DialogSettingsHelper.getInitialSize(getDialogSettingsSectionName(), size); 301 } 302 303 304 307 public boolean close() { 308 DialogSettingsHelper.persistShellGeometry(getShell(), getDialogSettingsSectionName()); 309 return super.close(); 310 } 311 } 312 | Popular Tags |