13c863006500b5f0bfaaf9c4057a4109671ead8d
[phpeclipse.git] /
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 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;
38
39 public class PHPEnvironmentTab extends AbstractLaunchConfigurationTab {
40         protected ListViewer loadPathListViewer;
41         protected java.util.List installedInterpretersWorkingCopy;
42         protected Combo interpreterCombo;
43         protected Button loadPathDefaultButton;
44
45         public PHPEnvironmentTab() {
46                 super();
47         }
48
49         public void createControl(Composite parent) {
50                 Composite composite = createPageRoot(parent);
51
52                 TabFolder tabFolder = new TabFolder(composite, SWT.NONE);
53                 GridData gridData = new GridData(GridData.FILL_BOTH);
54                 tabFolder.setLayoutData(gridData);
55
56                 addLoadPathTab(tabFolder);
57                 addInterpreterTab(tabFolder);
58         }
59
60         protected void addLoadPathTab(TabFolder tabFolder) {
61                 Composite loadPathComposite = new Composite(tabFolder, SWT.NONE);
62                 loadPathComposite.setLayout(new GridLayout());
63
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));
68
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);
73
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());
78                 
79                 loadPathDefaultButton.setEnabled(false); //for now, until the load path is customizable on the configuration
80         }
81
82         protected SelectionListener getLoadPathSelectionListener() {
83                 return new SelectionAdapter() {
84                         public void widgetSelected(SelectionEvent e) {
85                                 System.out.println("Loadpath list selection occurred: " + e.getSource());
86                         }
87                 };
88         }
89
90         protected SelectionListener getLoadPathDefaultButtonSelectionListener() {
91                 return new SelectionAdapter() {
92                         public void widgetSelected(SelectionEvent e) {
93                                 setUseLoadPathDefaults(((Button) e.getSource()).getSelection());
94                         }
95                 };
96         }
97
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));
106
107                 createVerticalSpacer(interpreterComposite, 2);
108
109                 interpreterCombo = new Combo(interpreterComposite, SWT.READ_ONLY);
110                 interpreterCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
111                 initializeInterpreterCombo(interpreterCombo);
112                 interpreterCombo.addModifyListener(getInterpreterComboModifyListener());
113
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"));
120                                 editor.create();
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()));
126                                 }
127                         }
128                 });
129
130                 TabItem interpreterTab = new TabItem(tabFolder, SWT.NONE);
131                 interpreterTab.setText(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.interpreterTab.label"));
132                 interpreterTab.setControl(interpreterComposite);
133         }
134
135         protected ModifyListener getInterpreterComboModifyListener() {
136                 return new ModifyListener() {
137                         public void modifyText(ModifyEvent evt) {
138                                 updateLaunchConfigurationDialog();
139                         }
140                 };
141         }
142
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);
148         }
149
150         public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
151         }
152
153         public void initializeFrom(ILaunchConfiguration configuration) {
154                 initializeLoadPath(configuration);
155                 initializeInterpreterSelection(configuration);
156         }
157
158         protected void initializeLoadPath(ILaunchConfiguration configuration) {
159                 boolean useDefaultLoadPath = true;
160                 try {
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);
170                                         }
171                                 }
172                         }
173                 } catch (CoreException e) {
174                         log(e);
175                 }
176         }
177
178         protected void setUseLoadPathDefaults(boolean useDefaults) {
179                 loadPathListViewer.getList().setEnabled(!useDefaults);
180                 loadPathDefaultButton.setSelection(useDefaults);
181         }
182
183         protected void initializeInterpreterSelection(ILaunchConfiguration configuration) {
184                 String interpreterName = null;
185                 try {
186                         interpreterName = configuration.getAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
187                 } catch (CoreException e) {
188                         log(e);
189                 }
190                 if (interpreterName != null && !interpreterName.equals(""))
191                         interpreterCombo.select(interpreterCombo.indexOf(interpreterName));
192         }
193
194         protected void initializeInterpreterCombo(Combo interpreterCombo) {
195                 installedInterpretersWorkingCopy = new ArrayList();
196                 installedInterpretersWorkingCopy.addAll(PHPRuntime.getDefault().getInstalledInterpreters());
197
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();
202                 }
203                 interpreterCombo.setItems(interpreterNames);
204
205                 PHPInterpreter selectedInterpreter = PHPRuntime.getDefault().getSelectedInterpreter();
206                 if (selectedInterpreter != null)
207                         interpreterCombo.select(interpreterCombo.indexOf(selectedInterpreter.getName()));
208         }
209
210         public void performApply(ILaunchConfigurationWorkingCopy configuration) {
211                 int selectionIndex = interpreterCombo.getSelectionIndex();
212                 if (selectionIndex >= 0)
213                         configuration.setAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, interpreterCombo.getItem(selectionIndex));
214
215                 configuration.setAttribute(PHPLaunchConfigurationAttribute.USE_DEFAULT_LOAD_PATH, loadPathDefaultButton.getSelection());
216
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());
223                         }
224                         configuration.setAttribute(PHPLaunchConfigurationAttribute.CUSTOM_LOAD_PATH, loadPathStrings);
225                 }
226         }
227
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);
235
236                 return composite;
237         }
238
239         public String getName() {
240                 return PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.name");
241         }
242
243         public boolean isValid(ILaunchConfiguration launchConfig) {
244                 try {
245                         String selectedInterpreter = launchConfig.getAttribute(PHPLaunchConfigurationAttribute.SELECTED_INTERPRETER, "");
246                         if (selectedInterpreter.length() == 0) {
247                                 setErrorMessage(PHPDebugUiMessages.getString("LaunchConfigurationTab.PHPEnvironment.interpreter_not_selected_error_message"));
248                                 return false;
249                         }
250                 } catch (CoreException e) {
251                         log(e);
252                 }
253                 
254                 setErrorMessage(null);
255                 return true;
256         }
257         
258         protected void log(Throwable t) {
259                 PHPDebugUiPlugin.getDefault().log(t);
260         }
261
262         public Image getImage() {
263                 return PHPUiImages.get(PHPUiImages.IMG_CTOOLS_PHP);
264         }
265
266 }