KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.debug.ui;
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.SWTFactory;
18 import org.eclipse.debug.internal.ui.preferences.StringVariablePreferencePage;
19 import org.eclipse.debug.internal.ui.stringsubstitution.IArgumentSelector;
20 import org.eclipse.debug.internal.ui.stringsubstitution.StringSubstitutionMessages;
21 import org.eclipse.debug.internal.ui.stringsubstitution.StringVariableLabelProvider;
22 import org.eclipse.debug.internal.ui.stringsubstitution.StringVariablePresentationManager;
23 import org.eclipse.jface.dialogs.IDialogConstants;
24 import org.eclipse.jface.dialogs.IDialogSettings;
25 import org.eclipse.jface.preference.IPreferenceNode;
26 import org.eclipse.jface.preference.PreferenceDialog;
27 import org.eclipse.jface.preference.PreferenceManager;
28 import org.eclipse.jface.preference.PreferenceNode;
29 import org.eclipse.jface.preference.PreferencePage;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.custom.BusyIndicator;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.swt.widgets.Label;
41 import org.eclipse.swt.widgets.Shell;
42 import org.eclipse.swt.widgets.Text;
43 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
44
45 /**
46  * A dialog that prompts the user to choose and configure a string
47  * substitution variable.
48  * <p>
49  * Clients may instantiate this class; not intended to be subclassed.
50  * </p>
51  * @since 3.1
52  */

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

69     public StringVariableSelectionDialog(Shell parent) {
70         super(parent, new StringVariableLabelProvider());
71         setShellStyle(getShellStyle() | SWT.RESIZE);
72         setTitle(StringSubstitutionMessages.StringVariableSelectionDialog_2);
73         setMessage(StringSubstitutionMessages.StringVariableSelectionDialog_3);
74         setMultipleSelection(false);
75         setElements(VariablesPlugin.getDefault().getStringVariableManager().getVariables());
76     }
77     
78     /**
79      * Returns the variable expression the user generated from this
80      * dialog, or <code>null</code> if none.
81      *
82      * @return variable expression the user generated from this
83      * dialog, or <code>null</code> if none
84      */

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

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

117     private void createArgumentArea(Composite parent) {
118         Composite container = new Composite(parent, SWT.NONE);
119         GridLayout layout = new GridLayout();
120         layout.numColumns = 2;
121         layout.makeColumnsEqualWidth = false;
122         layout.marginHeight = 0;
123         layout.marginWidth = 0;
124         container.setLayout(layout);
125         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
126         container.setLayoutData(gd);
127         container.setFont(parent.getFont());
128         
129         fEditVariablesButton = new Button(container, SWT.PUSH);
130         fEditVariablesButton.setFont(container.getFont());
131         fEditVariablesButton.setText(StringSubstitutionMessages.StringVariableSelectionDialog_0);
132         gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
133         gd.horizontalSpan = 2;
134         fEditVariablesButton.setLayoutData(gd);
135         fEditVariablesButton.addSelectionListener(new SelectionAdapter() {
136             public void widgetSelected(SelectionEvent e) {
137                 editVariables();
138             }
139         });
140         
141         Label desc = new Label(container, SWT.NONE);
142         desc.setFont(parent.getFont());
143         desc.setText(StringSubstitutionMessages.StringVariableSelectionDialog_6);
144         gd = new GridData(GridData.FILL_HORIZONTAL);
145         gd.horizontalSpan = 2;
146         desc.setLayoutData(gd);
147         
148         Composite args = new Composite(container, SWT.NONE);
149         layout = new GridLayout(2, false);
150         layout.marginHeight = 0;
151         layout.marginWidth = 0;
152         args.setLayout(layout);
153         gd = new GridData(GridData.FILL_HORIZONTAL);
154         gd.horizontalSpan = 2;
155         args.setLayoutData(gd);
156         args.setFont(container.getFont());
157         
158         fArgumentText = new Text(args, SWT.BORDER);
159         fArgumentText.setFont(container.getFont());
160         gd = new GridData(GridData.FILL_HORIZONTAL);
161         fArgumentText.setLayoutData(gd);
162         
163         fArgumentButton = new Button(args, SWT.PUSH);
164         fArgumentButton.setFont(parent.getFont());
165         fArgumentButton.setText(StringSubstitutionMessages.StringVariableSelectionDialog_7);
166         gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
167         gd.widthHint = SWTFactory.getButtonWidthHint(fArgumentButton);
168         fArgumentButton.setLayoutData(gd);
169         fArgumentButton.addSelectionListener(new SelectionAdapter() {
170             public void widgetSelected(SelectionEvent e) {
171                 configureArgument();
172             }
173         });
174
175         
176         desc = new Label(container, SWT.NONE);
177         desc.setFont(parent.getFont());
178         desc.setText(StringSubstitutionMessages.StringVariableSelectionDialog_8);
179         gd = new GridData(GridData.FILL_HORIZONTAL);
180         gd.horizontalSpan = 2;
181         desc.setLayoutData(gd);
182         
183         fDescriptionText = new Text(container, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
184         fDescriptionText.setFont(container.getFont());
185         fDescriptionText.setEditable(false);
186         gd = new GridData(GridData.FILL_HORIZONTAL);
187         gd.horizontalSpan = 2;
188         gd.heightHint = 50;
189         fDescriptionText.setLayoutData(gd);
190     }
191
192     protected void editVariables() {
193         PreferencePage page = new StringVariablePreferencePage();
194         page.setTitle(StringSubstitutionMessages.StringVariableSelectionDialog_1);
195         final IPreferenceNode targetNode = new PreferenceNode("org.eclipse.debug.ui.StringVariablePreferencePage", page); //$NON-NLS-1$
196

197         PreferenceManager manager = new PreferenceManager();
198         manager.addToRoot(targetNode);
199         final PreferenceDialog dialog = new PreferenceDialog(getShell(), manager);
200         
201         final Display display = DebugUIPlugin.getStandardDisplay();
202         BusyIndicator.showWhile(display, new Runnable JavaDoc() {
203             public void run() {
204                 dialog.create();
205                 dialog.setMessage(targetNode.getLabelText());
206                 if(dialog.open() == IDialogConstants.OK_ID) {
207                     final IStringVariable[] elements = VariablesPlugin.getDefault().getStringVariableManager().getVariables();
208                     display.asyncExec(new Runnable JavaDoc() {
209                         public void run() {
210                             setListElements(elements);
211                         }
212                     });
213                 }
214             }
215         });
216     }
217
218     /**
219      * Configures the argument for the selected variable.
220      */

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

236     protected void handleSelectionChanged() {
237         super.handleSelectionChanged();
238         Object JavaDoc[] objects = getSelectedElements();
239         boolean buttonEnabled = false;
240         boolean argEnabled = false;
241         String JavaDoc text = null;
242         if (objects.length == 1) {
243             IStringVariable variable = (IStringVariable)objects[0];
244              IArgumentSelector selector = StringVariablePresentationManager.getDefault().getArgumentSelector(variable);
245              if (variable instanceof IDynamicVariable) {
246                 argEnabled = ((IDynamicVariable)variable).supportsArgument();
247              }
248              buttonEnabled = argEnabled && selector != null;
249              text = variable.getDescription();
250         }
251         if (text == null) {
252             text = ""; //$NON-NLS-1$
253
}
254         fArgumentText.setEnabled(argEnabled);
255         fArgumentButton.setEnabled(buttonEnabled);
256         fDescriptionText.setText(text);
257     }
258
259     /* (non-Javadoc)
260      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
261      */

262     protected void okPressed() {
263         fArgumentValue = fArgumentText.getText().trim();
264         super.okPressed();
265     }
266
267     /**
268      * Returns the name of the section that this dialog stores its settings in
269      *
270      * @return String
271      */

272     private String JavaDoc getDialogSettingsSectionName() {
273         return IDebugUIConstants.PLUGIN_ID + ".STRING_VARIABLE_SELECTION_DIALOG_SECTION"; //$NON-NLS-1$
274
}
275     
276      /* (non-Javadoc)
277      * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
278      */

279     protected IDialogSettings getDialogBoundsSettings() {
280          IDialogSettings settings = DebugUIPlugin.getDefault().getDialogSettings();
281          IDialogSettings section = settings.getSection(getDialogSettingsSectionName());
282          if (section == null) {
283              section = settings.addNewSection(getDialogSettingsSectionName());
284          }
285          return section;
286     }
287 }
288
Popular Tags