|
| 1 | +/******************************************************************************* |
| 2 | + * IBM Confidential |
| 3 | + * OCO Source Materials |
| 4 | + * (C) Copyright IBM Corp. 2018 All Rights Reserved. |
| 5 | + * The source code for this program is not published or otherwise |
| 6 | + * divested of its trade secrets, irrespective of what has |
| 7 | + * been deposited with the U.S. Copyright Office. |
| 8 | + *******************************************************************************/ |
| 9 | + |
| 10 | +package com.ibm.microclimate.ui.internal.actions; |
| 11 | + |
| 12 | +import java.net.URI; |
| 13 | + |
| 14 | +import org.eclipse.jface.action.IAction; |
| 15 | +import org.eclipse.jface.viewers.ISelection; |
| 16 | +import org.eclipse.jface.viewers.IStructuredSelection; |
| 17 | +import org.eclipse.osgi.util.NLS; |
| 18 | +import org.eclipse.ui.IObjectActionDelegate; |
| 19 | +import org.eclipse.ui.IWorkbenchPart; |
| 20 | +import org.eclipse.ui.PlatformUI; |
| 21 | +import org.eclipse.ui.browser.IWebBrowser; |
| 22 | +import org.eclipse.ui.browser.IWorkbenchBrowserSupport; |
| 23 | + |
| 24 | +import com.ibm.microclimate.core.internal.MCLogger; |
| 25 | +import com.ibm.microclimate.core.internal.MCUtil; |
| 26 | +import com.ibm.microclimate.core.internal.connection.MicroclimateConnection; |
| 27 | +import com.ibm.microclimate.ui.internal.messages.Messages; |
| 28 | + |
| 29 | +public class OpenMicroclimateUIAction implements IObjectActionDelegate { |
| 30 | + |
| 31 | + protected MicroclimateConnection connection; |
| 32 | + |
| 33 | + @Override |
| 34 | + public void selectionChanged(IAction action, ISelection selection) { |
| 35 | + if (!(selection instanceof IStructuredSelection)) { |
| 36 | + action.setEnabled(false); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + IStructuredSelection sel = (IStructuredSelection) selection; |
| 41 | + if (sel.size() == 1) { |
| 42 | + Object obj = sel.getFirstElement(); |
| 43 | + if (obj instanceof MicroclimateConnection) { |
| 44 | + connection = (MicroclimateConnection)obj; |
| 45 | + action.setEnabled(connection.isConnected()); |
| 46 | + return; |
| 47 | + } |
| 48 | + } |
| 49 | + action.setEnabled(false); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void run(IAction action) { |
| 54 | + if (connection == null) { |
| 55 | + // should not be possible |
| 56 | + MCLogger.logError("Open Microclimate UI action ran but no Microclimate connection was selected"); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (!connection.isConnected()) { |
| 61 | + MCUtil.openDialog(true, Messages.OpenMicroclimateUIError, |
| 62 | + NLS.bind(Messages.OpenMicroclimateUINotConnectedError, connection.baseUrl)); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + URI uri = connection.baseUrl; |
| 67 | + |
| 68 | + try { |
| 69 | + IWorkbenchBrowserSupport browserSupport = PlatformUI.getWorkbench().getBrowserSupport(); |
| 70 | + IWebBrowser browser = browserSupport |
| 71 | + .createBrowser(IWorkbenchBrowserSupport.NAVIGATION_BAR | IWorkbenchBrowserSupport.LOCATION_BAR, |
| 72 | + uri.toString(), uri.toString(), "Microclimate UI - " + uri); |
| 73 | + |
| 74 | + browser.openURL(uri.toURL()); |
| 75 | + } catch (Exception e) { |
| 76 | + MCLogger.logError("Error opening Microclimate UI in browser", e); //$NON-NLS-1$ |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void setActivePart(IAction arg0, IWorkbenchPart arg1) { |
| 82 | + // nothing |
| 83 | + } |
| 84 | +} |
0 commit comments