Refactory: remove unused classes, imports, fields and methods.
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.ui / src / net / sourceforge / phpdt / internal / debug / ui / preferences / PHPInterpreterPreferencePage.java
index 3cfe475..84a4f31 100644 (file)
@@ -1,5 +1,6 @@
 package net.sourceforge.phpdt.internal.debug.ui.preferences;
 
+import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -7,6 +8,7 @@ import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
 import net.sourceforge.phpdt.internal.launching.PHPInterpreter;
 import net.sourceforge.phpdt.internal.launching.PHPRuntime;
 
+import org.eclipse.core.runtime.Platform;
 import org.eclipse.jface.preference.PreferencePage;
 import org.eclipse.jface.viewers.CheckStateChangedEvent;
 import org.eclipse.jface.viewers.CheckboxTableViewer;
@@ -23,22 +25,27 @@ import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.FileDialog;
 import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Table;
 import org.eclipse.swt.widgets.TableColumn;
 import org.eclipse.swt.widgets.TableItem;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchPreferencePage;
 
-public class PHPInterpreterPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
+public class PHPInterpreterPreferencePage extends PreferencePage implements
+               IWorkbenchPreferencePage {
        protected CheckboxTableViewer tableViewer;
+
        protected Button addButton, editButton, removeButton;
 
        public PHPInterpreterPreferencePage() {
                super();
        }
 
-       public void init(IWorkbench workbench) {}
+       public void init(IWorkbench workbench) {
+       }
 
        protected Control createContents(Composite parent) {
                noDefaultAndApplyButton();
@@ -48,8 +55,10 @@ public class PHPInterpreterPreferencePage extends PreferencePage implements IWor
                createInstalledInterpretersTableViewer(table);
                createButtonGroup(composite);
 
-               tableViewer.setInput(PHPRuntime.getDefault().getInstalledInterpreters());
-               PHPInterpreter selectedInterpreter = PHPRuntime.getDefault().getSelectedInterpreter();
+               tableViewer
+                               .setInput(PHPRuntime.getDefault().getInstalledInterpreters());
+               PHPInterpreter selectedInterpreter = PHPRuntime.getDefault()
+                               .getSelectedInterpreter();
                if (selectedInterpreter != null)
                        tableViewer.setChecked(selectedInterpreter, true);
 
@@ -68,7 +77,8 @@ public class PHPInterpreterPreferencePage extends PreferencePage implements IWor
 
                addButton = new Button(buttons, SWT.PUSH);
                addButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
-               addButton.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.addButton.label")); //$NON-NLS-1$
+               addButton.setText(PHPDebugUiMessages
+                               .getString("PHPInterpreterPreferencePage.addButton.label")); //$NON-NLS-1$
                addButton.addListener(SWT.Selection, new Listener() {
                        public void handleEvent(Event evt) {
                                addInterpreter();
@@ -77,7 +87,8 @@ public class PHPInterpreterPreferencePage extends PreferencePage implements IWor
 
                editButton = new Button(buttons, SWT.PUSH);
                editButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
-               editButton.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.editButton.label")); //$NON-NLS-1$
+               editButton.setText(PHPDebugUiMessages
+                               .getString("PHPInterpreterPreferencePage.editButton.label")); //$NON-NLS-1$
                editButton.addListener(SWT.Selection, new Listener() {
                        public void handleEvent(Event evt) {
                                editInterpreter();
@@ -86,7 +97,8 @@ public class PHPInterpreterPreferencePage extends PreferencePage implements IWor
 
                removeButton = new Button(buttons, SWT.PUSH);
                removeButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
-               removeButton.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.removeButton.label")); //$NON-NLS-1$
+               removeButton.setText(PHPDebugUiMessages
+                               .getString("PHPInterpreterPreferencePage.removeButton.label")); //$NON-NLS-1$
                removeButton.addListener(SWT.Selection, new Listener() {
                        public void handleEvent(Event evt) {
                                removeInterpreter();
@@ -100,18 +112,19 @@ public class PHPInterpreterPreferencePage extends PreferencePage implements IWor
                tableViewer.setLabelProvider(new PHPInterpreterLabelProvider());
                tableViewer.setContentProvider(new PHPInterpreterContentProvider());
 
-               tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
-                       public void selectionChanged(SelectionChangedEvent evt) {
-                               enableButtons();
-                       }
-               });
+               tableViewer
+                               .addSelectionChangedListener(new ISelectionChangedListener() {
+                                       public void selectionChanged(SelectionChangedEvent evt) {
+                                               enableButtons();
+                                       }
+                               });
 
                tableViewer.addCheckStateListener(new ICheckStateListener() {
                        public void checkStateChanged(CheckStateChangedEvent event) {
                                updateSelectedInterpreter(event.getElement());
                        }
                });
-               
+
                tableViewer.addDoubleClickListener(new IDoubleClickListener() {
                        public void doubleClick(DoubleClickEvent e) {
                                editInterpreter();
@@ -120,7 +133,8 @@ public class PHPInterpreterPreferencePage extends PreferencePage implements IWor
        }
 
        protected Table createInstalledInterpretersTable(Composite composite) {
-               Table table = new Table(composite, SWT.CHECK | SWT.BORDER | SWT.FULL_SELECTION);
+               Table table = new Table(composite, SWT.CHECK | SWT.BORDER
+                               | SWT.FULL_SELECTION);
 
                GridData data = new GridData(GridData.FILL_BOTH);
                data.widthHint = convertWidthInCharsToPixels(80);
@@ -130,12 +144,15 @@ public class PHPInterpreterPreferencePage extends PreferencePage implements IWor
                table.setLinesVisible(false);
 
                TableColumn column = new TableColumn(table, SWT.NULL);
-               column.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.PHPInterpreterTable.interpreterName")); //$NON-NLS-1$
-               column.setWidth(125);
+               column
+                               .setText(PHPDebugUiMessages
+                                               .getString("PHPInterpreterPreferencePage.PHPInterpreterTable.interpreterPath")); //$NON-NLS-1$
+               column.setWidth(400);
 
-               column = new TableColumn(table, SWT.NULL);
-               column.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.PHPInterpreterTable.interpreterPath")); //$NON-NLS-1$
-               column.setWidth(350);
+               // column = new TableColumn(table, SWT.NULL);
+               // column.setText(PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.PHPInterpreterTable.interpreterPath"));
+               // //$NON-NLS-1$
+               // column.setWidth(350);
 
                return table;
        }
@@ -149,12 +166,12 @@ public class PHPInterpreterPreferencePage extends PreferencePage implements IWor
        }
 
        protected void addInterpreter() {
-               PHPInterpreter newInterpreter = new PHPInterpreter(null, null);
-               EditInterpreterDialog editor = new EditInterpreterDialog(getShell(), PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.EditInterpreterDialog.addInterpreter.title")); //$NON-NLS-1$
-               editor.create();
-               editor.setInterpreterToEdit(newInterpreter);
-               if (EditInterpreterDialog.OK == editor.open())
+               PHPInterpreter newInterpreter = new PHPInterpreter(null);
+               File phpRuntime = getFile(getShell(), null);
+               if (phpRuntime != null) {
+                       newInterpreter.setInstallLocation(phpRuntime);
                        tableViewer.add(newInterpreter);
+               }
        }
 
        protected void removeInterpreter() {
@@ -181,20 +198,27 @@ public class PHPInterpreterPreferencePage extends PreferencePage implements IWor
        }
 
        protected void editInterpreter() {
-               EditInterpreterDialog editor = new EditInterpreterDialog(getShell(), PHPDebugUiMessages.getString("PHPInterpreterPreferencePage.EditInterpreterDialog.editInterpreter.title")); //$NON-NLS-1$
-               editor.create();
-               
                PHPInterpreter anInterpreter = getSelectedInterpreter();
-               editor.setInterpreterToEdit(anInterpreter);
-               if (EditInterpreterDialog.OK == editor.open())
+               File phpRuntime = anInterpreter.getInstallLocation();
+               if (phpRuntime != null) {
+                       File parent = phpRuntime.getParentFile();
+                       phpRuntime = getFile(getShell(), parent);
+               } else {
+                       phpRuntime = getFile(getShell(), null);
+               }
+               if (phpRuntime != null) {
+                       anInterpreter.setInstallLocation(phpRuntime);
                        tableViewer.update(anInterpreter, null);
+               }
+
        }
-       
+
        protected PHPInterpreter getSelectedInterpreter() {
-               IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
+               IStructuredSelection selection = (IStructuredSelection) tableViewer
+                               .getSelection();
                return (PHPInterpreter) selection.getFirstElement();
        }
-       
+
        public boolean performOk() {
                TableItem[] tableItems = tableViewer.getTable().getItems();
                List installedInterpreters = new ArrayList(tableItems.length);
@@ -204,9 +228,38 @@ public class PHPInterpreterPreferencePage extends PreferencePage implements IWor
 
                Object[] checkedElements = tableViewer.getCheckedElements();
                if (checkedElements.length > 0)
-                       PHPRuntime.getDefault().setSelectedInterpreter((PHPInterpreter) checkedElements[0]);
+                       PHPRuntime.getDefault().setSelectedInterpreter(
+                                       (PHPInterpreter) checkedElements[0]);
 
                return super.performOk();
        }
 
+       /**
+        * Helper to open the file chooser dialog.
+        * 
+        * @param startingDirectory
+        *            the directory to open the dialog on.
+        * @return File The File the user selected or <code>null</code> if they do
+        *         not.
+        */
+       public static File getFile(Shell shell, File startingDirectory) {
+
+               FileDialog dialog = new FileDialog(shell, SWT.OPEN);
+               if (startingDirectory != null) {
+                       dialog.setFileName(startingDirectory.getPath());
+               }
+               String operatingSystem = Platform.getOS();
+               if (operatingSystem.equals(Platform.OS_WIN32)) {
+                       String[] extensions = { "*.exe" };
+                       dialog.setFilterExtensions(extensions);
+               }
+               String file = dialog.open();
+               if (file != null) {
+                       file = file.trim();
+                       if (file.length() > 0)
+                               return new File(file);
+               }
+
+               return null;
+       }
 }
\ No newline at end of file