Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 35c76d28b81e60c28035c36d3a4902fc616f436d (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
/*******************************************************************************
 * Copyright (c) 2010, 2013 IBM Corporation 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:
 *     René Brandstetter - Bug 411821 - [QuickAccess] Contribute SearchField
 *                                      through a fragment or other means
 ******************************************************************************/

package org.eclipse.e4.ui.model.internal;

/**
 * A holder class for the full information to position an element in a list.
 * 
 * @author René Brandstetter
 */
public final class PositionInfo {
  /** The position type to use. */
  private final Position position;

  /**
   * The additional positioning information which can be used to position an
   * element relative to another element.
   */
  private final String positionReference;

  /**
   * The {@link PositionInfo} which represent an insert at the beginning of
   * the list.
   */
  public static final PositionInfo FIRST = new PositionInfo(Position.FIRST, null);

  /**
   * The {@link PositionInfo} which represent an insert at the end of the
   * list.
   */
  public static final PositionInfo LAST = new PositionInfo(Position.LAST, null);

  /**
   * Creates an instance of the PositionInfo.
   * 
   * @param position
   *          the kind of the positioning
   * @param positionReference
   *          additional information which is need to position an element
   *          (e.g.: index, ID of another element)
   * @throws NullPointerException
   *           if the <code>position</code> is <code>null</code>
   */
  public PositionInfo(Position position, String positionReference) {
    if (position == null) {
      throw new NullPointerException("No position given!");
    }

    this.position = position;
    this.positionReference = positionReference;
  }

  /**
   * Returns the kind/type of positioning which should be used.
   * 
   * @return the position
   */
  public Position getPosition() {
    return position;
  }

  /**
   * Returns additional information which is needed to place an element.
   * 
   * @return the positionReference, or <code>null</code> if no additional information is given
   */
  public String getPositionReference() {
    return positionReference;
  }

  /**
   * Returns the additional information which is needed to place an element as
   * an int.
   * 
   * @return the positionReference as an int
   * @throws NumberFormatException
   *           if the {@link #positionReference} can't be parsed to an int
   * @throws NullPointerException
   *           if the {@link #positionReference} is <code>null</code>
   */
  public int getPositionReferenceAsInteger() {
    return Integer.parseInt(positionReference);
  }

  /**
   * Creates a {@link PositionInfo} object out of the given positioning
   * string.
   * 
   * <p>
   * <b>Examples for a positioning string:</b>
   * <ul>
   * <li><code>last</code> - place an element to the end of a list</li>
   * <li><code>first</code> - place an element to the beginning of a list</li>
   * <li><code>index:3</code> - place an element at the provided index 3 in a
   * list</li>
   * <li><code>before:org.eclipse.test.id</code> - place an element in a list
   * in front of the element with the ID "org.eclipse.test.id"</li>
   * <li><code>after:org.eclipse.test.id</code> - place an element in a list
   * after the element with the ID "org.eclipse.test.id"</li>
   * </ul>
   * </p>
   * 
   * @param positionInfo
   *          the positioning string
   * @return a {@link PositionInfo} which holds all the data mentioned in the
   *         positioning string, or <code>null</code> if the positioning
   *         string doesn't hold a positioning information
   */
  public static PositionInfo parse(String positionInfo) {
    Position position = Position.find(positionInfo);
    if (position != null) {
      switch (position) {
        case FIRST:
          return PositionInfo.FIRST;

        case LAST:
          return PositionInfo.LAST;

        default:
          return new PositionInfo(position, positionInfo.substring(position.prefix.length()));
      }
    }

    return null;
  }

  @Override
  public String toString() {
    StringBuilder back = new StringBuilder(position.prefix);
    if (positionReference != null) {
      back.append(positionReference);
    }
    return back.toString();
  }
}

Back to the top