View Javadoc

1   package cz.cuni.amis.pogamut.sposh.elements;
2   
3   import java.util.List;
4   import java.util.NoSuchElementException;
5   
6   /**
7    * This is class used as intermediate class between PoshElement and DataNodes of
8    * POSH elements. Some common functions are implemented here.
9    *
10   * @author HonzaH
11   */
12  public abstract class PoshDummyElement extends PoshElement {
13  
14      /**
15       * Regular pattern, only text that maches this pattern can be a name of the
16       * POSH element. This pattern must be same as token
17       * <code>NAME</code> in the PoshParser.jj
18       * <p/>
19       * Because of dash (-), not all strings that match this pattern are FQN.
20       */
21      public static final String IDENT_PATTERN = "([a-zA-Z][_\\-a-zA-Z0-9]*\\.)*[a-zA-Z][_\\-a-zA-Z0-9]*";
22  
23      /**
24       * Function for moving an child element in the list of children. When node
25       * wants to change position of a child, it must first change its position in
26       * the data structures of the node and then it(the parent node of a child)
27       * must notify its listeners that a child has moved. The child is not
28       * notified (unless it has a listener on its parent).
29       * <p/>
30       * This method computes new index of a child in the collection and moves it
31       * there. The child will be at the new position and the rest of elements
32       * will move in such way that they will stay in the same order as before
33       * (imagine it as a single line of balls, you take out specified ball and
34       * put it at new position and other balls will move accordingly). Once all
35       * elements are in their proper places, the method emits a notification
36       * about movement of a child.
37       *
38       * @param collection list of elements, child must be one of them
39       * @param child child element that will be moved
40       * @param relativePosition how much will child move, how much will index of
41       * a child change (new_idx = org_inx + relativePosition).
42       * @return true if child has been moved, false if new position has been
43       * illegal
44       * @throws NoSuchElementException if child is not in the collection.
45       */
46      protected final boolean moveNodeInList(List collection, PoshElement child, int relativePosition) {
47          // find index of child
48          int childIndex = collection.indexOf(child);
49          if (childIndex == -1) {
50              throw new NoSuchElementException("Child " + child + "is not in collection.");
51          }
52          int bustPos;
53          if (relativePosition > 0) {
54              bustPos = childIndex + relativePosition + 1;
55          } else {
56              bustPos = childIndex + relativePosition;
57          }
58          // check if move is even possible
59          if (bustPos < 0 || bustPos > collection.size()) {
60              return false;
61          }
62  
63          collection.add(bustPos, child);
64  
65          // remove former child
66          if (relativePosition > 0) {
67              collection.remove(childIndex);
68          } else {
69              collection.remove(childIndex + 1);
70          }
71  
72          emitChildMove(child, relativePosition);
73          return true;
74      }
75  
76      /**
77       * Is passed name used by one of elements?
78       *
79       * @param name name to be tested
80       * @param elements elements againt which the test is done
81       * @return true if name is used, false otherwise
82       */
83      protected static boolean isUsedName(String name, List<? extends NamedLapElement> elements) {
84          for (NamedLapElement element : elements) {
85              String elementName = element.getName();
86              if (elementName.equals(name)) {
87                  return true;
88              }
89          }
90          return false;
91      }
92  
93      /**
94       * Get valid name for new element, but name can't be same as any name used
95       * by passed elements.
96       *
97       * @param template string used as prefix of created name
98       * @param elements elements that must have different name that created name
99       * @return valid name that can be immediately used as OK name of new drive.
100      */
101     protected static String getUnusedName(String template, List<? extends NamedLapElement> elements) {
102         int i = 1;
103         while (isUsedName(template + i, elements)) {
104             i++;
105         }
106         return template + i;
107     }
108 }