core/com.nokia.carbide.discovery.ui/src/com/nokia/carbide/internal/discovery/ui/view/DiscoveryView.java
changeset 1661 cd8159f371c7
parent 1660 a53047bc0aba
child 1665 84889eebc1c3
equal deleted inserted replaced
1660:a53047bc0aba 1661:cd8159f371c7
     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.view;
       
    18 
       
    19 
       
    20 import java.net.URI;
       
    21 import java.net.URISyntaxException;
       
    22 import java.text.MessageFormat;
       
    23 import java.util.ArrayList;
       
    24 import java.util.Collection;
       
    25 import java.util.HashSet;
       
    26 import java.util.List;
       
    27 import java.util.Set;
       
    28 
       
    29 import org.eclipse.equinox.internal.p2.discovery.Catalog;
       
    30 import org.eclipse.equinox.internal.p2.discovery.DiscoveryCore;
       
    31 import org.eclipse.equinox.internal.p2.discovery.compatibility.BundleDiscoveryStrategy;
       
    32 import org.eclipse.equinox.internal.p2.discovery.compatibility.RemoteBundleDiscoveryStrategy;
       
    33 import org.eclipse.equinox.internal.p2.discovery.model.CatalogItem;
       
    34 import org.eclipse.equinox.internal.p2.ui.discovery.DiscoveryUi;
       
    35 import org.eclipse.equinox.internal.p2.ui.discovery.wizards.CatalogConfiguration;
       
    36 import org.eclipse.equinox.internal.p2.ui.discovery.wizards.CatalogViewer;
       
    37 import org.eclipse.equinox.p2.core.IProvisioningAgent;
       
    38 import org.eclipse.equinox.p2.operations.ProvisioningSession;
       
    39 import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
       
    40 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
       
    41 import org.eclipse.equinox.p2.ui.ProvisioningUI;
       
    42 import org.eclipse.jface.action.Action;
       
    43 import org.eclipse.jface.action.IMenuManager;
       
    44 import org.eclipse.jface.action.IToolBarManager;
       
    45 import org.eclipse.jface.action.Separator;
       
    46 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
       
    47 import org.eclipse.jface.layout.GridDataFactory;
       
    48 import org.eclipse.jface.layout.GridLayoutFactory;
       
    49 import org.eclipse.jface.viewers.ISelectionChangedListener;
       
    50 import org.eclipse.jface.viewers.IStructuredSelection;
       
    51 import org.eclipse.jface.viewers.SelectionChangedEvent;
       
    52 import org.eclipse.jface.viewers.StructuredSelection;
       
    53 import org.eclipse.swt.SWT;
       
    54 import org.eclipse.swt.widgets.Composite;
       
    55 import org.eclipse.swt.widgets.Shell;
       
    56 import org.eclipse.ui.IActionBars;
       
    57 import org.eclipse.ui.PlatformUI;
       
    58 import org.eclipse.ui.actions.BaseSelectionListenerAction;
       
    59 import org.eclipse.ui.part.ViewPart;
       
    60 
       
    61 import com.nokia.carbide.discovery.ui.Activator;
       
    62 import com.nokia.carbide.discovery.ui.Messages;
       
    63 import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils;
       
    64 
       
    65 @SuppressWarnings("restriction")
       
    66 public class DiscoveryView extends ViewPart {
       
    67 	
       
    68 	private final class RunnableContextDialog extends ProgressMonitorDialog {
       
    69 		private final String title;
       
    70 
       
    71 		private RunnableContextDialog(Shell parent, String title) {
       
    72 			super(parent);
       
    73 			this.title = title;
       
    74 		}
       
    75 
       
    76 		@Override
       
    77 		protected void configureShell(Shell shell) {
       
    78 			super.configureShell(shell);
       
    79 			shell.setText(title);
       
    80 		}
       
    81 		
       
    82 	}
       
    83 
       
    84 	private static final String DIRECTORY_KEY = "com.nokia.carbide.discovery.directory"; //$NON-NLS-1$
       
    85 
       
    86 	private CatalogViewer viewer;
       
    87 	private Action refreshAction;
       
    88 	private BaseSelectionListenerAction checkAllAction;
       
    89 	private BaseSelectionListenerAction checkNoneAction;
       
    90 	private BaseSelectionListenerAction installAction;
       
    91 	private Action showInstallWizardAction;
       
    92 	private ISelectionChangedListener selectionListener;
       
    93 
       
    94 	/**
       
    95 	 * This is a callback that will allow us
       
    96 	 * to create the viewer and initialize it.
       
    97 	 */
       
    98 	public void createPartControl(Composite parent) {
       
    99 		Composite c = new Composite(parent, SWT.NONE);
       
   100 		GridLayoutFactory.fillDefaults().applyTo(c);
       
   101 		viewer = new CatalogViewer(getCatalog(), getSite(), 
       
   102 				new RunnableContextDialog(DiscoveryView.this.getViewSite().getShell(), 
       
   103 						Messages.DiscoveryView_GatherExtensionsTitle), 
       
   104 				getConfiguration());
       
   105 		viewer.createControl(c);
       
   106 		GridDataFactory.fillDefaults().grab(true, true).applyTo(viewer.getControl());
       
   107 		
       
   108 		// Create the help context id for the viewer's control
       
   109 		PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), 
       
   110 				"com.nokia.carbide.discovery.ui.view.DiscoveryView.catalogviewer"); //$NON-NLS-1$
       
   111 		makeActions();
       
   112 		contributeToActionBars();
       
   113 		if (!WorkbenchUtils.isJUnitRunning()) { // do not initialize the catalog if JUnit is running
       
   114 			getSite().getShell().getDisplay().asyncExec(new Runnable() {
       
   115 				@Override
       
   116 				public void run() {
       
   117 					viewer.updateCatalog();
       
   118 				}
       
   119 			});
       
   120 		}
       
   121 	}
       
   122 	
       
   123 	private CatalogConfiguration getConfiguration() {
       
   124 		CatalogConfiguration configuration = new CatalogConfiguration();
       
   125 		configuration.setShowTagFilter(false);
       
   126 		return configuration;
       
   127 	}
       
   128 
       
   129 	private Catalog getCatalog() {
       
   130 		Catalog catalog = new Catalog();
       
   131 		catalog.setEnvironment(DiscoveryCore.createEnvironment());
       
   132 		catalog.setVerifyUpdateSiteAvailability(false);
       
   133 		
       
   134 		// look for remote descriptor
       
   135 		RemoteBundleDiscoveryStrategy remoteDiscoveryStrategy = new RemoteBundleDiscoveryStrategy();
       
   136 		String url = Activator.getFromServerProperties(DIRECTORY_KEY);
       
   137 		if (url != null) {
       
   138 			remoteDiscoveryStrategy.setDirectoryUrl(url);
       
   139 			catalog.getDiscoveryStrategies().add(remoteDiscoveryStrategy);
       
   140 		}
       
   141 		else // look for descriptors from installed bundles
       
   142 			catalog.getDiscoveryStrategies().add(new BundleDiscoveryStrategy());
       
   143 
       
   144 		return catalog;
       
   145 	}
       
   146 
       
   147 	private void contributeToActionBars() {
       
   148 		IActionBars bars = getViewSite().getActionBars();
       
   149 		fillLocalPullDown(bars.getMenuManager());
       
   150 		fillLocalToolBar(bars.getToolBarManager());
       
   151 	}
       
   152 
       
   153 	private void fillLocalPullDown(IMenuManager manager) {
       
   154 		manager.add(checkAllAction);
       
   155 		manager.add(checkNoneAction);
       
   156 		manager.add(new Separator());
       
   157 		manager.add(installAction);
       
   158 		manager.add(showInstallWizardAction);
       
   159 	}
       
   160 
       
   161 	private void fillLocalToolBar(IToolBarManager manager) {
       
   162 		manager.add(refreshAction);
       
   163 		manager.add(installAction);
       
   164 	}
       
   165 
       
   166 	private void makeActions() {
       
   167 		refreshAction = new Action() {
       
   168 			public void run() {
       
   169 				viewer.setSelection(StructuredSelection.EMPTY);
       
   170 				viewer.updateCatalog();
       
   171 				viewer.refresh();
       
   172 			}
       
   173 		};
       
   174 		refreshAction.setText(Messages.DiscoveryView_RefreshLabel);
       
   175 		refreshAction.setImageDescriptor(Activator.getImageDescriptor("icons/refresh.gif")); //$NON-NLS-1$
       
   176 		checkAllAction = new BaseSelectionListenerAction(Messages.DiscoveryView_CheckAllLabel) {
       
   177 			public void run() {
       
   178 				viewer.setSelection(StructuredSelection.EMPTY);
       
   179 				viewer.setSelection(getAllItemsSelection());
       
   180 				viewer.refresh();
       
   181 			}
       
   182 
       
   183 			private IStructuredSelection getAllItemsSelection() {
       
   184 				List<CatalogItem> catalogItems = new ArrayList<CatalogItem>();
       
   185 				for (CatalogItem catalogItem : viewer.getCatalog().getItems()) {
       
   186 					if (!catalogItem.isInstalled())
       
   187 						catalogItems.add(catalogItem);
       
   188 				}	
       
   189 				return new StructuredSelection(catalogItems);
       
   190 			}
       
   191 
       
   192 			protected boolean updateSelection(IStructuredSelection selection) {
       
   193 				return !getAllItemsSelection().equals(selection);
       
   194 			};
       
   195 		};
       
   196 		checkNoneAction = new BaseSelectionListenerAction(Messages.DiscoveryView_UncheckAllLabel) {
       
   197 			public void run() {
       
   198 				viewer.setSelection(StructuredSelection.EMPTY);
       
   199 				viewer.refresh();
       
   200 			};
       
   201 			
       
   202 			protected boolean updateSelection(IStructuredSelection selection) {
       
   203 				return !selection.isEmpty();
       
   204 			};
       
   205 		};
       
   206 		installAction = new BaseSelectionListenerAction(Messages.DiscoveryView_InstallLabel) {
       
   207 			public void run() {
       
   208 				DiscoveryUi.install(viewer.getCheckedItems(), 
       
   209 						new RunnableContextDialog(DiscoveryView.this.getViewSite().getShell(), 
       
   210 								Messages.DiscoveryView_GatheringInstallInfoTitle));
       
   211 			};
       
   212 			
       
   213 			protected boolean updateSelection(IStructuredSelection selection) {
       
   214 				return !selection.isEmpty();
       
   215 			};
       
   216 		};
       
   217 		installAction.setImageDescriptor(Activator.getImageDescriptor("icons/icon-discovery.png")); //$NON-NLS-1$
       
   218 		showInstallWizardAction = new Action(Messages.DiscoveryView_AdvancedInstallLabel) {
       
   219 			public void run() {
       
   220 				showInstallWizard();
       
   221 			}
       
   222 		};
       
   223 		viewer.addSelectionChangedListener(checkAllAction);
       
   224 		viewer.addSelectionChangedListener(checkNoneAction);
       
   225 		viewer.addSelectionChangedListener(installAction);
       
   226 		selectionListener = new ISelectionChangedListener() {
       
   227 			@Override
       
   228 			public void selectionChanged(SelectionChangedEvent event) {
       
   229 				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
       
   230 				IActionBars bars = getViewSite().getActionBars();
       
   231 				bars.getStatusLineManager().setMessage(
       
   232 						selection.isEmpty() ? null : MessageFormat.format(
       
   233 								Messages.DiscoveryView_StatusLineFmt, selection.size()));
       
   234 			}
       
   235 		};
       
   236 		viewer.addSelectionChangedListener(selectionListener);
       
   237 	}
       
   238 	
       
   239 	@Override
       
   240 	public void dispose() {
       
   241 		viewer.removeSelectionChangedListener(checkAllAction);
       
   242 		viewer.removeSelectionChangedListener(checkNoneAction);
       
   243 		viewer.removeSelectionChangedListener(installAction);
       
   244 		viewer.removeSelectionChangedListener(selectionListener);
       
   245 		
       
   246 		super.dispose();
       
   247 	}
       
   248 
       
   249 	/**
       
   250 	 * Passing the focus request to the viewer's control.
       
   251 	 */
       
   252 	public void setFocus() {
       
   253 	}
       
   254 
       
   255 	private void showInstallWizard() {
       
   256 		ProvisioningUI defaultUI = ProvisioningUI.getDefaultUI();
       
   257 		ProvisioningSession session = defaultUI.getSession();
       
   258 		IProvisioningAgent agent = session.getProvisioningAgent();
       
   259 		IMetadataRepositoryManager metadataManager = (IMetadataRepositoryManager) agent.getService(IMetadataRepositoryManager.SERVICE_NAME);
       
   260 		IArtifactRepositoryManager artifactManager = (IArtifactRepositoryManager) agent.getService(IArtifactRepositoryManager.SERVICE_NAME);
       
   261 		for (URI uri : getCatalogURIs()) {
       
   262 			metadataManager.addRepository(uri);
       
   263 			artifactManager.addRepository(uri);
       
   264 		}
       
   265 		defaultUI.openInstallWizard(null, null, null);
       
   266 		
       
   267 //		String profileId = defaultUI.getProfileId();
       
   268 //		IProfileRegistry profileRegistry = (IProfileRegistry) agent.getService(IProfileRegistry.SERVICE_NAME);
       
   269 //		IProfile profile = profileRegistry.getProfile(profileId);
       
   270 //		IQueryResult<IInstallableUnit> queryResult = profile.query(QueryUtil.createIUGroupQuery(), null);
       
   271 //		List<String> list = new ArrayList<String>();
       
   272 //		for (Iterator<IInstallableUnit> iter = queryResult.iterator(); iter.hasNext();) {
       
   273 //			IInstallableUnit iu = iter.next();
       
   274 //			list.add(iu.getId() + "|" + iu.getVersion());
       
   275 //		}
       
   276 //		Collections.sort(list);
       
   277 //		for (String s : list) {
       
   278 //			System.out.println(s);
       
   279 //		}
       
   280 	}
       
   281 
       
   282 	private Collection<URI> getCatalogURIs() {
       
   283 		Set<URI> uris = new HashSet<URI>();
       
   284 		for (CatalogItem catalogItem : viewer.getCatalog().getItems()) {
       
   285 			try {
       
   286 				uris.add(new URI(catalogItem.getSiteUrl()));
       
   287 			} catch (URISyntaxException e) {
       
   288 				// ignore bad URIs
       
   289 			}
       
   290 		}
       
   291 		return uris;
       
   292 	};
       
   293 
       
   294 }