1 package net.sourceforge.phpdt.internal.debug.ui.preferences;
5 import org.eclipse.core.runtime.IPath;
6 import org.eclipse.core.runtime.IStatus;
7 import org.eclipse.core.runtime.Path;
8 import org.eclipse.core.runtime.Status;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.ModifyEvent;
11 import org.eclipse.swt.events.ModifyListener;
12 import org.eclipse.swt.events.SelectionAdapter;
13 import org.eclipse.swt.events.SelectionEvent;
14 import org.eclipse.swt.layout.GridData;
15 import org.eclipse.swt.layout.GridLayout;
16 import org.eclipse.swt.layout.RowData;
17 import org.eclipse.swt.layout.RowLayout;
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.DirectoryDialog;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.swt.widgets.Text;
25 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
26 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiPlugin;
27 import net.sourceforge.phpdt.internal.launching.PHPInterpreter;
28 import net.sourceforge.phpdt.internal.ui.dialog.StatusDialog;
30 public class EditInterpreterDialog extends StatusDialog {
31 protected PHPInterpreter interpreterToEdit;
32 protected Text interpreterNameText, interpreterLocationText;
33 protected IStatus[] allStatus = new IStatus[2];
35 public EditInterpreterDialog(Shell parentShell, String aDialogTitle) {
37 setTitle(aDialogTitle);
40 public void setInterpreterToEdit(PHPInterpreter anInterpreter) {
41 interpreterToEdit = anInterpreter;
43 String interpreterName = interpreterToEdit.getName();
44 interpreterNameText.setText(interpreterName != null ? interpreterName : ""); //$NON-NLS-1$
46 IPath installLocation = interpreterToEdit.getInstallLocation();
47 interpreterLocationText.setText(installLocation != null ? installLocation.toOSString() : ""); //$NON-NLS-1$
50 protected void createLocationEntryField(Composite composite) {
51 new Label(composite, SWT.NONE).setText(PHPDebugUiMessages.getString("EditInterpreterDialog.PHPInterpreter.path.label")); //$NON-NLS-1$
53 Composite locationComposite = new Composite(composite, SWT.NONE);
54 RowLayout locationLayout = new RowLayout();
55 locationLayout.marginLeft = 0;
56 locationComposite.setLayout(locationLayout);
58 interpreterLocationText = new Text(locationComposite, SWT.SINGLE | SWT.BORDER);
59 interpreterLocationText.addModifyListener(new ModifyListener() {
60 public void modifyText(ModifyEvent e) {
61 allStatus[1] = validateInterpreterLocationText();
65 interpreterLocationText.setLayoutData(new RowData(120, SWT.DEFAULT));
67 Button browseButton = new Button(composite, SWT.PUSH);
68 browseButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
69 browseButton.setText(PHPDebugUiMessages.getString("EditInterpreterDialog.PHPInterpreter.path.browse.button.label")); //$NON-NLS-1$
70 browseButton.addSelectionListener(new SelectionAdapter() {
71 public void widgetSelected(SelectionEvent e) {
72 browseForInstallDir();
77 protected void updateStatusLine() {
78 updateStatus(getMostSevereStatus());
81 protected IStatus getMostSevereStatus() {
82 IStatus max = new Status(0, PHPDebugUiPlugin.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$
83 for (int i = 0; i < allStatus.length; i++) {
84 IStatus curr = allStatus[i];
86 if (curr.matches(IStatus.ERROR)) {
89 if (max == null || curr.getSeverity() > max.getSeverity()) {
97 protected IStatus validateInterpreterLocationText() {
98 File path = new File(interpreterLocationText.getText());
100 File unix_php = new File(path, "php"); //$NON-NLS-1$ //$NON-NLS-2$
101 File windows_php_exe = new File(path, "php.exe"); //$NON-NLS-1$ //$NON-NLS-2$
102 if (unix_php.isFile() || windows_php_exe.isFile())
103 return new Status(IStatus.OK, PHPDebugUiPlugin.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
106 return new Status(IStatus.ERROR, PHPDebugUiPlugin.PLUGIN_ID, 1, PHPDebugUiMessages.getString("EditInterpreterDialog.PHPInterpreter.path.error"), null); //$NON-NLS-1$
109 protected void createNameEntryField(Composite composite) {
110 new Label(composite, SWT.NONE).setText(PHPDebugUiMessages.getString("EditInterpreterDialog.PHPInterpreter.name")); //$NON-NLS-1$
112 GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
113 gridData.horizontalSpan = 2;
115 interpreterNameText = new Text(composite, SWT.SINGLE | SWT.BORDER);
116 interpreterNameText.addModifyListener(new ModifyListener() {
117 public void modifyText(ModifyEvent e) {
118 allStatus[0] = validateInterpreterNameText();
122 interpreterNameText.setLayoutData(gridData);
125 protected IStatus validateInterpreterNameText() {
126 int status = IStatus.OK;
127 String message = ""; //$NON-NLS-1$
129 if (interpreterNameText.getText() == null || interpreterNameText.getText().length() <= 0) {
130 status = IStatus.ERROR;
131 message = PHPDebugUiMessages.getString("EditInterpreterDialog.PHPInterpreter.name.error"); //$NON-NLS-1$
134 return new Status(status, PHPDebugUiPlugin.PLUGIN_ID, 0, message, null);
137 protected void browseForInstallDir() {
138 DirectoryDialog dialog = new DirectoryDialog(getShell());
139 dialog.setFilterPath(interpreterLocationText.getText());
140 dialog.setMessage(PHPDebugUiMessages.getString("EditInterpreterDialog.PHPInterpreter.path.browse.message")); //$NON-NLS-1$
141 String newPath = dialog.open();
143 interpreterLocationText.setText(newPath);
146 protected void okPressed() {
147 if (interpreterToEdit == null)
148 interpreterToEdit = new PHPInterpreter(null, null);
150 interpreterToEdit.setName(interpreterNameText.getText());
151 interpreterToEdit.setInstallLocation(new Path(interpreterLocationText.getText()));
154 protected Control createDialogArea(Composite parent) {
155 Composite composite = (Composite) super.createDialogArea(parent);
156 GridLayout layout = new GridLayout();
157 layout.numColumns = 3;
158 composite.setLayout(layout);
160 createNameEntryField(composite);
161 createLocationEntryField(composite);