OSDN Git Service

Fix for issue 14189.
authorPierre Zurek <pierrezurek@gmail.com>
Sun, 23 Jan 2011 22:15:51 +0000 (23:15 +0100)
committerPierre Zurek <pierrezurek@gmail.com>
Sat, 29 Jan 2011 13:37:56 +0000 (14:37 +0100)
Now the user can set if he wants to change perspective
when he double-clicks in the LogCat view.
If he wants to change perspective, he can set which
perspective shall be opened.

Change-Id: I3ed68f3f8ad3bbfcd6d161b63a0c0f218dd778b6

eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/preferences/LogCatPreferencePage.java
eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/preferences/PreferenceInitializer.java
eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/views/LogCatView.java

index 6bcec04..626f850 100644 (file)
@@ -19,6 +19,7 @@ package com.android.ide.eclipse.ddms.preferences;
 import com.android.ide.eclipse.ddms.DdmsPlugin;
 import com.android.ide.eclipse.ddms.views.LogCatView;
 
+import org.eclipse.jface.preference.BooleanFieldEditor;
 import org.eclipse.jface.preference.ComboFieldEditor;
 import org.eclipse.jface.preference.FieldEditorPreferencePage;
 import org.eclipse.jface.preference.FontFieldEditor;
@@ -27,8 +28,10 @@ import org.eclipse.jface.util.PropertyChangeEvent;
 import org.eclipse.swt.SWTError;
 import org.eclipse.swt.graphics.Font;
 import org.eclipse.swt.graphics.FontData;
+import org.eclipse.ui.IPerspectiveDescriptor;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchPreferencePage;
+import org.eclipse.ui.PlatformUI;
 
 /**
  * Preference Pane for LogCat.
@@ -36,6 +39,9 @@ import org.eclipse.ui.IWorkbenchPreferencePage;
 public class LogCatPreferencePage extends FieldEditorPreferencePage implements
         IWorkbenchPreferencePage {
 
+    private BooleanFieldEditor mSwitchPerspective;
+    private ComboFieldEditor mWhichPerspective;
+
     public LogCatPreferencePage() {
         super(GRID);
         setPreferenceStore(DdmsPlugin.getDefault().getPreferenceStore());
@@ -73,8 +79,45 @@ public class LogCatPreferencePage extends FieldEditorPreferencePage implements
                     { "Go to Problem (error line)", LogCatView.CHOICE_ERROR_LINE },
                 }, getFieldEditorParent());
         addField(cfe);
+
+        mSwitchPerspective = new BooleanFieldEditor(PreferenceInitializer.ATTR_SWITCH_PERSPECTIVE,
+                "Switch Perspective", getFieldEditorParent());
+        addField(mSwitchPerspective);
+
+        IPerspectiveDescriptor[] perspectiveDescriptors =
+                PlatformUI.getWorkbench().getPerspectiveRegistry().getPerspectives();
+        String[][] perspectives;
+        if (perspectiveDescriptors.length > 0) {
+            perspectives = new String[perspectiveDescriptors.length][2];
+            for (int i = 0; i < perspectiveDescriptors.length; i++) {
+                IPerspectiveDescriptor perspective = perspectiveDescriptors[i];
+                perspectives[i][0] = perspective.getLabel();
+                perspectives[i][1] = perspective.getId();
+            }
+        } else {
+            perspectives = new String[0][0];
+        }
+        mWhichPerspective = new ComboFieldEditor(PreferenceInitializer.ATTR_PERSPECTIVE_ID,
+                "Switch to:", perspectives, getFieldEditorParent());
+        mWhichPerspective.setEnabled(getPreferenceStore()
+                .getBoolean(PreferenceInitializer.ATTR_SWITCH_PERSPECTIVE), getFieldEditorParent());
+        addField(mWhichPerspective);
     }
 
     public void init(IWorkbench workbench) {
     }
+
+    @Override
+    public void propertyChange(PropertyChangeEvent event) {
+        if (event.getSource().equals(mSwitchPerspective)) {
+            mWhichPerspective.setEnabled(mSwitchPerspective.getBooleanValue()
+                    , getFieldEditorParent());
+        }
+    }
+
+    @Override
+    protected void performDefaults() {
+        super.performDefaults();
+        mWhichPerspective.setEnabled(mSwitchPerspective.getBooleanValue(), getFieldEditorParent());
+    }
 }
index 4e47365..bd8d816 100644 (file)
@@ -74,6 +74,12 @@ public class PreferenceInitializer extends AbstractPreferenceInitializer {
     public final static String ATTR_ADBHOST_VALUE =
         DdmsPlugin.PLUGIN_ID + ".adbHostValue"; //$NON-NLS-1$
 
+    public final static String ATTR_SWITCH_PERSPECTIVE =
+        DdmsPlugin.PLUGIN_ID + ".switchPerspective"; //$NON-NLS-1$
+
+    public final static String ATTR_PERSPECTIVE_ID =
+        DdmsPlugin.PLUGIN_ID + ".perspectiveId"; //$NON-NLS-1$
+
     /*
      * (non-Javadoc)
      *
@@ -109,6 +115,8 @@ public class PreferenceInitializer extends AbstractPreferenceInitializer {
         store.setDefault(ATTR_LOGCAT_GOTO_PROBLEM, LogCatView.CHOICE_ERROR_LINE);
         store.setDefault(ATTR_USE_ADBHOST, DdmPreferences.DEFAULT_USE_ADBHOST);
         store.setDefault(ATTR_ADBHOST_VALUE, DdmPreferences.DEFAULT_ADBHOST_VALUE);
+        store.setDefault(ATTR_SWITCH_PERSPECTIVE, LogCatView.DEFAULT_SWITCH_PERSPECTIVE);
+        store.setDefault(ATTR_PERSPECTIVE_ID, LogCatView.DEFAULT_PERSPECTIVE_ID);
     }
 
     /**
index 7cb9b3c..0a9fc33 100644 (file)
@@ -52,12 +52,13 @@ import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.IActionBars;
 import org.eclipse.ui.IPerspectiveRegistry;
+import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.WorkbenchException;
 import org.eclipse.ui.actions.ActionFactory;
 import org.eclipse.ui.ide.IDE;
-import org.eclipse.ui.internal.WorkbenchPlugin;
-import org.eclipse.ui.internal.registry.PerspectiveDescriptor;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -92,6 +93,11 @@ public final class LogCatView extends SelectionDependentViewPart implements LogC
     public static final String CHOICE_ERROR_LINE =
         DdmsPlugin.PLUGIN_ID + ".logcat.ErrorLine"; //$NON-NLS-1$
 
+    /* Default values for the switch of perspective. */
+    public static final boolean DEFAULT_SWITCH_PERSPECTIVE = true;
+    public static final String DEFAULT_PERSPECTIVE_ID =
+        "org.eclipse.jdt.ui.JavaPerspective"; //$NON-NLS-1$
+
     private static LogCatView sThis;
     private LogPanel mLogPanel;
 
@@ -435,17 +441,21 @@ public final class LogCatView extends SelectionDependentViewPart implements LogC
     }
 
     void switchPerspective() {
-
-        IWorkbenchWindow window = getViewSite().getWorkbenchWindow()
-                .getWorkbench().getActiveWorkbenchWindow();
-        String rtPerspectiveId = "org.eclipse.jdt.ui.JavaPerspective";
-        IPerspectiveRegistry reg = WorkbenchPlugin.getDefault()
-                .getPerspectiveRegistry();
-        PerspectiveDescriptor rtPerspectiveDesc = (PerspectiveDescriptor) reg
-                .findPerspectiveWithId(rtPerspectiveId);
-        if (window != null) {
-            IWorkbenchPage page = window.getActivePage();
-            page.setPerspective(rtPerspectiveDesc);
+        IPreferenceStore store = DdmsPlugin.getDefault().getPreferenceStore();
+        if (store.getBoolean(PreferenceInitializer.ATTR_SWITCH_PERSPECTIVE)) {
+            IWorkbench workbench = PlatformUI.getWorkbench();
+            IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
+            IPerspectiveRegistry perspectiveRegistry = workbench.getPerspectiveRegistry();
+            String perspectiveId = store.getString(PreferenceInitializer.ATTR_PERSPECTIVE_ID);
+            if (perspectiveId != null
+                    && perspectiveId.length() > 0
+                    && perspectiveRegistry.findPerspectiveWithId(perspectiveId) != null) {
+                try {
+                    workbench.showPerspective(perspectiveId, window);
+                } catch (WorkbenchException e) {
+                    e.printStackTrace();
+                }
+            }
         }
     }