blob: 0f5cea91e8b51d10cd23537aac717db7032826a6 [file] [log] [blame]
kchong38cbf172006-03-29 03:38:21 +00001/*******************************************************************************
2 * Copyright (c) 2001, 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
kchong2be71b32006-04-11 16:32:03 +000011package org.eclipse.wst.xsd.ui.internal.common.commands;
kchong38cbf172006-03-29 03:38:21 +000012
13import org.eclipse.xsd.XSDFactory;
14import org.eclipse.xsd.XSDMaxExclusiveFacet;
15import org.eclipse.xsd.XSDMaxInclusiveFacet;
16import org.eclipse.xsd.XSDMinExclusiveFacet;
17import org.eclipse.xsd.XSDMinInclusiveFacet;
18import org.eclipse.xsd.XSDSimpleTypeDefinition;
19
20public class UpdateNumericBoundsFacetCommand extends BaseCommand
21{
22 XSDSimpleTypeDefinition xsdSimpleType;
23 String max, min;
24 boolean includeMin, includeMax;
25 private boolean doUpdateMax = false, doUpdateMin = false;
26 XSDMinInclusiveFacet minInclusiveFacet;
27 XSDMinExclusiveFacet minExclusiveFacet;
28 XSDMaxInclusiveFacet maxInclusiveFacet;
29 XSDMaxExclusiveFacet maxExclusiveFacet;
30
31
32 public UpdateNumericBoundsFacetCommand(String label, XSDSimpleTypeDefinition xsdSimpleType, boolean includeMin, boolean includeMax)
33 {
34 super(label);
35 this.xsdSimpleType = xsdSimpleType;
36 this.includeMin = includeMin;
37 this.includeMax = includeMax;
38
39 minInclusiveFacet = xsdSimpleType.getMinInclusiveFacet();
40 minExclusiveFacet = xsdSimpleType.getMinExclusiveFacet();
41 maxInclusiveFacet = xsdSimpleType.getMaxInclusiveFacet();
42 maxExclusiveFacet = xsdSimpleType.getMaxExclusiveFacet();
43
44 }
45
46 public void setMin(String min)
47 {
48 this.min = min;
49 doUpdateMin = true;
50 }
51
52 public void setMax(String max)
53 {
54 this.max = max;
55 doUpdateMax = true;
56 }
57
58 public void execute()
kchong59966042006-08-21 21:09:18 +000059 {
60 try
kchong38cbf172006-03-29 03:38:21 +000061 {
kchong59966042006-08-21 21:09:18 +000062 beginRecording(xsdSimpleType.getElement());
63
64 if (doUpdateMin)
kchong38cbf172006-03-29 03:38:21 +000065 {
kchong59966042006-08-21 21:09:18 +000066 if (includeMin)
kchong38cbf172006-03-29 03:38:21 +000067 {
kchong59966042006-08-21 21:09:18 +000068 if (minInclusiveFacet == null && min != null)
kchong38cbf172006-03-29 03:38:21 +000069 {
kchong59966042006-08-21 21:09:18 +000070 minInclusiveFacet = XSDFactory.eINSTANCE.createXSDMinInclusiveFacet();
71 minInclusiveFacet.setLexicalValue(min);
72 xsdSimpleType.getFacetContents().add(minInclusiveFacet);
73
74 if (minExclusiveFacet != null)
75 {
76 xsdSimpleType.getFacetContents().remove(minExclusiveFacet);
77 }
kchong38cbf172006-03-29 03:38:21 +000078 }
kchong59966042006-08-21 21:09:18 +000079 else if (minInclusiveFacet != null && min != null)
80 {
81 minInclusiveFacet.setLexicalValue(min);
82 }
83 else if (minInclusiveFacet != null && min == null)
kchong38cbf172006-03-29 03:38:21 +000084 {
85 xsdSimpleType.getFacetContents().remove(minInclusiveFacet);
86 }
87 }
kchong59966042006-08-21 21:09:18 +000088 else
89 // !includeMin
kchong38cbf172006-03-29 03:38:21 +000090 {
kchong59966042006-08-21 21:09:18 +000091 if (minExclusiveFacet == null && min != null)
kchong38cbf172006-03-29 03:38:21 +000092 {
kchong59966042006-08-21 21:09:18 +000093 minExclusiveFacet = XSDFactory.eINSTANCE.createXSDMinExclusiveFacet();
94 minExclusiveFacet.setLexicalValue(min);
95 xsdSimpleType.getFacetContents().add(minExclusiveFacet);
96
97 if (minInclusiveFacet != null)
98 {
99 xsdSimpleType.getFacetContents().remove(minInclusiveFacet);
100 }
101 }
102 else if (minExclusiveFacet != null && min != null)
103 {
104 minExclusiveFacet.setLexicalValue(min);
105 }
106 else if (minExclusiveFacet != null && min == null)
107 {
108 xsdSimpleType.getFacetContents().remove(minExclusiveFacet);
kchong38cbf172006-03-29 03:38:21 +0000109 }
110 }
kchong38cbf172006-03-29 03:38:21 +0000111 }
kchong59966042006-08-21 21:09:18 +0000112 else if (doUpdateMax)
kchong38cbf172006-03-29 03:38:21 +0000113 {
kchong59966042006-08-21 21:09:18 +0000114 if (includeMax)
kchong38cbf172006-03-29 03:38:21 +0000115 {
kchong59966042006-08-21 21:09:18 +0000116 if (maxInclusiveFacet == null && max != null)
117 {
118 maxInclusiveFacet = XSDFactory.eINSTANCE.createXSDMaxInclusiveFacet();
119 maxInclusiveFacet.setLexicalValue(max);
120 xsdSimpleType.getFacetContents().add(maxInclusiveFacet);
121
122 if (maxExclusiveFacet != null)
123 {
124 xsdSimpleType.getFacetContents().remove(maxExclusiveFacet);
125 }
126 }
127 else if (maxInclusiveFacet != null && max != null)
128 {
129 maxInclusiveFacet.setLexicalValue(max);
130 }
131 else if (maxInclusiveFacet != null && max == null)
kchong38cbf172006-03-29 03:38:21 +0000132 {
133 xsdSimpleType.getFacetContents().remove(maxInclusiveFacet);
134 }
135 }
kchong59966042006-08-21 21:09:18 +0000136 else
137 // !includeMax
kchong38cbf172006-03-29 03:38:21 +0000138 {
kchong59966042006-08-21 21:09:18 +0000139 if (maxExclusiveFacet == null && max != null)
140 {
141 maxExclusiveFacet = XSDFactory.eINSTANCE.createXSDMaxExclusiveFacet();
142 maxExclusiveFacet.setLexicalValue(max);
143 xsdSimpleType.getFacetContents().add(maxExclusiveFacet);
144
145 if (maxInclusiveFacet != null)
146 {
147 xsdSimpleType.getFacetContents().remove(maxInclusiveFacet);
148 }
149 }
150 else if (maxExclusiveFacet != null && max != null)
151 {
152 maxExclusiveFacet.setLexicalValue(max);
153 }
154 else if (maxExclusiveFacet != null && max == null)
155 {
156 xsdSimpleType.getFacetContents().remove(maxExclusiveFacet);
157 }
kchong38cbf172006-03-29 03:38:21 +0000158 }
159 }
kchong38cbf172006-03-29 03:38:21 +0000160
kchong59966042006-08-21 21:09:18 +0000161 formatChild(xsdSimpleType.getElement());
162 }
163 finally
164 {
165 endRecording();
166 }
kchong38cbf172006-03-29 03:38:21 +0000167 }
168}