Skip to content

Commit

Permalink
511215: Make Favorites a central element
Browse files Browse the repository at this point in the history
Select imported favorites for installation

Bug: 511215
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=511215
  • Loading branch information
creckord committed Mar 31, 2017
1 parent 1502c88 commit 608a49a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 28 deletions.
Expand Up @@ -11,12 +11,14 @@
package org.eclipse.epp.internal.mpc.ui.wizards;

import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.epp.internal.mpc.ui.catalog.FavoritesCatalog;
import org.eclipse.epp.internal.mpc.ui.catalog.FavoritesDiscoveryStrategy;
import org.eclipse.epp.internal.mpc.ui.catalog.MarketplaceDiscoveryStrategy;
import org.eclipse.epp.internal.mpc.ui.wizards.MarketplaceViewer.ContentType;
import org.eclipse.epp.mpc.ui.Operation;
import org.eclipse.equinox.internal.p2.discovery.AbstractDiscoveryStrategy;
import org.eclipse.jface.window.Window;

Expand Down Expand Up @@ -84,11 +86,16 @@ protected void postDiscovery() {
});
ImportFavoritesWizardDialog importWizard = new ImportFavoritesWizardDialog(wizard.getShell(), importFavoritesWizard);

Map<String, Operation> oldOperations = wizard.getSelectionModel().getItemIdToSelectedOperation();
int result = importWizard.open();
if (result == Window.OK) {
MarketplacePage catalogPage = wizard.getCatalogPage();
catalogPage.setActiveTab(ContentType.FAVORITES);
catalogPage.reloadCatalog();
Map<String, Operation> newOperations = wizard.getSelectionModel().getItemIdToSelectedOperation();
if (!newOperations.equals(oldOperations)) {
wizard.getCatalogPage().setPageComplete(!newOperations.isEmpty());
}
}
}

Expand Down
Expand Up @@ -102,9 +102,7 @@ protected CatalogViewer doCreateViewer(Composite parent) {

public void performImport() {
setErrorMessage(null);
IStructuredSelection selection = getViewer().getSelection();
@SuppressWarnings("unchecked")
List<MarketplaceNodeCatalogItem> importFavorites = selection.toList();
List<MarketplaceNodeCatalogItem> importFavorites = getSelection();
if (importFavorites.isEmpty()) {
return;
}
Expand Down Expand Up @@ -149,6 +147,13 @@ public Void call() throws Exception {
}
}

public List<MarketplaceNodeCatalogItem> getSelection() {
IStructuredSelection selection = getViewer().getSelection();
@SuppressWarnings("unchecked")
List<MarketplaceNodeCatalogItem> importFavorites = selection.toList();
return new ArrayList<MarketplaceNodeCatalogItem>(importFavorites);
}

private IUserFavoritesService findUserFavoritesService() {
IUserFavoritesService userFavoritesService = null;
for (AbstractDiscoveryStrategy strategy : getCatalog().getDiscoveryStrategies()) {
Expand Down
Expand Up @@ -10,13 +10,17 @@
*******************************************************************************/
package org.eclipse.epp.internal.mpc.ui.wizards;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.epp.internal.mpc.ui.catalog.MarketplaceCatalog;
import org.eclipse.epp.internal.mpc.ui.catalog.MarketplaceNodeCatalogItem;
import org.eclipse.epp.mpc.ui.IMarketplaceClientConfiguration;
import org.eclipse.epp.mpc.ui.IMarketplaceClientService;
import org.eclipse.epp.mpc.ui.MarketplaceClient;
import org.eclipse.epp.mpc.ui.Operation;
import org.eclipse.equinox.internal.p2.ui.discovery.wizards.DiscoveryWizard;
import org.eclipse.jface.wizard.IWizardContainer;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ImportFavoritesWizard extends DiscoveryWizard {

Expand All @@ -34,6 +38,16 @@ public ImportFavoritesWizard(MarketplaceCatalog catalog, MarketplaceCatalogConfi
this.parent = parent;
}

@Override
public MarketplaceCatalogConfiguration getConfiguration() {
return (MarketplaceCatalogConfiguration) super.getConfiguration();
}

@Override
public MarketplaceCatalog getCatalog() {
return (MarketplaceCatalog) super.getCatalog();
}

@Override
public void addPages() {
addPage(importFavoritesPage);
Expand All @@ -50,23 +64,34 @@ public boolean performFinish() {
}

private void showFavoritesInMarketplace() {
Display display = null;
Rectangle bounds = null;
IWizardContainer container = getContainer();
if (container instanceof WizardDialog) {
WizardDialog dialog = (WizardDialog) container;
Shell shell = dialog.getShell();
if (shell != null && !shell.isDisposed()) {
bounds = shell.getBounds();
display = shell.getDisplay();
}
List<MarketplaceNodeCatalogItem> selection = importFavoritesPage.getSelection();
if (selection.isEmpty()) {
return;
}
if (parent == null) {
openFavoritesInMarketplace(selection);
} else {
selectForInstallation(selection);
}
showFavoritesInMarketplace(display, bounds);
}

private void showFavoritesInMarketplace(Display display, Rectangle bounds) {
// ignore
private void selectForInstallation(List<MarketplaceNodeCatalogItem> selection) {
for (MarketplaceNodeCatalogItem item : selection) {
parent.getSelectionModel().select(item, Operation.INSTALL);
}
}

private void openFavoritesInMarketplace(List<MarketplaceNodeCatalogItem> selection) {
IMarketplaceClientService clientService = MarketplaceClient.getMarketplaceClientService();
IMarketplaceClientConfiguration config = clientService.newConfiguration();
MarketplaceCatalogConfiguration catalogConfiguration = getConfiguration();
config.setCatalogDescriptors(catalogConfiguration.getCatalogDescriptors());
config.setCatalogDescriptor(catalogConfiguration.getCatalogDescriptor());
Map<String, Operation> initialOperations = new HashMap<String, Operation>();
for (MarketplaceNodeCatalogItem item : selection) {
initialOperations.put(item.getData().getId(), Operation.INSTALL);
}
clientService.openFavorites(config);
}

public ImportFavoritesPage getImportFavoritesPage() {
Expand Down
Expand Up @@ -44,8 +44,7 @@ protected void installAll() {
for (CatalogItem catalogItem : items) {
if (catalogItem instanceof MarketplaceNodeCatalogItem) {
MarketplaceNodeCatalogItem nodeItem = (MarketplaceNodeCatalogItem) catalogItem;
if (selectionModel.getSelectedOperation(nodeItem) != Operation.INSTALL
&& nodeItem.getAvailableOperations().contains(Operation.INSTALL)) {
if (selectionModel.getSelectedOperation(nodeItem) == null) {
selectionModel.select(nodeItem, Operation.INSTALL);
}
}
Expand Down
Expand Up @@ -72,9 +72,29 @@ public void select(CatalogItem item, org.eclipse.epp.internal.mpc.ui.wizards.Ope
* @param operation
* the operation to perform. Providing {@link Operation#NONE} removes the selection
*/
public void select(CatalogItem item, Operation operation) {
public boolean select(CatalogItem item, Operation operation) {
boolean changed = false;
if (operation == null || Operation.NONE == operation) {
Operation sanitizedOperation = operation;
if (operation != null && Operation.NONE != operation && item instanceof MarketplaceNodeCatalogItem) {
MarketplaceNodeCatalogItem nodeItem = (MarketplaceNodeCatalogItem) item;
List<Operation> availableOperations = nodeItem.getAvailableOperations();
if (!availableOperations.contains(operation)) {
sanitizedOperation = null;
switch (operation) {
case INSTALL:
if (availableOperations.contains(Operation.UPDATE)) {
sanitizedOperation = Operation.UPDATE;
}
break;
case UPDATE:
if (availableOperations.contains(Operation.INSTALL)) {
sanitizedOperation = Operation.UPDATE;
}
break;
}
}
}
if (sanitizedOperation == null || Operation.NONE == sanitizedOperation) {
if (itemToOperation.remove(item) != Operation.NONE) {
changed = true;
}
Expand All @@ -88,8 +108,8 @@ public void select(CatalogItem item, Operation operation) {
}
}
} else {
Operation previous = itemToOperation.put(item, operation);
if (previous != operation) {
Operation previous = itemToOperation.put(item, sanitizedOperation);
if (previous != sanitizedOperation) {
changed = true;
if (entries != null) {
Iterator<CatalogItemEntry> it = entries.iterator();
Expand All @@ -99,14 +119,15 @@ public void select(CatalogItem item, Operation operation) {
it.remove();
}
}
CatalogItemEntry itemEntry = createItemEntry(item, operation);
CatalogItemEntry itemEntry = createItemEntry(item, sanitizedOperation);
entries.add(itemEntry);
}
}
}
if (changed) {
selectionChanged();
}
return changed;
}

public List<CatalogItemEntry> getCatalogItemEntries() {
Expand Down Expand Up @@ -425,6 +446,15 @@ public Map<CatalogItem, Operation> getItemToSelectedOperation() {
return Collections.unmodifiableMap(itemToOperation);
}

public Map<String, Operation> getItemIdToSelectedOperation() {
Map<CatalogItem, Operation> itemToSelectedOperation = getItemToSelectedOperation();
Map<String, Operation> itemIdToOperation = new HashMap<String, Operation>(itemToSelectedOperation.size());
for (Entry<CatalogItem, Operation> entry : itemToSelectedOperation.entrySet()) {
itemIdToOperation.put(entry.getKey().getId(), entry.getValue());
}
return itemIdToOperation;
}

public void selectionChanged() {
// ignore

Expand Down

0 comments on commit 608a49a

Please sign in to comment.