BorderMapper.java 13.3 KB
Newer Older
P
peterz 已提交
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
/*
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package org.jdesktop.synthdesigner.synthmodel.jibxhelpers;

import org.jibx.runtime.IMarshaller;
import org.jibx.runtime.IUnmarshaller;
import org.jibx.runtime.IAliasable;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;
import org.jibx.runtime.IMarshallingContext;
import org.jibx.runtime.impl.MarshallingContext;
import org.jibx.runtime.impl.UnmarshallingContext;
import org.jdesktop.swingx.designer.jibxhelpers.ColorMapper;

import javax.swing.border.LineBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.Border;
import javax.swing.BorderFactory;
import java.awt.Insets;
import java.awt.Color;
import org.jdesktop.synthdesigner.synthmodel.PainterBorder;

/**
 * BorderMapper - JIBX xml mapper for swing standard borders
 *
 * @author Jasper Potts
 */
public class BorderMapper implements IMarshaller, IUnmarshaller, IAliasable {
    private static enum BorderType {
        empty, line, etched, bevel, matte, compound, painter
    }
    private static enum SubType {
        raised(EtchedBorder.RAISED), lowered(EtchedBorder.LOWERED);
        private int subtype;

        SubType(int type) {
            this.subtype = type;
        }

        public int getSubType() {
            return subtype;
        }
    }
    private static final String ELEMENT_NAME = "border";
    private static final String TYPE_NAME = "type";
    private static final String SUB_TYPE_NAME = "subtype";
    private static final String TOP_NAME = "top";
    private static final String BOTTOM_NAME = "bottom";
    private static final String LEFT_NAME = "left";
    private static final String RIGHT_NAME = "right";
    private static final String THICKNESS_NAME = "thickness";
    private static final String INSIDE_NAME = "inside";
    private static final String OUTSIDE_NAME = "outside";
    private static final String PAINTER_NAME = "painter";

    private String uri;
    private int index;
    private String name;

    public BorderMapper() {
        uri = null;
        index = 0;
        name = ELEMENT_NAME;
    }

    public BorderMapper(String uri, int index, String name) {
        this.uri = uri;
        this.index = index;
        this.name = name;
    }

    public boolean isExtension(int i) {
        return false;
    }

    public boolean isPresent(IUnmarshallingContext iUnmarshallingContext) throws
            JiBXException {
        return iUnmarshallingContext.isAt(uri, ELEMENT_NAME);
    }

    public void marshal(Object object, IMarshallingContext iMarshallingContext)
            throws JiBXException {
        if (!(iMarshallingContext instanceof MarshallingContext)) {
            throw new JiBXException("Invalid object type for marshaller");
        } else {
            MarshallingContext ctx = (MarshallingContext) iMarshallingContext;
            if (object instanceof PainterBorder) {
                PainterBorder border = (PainterBorder) object;
                Insets insets = border.getBorderInsets();
                ctx.startTagAttributes(index, name)
                        .attribute(index, TYPE_NAME, BorderType.painter.toString())
                        .attribute(index, PAINTER_NAME, border.getPainterName())
                        .attribute(index, TOP_NAME, insets.top)
                        .attribute(index, BOTTOM_NAME, insets.bottom)
                        .attribute(index, LEFT_NAME, insets.left)
                        .attribute(index, RIGHT_NAME, insets.right)
                        .closeStartContent();
                ctx.endTag(index, name);
            } else if (object instanceof EmptyBorder) {
                Insets insets = ((EmptyBorder) object).getBorderInsets();
                ctx.startTagAttributes(index, name)
                        .attribute(index, TYPE_NAME,
                                BorderType.empty.toString())
                        .attribute(index, TOP_NAME, insets.top)
                        .attribute(index, BOTTOM_NAME, insets.bottom)
                        .attribute(index, LEFT_NAME, insets.left)
                        .attribute(index, RIGHT_NAME, insets.right)
                        .closeStartEmpty();
            } else if (object instanceof LineBorder) {
                LineBorder border = (LineBorder) object;
                ctx.startTagAttributes(index, name).
                        attribute(index, TYPE_NAME, BorderType.line.toString()).
                        attribute(index, THICKNESS_NAME, border.getThickness()).
                        closeStartContent();
                new ColorMapper().marshal(border.getLineColor(), ctx);
                ctx.endTag(index, name);
            } else if (object instanceof EtchedBorder) {
                EtchedBorder border = (EtchedBorder) object;
                ctx.startTagAttributes(index, name).
                        attribute(index, TYPE_NAME,
                                BorderType.etched.toString()).
                        attribute(index, SUB_TYPE_NAME,
                                border.getEtchType()==EtchedBorder.RAISED?
                                        SubType.raised.toString():
                                        SubType.lowered.toString()).
                        closeStartContent();
                new ColorMapper().marshal(border.getHighlightColor(), ctx);
                new ColorMapper().marshal(border.getShadowColor(), ctx);
                ctx.endTag(index, name);
            } else if (object instanceof BevelBorder) {
                BevelBorder border = (BevelBorder) object;
                ctx.startTagAttributes(index, name).
                        attribute(index, TYPE_NAME,
                                BorderType.bevel.toString()).
                        attribute(index, SUB_TYPE_NAME,
                                border.getBevelType()==BevelBorder.RAISED?
                                        SubType.raised.toString():
                                        SubType.lowered.toString()).
                        closeStartContent();
                new ColorMapper().marshal(border.getHighlightInnerColor(), ctx);
                new ColorMapper().marshal(border.getHighlightOuterColor(), ctx);
                new ColorMapper().marshal(border.getShadowInnerColor(), ctx);
                new ColorMapper().marshal(border.getHighlightOuterColor(), ctx);
                ctx.endTag(index, name);
            } else if (object instanceof MatteBorder) {
                MatteBorder border = (MatteBorder) object;
                Insets insets = ((EmptyBorder) object).getBorderInsets();
                ctx.startTagAttributes(index, name)
                        .attribute(index, TYPE_NAME,
                                BorderType.matte.toString())
                        .attribute(index, TOP_NAME, insets.top)
                        .attribute(index, BOTTOM_NAME, insets.bottom)
                        .attribute(index, LEFT_NAME, insets.left)
                        .attribute(index, RIGHT_NAME, insets.right)
                        .closeStartContent();
                new ColorMapper().marshal(border.getMatteColor(), ctx);
                // todo: we should support tiled icons here to be 100% complete
                ctx.endTag(index, name);
            } else if (object instanceof CompoundBorder) {
                CompoundBorder border = (CompoundBorder) object;
                ctx.startTagAttributes(index, name)
                        .attribute(index, TYPE_NAME,
                                BorderType.compound.toString())
                        .closeStartContent();
                new BorderMapper(null,0, INSIDE_NAME).marshal(border.getInsideBorder(),ctx);
                new BorderMapper(null,0, OUTSIDE_NAME).marshal(border.getOutsideBorder(),ctx);
                ctx.endTag(index, name);
            } else {
                throw new JiBXException("Invalid object type for marshaller");
            }
        }
    }

    public Object unmarshal(Object object,
                            IUnmarshallingContext iUnmarshallingContext)
            throws JiBXException {
        Border border = null;
        // make sure we're at the appropriate start tag
        UnmarshallingContext ctx = (UnmarshallingContext) iUnmarshallingContext;
        if (!ctx.isAt(uri, name)) {
            ctx.throwStartTagNameError(uri, name);
        }
        // get type
        BorderType type = BorderType.valueOf(ctx.attributeText(uri, TYPE_NAME)
                .toLowerCase());
        int top,bottom,left,right;
        Color color;
        switch(type){
            case empty:
                top = ctx.attributeInt(uri, TOP_NAME, index);
                bottom = ctx.attributeInt(uri, BOTTOM_NAME, index);
                left = ctx.attributeInt(uri, LEFT_NAME, index);
                right = ctx.attributeInt(uri, RIGHT_NAME, index);
                border = BorderFactory.createEmptyBorder(top,left,bottom,right);
                break;
            case line:
                int thickness = ctx.attributeInt(uri, THICKNESS_NAME, index);
                ctx.parsePastStartTag(uri,name);
                color = (Color)new ColorMapper().unmarshal(null,ctx);
                border = BorderFactory.createLineBorder(color,thickness);
                break;
            case etched:
                SubType etchedType = SubType.valueOf(
                        ctx.attributeText(uri, SUB_TYPE_NAME).toLowerCase());
                ctx.parsePastStartTag(uri,name);
                Color highColor = (Color)new ColorMapper()
                        .unmarshal(null,ctx);
                Color shadowColor = (Color)new ColorMapper()
                        .unmarshal(null,ctx);
                border = BorderFactory.createEtchedBorder(
                        etchedType.getSubType(),highColor,shadowColor);
                break;
            case bevel:
                SubType bevelType = SubType.valueOf(
                        ctx.attributeText(uri, SUB_TYPE_NAME).toLowerCase());
                ctx.parsePastStartTag(uri,name);
                Color innerHighColor = (Color)new ColorMapper()
                        .unmarshal(null,ctx);
                Color outerHighColor = (Color)new ColorMapper()
                        .unmarshal(null,ctx);
                Color innerShadowColor = (Color)new ColorMapper()
                        .unmarshal(null,ctx);
                Color outerShadowColor = (Color)new ColorMapper()
                        .unmarshal(null,ctx);
                border = BorderFactory.createBevelBorder(
                        bevelType.getSubType(),outerHighColor,innerHighColor,
                        outerShadowColor,innerShadowColor);
                break;
            case matte:
                top = ctx.attributeInt(uri, TOP_NAME, index);
                bottom = ctx.attributeInt(uri, BOTTOM_NAME, index);
                left = ctx.attributeInt(uri, LEFT_NAME, index);
                right = ctx.attributeInt(uri, RIGHT_NAME, index);
                ctx.parsePastStartTag(uri,name);
                color = (Color)new ColorMapper().unmarshal(null,ctx);
                border = BorderFactory.createMatteBorder(top,left,bottom,right,
                        color);
                break;
            case compound:
                ctx.parsePastStartTag(uri,name);
                Border inside = (Border) new BorderMapper(null,0, INSIDE_NAME)
                        .unmarshal(null,ctx);
                Border outside = (Border) new BorderMapper(null,0, OUTSIDE_NAME)
                        .unmarshal(null,ctx);
                border = BorderFactory.createCompoundBorder(outside, inside);
                break;
            case painter:
                String painterName = ctx.attributeText(uri, PAINTER_NAME);
                top = ctx.attributeInt(uri, TOP_NAME, index);
                bottom = ctx.attributeInt(uri, BOTTOM_NAME, index);
                left = ctx.attributeInt(uri, LEFT_NAME, index);
                right = ctx.attributeInt(uri, RIGHT_NAME, index);
                border = new PainterBorder(painterName, top, left, bottom, right);
        }
        ctx.parsePastEndTag(uri, name);
        return border;
    }
}