KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > 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.jdt.internal.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17
18 import org.eclipse.swt.widgets.Tree;
19 import org.eclipse.swt.widgets.TreeItem;
20
21 import org.eclipse.jface.action.Action;
22 import org.eclipse.jface.viewers.StructuredViewer;
23 import org.eclipse.jface.viewers.TableViewer;
24 import org.eclipse.jface.viewers.TreeViewer;
25
26 import org.eclipse.ui.PlatformUI;
27
28 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
29  
30 /**
31  * This action selects all entries currently showing in view.
32  */

33 public class SelectAllAction extends Action {
34
35     private StructuredViewer fViewer;
36
37     /**
38      * Creates the action for a TreeViewer
39      */

40     public SelectAllAction(TreeViewer viewer) {
41         this((StructuredViewer) viewer);
42     }
43     
44     /**
45      * Creates the action for a TableViewer
46      */

47     public SelectAllAction(TableViewer viewer) {
48         this((StructuredViewer) viewer);
49     }
50
51     private SelectAllAction(StructuredViewer viewer) {
52         super("selectAll"); //$NON-NLS-1$
53
setText(ActionMessages.SelectAllAction_label);
54         setToolTipText(ActionMessages.SelectAllAction_tooltip);
55         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.SELECT_ALL_ACTION);
56         Assert.isNotNull(viewer);
57         fViewer= viewer;
58     }
59     
60     private void collectExpandedAndVisible(TreeItem[] items, List JavaDoc result) {
61         for (int i= 0; i < items.length; i++) {
62             TreeItem item= items[i];
63             if (item.getData() != null) {
64                 result.add(item);
65                 if (item.getExpanded()) {
66                     collectExpandedAndVisible(item.getItems(), result);
67                 }
68             }
69         }
70     }
71
72     /**
73      * Selects all resources in the view.
74      */

75     public void run() {
76         if (fViewer instanceof TreeViewer) {
77             ArrayList JavaDoc allVisible= new ArrayList JavaDoc();
78             Tree tree= ((TreeViewer) fViewer).getTree();
79             collectExpandedAndVisible(tree.getItems(), allVisible);
80             tree.setSelection((TreeItem[]) allVisible.toArray(new TreeItem[allVisible.size()]));
81             fViewer.setSelection(fViewer.getSelection());
82         } else if (fViewer instanceof TableViewer) {
83             ((TableViewer) fViewer).getTable().selectAll();
84             // force viewer selection change
85
fViewer.setSelection(fViewer.getSelection());
86         }
87     }
88 }
89
Popular Tags