863d51f10d7dbdd208a09086c5ba7e3bcd660619
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.ui / src / net / sourceforge / phpdt / internal / debug / ui / launcher / PHPEnvironmentTab.java
1 package net.sourceforge.phpdt.internal.debug.ui.launcher;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
8 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiPlugin;
9 import net.sourceforge.phpdt.internal.debug.ui.preferences.EditInterpreterDialog;
10 import net.sourceforge.phpdt.internal.launching.PHPInterpreter;
11 import net.sourceforge.phpdt.internal.launching.PHPLaunchConfigurationAttribute;
12 import net.sourceforge.phpdt.internal.launching.PHPRuntime;
13 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
14 import net.sourceforge.phpeclipse.LoadPathEntry;
15 import net.sourceforge.phpeclipse.PHPCore;
16 import net.sourceforge.phpeclipse.resourcesview.PHPProject;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.debug.core.ILaunchConfiguration;
20 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
21 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
22 import org.eclipse.jface.viewers.ListViewer;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.ModifyEvent;
25 import org.eclipse.swt.events.ModifyListener;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.events.SelectionListener;
29 import org.eclipse.swt.graphics.Image;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Combo;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.swt.widgets.TabFolder;
37 import org.eclipse.swt.widgets.TabItem;
38 import org.eclipse.ui.internal.dialogs.ListContentProvider;
39
40 public class PHPEnvironmentTab extends AbstractLaunchConfigurationTab {
41         protected ListViewer loadPathListViewer;
42         protected java.util.List installedInterpretersWorkingCopy;
43         protected Combo interpreterCombo;
44         protected Button loadPathDefaultButton;
45
46         public PHPEnvironmentTab() {
47                 super();
48         }
49
50         public void createControl(Composite parent) {
51                 Composite composite = createPageRoot(parent);
52
53                 TabFolder tabFolder = new TabFolder(composite, SWT.NONE);
54                 GridData gridData = new GridData(GridData.FILL_BOTH);
55                 tabFolder.setLayoutData(gridData);
56
57                 addLoadPathTab(tabFolder);
58                 addInterpreterTab(tabFolder);
59         }
60
61         protected void addLoadPathTab(TabFolder tabFolder) {
62                 Composite loadPathComposite = new Composite(tabFolder, SWT.NONE);
63                 loadPathComposite.setLayout(new GridLayout());
64
65                 loadPathListViewer = new ListViewer(loadPathComposite, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
66                 loadPathListViewer.setContentProvider(new ListContentProvider());
67                 loadPathListViewer.setLabelProvider(new LoadPathEntryLabelProvider());
68                 loadPathListViewer.getList().setLayoutData(new GridData(GridData.FILL_BOTH));
69
70                 TabItem loadPathTab = new TabItem(tabFolder, SWT.NONE, 0);
71                 loadPathTab.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.loadPathTab.label"));
72                 loadPathTab.setControl(loadPathComposite);
73                 loadPathTab.setData(loadPathListViewer);
74
75                 loadPathDefaultButton = new Button(loadPathComposite, SWT.CHECK);
76                 loadPathDefaultButton.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.loadPathDefaultButton.label"));
77                 loadPathDefaultButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
78                 loadPathDefaultButton.addSelectionListener(getLoadPathDefaultButtonSelectionListener());
79                 
80                 loadPathDefaultButton.setEnabled(false); //for now, until the load path is customizable on the configuration
81         }
82
83         protected SelectionListener getLoadPathSelectionListener() {
84                 return new SelectionAdapter() {
85                         public void widgetSelected(SelectionEvent e) {
86                                 System.out.println("Loadpath list selection occurred: " + e.getSource());
87                         }
88                 };
89         }
90
91         protected SelectionListener getLoadPathDefaultButtonSelectionListener() {
92                 return new SelectionAdapter() {
93                         public void widgetSelected(SelectionEvent e) {
94                                 setUseLoadPathDefaults(((Button) e.getSource()).getSelection());
95                         }
96                 };
97         }
98
99         protected void addInterpreterTab(TabFolder tabFolder) {
100                 Composite interpreterComposite = new Composite(tabFolder, SWT.NONE);
101                 GridLayout layout = new GridLayout();
102                 layout.numColumns = 2;
103                 layout.marginHeight = 0;
104                 layout.marginWidth = 0;
105                 interpreterComposite.setLayout(layout);
106                 interpreterComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
107
108                 createVerticalSpacer(interpreterComposite, 2);
109
110                 interpreterCombo = new Combo(interpreterComposite, SWT.READ_ONLY);
111                 interpreterCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
112                 initializeInterpreterCombo(interpreterCombo);
113                 interpreterCombo.addModifyListener(getInterpreterComboModifyListener());
114
115                 Button interpreterAddButton = new Button(interpreterComposite, SWT.PUSH);
116                 interpreterAddButton.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.interpreterAddButton.label"));
117                 interpreterAddButton.addSelectionListener(new SelectionAdapter() {
118                         public void widgetSelected(SelectionEvent evt) {
119                                 PHPInterpreter newInterpreter = new PHPInterpreter(null, null);
120                                 EditInterpreterDialog editor = new EditInterpreterDialog(getShell(), PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.editInterpreterDialog.title"));
121                                 editor.create();
122                                 editor.setInterpreterToEdit(newInterpreter);
123                                 if (EditInterpreterDialog.OK == editor.open()) {
124                                         PHPRuntime.getDefault().addInstalledInterpreter(newInterpreter);
125                                         interpreterCombo.add(newInterpreter.getName());
126                                         interpreterCombo.select(interpreterCombo.indexOf(newInterpreter.getName()));
127                                 }
128                         }
129                 });
130
131                 TabItem interpreterTab = new TabItem(tabFolder, SWT.NONE);
132                 interpreterTab.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.interpreterTab.label"));
133                 interpreterTab.setControl(interpreterComposite);
134         }
135
136         protected ModifyListener getInterpreterComboModifyListener() {
137                 return new ModifyListener() {
138                         public void modifyText(ModifyEvent evt) {
139                                 updateLaunchConfigurationDialog();
140                         }
141                 };
142         }
143
144         protected void createVerticalSpacer(Composite comp, int colSpan) {
145                 Label label = new Label(comp, SWT.NONE);
146                 GridData gd = new GridData();
147                 gd.horizontalSpan = colSpan;
148                 label.setLayoutData(gd);
149         }
150
151         public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
152         }
153
154         public void initializeFrom(ILaunchConfiguration configuration) {
155                 initializeLoadPath(configuration);
156                 initializeInterpreterSelection(configuration);
157         }
158
159         protected void initializeLoadPath(ILaunchConfiguration configuration) {
160                 boolean useDefaultLoadPath = true;
161                 try {
162                         useDefaultLoadPath = configuration.getAttribute(PHPLaunchConfigurationAttribute.USE_DEFAULT_LOAD_PATH, true);
163                         setUseLoadPathDefaults(useDefaultLoadPath);
164                         if (useDefaultLoadPath) {
165                                 String projectName = configuration.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
166                                 if (projectName != "") {
167                                         PHPProject project = PHPCore.getPHPProject(projectName);
168                                         if (project != null) {
169                                                 List loadPathEntries = project.getLoadPathEntries();
170                                                 loadPathListViewer.setInput(loadPathEntries);
171                                         }
172                                 }
173                         }
174                 } catch (CoreException e) {
175                         log(e);
176                 }
177         }
178
179         protected void setUseLoadPathDefaults(boolean useDefaults) {
180                 loadPathListViewer.getList().setEnabled(!useDefaults);
181                 loadPathDefaultButton.setSelection(useDefaults);
182         }
183
184         protected void initializeInterpreterSelection(ILaunchConfiguration configuration) {
185                 String interpreterName = null;
186                 try {
187                         interpreterName = configuration.getAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
188                 } catch (CoreException e) {
189                         log(e);
190                 }
191                 if (interpreterName != null && !interpreterName.equals(""))
192                         interpreterCombo.select(interpreterCombo.indexOf(interpreterName));
193         }
194
195         protected void initializeInterpreterCombo(Combo interpreterCombo) {
196                 installedInterpretersWorkingCopy = new ArrayList();
197                 installedInterpretersWorkingCopy.addAll(PHPRuntime.getDefault().getInstalledInterpreters());
198
199                 String[] interpreterNames = new String[installedInterpretersWorkingCopy.size()];
200                 for (int interpreterIndex = 0; interpreterIndex < installedInterpretersWorkingCopy.size(); interpreterIndex++) {
201                         PHPInterpreter interpreter = (PHPInterpreter) installedInterpretersWorkingCopy.get(interpreterIndex);
202                         interpreterNames[interpreterIndex] = interpreter.getName();
203                 }
204                 interpreterCombo.setItems(interpreterNames);
205
206                 PHPInterpreter selectedInterpreter = PHPRuntime.getDefault().getSelectedInterpreter();
207                 if (selectedInterpreter != null)
208                         interpreterCombo.select(interpreterCombo.indexOf(selectedInterpreter.getName()));
209         }
210
211         public void performApply(ILaunchConfigurationWorkingCopy configuration) {
212                 int selectionIndex = interpreterCombo.getSelectionIndex();
213                 if (selectionIndex >= 0)
214                         configuration.setAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, interpreterCombo.getItem(selectionIndex));
215
216                 configuration.setAttribute(PHPLaunchConfigurationAttribute.USE_DEFAULT_LOAD_PATH, loadPathDefaultButton.getSelection());
217
218                 if (!loadPathDefaultButton.getSelection()) {
219                         List loadPathEntries = (List) loadPathListViewer.getInput();
220                         List loadPathStrings = new ArrayList();
221                         for (Iterator iterator = loadPathEntries.iterator(); iterator.hasNext();) {
222                                 LoadPathEntry entry = (LoadPathEntry) iterator.next();
223                                 loadPathStrings.add(entry.getPath().toString());
224                         }
225                         configuration.setAttribute(PHPLaunchConfigurationAttribute.CUSTOM_LOAD_PATH, loadPathStrings);
226                 }
227         }
228
229         protected Composite createPageRoot(Composite parent) {
230                 Composite composite = new Composite(parent, SWT.NULL);
231                 GridLayout layout = new GridLayout();
232                 layout.numColumns = 2;
233                 composite.setLayout(layout);
234                 createVerticalSpacer(composite, 2);
235                 setControl(composite);
236
237                 return composite;
238         }
239
240         public String getName() {
241                 return PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.name");
242         }
243
244         public boolean isValid(ILaunchConfiguration launchConfig) {
245                 try {
246                         String selectedInterpreter = launchConfig.getAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
247                         if (selectedInterpreter.length() == 0) {
248                                 setErrorMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.interpreter_not_selected_error_message"));
249                                 return false;
250                         }
251                 } catch (CoreException e) {
252                         log(e);
253                 }
254                 
255                 setErrorMessage(null);
256                 return true;
257         }
258         
259         protected void log(Throwable t) {
260                 PHPDebugUiPlugin.getDefault().log(t);
261         }
262
263         public Image getImage() {
264                 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP);
265         }
266
267 }