KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > gui > tree > ScenarioTreeViewer


1 /*
2  * CLIF is a Load Injection Framework
3  * Copyright (C) 2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * CLIF
20  *
21  * Contact: clif@objectweb.org
22  */

23 package org.objectweb.clif.scenario.util.isac.gui.tree;
24
25 import org.apache.log4j.Category;
26 import org.eclipse.jface.action.MenuManager;
27 import org.eclipse.jface.viewers.DoubleClickEvent;
28 import org.eclipse.jface.viewers.IDoubleClickListener;
29 import org.eclipse.jface.viewers.ISelectionChangedListener;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.viewers.SelectionChangedEvent;
32 import org.eclipse.jface.viewers.TreeViewer;
33 import org.eclipse.jface.window.Window;
34 import org.eclipse.swt.dnd.Clipboard;
35 import org.eclipse.swt.dnd.DND;
36 import org.eclipse.swt.dnd.Transfer;
37 import org.eclipse.swt.events.FocusEvent;
38 import org.eclipse.swt.events.FocusListener;
39 import org.eclipse.swt.events.MouseEvent;
40 import org.eclipse.swt.events.MouseListener;
41 import org.eclipse.swt.widgets.Composite;
42 import org.objectweb.clif.scenario.util.isac.gui.ScenarioGUIEditor;
43 import org.objectweb.clif.scenario.util.isac.gui.tree.dnd.ScenarioTreeDragListener;
44 import org.objectweb.clif.scenario.util.isac.gui.tree.dnd.ScenarioTreeDropAdapter;
45 import org.objectweb.clif.scenario.util.isac.gui.tree.dnd.ScenarioTreeTransfer;
46 import org.objectweb.clif.scenario.util.isac.util.tree.ScenarioNode;
47 import org.objectweb.clif.scenario.util.isac.util.tree.TreeManager;
48
49 /**
50  * This is the implementation of the scenario tree viewer
51  *
52  * @author JC Meillaud
53  * @author A Peyrard
54  */

55 public class ScenarioTreeViewer extends TreeViewer
56         implements
57             ISelectionChangedListener,
58             IDoubleClickListener,
59             FocusListener,
60             MouseListener {
61     static Category cat = Category.getInstance(ScenarioTreeViewer.class.getName());
62     private TreeManager treeManager;
63     private ScenarioGUIEditor window;
64     private Clipboard clipboard;
65     private ScenarioNode lastNodeSelected ;
66
67     /**
68      * Build a new scenario tree viewer in the given composite with a certain
69      * style
70      *
71      * @param c
72      * The parent composite
73      * @param style
74      * The style of the viewer
75      */

76     public ScenarioTreeViewer(Composite c, int style, Window w) {
77         super(c, style);
78         cat.debug("-> constructor") ;
79         this.window = (ScenarioGUIEditor) w;
80
81         // get the tree manager
82
this.treeManager = TreeManager.getTreeManager(null);
83
84         this.setContentProvider(new ScenarioTreeContentProvider());
85         this.setLabelProvider(new ScenarioTreeLabelProvider());
86         this.setInput(this.treeManager.getTree());
87
88         this.addSelectionChangedListener(this);
89         this.addDoubleClickListener(this);
90         this.getTree().addMouseListener(this) ;
91         Transfer[] types = new Transfer[]{ScenarioTreeTransfer.getInstance()};
92         int operations = DND.DROP_MOVE | DND.DROP_COPY;
93         this.addDragSupport(operations, types, new ScenarioTreeDragListener(
94                 this));
95         this.addDropSupport(operations, types,
96                 new ScenarioTreeDropAdapter(this));
97         // init the clipboard
98
this.clipboard = new Clipboard(c.getDisplay());
99     }
100
101     /**
102      * Set the popup menu
103      * @param menuManager The popu menu
104      */

105     public void setPopUpMenu(MenuManager menuManager) {
106         this.getTree().setMenu(menuManager.createContextMenu(this.getTree())) ;
107     }
108     
109     /**
110      * Clipboard getter
111      *
112      * @return The clipboard
113      */

114     public Clipboard getClipboard() {
115         cat.debug("-> getClipboard") ;
116         return this.clipboard;
117     }
118
119     /**
120      * Getter for the selected node of the tree
121      *
122      * @return The node which is selected
123      */

124     public ScenarioNode getSelectedNode() {
125         cat.debug("-> getSelectedNode") ;
126         IStructuredSelection selection = (IStructuredSelection) super
127                 .getSelection();
128         if (selection != null) {
129             return (ScenarioNode) selection.getFirstElement();
130         }
131         return (ScenarioNode) this.getInput();
132     }
133
134     /**
135      * This method appen when a selection changed event append
136      *
137      * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
138      */

139     public void selectionChanged(SelectionChangedEvent event) {
140         cat.debug("-> selectionChanged") ;
141         ScenarioNode node = getSelectedNode();
142         if (node == lastNodeSelected)
143             return ;
144         this.window.notifySelectionChanged(node);
145         lastNodeSelected = node ;
146     }
147
148     /**
149      * This method is called when a double click event append
150      *
151      * @see org.eclipse.jface.viewers.IDoubleClickListener#doubleClick(org.eclipse.jface.viewers.DoubleClickEvent)
152      */

153     public void doubleClick(DoubleClickEvent event) {
154         cat.debug("-> doubleClick") ;
155         IStructuredSelection selection = (IStructuredSelection) event
156                 .getSelection();
157
158         Object JavaDoc selected_element = selection.getFirstElement();
159         this.setExpandedState(selected_element, !this
160                 .getExpandedState(selected_element));
161     }
162
163     /**
164      * Expand the element or collapse it
165      * @param element The element
166      * @param state True if the element must be expand, or false if it must be collapse
167      */

168     public void expand(Object JavaDoc element, boolean state) {
169         cat.debug("-> expand") ;
170         ScenarioNode parent = ((ScenarioNode) element).getParent();
171         if (parent != null)
172             expand(parent, state);
173         this.setExpandedState(element, state);
174     }
175     
176     /* (non-Javadoc)
177      * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
178      */

179     public void focusGained(FocusEvent arg0) {
180         cat.warn("Gain the focus on treeViwer") ;
181     }
182     /* (non-Javadoc)
183      * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
184      */

185     public void focusLost(FocusEvent arg0) {
186         cat.warn("Lost the focus on treeViwer") ;
187     }
188     
189     /* (non-Javadoc)
190      * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
191      */

192     public void mouseDoubleClick(MouseEvent arg0) {
193         this.getTree().setFocus() ;
194     }
195     /* (non-Javadoc)
196      * @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
197      */

198     public void mouseDown(MouseEvent arg0) {
199     }
200     /* (non-Javadoc)
201      * @see org.eclipse.swt.events.MouseListener#mouseUp(org.eclipse.swt.events.MouseEvent)
202      */

203     public void mouseUp(MouseEvent arg0) {
204         this.getTree().setFocus() ;
205     }
206 }
Popular Tags