Source Code Cross Referenced for SJasperViewToolBar.java in  » J2EE » Sofia » com » salmonllc » jasperReports » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Sofia » com.salmonllc.jasperReports 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //The Salmon Open Framework for Internet Applications (SOFIA)
002:        //Copyright (C) 1999 - 2002, 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.BorderLayout;
022:        import java.awt.Container;
023:        import java.awt.Cursor;
024:        import java.awt.Dimension;
025:        import java.awt.Point;
026:        import java.awt.event.ActionEvent;
027:        import java.awt.event.ActionListener;
028:        import java.awt.event.KeyAdapter;
029:        import java.awt.event.KeyEvent;
030:        import java.awt.event.MouseAdapter;
031:        import java.awt.event.MouseEvent;
032:        import java.awt.event.MouseMotionAdapter;
033:        import java.io.ByteArrayInputStream;
034:        import java.io.File;
035:        import java.io.FileNotFoundException;
036:        import java.io.FileOutputStream;
037:        import java.io.PrintWriter;
038:        import java.net.MalformedURLException;
039:        import java.net.URL;
040:        import java.util.ArrayList;
041:        import java.util.Enumeration;
042:        import java.util.HashMap;
043:        import java.util.Hashtable;
044:        import java.util.StringTokenizer;
045:
046:        import javax.swing.ImageIcon;
047:        import javax.swing.JButton;
048:        import javax.swing.JComboBox;
049:        import javax.swing.JComponent;
050:        import javax.swing.JFileChooser;
051:        import javax.swing.JFrame;
052:        import javax.swing.JLabel;
053:        import javax.swing.JOptionPane;
054:        import javax.swing.JTextField;
055:        import javax.swing.JToolBar;
056:        import javax.swing.event.DocumentEvent;
057:        import javax.swing.event.DocumentListener;
058:        import javax.swing.filechooser.FileFilter;
059:
060:        import com.salmonllc.personalization.ProxySkinManager;
061:        import com.salmonllc.personalization.Skin;
062:        import com.salmonllc.personalization.SkinManager;
063:        import com.salmonllc.sql.DataStoreBuffer;
064:        import com.salmonllc.sql.DataStoreException;
065:        import com.salmonllc.sql.DataStoreInterface;
066:        import com.salmonllc.sql.DataStoreProxy;
067:        import com.salmonllc.sql.QBEBuilder;
068:        import com.salmonllc.swing.SComponentHelper;
069:        import com.salmonllc.swing.STextField;
070:        import com.salmonllc.util.VectorSort;
071:
072:        import dori.jasper.engine.JRException;
073:        import dori.jasper.engine.JRHyperlink;
074:        import dori.jasper.engine.JRPrintHyperlink;
075:        import dori.jasper.engine.JasperExportManager;
076:        import dori.jasper.engine.JasperFillManager;
077:        import dori.jasper.engine.JasperManager;
078:        import dori.jasper.engine.JasperPrint;
079:        import dori.jasper.engine.JasperPrintManager;
080:        import dori.jasper.engine.JasperReport;
081:        import dori.jasper.engine.util.JRLoader;
082:
083:        /**
084:         * A toolbar for the SJasperViewScreen component. When combined with the view screen into a panel this can provide a UI for viewing reports. All the actions for the viewer are implemented as inner classes in the toolbar
085:         */
086:        public class SJasperViewToolBar extends JToolBar {
087:            private static final Cursor HAND_CURSOR = new Cursor(
088:                    Cursor.HAND_CURSOR);
089:            private static final Cursor DEFAULT_CURSOR = new Cursor(
090:                    Cursor.DEFAULT_CURSOR);
091:            private static final Cursor WAIT_CURSOR = new Cursor(
092:                    Cursor.WAIT_CURSOR);
093:
094:            private String _serverURL;
095:            private SJasperViewScreen _screen;
096:            private DataStoreInterface _ds;
097:            private JButton _first, _last, _next, _prior;
098:            private SJasperViewInfo _info;
099:            private String _lastSort = "";
100:            private int _lastSortDir = -1;
101:            private JasperReport _rep;
102:            private boolean _busy = false;
103:            private Cursor _oldCursor;
104:            private JComboBox _zoom;
105:            private JTextField _pageNo;
106:            private STextField _filterString;
107:            private JButton _page;
108:            private JButton _reload;
109:            private JButton _print;
110:            private JButton _filter;
111:            private String _lastFilter = "";
112:            private JButton _export;
113:            private JToolBar _this  = this ;
114:            private String _selectionCriteria;
115:            private Hashtable _actions = new Hashtable();
116:            private QBEBuilder _filterBuilder;
117:
118:            public static final String ACTION_EXPORT = "export";
119:            public static final String ACTION_FILTER = "filter";
120:            public static final String ACTION_FIRST = "first";
121:            public static final String ACTION_LAST = "last";
122:            public static final String ACTION_NEXT = "next";
123:            public static final String ACTION_PRIOR = "prior";
124:            public static final String ACTION_ZOOM = "zoom";
125:            public static final String ACTION_PRINT = "print";
126:            public static final String ACTION_PAGE = "page";
127:            public static final String ACTION_RELOAD = "reload";
128:
129:            public class ActionSort extends VectorSort {
130:                public boolean compare(Object o1, Object o2) {
131:                    return ((ActionDef) o1).order < ((ActionDef) o2).order;
132:                }
133:            }
134:
135:            public class ActionDef {
136:                public String label;
137:                public String buttonLabel;
138:                public String buttonURL;
139:                public String buttonTooltip;
140:                public int order;
141:                public boolean seperatorAfter = false;
142:                public boolean visible = true;
143:                public JComponent labelComp;
144:                public JComponent textComp;
145:                public JComponent buttonComp;
146:            }
147:
148:            public class PrintAction implements  ActionListener {
149:                public void actionPerformed(ActionEvent e) {
150:                    try {
151:                        JasperPrintManager.printReport(
152:                                _screen.getJasperPrint(), true);
153:                    } catch (JRException e1) {
154:                        e1.printStackTrace();
155:                    }
156:                }
157:            }
158:
159:            public class ExportAction implements  ActionListener {
160:                public void actionPerformed(ActionEvent e) {
161:                    JFileChooser c = new JFileChooser();
162:                    ExportFilter tab = new ExportFilter("dat",
163:                            "Tab Delimited (*.dat)");
164:                    ExportFilter pdf = new ExportFilter("pdf",
165:                            "Adobe Acrobat (*.pdf)");
166:
167:                    c.addChoosableFileFilter(tab);
168:                    c.addChoosableFileFilter(pdf);
169:                    c.setDialogTitle("Export Report");
170:                    JFrame f = SComponentHelper.getParentFrame(_this );
171:                    while (c.showSaveDialog(f) == JFileChooser.APPROVE_OPTION) {
172:                        if (c.getSelectedFile().exists()) {
173:                            int ret = JOptionPane.showOptionDialog(f,
174:                                    "File Exists, Do you want to replace it?",
175:                                    "File Exists",
176:                                    JOptionPane.YES_NO_CANCEL_OPTION,
177:                                    JOptionPane.QUESTION_MESSAGE, null, null,
178:                                    null);
179:                            if (ret == JOptionPane.NO_OPTION)
180:                                continue;
181:                            else if (ret == JOptionPane.CANCEL_OPTION)
182:                                return;
183:                        }
184:                        String fileName = c.getSelectedFile().getAbsolutePath();
185:                        FileFilter filter = c.getFileFilter();
186:                        String ext = getExtension(c.getSelectedFile());
187:                        if (ext.equals("pdf") || filter == pdf) {
188:                            setBusy(true);
189:                            try {
190:                                JasperExportManager.exportReportToPdfFile(
191:                                        _screen.getJasperPrint(), fileName);
192:                            } catch (JRException e1) {
193:                                e1.printStackTrace();
194:                            }
195:                            setBusy(false);
196:                            break;
197:                        } else if (ext.equals("dat") || filter == tab) {
198:                            setBusy(true);
199:                            try {
200:                                PrintWriter pw = new PrintWriter(
201:                                        new FileOutputStream(fileName));
202:                                _ds.export(
203:                                        DataStoreBuffer.EXPORT_TAB_DELIMITED,
204:                                        true, pw);
205:                                pw.close();
206:                            } catch (FileNotFoundException e1) {
207:                                e1.printStackTrace();
208:                            }
209:                            setBusy(false);
210:                            break;
211:                        } else {
212:                            JOptionPane
213:                                    .showMessageDialog(f,
214:                                            "Please select a pdf or tab delimited file format to save as.");
215:                        }
216:
217:                    }
218:                }
219:            }
220:
221:            public class ExportFilter extends FileFilter {
222:
223:                private String _extension, _description;
224:
225:                public ExportFilter(String extension, String description) {
226:                    _extension = extension;
227:                    _description = description;
228:                }
229:
230:                //Accept all directories and all csv,pdf.
231:                public boolean accept(File f) {
232:                    if (f.isDirectory())
233:                        return true;
234:
235:                    String extension = getExtension(f);
236:                    if (extension != null && extension.length() > 0) {
237:                        if (extension.equals(_extension))
238:                            return true;
239:                        else
240:                            return false;
241:
242:                    }
243:                    return false;
244:                }
245:
246:                //The description of this filter
247:                public String getDescription() {
248:                    return _description;
249:                }
250:
251:                public String toString() {
252:                    return _description;
253:                }
254:            }
255:
256:            public class FirstAction implements  ActionListener {
257:                public void actionPerformed(java.awt.event.ActionEvent evt) {
258:                    _screen.gotoFirstPage();
259:                    enableDisable();
260:                }
261:            }
262:
263:            public class LastAction implements  ActionListener {
264:                public void actionPerformed(java.awt.event.ActionEvent evt) {
265:                    _screen.gotoLastPage();
266:                    enableDisable();
267:                }
268:            }
269:
270:            public class NextAction implements  ActionListener {
271:                public void actionPerformed(java.awt.event.ActionEvent evt) {
272:                    _screen.gotoNextPage();
273:                    enableDisable();
274:                }
275:            }
276:
277:            public class PriorAction implements  ActionListener {
278:                public void actionPerformed(java.awt.event.ActionEvent evt) {
279:                    _screen.gotoPriorPage();
280:                    enableDisable();
281:                }
282:            }
283:
284:            public class UpdateInfoAction extends MouseMotionAdapter {
285:                public void mouseMoved(MouseEvent e) {
286:                    if (!_busy)
287:                        setInfoMessage(e.getPoint());
288:                }
289:            }
290:
291:            public class ZoomAction implements  ActionListener {
292:                public void actionPerformed(ActionEvent e) {
293:                    String st = (String) _zoom.getSelectedItem();
294:                    int value = Integer.parseInt(st.substring(0,
295:                            st.length() - 1));
296:                    float f = (float) value / (float) 100;
297:                    _screen.setZoomPercent(f);
298:                    enableDisable();
299:                }
300:            }
301:
302:            public class ReportClickedAction extends MouseAdapter {
303:                public void mouseClicked(MouseEvent e) {
304:                    Point p = e.getPoint();
305:                    String sort = _screen.getSortNameAtPoint(p);
306:                    JRPrintHyperlink link = _screen.getHyperlinkAtPoint(p);
307:                    if (sort != null)
308:                        sortReport(_screen.getSortColumnsAtPoint(p));
309:                    else if (link != null) {
310:                        if (link.getHyperlinkType() == JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR
311:                                || link.getHyperlinkType() == JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE
312:                                || link.getHyperlinkType() == JRHyperlink.HYPERLINK_TYPE_REFERENCE)
313:                            _screen.gotoHyperLinkLocation(link, e
314:                                    .isControlDown());
315:                    }
316:                    ;
317:                    enableDisable();
318:                }
319:            }
320:
321:            public class PageCheckerAction implements  DocumentListener {
322:                public void insertUpdate(DocumentEvent e) {
323:                    enableDisablePageButton();
324:                }
325:
326:                public void removeUpdate(DocumentEvent e) {
327:                    enableDisablePageButton();
328:                }
329:
330:                public void changedUpdate(DocumentEvent e) {
331:                    enableDisablePageButton();
332:                }
333:            }
334:
335:            public class PageZoomAction extends KeyAdapter implements 
336:                    ActionListener {
337:                public void actionPerformed(ActionEvent e) {
338:                    int pageNo = Integer.parseInt(_pageNo.getText().trim()) - 1;
339:                    _screen.gotoPage(pageNo);
340:                    enableDisable();
341:
342:                }
343:
344:                public void keyReleased(KeyEvent e) {
345:                    if (e.getKeyCode() == KeyEvent.VK_ENTER
346:                            && _page.isEnabled())
347:                        actionPerformed(null);
348:                }
349:            }
350:
351:            public class ReloadAction implements  ActionListener {
352:                public void actionPerformed(ActionEvent e) {
353:                    try {
354:                        setBusy(true);
355:                        _ds.retrieve(_selectionCriteria);
356:
357:                        if (_lastFilter != null && _lastFilter.length() > 0) {
358:                            _ds.waitForRetrieve();
359:                            _ds.filter(_lastFilter);
360:                        }
361:
362:                        if (_lastSort.length() > 0) {
363:                            _ds.waitForRetrieve();
364:                            doSort(_lastSort, _lastSortDir);
365:                        }
366:
367:                        _ds.gotoFirst();
368:                        JasperPrint pr = JasperFillManager.fillReport(_rep,
369:                                new HashMap(), new SJasperDataSource(_ds));
370:                        _screen.setJasperPrint(pr);
371:
372:                        enableDisable();
373:                        setBusy(false);
374:                    } catch (Exception e1) {
375:                        e1.printStackTrace();
376:                    }
377:                }
378:
379:            }
380:
381:            public class FilterAction extends KeyAdapter implements 
382:                    ActionListener {
383:                public void actionPerformed(ActionEvent e) {
384:                    String filter = _filterBuilder
385:                            .generateDataStoreFilter((DataStoreBuffer) _ds);
386:                    String testFilter = filter == null ? "" : filter;
387:                    if (testFilter.equals(_lastFilter))
388:                        return;
389:                    _lastFilter = testFilter;
390:
391:                    try {
392:                        setBusy(true);
393:                        _ds.filter(filter);
394:                        if (_lastSort.length() > 0)
395:                            doSort(_lastSort, _lastSortDir);
396:                        _ds.gotoFirst();
397:                        JasperPrint pr = JasperFillManager.fillReport(_rep,
398:                                new HashMap(), new SJasperDataSource(_ds));
399:                        _screen.setJasperPrint(pr);
400:
401:                        enableDisable();
402:                        setBusy(false);
403:                    } catch (Exception ex) {
404:                    }
405:                }
406:
407:                public void keyReleased(KeyEvent e) {
408:                    if (e.getKeyCode() == KeyEvent.VK_ENTER)
409:                        actionPerformed(null);
410:                }
411:
412:            }
413:
414:            public SJasperViewToolBar(SJasperViewScreen screen,
415:                    DataStoreInterface ds, SJasperViewInfo info,
416:                    JasperReport rep, String imageBaseURL,
417:                    String selectionCriteria) {
418:                setFloatable(false);
419:
420:                _screen = screen;
421:                _ds = ds;
422:                _rep = rep;
423:                _info = info;
424:                _selectionCriteria = selectionCriteria;
425:
426:                if (imageBaseURL != null)
427:                    if (!imageBaseURL.endsWith("/"))
428:                        imageBaseURL += "/";
429:
430:                //reload action
431:                ActionDef def = new ActionDef();
432:                _reload = new JButton();
433:                def.buttonComp = _reload;
434:                if (imageBaseURL == null)
435:                    def.buttonLabel = "Reload";
436:                else
437:                    def.buttonURL = imageBaseURL + "reload.gif";
438:                def.buttonTooltip = "Reload Report Data From Database";
439:
440:                def.order = 0;
441:                _actions.put(ACTION_RELOAD, def);
442:
443:                //print action
444:                def = new ActionDef();
445:                _print = new JButton();
446:                def.buttonComp = _print;
447:                if (imageBaseURL == null)
448:                    def.buttonLabel = "Print";
449:                else
450:                    def.buttonURL = imageBaseURL + "print.gif";
451:                def.buttonTooltip = "Print Report";
452:
453:                def.order = 1;
454:                _actions.put(ACTION_PRINT, def);
455:
456:                //export action
457:                def = new ActionDef();
458:                _export = new JButton();
459:                def.buttonComp = _export;
460:                if (imageBaseURL == null)
461:                    def.buttonLabel = "Export";
462:                else
463:                    def.buttonURL = imageBaseURL + "export.gif";
464:                def.buttonTooltip = "Export Report To PDF or Tab Delimited Format";
465:                def.order = 2;
466:                def.seperatorAfter = true;
467:                _actions.put(ACTION_EXPORT, def);
468:
469:                //page action
470:                def = new ActionDef();
471:                _page = new JButton();
472:                def.buttonComp = _page;
473:                if (imageBaseURL == null)
474:                    def.buttonLabel = "Go";
475:                else
476:                    def.buttonURL = imageBaseURL + "page.gif";
477:                def.buttonTooltip = "Go to Selected Page";
478:                def.seperatorAfter = true;
479:                _pageNo = new STextField(2);
480:                def.textComp = _pageNo;
481:                def.order = 3;
482:                def.label = "Page: ";
483:                _actions.put(ACTION_PAGE, def);
484:
485:                //first action
486:                def = new ActionDef();
487:                _first = new JButton();
488:                def.buttonComp = _first;
489:                if (imageBaseURL == null)
490:                    def.buttonLabel = "First";
491:                else
492:                    def.buttonURL = imageBaseURL + "first.gif";
493:                def.buttonTooltip = "Go to First Page";
494:                def.seperatorAfter = false;
495:                def.order = 4;
496:                _actions.put(ACTION_FIRST, def);
497:
498:                //prior action
499:                def = new ActionDef();
500:                _prior = new JButton();
501:                def.buttonComp = _prior;
502:                if (imageBaseURL == null)
503:                    def.buttonLabel = "Prior";
504:                else
505:                    def.buttonURL = imageBaseURL + "prior.gif";
506:                def.buttonTooltip = "Go to Prior Page";
507:                def.seperatorAfter = false;
508:                def.order = 5;
509:                _actions.put(ACTION_PRIOR, def);
510:
511:                //next action
512:                def = new ActionDef();
513:                _next = new JButton();
514:                def.buttonComp = _next;
515:                if (imageBaseURL == null)
516:                    def.buttonLabel = "Next";
517:                else
518:                    def.buttonURL = imageBaseURL + "next.gif";
519:                def.buttonTooltip = "Go to Next Page";
520:                def.seperatorAfter = false;
521:                def.order = 6;
522:                _actions.put(ACTION_NEXT, def);
523:
524:                //last action
525:                def = new ActionDef();
526:                _last = new JButton();
527:                def.buttonComp = _last;
528:                if (imageBaseURL == null)
529:                    def.buttonLabel = "Last";
530:                else
531:                    def.buttonURL = imageBaseURL + "last.gif";
532:                def.buttonTooltip = "Go to Last Page";
533:                def.seperatorAfter = true;
534:                def.order = 7;
535:                _actions.put(ACTION_LAST, def);
536:
537:                //zoom action
538:                String zoomItems[] = { "50%", "75%", "100%", "125%", "150%",
539:                        "175%", "200%", "225%", "250%", "275%", "300%" };
540:                _zoom = new JComboBox(zoomItems);
541:                _zoom.setSelectedIndex(2);
542:                def = new ActionDef();
543:                def.label = "Zoom: ";
544:                def.seperatorAfter = true;
545:                def.order = 8;
546:                def.textComp = _zoom;
547:                _zoom.setPreferredSize(new Dimension(70, 20));
548:                _actions.put(ACTION_ZOOM, def);
549:
550:                //filter action
551:                def = new ActionDef();
552:                def.label = "Filter: ";
553:                _filterString = new STextField(5);
554:                _filterBuilder = new QBEBuilder();
555:                _filterBuilder.addCriteria("filter",
556:                        QBEBuilder.CRITERIA_TYPE_COMPLEX, "*");
557:                _filterString.setColumn(_filterBuilder, "filter");
558:                _filter = new JButton();
559:                def.buttonComp = _filter;
560:                if (imageBaseURL == null)
561:                    def.buttonLabel = "Go";
562:                else
563:                    def.buttonURL = imageBaseURL + "filter.gif";
564:                def.buttonTooltip = "Apply Filter To Report";
565:                def.textComp = _filterString;
566:                def.seperatorAfter = false;
567:                def.order = 9;
568:                _actions.put(ACTION_FILTER, def);
569:
570:                //add listeners		
571:                _zoom.addActionListener(new ZoomAction());
572:                _pageNo.getDocument().addDocumentListener(
573:                        new PageCheckerAction());
574:                _page.addActionListener(new PageZoomAction());
575:                _pageNo.addKeyListener(new PageZoomAction());
576:                _first.addActionListener(new FirstAction());
577:                _last.addActionListener(new LastAction());
578:                _next.addActionListener(new NextAction());
579:                _prior.addActionListener(new PriorAction());
580:                _screen.getMainViewer().addMouseMotionListener(
581:                        new UpdateInfoAction());
582:                _screen.getMainViewer().addMouseListener(
583:                        new ReportClickedAction());
584:                _print.addActionListener(new PrintAction());
585:                _filter.addActionListener(new FilterAction());
586:                _filterString.addKeyListener(new FilterAction());
587:                _export.addActionListener(new ExportAction());
588:                _reload.addActionListener(new ReloadAction());
589:
590:                buildToolbar();
591:                enableDisable();
592:            }
593:
594:            /**
595:             * Displays a OK, Cancel Question dialog with the string passed. Returns true if the user clicks OK and false if CANCEL
596:             */
597:            private boolean displayQuestion(String question, JFrame parent) {
598:                int dialogRet = JOptionPane.showOptionDialog(parent, question,
599:                        "Question", JOptionPane.YES_NO_OPTION,
600:                        JOptionPane.QUESTION_MESSAGE, null, null, null);
601:                return dialogRet == JOptionPane.OK_OPTION;
602:            }
603:
604:            /** 
605:             * Once toolbar button infomation has been changed (order,image,caption,etc...) call buildToolbar to create the GUI.
606:             *
607:             */
608:            public void buildToolbar() {
609:                removeAll();
610:                ActionSort ac = new ActionSort();
611:                Enumeration en = _actions.elements();
612:                while (en.hasMoreElements())
613:                    ac.add(en.nextElement());
614:                ac.sort();
615:                for (int i = 0; i < ac.size(); i++) {
616:                    ActionDef def = (ActionDef) ac.elementAt(i);
617:                    if (!def.visible)
618:                        continue;
619:                    if (def.label != null)
620:                        add(new JLabel(def.label));
621:                    if (def.textComp != null)
622:                        add(def.textComp);
623:                    if (def.buttonComp != null) {
624:                        JButton but = (JButton) def.buttonComp;
625:                        but.setText(def.buttonLabel);
626:                        but.setToolTipText(def.buttonTooltip);
627:                        if (def.buttonLabel == null) {
628:                            Dimension d = new Dimension(28, 28);
629:                            but.setPreferredSize(d);
630:                            but.setMaximumSize(d);
631:                        }
632:                        if (def.buttonURL != null) {
633:                            try {
634:                                ImageIcon ico = new ImageIcon(new URL(
635:                                        def.buttonURL));
636:                                but.setIcon(ico);
637:                            } catch (MalformedURLException e) {
638:                                e.printStackTrace();
639:                            }
640:                        }
641:                        add(but);
642:                    }
643:                    if (def.seperatorAfter)
644:                        addSeparator();
645:                }
646:            }
647:
648:            /**
649:             * Enables or disables buttons based on the state of the viewer
650:             */
651:            public void enableDisable() {
652:                _first.setEnabled(!_screen.isOnFirstPage());
653:                _prior.setEnabled(!_screen.isOnFirstPage());
654:                _next.setEnabled(!_screen.isOnLastPage());
655:                _last.setEnabled(!_screen.isOnLastPage());
656:                _info.setPageMessage(_screen.getPageNo() + 1, _screen
657:                        .getPageCount());
658:                _pageNo
659:                        .setText(new Integer(_screen.getPageNo() + 1)
660:                                .toString());
661:                _info.setZoomMessage(_screen.getZoomPercent());
662:                _print.setEnabled(_screen.getPageCount() > 0);
663:                enableDisablePageButton();
664:            }
665:
666:            private void enableDisablePageButton() {
667:                try {
668:                    int val = Integer.parseInt(_pageNo.getText().trim());
669:                    if (val > 0 && val <= _screen.getPageCount())
670:                        _page.setEnabled(true);
671:                    else
672:                        _page.setEnabled(false);
673:                } catch (Exception e) {
674:                    _page.setEnabled(false);
675:                }
676:            }
677:
678:            private void doSort(String sortString, int dir) {
679:                StringTokenizer tok = new StringTokenizer(sortString, ",");
680:                ArrayList l = new ArrayList();
681:                while (tok.hasMoreTokens()) {
682:                    String st = tok.nextToken();
683:                    int ndx = _ds.getColumnIndex(st);
684:                    if (ndx > -1)
685:                        l.add(new Integer(ndx));
686:                }
687:
688:                if (l.size() > 0) {
689:                    int sorts[] = new int[l.size()];
690:                    int dirs[] = new int[l.size()];
691:                    for (int i = 0; i < l.size(); i++) {
692:                        sorts[i] = ((Integer) l.get(i)).intValue();
693:                        dirs[i] = dir;
694:                    }
695:                    try {
696:                        _ds.sort(sorts, dirs);
697:                        _ds.gotoFirst();
698:                    } catch (Exception ex) {
699:                    }
700:                }
701:            }
702:
703:            private void sortReport(String sortString) {
704:                if (_busy)
705:                    return;
706:                setBusy(true);
707:                if (sortString.equals(_lastSort)) {
708:                    if (_lastSortDir == DataStoreBuffer.SORT_ASC)
709:                        _lastSortDir = DataStoreBuffer.SORT_DES;
710:                    else
711:                        _lastSortDir = DataStoreBuffer.SORT_ASC;
712:                } else
713:                    _lastSortDir = DataStoreBuffer.SORT_ASC;
714:                _lastSort = sortString;
715:                doSort(sortString, _lastSortDir);
716:                try {
717:                    Thread.sleep(500);
718:                    JasperPrint pr = JasperFillManager.fillReport(_rep,
719:                            new HashMap(), new SJasperDataSource(_ds));
720:                    _screen.setJasperPrint(pr);
721:                } catch (Exception ex) {
722:                    ex.printStackTrace();
723:                }
724:
725:                setBusy(false);
726:            }
727:
728:            private void setInfoMessage(Point p) {
729:                String sort = _screen.getSortNameAtPoint(p);
730:                JRPrintHyperlink link = _screen.getHyperlinkAtPoint(p);
731:                if (sort != null) {
732:                    setMainCursor(HAND_CURSOR);
733:                    _info.setMessage("Sort Report On: " + sort);
734:                } else if (link != null) {
735:                    int type = link.getHyperlinkType();
736:                    setMainCursor(HAND_CURSOR);
737:                    if (type == JRHyperlink.HYPERLINK_TYPE_REFERENCE
738:                            || type == JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE
739:                            || type == JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE) {
740:                        if (link.getHyperlinkReference() != null)
741:                            _info.setMessage("Link To: "
742:                                    + link.getHyperlinkReference());
743:                    } else if (type == JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR)
744:                        _info.setMessage("Jump To: #"
745:                                + link.getHyperlinkAnchor());
746:                    else if (type == JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE)
747:                        _info.setMessage("Jump To Page: "
748:                                + link.getHyperlinkPage());
749:                } else {
750:                    setMainCursor(DEFAULT_CURSOR);
751:                    _info.setMessage(" ");
752:                }
753:            }
754:
755:            private void setBusy(boolean busy) {
756:                _busy = busy;
757:                if (!busy)
758:                    setMainCursor(_oldCursor);
759:                else
760:                    setMainCursor(WAIT_CURSOR);
761:            }
762:
763:            private Cursor setMainCursor(Cursor cur) {
764:                if (cur == null)
765:                    cur = DEFAULT_CURSOR;
766:                Cursor oc = _oldCursor;
767:                _screen.getMainViewer().setCursor(cur);
768:                if (cur != _oldCursor && cur != WAIT_CURSOR)
769:                    _oldCursor = cur;
770:                return oc;
771:            }
772:
773:            private static String getExtension(File f) {
774:                String ext = "";
775:                String s = f.getName();
776:                int i = s.lastIndexOf('.');
777:
778:                if (i > 0 && i < s.length() - 1) {
779:                    ext = s.substring(i + 1).toLowerCase();
780:                }
781:                return ext;
782:            }
783:
784:            /**
785:             * Sets the label for an action (text on the right of it)
786:             */
787:            public void setActionLabel(String action, String newLabel) {
788:                ActionDef d = (ActionDef) _actions.get(action);
789:                if (d != null)
790:                    d.label = newLabel;
791:            }
792:
793:            /**
794:             * Sets the button label for an action 
795:             */
796:
797:            public void setActionButtonLabel(String action,
798:                    String newButtonLabel) {
799:                ActionDef d = (ActionDef) _actions.get(action);
800:                if (d != null)
801:                    d.buttonLabel = newButtonLabel;
802:            }
803:
804:            /**
805:             * Sets the URL for the image to appear on an action button
806:             */
807:            public void setActionButtonURL(String action, String newButtonURL) {
808:                ActionDef d = (ActionDef) _actions.get(action);
809:                if (d != null)
810:                    d.buttonURL = newButtonURL;
811:            }
812:
813:            /**
814:             * Sets the tooltip text for an action button
815:             */
816:            public void setActionButtonTooltip(String action, String newTooltip) {
817:                ActionDef d = (ActionDef) _actions.get(action);
818:                if (d != null)
819:                    d.buttonTooltip = newTooltip;
820:            }
821:
822:            /**
823:             * Sets the numeric order an action will appear on the toolbar. Actions are sorted from lowest to highest and start on the left of the toolbar.
824:             */
825:            public void setActionOrder(String action, int newOrder) {
826:                ActionDef d = (ActionDef) _actions.get(action);
827:                if (d != null)
828:                    d.order = newOrder;
829:            }
830:
831:            /**
832:             * Set to true if an action should have a seperator bar following it
833:             */
834:            public void setActionSeperator(String action, boolean seperator) {
835:                ActionDef d = (ActionDef) _actions.get(action);
836:                if (d != null)
837:                    d.seperatorAfter = seperator;
838:            }
839:
840:            /**
841:             * Set to true for the action to be visible and false if not
842:             */
843:            public void setActionVisible(String action, boolean visible) {
844:                ActionDef d = (ActionDef) _actions.get(action);
845:                if (d != null)
846:                    d.visible = visible;
847:            }
848:
849:            /**
850:             * A utility method that computes the absolute URL for a relative URL using the specified code base
851:             */
852:            public static String computeRelativeURL(String codeBase, String url) {
853:                if (url.startsWith("/")) {
854:                    String baseURL = codeBase;
855:                    int pos = baseURL.indexOf("/Jsp/");
856:                    if (pos != -1)
857:                        baseURL = codeBase.substring(0, pos);
858:                    if (baseURL.endsWith("/"))
859:                        baseURL = baseURL.substring(0, baseURL.length() - 1);
860:                    url = baseURL + url;
861:                } else if (!url.startsWith("http")) {
862:                    url = codeBase + url;
863:                }
864:                return url;
865:            }
866:
867:            /**
868:             * A utility method used to quickly build a GUI for the viewer
869:             * @param serverURL The code base for the web application that launched the applet
870:             * @param modelClass The class name of the remote datastore used for the report
871:             * @param selectionCriteria The selection criteria to be passed to model.retrieve()
872:             * @param reportDefURL The relative URL for the report definition
873:             * @param imageBaseURL The relative URL for the server directory containing the toolbar images
874:             * @param sessionID The Session ID for the web application launching the viewer
875:             * @param contentPane The content pane for the application frame or applet hosting the viewer
876:             * @param showImages A true/false indicating whether or not images or text should be displayed on toolbar buttons
877:             * @param showInfoArea A true/false indicating whether or not to display the information status bar as part of the viewer
878:             * @param urlOpener The URL Opener class used for following links in a report 
879:             * @param messageDisplayer The Message Displayer object used for communicating information status bar messages to the appropriate user interface component (either the SJasperViewInfo object or the browsers status bar)
880:             * @return the instance of the toolbar created. The toolbar can be further customized by calling setAction methods and then buildGUI.
881:             */
882:            public static SJasperViewToolBar buildGUI(String serverURL,
883:                    String modelClass, String selectionCriteria,
884:                    String reportDefURL, String imageBaseURL, String sessionID,
885:                    Container contentPane, boolean showImages,
886:                    boolean showInfoArea, URLOpener urlOpener,
887:                    MessageDisplayer messageDisplayer)
888:                    throws DataStoreException, MalformedURLException,
889:                    JRException {
890:                String baseURL = serverURL;
891:                int pos = serverURL.indexOf("/Jsp/");
892:                if (pos != -1)
893:                    serverURL = serverURL.substring(0, pos);
894:                if (!serverURL.endsWith("/"))
895:                    serverURL += "/";
896:
897:                if (reportDefURL.startsWith("/"))
898:                    reportDefURL = reportDefURL.substring(1);
899:                if (selectionCriteria != null
900:                        && selectionCriteria.equals("null"))
901:                    selectionCriteria = null;
902:
903:                DataStoreProxy ds = new DataStoreProxy(serverURL
904:                        + "DataServer/" + modelClass, sessionID, true);
905:                ds.retrieve(selectionCriteria);
906:                ds.waitForRetrieve();
907:                ds.gotoFirst();
908:                byte[] repBytes = JRLoader.loadBytes(new URL(serverURL
909:                        + reportDefURL));
910:                JasperReport rep = JasperManager
911:                        .loadReport(new ByteArrayInputStream(repBytes));
912:                JasperPrint pr = JasperFillManager.fillReport(rep,
913:                        new HashMap(), new SJasperDataSource(ds));
914:                SJasperViewScreen viewer = new SJasperViewScreen(pr, urlOpener);
915:                SJasperViewInfo info = new SJasperViewInfo(messageDisplayer);
916:                SJasperViewToolBar tb = new SJasperViewToolBar(viewer, ds,
917:                        info, rep, showImages ? imageBaseURL : null,
918:                        selectionCriteria);
919:                tb.setServerURL(baseURL);
920:                contentPane.setLayout(new BorderLayout());
921:                contentPane.add(tb, BorderLayout.NORTH);
922:                contentPane.add(viewer, BorderLayout.CENTER);
923:                if (showInfoArea)
924:                    contentPane.add(info, BorderLayout.SOUTH);
925:
926:                SkinManager man = new ProxySkinManager(serverURL
927:                        + "PersonalizationServer", sessionID);
928:                Skin sk = new Skin();
929:                man.load(null, sk);
930:                SComponentHelper.applySkin(sk, contentPane);
931:                return tb;
932:            }
933:
934:            /**
935:             * @returns the Server URL
936:             */
937:            public String getServerURL() {
938:                return _serverURL;
939:            }
940:
941:            /**
942:             * Sets the server URL
943:             */
944:            public void setServerURL(String string) {
945:                _serverURL = string;
946:            }
947:
948:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.