| /******************************************************************************* |
| * Copyright (c) 2014 Bosch Software Innovations GmbH and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * and Eclipse Distribution License v1.0 which accompany this distribution. |
| * |
| * The Eclipse Public License is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * The Eclipse Distribution License is available at |
| * http://www.eclipse.org/org/documents/edl-v10.php. |
| * |
| * Contributors: |
| * Bosch Software Innovations GmbH - Please refer to git log |
| * |
| *******************************************************************************/ |
| package org.eclipse.vorto.api.common.project; |
| |
| import org.eclipse.core.runtime.IAdaptable; |
| import org.eclipse.jdt.core.IJavaProject; |
| import org.eclipse.jface.text.ITextSelection; |
| import org.eclipse.jface.viewers.ISelection; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.ui.ISelectionService; |
| import org.eclipse.ui.IViewPart; |
| import org.eclipse.ui.IViewReference; |
| import org.eclipse.ui.IWorkbench; |
| import org.eclipse.ui.IWorkbenchWindow; |
| import org.eclipse.ui.PlatformUI; |
| import org.eclipse.vorto.api.common.ApplicationConstants; |
| |
| |
| public class IProjectSelectionFactory implements IEclipseProjectSelectionFactory { |
| |
| private static IProjectSelectionFactory instance = null; |
| |
| private IProjectSelectionFactory() { |
| |
| } |
| |
| public static IProjectSelectionFactory getInstance() { |
| if (instance == null) { |
| instance = new IProjectSelectionFactory(); |
| } |
| return instance; |
| } |
| |
| @SuppressWarnings("unchecked") |
| @Override |
| public <IProject> IProject getProjectFromSelection() { |
| if (PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null) { |
| |
| IWorkbench workbench = PlatformUI.getWorkbench(); |
| IWorkbenchWindow activeWorkbenchWindow = workbench |
| .getActiveWorkbenchWindow(); |
| ISelectionService selectionService = activeWorkbenchWindow |
| .getSelectionService(); |
| ISelection selection2 = selectionService.getSelection(); |
| IViewPart activePart = null; |
| IStructuredSelection selection = null; |
| |
| if (selection2 instanceof ITextSelection) { |
| IViewReference[] viewReferences = PlatformUI.getWorkbench() |
| .getActiveWorkbenchWindow().getActivePage() |
| .getViewReferences(); |
| for (IViewReference view : viewReferences) { |
| if (ApplicationConstants.VORTO_TREE_VIEW_ID.equals(view |
| .getId())) { |
| activePart = view.getView(true); |
| break; |
| } |
| } |
| selection = (IStructuredSelection) activePart.getSite() |
| .getSelectionProvider().getSelection(); |
| } else { |
| selection = (IStructuredSelection) selection2; |
| } |
| |
| if (selection.isEmpty()) { |
| return null; |
| } |
| |
| Object firstElement = selection.getFirstElement(); |
| if (firstElement instanceof IJavaProject) { |
| return (IProject) ((IJavaProject) firstElement).getProject(); |
| } else if (firstElement instanceof IAdaptable) { |
| IProject project = (IProject) ((IAdaptable) firstElement); |
| return project; |
| } else if (firstElement instanceof IoTModelProject) { |
| IProject project = (IProject) ((IoTModelProject) firstElement) |
| .getProject(); |
| return project; |
| } else |
| return (IProject) firstElement; |
| } |
| |
| return null; |
| } |
| |
| } |