001    /*
002     * Jpkg - Java library and tools for operating system package creation.
003     *
004     * Copyright (c) 2007 Three Rings Design, Inc.
005     * All rights reserved.
006     *
007     * Redistribution and use in source and binary forms, with or without
008     * modification, are permitted provided that the following conditions
009     * are met:
010     * 1. Redistributions of source code must retain the above copyright
011     *    notice, this list of conditions and the following disclaimer.
012     * 2. Redistributions in binary form must reproduce the above copyright
013     *    notice, this list of conditions and the following disclaimer in the
014     *    documentation and/or other materials provided with the distribution.
015     * 3. Neither the name of the copyright owner nor the names of contributors
016     *    may be used to endorse or promote products derived from this software
017     *    without specific prior written permission.
018     *
019     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
020     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
021     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022     * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
023     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
024     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
025     * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
026     * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
027     * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028     * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
029     * POSSIBILITY OF SUCH DAMAGE.
030     */
031    package com.threerings.jpkg.ant.dpkg.info;
032    
033    import com.threerings.antidote.field.text.SingleLineTextField;
034    import com.threerings.antidote.property.IntegerProperty;
035    import com.threerings.antidote.property.StringProperty;
036    import com.threerings.jpkg.debian.ControlDataInvalidException;
037    import com.threerings.jpkg.debian.PackageVersion;
038    
039    import static com.threerings.antidote.MutabilityHelper.areMutablesSet;
040    import static com.threerings.antidote.MutabilityHelper.requiresValidation;
041    
042    
043    /**
044     * Stores the <info> <version> field, which is the package version.
045     * @see PackageVersion
046     */
047    public class Version extends SingleLineTextField
048    {
049        // from Field
050        public String getFieldName ()
051        {
052            return "version";
053        }
054    
055        /**
056         * Ant setter field: epoch.
057         */
058        public void setEpoch (String value)
059        {
060            _epoch.setValue(value);
061        }
062    
063        /**
064         * Ant setter field: debianVersion.
065         */
066        public void setDebianVersion (String value)
067        {
068            _debianVersion.setValue(value);
069        }
070    
071        /**
072         * Returns the user data converted into a {@link PackageVersion}. Cannot be called before validate().
073         */
074        public PackageVersion getPackageVersion ()
075        {
076            requiresValidation(_packageVersion);
077            return _packageVersion;
078        }
079    
080        @Override // from SingleLineTextField
081        protected void validateTextField ()
082        {
083            try {
084                // check the optional properties
085                switch (areMutablesSet(_epoch, _debianVersion)) {
086                    case ALL_UNSET:
087                        // the optional fields are not set, construct the simple version
088                        _packageVersion = new PackageVersion(getText());
089                        return;
090    
091                    case SOME_UNSET:
092                        reportUnsetDependentProperties(_epoch, _debianVersion);
093                        reportUnsetDependentProperties(_debianVersion, _epoch);
094                        return;
095    
096                    case ALL_SET:
097                        // continue to validation
098                        break;
099                }
100    
101                // validate the optional properties
102                switch (validateProperties(_debianVersion, _epoch)) {
103                    case SOME_INVALID:
104                    case ALL_INVALID:
105                        return;
106    
107                    case ALL_VALID:
108                        // construct the complex version
109                        _packageVersion = new PackageVersion(
110                            getText(), _debianVersion.getValue(), _epoch.getValue());
111                        return;
112                }
113    
114            } catch (final ControlDataInvalidException cdie) {
115                appendViolation(new ControlDataViolation(this, cdie));
116            }
117        }
118    
119        /** The PackageVersion object representing the user supplied data. */
120        private PackageVersion _packageVersion;
121    
122        /** Ant adder/setter fields. */
123        private final StringProperty _debianVersion = new StringProperty("debianversion", this);
124        private final IntegerProperty _epoch = new IntegerProperty("epoch", this);
125    }