1
/*******************************************************************************
2
* Copyright (C) 2003-2007, 2013, Guillaume Brocker
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
10
* Guillaume Brocker - Initial API and implementation
12
******************************************************************************/
14
package eclox.ui.editor.advanced;
16
import java.util.Iterator;
17
import java.util.List;
18
import java.util.Stack;
19
import java.util.Vector;
21
import org.eclipse.jface.viewers.IStructuredSelection;
24
* Implements a selection that provides the history of all selected
25
* items as well as the navigation among those items.
29
public class NavigableSelection implements IStructuredSelection {
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.
36
* @brief Builds a new selection that holds the given object.
38
* @param object the new object to select
40
* @return the new selection
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());
50
result.elements.add(object);
59
* @brief Retrieves the collection of items that follow the current item
62
* @return a stack of items
64
public Stack getNextElements() {
69
* @brief Builds the selection that follows in the history.
71
* @return a selection or null if none
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();
87
* @brief Retrieves the collection of items that preceed the current item
90
* @return a stack of items
92
public Stack getPreviousElements() {
93
return previousElements;
97
* @brief Builds the selection that preceeds in the history.
99
* @return a selection or null if none
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());
115
* @see org.eclipse.jface.viewers.ISelection#isEmpty()
117
public boolean isEmpty() {
118
return elements.isEmpty();
122
* Retrieves the selected element.
124
* @see org.eclipse.jface.viewers.IStructuredSelection#getFirstElement()
126
public Object getFirstElement() {
127
return elements.isEmpty() ? null : elements.firstElement();
131
* @see org.eclipse.jface.viewers.IStructuredSelection#iterator()
133
public Iterator iterator() {
134
return elements.iterator();
138
* @see org.eclipse.jface.viewers.IStructuredSelection#size()
141
return elements.size();
145
* @see org.eclipse.jface.viewers.IStructuredSelection#toArray()
147
public Object[] toArray() {
148
return elements.toArray();
152
* @see org.eclipse.jface.viewers.IStructuredSelection#toList()
154
public List toList() {
155
return new Vector(elements);