~ubuntu-branches/ubuntu/utopic/eclipse-linuxtools/utopic

« back to all changes in this revision

Viewing changes to systemtap/org.eclipse.linuxtools.systemtap.ui.ide/src/org/eclipse/linuxtools/internal/systemtap/ui/ide/actions/TreeExpandCollapseAction.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2014-05-12 18:11:40 UTC
  • mfrom: (3.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20140512181140-w237r3vsah1tmybz
Tags: 2.2.1-1
* New upstream release.
* Refreshed d/patches.
* Removed eclipse-cdt-valgrind-remote package, all its functionality
  is now provided by eclipse-cdt-profiling-framework-remote.
* Added remove-license-feature.patch.
* Bump Standards-Version to 3.9.5.
* Enable eclipse-changelog package.
* Enable eclipse-rpm-editor package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 *
 
8
 * Contributors:
 
9
 *     IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
 
10
 *******************************************************************************/
 
11
 
 
12
package org.eclipse.linuxtools.internal.systemtap.ui.ide.actions;
 
13
 
 
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;
 
22
 
 
23
 
 
24
/**
 
25
 * This <code>Action</code> expands or collapses the Viewer to the level of the element that the
 
26
 * user selected.
 
27
 * @author Henry Hughes
 
28
 * @author Ryan Morse
 
29
 */
 
30
public class TreeExpandCollapseAction extends Action implements
 
31
                ISelectionListener {
 
32
        private final IWorkbenchWindow fWindow;
 
33
        private IStructuredSelection selection;
 
34
        private final BrowserView viewer;
 
35
 
 
36
        /**
 
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
 
40
         */
 
41
        public TreeExpandCollapseAction(BrowserView view) {
 
42
                super();
 
43
                fWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
 
44
                fWindow.getSelectionService().addSelectionListener(this);
 
45
                this.viewer = view;
 
46
        }
 
47
 
 
48
        /**
 
49
         * Updates <code>selection</code> with the current selection whenever the user changes
 
50
         * the current selection.
 
51
         */
 
52
        @Override
 
53
        public void selectionChanged(IWorkbenchPart part, ISelection incoming) {
 
54
                if (incoming instanceof IStructuredSelection) {
 
55
                        selection = (IStructuredSelection) incoming;
 
56
                        setEnabled(selection.size() == 1);
 
57
                } else {
 
58
                        // Other selections, for example containing text or of other kinds.
 
59
                        setEnabled(false);
 
60
                }
 
61
        }
 
62
 
 
63
        public void dispose() {
 
64
                fWindow.getSelectionService().removeSelectionListener(this);
 
65
        }
 
66
 
 
67
        /**
 
68
         * The main body of the action. Expands or Collapses the viewer specified at construction to
 
69
         * the level of the current selection.
 
70
         */
 
71
        @Override
 
72
        public void run() {
 
73
                ISelection incoming = viewer.getViewer().getSelection();
 
74
                IStructuredSelection selection = (IStructuredSelection)incoming;
 
75
                Object o = selection.getFirstElement();
 
76
 
 
77
                if(o == null) {
 
78
                        return;
 
79
                }
 
80
 
 
81
                Object[] objs = viewer.getViewer().getVisibleExpandedElements();
 
82
                boolean doExpand = true;
 
83
 
 
84
                for(int i = 0; i < objs.length; i++)
 
85
                        if(objs[i] == o)
 
86
                                doExpand = false;
 
87
 
 
88
                if(doExpand) {
 
89
                        viewer.getViewer().expandToLevel(o,1);
 
90
                } else {
 
91
                        viewer.getViewer().collapseToLevel(o,1);
 
92
                }
 
93
        }
 
94
}