Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 7e73ff5e9fbd30eb4a27175a5bdf19168ef0839e (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
/*******************************************************************************
 * Copyright (c) 2007 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.xsd.ui.internal.actions;

import java.util.Iterator;
import java.util.List;

import org.eclipse.xsd.XSDAttributeDeclaration;
import org.eclipse.xsd.XSDConcreteComponent;
import org.eclipse.xsd.XSDModelGroup;
import org.eclipse.xsd.XSDParticle;
import org.eclipse.xsd.XSDParticleContent;
import org.w3c.dom.Node;

public class MoveXSDElementAction extends MoveXSDBaseAction
{
  private static int INSERT_BEFORE = 0;
  private static int INSERT_AFTER = 1;
  private static int INSERT_DIRECT = 2;
  protected List selectedNodes;
  protected Node parentNode;
  protected Node previousRefChild, nextRefChild;
  int insertType;

  XSDModelGroup parentModelGroup;
  XSDConcreteComponent selected, previousRefComponent, nextRefComponent;
  boolean insertAtEnd = true;

  public MoveXSDElementAction(XSDModelGroup parentComponent, XSDConcreteComponent selected, XSDConcreteComponent previousRefChildComponent, XSDConcreteComponent nextRefChildComponent)
  {
    super();
    this.parentModelGroup = parentComponent;
    this.selected = selected;
    this.previousRefComponent = previousRefChildComponent;
    this.nextRefComponent = nextRefChildComponent;

    if (parentComponent == null)
      return;
    parentNode = parentComponent.getElement();
    nextRefChild = nextRefChildComponent != null ? nextRefChildComponent.getElement() : null;
    previousRefChild = previousRefChildComponent != null ? previousRefChildComponent.getElement() : null;

    if (nextRefComponent != null)
    {
      if (nextRefComponent.getContainer().getContainer() == parentModelGroup)
      {
        insertType = INSERT_BEFORE;
      }
    }
    if (previousRefComponent != null)
    {
      if (previousRefComponent.getContainer().getContainer() == parentModelGroup)
      {
        insertType = INSERT_AFTER;
      }
    }
    if (nextRefChildComponent == null && previousRefChildComponent == null)
    {
      insertType = INSERT_DIRECT;
    }
  }

  public MoveXSDElementAction(XSDModelGroup parentComponent, XSDConcreteComponent selected, XSDConcreteComponent previousRefChildComponent, XSDConcreteComponent nextRefChildComponent, boolean insertAtEnd)
  {
    this(parentComponent, selected, previousRefChildComponent, nextRefChildComponent);
    this.insertAtEnd = insertAtEnd;
  }
  
  public boolean canMove()
  {
    boolean result = true;
   
    if (nextRefComponent instanceof XSDAttributeDeclaration || previousRefComponent instanceof XSDAttributeDeclaration || parentModelGroup == null)
      return false;

    return result;
  }

  /*
   * @see IAction#run()
   */
  public void run()
  {
    int originalIndex = 0;
    for (Iterator particles = parentModelGroup.getContents().iterator(); particles.hasNext();)
    {
      XSDParticle particle = (XSDParticle) particles.next();
      XSDParticleContent particleContent = particle.getContent();
      if (particleContent == selected)
      {
        parentModelGroup.getContents().remove(particle);
        break;
      }
      originalIndex++;
    }
    int index = 0;
    boolean addedBack = false;
    if (insertType == INSERT_DIRECT)
    {
      XSDConcreteComponent container = selected.getContainer();
      if (container != null)
      {
        XSDConcreteComponent container2 = container.getContainer();
        if (container2 instanceof XSDModelGroup)
        {
          ((XSDModelGroup) container2).getContents().remove(container);
        }
        if (insertAtEnd)
          parentModelGroup.getContents().add(container);
        else
          parentModelGroup.getContents().add(0, container);
        addedBack = true;
      }
      return;
    }

    List particles = parentModelGroup.getContents();
    for (Iterator iterator = particles.iterator(); iterator.hasNext();)
    {
      XSDParticle particle = (XSDParticle) iterator.next();
      XSDParticleContent particleContent = particle.getContent();
      if (insertType == INSERT_BEFORE)
      {
        if (particleContent == nextRefComponent)
        {
          parentModelGroup.getContents().add(index, selected.getContainer());
          addedBack = true;
          break;
        }
        if (selected == nextRefComponent && originalIndex == index)
        {
          parentModelGroup.getContents().add(index, selected.getContainer());
          addedBack = true;
          break;
        }
      }
      else if (insertType == INSERT_AFTER)
      {
        if (particleContent == previousRefComponent)
        {
          parentModelGroup.getContents().add(index + 1, selected.getContainer());
          addedBack = true;
          break;
        }
        if (selected == previousRefComponent && originalIndex == index)
        {
          parentModelGroup.getContents().add(index, selected.getContainer());
          addedBack = true;
          break;
        }
      }
      index++;
    }
    if (particles.size() == 0)
    {
      parentModelGroup.getContents().add(selected.getContainer());
      addedBack = true;
    }

    if (!addedBack)
    {
      parentModelGroup.getContents().add(originalIndex, selected.getContainer());
    }
  }
}

Back to the top