View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.commander.editors.graphical.actions;
23  
24  import java.util.List;
25  
26  import org.eclipse.jface.action.Action;
27  import org.eclipse.jface.viewers.ISelection;
28  import org.eclipse.jface.viewers.ISelectionChangedListener;
29  import org.eclipse.jface.viewers.IStructuredSelection;
30  import org.eclipse.jface.viewers.SelectionChangedEvent;
31  import org.eclipse.jface.viewers.TreeViewer;
32  import org.eclipse.ui.IWorkbenchWindow;
33  import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
34  import org.fosstrak.llrp.commander.editors.graphical.LLRPMasterDetailsBlock;
35  import org.fosstrak.llrp.commander.util.LLRP;
36  import org.fosstrak.llrp.commander.util.LLRPTreeMaintainer;
37  import org.llrp.ltk.types.*;
38  
39  
40  /**
41   * This class represents the action to delete an LLRP parameter.
42   *
43   * @author Ulrich Etter, ETHZ
44   *
45   */
46  public class DeleteParameterAction extends Action implements ISelectionChangedListener, IWorkbenchAction {
47  	
48  	public final static String ID = "org.eclipse.ui.forms.article.deleteParameter";
49  	private IStructuredSelection selection;
50  	private TreeViewer viewer;
51  	private LLRPTreeMaintainer treeMaintainer;
52  	private LLRPMasterDetailsBlock block;
53  	
54  	public DeleteParameterAction(IWorkbenchWindow window, TreeViewer viewer, LLRPTreeMaintainer treeMaintainer, LLRPMasterDetailsBlock block){
55  		setId(ID);
56  		setText("&Delete");
57  		setToolTipText("Delete this llrp parameter.");
58  
59  		viewer.addSelectionChangedListener(this);
60  		
61  		this.viewer = viewer;
62  		this.treeMaintainer = treeMaintainer;
63  		this.block = block;
64  	}
65  	
66  	@Override
67  	public void run(){
68  		LLRPParameter parameterToDelete = (LLRPParameter) selection.getFirstElement();
69  		Object parent = treeMaintainer.getParent(parameterToDelete);
70  		if (parent instanceof LLRPMessage || parent instanceof LLRPParameter){
71  			for (String childName : LLRP.getParameterAndChoiceNames(treeMaintainer.getDefinition(parent))){
72  				if (treeMaintainer.getChild(parent, childName) == parameterToDelete){
73  					treeMaintainer.setChild(parent, childName, null);
74  				}
75  			}
76  		}
77  		else if (parent instanceof List){
78  			treeMaintainer.removeChild((List<LLRPParameter>)parent, parameterToDelete);
79  		}
80  		block.refresh(parent);
81  	}
82  
83  	public void dispose() {
84  		viewer.removeSelectionChangedListener(this);
85  	}
86  
87  	public void selectionChanged(SelectionChangedEvent event) {
88  		boolean enable = false;
89  		ISelection incoming = event.getSelection();
90  		if (incoming instanceof IStructuredSelection){
91  			selection = (IStructuredSelection) incoming;
92  			if ((selection.size() == 1) && 
93  					(selection.getFirstElement() instanceof LLRPParameter)){
94  				enable = true;
95  			}
96  		}
97  		setEnabled(enable);
98  	}
99  
100 }