Commit | Line | Data |
---|---|---|
ef906471 XR |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 Kalray | |
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 | * Xavier Raynaud - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.views.filter; | |
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.linuxtools.tmf.core.filter.model.ITmfFilterTreeNode; | |
19 | import org.eclipse.linuxtools.tmf.core.filter.model.TmfFilterNode; | |
20 | import org.eclipse.ui.IWorkbenchPage; | |
21 | import org.eclipse.ui.IWorkbenchPart; | |
22 | import org.eclipse.ui.IWorkbenchWindow; | |
23 | import org.eclipse.ui.PlatformUI; | |
24 | ||
25 | /** | |
26 | * Handler for paste command in filter view | |
27 | * @author Xavier Raynaud <xavier.raynaud@kalray.eu> | |
c4767854 | 28 | * @since 3.0 |
ef906471 XR |
29 | */ |
30 | public class PasteHandler extends AbstractHandler { | |
31 | ||
32 | @Override | |
33 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
34 | // Check if we are closing down | |
35 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
36 | if (window == null) { | |
37 | return null; | |
38 | } | |
39 | ||
40 | // Get the selection | |
41 | IWorkbenchPage page = window.getActivePage(); | |
42 | IWorkbenchPart part = page.getActivePart(); | |
43 | if (!(part instanceof FilterView)) { | |
44 | return null; | |
45 | } | |
46 | FilterView v = (FilterView) part; | |
47 | ||
48 | ITmfFilterTreeNode objectToPaste = FilterEditUtils.getTransferredTreeNode(); | |
49 | objectToPaste = objectToPaste.clone(); | |
50 | ITmfFilterTreeNode sel = v.getSelection(); | |
51 | if (sel == null || TmfFilterNode.NODE_NAME.equals(objectToPaste.getNodeName())) { | |
52 | sel = v.getFilterRoot(); | |
53 | } | |
54 | ||
55 | sel.addChild(objectToPaste); | |
56 | v.refresh(); | |
57 | v.setSelection(objectToPaste); | |
58 | return null; | |
59 | } | |
60 | ||
61 | @Override | |
62 | public boolean isEnabled() { | |
63 | // Check if we are closing down | |
64 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
65 | if (window == null) { | |
66 | return false; | |
67 | } | |
68 | ||
69 | // Get the selection | |
70 | IWorkbenchPage page = window.getActivePage(); | |
71 | IWorkbenchPart part = page.getActivePart(); | |
72 | if (!(part instanceof FilterView)) { | |
73 | return false; | |
74 | } | |
75 | FilterView v = (FilterView) part; | |
76 | ITmfFilterTreeNode sel = v.getSelection(); | |
77 | if (sel == null) { | |
78 | sel = v.getFilterRoot(); | |
79 | } | |
80 | ITmfFilterTreeNode objectToPaste = FilterEditUtils.getTransferredTreeNode(); | |
81 | if (objectToPaste != null && | |
82 | (sel.getValidChildren().contains(objectToPaste.getNodeName()) | |
83 | || TmfFilterNode.NODE_NAME.equals(objectToPaste.getNodeName()))) { | |
84 | return true; | |
85 | } | |
86 | return false; | |
87 | } | |
88 | ||
89 | } |