Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0e66c357b7d2bbcb93258ca83ba6278d698f407b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*******************************************************************************
 * Copyright (c) 2012, 2013, 2014 Original authors and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Original authors and others - initial API and implementation
 *     Dirk Fauth - added ITraversalStrategy handling
 ******************************************************************************/
package org.eclipse.nebula.widgets.nattable.selection;

import org.eclipse.nebula.widgets.nattable.command.ILayerCommand;
import org.eclipse.nebula.widgets.nattable.command.ILayerCommandHandler;
import org.eclipse.nebula.widgets.nattable.layer.ILayer;
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer.MoveDirectionEnum;
import org.eclipse.nebula.widgets.nattable.selection.command.MoveSelectionCommand;

/**
 * Abstraction of the selection behavior during navigation in the grid.
 * Implementations of this class specify what to select when the selection moves
 * by responding to the {@link MoveSelectionCommand}.
 *
 * @param <T>
 *            The type of the {@link ILayerCommand} this
 *            {@link ILayerCommandHandler} handles. Needs to be a
 *            {@link MoveSelectionCommand} or subtype.
 *
 * @see MoveCellSelectionCommandHandler
 * @see MoveRowSelectionCommandHandler
 */
public abstract class MoveSelectionCommandHandler<T extends MoveSelectionCommand> implements ILayerCommandHandler<T> {

    /**
     * The SelectionLayer instance which is needed to perform selection
     * operations.
     */
    protected final SelectionLayer selectionLayer;

    /**
     * The strategy to use on traversal. Specifies the behavior when the
     * movement reaches a border.
     */
    protected final ITraversalStrategy traversalStrategy;

    /**
     * Create a MoveSelectionCommandHandler for the given {@link SelectionLayer}
     * . Uses the {@link ITraversalStrategy#AXIS_TRAVERSAL_STRATEGY} as default
     * strategy for selection movement.
     *
     * @param selectionLayer
     *            The {@link SelectionLayer} on which the selection should be
     *            performed.
     */
    public MoveSelectionCommandHandler(SelectionLayer selectionLayer) {
        this(selectionLayer, ITraversalStrategy.AXIS_TRAVERSAL_STRATEGY);
    }

    /**
     * Create a MoveSelectionCommandHandler for the given {@link SelectionLayer}
     * .
     *
     * @param selectionLayer
     *            The {@link SelectionLayer} on which the selection should be
     *            performed.
     * @param traversalStrategy
     *            The strategy that should be used for selection movements. Can
     *            not be <code>null</code>.
     */
    public MoveSelectionCommandHandler(SelectionLayer selectionLayer, ITraversalStrategy traversalStrategy) {
        if (traversalStrategy == null) {
            throw new IllegalArgumentException("You need to specify an ITraversalStrategy!"); //$NON-NLS-1$
        }
        this.selectionLayer = selectionLayer;
        this.traversalStrategy = traversalStrategy;
    }

    @Override
    public boolean doCommand(ILayer targetLayer, T command) {
        if (command.convertToTargetLayer(this.selectionLayer)) {
            moveSelection(command.getDirection(), getTraversalStrategy(command),
                    command.isShiftMask(), command.isControlMask());
            return true;
        }
        return false;
    }

    /**
     * Determines the {@link ITraversalStrategy} that should be used to move the
     * selection on handling the given command. The strategy is determined in
     * the following way:
     * <ol>
     * <li>Return the {@link ITraversalStrategy} carried by the command</li>
     * <li>If it doesn't contain a {@link ITraversalStrategy} but a carries a
     * dedicated step count, create a temporary {@link ITraversalStrategy} that
     * is configured with the locally configured {@link ITraversalStrategy} but
     * returns the step count carried by the command.</li>
     * <li>If the command doesn't carry a {@link ITraversalStrategy} and no
     * dedicated step count, the {@link ITraversalStrategy} registered with this
     * command handler is returned.</li>
     * </ol>
     *
     * @param command
     *            The current handled command.
     * @return The {@link ITraversalStrategy} that should be used to move the
     *         selection.
     */
    protected ITraversalStrategy getTraversalStrategy(final T command) {
        // if the command comes with a strategy we use it
        ITraversalStrategy result = command.getTraversalStrategy();

        if (result == null) {
            if (command.getStepSize() != null) {
                // command carries a step size, so we use the internal strategy
                // with the transported step size this is mainly for backwards
                // compatibility
                result = new ITraversalStrategy() {

                    @Override
                    public TraversalScope getTraversalScope() {
                        return MoveSelectionCommandHandler.this.traversalStrategy.getTraversalScope();
                    }

                    @Override
                    public boolean isCycle() {
                        return MoveSelectionCommandHandler.this.traversalStrategy.isCycle();
                    }

                    @Override
                    public int getStepCount() {
                        return command.getStepSize();
                    }
                };
            }
            else {
                result = this.traversalStrategy;
            }
        }

        return result;
    }

    /**
     * Moves the selection from the current position into the given move
     * direction.
     *
     * @param moveDirection
     *            The direction to move to.
     * @param traversalStrategy
     *            the traversal strategy to determine the number of steps to
     *            move and the behavior on moving over the border
     * @param withShiftMask
     *            boolean flag to indicate whether the shift key modifier is
     *            enabled or not
     * @param withControlMask
     *            boolean flag to indicate whether the control key modifier is
     *            enabled or not
     */
    protected void moveSelection(MoveDirectionEnum moveDirection, ITraversalStrategy traversalStrategy,
            boolean withShiftMask, boolean withControlMask) {

        switch (moveDirection) {
            case UP:
                moveLastSelectedUp(traversalStrategy, withShiftMask, withControlMask);
                break;
            case DOWN:
                moveLastSelectedDown(traversalStrategy, withShiftMask, withControlMask);
                break;
            case LEFT:
                moveLastSelectedLeft(traversalStrategy, withShiftMask, withControlMask);
                break;
            case RIGHT:
                moveLastSelectedRight(traversalStrategy, withShiftMask, withControlMask);
                break;
            default:
                break;
        }
    }

    /**
     * Moves the selection from the current position to the right.
     *
     * @param traversalStrategy
     *            the traversal strategy to determine the number of steps to
     *            move and the behavior on moving over the border
     * @param withShiftMask
     *            boolean flag to indicate whether the shift key modifier is
     *            enabled or not
     * @param withControlMask
     *            boolean flag to indicate whether the control key modifier is
     *            enabled or not
     */
    protected abstract void moveLastSelectedRight(ITraversalStrategy traversalStrategy,
            boolean withShiftMask, boolean withControlMask);

    /**
     * Moves the selection from the current position to the left.
     *
     * @param traversalStrategy
     *            the traversal strategy to determine the number of steps to
     *            move and the behavior on moving over the border
     * @param withShiftMask
     *            boolean flag to indicate whether the shift key modifier is
     *            enabled or not
     * @param withControlMask
     *            boolean flag to indicate whether the control key modifier is
     *            enabled or not
     */
    protected abstract void moveLastSelectedLeft(ITraversalStrategy traversalStrategy,
            boolean withShiftMask, boolean withControlMask);

    /**
     * Moves the selection from the current position up.
     *
     * @param traversalStrategy
     *            the traversal strategy to determine the number of steps to
     *            move and the behavior on moving over the border
     * @param withShiftMask
     *            boolean flag to indicate whether the shift key modifier is
     *            enabled or not
     * @param withControlMask
     *            boolean flag to indicate whether the control key modifier is
     *            enabled or not
     */
    protected abstract void moveLastSelectedUp(ITraversalStrategy traversalStrategy,
            boolean withShiftMask, boolean withControlMask);

    /**
     * Moves the selection from the current position down.
     *
     * @param traversalStrategy
     *            the traversal strategy to determine the number of steps to
     *            move and the behavior on moving over the border
     * @param withShiftMask
     *            boolean flag to indicate whether the shift key modifier is
     *            enabled or not
     * @param withControlMask
     *            boolean flag to indicate whether the control key modifier is
     *            enabled or not
     */
    protected abstract void moveLastSelectedDown(ITraversalStrategy traversalStrategy,
            boolean withShiftMask, boolean withControlMask);

}

Back to the top