KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > launchConfigurations > LaunchConfigurationViewer


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.launchConfigurations;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jface.viewers.StructuredSelection;
18 import org.eclipse.jface.viewers.TreePath;
19 import org.eclipse.jface.viewers.TreeViewer;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Tree;
22 import org.eclipse.swt.widgets.TreeItem;
23 import org.eclipse.swt.widgets.Widget;
24
25 /**
26  * This class allow the notion of the viewer to be abstracted from the launch configuration view, as well as allowing the over-riding of
27  * selection preservation when filtering/deletion occurs
28  * @since 3.3
29  */

30 public class LaunchConfigurationViewer extends TreeViewer {
31
32     private int fTotalCount = 0;
33     private LaunchConfigurationView fView = null;
34     
35     /**
36      * Constructor
37      * @param tree the tree to create the viewer on
38      */

39     public LaunchConfigurationViewer(Tree tree) {
40         super(tree);
41     }
42     
43     /**
44      * Constructor
45      * @param parent
46      * @param style
47      */

48     public LaunchConfigurationViewer(Composite parent, int style) {
49         this(new Tree(parent, style));
50     }
51
52     /**
53      * @see org.eclipse.jface.viewers.StructuredViewer#preservingSelection(java.lang.Runnable)
54      */

55     protected void preservingSelection(Runnable JavaDoc updateCode) {
56         IStructuredSelection selection = (IStructuredSelection) getSelection();
57         if(!selection.isEmpty()) {
58             int[] indices = collectIndices(selection.getFirstElement());
59             updateCode.run();
60             ArrayList JavaDoc set = new ArrayList JavaDoc();
61             Object JavaDoc o = null;
62             for(Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
63                 o = iter.next();
64                 if(internalGetWidgetToSelect(o) != null) {
65                     if(!set.contains(o)) {
66                         set.add(o);
67                     }
68                 }
69             }
70             if(set.isEmpty()) {
71                 //make a new selection based on the first item in the structured selection
72
Tree tree = getTree();
73                 if(tree.getItemCount() > 0) {
74                     int index = selectIndex(tree.getItemCount(), indices[0]);
75                     if(index > -1) {
76                         TreeItem pitem = null;
77                         if(indices[0] > tree.getItemCount()-1) {
78                             pitem = tree.getItem(tree.getItemCount()-1);
79                         }
80                         else {
81                             pitem = tree.getItem(indices[0]);
82                         }
83                         if(pitem != null) {
84                             o = pitem.getData();
85                             if(indices[1] > -1) {
86                                 index = selectIndex(pitem.getItemCount(), indices[1]);
87                                 if(index > -1) {
88                                     Object JavaDoc d = pitem.getItem(index).getData();
89                                     if(d != null) {
90                                         o = d;
91                                     }
92                                 }
93                                 else {
94                                     if(pitem.getItemCount() > 0) {
95                                         o = pitem.getItem((indices[1]-1 > -1 ? indices[1]-1 : 0)).getData();
96                                         if(o == null) {
97                                             o = pitem.getData();
98                                         }
99                                     }
100                                 }
101                             }
102                         }
103                     }
104                     if(!set.contains(o)) {
105                         set.add(o);
106                     }
107                 }
108             }
109             setSelection(new StructuredSelection(set), true);
110         }
111         else {
112             super.preservingSelection(updateCode);
113         }
114         getTree().getHorizontalBar().setSelection(0);
115     }
116     
117     /**
118      * Covers the case of an outlier indice
119      * @param count the count to compare the index to
120      * @param index the index to compare against the count
121      * @return the adjusted index in the event index is an outlier, or -1 if it falls within the 'count' range
122      */

123     private int selectIndex(int count, int index) {
124         if(index < count) {
125             return index;
126         }
127         if(index > count-1) {
128             return count-1;
129         }
130         if(index < 0) {
131             return 0;
132         }
133         return -1;
134     }
135     
136     /**
137      * Returns the total count of all of the children that <i>could</i> be visible at
138      * the time the input was set to the viewer
139      * @return the total number of elements
140      */

141     protected int getTotalChildCount() {
142         return fTotalCount;
143     }
144     
145     /**
146      * @see org.eclipse.jface.viewers.AbstractTreeViewer#remove(java.lang.Object)
147      */

148     public void remove(Object JavaDoc elementsOrTreePaths) {
149         super.remove(elementsOrTreePaths);
150         fTotalCount--;
151     }
152
153     /**
154      * @see org.eclipse.jface.viewers.TreeViewer#internalAdd(org.eclipse.swt.widgets.Widget, java.lang.Object, java.lang.Object[])
155      */

156     protected void internalAdd(Widget widget, Object JavaDoc parentElement, Object JavaDoc[] childElements) {
157         super.internalAdd(widget, parentElement, childElements);
158         fTotalCount++;
159     }
160
161     /**
162      * @see org.eclipse.jface.viewers.AbstractTreeViewer#inputChanged(java.lang.Object, java.lang.Object)
163      */

164     protected void inputChanged(Object JavaDoc input, Object JavaDoc oldInput) {
165         super.inputChanged(input, oldInput);
166         //calc the total number of items that could be visible in the view
167
LaunchConfigurationTreeContentProvider cp = (LaunchConfigurationTreeContentProvider) getContentProvider();
168         Object JavaDoc[] types = cp.getElements(null);
169         LaunchGroupFilter filter = new LaunchGroupFilter(((LaunchConfigurationsDialog)LaunchConfigurationsDialog.getCurrentlyVisibleLaunchConfigurationDialog()).getLaunchGroup());
170         for(int i = 0; i < types.length; i++) {
171             if(filter.select(this, types[i], null)) {
172                 fTotalCount += cp.getChildren(types[i]).length + 1; //+1 for the type
173
}
174         }
175     }
176
177     /**
178      * returns the number of children that are remaining in the view.
179      * Note that this method will force the loading of all children
180      * @return the count of all children in the viewer
181      *
182      * @since 3.3
183      */

184     protected int getNonFilteredChildCount() {
185         int count = 0;
186         getTree().setRedraw(false);
187         TreeItem[] items = getTree().getItems();
188         count += items.length;
189         boolean expanded = false;
190         TreeItem item = null;
191         for(int i = 0; i < items.length; i++) {
192             item = items[i];
193             expanded = item.getExpanded();
194             setExpandedState(item.getData(), true);
195             count += item.getItems().length;
196             item.setExpanded(expanded);
197         }
198         getTree().setRedraw(true);
199         return count;
200     }
201     
202     /**
203      * Collects the indices of the child and parent items for the specified element
204      * @param object the element to collect indices for
205      * @return an array of indices for the specified element
206      */

207     private int[] collectIndices(Object JavaDoc object) {
208         int[] indices = {-1, -1};
209         if(object != null) {
210             TreeItem item = (TreeItem) findItem(object);
211             if(item != null) {
212                 TreePath path = getTreePathFromItem(item);
213                 item = (TreeItem) findItem(path.getFirstSegment());
214                 if(item != null) {
215                     indices[0] = getTree().indexOf(item);
216                     if(path.getSegmentCount() == 2) {
217                         indices[1] = indexOf(item.getItems(), path.getLastSegment());
218                     }
219                 }
220             }
221         }
222         return indices;
223     }
224     
225     /**
226      * Finds the index of the specified object in the given array of tree items
227      * @param items the items to search for the specified object
228      * @param object the object to find the index of
229      * @return the index of the specified object inthe listing of tree items, or -1 if not found
230      */

231     private int indexOf(TreeItem[] items, Object JavaDoc object) {
232         if(object != null) {
233             for(int i = 0; i < items.length; i++) {
234                 if(object.equals(items[i].getData())) {
235                     return i;
236                 }
237             }
238         }
239         return -1;
240     }
241
242     /**
243      * The filter changed due to text typing - update the filter count
244      */

245     protected void filterChanged() {
246         if (fView != null) {
247             fView.updateFilterLabel();
248         }
249         
250     }
251
252     /**
253      * @param launchConfigurationView
254      */

255     protected void setLaunchConfigurationView(LaunchConfigurationView launchConfigurationView) {
256         fView = launchConfigurationView;
257     }
258 }
259
Popular Tags