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