Switched to Eclipse 2.1 development
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.ui / src / net / sourceforge / phpdt / internal / debug / ui / preferences / EditInterpreterDialog.java
1 package net.sourceforge.phpdt.internal.debug.ui.preferences;
2
3 import java.io.File;
4
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.dialog.StatusDialog;
9
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;
30
31 public class EditInterpreterDialog extends StatusDialog {
32         protected PHPInterpreter interpreterToEdit;
33         protected Text interpreterNameText, interpreterLocationText;
34         protected IStatus[] allStatus = new IStatus[2];
35
36         public EditInterpreterDialog(Shell parentShell, String aDialogTitle) {
37                 super(parentShell);
38                 setTitle(aDialogTitle);
39         }
40         
41         public void setInterpreterToEdit(PHPInterpreter anInterpreter) {
42                 interpreterToEdit = anInterpreter;
43                 
44                 String interpreterName = interpreterToEdit.getName();
45                 interpreterNameText.setText(interpreterName != null ? interpreterName : ""); //$NON-NLS-1$
46
47                 IPath installLocation = interpreterToEdit.getInstallLocation();
48                 interpreterLocationText.setText(installLocation != null ? installLocation.toOSString() : ""); //$NON-NLS-1$
49         }
50
51         protected void createLocationEntryField(Composite composite) {
52                 new Label(composite, SWT.NONE).setText(PHPDebugUiMessages.getString("EditInterpreterDialog.PHPInterpreter.path.label")); //$NON-NLS-1$
53
54                 Composite locationComposite = new Composite(composite, SWT.NONE);
55                 RowLayout locationLayout = new RowLayout();
56                 locationLayout.marginLeft = 0;
57                 locationComposite.setLayout(locationLayout);
58
59                 interpreterLocationText = new Text(locationComposite, SWT.SINGLE | SWT.BORDER);
60                 interpreterLocationText.addModifyListener(new ModifyListener() {
61                         public void modifyText(ModifyEvent e) {
62                                 allStatus[1] = validateInterpreterLocationText();
63                                 updateStatusLine();
64                         }
65                 });
66                 interpreterLocationText.setLayoutData(new RowData(120, SWT.DEFAULT));
67
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();
74                         }
75                 });
76         }
77
78         protected void updateStatusLine() {
79                 updateStatus(getMostSevereStatus());
80         }
81
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];
86                         if (curr != null) {
87                                 if (curr.matches(IStatus.ERROR)) {
88                                         return curr;
89                                 }
90                                 if (max == null || curr.getSeverity() > max.getSeverity()) {
91                                         max = curr;
92                                 }
93                         }
94                 }
95                 return max;
96         }
97
98         protected IStatus validateInterpreterLocationText() {
99                 File path = new File(interpreterLocationText.getText());
100                 if (path.exists()) {
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$
105                 }
106
107                 return new Status(IStatus.ERROR, PHPDebugUiPlugin.PLUGIN_ID, 1, PHPDebugUiMessages.getString("EditInterpreterDialog.PHPInterpreter.path.error"), null); //$NON-NLS-1$
108         }
109
110         protected void createNameEntryField(Composite composite) {
111                 new Label(composite, SWT.NONE).setText(PHPDebugUiMessages.getString("EditInterpreterDialog.PHPInterpreter.name")); //$NON-NLS-1$
112
113                 GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
114                 gridData.horizontalSpan = 2;
115
116                 interpreterNameText = new Text(composite, SWT.SINGLE | SWT.BORDER);
117                 interpreterNameText.addModifyListener(new ModifyListener() {
118                         public void modifyText(ModifyEvent e) {
119                                 allStatus[0] = validateInterpreterNameText();
120                                 updateStatusLine();
121                         }
122                 });
123                 interpreterNameText.setLayoutData(gridData);
124         }
125
126         protected IStatus validateInterpreterNameText() {
127                 int status = IStatus.OK;
128                 String message = ""; //$NON-NLS-1$
129
130                 if (interpreterNameText.getText() == null || interpreterNameText.getText().length() <= 0) {
131                         status = IStatus.ERROR;
132                         message = PHPDebugUiMessages.getString("EditInterpreterDialog.PHPInterpreter.name.error"); //$NON-NLS-1$
133                 }
134
135                 return new Status(status, PHPDebugUiPlugin.PLUGIN_ID, 0, message, null);
136         }
137
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();
143                 if (newPath != null)
144                         interpreterLocationText.setText(newPath);
145         }
146
147         protected void okPressed() {
148                 if (interpreterToEdit == null)
149                         interpreterToEdit = new PHPInterpreter(null, null);
150
151                 interpreterToEdit.setName(interpreterNameText.getText());
152                 interpreterToEdit.setInstallLocation(new Path(interpreterLocationText.getText()));
153                 super.okPressed();
154         }
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);
160
161                 createNameEntryField(composite);
162                 createLocationEntryField(composite);
163                 
164                 return composite;
165         }
166
167 }