kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 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 | *******************************************************************************/ |
kchong | 2be71b3 | 2006-04-11 16:32:03 +0000 | [diff] [blame] | 11 | package org.eclipse.wst.xsd.ui.internal.common.commands; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 12 | |
| 13 | import org.eclipse.xsd.XSDFactory; |
| 14 | import org.eclipse.xsd.XSDMaxExclusiveFacet; |
| 15 | import org.eclipse.xsd.XSDMaxInclusiveFacet; |
| 16 | import org.eclipse.xsd.XSDMinExclusiveFacet; |
| 17 | import org.eclipse.xsd.XSDMinInclusiveFacet; |
| 18 | import org.eclipse.xsd.XSDSimpleTypeDefinition; |
| 19 | |
| 20 | public 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() |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 59 | { |
| 60 | try |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 61 | { |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 62 | beginRecording(xsdSimpleType.getElement()); |
| 63 | |
| 64 | if (doUpdateMin) |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 65 | { |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 66 | if (includeMin) |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 67 | { |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 68 | if (minInclusiveFacet == null && min != null) |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 69 | { |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 70 | 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 | } |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 78 | } |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 79 | else if (minInclusiveFacet != null && min != null) |
| 80 | { |
| 81 | minInclusiveFacet.setLexicalValue(min); |
| 82 | } |
| 83 | else if (minInclusiveFacet != null && min == null) |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 84 | { |
| 85 | xsdSimpleType.getFacetContents().remove(minInclusiveFacet); |
| 86 | } |
| 87 | } |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 88 | else |
| 89 | // !includeMin |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 90 | { |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 91 | if (minExclusiveFacet == null && min != null) |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 92 | { |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 93 | 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); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 111 | } |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 112 | else if (doUpdateMax) |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 113 | { |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 114 | if (includeMax) |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 115 | { |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 116 | 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) |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 132 | { |
| 133 | xsdSimpleType.getFacetContents().remove(maxInclusiveFacet); |
| 134 | } |
| 135 | } |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 136 | else |
| 137 | // !includeMax |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 138 | { |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 139 | 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 | } |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 160 | |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 161 | formatChild(xsdSimpleType.getElement()); |
| 162 | } |
| 163 | finally |
| 164 | { |
| 165 | endRecording(); |
| 166 | } |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 167 | } |
| 168 | } |