KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > stringsubstitution > StringVariableSelectionDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
45  * A dialog that prompts the user to choose and configure a string
46  * substitution variable.
47  *
48  * @since 3,0
49  */

50 public class StringVariableSelectionDialog extends ElementListSelectionDialog {
51     
52     // button to configure variable's argument
53
private Button fArgumentButton;
54     // variable description
55
private Text fDescriptionText;
56     // the argument value
57
private Text fArgumentText;
58     private String JavaDoc fArgumentValue;
59     private Button fEditVariablesButton;
60
61     /**
62      * Constructs a new string substitution variable selection dialog.
63      *
64      * @param parent parent shell
65      */

66     public StringVariableSelectionDialog(Shell parent) {
67         super(parent, new StringVariableLabelProvider());
68         setShellStyle(getShellStyle() | SWT.RESIZE);
69         setTitle(StringSubstitutionMessages.getString("StringVariableSelectionDialog.2")); //$NON-NLS-1$
70
setMessage(StringSubstitutionMessages.getString("StringVariableSelectionDialog.3")); //$NON-NLS-1$
71
setMultipleSelection(false);
72         setElements(VariablesPlugin.getDefault().getStringVariableManager().getVariables());
73     }
74     
75     /**
76      * Returns the variable expression the user generated from this
77      * dialog, or <code>null</code> if none.
78      *
79      * @return variable expression the user generated from this
80      * dialog, or <code>null</code> if none
81      */

82     public String JavaDoc getVariableExpression() {
83         Object JavaDoc[] selected = getResult();
84         if (selected != null && selected.length == 1) {
85             IStringVariable variable = (IStringVariable)selected[0];
86             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
87             buffer.append("${"); //$NON-NLS-1$
88
buffer.append(variable.getName());
89             if (fArgumentValue != null && fArgumentValue.length() > 0) {
90                 buffer.append(":"); //$NON-NLS-1$
91
buffer.append(fArgumentValue);
92             }
93             buffer.append("}"); //$NON-NLS-1$
94
return buffer.toString();
95         }
96         return null;
97     }
98
99     /* (non-Javadoc)
100      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
101      */

102     protected Control createDialogArea(Composite parent) {
103         Control control = super.createDialogArea(parent);
104         createArgumentArea((Composite)control);
105         return control;
106     }
107
108     /**
109      * Creates an area to display a description of the selected variable
110      * and a button to configure the variable's argument.
111      *
112      * @param parent parnet widget
113      */

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")); //$NON-NLS-1$
129
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")); //$NON-NLS-1$
141
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")); //$NON-NLS-1$
163
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")); //$NON-NLS-1$
176
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")); //$NON-NLS-1$
194
final IPreferenceNode targetNode = new PreferenceNode("org.eclipse.debug.ui.StringVariablePreferencePage", page); //$NON-NLS-1$
195

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 JavaDoc() {
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 JavaDoc() {
208                         public void run() {
209                             setListElements(elements);
210                         }
211                     });
212                 }
213             }
214         });
215     }
216
217     /**
218      * Configures the argument for the selected variable.
219      */

220     protected void configureArgument() {
221         Object JavaDoc[] objects = getSelectedElements();
222         IStringVariable variable = (IStringVariable)objects[0];
223         IArgumentSelector selector = StringVariablePresentationManager.getDefault().getArgumentSelector(variable);
224         String JavaDoc value = selector.selectArgument(variable, getShell());
225         if (value != null) {
226             fArgumentText.setText(value);
227         }
228     }
229
230     /**
231      * Update variable description and argument button enablement.
232      *
233      * @see org.eclipse.ui.dialogs.AbstractElementListSelectionDialog#handleSelectionChanged()
234      */

235     protected void handleSelectionChanged() {
236         super.handleSelectionChanged();
237         Object JavaDoc[] objects = getSelectedElements();
238         boolean buttonEnabled = false;
239         boolean argEnabled = false;
240         String JavaDoc 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 = ""; //$NON-NLS-1$
252
}
253         fArgumentText.setEnabled(argEnabled);
254         fArgumentButton.setEnabled(buttonEnabled);
255         fDescriptionText.setText(text);
256     }
257
258     /* (non-Javadoc)
259      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
260      */

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     /**
276      * Returns the name of the section that this dialog stores its settings in
277      *
278      * @return String
279      */

280     protected String JavaDoc getDialogSettingsSectionName() {
281         return IDebugUIConstants.PLUGIN_ID + ".STRING_VARIABLE_SELECTION_DIALOG_SECTION"; //$NON-NLS-1$
282
}
283
284     /* (non-Javadoc)
285      * @see org.eclipse.jface.window.Window#getInitialLocation(org.eclipse.swt.graphics.Point)
286      */

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     /* (non-Javadoc)
296      * @see org.eclipse.jface.window.Window#getInitialSize()
297      */

298     protected Point getInitialSize() {
299         Point size = super.getInitialSize();
300         return DialogSettingsHelper.getInitialSize(getDialogSettingsSectionName(), size);
301     }
302     
303     
304     /* (non-Javadoc)
305      * @see org.eclipse.jface.window.Window#close()
306      */

307     public boolean close() {
308         DialogSettingsHelper.persistShellGeometry(getShell(), getDialogSettingsSectionName());
309         return super.close();
310     }
311 }
312
Popular Tags