001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.printing.ui.internal;
018:
019: import java.awt.print.PrinterJob;
020:
021: import net.refractions.udig.printing.model.Page;
022: import net.refractions.udig.printing.ui.internal.editor.PageEditorInput;
023: import net.refractions.udig.project.ui.UDIGEditorInput;
024:
025: import org.eclipse.core.runtime.IProgressMonitor;
026: import org.eclipse.core.runtime.IStatus;
027: import org.eclipse.core.runtime.Status;
028: import org.eclipse.core.runtime.jobs.Job;
029: import org.eclipse.jface.action.Action;
030: import org.eclipse.jface.action.IAction;
031: import org.eclipse.jface.viewers.ISelection;
032: import org.eclipse.ui.IEditorActionDelegate;
033: import org.eclipse.ui.IEditorPart;
034: import org.eclipse.ui.PlatformUI;
035:
036: /**
037: * Provides ...TODO summary sentence
038: * <p>
039: * TODO Description
040: * </p><p>
041: * Responsibilities:
042: * <ul>
043: * <li>
044: * <li>
045: * </ul>
046: * </p><p>
047: * Example Use:<pre><code>
048: * PrintAction x = new PrintAction( ... );
049: * TODO code example
050: * </code></pre>
051: * </p>
052: * @author Richard Gould
053: * @since 0.3
054: */
055: public class PrintAction extends Action implements
056: IEditorActionDelegate {
057:
058: public PrintAction() {
059: super ();
060: }
061:
062: public void run() {
063: Page page = null;
064: UDIGEditorInput editorInput = (UDIGEditorInput) PlatformUI
065: .getWorkbench().getActiveWorkbenchWindow()
066: .getActivePage().getActiveEditor().getEditorInput();
067: if (editorInput instanceof PageEditorInput) {
068: page = (Page) ((PageEditorInput) editorInput)
069: .getProjectElement();
070: }
071: if (page == null) {
072: throw new RuntimeException(Messages.PrintAction_pageError);
073: }
074:
075: final String jobName = page.getName();
076:
077: final PrintingEngine engine = new PrintingEngine(page);
078:
079: Job job = new Job(Messages.PrintAction_jobTitle) {
080: protected IStatus run(IProgressMonitor monitor) {
081:
082: engine.setMonitor(monitor);
083:
084: PrinterJob printerJob = PrinterJob.getPrinterJob();
085:
086: engine.setPrinterJob(printerJob);
087:
088: if (printerJob.printDialog()) {
089: try {
090: printerJob.setPageable(engine);
091: printerJob.setJobName(jobName);
092: printerJob.print();
093: } catch (Exception e) {
094: e.printStackTrace();
095: }
096: }
097:
098: return Status.OK_STATUS;
099: }
100: };
101:
102: if (job.isSystem())
103: job.setSystem(false);
104:
105: job.schedule();
106: }
107:
108: /**
109: * TODO summary sentence for setActiveEditor ...
110: *
111: * @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.eclipse.jface.action.IAction, org.eclipse.ui.IEditorPart)
112: * @param action
113: * @param targetEditor
114: */
115: public void setActiveEditor(IAction action, IEditorPart targetEditor) {
116: }
117:
118: /**
119: * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
120: * @param action
121: */
122: public void run(IAction action) {
123: run();
124: }
125:
126: /**
127: * TODO summary sentence for selectionChanged ...
128: *
129: * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
130: * @param action
131: * @param selection
132: */
133: public void selectionChanged(IAction action, ISelection selection) {
134: }
135: }
|