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: a26649ce7a6f437afbd83eff18d1658c404643c4 (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
/*******************************************************************************
 * Copyright (c) 2001, 2006 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.adapters;

import org.eclipse.wst.xsd.ui.internal.adt.design.IAnnotationProvider;
import org.eclipse.wst.xsd.ui.internal.editor.Messages;
import org.eclipse.xsd.XSDConcreteComponent;
import org.eclipse.xsd.XSDParticle;

public class XSDParticleAdapter extends XSDBaseAdapter implements IAnnotationProvider
{
  public XSDParticleAdapter()
  {
    super();
  }

  public int getMaxOccurs()
  {
    return getMaxOccurs((XSDConcreteComponent) target);
  }

  public int getMinOccurs()
  {
    return getMinOccurs((XSDConcreteComponent) target);
  }

  public static int getMinOccurs(XSDConcreteComponent component)
  {
    int minOccur = -2;
    if (component != null)
    {
      Object o = component.getContainer();
      if (o instanceof XSDParticle)
      {
        if (((XSDParticle) o).isSetMinOccurs())
        {
          try
          {
            minOccur = ((XSDParticle) o).getMinOccurs();
          }
          catch (Exception e)
          {
          }
        }
      }
    }
    return minOccur;
  }

  public static int getMaxOccurs(XSDConcreteComponent component)
  {
    int maxOccur = -2;
    if (component != null)
    {
      Object o = component.getContainer();
      if (o instanceof XSDParticle)
      {
        if (((XSDParticle) o).isSetMaxOccurs())
        {
          try
          {
            maxOccur = ((XSDParticle) o).getMaxOccurs();
          }
          catch (Exception e)
          {
          }
        }
      }
    }
    return maxOccur;
  }

  public String getNameAnnotationString()
  {
    return buildAnnotationString(true);
  }

  public String getNameAnnotationToolTipString()
  {
    return buildAnnotationString(false);
  }

  public String getTypeAnnotationString()
  {
    return null;
  }

  public String getTypeAnnotationToolTipString()
  {
    return null;
  }

  protected String buildAnnotationString(boolean isForLabel)
  {
    String occurenceDescription = ""; //$NON-NLS-1$
    String toolTipDescription = ""; //$NON-NLS-1$
    // TODO: set int values as defined constants
    // -2 means the user didn't specify (so the default is 1)
    int minOccurs = getMinOccurs();
    int maxOccurs = getMaxOccurs();

    // This is for the attribute field case, which has no
    // occurrence attributes
    if (minOccurs == -3 && maxOccurs == -3)
    {
      occurenceDescription = ""; //$NON-NLS-1$
    }
    else if (minOccurs == 0 && (maxOccurs == -2 || maxOccurs == 1))
    {
      occurenceDescription = "[0..1]"; //$NON-NLS-1$
      toolTipDescription = Messages._UI_LABEL_OPTIONAL;
    }
    else if (minOccurs == 0 && maxOccurs == -1)
    {
      occurenceDescription = "[0..*]"; //$NON-NLS-1$
      toolTipDescription = Messages._UI_LABEL_ZERO_OR_MORE;
    }
    else if ((minOccurs == 1 && maxOccurs == -1) || (minOccurs == -2 && maxOccurs == -1))
    {
      occurenceDescription = "[1..*]"; //$NON-NLS-1$
      toolTipDescription = Messages._UI_LABEL_ONE_OR_MORE;
    }
    else if ((minOccurs == 1 && maxOccurs == 1) || (minOccurs == -2 && maxOccurs == 1) || (minOccurs == 1 && maxOccurs == -2))
    {
      occurenceDescription = "[1..1]"; //$NON-NLS-1$
      toolTipDescription = Messages._UI_LABEL_REQUIRED;
    }
    else if (minOccurs == -2 && maxOccurs == -2)
    {
      occurenceDescription = ""; //$NON-NLS-1$
      // none specified, so don't have any toolTip description
    }
    else
    {
      if (maxOccurs == -2)
        maxOccurs = 1;
      String maxSymbol = maxOccurs == -1 ? "*" : "" + maxOccurs; //$NON-NLS-1$ //$NON-NLS-2$
      String minSymbol = minOccurs == -2 ? "1" : "" + minOccurs; //$NON-NLS-1$ //$NON-NLS-2$
      occurenceDescription = "[" + minSymbol + ".." + maxSymbol + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      toolTipDescription = Messages._UI_LABEL_ARRAY;
    }

    if (isForLabel)
    {
      return occurenceDescription;
    }
    else
    {
      return toolTipDescription;
    }
  }

}

Back to the top