1
/*******************************************************************************
2
* Copyright (c) 2006 IBM Corporation.
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
9
* IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
10
*******************************************************************************/
12
package org.eclipse.linuxtools.internal.systemtap.ui.ide.actions;
14
import org.eclipse.jface.action.Action;
15
import org.eclipse.jface.viewers.ISelection;
16
import org.eclipse.jface.viewers.IStructuredSelection;
17
import org.eclipse.linuxtools.internal.systemtap.ui.ide.views.BrowserView;
18
import org.eclipse.ui.ISelectionListener;
19
import org.eclipse.ui.IWorkbenchPart;
20
import org.eclipse.ui.IWorkbenchWindow;
21
import org.eclipse.ui.PlatformUI;
25
* This <code>Action</code> expands or collapses the Viewer to the level of the element that the
27
* @author Henry Hughes
30
public class TreeExpandCollapseAction extends Action implements
32
private final IWorkbenchWindow fWindow;
33
private IStructuredSelection selection;
34
private final BrowserView viewer;
37
* The default constructor. Takes a <code>Class</code> representing the viewer that it is to expand
38
* or collapse, as there is only one in the workbench at a time.
39
* @param cls <code>Class</code> of the viewer to expand/collapse
41
public TreeExpandCollapseAction(BrowserView view) {
43
fWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
44
fWindow.getSelectionService().addSelectionListener(this);
49
* Updates <code>selection</code> with the current selection whenever the user changes
50
* the current selection.
53
public void selectionChanged(IWorkbenchPart part, ISelection incoming) {
54
if (incoming instanceof IStructuredSelection) {
55
selection = (IStructuredSelection) incoming;
56
setEnabled(selection.size() == 1);
58
// Other selections, for example containing text or of other kinds.
63
public void dispose() {
64
fWindow.getSelectionService().removeSelectionListener(this);
68
* The main body of the action. Expands or Collapses the viewer specified at construction to
69
* the level of the current selection.
73
ISelection incoming = viewer.getViewer().getSelection();
74
IStructuredSelection selection = (IStructuredSelection)incoming;
75
Object o = selection.getFirstElement();
81
Object[] objs = viewer.getViewer().getVisibleExpandedElements();
82
boolean doExpand = true;
84
for(int i = 0; i < objs.length; i++)
89
viewer.getViewer().expandToLevel(o,1);
91
viewer.getViewer().collapseToLevel(o,1);