Skip to content

Commit

Permalink
[JBIDE-21352] server adapter wiz: preselecting project select in proj…
Browse files Browse the repository at this point in the history
…ect views

We now preselect the (1st) project that's selected in (using this order of prescedence)
* package explorer
* project explorer
* resource navigator
  • Loading branch information
adietish committed Dec 17, 2015
1 parent d8305d0 commit 26a1d1e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 35 deletions.
@@ -0,0 +1,40 @@
/*******************************************************************************
* Copyright (c) 2011 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package org.jboss.tools.openshift.internal.common.ui.utils;

import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.jboss.tools.openshift.internal.common.ui.OpenShiftCommonUIActivator;

/**
* @author André Dietisheim
*/
public class OpenShiftUIUtils {

private static final String OPENSHIFT_EXPLORER_VIEW_ID = "org.jboss.tools.openshift.express.ui.explorer.expressConsoleView";

public static void showOpenShiftExplorerView() {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
try {
PlatformUI.getWorkbench()
.getActiveWorkbenchWindow()
.getActivePage()
.showView(OPENSHIFT_EXPLORER_VIEW_ID);
} catch (PartInitException e) {
OpenShiftCommonUIActivator.getDefault().getLogger().logError("Failed to show the OpenShift Explorer view", e);
}
}
});
}
}
Expand Up @@ -13,6 +13,7 @@
import java.lang.reflect.Array;
import java.util.ArrayList;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
Expand Down Expand Up @@ -44,33 +45,17 @@
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.menus.IMenuService;
import org.jboss.tools.openshift.internal.common.ui.OpenShiftCommonUIActivator;

/**
* @author André Dietisheim
*/
public class UIUtils {

private static final String OPENSHIFT_EXPLORER_VIEW_ID = "org.jboss.tools.openshift.express.ui.explorer.expressConsoleView";

public static void showOpenShiftExplorerView() {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
try {
PlatformUI.getWorkbench()
.getActiveWorkbenchWindow()
.getActivePage()
.showView(OPENSHIFT_EXPLORER_VIEW_ID);
} catch (PartInitException e) {
OpenShiftCommonUIActivator.getDefault().getLogger().logError("Failed to show the OpenShift Explorer view", e);
}
}
});
}
public static final String PACKAGE_EXPLORER_ID = "org.eclipse.jdt.ui.PackageExplorer";
public static final String PROJECT_EXPLORER_ID = "org.eclipse.ui.navigator.ProjectExplorer";
public static final String RESOURCE_NAVIGATOR_ID = "org.eclipse.ui.views.ResourceNavigator";

public static void selectAllOnFocus(final Text text) {
final FocusListener onFocus = new FocusAdapter() {
Expand Down Expand Up @@ -280,6 +265,41 @@ public static boolean areNumOfElementsSelected(int numOf, ISelection selection)
public static boolean isSingleSelection(ISelection selection) {
return areNumOfElementsSelected(1, selection);
}

/**
* Returns the first selected element that's of the given type, in the view identified by the given id.
* Returns {@code null} if the view is not opened, doesn't exist or the selected element is not of the requested type.
*
* @param viewId the id of the view whose selection we'll look at
* @param clazz the type of selected item that you get
* @return the selected item of the given type in the given view.
*/
public static <T> T getFirstSelectedElement(String viewId, Class<T> clazz) {
IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
ISelection selection = workbenchWindow.getSelectionService().getSelection(viewId);
return getFirstElement(selection, clazz);
}

/**
* Returns the 1st project that's selected in the following views (in a fallback order):
* <ul>
* <li>package explorer</li>
* <li>project explorer</li>
* <li>resource navigator</li>
* </ul>
* @return the project selected in package-, project-explorer or navigator (in this order of prescedence).
*/
public static IProject getFirstSelectedProject() {
IProject project = getFirstSelectedElement(PACKAGE_EXPLORER_ID, IProject.class);
if (project == null) {
project = getFirstSelectedElement(PROJECT_EXPLORER_ID, IProject.class);
if (project == null) {
project = getFirstSelectedElement(RESOURCE_NAVIGATOR_ID, IProject.class);
}
}
return project;

}

public static void copyBackground(Control source, Control destination) {
destination.setBackground(source.getBackground());
Expand Down
Expand Up @@ -42,14 +42,12 @@
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.jface.wizard.IWizardContainer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
Expand All @@ -64,8 +62,6 @@
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wst.server.core.IServerWorkingCopy;
import org.eclipse.wst.server.ui.wizard.IWizardHandle;
import org.jboss.ide.eclipse.as.ui.editor.DeploymentTypeUIUtil.ICompletable;
Expand All @@ -76,8 +72,8 @@
import org.jboss.tools.openshift.internal.common.ui.OpenShiftCommonImages;
import org.jboss.tools.openshift.internal.common.ui.SelectExistingProjectDialog;
import org.jboss.tools.openshift.internal.common.ui.databinding.FormPresenterSupport;
import org.jboss.tools.openshift.internal.common.ui.databinding.RequiredControlDecorationUpdater;
import org.jboss.tools.openshift.internal.common.ui.databinding.FormPresenterSupport.IFormPresenter;
import org.jboss.tools.openshift.internal.common.ui.databinding.RequiredControlDecorationUpdater;
import org.jboss.tools.openshift.internal.common.ui.utils.UIUtils;
import org.jboss.tools.openshift.internal.ui.treeitem.Model2ObservableTreeItemConverter;
import org.jboss.tools.openshift.internal.ui.treeitem.ObservableTreeItem;
Expand Down Expand Up @@ -118,7 +114,7 @@ public Composite createComposite(Composite parent, IWizardHandle handle) {

@Override
public void setMessage(String message, int type) {
((WizardPage) handle).setMessage(message, type);
WizardFragmentUtils.getWizardPage(handle).setMessage(message, type);
}

@Override
Expand All @@ -137,7 +133,7 @@ public Control getControl() {

loadResources(model, WizardFragmentUtils.getWizardPage(handle).getWizard().getContainer());

IProject selectedProject = getSelectedWorkbenchProject();
IProject selectedProject = UIUtils.getFirstSelectedProject();
if (selectedProject != null) {
model.setDeployProject(selectedProject);
}
Expand Down Expand Up @@ -510,12 +506,6 @@ protected IStatus run(IProgressMonitor monitor) {
}
}

private IProject getSelectedWorkbenchProject() {
IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
ISelection selection = win.getSelectionService().getSelection();
return UIUtils.getFirstElement(selection, IProject.class);
}

@Override
public void performFinish(IProgressMonitor monitor) throws CoreException {
super.performFinish(monitor);
Expand Down
Expand Up @@ -22,7 +22,7 @@
import org.jboss.tools.openshift.common.ui.wizard.AbstractOpenShiftWizard;
import org.jboss.tools.openshift.internal.common.core.UsageStats;
import org.jboss.tools.openshift.internal.common.core.job.JobChainBuilder;
import org.jboss.tools.openshift.internal.common.ui.utils.UIUtils;
import org.jboss.tools.openshift.internal.common.ui.utils.OpenShiftUIUtils;
import org.jboss.tools.openshift.internal.ui.OpenShiftUIActivator;
import org.jboss.tools.openshift.internal.ui.dialog.ResourceSummaryDialog;
import org.jboss.tools.openshift.internal.ui.job.DeployImageJob;
Expand Down Expand Up @@ -75,7 +75,7 @@ public void run() {
message).open();
}
});
UIUtils.showOpenShiftExplorerView();
OpenShiftUIUtils.showOpenShiftExplorerView();
}
}
});
Expand Down
Expand Up @@ -33,6 +33,7 @@
import org.jboss.tools.openshift.core.connection.ConnectionsRegistryUtil;
import org.jboss.tools.openshift.internal.common.core.UsageStats;
import org.jboss.tools.openshift.internal.common.core.job.JobChainBuilder;
import org.jboss.tools.openshift.internal.common.ui.utils.OpenShiftUIUtils;
import org.jboss.tools.openshift.internal.common.ui.utils.UIUtils;
import org.jboss.tools.openshift.internal.common.ui.wizard.IConnectionAwareWizard;
import org.jboss.tools.openshift.internal.ui.OpenShiftUIActivator;
Expand Down Expand Up @@ -121,7 +122,7 @@ public void run() {
new NewApplicationSummaryDialog(getShell(), createJob, message).open();
}
});
UIUtils.showOpenShiftExplorerView();
OpenShiftUIUtils.showOpenShiftExplorerView();
if (model.getEclipseProject() != null) {
//No need to import the project from git, it's already here
return;
Expand Down

0 comments on commit 26a1d1e

Please sign in to comment.