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