jdk/src/share/classes/javax/swing/text/rtf/RTFAttributes.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 438 2ae294e4518c
child 1299 027d966d5658
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 438
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text.rtf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import javax.swing.text.StyleConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.text.AttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.text.MutableAttributeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.text.TabStop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
class RTFAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static RTFAttribute attributes[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        Vector a = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        int CHR = RTFAttribute.D_CHARACTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        int PGF = RTFAttribute.D_PARAGRAPH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        int SEC = RTFAttribute.D_SECTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        int DOC = RTFAttribute.D_DOCUMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        int PST = RTFAttribute.D_META;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        Boolean True = Boolean.valueOf(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        Boolean False = Boolean.valueOf(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        a.addElement(new BooleanAttribute(CHR, StyleConstants.Italic, "i"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        a.addElement(new BooleanAttribute(CHR, StyleConstants.Bold, "b"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        a.addElement(new BooleanAttribute(CHR, StyleConstants.Underline, "ul"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        a.addElement(NumericAttribute.NewTwips(PGF, StyleConstants.LeftIndent, "li",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                        0f, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        a.addElement(NumericAttribute.NewTwips(PGF, StyleConstants.RightIndent, "ri",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                                        0f, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        a.addElement(NumericAttribute.NewTwips(PGF, StyleConstants.FirstLineIndent, "fi",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                                        0f, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        a.addElement(new AssertiveAttribute(PGF, StyleConstants.Alignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                            "ql", StyleConstants.ALIGN_LEFT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        a.addElement(new AssertiveAttribute(PGF, StyleConstants.Alignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                            "qr", StyleConstants.ALIGN_RIGHT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        a.addElement(new AssertiveAttribute(PGF, StyleConstants.Alignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                            "qc", StyleConstants.ALIGN_CENTER));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        a.addElement(new AssertiveAttribute(PGF, StyleConstants.Alignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                                            "qj", StyleConstants.ALIGN_JUSTIFIED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        a.addElement(NumericAttribute.NewTwips(PGF, StyleConstants.SpaceAbove,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                                        "sa", 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        a.addElement(NumericAttribute.NewTwips(PGF, StyleConstants.SpaceBelow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                        "sb", 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        a.addElement(new AssertiveAttribute(PST, RTFReader.TabAlignmentKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                            "tqr", TabStop.ALIGN_RIGHT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        a.addElement(new AssertiveAttribute(PST, RTFReader.TabAlignmentKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                            "tqc", TabStop.ALIGN_CENTER));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        a.addElement(new AssertiveAttribute(PST, RTFReader.TabAlignmentKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                            "tqdec", TabStop.ALIGN_DECIMAL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        a.addElement(new AssertiveAttribute(PST, RTFReader.TabLeaderKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                            "tldot", TabStop.LEAD_DOTS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        a.addElement(new AssertiveAttribute(PST, RTFReader.TabLeaderKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                            "tlhyph", TabStop.LEAD_HYPHENS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        a.addElement(new AssertiveAttribute(PST, RTFReader.TabLeaderKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                            "tlul", TabStop.LEAD_UNDERLINE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        a.addElement(new AssertiveAttribute(PST, RTFReader.TabLeaderKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                            "tlth", TabStop.LEAD_THICKLINE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        a.addElement(new AssertiveAttribute(PST, RTFReader.TabLeaderKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                            "tleq", TabStop.LEAD_EQUALS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        /* The following aren't actually recognized by Swing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        a.addElement(new BooleanAttribute(CHR, Constants.Caps,      "caps"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        a.addElement(new BooleanAttribute(CHR, Constants.Outline,   "outl"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        a.addElement(new BooleanAttribute(CHR, Constants.SmallCaps, "scaps"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        a.addElement(new BooleanAttribute(CHR, Constants.Shadow,    "shad"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        a.addElement(new BooleanAttribute(CHR, Constants.Hidden,    "v"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        a.addElement(new BooleanAttribute(CHR, Constants.Strikethrough,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                               "strike"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        a.addElement(new BooleanAttribute(CHR, Constants.Deleted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                               "deleted"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        a.addElement(new AssertiveAttribute(DOC, "saveformat", "defformat", "RTF"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        a.addElement(new AssertiveAttribute(DOC, "landscape", "landscape"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        a.addElement(NumericAttribute.NewTwips(DOC, Constants.PaperWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                               "paperw", 12240));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        a.addElement(NumericAttribute.NewTwips(DOC, Constants.PaperHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                               "paperh", 15840));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        a.addElement(NumericAttribute.NewTwips(DOC, Constants.MarginLeft,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                               "margl",  1800));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        a.addElement(NumericAttribute.NewTwips(DOC, Constants.MarginRight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                               "margr",  1800));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        a.addElement(NumericAttribute.NewTwips(DOC, Constants.MarginTop,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                               "margt",  1440));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        a.addElement(NumericAttribute.NewTwips(DOC, Constants.MarginBottom,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                               "margb",  1440));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        a.addElement(NumericAttribute.NewTwips(DOC, Constants.GutterWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                               "gutter", 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        a.addElement(new AssertiveAttribute(PGF, Constants.WidowControl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                            "nowidctlpar", False));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        a.addElement(new AssertiveAttribute(PGF, Constants.WidowControl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                            "widctlpar", True));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        a.addElement(new AssertiveAttribute(DOC, Constants.WidowControl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                            "widowctrl", True));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        RTFAttribute[] attrs = new RTFAttribute[a.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        a.copyInto(attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        attributes = attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    static Dictionary attributesByKeyword()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        Dictionary d = new Hashtable(attributes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        int i, m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        m = attributes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for(i = 0; i < m; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            d.put(attributes[i].rtfName(), attributes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    static abstract class GenericAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        Object swingName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        String rtfName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        protected GenericAttribute(int d,Object s, String r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            domain = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            swingName = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            rtfName = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        public int domain() { return domain; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        public Object swingName() { return swingName; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        public String rtfName() { return rtfName; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        abstract boolean set(MutableAttributeSet target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        abstract boolean set(MutableAttributeSet target, int parameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        abstract boolean setDefault(MutableAttributeSet target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        public boolean write(AttributeSet source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                             RTFGenerator target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                             boolean force)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return writeValue(source.getAttribute(swingName), target, force);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        public boolean writeValue(Object value, RTFGenerator target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                  boolean force)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    static class BooleanAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        extends GenericAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        implements RTFAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        boolean rtfDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        boolean swingDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        protected static final Boolean True = Boolean.valueOf(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        protected static final Boolean False = Boolean.valueOf(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        public BooleanAttribute(int d, Object s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                String r, boolean ds, boolean dr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            super(d, s, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            swingDefault = ds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            rtfDefault = dr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        public BooleanAttribute(int d, Object s, String r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            super(d, s, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            swingDefault = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            rtfDefault = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        public boolean set(MutableAttributeSet target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            /* TODO: There's some ambiguity about whether this should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
               *set* or *toggle* the attribute. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            target.addAttribute(swingName, True);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return true;  /* true indicates we were successful */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        public boolean set(MutableAttributeSet target, int parameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            /* See above note in the case that parameter==1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            Boolean value = ( parameter != 0 ? True : False );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            target.addAttribute(swingName, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return true; /* true indicates we were successful */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        public boolean setDefault(MutableAttributeSet target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (swingDefault != rtfDefault ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                ( target.getAttribute(swingName) != null ) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
              target.addAttribute(swingName, Boolean.valueOf(rtfDefault));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        public boolean writeValue(Object o_value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                  RTFGenerator target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                  boolean force)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            Boolean val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (o_value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
              val = Boolean.valueOf(swingDefault);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
              val = (Boolean)o_value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            if (force || (val.booleanValue() != rtfDefault)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                if (val.booleanValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    target.writeControlWord(rtfName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    target.writeControlWord(rtfName, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    static class AssertiveAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        extends GenericAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        implements RTFAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        Object swingValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        public AssertiveAttribute(int d, Object s, String r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            super(d, s, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            swingValue = Boolean.valueOf(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        public AssertiveAttribute(int d, Object s, String r, Object v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            super(d, s, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            swingValue = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        public AssertiveAttribute(int d, Object s, String r, int v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            super(d, s, r);
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   284
            swingValue = Integer.valueOf(v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        public boolean set(MutableAttributeSet target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (swingValue == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                target.removeAttribute(swingName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                target.addAttribute(swingName, swingValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        public boolean set(MutableAttributeSet target, int parameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        public boolean setDefault(MutableAttributeSet target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            target.removeAttribute(swingName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        public boolean writeValue(Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                  RTFGenerator target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                  boolean force)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                return ! force;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (value.equals(swingValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                target.writeControlWord(rtfName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            return ! force;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    static class NumericAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        extends GenericAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        implements RTFAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        int rtfDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        Number swingDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        float scale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        protected NumericAttribute(int d, Object s, String r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            super(d, s, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            rtfDefault = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            swingDefault = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            scale = 1f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        public NumericAttribute(int d, Object s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                String r, int ds, int dr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   346
            this(d, s, r, Integer.valueOf(ds), dr, 1f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        public NumericAttribute(int d, Object s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                String r, Number ds, int dr, float sc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            super(d, s, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            swingDefault = ds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            rtfDefault = dr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            scale = sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        public static NumericAttribute NewTwips(int d, Object s, String r,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                                float ds, int dr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return new NumericAttribute(d, s, r, new Float(ds), dr, 20f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        public static NumericAttribute NewTwips(int d, Object s, String r,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                                int dr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            return new NumericAttribute(d, s, r, null, dr, 20f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        public boolean set(MutableAttributeSet target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        public boolean set(MutableAttributeSet target, int parameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            Number swingValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            if (scale == 1f)
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   380
                swingValue = Integer.valueOf(parameter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                swingValue = new Float(parameter / scale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            target.addAttribute(swingName, swingValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        public boolean setDefault(MutableAttributeSet target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            Number old = (Number)target.getAttribute(swingName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            if (old == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                old = swingDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (old != null && (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    (scale == 1f && old.intValue() == rtfDefault) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    (Math.round(old.floatValue() * scale) == rtfDefault)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
               ))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            set(target, rtfDefault);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        public boolean writeValue(Object o_value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                                  RTFGenerator target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                                  boolean force)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            Number value = (Number)o_value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                value = swingDefault;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                /* TODO: What is the proper behavior if the Swing object does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                   not specify a value, and we don't know its default value?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                   Currently we pretend that the RTF default value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                   equivalent (probably a workable assumption) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            int int_value = Math.round(value.floatValue() * scale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            if (force || (int_value != rtfDefault))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                target.writeControlWord(rtfName, int_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
}