1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements.  See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership.  The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License.  You may obtain a copy of the License at
10 *
11 *   http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied.  See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21 
22 package org.apache.openoffice.ooxml.viewer.tokenview;
23 
24 import java.awt.Color;
25 import java.awt.FontMetrics;
26 import java.awt.Graphics2D;
27 
28 public class Run<TokenType>
29 {
Run( final String sText, final TokenType eTokenType, final Style aStyle, final int nOffset)30     Run (
31         final String sText,
32         final TokenType eTokenType,
33         final Style aStyle,
34         final int nOffset)
35     {
36         msText = sText;
37         meTokenType = eTokenType;
38         maStyle = aStyle!=null ? aStyle : Style.DefaultStyle;
39         mnStreamOffset = nOffset;
40 
41         mnWidth = -1;
42         mnHeight = -1;
43         mnOffset = nOffset;
44         maParent = null;
45         maGroupEnd = null;
46         maLine = null;
47     }
48 
49 
50 
51 
Format( final Graphics2D aG2)52     public void Format (
53         final Graphics2D aG2)
54     {
55         final FontMetrics aMetrics = aG2.getFontMetrics(maStyle.GetFont());
56         mnWidth = aMetrics.stringWidth(msText);
57         mnHeight = aMetrics.getHeight();
58         mnOffset = -aMetrics.getDescent();
59     }
60 
61 
62 
63 
Paint( final Graphics2D aG2, final int nX, final int nY, final Color aBackgroundColor)64     public void Paint (
65         final Graphics2D aG2,
66         final int nX,
67         final int nY,
68         final Color aBackgroundColor)
69     {
70         maStyle.Set(aG2);
71 
72         if (mnWidth < 0)
73         {
74             mnWidth = aG2.getFontMetrics().stringWidth(msText);
75             mnHeight = aG2.getFontMetrics().getHeight();
76         }
77 
78         if (aBackgroundColor != null)
79         {
80             final Color aSavedColor = aG2.getColor();
81             aG2.setColor(aBackgroundColor);
82             aG2.fillRect(nX,nY-mnHeight, mnWidth, mnHeight);
83             aG2.setColor(aSavedColor);
84         }
85         aG2.drawString(msText, nX, nY+mnOffset);
86 
87         if (msToolTipText != null)
88         {
89             aG2.drawLine(nX, nY-1, nX+mnWidth, nY-1);
90         }
91     }
92 
93 
94 
95 
GetText()96     public String GetText()
97     {
98         return msText;
99     }
100 
101 
102 
103 
GetStyle()104     public Style GetStyle ()
105     {
106         return maStyle;
107     }
108 
109 
110 
111 
GetStreamOffset()112     public int GetStreamOffset ()
113     {
114         return mnStreamOffset;
115     }
116 
117 
118 
119 
GetStreamEndOffset()120     public int GetStreamEndOffset ()
121     {
122         return mnStreamOffset + msText.length();
123     }
124 
125 
126 
127 
GetWidth()128     public int GetWidth()
129     {
130         return mnWidth;
131     }
132 
133 
134 
135 
GetHeight()136     public int GetHeight ()
137     {
138         return mnHeight;
139     }
140 
141 
142 
143 
SetGroupParent(final Run<TokenType> aParent)144     public void SetGroupParent (final Run<TokenType> aParent)
145     {
146         maParent = aParent;
147     }
148 
149 
150 
151 
SetGroupEnd(final Run<TokenType> aRun)152     public void SetGroupEnd (final Run<TokenType> aRun)
153     {
154         maGroupEnd = aRun;
155     }
156 
157 
158 
159 
GetGroupEnd()160     public Run<TokenType> GetGroupEnd()
161     {
162         return maGroupEnd;
163     }
164 
165 
166 
167 
IsGroup()168     public boolean IsGroup ()
169     {
170         if (maGroupEnd == null)
171             return false;
172         else if (maLine == maGroupEnd.maLine)
173             return true;
174         else
175             return true;
176     }
177 
178 
179 
180 
GetParent()181     public Run<TokenType> GetParent ()
182     {
183         return maParent;
184     }
185 
186 
187 
188 
GetLine()189     public Line<TokenType> GetLine ()
190     {
191         return maLine;
192     }
193 
194 
195 
196 
SetLine(final Line<TokenType> aLine)197     public void SetLine (final Line<TokenType> aLine)
198     {
199         maLine = aLine;
200     }
201 
202 
203 
204 
SetToolTipText(final String sText)205     public void SetToolTipText (final String sText)
206     {
207         msToolTipText = sText;
208     }
209 
210 
211 
212 
GetToolTipText()213     public String GetToolTipText ()
214     {
215         return msToolTipText;
216     }
217 
218 
219 
220 
GetTokenType()221     public TokenType GetTokenType ()
222     {
223         return meTokenType;
224     }
225 
226 
227 
228 
229     @Override
toString()230     public String toString ()
231     {
232         return "run '"+msText+"' @ "+mnOffset;
233     }
234 
235 
236 
237 
238     private final String msText;
239     private final TokenType meTokenType;
240     private final Style maStyle;
241     private final int mnStreamOffset;
242     private int mnWidth;
243     private int mnHeight;
244     private int mnOffset;
245     private Run<TokenType> maParent;
246     private Run<TokenType> maGroupEnd;
247     private Line<TokenType> maLine;
248     private String msToolTipText;
249 }
250