Author: snjeza
Date: 2010-09-15 16:38:58 -0400 (Wed, 15 Sep 2010)
New Revision: 24951
Modified:
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/model/Project.java
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/model/ProjectUtil.java
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/wizard/NewProjectExamplesWizard.java
Log:
JBIDE-7073 Project Examples Should Provide The Option To Switch To A Specified
Perspective
Modified:
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/model/Project.java
===================================================================
---
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/model/Project.java 2010-09-15
16:54:36 UTC (rev 24950)
+++
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/model/Project.java 2010-09-15
20:38:58 UTC (rev 24951)
@@ -33,14 +33,15 @@
private String site;
private List<ProjectFix> fixes = new ArrayList<ProjectFix>();
private List<ProjectFix> unsatisfiedFixes;
+ private String perspectiveId;
-
public Project() {
name=""; //$NON-NLS-1$
shortDescription=""; //$NON-NLS-1$
description=""; //$NON-NLS-1$
url=""; //$NON-NLS-1$
welcome=false;
+ perspectiveId = null;
setCategory(Category.OTHER);
}
@@ -171,4 +172,12 @@
public void setUnsatisfiedFixes(List<ProjectFix> unsatisfiedFixes) {
this.unsatisfiedFixes = unsatisfiedFixes;
}
+
+ public String getPerspectiveId() {
+ return perspectiveId;
+ }
+
+ public void setPerspectiveId(String perspectiveId) {
+ this.perspectiveId = perspectiveId;
+ }
}
Modified:
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/model/ProjectUtil.java
===================================================================
---
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/model/ProjectUtil.java 2010-09-15
16:54:36 UTC (rev 24950)
+++
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/model/ProjectUtil.java 2010-09-15
20:38:58 UTC (rev 24951)
@@ -282,6 +282,9 @@
if (nodeName.equals(URL)) {
project.setUrl(getContent(child));
}
+ if (nodeName.equals("perspectiveId")) { //$NON-NLS-1$
+ project.setPerspectiveId(getContent(child));
+ }
if (nodeName.equals("size")) { //$NON-NLS-1$
long size = 0;
try {
Modified:
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/wizard/NewProjectExamplesWizard.java
===================================================================
---
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/wizard/NewProjectExamplesWizard.java 2010-09-15
16:54:36 UTC (rev 24950)
+++
trunk/examples/plugins/org.jboss.tools.project.examples/src/org/jboss/tools/project/examples/wizard/NewProjectExamplesWizard.java 2010-09-15
20:38:58 UTC (rev 24951)
@@ -21,8 +21,10 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
+import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
@@ -49,8 +51,11 @@
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jdt.internal.core.OpenableElementInfo;
import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.jface.dialogs.ErrorDialog;
+import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.MessageDialogWithToggle;
+import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
@@ -58,19 +63,36 @@
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.INewWizard;
+import org.eclipse.ui.IPerspectiveDescriptor;
+import org.eclipse.ui.IPerspectiveRegistry;
+import org.eclipse.ui.IPluginContribution;
import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchPreferenceConstants;
+import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.WorkbenchException;
+import org.eclipse.ui.activities.IActivityManager;
+import org.eclipse.ui.activities.IIdentifier;
+import org.eclipse.ui.activities.IWorkbenchActivitySupport;
+import org.eclipse.ui.activities.WorkbenchActivityHelper;
import org.eclipse.ui.browser.IWebBrowser;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
import org.eclipse.ui.dialogs.IOverwriteQuery;
+import org.eclipse.ui.ide.IDE;
+import org.eclipse.ui.internal.IPreferenceConstants;
+import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.internal.cheatsheets.state.DefaultStateManager;
import org.eclipse.ui.internal.cheatsheets.views.CheatSheetView;
import org.eclipse.ui.internal.cheatsheets.views.ViewUtilities;
+import org.eclipse.ui.internal.ide.IDEInternalPreferences;
+import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
+import org.eclipse.ui.internal.util.PrefUtil;
import org.eclipse.ui.internal.wizards.datatransfer.ZipLeveledStructureProvider;
+import org.eclipse.ui.internal.wizards.newresource.ResourceMessages;
import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider;
import org.eclipse.ui.wizards.datatransfer.ImportOperation;
-import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider;
import org.jboss.tools.project.examples.Messages;
import org.jboss.tools.project.examples.ProjectExamplesActivator;
import org.jboss.tools.project.examples.dialog.MarkerDialog;
@@ -196,6 +218,7 @@
public void done(IJobChangeEvent event) {
try {
+ updatePerspective();
ProjectExamplesActivator.waitForBuildAndValidation
.schedule();
ProjectExamplesActivator.waitForBuildAndValidation
@@ -238,6 +261,203 @@
return true;
}
+ protected void updatePerspective() {
+ if (projects == null || projects.size() != 1) {
+ return;
+ }
+ final String perspectiveId = projects.get(0).getPerspectiveId();
+ if (perspectiveId == null || perspectiveId.length() <= 0) {
+ return;
+ }
+ // Retrieve the new project open perspective preference setting
+ String perspSetting = PrefUtil.getAPIPreferenceStore().getString(
+ IDE.Preferences.PROJECT_OPEN_NEW_PERSPECTIVE);
+
+ String promptSetting = IDEWorkbenchPlugin.getDefault()
+ .getPreferenceStore().getString(
+ IDEInternalPreferences.PROJECT_SWITCH_PERSP_MODE);
+
+ // Return if do not switch perspective setting and are not prompting
+ if (!(promptSetting.equals(MessageDialogWithToggle.PROMPT))
+ && perspSetting.equals(IWorkbenchPreferenceConstants.NO_NEW_PERSPECTIVE)) {
+ return;
+ }
+
+ // Map perspective id to descriptor.
+ IPerspectiveRegistry reg = PlatformUI.getWorkbench()
+ .getPerspectiveRegistry();
+
+ // leave this code in - the perspective of a given project may map to
+ // activities other than those that the wizard itself maps to.
+ final IPerspectiveDescriptor finalPersp = reg
+ .findPerspectiveWithId(perspectiveId);
+ if (finalPersp != null && finalPersp instanceof IPluginContribution) {
+ IPluginContribution contribution = (IPluginContribution) finalPersp;
+ if (contribution.getPluginId() != null) {
+ IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI
+ .getWorkbench().getActivitySupport();
+ IActivityManager activityManager = workbenchActivitySupport
+ .getActivityManager();
+ IIdentifier identifier = activityManager
+ .getIdentifier(WorkbenchActivityHelper
+ .createUnifiedId(contribution));
+ Set idActivities = identifier.getActivityIds();
+
+ if (!idActivities.isEmpty()) {
+ Set enabledIds = new HashSet(activityManager
+ .getEnabledActivityIds());
+
+ if (enabledIds.addAll(idActivities)) {
+ workbenchActivitySupport
+ .setEnabledActivityIds(enabledIds);
+ }
+ }
+ }
+ } else {
+ IDEWorkbenchPlugin.log("Unable to find perspective " //$NON-NLS-1$
+ + perspectiveId
+ + " in NewProjectExamplesWizard.updatePerspective"); //$NON-NLS-1$
+ return;
+ }
+ IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ if (window == null) {
+ Display.getDefault().syncExec(new Runnable() {
+
+ @Override
+ public void run() {
+ IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ switchPerspective(perspectiveId, finalPersp, win);
+ }
+ });
+ } else {
+ switchPerspective(perspectiveId, finalPersp, window);
+ }
+ }
+
+ private void switchPerspective(String perspectiveId,
+ IPerspectiveDescriptor finalPersp, IWorkbenchWindow window) {
+ if (window != null) {
+ IWorkbenchPage page = window.getActivePage();
+ if (page != null) {
+ IPerspectiveDescriptor currentPersp = page.getPerspective();
+ if (currentPersp != null
+ && perspectiveId.equals(currentPersp.getId())) {
+ return;
+ }
+ }
+ }
+
+ if (!confirmPerspectiveSwitch(window, finalPersp)) {
+ return;
+ }
+
+ int workbenchPerspectiveSetting =
WorkbenchPlugin.getDefault().getPreferenceStore().getInt(IPreferenceConstants.OPEN_PERSP_MODE);
+
+ if (workbenchPerspectiveSetting == IPreferenceConstants.OPM_NEW_WINDOW) {
+ openInNewWindow(finalPersp);
+ return;
+ }
+
+ replaceCurrentPerspective(finalPersp);
+ }
+
+ /**
+ * Prompts the user for whether to switch perspectives.
+ *
+ * @param window
+ * The workbench window in which to switch perspectives; must not
+ * be <code>null</code>
+ * @param finalPersp
+ * The perspective to switch to; must not be <code>null</code>.
+ *
+ * @return <code>true</code> if it's OK to switch,
<code>false</code>
+ * otherwise
+ */
+ private static boolean confirmPerspectiveSwitch(IWorkbenchWindow window,
+ IPerspectiveDescriptor finalPersp) {
+ IPreferenceStore store = IDEWorkbenchPlugin.getDefault()
+ .getPreferenceStore();
+ String pspm = store
+ .getString(IDEInternalPreferences.PROJECT_SWITCH_PERSP_MODE);
+ if (!IDEInternalPreferences.PSPM_PROMPT.equals(pspm)) {
+ // Return whether or not we should always switch
+ return IDEInternalPreferences.PSPM_ALWAYS.equals(pspm);
+ }
+ String desc = finalPersp.getDescription();
+ String message;
+ if (desc == null || desc.length() == 0)
+ message = NLS.bind(ResourceMessages.NewProject_perspSwitchMessage,
+ finalPersp.getLabel());
+ else
+ message = NLS.bind(
+ ResourceMessages.NewProject_perspSwitchMessageWithDesc,
+ new String[] { finalPersp.getLabel(), desc });
+
+ MessageDialogWithToggle dialog = MessageDialogWithToggle
+ .openYesNoQuestion(window.getShell(),
+ ResourceMessages.NewProject_perspSwitchTitle, message,
+ null /* use the default message for the toggle */,
+ false /* toggle is initially unchecked */, store,
+ IDEInternalPreferences.PROJECT_SWITCH_PERSP_MODE);
+ int result = dialog.getReturnCode();
+
+ // If we are not going to prompt anymore propogate the choice.
+ if (dialog.getToggleState()) {
+ String preferenceValue;
+ if (result == IDialogConstants.YES_ID) {
+ // Doesn't matter if it is replace or new window
+ // as we are going to use the open perspective setting
+ preferenceValue = IWorkbenchPreferenceConstants.OPEN_PERSPECTIVE_REPLACE;
+ } else {
+ preferenceValue = IWorkbenchPreferenceConstants.NO_NEW_PERSPECTIVE;
+ }
+
+ // update PROJECT_OPEN_NEW_PERSPECTIVE to correspond
+ PrefUtil.getAPIPreferenceStore().setValue(
+ IDE.Preferences.PROJECT_OPEN_NEW_PERSPECTIVE,
+ preferenceValue);
+ }
+ return result == IDialogConstants.YES_ID;
+ }
+
+ /*
+ * (non-Javadoc) Opens a new window with a particular perspective and input.
+ */
+ private static void openInNewWindow(IPerspectiveDescriptor desc) {
+
+ // Open the page.
+ try {
+ PlatformUI.getWorkbench().openWorkbenchWindow(desc.getId(),
+ ResourcesPlugin.getWorkspace().getRoot());
+ } catch (WorkbenchException e) {
+ IWorkbenchWindow window = PlatformUI.getWorkbench()
+ .getActiveWorkbenchWindow();
+ if (window != null) {
+ ErrorDialog.openError(window.getShell(),
ResourceMessages.NewProject_errorOpeningWindow,
+ e.getMessage(), e.getStatus());
+ }
+ }
+ }
+
+ /*
+ * (non-Javadoc) Replaces the current perspective with the new one.
+ */
+ private static void replaceCurrentPerspective(IPerspectiveDescriptor persp) {
+
+ // Get the active page.
+ IWorkbenchWindow window = PlatformUI.getWorkbench()
+ .getActiveWorkbenchWindow();
+ if (window == null) {
+ return;
+ }
+ IWorkbenchPage page = window.getActivePage();
+ if (page == null) {
+ return;
+ }
+
+ // Set the perspective.
+ page.setPerspective(persp);
+ }
private void openWelcome() {
for(final Project project:projects) {
if (project.isWelcome()) {
@@ -483,4 +703,5 @@
}
}
}
+
}