KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > txt > border > DashedBorderElement


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: DashedBorderElement.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.txt.border;
21
22 import java.util.Arrays JavaDoc;
23
24 /**
25  * This class is responsible for managing of dashed border elements.
26  */

27 public class DashedBorderElement extends AbstractBorderElement {
28     
29     private static final char DASH_HORIZONTAL = '-';
30
31     private static final char DASH_VERTICAL = '|';
32     
33     private static final char UNDEFINED = '?';
34     
35     private static final int UP2 = 1;
36     
37     private static final int RIGHT2 = 2;
38     
39     private static final int DOWN2 = 4;
40     
41     private static final int LEFT2 = 8;
42     
43     private static char[] map = new char[20];
44     
45     static {
46         Arrays.fill(map, UNDEFINED);
47         map[0] = ' ';
48         map[UP2] = DASH_VERTICAL;
49         map[DOWN2] = DASH_VERTICAL;
50         map[UP2 + DOWN2] = DASH_VERTICAL;
51         
52         map[LEFT2] = DASH_HORIZONTAL;
53         map[RIGHT2] = DASH_HORIZONTAL;
54         map[LEFT2 + RIGHT2] = DASH_HORIZONTAL;
55     }
56     
57     /**
58      * Constructs a newly allocated <code>DashedBorderElement</code> object.
59      * Fills <code>data</code> using superclass constructor.
60      *
61      * @param type binary representation of type gives <code>data</code>
62      */

63     public DashedBorderElement(int type) {
64         super(type);
65     }
66
67     /**
68      * Merges dashed border element with instance of solid and double border
69      * element, returns instance of <code>SolidAndDoubleBorderElement</code>.
70      *
71      * @param sdb instance of <code>SolidAndDoubleBorderElement</code> to merge
72      * @return merged border element
73      */

74     private AbstractBorderElement mergeSolid(SolidAndDoubleBorderElement sdb) {
75         AbstractBorderElement e = new SolidAndDoubleBorderElement(EN_SOLID, 0);
76         for (int i = 0; i < 4; i++) {
77             e.setData(i, Math.max(data[i], sdb.getData(i)));
78         }
79         return e;
80     }
81
82     /**
83      * Merges dashed border element with dashed border element and returns
84      * instance of <code>DashedBorderElement</code>.
85      *
86      * @param dbe instance of <code>DashedBorderElement</code> to merge
87      * @return merged border element
88      */

89     private AbstractBorderElement mergeDashed(DashedBorderElement dbe) {
90         for (int i = 0; i < 4; i++) {
91             data[i] = Math.max(data[i], dbe.getData(i));
92         }
93         return this;
94     }
95     
96     /**
97      * Converts dashed border element to
98      * <code>SolidAndDoubleBorderElement</code>.
99      *
100      * @return converted instance of <code>SolidAndDoubleBorderElement</code>
101      */

102     private AbstractBorderElement toSolidAndDouble() {
103         AbstractBorderElement e = new SolidAndDoubleBorderElement(EN_SOLID, 0);
104         for (int i = 0; i < 4; i++) {
105             e.setData(i, data[i]);
106         }
107         return e;
108     }
109
110     /**
111      * Merges with border element.
112      * @param e instance of AbstractBorderElement
113      * @return instance of AbstractBorderElement
114      */

115     public AbstractBorderElement merge(AbstractBorderElement e) {
116         AbstractBorderElement abe = this;
117         if (e instanceof SolidAndDoubleBorderElement) {
118             abe = mergeSolid((SolidAndDoubleBorderElement) e);
119         } else if (e instanceof DashedBorderElement) {
120             abe = mergeDashed((DashedBorderElement) e);
121         } else {
122             abe = e;
123         }
124         return abe;
125     }
126
127     /**
128      * @see org.apache.fop.render.txt.border.AbstractBorderElement#convert2Char()
129      */

130     public char convert2Char() {
131         int key = 0;
132         key += data[UP] * UP2;
133         key += data[DOWN] * DOWN2;
134         key += data[LEFT] * LEFT2;
135         key += data[RIGHT] * RIGHT2;
136         char ch = map[key];
137         if (ch == UNDEFINED) {
138             ch = toSolidAndDouble().convert2Char();
139         }
140         return ch;
141     }
142 }
143
Popular Tags