summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/actions/LaunchHobAction.java
blob: e92fac0a75d16776e148eb6a5cdb83ce800085cf (plain)
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
/*******************************************************************************
 * Copyright (c) 2011 Intel Corporation.
 * 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:
 * Intel - initial API and implementation
 *******************************************************************************/
package org.yocto.bc.ui.actions;

import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;

import org.eclipse.swt.widgets.Shell;

import org.yocto.bc.ui.builder.BitbakeCommanderNature;


public class LaunchHobAction  {
    private static final String DIALOG_TITLE  = "Launch HOB";
	
	public void run(IAction action) {
		IResource resource = getSelectedResource();
		if (resource == null)
			return;

		IProject project = resource.getProject();
		LaunchHobDialog hobDialog = new LaunchHobDialog(new Shell(), DIALOG_TITLE, project);
		hobDialog.open();
		String buildDir = hobDialog.getBuildDir();
	
		if (buildDir != null) {			
			try {
				BitbakeCommanderNature.launchHob(project,buildDir);
			} catch (Exception e){
				System.out.println(e.getMessage());
			}
		}
		
	}

	public void dispose() {

	}
	
	private IResource getSelectedResource() {
		IWorkbench iworkbench = PlatformUI.getWorkbench();
		if (iworkbench == null){
			return null;
		}
		IWorkbenchWindow iworkbenchwindow = iworkbench.getActiveWorkbenchWindow();
		if (iworkbenchwindow == null) {
			return null;
		}
		IWorkbenchPage iworkbenchpage = iworkbenchwindow.getActivePage();
		if (iworkbenchpage == null) {
			return null;
		}
		ISelection sel = iworkbenchpage.getSelection();

		if (!(sel instanceof IStructuredSelection))
			return null;
		IStructuredSelection ss = (IStructuredSelection) sel;
			Object element = ss.getFirstElement();
		if (element instanceof IResource)
		    return (IResource) element;
		if (!(element instanceof IAdaptable))
		    return null;
		 IAdaptable adaptable = (IAdaptable)element;
		 Object adapter = adaptable.getAdapter(IResource.class);
		    return (IResource) adapter;
	}
}