KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > SelectAllAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.search.internal.ui;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.swt.widgets.Tree;
17 import org.eclipse.swt.widgets.TreeItem;
18
19 import org.eclipse.jface.action.Action;
20 import org.eclipse.jface.viewers.StructuredViewer;
21 import org.eclipse.jface.viewers.TableViewer;
22 import org.eclipse.jface.viewers.TreeViewer;
23
24 import org.eclipse.ui.PlatformUI;
25  
26 /**
27  * This action selects all entries currently showing in view.
28  */

29 public class SelectAllAction extends Action {
30
31     private StructuredViewer fViewer;
32     
33     /**
34      * Creates the action.
35      */

36     public SelectAllAction() {
37         super("selectAll"); //$NON-NLS-1$
38
setText(SearchMessages.SelectAllAction_label);
39         setToolTipText(SearchMessages.SelectAllAction_tooltip);
40         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, ISearchHelpContextIds.SELECT_ALL_ACTION);
41     }
42     
43     public void setViewer(StructuredViewer viewer) {
44         fViewer= viewer;
45     }
46     
47     private void collectExpandedAndVisible(TreeItem[] items, List JavaDoc result) {
48         for (int i= 0; i < items.length; i++) {
49             TreeItem item= items[i];
50             result.add(item);
51             if (item.getExpanded()) {
52                 collectExpandedAndVisible(item.getItems(), result);
53             }
54         }
55     }
56
57     /**
58      * Selects all resources in the view.
59      */

60     public void run() {
61         if (fViewer == null || fViewer.getControl().isDisposed()) {
62             return;
63         }
64         if (fViewer instanceof TreeViewer) {
65             ArrayList JavaDoc allVisible= new ArrayList JavaDoc();
66             Tree tree= ((TreeViewer) fViewer).getTree();
67             collectExpandedAndVisible(tree.getItems(), allVisible);
68             tree.setSelection((TreeItem[]) allVisible.toArray(new TreeItem[allVisible.size()]));
69         } else if (fViewer instanceof TableViewer) {
70             ((TableViewer) fViewer).getTable().selectAll();
71             // force viewer selection change
72
fViewer.setSelection(fViewer.getSelection());
73         }
74     }
75 }
76
Popular Tags