001: /*
002: * TextArea.java - The Area where date are written
003: * Copyright (C) 2003 Alexandre THOMAS
004: * alexthomas@free.fr
005: * http://helpgui.sourceforge.net
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package salomeTMF_plug.helpgui.gui;
023:
024: import java.awt.Graphics;
025: import java.awt.Graphics2D;
026: import java.awt.Rectangle;
027: import java.awt.Shape;
028: import java.awt.print.PageFormat;
029: import java.awt.print.Printable;
030: import java.io.IOException;
031: import java.net.URL;
032:
033: import javax.swing.JTextPane;
034: import javax.swing.text.View;
035:
036: import org.objectweb.salome_tmf.api.Util;
037:
038: import salomeTMF_plug.helpgui.page.Page;
039: import salomeTMF_plug.helpgui.util.Out;
040:
041: /**
042: * Class where the date are showed
043: *
044: * @author Alexandre THOMAS
045: */
046: public class TextArea extends JTextPane implements Printable {
047:
048: /* Attribut for printing */
049: int currentPage = -1;
050: double pageEndY = 0;
051: double pageStartY = 0;
052: boolean scaleWidthToFit = true;
053:
054: /** Constructor */
055: public TextArea() {
056: super ();
057: setEditable(false);
058: }
059:
060: /** Fonction call when a new page has to be show */
061: public void update(Page page) {
062: if (!page.isLeaf() && page.getTarget() == null) {
063: return;
064: }
065:
066: try {
067: URL url;
068: if (page.isPluginPage()) {
069: Util.log("[helpgui:page] Try open plug-in page : "
070: + MainPanel.pluginDir + "/"
071: + page.getPluginsName() + "/"
072: + MainPanel.helpDir + "/" + MainPanel.lang
073: + "/" + page.getLink());
074: try {
075: url = new URL(MainPanel.helpPath + "/"
076: + MainPanel.pluginDir + "/"
077: + page.getPluginsName() + "/"
078: + MainPanel.helpDir + "/" + MainPanel.lang
079: + "/" + page.getLink());
080: setPage(url);
081: } catch (Exception e1) {
082: Util
083: .log("[helpgui:page] Error then try to open main page : "
084: + MainPanel.pluginDir
085: + "/"
086: + page.getPluginsName()
087: + "/"
088: + MainPanel.helpDir
089: + "/"
090: + MainPanel.defaultlang
091: + "/"
092: + page.getLink());
093: url = new URL(MainPanel.helpPath + "/"
094: + MainPanel.pluginDir + "/"
095: + page.getPluginsName() + "/"
096: + MainPanel.helpDir + "/"
097: + MainPanel.defaultlang + "/"
098: + page.getLink());
099: setPage(url);
100: }
101: } else {
102: Util.log("[helpgui:page] Try open main help page : "
103: + MainPanel.helpDir + "/" + MainPanel.lang
104: + "/" + page.getLink());
105: try {
106: url = new URL(MainPanel.helpPath + "/"
107: + MainPanel.helpDir + "/" + MainPanel.lang
108: + "/" + page.getLink());
109: setPage(url);
110: } catch (Exception e1) {
111: Util
112: .log("[helpgui:page] Error then try open main page : "
113: + MainPanel.helpDir
114: + "/"
115: + MainPanel.defaultlang
116: + "/"
117: + page.getLink());
118: url = new URL(MainPanel.helpPath + "/"
119: + MainPanel.helpDir + "/"
120: + MainPanel.defaultlang + "/"
121: + page.getLink());
122: setPage(url);
123: }
124: }
125: } catch (IOException e) {
126: Out.msg("Unable to show the page : " + e, Out.FAILED);
127: setText("Error in showing the page : " + e);
128: } catch (Exception e) {
129: Out.msg("Error in showing the page", Out.FAILED);
130: setText("Error in showing the page : " + e);
131: }
132: }
133:
134: /** Print the current page */
135: // Adding code for print capacity
136: public int print(Graphics graphics, PageFormat pageFormat,
137: int pageIndex) {
138: //This function is the main printable overided function
139:
140: double scale = 1.0;
141: Graphics2D graphics2D;
142: View rootView;
143: graphics2D = (Graphics2D) graphics;
144:
145: setSize((int) pageFormat.getImageableWidth(), Integer.MAX_VALUE);
146: validate();
147:
148: rootView = this .getUI().getRootView(this );
149:
150: if ((scaleWidthToFit)
151: && (getMinimumSize().getWidth() > pageFormat
152: .getImageableWidth())) {
153: scale = pageFormat.getImageableWidth()
154: / getMinimumSize().getWidth();
155: graphics2D.scale(scale, scale);
156: }
157:
158: //The below four command lines shows that the content is clipped
159: //to the size of the printable page
160:
161: graphics2D.setClip((int) (pageFormat.getImageableX() / scale),
162: (int) (pageFormat.getImageableY() / scale),
163: (int) (pageFormat.getImageableWidth() / scale),
164: (int) (pageFormat.getImageableHeight() / scale));
165:
166: //The below if statement is to check to see if there is a new page to render
167:
168: if (pageIndex > currentPage) {
169: currentPage = pageIndex;
170: pageStartY += pageEndY;
171: pageEndY = graphics2D.getClipBounds().getHeight();
172: }
173:
174: graphics2D.translate(graphics2D.getClipBounds().getX(),
175: graphics2D.getClipBounds().getY());
176:
177: Rectangle allocation = new Rectangle(0, (int) -pageStartY,
178: (int) (getMinimumSize().getWidth()),
179: (int) (getPreferredSize().getHeight()));
180:
181: //The below if else statements return PAGE_EXISTS only if the class
182: //sees that there are some contents in the document by calling the printView class
183:
184: if (printView(graphics2D, allocation, rootView)) {
185: return PAGE_EXISTS;
186: }
187:
188: else {
189: pageStartY = 0;
190: pageEndY = 0;
191: currentPage = -1;
192: return NO_SUCH_PAGE;
193: }
194: }
195:
196: protected boolean printView(Graphics2D graphics2D,
197: Shape allocation, View view) {
198: //This function paints the page if it exists
199:
200: boolean pageExists = false;
201: Rectangle clipRectangle = graphics2D.getClipBounds();
202: Shape childAllocation;
203: View childView;
204:
205: if (view.getViewCount() > 0) {
206: for (int i = 0; i < view.getViewCount(); i++) {
207: childAllocation = view
208: .getChildAllocation(i, allocation);
209: if (childAllocation != null) {
210: childView = view.getView(i);
211: if (printView(graphics2D, childAllocation,
212: childView)) {
213: pageExists = true;
214: }
215: }
216:
217: }
218:
219: } else {
220: //The below if statement checks if there are pages currently to paint
221: if (allocation.getBounds().getMaxY() >= clipRectangle
222: .getY()) {
223: pageExists = true;
224: if ((allocation.getBounds().getHeight() > clipRectangle
225: .getHeight())
226: && (allocation.intersects(clipRectangle))) {
227: view.paint(graphics2D, allocation);
228: } else {
229: if (allocation.getBounds().getY() >= clipRectangle
230: .getY()) {
231: if (allocation.getBounds().getMaxY() <= clipRectangle
232: .getMaxY() - 15) {
233: view.paint(graphics2D, allocation);
234: } else {
235: if (allocation.getBounds().getY() < pageEndY) {
236: pageEndY = allocation.getBounds()
237: .getY();
238: }
239: }
240: }
241: }
242: }
243: }
244: return pageExists;
245: }
246:
247: }
|