001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright � 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.print;
051:
052: import java.awt.BorderLayout;
053: import java.awt.Color;
054: import java.awt.Dimension;
055: import java.awt.Graphics;
056: import java.awt.Graphics2D;
057: import java.awt.event.ActionEvent;
058: import java.awt.geom.AffineTransform;
059: import java.awt.geom.Dimension2D;
060: import java.awt.geom.Rectangle2D;
061: import java.awt.image.BufferedImage;
062: import java.awt.print.PageFormat;
063: import java.awt.print.PrinterException;
064: import java.util.ArrayList;
065:
066: import javax.swing.Action;
067: import javax.swing.JButton;
068: import javax.swing.JFrame;
069: import javax.swing.JPanel;
070: import javax.swing.JScrollPane;
071: import javax.swing.JToolBar;
072: import javax.swing.ScrollPaneConstants;
073: import javax.swing.WindowConstants;
074:
075: import org.apache.batik.util.gui.resource.ActionMap;
076: import org.apache.batik.util.gui.resource.MissingListenerException;
077:
078: import com.projity.help.HelpUtil;
079: import com.projity.menu.MenuActionConstants;
080: import com.projity.menu.MenuActionsMap;
081: import com.projity.menu.MenuManager;
082: import com.projity.pm.graphic.IconManager;
083: import com.projity.strings.Messages;
084: import com.projity.util.Environment;
085:
086: public class PrintPreviewFrame extends JFrame implements ActionMap,
087: MenuActionConstants {
088:
089: private PagePanel pagePanel;
090:
091: protected PrintDocument document;
092: private int pageIndex = 0;
093: private double zoom;
094:
095: public PrintPreviewFrame(PrintDocument document) {
096: super ();
097: this .document = document;
098: zoom = 1 / document.getPageZoom();
099: init();
100: HelpUtil.addDocHelp(this , "Print_Preview");
101: }
102:
103: protected MenuManager menuManager;
104:
105: public void init() {
106: setSize(800, 600);
107: setExtendedState(MAXIMIZED_BOTH);
108: setTitle(Messages.getString("PrintPreviewFrame.PrintPreview")); //$NON-NLS-1$
109: setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
110:
111: menuManager = MenuManager.getInstance(this );
112: JToolBar toolBar = menuManager
113: .getToolBar(MenuManager.PRINT_PREVIEW_TOOL_BAR);
114: getContentPane().add(toolBar, BorderLayout.BEFORE_FIRST_LINE);
115: actionsMap.setEnabledDocumentMenuActions(true);
116:
117: pagePanel = new PagePanel();
118: pagePanel.setPreferredSize(new Dimension((int) Math
119: .floor(document.getPageFormat().getWidth()), (int) Math
120: .floor(document.getPageFormat().getHeight())));
121: getContentPane()
122: .add(
123: new JScrollPane(
124: pagePanel,
125: ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
126: ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS),
127: BorderLayout.CENTER);
128: }
129:
130: protected MenuActionsMap actionsMap;
131:
132: public void addHandlers() {
133: actionsMap = new MenuActionsMap(this , menuManager);
134: actionsMap.addHandler(ACTION_PRINTPREVIEW_PRINT,
135: new PrintAction());
136: actionsMap.addHandler(ACTION_PRINTPREVIEW_FORMAT,
137: new FormatAction());
138: actionsMap.addHandler(ACTION_PRINTPREVIEW_BACK,
139: new BackAction());
140: actionsMap.addHandler(ACTION_PRINTPREVIEW_FORWARD,
141: new ForwardAction());
142: actionsMap.addHandler(ACTION_PRINTPREVIEW_UP, new UpAction());
143: actionsMap.addHandler(ACTION_PRINTPREVIEW_DOWN,
144: new DownAction());
145: actionsMap.addHandler(ACTION_PRINTPREVIEW_FIRST,
146: new FirstAction());
147: actionsMap.addHandler(ACTION_PRINTPREVIEW_LAST,
148: new LastAction());
149: actionsMap.addHandler(ACTION_PRINTPREVIEW_ZOOMIN,
150: new ZoomInAction());
151: actionsMap.addHandler(ACTION_PRINTPREVIEW_ZOOMOUT,
152: new ZoomOutAction());
153: actionsMap.addHandler(ACTION_PRINTPREVIEW_ZOOMRESET,
154: new ZoomResetAction());
155: actionsMap.addHandler(ACTION_PRINTPREVIEW_LEFT_VIEW,
156: new LeftViewAction());
157: actionsMap.addHandler(ACTION_PRINTPREVIEW_RIGHT_VIEW,
158: new RightViewAction());
159: }
160:
161: public Action getAction(String key) throws MissingListenerException {
162: if (actionsMap == null)
163: addHandlers();
164:
165: Action action = actionsMap.getConcreteAction(key);
166: if (action == null)
167: throw new MissingListenerException(
168: "no listener for PrintPreviewFrame", getClass().getName(), key); //$NON-NLS-1$
169:
170: return action;
171: }
172:
173: public String getStringFromAction(Action action)
174: throws MissingListenerException {
175: if (actionsMap == null)
176: addHandlers();
177: return actionsMap.getStringFromAction(action);
178: }
179:
180: public class PrintAction extends MenuActionsMap.GlobalMenuAction {
181: public void actionPerformed(ActionEvent arg0) {
182: document.print();
183: }
184: }
185:
186: public class FormatAction extends MenuActionsMap.GlobalMenuAction {
187: public void actionPerformed(ActionEvent arg0) {
188: document.format();
189: }
190: }
191:
192: public class BackAction extends MenuActionsMap.GlobalMenuAction {
193: public void actionPerformed(ActionEvent arg0) {
194: previousPage();
195: }
196: }
197:
198: public class ForwardAction extends MenuActionsMap.GlobalMenuAction {
199: public void actionPerformed(ActionEvent arg0) {
200: nextPage();
201: }
202: }
203:
204: public class UpAction extends MenuActionsMap.GlobalMenuAction {
205: public void actionPerformed(ActionEvent arg0) {
206: upperPage();
207: }
208: }
209:
210: public class DownAction extends MenuActionsMap.GlobalMenuAction {
211: public void actionPerformed(ActionEvent arg0) {
212: lowerPage();
213: }
214: }
215:
216: public class FirstAction extends MenuActionsMap.GlobalMenuAction {
217: public void actionPerformed(ActionEvent arg0) {
218: firstPage();
219: }
220: }
221:
222: public class LastAction extends MenuActionsMap.GlobalMenuAction {
223: public void actionPerformed(ActionEvent arg0) {
224: lastPage();
225: }
226: }
227:
228: public class ZoomInAction extends MenuActionsMap.GlobalMenuAction {
229: public void actionPerformed(ActionEvent arg0) {
230: zoomIn();
231: }
232: }
233:
234: public class ZoomResetAction extends
235: MenuActionsMap.GlobalMenuAction {
236: public void actionPerformed(ActionEvent arg0) {
237: zoomReset();
238: }
239: }
240:
241: public class ZoomOutAction extends MenuActionsMap.GlobalMenuAction {
242: public void actionPerformed(ActionEvent arg0) {
243: zoomOut();
244: }
245: }
246:
247: public class LeftViewAction extends MenuActionsMap.GlobalMenuAction {
248: public void actionPerformed(ActionEvent arg0) {
249: toggleLeftView();
250: }
251:
252: protected boolean needsDocument() {
253: return true;
254: } //otherwise allowed won't be checked
255:
256: protected boolean allowed(boolean enable) {
257: if (!(document instanceof GraphPageable))
258: return false;
259: GraphPageable gp = (GraphPageable) document;
260: return gp.getRenderer().getParams()
261: .isSupportLeftAndRightParts();
262: }
263: }
264:
265: public class RightViewAction extends
266: MenuActionsMap.GlobalMenuAction {
267: public void actionPerformed(ActionEvent arg0) {
268: toggleRightView();
269: }
270:
271: protected boolean needsDocument() {
272: return true;
273: } //otherwise allowed won't be checked
274:
275: protected boolean allowed(boolean enable) {
276: if (!(document instanceof GraphPageable))
277: return false;
278: GraphPageable gp = (GraphPageable) document;
279: return gp.getRenderer().getParams()
280: .isSupportLeftAndRightParts();
281: }
282: }
283:
284: public void setZoomOut(double zoom) {
285: this .zoom = zoom;
286: Dimension2D size = pagePanel.getSize();
287: pagePanel.setSize((int) (size.getWidth() * zoom), (int) (size
288: .getHeight() * zoom));
289: //TODO loose of precision due to (int)
290: }
291:
292: public double getZoom() {
293: return zoom;
294: }
295:
296: public void nextPage() {
297: if (pageIndex < document.getNumberOfPages() - 1) {
298: pageIndex++;
299: update();
300: }
301: }
302:
303: public void previousPage() {
304: if (pageIndex > 0) {
305: pageIndex--;
306: update();
307: }
308: }
309:
310: public void lowerPage() {
311: if (pageIndex < document.getNumberOfPages()
312: - document.getColumnCount()) {
313: pageIndex += document.getColumnCount();
314: update();
315: }
316: }
317:
318: public void upperPage() {
319: if (pageIndex >= document.getColumnCount()) {
320: pageIndex -= document.getColumnCount();
321: update();
322: }
323: }
324:
325: public void firstPage() {
326: if (pageIndex != 0) {
327: pageIndex = 0;
328: update();
329: }
330: }
331:
332: public void lastPage() {
333: if (pageIndex != document.getNumberOfPages() - 1) {
334: pageIndex = document.getNumberOfPages() - 1;
335: update();
336: }
337: }
338:
339: public void print() {
340: document.print();
341: }
342:
343: protected void update() {
344: document.update();
345: pagePanel.repaint();
346: }
347:
348: protected void zoomIn() {
349: zoom *= 1.25;
350: update();
351: }
352:
353: protected void zoomOut() {
354: zoom /= 1.25;
355: update();
356: }
357:
358: protected void zoomReset() {
359: zoom = 1.0;
360: update();
361: }
362:
363: protected void toggleLeftView() {
364: if (document instanceof GraphPageable) {
365: GraphPageable gp = (GraphPageable) document;
366: boolean visible = gp.getRenderer().getParams()
367: .isLeftPartVisible();
368: gp.getRenderer().getParams().setLeftPartVisible(!visible);
369: if (visible)
370: gp.getRenderer().getParams().setRightPartVisible(true);
371: pageIndex = 0;
372: updateButtons();
373: update();
374: }
375: }
376:
377: protected void toggleRightView() {
378: if (document instanceof GraphPageable) {
379: GraphPageable gp = (GraphPageable) document;
380: boolean visible = gp.getRenderer().getParams()
381: .isRightPartVisible();
382: gp.getRenderer().getParams().setRightPartVisible(!visible);
383: if (visible)
384: gp.getRenderer().getParams().setLeftPartVisible(true);
385: pageIndex = 0;
386: updateButtons();
387: update();
388: }
389: }
390:
391: protected void updateButtons() {
392: if (document instanceof GraphPageable) {
393: GraphPageable gp = (GraphPageable) document;
394:
395: boolean visible = gp.getRenderer().getParams()
396: .isLeftPartVisible();
397: ArrayList buttons = menuManager.getToolBarFactory()
398: .getButtonsFromId("PrintPreviewLeftView");
399: if (buttons != null && buttons.size() == 1) {
400: JButton b = (JButton) buttons.get(0);
401: b.setIcon(IconManager
402: .getIcon(visible ? "print.leftView"
403: : "print.leftViewHidden"));
404: }
405: menuManager.setActionSelected(
406: ACTION_PRINTPREVIEW_LEFT_VIEW, visible);
407:
408: visible = gp.getRenderer().getParams().isRightPartVisible();
409: buttons = menuManager.getToolBarFactory().getButtonsFromId(
410: "PrintPreviewRightView");
411: if (buttons != null && buttons.size() == 1) {
412: JButton b = (JButton) buttons.get(0);
413: b.setIcon(IconManager
414: .getIcon(visible ? "print.rightView"
415: : "print.rightViewHidden"));
416: }
417: menuManager.setActionSelected(
418: ACTION_PRINTPREVIEW_RIGHT_VIEW, visible);
419: }
420: }
421:
422: public PagePanel getPagePanel() {
423: return pagePanel;
424: }
425:
426: public class PagePanel extends JPanel {
427: public PagePanel() {
428: super ();
429: document.update();
430: }
431:
432: public void paint(Graphics g) {
433:
434: super .paint(g);
435:
436: setBackground(Color.GRAY);
437:
438: Dimension size = getSize();
439: double pw = size.getWidth();
440: double ph = size.getHeight();
441:
442: BufferedImage doubleBuffer = new BufferedImage(size.width,
443: size.height, BufferedImage.TYPE_INT_RGB);
444: Graphics2D g2 = (Graphics2D) doubleBuffer.getGraphics();
445:
446: AffineTransform svgTransform = g2.getTransform();
447: Color svgColor = g2.getColor();
448: //Stroke svgStroke=g2.getStroke();
449:
450: g2.setColor(Color.GRAY);
451: g2.fill(new Rectangle2D.Double(0.0, 0.0, pw, ph));
452:
453: g2.scale(zoom, zoom);
454:
455: PageFormat pageFormat = document.getPageFormat();
456: double dw = 0.0, dh = 0.0;
457: pw /= zoom;
458: ph /= zoom;
459: if (pw > pageFormat.getWidth())
460: dw = (pw - pageFormat.getWidth()) / 2;
461: if (ph > pageFormat.getHeight())
462: dh = (ph - pageFormat.getHeight()) / 2;
463: g2.translate(dw, dh);
464:
465: g2.setColor(Color.WHITE);
466: g2.fill(new Rectangle2D.Double(0.0, 0.0, pageFormat
467: .getWidth(), pageFormat.getHeight()));
468:
469: g2.setColor(svgColor);
470: //g2.setStroke(svgStroke);
471:
472: try {
473: document.print(g2, pageIndex);
474: } catch (PrinterException e) {
475: // TODO Auto-generated catch block
476: e.printStackTrace();
477: }
478:
479: g2.setTransform(svgTransform);
480: if (doubleBuffer != null)
481: g.drawImage(doubleBuffer, 0, 0, this);
482:
483: }
484:
485: }
486: }
|