1 package net.sourceforge.phpdt.internal.debug.ui.preferences;
5 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
6 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiPlugin;
7 import net.sourceforge.phpdt.internal.launching.PHPInterpreter;
8 import net.sourceforge.phpdt.internal.ui.dialogs.StatusDialog;
10 import org.eclipse.core.runtime.IPath;
11 import org.eclipse.core.runtime.IStatus;
12 import org.eclipse.core.runtime.Path;
13 import org.eclipse.core.runtime.Status;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.layout.RowData;
22 import org.eclipse.swt.layout.RowLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.DirectoryDialog;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.Text;
31 public class EditInterpreterDialog extends StatusDialog {
32 protected PHPInterpreter interpreterToEdit;
33 protected Text interpreterNameText, interpreterLocationText;
34 protected IStatus[] allStatus = new IStatus[2];
36 public EditInterpreterDialog(Shell parentShell, String aDialogTitle) {
38 setTitle(aDialogTitle);
41 public void setInterpreterToEdit(PHPInterpreter anInterpreter) {
42 interpreterToEdit = anInterpreter;
44 String interpreterName = interpreterToEdit.getName();
45 interpreterNameText.setText(interpreterName != null ? interpreterName : ""); //$NON-NLS-1$
47 IPath installLocation = interpreterToEdit.getInstallLocation();
48 interpreterLocationText.setText(installLocation != null ? installLocation.toOSString() : ""); //$NON-NLS-1$
51 protected void createLocationEntryField(Composite composite) {
52 new Label(composite, SWT.NONE).setText(PHPDebugUiMessages.getString("EditInterpreterDialog.PHPInterpreter.path.label")); //$NON-NLS-1$
54 Composite locationComposite = new Composite(composite, SWT.NONE);
55 RowLayout locationLayout = new RowLayout();
56 locationLayout.marginLeft = 0;
57 locationComposite.setLayout(locationLayout);
59 interpreterLocationText = new Text(locationComposite, SWT.SINGLE | SWT.BORDER);
60 interpreterLocationText.addModifyListener(new ModifyListener() {
61 public void modifyText(ModifyEvent e) {
62 allStatus[1] = validateInterpreterLocationText();
66 interpreterLocationText.setLayoutData(new RowData(120, SWT.DEFAULT));
68 Button browseButton = new Button(composite, SWT.PUSH);
69 browseButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
70 browseButton.setText(PHPDebugUiMessages.getString("EditInterpreterDialog.PHPInterpreter.path.browse.button.label")); //$NON-NLS-1$
71 browseButton.addSelectionListener(new SelectionAdapter() {
72 public void widgetSelected(SelectionEvent e) {
73 browseForInstallDir();
78 protected void updateStatusLine() {
79 updateStatus(getMostSevereStatus());
82 protected IStatus getMostSevereStatus() {
83 IStatus max = new Status(0, PHPDebugUiPlugin.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$
84 for (int i = 0; i < allStatus.length; i++) {
85 IStatus curr = allStatus[i];
87 if (curr.matches(IStatus.ERROR)) {
90 if (max == null || curr.getSeverity() > max.getSeverity()) {
98 protected IStatus validateInterpreterLocationText() {
99 File path = new File(interpreterLocationText.getText());
101 File unix_php = new File(path, "php"); //$NON-NLS-1$ //$NON-NLS-2$
102 File windows_php_exe = new File(path, "php.exe"); //$NON-NLS-1$ //$NON-NLS-2$
103 if (unix_php.isFile() || windows_php_exe.isFile())
104 return new Status(IStatus.OK, PHPDebugUiPlugin.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
107 return new Status(IStatus.ERROR, PHPDebugUiPlugin.PLUGIN_ID, 1, PHPDebugUiMessages.getString("EditInterpreterDialog.PHPInterpreter.path.error"), null); //$NON-NLS-1$
110 protected void createNameEntryField(Composite composite) {
111 new Label(composite, SWT.NONE).setText(PHPDebugUiMessages.getString("EditInterpreterDialog.PHPInterpreter.name")); //$NON-NLS-1$
113 GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
114 gridData.horizontalSpan = 2;
116 interpreterNameText = new Text(composite, SWT.SINGLE | SWT.BORDER);
117 interpreterNameText.addModifyListener(new ModifyListener() {
118 public void modifyText(ModifyEvent e) {
119 allStatus[0] = validateInterpreterNameText();
123 interpreterNameText.setLayoutData(gridData);
126 protected IStatus validateInterpreterNameText() {
127 int status = IStatus.OK;
128 String message = ""; //$NON-NLS-1$
130 if (interpreterNameText.getText() == null || interpreterNameText.getText().length() <= 0) {
131 status = IStatus.ERROR;
132 message = PHPDebugUiMessages.getString("EditInterpreterDialog.PHPInterpreter.name.error"); //$NON-NLS-1$
135 return new Status(status, PHPDebugUiPlugin.PLUGIN_ID, 0, message, null);
138 protected void browseForInstallDir() {
139 DirectoryDialog dialog = new DirectoryDialog(getShell());
140 dialog.setFilterPath(interpreterLocationText.getText());
141 dialog.setMessage(PHPDebugUiMessages.getString("EditInterpreterDialog.PHPInterpreter.path.browse.message")); //$NON-NLS-1$
142 String newPath = dialog.open();
144 interpreterLocationText.setText(newPath);
147 protected void okPressed() {
148 if (interpreterToEdit == null)
149 interpreterToEdit = new PHPInterpreter(null, null);
151 interpreterToEdit.setName(interpreterNameText.getText());
152 interpreterToEdit.setInstallLocation(new Path(interpreterLocationText.getText()));
155 protected Control createDialogArea(Composite parent) {
156 Composite composite = (Composite) super.createDialogArea(parent);
157 GridLayout layout = new GridLayout();
158 layout.numColumns = 3;
159 composite.setLayout(layout);
161 createNameEntryField(composite);
162 createLocationEntryField(composite);