core/com.nokia.carbide.discovery.ui/src/com/nokia/carbide/internal/discovery/ui/extension/AbstractDiscoveryPortalPageLayer.java
changeset 1734 bf670d38123a
child 1793 b3fa776e81bd
equal deleted inserted replaced
1729:f77f2721ad5a 1734:bf670d38123a
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 package com.nokia.carbide.internal.discovery.ui.extension;
       
    18 
       
    19 import java.net.URI;
       
    20 import java.net.URISyntaxException;
       
    21 import java.text.MessageFormat;
       
    22 import java.util.ArrayList;
       
    23 import java.util.Collection;
       
    24 import java.util.HashSet;
       
    25 import java.util.List;
       
    26 import java.util.Set;
       
    27 
       
    28 import org.eclipse.equinox.internal.p2.discovery.Catalog;
       
    29 import org.eclipse.equinox.internal.p2.discovery.DiscoveryCore;
       
    30 import org.eclipse.equinox.internal.p2.discovery.compatibility.BundleDiscoveryStrategy;
       
    31 import org.eclipse.equinox.internal.p2.discovery.compatibility.RemoteBundleDiscoveryStrategy;
       
    32 import org.eclipse.equinox.internal.p2.discovery.model.CatalogItem;
       
    33 import org.eclipse.equinox.internal.p2.ui.discovery.DiscoveryUi;
       
    34 import org.eclipse.equinox.internal.p2.ui.discovery.wizards.CatalogConfiguration;
       
    35 import org.eclipse.equinox.internal.p2.ui.discovery.wizards.CatalogViewer;
       
    36 import org.eclipse.equinox.p2.core.IProvisioningAgent;
       
    37 import org.eclipse.equinox.p2.operations.ProvisioningSession;
       
    38 import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
       
    39 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
       
    40 import org.eclipse.equinox.p2.ui.ProvisioningUI;
       
    41 import org.eclipse.jface.action.Action;
       
    42 import org.eclipse.jface.action.IAction;
       
    43 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
       
    44 import org.eclipse.jface.layout.GridDataFactory;
       
    45 import org.eclipse.jface.layout.GridLayoutFactory;
       
    46 import org.eclipse.jface.viewers.ISelectionChangedListener;
       
    47 import org.eclipse.jface.viewers.IStructuredSelection;
       
    48 import org.eclipse.jface.viewers.SelectionChangedEvent;
       
    49 import org.eclipse.jface.viewers.StructuredSelection;
       
    50 import org.eclipse.swt.SWT;
       
    51 import org.eclipse.swt.widgets.Composite;
       
    52 import org.eclipse.swt.widgets.Control;
       
    53 import org.eclipse.swt.widgets.Display;
       
    54 import org.eclipse.swt.widgets.Shell;
       
    55 import org.eclipse.ui.IActionBars;
       
    56 import org.eclipse.ui.IEditorPart;
       
    57 import org.eclipse.ui.actions.BaseSelectionListenerAction;
       
    58 
       
    59 import com.nokia.carbide.discovery.ui.Activator;
       
    60 import com.nokia.carbide.discovery.ui.Messages;
       
    61 import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils;
       
    62 
       
    63 @SuppressWarnings("restriction")
       
    64 public abstract class AbstractDiscoveryPortalPageLayer implements IPortalPageLayer {
       
    65 
       
    66 	private class RunnableContextDialog extends ProgressMonitorDialog {
       
    67 		private final String title;
       
    68 
       
    69 		private RunnableContextDialog(Shell parent, String title) {
       
    70 			super(parent);
       
    71 			this.title = title;
       
    72 		}
       
    73 
       
    74 		@Override
       
    75 		protected void configureShell(Shell shell) {
       
    76 			super.configureShell(shell);
       
    77 			shell.setText(title);
       
    78 		}
       
    79 		
       
    80 	}
       
    81 
       
    82 	protected class ActionBar implements IActionBar {
       
    83 		private IAction[] actions;
       
    84 
       
    85 		public ActionBar(IEditorPart part) {
       
    86 			actions = makeActions(part);
       
    87 		}
       
    88 		
       
    89 		@Override
       
    90 		public String getTitle() {
       
    91 			return Messages.AbstractDiscoveryPortalPageLayer_Title;
       
    92 		}
       
    93 
       
    94 		@Override
       
    95 		public IAction[] getActions() {
       
    96 			return actions;
       
    97 		}
       
    98 
       
    99 		@Override
       
   100 		public String[] getHighlightedActionIds() {
       
   101 			return new String[] {INSTALL_ACTION_ID};
       
   102 		}
       
   103 	}
       
   104 	
       
   105 	protected static final String INSTALL_ACTION_ID = 
       
   106 		AbstractDiscoveryPortalPageLayer.class.getName() + ".install"; //$NON-NLS-1$
       
   107 	protected static final String UNCHECK_ALL_ACTION_ID = 
       
   108 		AbstractDiscoveryPortalPageLayer.class.getName() + ".uncheckAll"; //$NON-NLS-1$
       
   109 	protected static final String CHECK_ALL_ACTION_ID = 
       
   110 		AbstractDiscoveryPortalPageLayer.class.getName() + ".checkAll"; //$NON-NLS-1$
       
   111 	protected static final String ADV_INSTALL_ACTION_ID = 
       
   112 		AbstractDiscoveryPortalPageLayer.class.getName() + ".advancedInstall"; //$NON-NLS-1$
       
   113 	protected static final String REFRESH_ACTION_ID = 
       
   114 		AbstractDiscoveryPortalPageLayer.class.getName() + ".refresh"; //$NON-NLS-1$
       
   115 
       
   116 	private CatalogViewer viewer;
       
   117 	private List<ISelectionChangedListener> selectionListeners;
       
   118 	private IActionUIUpdater updater;
       
   119 
       
   120 	@Override
       
   121 	public Control createControl(Composite parent, IEditorPart part) {
       
   122 		Composite c = new Composite(parent, SWT.NONE);
       
   123 		GridLayoutFactory.swtDefaults().applyTo(c);
       
   124 		viewer = new CatalogViewer(getCatalog(), part.getEditorSite(), 
       
   125 				new RunnableContextDialog(part.getEditorSite().getShell(), 
       
   126 						Messages.AbstractDiscoveryPortalPageLayer_GatheringExtensionsDesc), 
       
   127 				getConfiguration());
       
   128 		viewer.createControl(c);
       
   129 		GridDataFactory.fillDefaults().grab(true, true).applyTo(viewer.getControl());
       
   130 		
       
   131 		return c;
       
   132 	}
       
   133 
       
   134 	@Override
       
   135 	public void init() {
       
   136 		if (!WorkbenchUtils.isJUnitRunning()) { // do not initialize the catalog if JUnit is running
       
   137 			Display.getDefault().asyncExec(new Runnable() {
       
   138 				@Override
       
   139 				public void run() {
       
   140 					for (ISelectionChangedListener listener : selectionListeners) {
       
   141 						viewer.addSelectionChangedListener(listener);
       
   142 					}
       
   143 					viewer.updateCatalog();
       
   144 				}
       
   145 			});
       
   146 		}
       
   147 	}
       
   148 
       
   149 	@Override
       
   150 	public IActionBar[] createCommandBars(IEditorPart part, IActionUIUpdater updater) {
       
   151 		this.updater = updater;
       
   152 		return new IActionBar[] { new ActionBar(part) };
       
   153 	}
       
   154 
       
   155 	protected CatalogConfiguration getConfiguration() {
       
   156 		CatalogConfiguration configuration = new CatalogConfiguration();
       
   157 		configuration.setShowTagFilter(false);
       
   158 		return configuration;
       
   159 	}
       
   160 
       
   161 	protected Catalog getCatalog() {
       
   162 		Catalog catalog = new Catalog();
       
   163 		catalog.setEnvironment(DiscoveryCore.createEnvironment());
       
   164 		catalog.setVerifyUpdateSiteAvailability(false);
       
   165 		
       
   166 		// look for remote descriptor
       
   167 		RemoteBundleDiscoveryStrategy remoteDiscoveryStrategy = new RemoteBundleDiscoveryStrategy();
       
   168 		String url = getDirectoryURL();
       
   169 		if (url != null) {
       
   170 			remoteDiscoveryStrategy.setDirectoryUrl(url);
       
   171 			catalog.getDiscoveryStrategies().add(remoteDiscoveryStrategy);
       
   172 		}
       
   173 		else // look for descriptors from installed bundles
       
   174 			catalog.getDiscoveryStrategies().add(new BundleDiscoveryStrategy());
       
   175 	
       
   176 		return catalog;
       
   177 	}
       
   178 
       
   179 	protected String getDirectoryURL() {
       
   180 		return Activator.getFromServerProperties(getClass().getName());
       
   181 	}
       
   182 
       
   183 	protected IAction[] makeActions(final IEditorPart part) {
       
   184 		selectionListeners = new ArrayList<ISelectionChangedListener>();
       
   185 		List<IAction> actions = new ArrayList<IAction>();
       
   186 		IAction action;
       
   187 		
       
   188 		// install
       
   189 		action = new BaseSelectionListenerAction(Messages.AbstractDiscoveryPortalPageLayer_InstallActionLabel) {
       
   190 			public void run() {
       
   191 				DiscoveryUi.install(viewer.getCheckedItems(), 
       
   192 						new RunnableContextDialog(part.getEditorSite().getShell(), 
       
   193 								Messages.AbstractDiscoveryPortalPageLayer_GatheringInstallInfoDesc));
       
   194 			};
       
   195 			
       
   196 			protected boolean updateSelection(IStructuredSelection selection) {
       
   197 				scheduleUpdateAllActionUIs();
       
   198 				return !selection.isEmpty();
       
   199 			};
       
   200 		};
       
   201 		action.setToolTipText(Messages.AbstractDiscoveryPortalPageLayer_InstallActionTooltip);
       
   202 		action.setId(INSTALL_ACTION_ID);
       
   203 		selectionListeners.add((ISelectionChangedListener) action);
       
   204 		actions.add(action);
       
   205 		
       
   206 		// refresh
       
   207 		action = new Action(Messages.AbstractDiscoveryPortalPageLayer_RefreshActionLabel) {
       
   208 			public void run() {
       
   209 				viewer.setSelection(StructuredSelection.EMPTY);
       
   210 				viewer.updateCatalog();
       
   211 				viewer.refresh();
       
   212 			}
       
   213 		};
       
   214 		action.setId(REFRESH_ACTION_ID);
       
   215 		actions.add(action);
       
   216 		
       
   217 		// check all
       
   218 		action = new BaseSelectionListenerAction(Messages.AbstractDiscoveryPortalPageLayer_CheckAllActionLabel) {
       
   219 			public void run() {
       
   220 				viewer.setSelection(StructuredSelection.EMPTY);
       
   221 				viewer.setSelection(getAllItemsSelection());
       
   222 				viewer.refresh();
       
   223 			}
       
   224 	
       
   225 			private IStructuredSelection getAllItemsSelection() {
       
   226 				List<CatalogItem> catalogItems = new ArrayList<CatalogItem>();
       
   227 				for (CatalogItem catalogItem : viewer.getCatalog().getItems()) {
       
   228 					if (!catalogItem.isInstalled())
       
   229 						catalogItems.add(catalogItem);
       
   230 				}	
       
   231 				return new StructuredSelection(catalogItems);
       
   232 			}
       
   233 	
       
   234 			protected boolean updateSelection(IStructuredSelection selection) {
       
   235 				scheduleUpdateAllActionUIs();
       
   236 				return !getAllItemsSelection().equals(selection);
       
   237 			}
       
   238 		};
       
   239 		action.setId(CHECK_ALL_ACTION_ID);
       
   240 		selectionListeners.add((ISelectionChangedListener) action);
       
   241 		actions.add(action);
       
   242 		
       
   243 		// uncheck all
       
   244 		action = new BaseSelectionListenerAction(Messages.AbstractDiscoveryPortalPageLayer_UncheckAllActionLabel) {
       
   245 			public void run() {
       
   246 				viewer.setSelection(StructuredSelection.EMPTY);
       
   247 				viewer.refresh();
       
   248 			};
       
   249 			
       
   250 			protected boolean updateSelection(IStructuredSelection selection) {
       
   251 				scheduleUpdateAllActionUIs();
       
   252 				return !selection.isEmpty();
       
   253 			};
       
   254 		};
       
   255 		action.setId(UNCHECK_ALL_ACTION_ID);
       
   256 		selectionListeners.add((ISelectionChangedListener) action);
       
   257 		actions.add(action);
       
   258 		
       
   259 		// advanced install
       
   260 		action = new Action(Messages.AbstractDiscoveryPortalPageLayer_AdvancedInstallActionLabel) {
       
   261 			public void run() {
       
   262 				showInstallWizard();
       
   263 			}
       
   264 		};
       
   265 		action.setId(ADV_INSTALL_ACTION_ID);
       
   266 		actions.add(action);
       
   267 		
       
   268 		ISelectionChangedListener selectionListener = new ISelectionChangedListener() {
       
   269 			@Override
       
   270 			public void selectionChanged(SelectionChangedEvent event) {
       
   271 				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
       
   272 				IActionBars bars = part.getEditorSite().getActionBars();
       
   273 				bars.getStatusLineManager().setMessage(
       
   274 						selection.isEmpty() ? null : MessageFormat.format(
       
   275 								Messages.AbstractDiscoveryPortalPageLayer_CheckedItemsStatusMessage, selection.size()));
       
   276 			}
       
   277 		};
       
   278 		selectionListeners.add(selectionListener);
       
   279 		
       
   280 		return (IAction[]) actions.toArray(new IAction[actions.size()]);
       
   281 	}
       
   282 
       
   283 	@Override
       
   284 	public void dispose() {
       
   285 		for (ISelectionChangedListener listener : selectionListeners) {
       
   286 			viewer.removeSelectionChangedListener(listener);
       
   287 		}
       
   288 	}
       
   289 
       
   290 	protected void showInstallWizard() {
       
   291 		ProvisioningUI defaultUI = ProvisioningUI.getDefaultUI();
       
   292 		ProvisioningSession session = defaultUI.getSession();
       
   293 		IProvisioningAgent agent = session.getProvisioningAgent();
       
   294 		IMetadataRepositoryManager metadataManager = (IMetadataRepositoryManager) agent.getService(IMetadataRepositoryManager.SERVICE_NAME);
       
   295 		IArtifactRepositoryManager artifactManager = (IArtifactRepositoryManager) agent.getService(IArtifactRepositoryManager.SERVICE_NAME);
       
   296 		for (URI uri : getCatalogURIs()) {
       
   297 			metadataManager.addRepository(uri);
       
   298 			artifactManager.addRepository(uri);
       
   299 		}
       
   300 		defaultUI.openInstallWizard(null, null, null);
       
   301 	}
       
   302 
       
   303 	protected Collection<URI> getCatalogURIs() {
       
   304 		Set<URI> uris = new HashSet<URI>();
       
   305 		for (CatalogItem catalogItem : viewer.getCatalog().getItems()) {
       
   306 			try {
       
   307 				uris.add(new URI(catalogItem.getSiteUrl()));
       
   308 			} catch (URISyntaxException e) {
       
   309 				// ignore bad URIs
       
   310 			}
       
   311 		}
       
   312 		return uris;
       
   313 	}
       
   314 
       
   315 	protected void scheduleUpdateAllActionUIs() {
       
   316 		Display.getDefault().asyncExec(new Runnable() {
       
   317 			@Override
       
   318 			public void run() {
       
   319 				updater.updateAll();
       
   320 			}
       
   321 		});
       
   322 	}
       
   323 
       
   324 }