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  import org.eclipse.jface.action.Action;
26  import org.eclipse.jface.viewers.StructuredSelection;
27  import org.eclipse.jface.viewers.TreeViewer;
28  import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
29  import org.fosstrak.llrp.commander.util.LLRPTreeMaintainer;
30  import org.llrp.ltk.types.LLRPMessage;
31  import org.llrp.ltk.types.LLRPParameter;
32  
33  /**
34   * This class represents the action to add a new LLRP parameter to a message or to a parameter.
35   *
36   * @author Ulrich Etter, ETHZ
37   *
38   */
39  public class AddParameterAction extends Action implements IWorkbenchAction {
40  	
41  	public final static String ID = "org.fosstrak.llrp.commander.editors.graphical.actions.AddParameterAction";
42  	private TreeViewer viewer;
43  	private LLRPTreeMaintainer treeMaintainer;
44  	private Object treeElement;
45  	private String childName;
46  	private String parameterName;
47  	
48  	public AddParameterAction(
49  			TreeViewer viewer, 
50  			LLRPTreeMaintainer treeMaintainer, 
51  			Object treeElement, 
52  			String childName,
53  			String parameterName){
54  		
55  		setId(ID);
56  		setText(parameterName);
57  		
58  		this.viewer = viewer;
59  		this.treeMaintainer = treeMaintainer;
60  		this.treeElement = treeElement;
61  		this.childName = childName;
62  		this.parameterName = parameterName;
63  	}
64  	
65  	@Override
66  	public void run(){
67  		Class parameterClass;
68  		try {
69  			parameterClass = Class.forName("org.llrp.ltk.generated.parameters." + parameterName);
70  			LLRPParameter subParameter = (LLRPParameter) parameterClass.getConstructor(new Class[0]).newInstance(new Object[0]);
71  			if (treeElement instanceof LLRPMessage || treeElement instanceof LLRPParameter){
72  				treeMaintainer.setChild(treeElement, childName, subParameter);
73  			}
74  			else if (treeElement instanceof List){
75  				treeMaintainer.addChild((List<LLRPParameter>) treeElement, subParameter);
76  			}
77  			viewer.refresh();
78  			viewer.setSelection(new StructuredSelection(subParameter), true);
79  		} catch (Exception e) {
80  			e.printStackTrace();
81  		}
82  	}
83  
84  	public void dispose() {
85  		
86  	}
87  
88  }
89