Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 11ef8637948fab27cc13a79d058b77c042a53f1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/****************************************************************************
 * Copyright (c) 2008 Composent, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are 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:
 *    Composent, Inc. - initial API and implementation
 *****************************************************************************/

package org.eclipse.ecf.internal.examples.updatesite.client;

import java.net.*;
import java.util.Arrays;
import java.util.List;
import org.eclipse.ecf.discovery.IServiceInfo;
import org.eclipse.ecf.discovery.identity.IServiceID;
import org.eclipse.ecf.discovery.ui.views.IServiceAccessHandler;
import org.eclipse.jface.action.*;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
import org.eclipse.update.internal.ui.UpdateUI;
import org.eclipse.update.internal.ui.model.SiteBookmark;
import org.eclipse.update.internal.ui.model.UpdateModel;
import org.eclipse.update.ui.UpdateManagerUI;

public class UpdateSiteServiceAccessHandler implements IServiceAccessHandler {

	static final String SERVICE = Messages.UpdateSiteServiceAccessHandler_UPDATESITE_SERVICE;
	static final String PATH = "path"; //$NON-NLS-1$
	static final String NAME = "name"; //$NON-NLS-1$
	static final String BROWSER_PATH_SUFFIX = Messages.UpdateSiteServiceAccessHandler_UPDATESITE_INDEX_HTML;

	static final IContributionItem[] EMPTY_CONTRIBUTION = {};

	public UpdateSiteServiceAccessHandler() {
		// nothing
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.ui.views.IServiceAccessHandler#getContributionsForService(org.eclipse.ecf.discovery.IServiceInfo)
	 */
	public IContributionItem[] getContributionsForService(IServiceInfo serviceInfo) {
		final IServiceID serviceID = serviceInfo.getServiceID();
		final List serviceTypes = Arrays.asList(serviceID.getServiceTypeID().getServices());
		String protocol = null;
		if (serviceTypes.contains(SERVICE))
			protocol = "http"; //$NON-NLS-1$
		if (protocol == null)
			return EMPTY_CONTRIBUTION;
		final URI location = serviceInfo.getLocation();
		final StringBuffer buf = new StringBuffer(protocol);
		buf.append("://").append(location.getHost()); //$NON-NLS-1$
		if (location.getPort() != -1)
			buf.append(":").append(location.getPort()); //$NON-NLS-1$ 
		final String path = serviceInfo.getServiceProperties().getPropertyString(PATH);
		final String name = serviceInfo.getServiceProperties().getPropertyString(NAME);
		if (path != null)
			buf.append(path);
		final String urlString = buf.toString();
		final IAction openUpdateSiteAction = new Action() {
			public void run() {
				addURLToUpdateSite(name, urlString);
			}
		};

		openUpdateSiteAction.setText(Messages.UpdateSiteServiceAccessHandler_OPEN_INSTALLER_MENU_TEXT);

		final Action browserAction = new Action() {
			public void run() {
				openBrowser(urlString + BROWSER_PATH_SUFFIX);
			}
		};
		browserAction.setText(Messages.UpdateSiteServiceAccessHandler_OPEN_BROWSER_MENU_TEXT);

		return new IContributionItem[] {new ActionContributionItem(openUpdateSiteAction), new ActionContributionItem(browserAction)};
	}

	private void addURLToUpdateSite(String name, String urlString) {
		try {
			final UpdateModel model = UpdateUI.getDefault().getUpdateModel();
			final SiteBookmark bookmark = new SiteBookmark(name, new URL(urlString), false);
			bookmark.setSelected(true);
			model.addBookmark(bookmark);
			model.saveBookmarks();

			UpdateManagerUI.openInstaller(Display.getDefault().getActiveShell());
		} catch (final MalformedURLException e) {
			e.printStackTrace();
		}
	}

	protected void openBrowser(String urlString) {
		final IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport();
		try {
			support.createBrowser(null).openURL(new URL(urlString));
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

}

Back to the top