~ubuntu-branches/ubuntu/utopic/eclipse-eclox/utopic

« back to all changes in this revision

Viewing changes to eclox.ui/src/eclox/ui/editor/advanced/NavigableSelection.java

  • Committer: Package Import Robot
  • Author(s): Graham Inggs
  • Date: 2013-07-07 20:33:10 UTC
  • Revision ID: package-import@ubuntu.com-20130707203310-a44yw80gqtc2s9ob
Tags: upstream-0.10.0
ImportĀ upstreamĀ versionĀ 0.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (C) 2003-2007, 2013, Guillaume Brocker
 
3
 * 
 
4
 * All rights reserved. This program and the accompanying materials
 
5
 * are made available under the terms of the Eclipse Public License v1.0
 
6
 * which accompanies this distribution, and is available at
 
7
 * http://www.eclipse.org/legal/epl-v10.html
 
8
 *
 
9
 * Contributors:
 
10
 *     Guillaume Brocker - Initial API and implementation
 
11
 *
 
12
 ******************************************************************************/ 
 
13
 
 
14
package eclox.ui.editor.advanced;
 
15
 
 
16
import java.util.Iterator;
 
17
import java.util.List;
 
18
import java.util.Stack;
 
19
import java.util.Vector;
 
20
 
 
21
import org.eclipse.jface.viewers.IStructuredSelection;
 
22
 
 
23
/**
 
24
 * Implements a selection that provides the history of all selected
 
25
 * items as well as the navigation among those items.
 
26
 * 
 
27
 * @author gbrocker
 
28
 */
 
29
public class NavigableSelection implements IStructuredSelection {
 
30
        
 
31
        private Stack   previousElements = new Stack(); ///< Contains all previously selected elements.
 
32
        private Stack   nextElements = new Stack();             ///< Contains all next selected elements.
 
33
        private Vector  elements = new Vector();                ///< References the current element of the selection.
 
34
        
 
35
        /**
 
36
         * @brief       Builds a new selection that holds the given object.
 
37
         * 
 
38
         * @param       object  the new object to select
 
39
         * 
 
40
         * @return      the new selection
 
41
         */
 
42
        public NavigableSelection select(Object object) {
 
43
                NavigableSelection      result;
 
44
                if( getFirstElement() != object ) {
 
45
                        result = new NavigableSelection();
 
46
                        if( isEmpty() == false ) {
 
47
                                result.previousElements.addAll(previousElements);
 
48
                                result.previousElements.push(elements.firstElement());
 
49
                        }
 
50
                        result.elements.add(object);
 
51
                }
 
52
                else {
 
53
                        result = this;
 
54
                }
 
55
                return result;
 
56
        }
 
57
 
 
58
        /**
 
59
         * @brief       Retrieves the collection of items that follow the current item
 
60
         *                      in the history.
 
61
         * 
 
62
         * @return      a stack of items
 
63
         */
 
64
        public Stack getNextElements() {
 
65
                return nextElements;
 
66
        }
 
67
        
 
68
        /**
 
69
         * @brief       Builds the selection that follows in the history.
 
70
         * 
 
71
         * @return      a selection or null if none
 
72
         */
 
73
        public NavigableSelection getNextSelection() {
 
74
                NavigableSelection result = null;
 
75
                if( nextElements.empty() == false ) {
 
76
                        result = new NavigableSelection();
 
77
                        result.previousElements.addAll(previousElements);
 
78
                        result.previousElements.push(elements.firstElement());
 
79
                        result.elements.add(nextElements.peek());
 
80
                        result.nextElements.addAll(nextElements);
 
81
                        result.nextElements.pop();
 
82
                }
 
83
                return result;
 
84
        }
 
85
        
 
86
        /**
 
87
         * @brief       Retrieves the collection of items that preceed the current item
 
88
         *                      in the history.
 
89
         * 
 
90
         * @return      a stack of items
 
91
         */
 
92
        public Stack getPreviousElements() {
 
93
                return previousElements;
 
94
        }
 
95
        
 
96
        /**
 
97
         * @brief       Builds the selection that preceeds in the history.
 
98
         * 
 
99
         * @return      a selection or null if none
 
100
         */
 
101
        public NavigableSelection getPreviousSelection() {
 
102
                NavigableSelection result = null;
 
103
                if( previousElements.empty() == false ) {
 
104
                        result = new NavigableSelection();
 
105
                        result.previousElements.addAll(previousElements);
 
106
                        result.previousElements.pop();
 
107
                        result.elements.add(previousElements.peek());
 
108
                        result.nextElements.addAll(nextElements);
 
109
                        result.nextElements.push(elements.firstElement());
 
110
                }
 
111
                return result;
 
112
        }
 
113
        
 
114
        /**
 
115
         * @see org.eclipse.jface.viewers.ISelection#isEmpty()
 
116
         */
 
117
        public boolean isEmpty() {
 
118
                return elements.isEmpty();
 
119
        }
 
120
        
 
121
        /**
 
122
         * Retrieves the selected element.
 
123
         * 
 
124
         * @see org.eclipse.jface.viewers.IStructuredSelection#getFirstElement()
 
125
         */
 
126
        public Object getFirstElement() {
 
127
                return elements.isEmpty() ? null : elements.firstElement();
 
128
        }
 
129
 
 
130
        /**
 
131
         * @see org.eclipse.jface.viewers.IStructuredSelection#iterator()
 
132
         */
 
133
        public Iterator iterator() {
 
134
                return elements.iterator();
 
135
        }
 
136
 
 
137
        /**
 
138
         * @see org.eclipse.jface.viewers.IStructuredSelection#size()
 
139
         */
 
140
        public int size() {
 
141
                return elements.size();
 
142
        }
 
143
 
 
144
        /**
 
145
         * @see org.eclipse.jface.viewers.IStructuredSelection#toArray()
 
146
         */
 
147
        public Object[] toArray() {
 
148
                return elements.toArray();
 
149
        }
 
150
 
 
151
        /**
 
152
         * @see org.eclipse.jface.viewers.IStructuredSelection#toList()
 
153
         */
 
154
        public List toList() {
 
155
                return new Vector(elements);
 
156
        }
 
157
 
 
158
}