KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > VariablePathDialogField


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.internal.ui.wizards.buildpaths;
12 import java.util.List JavaDoc;
13
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.core.runtime.Path;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.SelectionListener;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.swt.widgets.Text;
27
28 import org.eclipse.jface.dialogs.StatusDialog;
29 import org.eclipse.jface.viewers.DoubleClickEvent;
30 import org.eclipse.jface.viewers.IDoubleClickListener;
31 import org.eclipse.jface.viewers.ISelectionChangedListener;
32 import org.eclipse.jface.viewers.SelectionChangedEvent;
33 import org.eclipse.jface.window.Window;
34
35 import org.eclipse.ui.PlatformUI;
36
37 import org.eclipse.jdt.core.JavaCore;
38
39 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
42 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
43 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
44 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField;
45
46 public class VariablePathDialogField extends StringButtonDialogField {
47
48     public static class ChooseVariableDialog extends StatusDialog implements ISelectionChangedListener, IDoubleClickListener {
49         private VariableBlock fVariableBlock;
50
51         public ChooseVariableDialog(Shell parent, String JavaDoc variableSelection) {
52             super(parent);
53             int shellStyle= getShellStyle();
54             setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE);
55             
56             setTitle(NewWizardMessages.VariablePathDialogField_variabledialog_title);
57             fVariableBlock= new VariableBlock(false, variableSelection);
58         }
59
60         protected Control createDialogArea(Composite parent) {
61             Composite composite= (Composite) super.createDialogArea(parent);
62             Control control= fVariableBlock.createContents(composite);
63
64             GridData data= new GridData(GridData.FILL_BOTH);
65             data.widthHint= convertWidthInCharsToPixels(80);
66             data.heightHint= convertHeightInCharsToPixels(15);
67             control.setLayoutData(data);
68             
69             fVariableBlock.addDoubleClickListener(this);
70             fVariableBlock.addSelectionChangedListener(this);
71             applyDialogFont(composite);
72             return composite;
73         }
74
75         protected void okPressed() {
76             fVariableBlock.performOk();
77             super.okPressed();
78         }
79
80         public String JavaDoc getSelectedVariable() {
81             List JavaDoc elements= fVariableBlock.getSelectedElements();
82             return ((CPVariableElement) elements.get(0)).getName();
83         }
84
85         /*
86          * @see IDoubleClickListener#doubleClick(DoubleClickEvent)
87          */

88         public void doubleClick(DoubleClickEvent event) {
89             if (getStatus().isOK()) {
90                 okPressed();
91             }
92         }
93         
94         /* (non-Javadoc)
95          * @see ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
96          */

97         public void selectionChanged(SelectionChangedEvent event) {
98             List JavaDoc elements= fVariableBlock.getSelectedElements();
99             StatusInfo status= new StatusInfo();
100             if (elements.size() != 1) {
101                 status.setError(""); //$NON-NLS-1$
102
}
103             updateStatus(status);
104         }
105         /*
106          * @see org.eclipse.jface.window.Window#configureShell(Shell)
107          */

108         protected void configureShell(Shell newShell) {
109             super.configureShell(newShell);
110             PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.CHOOSE_VARIABLE_DIALOG);
111         }
112     }
113
114     private Button fBrowseVariableButton;
115     private String JavaDoc fVariableButtonLabel;
116
117     public VariablePathDialogField(IStringButtonAdapter adapter) {
118         super(adapter);
119     }
120
121     public void setVariableButtonLabel(String JavaDoc label) {
122         fVariableButtonLabel= label;
123     }
124
125     // ------- layout helpers
126

127     public Control[] doFillIntoGrid(Composite parent, int nColumns) {
128         assertEnoughColumns(nColumns);
129
130         Label label= getLabelControl(parent);
131         label.setLayoutData(gridDataForLabel(1));
132         Text text= getTextControl(parent);
133         text.setLayoutData(gridDataForText(nColumns - 3));
134         Button variableButton= getBrowseVariableControl(parent);
135         variableButton.setLayoutData(gridDataForButton(variableButton, 1));
136         Button browseButton= getChangeControl(parent);
137         browseButton.setLayoutData(gridDataForButton(browseButton, 1));
138         return new Control[] { label, text, variableButton, browseButton };
139     }
140
141     public int getNumberOfControls() {
142         return 4;
143     }
144     public Button getBrowseVariableControl(Composite parent) {
145         if (fBrowseVariableButton == null) {
146             assertCompositeNotNull(parent);
147
148             fBrowseVariableButton= new Button(parent, SWT.PUSH);
149             fBrowseVariableButton.setText(fVariableButtonLabel);
150             fBrowseVariableButton.setEnabled(isEnabled());
151             fBrowseVariableButton.addSelectionListener(new SelectionListener() {
152                 public void widgetDefaultSelected(SelectionEvent e) {
153                     chooseVariablePressed();
154                 }
155                 public void widgetSelected(SelectionEvent e) {
156                     chooseVariablePressed();
157                 }
158             });
159
160         }
161         return fBrowseVariableButton;
162     }
163
164     public IPath getPath() {
165         return new Path(getText());
166     }
167
168     public String JavaDoc getVariable() {
169         IPath path= getPath();
170         if (!path.isEmpty()) {
171             return path.segment(0);
172         }
173         return null;
174     }
175
176     public IPath getPathExtension() {
177         return new Path(getText()).removeFirstSegments(1).setDevice(null);
178     }
179
180     public IPath getResolvedPath() {
181         String JavaDoc variable= getVariable();
182         if (variable != null) {
183             IPath path= JavaCore.getClasspathVariable(variable);
184             if (path != null) {
185                 return path.append(getPathExtension());
186             }
187         }
188         return null;
189     }
190
191     private Shell getShell() {
192         if (fBrowseVariableButton != null) {
193             return fBrowseVariableButton.getShell();
194         }
195         return JavaPlugin.getActiveWorkbenchShell();
196     }
197
198     private void chooseVariablePressed() {
199         String JavaDoc variable= getVariable();
200         ChooseVariableDialog dialog= new ChooseVariableDialog(getShell(), variable);
201         if (dialog.open() == Window.OK) {
202             IPath newPath= new Path(dialog.getSelectedVariable()).append(getPathExtension());
203             setText(newPath.toString());
204         }
205     }
206
207     protected void updateEnableState() {
208         super.updateEnableState();
209         if (isOkToUse(fBrowseVariableButton)) {
210             fBrowseVariableButton.setEnabled(isEnabled());
211         }
212     }
213
214 }
215
Popular Tags