3997682a77fdcb72ea32720c6f30bb454432209e
[phpeclipse.git] /
1 package net.sourceforge.phpdt.internal.debug.ui.preferences;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
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;
31
32 public class PHPInterpreterPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
33         protected CheckboxTableViewer tableViewer;
34         protected Button addButton, editButton, removeButton;
35
36         public PHPInterpreterPreferencePage() {
37                 super();
38         }
39
40         public void init(IWorkbench workbench) {}
41
42         protected Control createContents(Composite parent) {
43                 noDefaultAndApplyButton();
44
45                 Composite composite = createPageRoot(parent);
46                 Table table = createInstalledInterpretersTable(composite);
47                 createInstalledInterpretersTableViewer(table);
48                 createButtonGroup(composite);
49
50                 tableViewer.setInput(PHPRuntime.getDefault().getInstalledInterpreters());
51                 PHPInterpreter selectedInterpreter = PHPRuntime.getDefault().getSelectedInterpreter();
52                 if (selectedInterpreter != null)
53                         tableViewer.setChecked(selectedInterpreter, true);
54
55                 enableButtons();
56
57                 return composite;
58         }
59
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);
67
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) {
73                                 addInterpreter();
74                         }
75                 });
76
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) {
82                                 editInterpreter();
83                         }
84                 });
85
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) {
91                                 removeInterpreter();
92                         }
93                 });
94         }
95
96         protected void createInstalledInterpretersTableViewer(Table table) {
97                 tableViewer = new CheckboxTableViewer(table);
98
99                 tableViewer.setLabelProvider(new PHPInterpreterLabelProvider());
100                 tableViewer.setContentProvider(new PHPInterpreterContentProvider());
101
102                 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
103                         public void selectionChanged(SelectionChangedEvent evt) {
104                                 enableButtons();
105                         }
106                 });
107
108                 tableViewer.addCheckStateListener(new ICheckStateListener() {
109                         public void checkStateChanged(CheckStateChangedEvent event) {
110                                 updateSelectedInterpreter(event.getElement());
111                         }
112                 });
113                 
114                 tableViewer.addDoubleClickListener(new IDoubleClickListener() {
115                         public void doubleClick(DoubleClickEvent e) {
116                                 editInterpreter();
117                         }
118                 });
119         }
120
121         protected Table createInstalledInterpretersTable(Composite composite) {
122                 Table table = new Table(composite, SWT.CHECK | SWT.BORDER | SWT.FULL_SELECTION);
123
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);
130
131                 TableColumn column = new TableColumn(table, SWT.NULL);
132                 column.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.PHPInterpreterTable.interpreterName")); //$NON-NLS-1$
133                 column.setWidth(125);
134
135                 column = new TableColumn(table, SWT.NULL);
136                 column.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.PHPInterpreterTable.interpreterPath")); //$NON-NLS-1$
137                 column.setWidth(350);
138
139                 return table;
140         }
141
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);
147                 return composite;
148         }
149
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$
153                 editor.create();
154                 editor.setInterpreterToEdit(newInterpreter);
155                 if (EditInterpreterDialog.OK == editor.open())
156                         tableViewer.add(newInterpreter);
157         }
158
159         protected void removeInterpreter() {
160                 tableViewer.remove(getSelectedInterpreter());
161         }
162
163         protected void enableButtons() {
164                 if (getSelectedInterpreter() != null) {
165                         editButton.setEnabled(true);
166                         removeButton.setEnabled(true);
167                 } else {
168                         editButton.setEnabled(false);
169                         removeButton.setEnabled(false);
170                 }
171         }
172
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);
177                 }
178
179                 tableViewer.setChecked(interpreter, true);
180         }
181
182         protected void editInterpreter() {
183                 EditInterpreterDialog editor = new EditInterpreterDialog(getShell(), PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.EditInterpreterDialog.editInterpreter.title")); //$NON-NLS-1$
184                 editor.create();
185                 
186                 PHPInterpreter anInterpreter = getSelectedInterpreter();
187                 editor.setInterpreterToEdit(anInterpreter);
188                 if (EditInterpreterDialog.OK == editor.open())
189                         tableViewer.update(anInterpreter, null);
190         }
191         
192         protected PHPInterpreter getSelectedInterpreter() {
193                 IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
194                 return (PHPInterpreter) selection.getFirstElement();
195         }
196         
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);
203
204                 Object[] checkedElements = tableViewer.getCheckedElements();
205                 if (checkedElements.length > 0)
206                         PHPRuntime.getDefault().setSelectedInterpreter((PHPInterpreter) checkedElements[0]);
207
208                 return super.performOk();
209         }
210
211 }