001: //The Salmon Open Framework for Internet Applications (SOFIA)
002: //Copyright (C) 1999 - 2003, Salmon LLC
003: //
004: //This program is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU General Public License version 2
006: //as published by the Free Software Foundation;
007: //
008: //This program is distributed in the hope that it will be useful,
009: //but WITHOUT ANY WARRANTY; without even the implied warranty of
010: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: //GNU General Public License for more details.
012: //
013: //You should have received a copy of the GNU General Public License
014: //along with this program; if not, write to the Free Software
015: //Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: //
017: //For more information please visit http://www.salmonllc.com
018: //** End Copyright Statement ***************************************************
019: package com.salmonllc.jasperReports;
020:
021: import java.awt.Color;
022: import java.awt.Container;
023: import java.awt.Font;
024: import java.awt.GridLayout;
025: import java.awt.Point;
026: import java.awt.Rectangle;
027: import java.net.MalformedURLException;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Map;
033:
034: import javax.swing.BorderFactory;
035: import javax.swing.ImageIcon;
036: import javax.swing.JComponent;
037: import javax.swing.JLabel;
038: import javax.swing.JPanel;
039: import javax.swing.JScrollPane;
040: import javax.swing.JViewport;
041: import javax.swing.SwingConstants;
042:
043: import dori.jasper.engine.JRException;
044: import dori.jasper.engine.JRHyperlink;
045: import dori.jasper.engine.JRPrintAnchor;
046: import dori.jasper.engine.JRPrintAnchorIndex;
047: import dori.jasper.engine.JRPrintElement;
048: import dori.jasper.engine.JRPrintHyperlink;
049: import dori.jasper.engine.JRPrintPage;
050: import dori.jasper.engine.JRPrintText;
051: import dori.jasper.engine.JasperPrint;
052: import dori.jasper.engine.JasperPrintManager;
053:
054: /**
055: * This class serves as the basic GUI element for a SOFIA Based Jasper Report Viewer. It provides an unembelished user interface that allows for the viewing of a page in a Jasper print and a few helper methods to query or change the state of the page being viewed.
056: */
057: public class SJasperViewScreen extends JPanel {
058: private JasperPrint _pr;
059: private JLabel _main;
060: private JPanel _noData;
061: private JLabel _noDataLabel;
062: private int _page = 0;
063: private JScrollPane _pane;
064: private float _zoom = 1.0F;
065: private ArrayList _links = new ArrayList();
066: private ArrayList _sorts = new ArrayList();
067: private URLOpener _urlOpener;
068:
069: /**
070: * Creates a view screen for a particular JasperPrint object. The page will be set to zero and the zoom to 100%
071: */
072: public SJasperViewScreen(JasperPrint pr) {
073: buildComponents(pr, -1, 0.0F);
074: }
075:
076: /**
077: * Creates a view screen for a particular JasperPrint object. The page will be set to zero and the zoom to 100%
078: */
079: public SJasperViewScreen(JasperPrint pr, URLOpener urlOpener) {
080: _urlOpener = urlOpener;
081: buildComponents(pr, -1, 0.0F);
082: }
083:
084: /**
085: * Creates a view screen for a particular JasperPrint object.
086: */
087: public SJasperViewScreen(JasperPrint pr, int pageNo,
088: float zoomPercent, URLOpener urlOpener) {
089: _urlOpener = urlOpener;
090: buildComponents(pr, pageNo, zoomPercent);
091: }
092:
093: private void buildComponents(JasperPrint pr, int pageNo,
094: float zoomPercent) {
095: setLayout(new GridLayout(1, 1));
096: _pr = pr;
097: _main = new JLabel();
098: _pane = new JScrollPane(_main);
099:
100: _noData = new JPanel(new GridLayout(1, 1));
101: _noData.setBorder(BorderFactory.createEtchedBorder());
102: _noDataLabel = new JLabel("No Data Found",
103: SwingConstants.CENTER);
104: _noDataLabel.setFont(new Font("Helvetica", Font.BOLD, 24));
105: _noDataLabel.setBackground(Color.white);
106: _noDataLabel.setOpaque(true);
107: _noData.add(_noDataLabel);
108: if (pageNo != -1) {
109: _page = pageNo;
110: _zoom = zoomPercent;
111: }
112: refreshScreen();
113: }
114:
115: /**
116: * Gets the current page number
117: */
118: public int getPageNo() {
119: return _page;
120: }
121:
122: /**
123: * Gets the JasperPrint that the viewer is using
124: */
125: public JasperPrint getJasperPrint() {
126: return _pr;
127: }
128:
129: /**
130: * gets the zoom percent for the report 1.0F = 100%
131: */
132: public float getZoomPercent() {
133: return _zoom;
134: }
135:
136: /**
137: * Sets the JasperPrint that the viewer is using
138: */
139: public void setJasperPrint(JasperPrint print) {
140: _pr = print;
141: _page = 0;
142: refreshScreen();
143: }
144:
145: /**
146: * sets the zoom percent for the report 1.0F = 100%
147: */
148: public void setZoomPercent(float f) {
149: _zoom = f;
150: refreshScreen();
151: }
152:
153: /**
154: * Returns the conponent representing the report page viewer
155: */
156: public JComponent getMainViewer() {
157: return _main;
158: }
159:
160: /**
161: * Returns true if the specified page number exists
162: */
163:
164: public boolean isPageValid(int pageNo) {
165: if (pageNo < 0)
166: return false;
167: List l = _pr.getPages();
168: if (l == null)
169: return false;
170: return pageNo < l.size();
171: }
172:
173: /**
174: * Returns true if the viewer is on the last page of the report or if the report is empty
175: */
176:
177: public boolean isOnLastPage() {
178: List l = _pr.getPages();
179: if (l == null || l.size() == 0)
180: return true;
181: return _page == (l.size() - 1);
182: }
183:
184: /**
185: * Returns true if the viewer is on the first page of the report or if the report is empty
186: */
187:
188: public boolean isOnFirstPage() {
189: List l = _pr.getPages();
190: if (l == null || l.size() == 0)
191: return true;
192: return _page == 0;
193: }
194:
195: /**
196: * Cause the viewer to display the next page in the report
197: */
198: public void gotoNextPage() {
199: gotoPage(_page + 1);
200: }
201:
202: /**
203: * Cause the viewer to display the prior page in the report
204: */
205:
206: public void gotoPriorPage() {
207: gotoPage(_page - 1);
208: }
209:
210: /**
211: * Cause the viewer to display the first page in the report
212: */
213: public void gotoFirstPage() {
214: gotoPage(0);
215: }
216:
217: /**
218: * Cause the viewer to display the last page in the report
219: */
220: public void gotoLastPage() {
221: gotoPage(getPageCount() - 1);
222: }
223:
224: /**
225: * Cause the viewer to display the specified page in the report
226: */
227: public void gotoPage(int page) {
228: if (isPageValid(page)) {
229: _page = page;
230: refreshScreen();
231: }
232: }
233:
234: /**
235: * Returns the number of pages in the print
236: */
237: public int getPageCount() {
238: List l = _pr.getPages();
239: if (l == null)
240: return 0;
241: else
242: return l.size();
243: }
244:
245: /**
246: * Returns the column name(s) of the fields in the sort anchor at the specified point or null if there is no sort anchor at that point. Sort anchors are Jasper Report constructs specific to The SOFIA Viewer which allow the user to click on a title and sort by it. They are text fields with the anchor link expression set using the convention "sort:comma seperated list of field names to sort by"
247: * @param p Point relative to the main viewer in the component
248: * @see SJasperViewScreen#getMainViewer()
249: */
250: public String getSortColumnsAtPoint(Point p) {
251: Object item[] = null;
252: Rectangle r = null;
253: for (int i = 0; i < _sorts.size(); i++) {
254: item = (Object[]) _sorts.get(i);
255: r = (Rectangle) item[0];
256: if (r.contains(p))
257: return (String) item[1];
258: }
259: return null;
260: }
261:
262: /**
263: * Returns the text for the sort anchor at a specified point or null if there is no sort anchor at that point. Sort anchors are Jasper Report constructs specific to The SOFIA Viewer which allow the user to click on a title and sort by it. They are text fields with the anchor link expression set using the convention "sort:comma seperated list of field names to sort by"
264: * @param p Point relative to the main viewer in the component
265: * @see SJasperViewScreen#getMainViewer()
266: */
267: public String getSortNameAtPoint(Point p) {
268: Object item[] = null;
269: Rectangle r = null;
270: for (int i = 0; i < _sorts.size(); i++) {
271: item = (Object[]) _sorts.get(i);
272: r = (Rectangle) item[0];
273: if (r.contains(p))
274: return (String) item[2];
275: }
276: return null;
277: }
278:
279: /**
280: * Returns the hyperlink at the current point or null if there isn't one at the point.
281: * @param p Point relative to the main viewer in the component
282: * @see SJasperViewScreen#getMainViewer()
283: */
284: public JRPrintHyperlink getHyperlinkAtPoint(Point p) {
285: Object item[] = null;
286: Rectangle r = null;
287: for (int i = 0; i < _links.size(); i++) {
288: item = (Object[]) _links.get(i);
289: r = (Rectangle) item[0];
290: if (r.contains(p))
291: return (JRPrintHyperlink) item[1];
292: }
293: return null;
294: }
295:
296: /**
297: * Returns the position of upper right hand corner of the main viewer in the panel
298: */
299: public Point getMainViewPosition() {
300: return new Point(_pane.getViewport().getX(), _pane
301: .getViewport().getY());
302: }
303:
304: /**
305: * For JRPrintHyperLinks that are local to this report JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR or JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE, this method will zoom to the correct spot in the document
306: * @return true if the link could be processed and false if not
307: */
308: public boolean gotoHyperLinkLocation(JRPrintHyperlink link,
309: boolean ctrlDown) {
310: if (link.getHyperlinkType() == JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR) {
311: if (link.getHyperlinkAnchor() != null) {
312: Map anchorIndexes = _pr.getAnchorIndexes();
313: JRPrintAnchorIndex anchorIndex = (JRPrintAnchorIndex) anchorIndexes
314: .get(link.getHyperlinkAnchor());
315: if (anchorIndex.getPageIndex() != _page)
316: gotoPage(anchorIndex.getPageIndex());
317: Container container = _main.getParent();
318: if (container instanceof JViewport) {
319: JViewport viewport = (JViewport) container;
320: int newX = (int) (anchorIndex.getElement().getX() * _zoom);
321: int newY = (int) (anchorIndex.getElement().getY() * _zoom);
322: int maxX = _main.getWidth() - viewport.getWidth();
323: int maxY = _main.getHeight() - viewport.getHeight();
324: if (newX < 0)
325: newX = 0;
326: if (newX > maxX)
327: newX = maxX;
328: if (newY < 0)
329: newY = 0;
330: if (newY > maxY)
331: newY = maxY;
332: viewport.setViewPosition(new Point(newX, newY));
333: }
334: return true;
335: }
336: } else if (link.getHyperlinkType() == JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE) {
337: int page = _page + 1;
338: if (link.getHyperlinkPage() != null)
339: page = link.getHyperlinkPage().intValue();
340:
341: if (page >= 1 && page <= _pr.getPages().size()
342: && page != _page + 1) {
343: this .gotoPage(page - 1);
344: Container container = _main.getParent();
345: if (container instanceof JViewport) {
346: JViewport viewport = (JViewport) container;
347: viewport.setViewPosition(new Point(0, 0));
348: }
349: }
350: return true;
351: } else if (link.getHyperlinkType() == JRHyperlink.HYPERLINK_TYPE_REFERENCE) {
352: if (_urlOpener != null) {
353: try {
354: if (!ctrlDown)
355: _urlOpener
356: .openURL(link.getHyperlinkReference());
357: else
358: _urlOpener.openURL(
359: link.getHyperlinkReference(),
360: "newFrame");
361: } catch (MalformedURLException e) {
362: e.printStackTrace();
363: }
364: }
365: }
366:
367: return false;
368: }
369:
370: /**
371: * Sets the message to be displayed when there is no data in the report
372: * @param message
373: */
374: public void setNoDataMessage(String message) {
375: _noDataLabel.setText(message);
376: }
377:
378: private void refreshScreen() {
379: try {
380: removeAll();
381: if (getPageCount() == 0)
382: add(_noData);
383: else {
384: add(_pane);
385: java.awt.Image image = JasperPrintManager
386: .printPageToImage(_pr, _page, _zoom);
387: ImageIcon imageIcon = new ImageIcon(image);
388: _main.setText(null);
389: _main.setIcon(imageIcon);
390: _main.setVisible(true);
391: setUpLinksAndSorts();
392: }
393: revalidate();
394: repaint();
395: } catch (JRException ex) {
396: ex.printStackTrace();
397: }
398: }
399:
400: private void setUpLinksAndSorts() {
401: _links.clear();
402: _sorts.clear();
403: java.util.List pages = _pr.getPages();
404: JRPrintPage page = (JRPrintPage) pages.get(_page);
405: Collection elements = page.getElements();
406:
407: if (elements != null && elements.size() > 0) {
408: JRPrintElement element = null;
409: JRPrintHyperlink hyperlink = null;
410: Rectangle rect = null;
411: for (Iterator it = elements.iterator(); it.hasNext();) {
412: element = (JRPrintElement) it.next();
413: if (element instanceof JRPrintHyperlink
414: && ((JRPrintHyperlink) element)
415: .getHyperlinkType() != JRHyperlink.HYPERLINK_TYPE_NONE) {
416: hyperlink = (JRPrintHyperlink) element;
417: rect = new Rectangle(
418: (int) (element.getX() * _zoom),
419: (int) (element.getY() * _zoom),
420: (int) (element.getWidth() * _zoom),
421: (int) (element.getHeight() * _zoom));
422: Object item[] = { rect, hyperlink };
423: _links.add(item);
424: } else if (element instanceof JRPrintAnchor) {
425: String anchorName = ((JRPrintAnchor) element)
426: .getAnchorName();
427: if (anchorName != null) {
428: if (anchorName.toLowerCase()
429: .startsWith("sort:")) {
430: rect = new Rectangle(
431: (int) (element.getX() * _zoom),
432: (int) (element.getY() * _zoom),
433: (int) (element.getWidth() * _zoom),
434: (int) (element.getHeight() * _zoom));
435: Object item[] = { rect,
436: anchorName.substring(5), "" };
437: if (element instanceof JRPrintText)
438: item[2] = ((JRPrintText) element)
439: .getText();
440: _sorts.add(item);
441: }
442: }
443: }
444: }
445: }
446:
447: }
448:
449: }
|