001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.pd;
031:
032: import de.intarsys.pdf.cos.COSArray;
033: import de.intarsys.pdf.cos.COSBasedObject;
034: import de.intarsys.pdf.cos.COSInteger;
035: import de.intarsys.pdf.cos.COSName;
036: import de.intarsys.pdf.cos.COSObject;
037:
038: /**
039: * The definition of a border style for an annotation.
040: */
041: public class PDBorderStyle extends PDObject {
042: /**
043: * The meta class implementation
044: */
045: static public class MetaClass extends PDObject.MetaClass {
046: protected MetaClass(Class instanceClass) {
047: super (instanceClass);
048: }
049:
050: protected COSBasedObject doCreateCOSBasedObject(COSObject object) {
051: return new PDBorderStyle(object);
052: }
053: }
054:
055: /** The meta class instance */
056: static public final MetaClass META = new MetaClass(MetaClass.class
057: .getDeclaringClass());
058:
059: /** The width type. */
060: static public final COSName DK_W = COSName.constant("W");
061:
062: /** The Style type. */
063: static public final COSName DK_S = COSName.constant("S");
064:
065: /** Style S: solid */
066: static public final COSName CN_S_S = COSName.constant("S");
067:
068: /** Style D: dashed */
069: static public final COSName CN_S_D = COSName.constant("D");
070:
071: /** Style B: beveled */
072: static public final COSName CN_S_B = COSName.constant("B");
073:
074: /** Style D: Inset */
075: static public final COSName CN_S_I = COSName.constant("I");
076:
077: /** Style D: underlined */
078: static public final COSName CN_S_U = COSName.constant("U");
079:
080: /** The DashArray type. */
081: static public final COSName DK_D = COSName.constant("D"); //
082:
083: /** The border type name */
084: static public final COSName CN_Type_Border = COSName
085: .constant("Border"); //
086:
087: protected PDBorderStyle(COSObject object) {
088: super (object);
089: }
090:
091: public void setDashArray(int[] newDashArray) {
092: if ((newDashArray == null)
093: || ((newDashArray.length == 1) && (newDashArray[0] == 3))) {
094: cosRemoveField(DK_D);
095: return;
096: }
097:
098: COSArray a = COSArray.create(newDashArray.length);
099: cosSetField(DK_D, a); // overwrite existing array
100:
101: for (int i = 0; i < newDashArray.length; i++) {
102: a.add(COSInteger.create(newDashArray[i]));
103: }
104: }
105:
106: public int[] getDashArray() {
107: COSArray array = cosGetField(DK_D).asArray();
108: if (array != null) {
109: int[] result = new int[array.size()];
110: for (int i = 0; i < array.size(); i++) {
111: COSInteger value = array.get(i).asInteger();
112: if (value != null) {
113: result[i] = value.intValue();
114: } else {
115: // TODO 3 wrong default, maybe restrict
116: result[i] = 0;
117: }
118: }
119: return result;
120: }
121: return new int[] { 3 }; // default
122: }
123:
124: public void setStyle(COSName newStyle) {
125: cosSetField(DK_S, newStyle);
126: }
127:
128: public COSName getStyle() {
129: COSName style = cosGetField(DK_S).asName();
130: if (style != null) {
131: return style;
132: }
133: return CN_S_S;
134: }
135:
136: /*
137: * (non-Javadoc)
138: *
139: * @see de.intarsys.pdf.pd.PDObject#cosGetExpectedType()
140: */
141: protected COSName cosGetExpectedType() {
142: return CN_Type_Border;
143: }
144:
145: /**
146: * Set the style. If newWidth = 0, no border is drawn.
147: *
148: * @param newWidth
149: * The new width.
150: */
151: public void setWidth(float newWidth) {
152: if (newWidth != 1) {
153: setFieldFixed(DK_W, newWidth);
154: } else {
155: cosRemoveField(DK_W);
156: }
157: }
158:
159: public float getWidth() {
160: return getFieldFixed(DK_W, 1);
161: }
162: }
|