Source Code Cross Referenced for Preview.java in  » IDE-Netbeans » print » org » netbeans » modules » print » impl » ui » 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 » IDE Netbeans » print » org.netbeans.modules.print.impl.ui 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         *
004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * The contents of this file are subject to the terms of either the GNU
007:         * General Public License Version 2 only ("GPL") or the Common
008:         * Development and Distribution License("CDDL") (collectively, the
009:         * "License"). You may not use this file except in compliance with the
010:         * License. You can obtain a copy of the License at
011:         * http://www.netbeans.org/cddl-gplv2.html
012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013:         * specific language governing permissions and limitations under the
014:         * License.  When distributing the software, include this License Header
015:         * Notice in each file and include the License file at
016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
017:         * particular file as subject to the "Classpath" exception as provided
018:         * by Sun in the GPL Version 2 section of the License file that
019:         * accompanied this code. If applicable, add the following below the
020:         * License Header, with the fields enclosed by brackets [] replaced by
021:         * your own identifying information:
022:         * "Portions Copyrighted [year] [name of copyright owner]"
023:         *
024:         * Contributor(s):
025:         *
026:         * The Original Software is NetBeans. The Initial Developer of the Original
027:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028:         * Microsystems, Inc. All Rights Reserved.
029:         *
030:         * If you wish your version of this file to be governed by only the CDDL
031:         * or only the GPL Version 2, indicate your decision by adding
032:         * "[Contributor] elects to include this software in this distribution
033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
034:         * single choice of license, a recipient has the option to distribute
035:         * your version of this file under either the CDDL, the GPL Version 2 or
036:         * to extend the choice of license to its licensees as provided above.
037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
038:         * Version 2 license, then the option applies only if the new code is
039:         * made subject to such option by the copyright holder.
040:         */
041:        package org.netbeans.modules.print.impl.ui;
042:
043:        import java.awt.Color;
044:        import java.awt.Dimension;
045:        import java.awt.GridBagConstraints;
046:        import java.awt.GridBagLayout;
047:        import java.awt.Insets;
048:        import java.awt.Point;
049:        import java.awt.Toolkit;
050:        import java.awt.event.ActionEvent;
051:        import java.awt.event.ActionListener;
052:        import java.awt.event.KeyAdapter;
053:        import java.awt.event.KeyEvent;
054:        import java.awt.event.KeyListener;
055:        import java.awt.event.MouseAdapter;
056:        import java.awt.event.MouseEvent;
057:        import java.awt.event.MouseWheelEvent;
058:        import java.awt.event.MouseWheelListener;
059:        import java.util.ArrayList;
060:        import java.util.Date;
061:        import java.util.List;
062:
063:        import javax.swing.AbstractAction;
064:        import javax.swing.ActionMap;
065:        import javax.swing.InputMap;
066:        import javax.swing.JButton;
067:        import javax.swing.JComponent;
068:        import javax.swing.JPanel;
069:        import javax.swing.JTextField;
070:        import javax.swing.JToggleButton;
071:        import javax.swing.JScrollPane;
072:        import javax.swing.JViewport;
073:        import javax.swing.KeyStroke;
074:        import javax.swing.ScrollPaneConstants;
075:        import javax.swing.SwingUtilities;
076:
077:        import org.openide.DialogDescriptor;
078:        import org.netbeans.modules.print.spi.PrintPage;
079:        import org.netbeans.modules.print.spi.PrintProvider;
080:        import org.netbeans.modules.print.impl.util.Option;
081:        import org.netbeans.modules.print.impl.util.Percent;
082:        import static org.netbeans.modules.print.impl.util.UI.*;
083:
084:        /**
085:         * @author Vladimir Yaroslavskiy
086:         * @version 2005.12.14
087:         */
088:        public final class Preview extends Dialog implements  Percent.Listener {
089:
090:            public static Preview getDefault() {
091:                return DEFAULT;
092:            }
093:
094:            private Preview() {
095:                myPrinter = new Printer();
096:                myKeyListener = new KeyAdapter() {
097:                    public void keyPressed(KeyEvent event) {
098:                        char ch = event.getKeyChar();
099:
100:                        if (ch == '+' || ch == '=') {
101:                            myScale.increaseValue();
102:                        } else if (ch == '-' || ch == '_') {
103:                            myScale.decreaseValue();
104:                        } else if (ch == '/') {
105:                            myScale.normalValue();
106:                        } else if (ch == '*') {
107:                            showCustom(true);
108:                        }
109:                    }
110:                };
111:            }
112:
113:            public void print(List<PrintProvider> providers, boolean withPreview) {
114:                assert providers != null : "Print providers can not be null"; // NOI18N
115:                assert providers.size() > 0 : "Must be at least one provider"; // NOI18N
116:                //out();
117:                //out("Do action");
118:                myPrintProviders = providers;
119:
120:                if (withPreview) {
121:                    show();
122:                } else {
123:                    print(true);
124:                }
125:            }
126:
127:            private JPanel createPanel() {
128:                //out("Create Main panel");
129:                JPanel p = new JPanel(new GridBagLayout());
130:                JPanel panel = new JPanel(new GridBagLayout());
131:                GridBagConstraints c = new GridBagConstraints();
132:
133:                // navigate
134:                c.anchor = GridBagConstraints.WEST;
135:                p.add(createNavigatePanel(), c);
136:
137:                // scale
138:                c.weightx = 1.0;
139:                c.weighty = 0.0;
140:                p.add(createScalePanel(), c);
141:
142:                // toggle
143:                c.anchor = GridBagConstraints.EAST;
144:                c.insets = new Insets(TINY_INSET, MEDIUM_INSET, TINY_INSET,
145:                        MEDIUM_INSET);
146:                myToggle = createToggleButton(new ButtonAction(icon(
147:                        Option.class, "toggle"), i18n("TLT_Toggle")) { // NOI18N
148:                    public void actionPerformed(ActionEvent event) {
149:                        toggle();
150:                    }
151:                });
152:                myToggle.addKeyListener(myKeyListener);
153:                p.add(myToggle, c);
154:
155:                c.gridx = 0;
156:                c.gridy = 0;
157:                c.fill = GridBagConstraints.HORIZONTAL;
158:                c.anchor = GridBagConstraints.WEST;
159:                c.insets = new Insets(MEDIUM_INSET, 0, MEDIUM_INSET, 0);
160:                panel.add(p, c);
161:
162:                // scroll
163:                c.gridy++;
164:                c.weightx = 1.0;
165:                c.weighty = 1.0;
166:                c.fill = GridBagConstraints.BOTH;
167:                c.insets = new Insets(0, 0, 0, 0);
168:                panel.add(createScrollPanel(), c);
169:
170:                toggle();
171:
172:                return panel;
173:            }
174:
175:            @Override
176:            protected void updated() {
177:                //out("Update content");
178:                createPapers();
179:                toggle();
180:            }
181:
182:            private void toggle() {
183:                updatePaperNumber();
184:                addPapers();
185:                updateButtons();
186:            }
187:
188:            private JComponent createNavigatePanel() {
189:                JPanel panel = new JPanel(new GridBagLayout());
190:                GridBagConstraints c = new GridBagConstraints();
191:
192:                // first
193:                panel = new JPanel(new GridBagLayout());
194:                c.insets = new Insets(TINY_INSET, TINY_INSET, TINY_INSET,
195:                        TINY_INSET);
196:                myFirst = createButton(new ButtonAction(icon(Option.class,
197:                        "first"), i18n("TLT_First")) { // NOI18N
198:                    public void actionPerformed(ActionEvent event) {
199:                        first();
200:                    }
201:                });
202:                myFirst.addKeyListener(myKeyListener);
203:                panel.add(myFirst, c);
204:
205:                // previous
206:                myPrevious = createButton(new ButtonAction(icon(Option.class,
207:                        "previous"), // NOI18N
208:                        i18n("TLT_Previous")) { // NOI18N
209:                    public void actionPerformed(ActionEvent event) {
210:                        previous();
211:                    }
212:                });
213:                myPrevious.addKeyListener(myKeyListener);
214:                panel.add(myPrevious, c);
215:
216:                // text field
217:                myGoto = new JTextField();
218:                int width = (int) Math
219:                        .round(myPrevious.getPreferredSize().width
220:                                / PREVIEW_FACTOR);
221:                int height = myPrevious.getPreferredSize().height;
222:                myGoto.setPreferredSize(new Dimension(width, height));
223:                myGoto.setMinimumSize(new Dimension(width, height));
224:
225:                InputMap inputMap = myGoto.getInputMap();
226:                ActionMap actionMap = myGoto.getActionMap();
227:
228:                populateInputMap(inputMap);
229:                populateActionMap(actionMap);
230:
231:                myGoto.setHorizontalAlignment(JTextField.CENTER);
232:                myGoto.setToolTipText(i18n("TLT_Goto")); // NOI18N
233:                myGoto.addActionListener(new ActionListener() {
234:                    public void actionPerformed(ActionEvent event) {
235:                        goTo();
236:                    }
237:                });
238:                panel.add(myGoto, c);
239:
240:                // next
241:                myNext = createButton(new ButtonAction(icon(Option.class,
242:                        "next"), i18n("TLT_Next")) { // NOI18N
243:                    public void actionPerformed(ActionEvent event) {
244:                        next();
245:                    }
246:                });
247:                myNext.addKeyListener(myKeyListener);
248:                panel.add(myNext, c);
249:
250:                // last
251:                myLast = createButton(new ButtonAction(icon(Option.class,
252:                        "last"), i18n("TLT_Last")) { // NOI18N
253:                    public void actionPerformed(ActionEvent event) {
254:                        last();
255:                    }
256:                });
257:                myLast.addKeyListener(myKeyListener);
258:                panel.add(myLast, c);
259:
260:                return panel;
261:            }
262:
263:            private void populateInputMap(InputMap inputMap) {
264:                inputMap.put(KeyStroke.getKeyStroke('k'), INCREASE);
265:                inputMap.put(KeyStroke.getKeyStroke('K'), INCREASE);
266:                inputMap.put(KeyStroke.getKeyStroke('+'), INCREASE);
267:                inputMap.put(KeyStroke.getKeyStroke('='), INCREASE);
268:                inputMap.put(KeyStroke.getKeyStroke('g'), DECREASE);
269:                inputMap.put(KeyStroke.getKeyStroke('G'), DECREASE);
270:                inputMap.put(KeyStroke.getKeyStroke('-'), DECREASE);
271:                inputMap.put(KeyStroke.getKeyStroke('_'), DECREASE);
272:                inputMap.put(KeyStroke.getKeyStroke('l'), LAST);
273:                inputMap.put(KeyStroke.getKeyStroke('L'), LAST);
274:                inputMap.put(KeyStroke.getKeyStroke('*'), LAST);
275:                inputMap.put(KeyStroke.getKeyStroke('f'), FIRST);
276:                inputMap.put(KeyStroke.getKeyStroke('F'), FIRST);
277:                inputMap.put(KeyStroke.getKeyStroke('/'), FIRST);
278:            }
279:
280:            private void populateActionMap(ActionMap actionMap) {
281:                actionMap.put(INCREASE, new AbstractAction() {
282:                    public void actionPerformed(ActionEvent event) {
283:                        next();
284:                    }
285:                });
286:                actionMap.put(DECREASE, new AbstractAction() {
287:                    public void actionPerformed(ActionEvent event) {
288:                        previous();
289:                    }
290:                });
291:                actionMap.put(LAST, new AbstractAction() {
292:                    public void actionPerformed(ActionEvent event) {
293:                        last();
294:                    }
295:                });
296:                actionMap.put(FIRST, new AbstractAction() {
297:                    public void actionPerformed(ActionEvent event) {
298:                        first();
299:                    }
300:                });
301:            }
302:
303:            private JComponent createScalePanel() {
304:                //out("Create scale panel");
305:                JPanel panel = new JPanel(new GridBagLayout());
306:                GridBagConstraints c = new GridBagConstraints();
307:
308:                // fit to window
309:                c.insets = new Insets(TINY_INSET, MEDIUM_INSET, TINY_INSET,
310:                        TINY_INSET);
311:                myFit = createButton(new ButtonAction(
312:                        icon(Option.class, "fit"), i18n("TLT_Fit")) { // NOI18N
313:                    public void actionPerformed(ActionEvent event) {
314:                        showCustom(true);
315:                    }
316:                });
317:                myFit.addKeyListener(myKeyListener);
318:                panel.add(myFit, c);
319:
320:                // scale
321:                c.insets = new Insets(TINY_INSET, TINY_INSET, TINY_INSET,
322:                        TINY_INSET);
323:                myScale = new Percent(this , Option.getDefault().getScale(),
324:                        PERCENTS, CUSTOMS.length - 1, CUSTOMS,
325:                        i18n("TLT_Preview_Scale") // NOI18N
326:                );
327:                int width = myScale.getPreferredSize().width;
328:                int height = myPrevious.getPreferredSize().height;
329:                myScale.setPreferredSize(new Dimension(width, height));
330:                myScale.setMinimumSize(new Dimension(width, height));
331:                panel.add(myScale, c);
332:
333:                // decrease
334:                myDecrease = createButton(new ButtonAction(icon(Option.class,
335:                        "minus"), i18n("TLT_Zoom_Out")) { // NOI18N
336:                    public void actionPerformed(ActionEvent event) {
337:                        myScale.decreaseValue();
338:                    }
339:                });
340:                myDecrease.addKeyListener(myKeyListener);
341:                panel.add(myDecrease, c);
342:
343:                // increase
344:                myIncrease = createButton(new ButtonAction(icon(Option.class,
345:                        "plus"), i18n("TLT_Zoom_In")) { // NOI18N
346:                    public void actionPerformed(ActionEvent event) {
347:                        myScale.increaseValue();
348:                    }
349:                });
350:                myIncrease.addKeyListener(myKeyListener);
351:                panel.add(myIncrease, c);
352:
353:                return panel;
354:            }
355:
356:            private JComponent createScrollPanel() {
357:                //out("Create scroll panel");
358:                GridBagConstraints c = new GridBagConstraints();
359:
360:                // papers
361:                myPaperPanel = new JPanel(new GridBagLayout());
362:                myPaperPanel.setBackground(Color.lightGray);
363:                JPanel panel = new JPanel(new GridBagLayout());
364:
365:                c.gridy = 1;
366:                c.anchor = GridBagConstraints.NORTHWEST;
367:                c.weightx = 1.0;
368:                c.weighty = 1.0;
369:                c.insets = new Insets(0, 0, 0, 0);
370:                panel.setBackground(Color.lightGray);
371:                panel.add(myPaperPanel, c);
372:
373:                //  panel.setBorder(new javax.swing.border.LineBorder(java.awt.Color.yellow));
374:                //  optionPanel.setBorder(new javax.swing.border.LineBorder(java.awt.Color.green));
375:                //  myPaperPanel.setBorder(new javax.swing.border.LineBorder(java.awt.Color.green));
376:
377:                // scroll
378:                c.fill = GridBagConstraints.BOTH;
379:                myScrollPane = new MyScrollPane(panel);
380:                myScrollPane.setFocusable(true);
381:
382:                myScrollPane.addWheelListener(new MouseWheelListener() {
383:                    public void mouseWheelMoved(MouseWheelEvent event) {
384:                        if (SwingUtilities.isRightMouseButton(event)
385:                                || event.isControlDown()) {
386:                            myScrollPane.setWheelScrollingEnabled(false);
387:
388:                            if (event.getWheelRotation() > 0) {
389:                                myScale.increaseValue();
390:                            } else {
391:                                myScale.decreaseValue();
392:                            }
393:                        } else {
394:                            myScrollPane.setWheelScrollingEnabled(true);
395:                        }
396:                    }
397:                });
398:                myScrollPane.addMouseListener(new MouseAdapter() {
399:                    public void mouseClicked(MouseEvent event) {
400:                        if (event.getClickCount() == 2) {
401:                            if (SwingUtilities.isRightMouseButton(event)) {
402:                                myScale.customValue(CUSTOMS.length - 1);
403:                                myCustomIndex = 0;
404:                            } else {
405:                                showCustom(true);
406:                            }
407:                        }
408:                    }
409:                });
410:                myScrollPane.addKeyListener(myKeyListener);
411:
412:                return myScrollPane;
413:            }
414:
415:            private void showCustom(boolean doNext) {
416:                if (doNext) {
417:                    myCustomIndex++;
418:
419:                    if (myCustomIndex == CUSTOMS.length) {
420:                        myCustomIndex = 0;
421:                    }
422:                }
423:                myScale.customValue(myCustomIndex);
424:            }
425:
426:            private void updateButtons() {
427:                myGoto.setText(getPaper(myPaperNumber));
428:                myFirst.setEnabled(myPaperNumber > 1);
429:                myPrevious.setEnabled(myPaperNumber > 1);
430:                myNext.setEnabled(myPaperNumber < getPaperCount());
431:                myLast.setEnabled(myPaperNumber < getPaperCount());
432:                boolean enabled = getPaperCount() > 0;
433:                myGoto.setEnabled(enabled);
434:                myScale.setEnabled(enabled);
435:                myToggle.setEnabled(enabled);
436:                myFit.setEnabled(enabled);
437:                myIncrease.setEnabled(enabled);
438:                myDecrease.setEnabled(enabled);
439:                myPrintButton.setEnabled(enabled);
440:            }
441:
442:            private void scrollTo() {
443:                //out("Scroll to: " + myPaperNumber);
444:                Paper paper = myPapers.get(myPaperNumber - 1);
445:                int gap = getGap();
446:                int x = paper.getX() - gap;
447:                int y = paper.getY() - gap;
448:                int w = paper.getWidth();
449:                int h = paper.getHeight();
450:                JViewport view = myScrollPane.getViewport();
451:
452:                if (!view.getViewRect().contains(x, y, w, h)) {
453:                    view.setViewPosition(new Point(x, y));
454:                    updatePaperPanel();
455:                }
456:            }
457:
458:            public double getCustomValue(int index) {
459:                if (getPaperCount() == 0) {
460:                    return 0.0;
461:                }
462:                int width = myPapers.get(0).getPaperWidth() + GAP_SIZE;
463:                int height = myPapers.get(0).getPaperHeight() + GAP_SIZE;
464:
465:                if (index == 0) {
466:                    return getWidthScale(width);
467:                }
468:                if (index == 1) {
469:                    return getHeightScale(height);
470:                }
471:                if (index == 2) {
472:                    return getAllScale(width, height);
473:                }
474:                return 1.0;
475:            }
476:
477:            private double getWidthScale(int width) {
478:                final int JAVA_INSET = 5;
479:
480:                double scrollWidth = (double) (myScrollPane.getWidth()
481:                        - myScrollPane.getVerticalScrollBar().getWidth() - JAVA_INSET);
482:
483:                return scrollWidth / width;
484:            }
485:
486:            private double getHeightScale(int height) {
487:                final int JAVA_INSET = 5;
488:
489:                double scrollHeight = (double) (myScrollPane.getHeight()
490:                        - myScrollPane.getHorizontalScrollBar().getHeight() - JAVA_INSET);
491:
492:                return scrollHeight / height;
493:            }
494:
495:            private double getAllScale(int width, int height) {
496:                int w = width;
497:                int h = height;
498:
499:                if (!isSingleMode()) {
500:                    int maxRow = 0;
501:                    int maxColumn = 0;
502:
503:                    for (Paper paper : myPapers) {
504:                        maxRow = Math.max(maxRow, paper.getRow());
505:                        maxColumn = Math.max(maxColumn, paper.getColumn());
506:                    }
507:                    w *= maxColumn + 1;
508:                    h *= maxRow + 1;
509:                }
510:                return Math.min(getWidthScale(w), getHeightScale(h));
511:            }
512:
513:            public void valueChanged(double value, int index) {
514:                //out();
515:                //out("Set scale: " + value + " " + index);
516:                if (index != -1) {
517:                    myCustomIndex = index;
518:                }
519:                if (getPaperCount() == 0) {
520:                    return;
521:                }
522:                for (Paper paper : myPapers) {
523:                    paper.setScale(value);
524:                }
525:                addPapers();
526:            }
527:
528:            private void addPapers() {
529:                //out("Add papers");
530:                myPaperPanel.removeAll();
531:
532:                if (getPaperCount() == 0) {
533:                    updatePaperPanel();
534:                    return;
535:                }
536:                int gap = getGap();
537:                GridBagConstraints c = new GridBagConstraints();
538:                c.insets = new Insets(gap, gap, 0, 0);
539:
540:                if (isSingleMode()) {
541:                    myPaperPanel.add(myPapers.get(myPaperNumber - 1), c);
542:                } else {
543:                    for (Paper paper : myPapers) {
544:                        c.gridx = paper.getColumn();
545:                        c.gridy = paper.getRow();
546:                        myPaperPanel.add(paper, c);
547:                    }
548:                }
549:                updatePaperPanel();
550:            }
551:
552:            private void updatePaperPanel() {
553:                myPaperPanel.revalidate();
554:                myPaperPanel.repaint();
555:            }
556:
557:            private void createPapers() {
558:                myPapers = new ArrayList<Paper>();
559:
560:                int width = Option.getDefault().getPageWidth();
561:                int height = Option.getDefault().getPageHeight();
562:
563:                double zoom = Option.getDefault().getZoom();
564:                double scale = 1.0;
565:
566:                if (myScale != null) {
567:                    scale = myScale.getValue();
568:                }
569:                int delta = 0;
570:                int number = 0;
571:
572:                for (PrintProvider provider : myPrintProviders) {
573:                    String name = provider.getName();
574:
575:                    if (name == null) {
576:                        name = ""; // NOI18N
577:                    }
578:                    Date modified = provider.getLastModifiedDate();
579:
580:                    if (modified == null) {
581:                        modified = new Date(System.currentTimeMillis());
582:                    }
583:                    PrintPage[][] pages = provider
584:                            .getPages(width, height, zoom);
585:                    //out("Create papers: " + pages.length);
586:
587:                    for (int i = 0; i < pages.length; i++) {
588:                        for (int j = 0; j < pages[i].length; j++) {
589:                            PrintPage page = pages[i][j];
590:
591:                            if (page == null) {
592:                                continue;
593:                            }
594:                            Paper paper = new Paper(page, name, modified);
595:                            paper
596:                                    .setCoordinate(number + 1, i + delta, j,
597:                                            scale);
598:                            myPapers.add(paper);
599:                            number++;
600:                        }
601:                    }
602:                    delta += pages.length;
603:                }
604:                int count = myPapers.size();
605:
606:                for (Paper paper : myPapers) {
607:                    paper.setCount(count);
608:                }
609:            }
610:
611:            private int getPaperCount() {
612:                if (myPapers == null) {
613:                    return 0;
614:                }
615:                return myPapers.size();
616:            }
617:
618:            public void invalidValue(String value) {
619:            }
620:
621:            private void first() {
622:                updatePaperNumber();
623:                changePaper();
624:            }
625:
626:            private void previous() {
627:                if (myPaperNumber == 1) {
628:                    return;
629:                }
630:                myPaperNumber--;
631:                changePaper();
632:            }
633:
634:            private void next() {
635:                if (myPaperNumber == getPaperCount()) {
636:                    return;
637:                }
638:                myPaperNumber++;
639:                changePaper();
640:            }
641:
642:            private void last() {
643:                myPaperNumber = getPaperCount();
644:                changePaper();
645:            }
646:
647:            private void goTo() {
648:                String value = myGoto.getText();
649:                int number = getPaperNumber(value);
650:                int count = getPaperCount();
651:
652:                if (number < 1 || number > count) {
653:                    myGoto.setText(getPaper(myPaperNumber));
654:                } else {
655:                    myPaperNumber = number;
656:                    changePaper();
657:                }
658:                myGoto.selectAll();
659:            }
660:
661:            private void changePaper() {
662:                if (isSingleMode()) {
663:                    addPapers();
664:                } else {
665:                    scrollTo();
666:                }
667:                updateButtons();
668:            }
669:
670:            private void updatePaperNumber() {
671:                myPaperNumber = getPaperCount() == 0 ? 0 : 1;
672:            }
673:
674:            private int getGap() {
675:                return (int) Math.round(GAP_SIZE * myScale.getValue());
676:            }
677:
678:            private String getPaper(int value) {
679:                return Option.getPageOfCount(String.valueOf(value), String
680:                        .valueOf(getPaperCount()));
681:            }
682:
683:            @Override
684:            protected DialogDescriptor createDescriptor() {
685:                Object[] rightButtons = getRightButtons();
686:                Object[] leftButtons = getLeftButtons();
687:
688:                DialogDescriptor descriptor = new DialogDescriptor(
689:                        getResizable(createPanel()),
690:                        i18n("LBL_Print_Preview"), // NOI18N
691:                        true, rightButtons, myPrintButton,
692:                        DialogDescriptor.DEFAULT_ALIGN, null, null);
693:                descriptor.setClosingOptions(new Object[] { myPrintButton,
694:                        myCloseButton });
695:                descriptor.setAdditionalOptions(leftButtons);
696:
697:                return descriptor;
698:            }
699:
700:            @Override
701:            protected void opened() {
702:                //out("Opened");
703:                myScrollPane.requestFocus();
704:            }
705:
706:            @Override
707:            protected void resized() {
708:                if (myScale.isCustomValue()) {
709:                    showCustom(false);
710:                }
711:            }
712:
713:            private Object[] getLeftButtons() {
714:                JButton pageSetup = createButton(new ButtonAction(
715:                        i18n("LBL_Page_Setup_Button"), // NOI18N
716:                        i18n("TLT_Page_Setup_Button")) { // NOI18N
717:                    public void actionPerformed(ActionEvent event) {
718:                        if (Option.getDefault().showPageSetup()) {
719:                            updated();
720:                        }
721:                    }
722:                });
723:                JButton printOption = createButton(new ButtonAction(
724:                        i18n("LBL_Print_Option_Button"), // NOI18N
725:                        i18n("TLT_Print_Option_Button")) { // NOI18N
726:                    public void actionPerformed(ActionEvent event) {
727:                        option();
728:                    }
729:                });
730:                printOption.addKeyListener(myKeyListener);
731:
732:                return new Object[] { pageSetup, printOption, };
733:            }
734:
735:            private Object[] getRightButtons() {
736:                myPrintButton = createButton(new ButtonAction(
737:                        i18n("LBL_Print_Button"), // NOI18N
738:                        i18n("TLT_Print_Button")) { // NOI18N
739:                    public void actionPerformed(ActionEvent event) {
740:                        print(false);
741:                    }
742:                });
743:                myPrintButton.addKeyListener(myKeyListener);
744:
745:                myCloseButton = createButton(new ButtonAction(
746:                        i18n("LBL_Close_Button"), // NOI18N
747:                        i18n("TLT_Close_Button")) { // NOI18N
748:                    public void actionPerformed(ActionEvent event) {
749:                        close();
750:                    }
751:                });
752:                myCloseButton.addKeyListener(myKeyListener);
753:
754:                return new Object[] { myPrintButton, myCloseButton, };
755:            }
756:
757:            private void close() {
758:                //out("Closed");
759:                myPapers = null;
760:                myPrintProviders = null;
761:                Option.getDefault().setScale(myScale.getValue());
762:            }
763:
764:            private boolean isSingleMode() {
765:                return myToggle.isSelected();
766:            }
767:
768:            private void print(boolean doUpdate) {
769:                if (doUpdate) {
770:                    createPapers();
771:                }
772:                myPrinter.print(myPapers);
773:            }
774:
775:            private void option() {
776:                new Attribute(this ).show();
777:            }
778:
779:            private int getPaperNumber(String text) {
780:                String value = text.trim();
781:                StringBuffer buffer = new StringBuffer();
782:
783:                for (int i = 0; i < value.length(); i++) {
784:                    char c = value.charAt(i);
785:
786:                    if (!isAplha(c)) {
787:                        break;
788:                    }
789:                    buffer.append(c);
790:                }
791:                return getInt(buffer.toString());
792:            }
793:
794:            private boolean isAplha(char c) {
795:                return "0123456789".indexOf(c) != -1; // NOI18N
796:            }
797:
798:            // ----------------------------------------------------------
799:            private static final class MyScrollPane extends JScrollPane {
800:                MyScrollPane(JPanel panel) {
801:                    super (panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
802:                            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
803:                    getVerticalScrollBar().setUnitIncrement(SCROLL_INCREMENT);
804:
805:                    int screenHeight = Toolkit.getDefaultToolkit()
806:                            .getScreenSize().height;
807:                    int height = (int) Math
808:                            .round(screenHeight * PREVIEW_FACTOR);
809:                    int width = (int) Math.round(height * PREVIEW_FACTOR);
810:
811:                    Dimension dimension = new Dimension(width, height);
812:                    setMinimumSize(dimension);
813:                    setPreferredSize(dimension);
814:                }
815:
816:                public void addMouseWheelListener(MouseWheelListener listener) {
817:                    if (myMouseWheelListeners == null) {
818:                        myMouseWheelListeners = new ArrayList<MouseWheelListener>();
819:                    }
820:                    //out("Listener: " + listener.getClass().getName());
821:                    myMouseWheelListeners.add(listener);
822:                }
823:
824:                public void addWheelListener(MouseWheelListener wheelListener) {
825:                    super .addMouseWheelListener(wheelListener);
826:
827:                    for (int i = 0; i < myMouseWheelListeners.size(); i++) {
828:                        super .addMouseWheelListener(myMouseWheelListeners
829:                                .get(i));
830:                    }
831:                }
832:
833:                private List<MouseWheelListener> myMouseWheelListeners;
834:            }
835:
836:            private JPanel myPaperPanel;
837:            private List<Paper> myPapers;
838:
839:            private JButton myFirst;
840:            private JButton myPrevious;
841:            private JButton myNext;
842:            private JButton myLast;
843:
844:            private JButton myFit;
845:            private JButton myIncrease;
846:            private JButton myDecrease;
847:
848:            private JButton myPrintButton;
849:            private JButton myCloseButton;
850:
851:            private Percent myScale;
852:            private JTextField myGoto;
853:            private int myPaperNumber;
854:            private int myCustomIndex;
855:            private JToggleButton myToggle;
856:            private MyScrollPane myScrollPane;
857:            private KeyListener myKeyListener;
858:
859:            private Printer myPrinter;
860:            private List<PrintProvider> myPrintProviders;
861:
862:            private static final int GAP_SIZE = 20;
863:            private static final int SCROLL_INCREMENT = 40;
864:            private static final double PREVIEW_FACTOR = 0.75;
865:            private static final int[] PERCENTS = new int[] { 25, 50, 75, 100,
866:                    200, 400 };
867:
868:            private static final String INCREASE = "increase"; // NOI18N
869:            private static final String DECREASE = "decrease"; // NOI18N
870:            private static final String LAST = "last"; // NOI18N
871:            private static final String FIRST = "first"; // NOI18N
872:
873:            private final String[] CUSTOMS = new String[] {
874:                    i18n("LBL_Fit_to_Width"), // NOI18N
875:                    i18n("LBL_Fit_to_Height"), // NOI18N
876:                    i18n("LBL_Fit_to_All"), // NOI18N
877:            };
878:            private static final Preview DEFAULT = new Preview();
879:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.