1 package net.sourceforge.phpdt.internal.debug.ui.launcher;
3 import java.util.ArrayList;
4 import java.util.Iterator;
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 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.debug.core.ILaunchConfiguration;
19 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
20 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
21 import org.eclipse.jface.viewers.ListViewer;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.ModifyEvent;
24 import org.eclipse.swt.events.ModifyListener;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.events.SelectionListener;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Combo;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.TabFolder;
36 import org.eclipse.swt.widgets.TabItem;
37 import org.eclipse.ui.internal.dialogs.ListContentProvider;
39 public class PHPEnvironmentTab extends AbstractLaunchConfigurationTab {
40 protected ListViewer loadPathListViewer;
41 protected java.util.List installedInterpretersWorkingCopy;
42 protected Combo interpreterCombo;
43 protected Button loadPathDefaultButton;
45 public PHPEnvironmentTab() {
49 public void createControl(Composite parent) {
50 Composite composite = createPageRoot(parent);
52 TabFolder tabFolder = new TabFolder(composite, SWT.NONE);
53 GridData gridData = new GridData(GridData.FILL_BOTH);
54 tabFolder.setLayoutData(gridData);
56 addLoadPathTab(tabFolder);
57 addInterpreterTab(tabFolder);
60 protected void addLoadPathTab(TabFolder tabFolder) {
61 Composite loadPathComposite = new Composite(tabFolder, SWT.NONE);
62 loadPathComposite.setLayout(new GridLayout());
64 loadPathListViewer = new ListViewer(loadPathComposite, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
65 loadPathListViewer.setContentProvider(new ListContentProvider());
66 loadPathListViewer.setLabelProvider(new LoadPathEntryLabelProvider());
67 loadPathListViewer.getList().setLayoutData(new GridData(GridData.FILL_BOTH));
69 TabItem loadPathTab = new TabItem(tabFolder, SWT.NONE, 0);
70 loadPathTab.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.loadPathTab.label"));
71 loadPathTab.setControl(loadPathComposite);
72 loadPathTab.setData(loadPathListViewer);
74 loadPathDefaultButton = new Button(loadPathComposite, SWT.CHECK);
75 loadPathDefaultButton.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.loadPathDefaultButton.label"));
76 loadPathDefaultButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
77 loadPathDefaultButton.addSelectionListener(getLoadPathDefaultButtonSelectionListener());
79 loadPathDefaultButton.setEnabled(false); //for now, until the load path is customizable on the configuration
82 protected SelectionListener getLoadPathSelectionListener() {
83 return new SelectionAdapter() {
84 public void widgetSelected(SelectionEvent e) {
85 System.out.println("Loadpath list selection occurred: " + e.getSource());
90 protected SelectionListener getLoadPathDefaultButtonSelectionListener() {
91 return new SelectionAdapter() {
92 public void widgetSelected(SelectionEvent e) {
93 setUseLoadPathDefaults(((Button) e.getSource()).getSelection());
98 protected void addInterpreterTab(TabFolder tabFolder) {
99 Composite interpreterComposite = new Composite(tabFolder, SWT.NONE);
100 GridLayout layout = new GridLayout();
101 layout.numColumns = 2;
102 layout.marginHeight = 0;
103 layout.marginWidth = 0;
104 interpreterComposite.setLayout(layout);
105 interpreterComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
107 createVerticalSpacer(interpreterComposite, 2);
109 interpreterCombo = new Combo(interpreterComposite, SWT.READ_ONLY);
110 interpreterCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
111 initializeInterpreterCombo(interpreterCombo);
112 interpreterCombo.addModifyListener(getInterpreterComboModifyListener());
114 Button interpreterAddButton = new Button(interpreterComposite, SWT.PUSH);
115 interpreterAddButton.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.interpreterAddButton.label"));
116 interpreterAddButton.addSelectionListener(new SelectionAdapter() {
117 public void widgetSelected(SelectionEvent evt) {
118 PHPInterpreter newInterpreter = new PHPInterpreter(null, null);
119 EditInterpreterDialog editor = new EditInterpreterDialog(getShell(), PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.editInterpreterDialog.title"));
121 editor.setInterpreterToEdit(newInterpreter);
122 if (EditInterpreterDialog.OK == editor.open()) {
123 PHPRuntime.getDefault().addInstalledInterpreter(newInterpreter);
124 interpreterCombo.add(newInterpreter.getName());
125 interpreterCombo.select(interpreterCombo.indexOf(newInterpreter.getName()));
130 TabItem interpreterTab = new TabItem(tabFolder, SWT.NONE);
131 interpreterTab.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.interpreterTab.label"));
132 interpreterTab.setControl(interpreterComposite);
135 protected ModifyListener getInterpreterComboModifyListener() {
136 return new ModifyListener() {
137 public void modifyText(ModifyEvent evt) {
138 updateLaunchConfigurationDialog();
143 protected void createVerticalSpacer(Composite comp, int colSpan) {
144 Label label = new Label(comp, SWT.NONE);
145 GridData gd = new GridData();
146 gd.horizontalSpan = colSpan;
147 label.setLayoutData(gd);
150 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
153 public void initializeFrom(ILaunchConfiguration configuration) {
154 initializeLoadPath(configuration);
155 initializeInterpreterSelection(configuration);
158 protected void initializeLoadPath(ILaunchConfiguration configuration) {
159 boolean useDefaultLoadPath = true;
161 useDefaultLoadPath = configuration.getAttribute(PHPLaunchConfigurationAttribute.USE_DEFAULT_LOAD_PATH, true);
162 setUseLoadPathDefaults(useDefaultLoadPath);
163 if (useDefaultLoadPath) {
164 String projectName = configuration.getAttribute(PHPLaunchConfigurationAttribute.PROJECT_NAME, "");
165 if (projectName != "") {
166 PHPProject project = PHPCore.getPHPProject(projectName);
167 if (project != null) {
168 List loadPathEntries = project.getLoadPathEntries();
169 loadPathListViewer.setInput(loadPathEntries);
173 } catch (CoreException e) {
178 protected void setUseLoadPathDefaults(boolean useDefaults) {
179 loadPathListViewer.getList().setEnabled(!useDefaults);
180 loadPathDefaultButton.setSelection(useDefaults);
183 protected void initializeInterpreterSelection(ILaunchConfiguration configuration) {
184 String interpreterName = null;
186 interpreterName = configuration.getAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
187 } catch (CoreException e) {
190 if (interpreterName != null && !interpreterName.equals(""))
191 interpreterCombo.select(interpreterCombo.indexOf(interpreterName));
194 protected void initializeInterpreterCombo(Combo interpreterCombo) {
195 installedInterpretersWorkingCopy = new ArrayList();
196 installedInterpretersWorkingCopy.addAll(PHPRuntime.getDefault().getInstalledInterpreters());
198 String[] interpreterNames = new String[installedInterpretersWorkingCopy.size()];
199 for (int interpreterIndex = 0; interpreterIndex < installedInterpretersWorkingCopy.size(); interpreterIndex++) {
200 PHPInterpreter interpreter = (PHPInterpreter) installedInterpretersWorkingCopy.get(interpreterIndex);
201 interpreterNames[interpreterIndex] = interpreter.getName();
203 interpreterCombo.setItems(interpreterNames);
205 PHPInterpreter selectedInterpreter = PHPRuntime.getDefault().getSelectedInterpreter();
206 if (selectedInterpreter != null)
207 interpreterCombo.select(interpreterCombo.indexOf(selectedInterpreter.getName()));
210 public void performApply(ILaunchConfigurationWorkingCopy configuration) {
211 int selectionIndex = interpreterCombo.getSelectionIndex();
212 if (selectionIndex >= 0)
213 configuration.setAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, interpreterCombo.getItem(selectionIndex));
215 configuration.setAttribute(PHPLaunchConfigurationAttribute.USE_DEFAULT_LOAD_PATH, loadPathDefaultButton.getSelection());
217 if (!loadPathDefaultButton.getSelection()) {
218 List loadPathEntries = (List) loadPathListViewer.getInput();
219 List loadPathStrings = new ArrayList();
220 for (Iterator iterator = loadPathEntries.iterator(); iterator.hasNext();) {
221 LoadPathEntry entry = (LoadPathEntry) iterator.next();
222 loadPathStrings.add(entry.getPath().toString());
224 configuration.setAttribute(PHPLaunchConfigurationAttribute.CUSTOM_LOAD_PATH, loadPathStrings);
228 protected Composite createPageRoot(Composite parent) {
229 Composite composite = new Composite(parent, SWT.NULL);
230 GridLayout layout = new GridLayout();
231 layout.numColumns = 2;
232 composite.setLayout(layout);
233 createVerticalSpacer(composite, 2);
234 setControl(composite);
239 public String getName() {
240 return PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.name");
243 public boolean isValid(ILaunchConfiguration launchConfig) {
245 String selectedInterpreter = launchConfig.getAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
246 if (selectedInterpreter.length() == 0) {
247 setErrorMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.interpreter_not_selected_error_message"));
250 } catch (CoreException e) {
254 setErrorMessage(null);
258 protected void log(Throwable t) {
259 PHPDebugUiPlugin.getDefault().log(t);
262 public Image getImage() {
263 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP);