1 package net.sourceforge.phpdt.internal.debug.ui.preferences;
3 import java.util.ArrayList;
6 import org.eclipse.jface.preference.PreferencePage;
7 import org.eclipse.jface.viewers.CheckStateChangedEvent;
8 import org.eclipse.jface.viewers.CheckboxTableViewer;
9 import org.eclipse.jface.viewers.DoubleClickEvent;
10 import org.eclipse.jface.viewers.ICheckStateListener;
11 import org.eclipse.jface.viewers.IDoubleClickListener;
12 import org.eclipse.jface.viewers.ISelectionChangedListener;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.jface.viewers.SelectionChangedEvent;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.layout.GridLayout;
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.Event;
22 import org.eclipse.swt.widgets.Listener;
23 import org.eclipse.swt.widgets.Table;
24 import org.eclipse.swt.widgets.TableColumn;
25 import org.eclipse.swt.widgets.TableItem;
26 import org.eclipse.ui.IWorkbench;
27 import org.eclipse.ui.IWorkbenchPreferencePage;
28 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
29 import net.sourceforge.phpdt.internal.launching.PHPInterpreter;
30 import net.sourceforge.phpdt.internal.launching.PHPRuntime;
32 public class PHPInterpreterPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
33 protected CheckboxTableViewer tableViewer;
34 protected Button addButton, editButton, removeButton;
36 public PHPInterpreterPreferencePage() {
40 public void init(IWorkbench workbench) {}
42 protected Control createContents(Composite parent) {
43 noDefaultAndApplyButton();
45 Composite composite = createPageRoot(parent);
46 Table table = createInstalledInterpretersTable(composite);
47 createInstalledInterpretersTableViewer(table);
48 createButtonGroup(composite);
50 tableViewer.setInput(PHPRuntime.getDefault().getInstalledInterpreters());
51 PHPInterpreter selectedInterpreter = PHPRuntime.getDefault().getSelectedInterpreter();
52 if (selectedInterpreter != null)
53 tableViewer.setChecked(selectedInterpreter, true);
60 protected void createButtonGroup(Composite composite) {
61 Composite buttons = new Composite(composite, SWT.NULL);
62 buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
63 GridLayout layout = new GridLayout();
64 layout.marginHeight = 0;
65 layout.marginWidth = 0;
66 buttons.setLayout(layout);
68 addButton = new Button(buttons, SWT.PUSH);
69 addButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
70 addButton.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.addButton.label")); //$NON-NLS-1$
71 addButton.addListener(SWT.Selection, new Listener() {
72 public void handleEvent(Event evt) {
77 editButton = new Button(buttons, SWT.PUSH);
78 editButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
79 editButton.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.editButton.label")); //$NON-NLS-1$
80 editButton.addListener(SWT.Selection, new Listener() {
81 public void handleEvent(Event evt) {
86 removeButton = new Button(buttons, SWT.PUSH);
87 removeButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
88 removeButton.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.removeButton.label")); //$NON-NLS-1$
89 removeButton.addListener(SWT.Selection, new Listener() {
90 public void handleEvent(Event evt) {
96 protected void createInstalledInterpretersTableViewer(Table table) {
97 tableViewer = new CheckboxTableViewer(table);
99 tableViewer.setLabelProvider(new PHPInterpreterLabelProvider());
100 tableViewer.setContentProvider(new PHPInterpreterContentProvider());
102 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
103 public void selectionChanged(SelectionChangedEvent evt) {
108 tableViewer.addCheckStateListener(new ICheckStateListener() {
109 public void checkStateChanged(CheckStateChangedEvent event) {
110 updateSelectedInterpreter(event.getElement());
114 tableViewer.addDoubleClickListener(new IDoubleClickListener() {
115 public void doubleClick(DoubleClickEvent e) {
121 protected Table createInstalledInterpretersTable(Composite composite) {
122 Table table = new Table(composite, SWT.CHECK | SWT.BORDER | SWT.FULL_SELECTION);
124 GridData data = new GridData(GridData.FILL_BOTH);
125 data.widthHint = convertWidthInCharsToPixels(80);
126 data.heightHint = convertHeightInCharsToPixels(10);
127 table.setLayoutData(data);
128 table.setHeaderVisible(true);
129 table.setLinesVisible(false);
131 TableColumn column = new TableColumn(table, SWT.NULL);
132 column.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.PHPInterpreterTable.interpreterName")); //$NON-NLS-1$
133 column.setWidth(125);
135 column = new TableColumn(table, SWT.NULL);
136 column.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.PHPInterpreterTable.interpreterPath")); //$NON-NLS-1$
137 column.setWidth(350);
142 protected Composite createPageRoot(Composite parent) {
143 Composite composite = new Composite(parent, SWT.NULL);
144 GridLayout layout = new GridLayout();
145 layout.numColumns = 2;
146 composite.setLayout(layout);
150 protected void addInterpreter() {
151 PHPInterpreter newInterpreter = new PHPInterpreter(null, null);
152 EditInterpreterDialog editor = new EditInterpreterDialog(getShell(), PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.EditInterpreterDialog.addInterpreter.title")); //$NON-NLS-1$
154 editor.setInterpreterToEdit(newInterpreter);
155 if (EditInterpreterDialog.OK == editor.open())
156 tableViewer.add(newInterpreter);
159 protected void removeInterpreter() {
160 tableViewer.remove(getSelectedInterpreter());
163 protected void enableButtons() {
164 if (getSelectedInterpreter() != null) {
165 editButton.setEnabled(true);
166 removeButton.setEnabled(true);
168 editButton.setEnabled(false);
169 removeButton.setEnabled(false);
173 protected void updateSelectedInterpreter(Object interpreter) {
174 Object[] checkedElements = tableViewer.getCheckedElements();
175 for (int i = 0; i < checkedElements.length; i++) {
176 tableViewer.setChecked(checkedElements[i], false);
179 tableViewer.setChecked(interpreter, true);
182 protected void editInterpreter() {
183 EditInterpreterDialog editor = new EditInterpreterDialog(getShell(), PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.EditInterpreterDialog.editInterpreter.title")); //$NON-NLS-1$
186 PHPInterpreter anInterpreter = getSelectedInterpreter();
187 editor.setInterpreterToEdit(anInterpreter);
188 if (EditInterpreterDialog.OK == editor.open())
189 tableViewer.update(anInterpreter, null);
192 protected PHPInterpreter getSelectedInterpreter() {
193 IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
194 return (PHPInterpreter) selection.getFirstElement();
197 public boolean performOk() {
198 TableItem[] tableItems = tableViewer.getTable().getItems();
199 List installedInterpreters = new ArrayList(tableItems.length);
200 for (int i = 0; i < tableItems.length; i++)
201 installedInterpreters.add(tableItems[i].getData());
202 PHPRuntime.getDefault().setInstalledInterpreters(installedInterpreters);
204 Object[] checkedElements = tableViewer.getCheckedElements();
205 if (checkedElements.length > 0)
206 PHPRuntime.getDefault().setSelectedInterpreter((PHPInterpreter) checkedElements[0]);
208 return super.performOk();