Commit | Line | Data |
---|---|---|
abfad0aa FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.views.project.handlers; | |
14 | ||
15 | import org.eclipse.core.commands.AbstractHandler; | |
16 | import org.eclipse.core.commands.ExecutionEvent; | |
17 | import org.eclipse.core.commands.ExecutionException; | |
18 | import org.eclipse.core.resources.IFolder; | |
19 | import org.eclipse.jface.viewers.ISelection; | |
20 | import org.eclipse.jface.viewers.StructuredSelection; | |
21 | import org.eclipse.jface.window.Window; | |
22 | import org.eclipse.linuxtools.tmf.ui.views.project.ProjectView; | |
23 | import org.eclipse.linuxtools.tmf.ui.views.project.dialogs.NewExperimentDialog; | |
24 | import org.eclipse.linuxtools.tmf.ui.views.project.model.ITmfProjectTreeNode; | |
25 | import org.eclipse.linuxtools.tmf.ui.views.project.model.TmfExperimentFolderNode; | |
26 | import org.eclipse.linuxtools.tmf.ui.views.project.model.TmfProjectNode; | |
27 | import org.eclipse.swt.widgets.Shell; | |
28 | import org.eclipse.ui.IWorkbenchPage; | |
29 | import org.eclipse.ui.IWorkbenchWindow; | |
30 | import org.eclipse.ui.PlatformUI; | |
31 | ||
32 | /** | |
33 | * <b><u>NewExperimentHandler</u></b> | |
34 | * <p> | |
35 | * TODO: Implement me. Please. | |
36 | */ | |
37 | public class NewExperimentHandler extends AbstractHandler { | |
38 | ||
39 | private TmfProjectNode fProject = null; | |
40 | ||
41 | // ------------------------------------------------------------------------ | |
42 | // Validation | |
43 | // ------------------------------------------------------------------------ | |
44 | ||
9ccc6d01 | 45 | @Override |
abfad0aa FC |
46 | public boolean isEnabled() { |
47 | ||
48 | // Check if we are closing down | |
49 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
50 | if (window == null) | |
51 | return false; | |
52 | ||
53 | // Check if we are in the Project View | |
54 | IWorkbenchPage page = window.getActivePage(); | |
55 | if (!(page.getActivePart() instanceof ProjectView)) | |
56 | return false; | |
57 | ||
58 | // Check if a project is selected | |
59 | ISelection selection = page.getSelection(ProjectView.ID); | |
60 | if (selection instanceof StructuredSelection) { | |
61 | Object element = ((StructuredSelection) selection).getFirstElement(); | |
62 | if (element instanceof ITmfProjectTreeNode) { | |
63 | ITmfProjectTreeNode node = (ITmfProjectTreeNode) element; | |
64 | while (node != null && !(node instanceof TmfProjectNode)) { | |
65 | node = node.getParent(); | |
66 | } | |
67 | fProject = (node instanceof TmfProjectNode) ? (TmfProjectNode) node : null; | |
68 | } | |
69 | } | |
70 | ||
71 | return (fProject != null && fProject.isOpen() && fProject.isTmfProject()); | |
72 | } | |
73 | ||
74 | // ------------------------------------------------------------------------ | |
75 | // Execution | |
76 | // ------------------------------------------------------------------------ | |
77 | ||
d4011df2 | 78 | @Override |
abfad0aa FC |
79 | public Object execute(ExecutionEvent event) throws ExecutionException { |
80 | ||
81 | // Open the experiment creation dialog | |
82 | TmfExperimentFolderNode experimentFolder = fProject.getExperimentsFolder(); | |
83 | Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); | |
84 | NewExperimentDialog dialog = new NewExperimentDialog(shell, experimentFolder); | |
85 | dialog.open(); | |
86 | ||
87 | if (dialog.getReturnCode() == Window.OK) { | |
88 | Object[] experiments = dialog.getResult(); | |
89 | if (experiments[0] instanceof IFolder) { | |
90 | experimentFolder.addExperiment((IFolder) experiments[0]); | |
91 | } | |
92 | } | |
93 | ||
94 | return null; | |
95 | } | |
96 | ||
97 | } |