KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > rtflib > rtfdoc > RtfHyperLink


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: RtfHyperLink.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.rtf.rtflib.rtfdoc;
21
22 /*
23  * This file is part of the RTF library of the FOP project, which was originally
24  * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
25  * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
26  * the FOP project.
27  */

28
29 import java.io.Writer JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 /**
33  * Creates an hyperlink.
34  * This class belongs to the <fo:basic-link> tag processing.
35  * @author <a HREF="mailto:a.putz@skynamics.com">Andreas Putz</a>
36  *
37  * {\field {\*\fldinst HYPERLINK "http://www.test.de" }{\fldrslt Joe Smith}}
38  */

39 public class RtfHyperLink
40 extends RtfContainer
41 implements IRtfTextContainer,
42            IRtfTextrunContainer {
43
44     //////////////////////////////////////////////////
45
// @@ Members
46
//////////////////////////////////////////////////
47

48     /** The url of the image */
49     protected String JavaDoc url = null;
50
51     /** RtfText */
52     protected RtfText mText = null;
53
54     //////////////////////////////////////////////////
55
// @@ Construction
56
//////////////////////////////////////////////////
57

58
59     /**
60      * Default constructor.
61      *
62      * @param parent a <code>RtfContainer</code> value
63      * @param writer a <code>Writer</code> value
64      * @param str text of the link
65      * @param attr a <code>RtfAttributes</code> value
66      * @throws IOException for I/O problems
67      */

68     public RtfHyperLink (IRtfTextContainer parent, Writer JavaDoc writer, String JavaDoc str, RtfAttributes attr)
69         throws IOException JavaDoc {
70         super ((RtfContainer) parent, writer, attr);
71         new RtfText (this, writer, str, attr);
72     }
73     
74     public RtfHyperLink (RtfTextrun parent, Writer JavaDoc writer, RtfAttributes attr)
75         throws IOException JavaDoc {
76         super ((RtfContainer) parent, writer, attr);
77     }
78
79
80     //////////////////////////////////////////////////
81
// @@ RtfElement implementation
82
//////////////////////////////////////////////////
83

84     /**
85      * Writes the RTF content to m_writer.
86      *
87      * @exception IOException On error
88      */

89     public void writeRtfPrefix () throws IOException JavaDoc {
90         super.writeGroupMark (true);
91         super.writeControlWord ("field");
92
93         super.writeGroupMark (true);
94         super.writeStarControlWord ("fldinst");
95
96         writer.write ("HYPERLINK \"" + url + "\" ");
97         super.writeGroupMark (false);
98
99         super.writeGroupMark (true);
100         super.writeControlWord ("fldrslt");
101
102         // start a group for this paragraph and write our own attributes if needed
103
if (attrib != null && attrib.isSet ("cs")) {
104             writeGroupMark (true);
105             writeAttributes(attrib, new String JavaDoc [] {"cs"});
106         }
107     }
108
109     /**
110      * Writes the RTF content to m_writer.
111      *
112      * @exception IOException On error
113      */

114     public void writeRtfSuffix () throws IOException JavaDoc {
115         if (attrib != null && attrib.isSet ("cs")) {
116             writeGroupMark (false);
117         }
118         super.writeGroupMark (false);
119         super.writeGroupMark (false);
120     }
121
122
123     //////////////////////////////////////////////////
124
// @@ IRtfContainer implementation
125
//////////////////////////////////////////////////
126

127     /**
128      * close current text run if any and start a new one with default attributes
129      * @param str if not null, added to the RtfText created
130      * @throws IOException for I/O problems
131      * @return new RtfText object
132      */

133     public RtfText newText (String JavaDoc str) throws IOException JavaDoc {
134         return newText (str, null);
135     }
136
137     /**
138      * close current text run if any and start a new one
139      * @param str if not null, added to the RtfText created
140      * @param attr attributes of text to add
141      * @throws IOException for I/O problems
142      * @return the new RtfText object
143      */

144     public RtfText newText (String JavaDoc str, RtfAttributes attr) throws IOException JavaDoc {
145         closeAll ();
146         mText = new RtfText (this, writer, str, attr);
147         return mText;
148     }
149
150     /**
151      * IRtfTextContainer requirement:
152      * @return a copy of our attributes
153      */

154     public RtfAttributes getTextContainerAttributes() {
155         if (attrib == null) {
156             return null;
157         }
158         return (RtfAttributes) this.attrib.clone ();
159     }
160
161
162     /**
163      * add a line break
164      * @throws IOException for I/O problems
165      */

166     public void newLineBreak () throws IOException JavaDoc {
167         new RtfLineBreak (this, writer);
168     }
169
170
171     //////////////////////////////////////////////////
172
// @@ Common container methods
173
//////////////////////////////////////////////////
174

175     private void closeCurrentText () throws IOException JavaDoc {
176         if (mText != null) {
177             mText.close ();
178         }
179     }
180
181     private void closeAll () throws IOException JavaDoc {
182         closeCurrentText();
183     }
184
185
186     //////////////////////////////////////////////////
187
// @@ Member access
188
//////////////////////////////////////////////////
189

190     /**
191      * Sets the url of the external link.
192      *
193      * @param url Link url like "http://..."
194      */

195     public void setExternalURL (String JavaDoc url) {
196         this.url = url;
197     }
198
199     /**
200      * Sets the url of the external link.
201      *
202      * @param jumpTo Name of the text mark
203      */

204     public void setInternalURL (String JavaDoc jumpTo) {
205         int now = jumpTo.length ();
206         int max = RtfBookmark.MAX_BOOKMARK_LENGTH;
207         this.url = "#" + jumpTo.substring (0, now > max ? max : now);
208         this.url = this.url.replace ('.', RtfBookmark.REPLACE_CHARACTER);
209         this.url = this.url.replace (' ', RtfBookmark.REPLACE_CHARACTER);
210     }
211
212     /**
213      *
214      * @return false (always)
215      */

216     public boolean isEmpty () {
217         return false;
218     }
219     
220     public RtfTextrun getTextrun()
221     throws IOException JavaDoc {
222         RtfTextrun textrun = RtfTextrun.getTextrun(this, writer, null);
223         return textrun;
224     }
225 }
226
Popular Tags