| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011 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 | * Bernd Hufmann - Initial API and implementation |
| 11 | * |
| 12 | *******************************************************************************/ |
| 13 | package org.eclipse.linuxtools.lttng.ui.tracecontrol.actions; |
| 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.Iterator; |
| 17 | import java.util.List; |
| 18 | |
| 19 | import org.eclipse.jface.action.IAction; |
| 20 | import org.eclipse.jface.dialogs.MessageDialog; |
| 21 | import org.eclipse.jface.viewers.ISelection; |
| 22 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 23 | import org.eclipse.linuxtools.lttng.core.tracecontrol.model.TraceResource; |
| 24 | import org.eclipse.linuxtools.lttng.ui.tracecontrol.Messages; |
| 25 | import org.eclipse.rse.core.events.ISystemRemoteChangeEvents; |
| 26 | import org.eclipse.rse.core.model.ISystemRegistry; |
| 27 | import org.eclipse.rse.core.model.SystemStartHere; |
| 28 | import org.eclipse.rse.ui.SystemBasePlugin; |
| 29 | import org.eclipse.swt.widgets.Shell; |
| 30 | import org.eclipse.ui.IObjectActionDelegate; |
| 31 | import org.eclipse.ui.IViewActionDelegate; |
| 32 | import org.eclipse.ui.IViewPart; |
| 33 | import org.eclipse.ui.IWorkbenchPart; |
| 34 | import org.eclipse.ui.IWorkbenchWindow; |
| 35 | import org.eclipse.ui.IWorkbenchWindowActionDelegate; |
| 36 | |
| 37 | /** |
| 38 | * <b><u>DeleteTrace</u></b> |
| 39 | * <p> |
| 40 | * Action implementation to delete a new trace. |
| 41 | * </p> |
| 42 | */ |
| 43 | public class DeleteTrace implements IObjectActionDelegate, IWorkbenchWindowActionDelegate, IViewActionDelegate { |
| 44 | |
| 45 | // ------------------------------------------------------------------------ |
| 46 | // Attributes |
| 47 | // ------------------------------------------------------------------------ |
| 48 | |
| 49 | private List<TraceResource> fSelectedTraces; |
| 50 | |
| 51 | // ------------------------------------------------------------------------ |
| 52 | // Constructors |
| 53 | // ------------------------------------------------------------------------ |
| 54 | |
| 55 | public DeleteTrace() { |
| 56 | fSelectedTraces = new ArrayList<TraceResource>(); |
| 57 | } |
| 58 | |
| 59 | // ------------------------------------------------------------------------ |
| 60 | // Operations |
| 61 | // ------------------------------------------------------------------------ |
| 62 | |
| 63 | /* |
| 64 | * (non-Javadoc) |
| 65 | * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart) |
| 66 | */ |
| 67 | @Override |
| 68 | public void setActivePart(IAction arg0, IWorkbenchPart arg1) { |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Gets the first selected target. |
| 73 | * |
| 74 | * @return first selected target. |
| 75 | */ |
| 76 | protected TraceResource getSelectedTarget() { |
| 77 | if (fSelectedTraces.size() > 0) { |
| 78 | return (TraceResource) fSelectedTraces.get(0); |
| 79 | } |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * (non-Javadoc) |
| 85 | * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) |
| 86 | */ |
| 87 | @Override |
| 88 | public void run(IAction arg0) { |
| 89 | |
| 90 | boolean confirm = MessageDialog.openConfirm(getShell(), |
| 91 | Messages.DeleteTrace_ConfirmTitle, |
| 92 | Messages.DeleteTrace_ConfirmMessage); |
| 93 | if (!confirm) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | int size = fSelectedTraces.size(); |
| 98 | for (int i = 0; i < size; i++) { |
| 99 | |
| 100 | final TraceResource trace = (TraceResource) fSelectedTraces.get(i); |
| 101 | |
| 102 | trace.getParent().removeTrace(trace); |
| 103 | |
| 104 | ISystemRegistry registry = SystemStartHere.getSystemRegistry(); |
| 105 | registry.fireRemoteResourceChangeEvent(ISystemRemoteChangeEvents.SYSTEM_REMOTE_RESOURCE_DELETED, trace, trace.getParent(), trace.getSubSystem(), null); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * (non-Javadoc) |
| 111 | * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection) |
| 112 | */ |
| 113 | @SuppressWarnings("unchecked") |
| 114 | @Override |
| 115 | public void selectionChanged(IAction action, ISelection selection) { |
| 116 | |
| 117 | if (selection instanceof IStructuredSelection) { |
| 118 | fSelectedTraces.clear(); |
| 119 | |
| 120 | // store the selected targets to be used when running |
| 121 | Iterator<IStructuredSelection> theSet = ((IStructuredSelection) selection).iterator(); |
| 122 | while (theSet.hasNext()) { |
| 123 | Object obj = theSet.next(); |
| 124 | if (obj instanceof TraceResource) { |
| 125 | fSelectedTraces.add((TraceResource)obj); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns the active workbench shell of this plug-in. |
| 134 | * |
| 135 | * @return active workbench shell. |
| 136 | */ |
| 137 | protected Shell getShell() { |
| 138 | return SystemBasePlugin.getActiveWorkbenchShell(); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * (non-Javadoc) |
| 143 | * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow) |
| 144 | */ |
| 145 | @Override |
| 146 | public void init(IWorkbenchWindow window) { |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * (non-Javadoc) |
| 151 | * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose() |
| 152 | */ |
| 153 | @Override |
| 154 | public void dispose() { |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * (non-Javadoc) |
| 159 | * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart) |
| 160 | */ |
| 161 | @Override |
| 162 | public void init(IViewPart view) { |
| 163 | } |
| 164 | } |