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;
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;
40 public class PHPEnvironmentTab extends AbstractLaunchConfigurationTab {
41 protected ListViewer loadPathListViewer;
42 protected java.util.List installedInterpretersWorkingCopy;
43 protected Combo interpreterCombo;
44 protected Button loadPathDefaultButton;
46 public PHPEnvironmentTab() {
50 public void createControl(Composite parent) {
51 Composite composite = createPageRoot(parent);
53 TabFolder tabFolder = new TabFolder(composite, SWT.NONE);
54 GridData gridData = new GridData(GridData.FILL_BOTH);
55 tabFolder.setLayoutData(gridData);
57 addLoadPathTab(tabFolder);
58 addInterpreterTab(tabFolder);
61 protected void addLoadPathTab(TabFolder tabFolder) {
62 Composite loadPathComposite = new Composite(tabFolder, SWT.NONE);
63 loadPathComposite.setLayout(new GridLayout());
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));
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);
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());
80 loadPathDefaultButton.setEnabled(false); //for now, until the load path is customizable on the configuration
83 protected SelectionListener getLoadPathSelectionListener() {
84 return new SelectionAdapter() {
85 public void widgetSelected(SelectionEvent e) {
86 System.out.println("Loadpath list selection occurred: " + e.getSource());
91 protected SelectionListener getLoadPathDefaultButtonSelectionListener() {
92 return new SelectionAdapter() {
93 public void widgetSelected(SelectionEvent e) {
94 setUseLoadPathDefaults(((Button) e.getSource()).getSelection());
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));
108 createVerticalSpacer(interpreterComposite, 2);
110 interpreterCombo = new Combo(interpreterComposite, SWT.READ_ONLY);
111 interpreterCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
112 initializeInterpreterCombo(interpreterCombo);
113 interpreterCombo.addModifyListener(getInterpreterComboModifyListener());
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"));
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()));
131 TabItem interpreterTab = new TabItem(tabFolder, SWT.NONE);
132 interpreterTab.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.interpreterTab.label"));
133 interpreterTab.setControl(interpreterComposite);
136 protected ModifyListener getInterpreterComboModifyListener() {
137 return new ModifyListener() {
138 public void modifyText(ModifyEvent evt) {
139 updateLaunchConfigurationDialog();
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);
151 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
154 public void initializeFrom(ILaunchConfiguration configuration) {
155 initializeLoadPath(configuration);
156 initializeInterpreterSelection(configuration);
159 protected void initializeLoadPath(ILaunchConfiguration configuration) {
160 boolean useDefaultLoadPath = true;
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);
174 } catch (CoreException e) {
179 protected void setUseLoadPathDefaults(boolean useDefaults) {
180 loadPathListViewer.getList().setEnabled(!useDefaults);
181 loadPathDefaultButton.setSelection(useDefaults);
184 protected void initializeInterpreterSelection(ILaunchConfiguration configuration) {
185 String interpreterName = null;
187 interpreterName = configuration.getAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
188 } catch (CoreException e) {
191 if (interpreterName != null && !interpreterName.equals(""))
192 interpreterCombo.select(interpreterCombo.indexOf(interpreterName));
195 protected void initializeInterpreterCombo(Combo interpreterCombo) {
196 installedInterpretersWorkingCopy = new ArrayList();
197 installedInterpretersWorkingCopy.addAll(PHPRuntime.getDefault().getInstalledInterpreters());
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();
204 interpreterCombo.setItems(interpreterNames);
206 PHPInterpreter selectedInterpreter = PHPRuntime.getDefault().getSelectedInterpreter();
207 if (selectedInterpreter != null)
208 interpreterCombo.select(interpreterCombo.indexOf(selectedInterpreter.getName()));
211 public void performApply(ILaunchConfigurationWorkingCopy configuration) {
212 int selectionIndex = interpreterCombo.getSelectionIndex();
213 if (selectionIndex >= 0)
214 configuration.setAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, interpreterCombo.getItem(selectionIndex));
216 configuration.setAttribute(PHPLaunchConfigurationAttribute.USE_DEFAULT_LOAD_PATH, loadPathDefaultButton.getSelection());
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());
225 configuration.setAttribute(PHPLaunchConfigurationAttribute.CUSTOM_LOAD_PATH, loadPathStrings);
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);
240 public String getName() {
241 return PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.name");
244 public boolean isValid(ILaunchConfiguration launchConfig) {
246 String selectedInterpreter = launchConfig.getAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
247 if (selectedInterpreter.length() == 0) {
248 setErrorMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.interpreter_not_selected_error_message"));
251 } catch (CoreException e) {
255 setErrorMessage(null);
259 protected void log(Throwable t) {
260 PHPDebugUiPlugin.log(t);
263 public Image getImage() {
264 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP);