1 package net.sourceforge.phpdt.internal.debug.ui.preferences;
3 import java.util.ArrayList;
6 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
7 import net.sourceforge.phpdt.internal.launching.PHPInterpreter;
8 import net.sourceforge.phpdt.internal.launching.PHPRuntime;
10 import org.eclipse.jface.preference.PreferencePage;
11 import org.eclipse.jface.viewers.CheckStateChangedEvent;
12 import org.eclipse.jface.viewers.CheckboxTableViewer;
13 import org.eclipse.jface.viewers.DoubleClickEvent;
14 import org.eclipse.jface.viewers.ICheckStateListener;
15 import org.eclipse.jface.viewers.IDoubleClickListener;
16 import org.eclipse.jface.viewers.ISelectionChangedListener;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.viewers.SelectionChangedEvent;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Event;
26 import org.eclipse.swt.widgets.Listener;
27 import org.eclipse.swt.widgets.Table;
28 import org.eclipse.swt.widgets.TableColumn;
29 import org.eclipse.swt.widgets.TableItem;
30 import org.eclipse.ui.IWorkbench;
31 import org.eclipse.ui.IWorkbenchPreferencePage;
33 public class PHPInterpreterPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
34 protected CheckboxTableViewer tableViewer;
35 protected Button addButton, editButton, removeButton;
37 public PHPInterpreterPreferencePage() {
41 public void init(IWorkbench workbench) {}
43 protected Control createContents(Composite parent) {
44 noDefaultAndApplyButton();
46 Composite composite = createPageRoot(parent);
47 Table table = createInstalledInterpretersTable(composite);
48 createInstalledInterpretersTableViewer(table);
49 createButtonGroup(composite);
51 tableViewer.setInput(PHPRuntime.getDefault().getInstalledInterpreters());
52 PHPInterpreter selectedInterpreter = PHPRuntime.getDefault().getSelectedInterpreter();
53 if (selectedInterpreter != null)
54 tableViewer.setChecked(selectedInterpreter, true);
61 protected void createButtonGroup(Composite composite) {
62 Composite buttons = new Composite(composite, SWT.NULL);
63 buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
64 GridLayout layout = new GridLayout();
65 layout.marginHeight = 0;
66 layout.marginWidth = 0;
67 buttons.setLayout(layout);
69 addButton = new Button(buttons, SWT.PUSH);
70 addButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
71 addButton.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.addButton.label")); //$NON-NLS-1$
72 addButton.addListener(SWT.Selection, new Listener() {
73 public void handleEvent(Event evt) {
78 editButton = new Button(buttons, SWT.PUSH);
79 editButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
80 editButton.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.editButton.label")); //$NON-NLS-1$
81 editButton.addListener(SWT.Selection, new Listener() {
82 public void handleEvent(Event evt) {
87 removeButton = new Button(buttons, SWT.PUSH);
88 removeButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
89 removeButton.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.removeButton.label")); //$NON-NLS-1$
90 removeButton.addListener(SWT.Selection, new Listener() {
91 public void handleEvent(Event evt) {
97 protected void createInstalledInterpretersTableViewer(Table table) {
98 tableViewer = new CheckboxTableViewer(table);
100 tableViewer.setLabelProvider(new PHPInterpreterLabelProvider());
101 tableViewer.setContentProvider(new PHPInterpreterContentProvider());
103 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
104 public void selectionChanged(SelectionChangedEvent evt) {
109 tableViewer.addCheckStateListener(new ICheckStateListener() {
110 public void checkStateChanged(CheckStateChangedEvent event) {
111 updateSelectedInterpreter(event.getElement());
115 tableViewer.addDoubleClickListener(new IDoubleClickListener() {
116 public void doubleClick(DoubleClickEvent e) {
122 protected Table createInstalledInterpretersTable(Composite composite) {
123 Table table = new Table(composite, SWT.CHECK | SWT.BORDER | SWT.FULL_SELECTION);
125 GridData data = new GridData(GridData.FILL_BOTH);
126 data.widthHint = convertWidthInCharsToPixels(80);
127 data.heightHint = convertHeightInCharsToPixels(10);
128 table.setLayoutData(data);
129 table.setHeaderVisible(true);
130 table.setLinesVisible(false);
132 TableColumn column = new TableColumn(table, SWT.NULL);
133 column.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.PHPInterpreterTable.interpreterName")); //$NON-NLS-1$
134 column.setWidth(125);
136 column = new TableColumn(table, SWT.NULL);
137 column.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.PHPInterpreterTable.interpreterPath")); //$NON-NLS-1$
138 column.setWidth(350);
143 protected Composite createPageRoot(Composite parent) {
144 Composite composite = new Composite(parent, SWT.NULL);
145 GridLayout layout = new GridLayout();
146 layout.numColumns = 2;
147 composite.setLayout(layout);
151 protected void addInterpreter() {
152 PHPInterpreter newInterpreter = new PHPInterpreter(null, null);
153 EditInterpreterDialog editor = new EditInterpreterDialog(getShell(), PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.EditInterpreterDialog.addInterpreter.title")); //$NON-NLS-1$
155 editor.setInterpreterToEdit(newInterpreter);
156 if (EditInterpreterDialog.OK == editor.open())
157 tableViewer.add(newInterpreter);
160 protected void removeInterpreter() {
161 tableViewer.remove(getSelectedInterpreter());
164 protected void enableButtons() {
165 if (getSelectedInterpreter() != null) {
166 editButton.setEnabled(true);
167 removeButton.setEnabled(true);
169 editButton.setEnabled(false);
170 removeButton.setEnabled(false);
174 protected void updateSelectedInterpreter(Object interpreter) {
175 Object[] checkedElements = tableViewer.getCheckedElements();
176 for (int i = 0; i < checkedElements.length; i++) {
177 tableViewer.setChecked(checkedElements[i], false);
180 tableViewer.setChecked(interpreter, true);
183 protected void editInterpreter() {
184 EditInterpreterDialog editor = new EditInterpreterDialog(getShell(), PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.EditInterpreterDialog.editInterpreter.title")); //$NON-NLS-1$
187 PHPInterpreter anInterpreter = getSelectedInterpreter();
188 editor.setInterpreterToEdit(anInterpreter);
189 if (EditInterpreterDialog.OK == editor.open())
190 tableViewer.update(anInterpreter, null);
193 protected PHPInterpreter getSelectedInterpreter() {
194 IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
195 return (PHPInterpreter) selection.getFirstElement();
198 public boolean performOk() {
199 TableItem[] tableItems = tableViewer.getTable().getItems();
200 List installedInterpreters = new ArrayList(tableItems.length);
201 for (int i = 0; i < tableItems.length; i++)
202 installedInterpreters.add(tableItems[i].getData());
203 PHPRuntime.getDefault().setInstalledInterpreters(installedInterpreters);
205 Object[] checkedElements = tableViewer.getCheckedElements();
206 if (checkedElements.length > 0)
207 PHPRuntime.getDefault().setSelectedInterpreter((PHPInterpreter) checkedElements[0]);
209 return super.performOk();