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.util.Stack;
25 
26 
27 public class DocumentFactory<TokenType>
28 {
29     interface IRepaintTarget
30     {
RequestRepaint()31         void RequestRepaint();
32     }
33 
34 
35 
36 
DocumentFactory( final LineContainer<TokenType> aLines, final IRepaintTarget aRepaintTarget)37     DocumentFactory (
38         final LineContainer<TokenType> aLines,
39         final IRepaintTarget aRepaintTarget)
40     {
41         maLines = aLines;
42         maRepaintTarget = aRepaintTarget;
43         mbIsGroupBeginPending = false;
44         maGroupStartStack = new Stack<Run<TokenType>>();
45         maLastRun = null;
46         maCurrentGroup = null;
47         StartNewLine();
48     }
49 
50 
51 
52 
AddText( final String sText, final TokenType eTokenType, final Style aStyle, final int nOffset)53     public void AddText (
54         final String sText,
55         final TokenType eTokenType,
56         final Style aStyle,
57         final int nOffset)
58     {
59         final Run<TokenType> aRun = new Run<TokenType>(sText, eTokenType, aStyle, nOffset);
60         synchronized(maLines)
61         {
62             maCurrentLine.AddRun(aRun);
63             if (sText.endsWith("\n"))
64                 StartNewLine();
65         }
66 
67         if (mbIsGroupBeginPending)
68         {
69             maGroupStartStack.push(maCurrentGroup);
70             aRun.SetGroupParent(maCurrentGroup);
71             maCurrentGroup = aRun;
72             mbIsGroupBeginPending = false;
73         }
74         else if (maCurrentGroup != null)
75             aRun.SetGroupParent(maCurrentGroup);
76         maLastRun = aRun;
77 
78         maRepaintTarget.RequestRepaint();
79     }
80 
81 
82 
83 
FinishText()84     public void FinishText ()
85     {
86         StartNewLine();
87     }
88 
89 
90 
91 
BeginGroup()92     public void BeginGroup()
93     {
94         mbIsGroupBeginPending = true;
95     }
96 
97 
98 
99 
EndGroup()100     public void EndGroup ()
101     {
102         maCurrentGroup.SetGroupEnd(maLastRun);
103         maCurrentGroup = maGroupStartStack.pop();
104     }
105 
106 
107 
108 
109 
StartNewLine()110     private void StartNewLine ()
111     {
112         if (maCurrentLine != null)
113             maLines.AddLine(maCurrentLine);
114         maCurrentLine = new Line<TokenType>();
115     }
116 
117 
118 
119 
120     private final LineContainer<TokenType> maLines;
121     private final IRepaintTarget maRepaintTarget;
122     private Line<TokenType> maCurrentLine;
123     private boolean mbIsGroupBeginPending;
124     private Stack<Run<TokenType>> maGroupStartStack;
125     private Run<TokenType> maLastRun;
126     private Run<TokenType> maCurrentGroup;
127 }
128