1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.wizards.buildpaths;
12 import java.util.List;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.widgets.Button;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.swt.widgets.Text;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.Path;
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;
35 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.jdt.core.JavaCore;
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;
46 public class VariablePathDialogField extends StringButtonDialogField {
48 public static class ChooseVariableDialog extends StatusDialog implements ISelectionChangedListener, IDoubleClickListener {
49 private VariableBlock fVariableBlock;
51 public ChooseVariableDialog(Shell parent, String variableSelection) {
54 setTitle(NewWizardMessages.VariablePathDialogField_variabledialog_title);
55 fVariableBlock= new VariableBlock(false, variableSelection);
59 protected Control createDialogArea(Composite parent) {
60 Composite composite= (Composite) super.createDialogArea(parent);
61 Control control= fVariableBlock.createContents(composite);
63 GridData data= new GridData(GridData.FILL_BOTH);
64 data.widthHint= convertWidthInCharsToPixels(80);
65 data.heightHint= convertHeightInCharsToPixels(15);
66 control.setLayoutData(data);
68 fVariableBlock.addDoubleClickListener(this);
69 fVariableBlock.addSelectionChangedListener(this);
70 applyDialogFont(composite);
75 * @see org.eclipse.jface.dialogs.Dialog#isResizable()
79 protected boolean isResizable() {
84 protected void okPressed() {
85 fVariableBlock.performOk();
89 public String getSelectedVariable() {
90 List<CPVariableElement> elements= fVariableBlock.getSelectedElements();
91 return elements.get(0).getName();
95 * @see IDoubleClickListener#doubleClick(DoubleClickEvent)
97 public void doubleClick(DoubleClickEvent event) {
98 if (getStatus().isOK()) {
104 * @see ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
106 public void selectionChanged(SelectionChangedEvent event) {
107 List<CPVariableElement> elements= fVariableBlock.getSelectedElements();
108 StatusInfo status= new StatusInfo();
109 if (elements.size() != 1) {
110 status.setError(""); //$NON-NLS-1$
112 updateStatus(status);
115 * @see org.eclipse.jface.window.Window#configureShell(Shell)
118 protected void configureShell(Shell newShell) {
119 super.configureShell(newShell);
120 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.CHOOSE_VARIABLE_DIALOG);
124 private Button fBrowseVariableButton;
125 private String fVariableButtonLabel;
127 public VariablePathDialogField(IStringButtonAdapter adapter) {
131 public void setVariableButtonLabel(String label) {
132 fVariableButtonLabel= label;
135 // ------- layout helpers
138 public Control[] doFillIntoGrid(Composite parent, int nColumns) {
139 assertEnoughColumns(nColumns);
141 Label label= getLabelControl(parent);
142 label.setLayoutData(gridDataForLabel(1));
143 Text text= getTextControl(parent);
144 text.setLayoutData(gridDataForText(nColumns - 3));
145 Button variableButton= getBrowseVariableControl(parent);
146 variableButton.setLayoutData(gridDataForButton(variableButton, 1));
147 Button browseButton= getChangeControl(parent);
148 browseButton.setLayoutData(gridDataForButton(browseButton, 1));
149 return new Control[] { label, text, variableButton, browseButton };
153 public int getNumberOfControls() {
156 public Button getBrowseVariableControl(Composite parent) {
157 if (fBrowseVariableButton == null) {
158 assertCompositeNotNull(parent);
160 fBrowseVariableButton= new Button(parent, SWT.PUSH);
161 fBrowseVariableButton.setText(fVariableButtonLabel);
162 fBrowseVariableButton.setEnabled(isEnabled());
163 fBrowseVariableButton.addSelectionListener(new SelectionListener() {
164 public void widgetDefaultSelected(SelectionEvent e) {
165 chooseVariablePressed();
167 public void widgetSelected(SelectionEvent e) {
168 chooseVariablePressed();
173 return fBrowseVariableButton;
176 public IPath getPath() {
177 return new Path(getText());
180 public String getVariable() {
181 IPath path= getPath();
182 if (!path.isEmpty()) {
183 return path.segment(0);
188 public IPath getPathExtension() {
189 return new Path(getText()).removeFirstSegments(1).setDevice(null);
192 public IPath getResolvedPath() {
193 String variable= getVariable();
194 if (variable != null) {
195 IPath path= JavaCore.getClasspathVariable(variable);
197 return path.append(getPathExtension());
203 private Shell getShell() {
204 if (fBrowseVariableButton != null) {
205 return fBrowseVariableButton.getShell();
207 return JavaPlugin.getActiveWorkbenchShell();
210 private void chooseVariablePressed() {
211 String variable= getVariable();
212 ChooseVariableDialog dialog= new ChooseVariableDialog(getShell(), variable);
213 if (dialog.open() == Window.OK) {
214 IPath newPath= new Path(dialog.getSelectedVariable()).append(getPathExtension());
215 setText(newPath.toString());
220 protected void updateEnableState() {
221 super.updateEnableState();
222 if (isOkToUse(fBrowseVariableButton)) {
223 fBrowseVariableButton.setEnabled(isEnabled());