Print fucntion for xpad
Sylvestre Koumar [Mon, 21 Sep 2009 12:02:18 +0000 (14:02 +0200)]
scilab/modules/xpad/src/java/org/scilab/modules/xpad/actions/PageSetupAction.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/actions/PrintAction.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/actions/PrintPreviewAction.java

index b689874..9113321 100644 (file)
 
 package org.scilab.modules.xpad.actions;
 
+import java.awt.print.PageFormat;
+import java.awt.print.PrinterJob;
+
+import javax.print.attribute.HashPrintRequestAttributeSet;
+
 import org.scilab.modules.gui.menuitem.MenuItem;
 import org.scilab.modules.xpad.Xpad;
 
 public class PageSetupAction extends DefaultAction {
+       
+       private static PageFormat pageFormat;
 
        private PageSetupAction(Xpad editor) {
                super("Page Setup...", editor);
        }
 
+    public void doAction() {
+       //PrinterJob printerJob = PrinterJob.getPrinterJob();
+       //pageFormat = printerJob.pageDialog(new HashPrintRequestAttributeSet());
+    }
+    
         public static MenuItem createMenu(Xpad editor) {
                return createMenu("Page Setup...", null, new PageSetupAction(editor), null);
         }
+        
+        public static PageFormat getPageFormat() {
+                return pageFormat;
+        }
 }
index 4f4bde9..84ec4aa 100644 (file)
@@ -14,11 +14,14 @@ package org.scilab.modules.xpad.actions;
 
 import java.awt.event.ActionEvent;
 import java.awt.event.KeyEvent;
+import java.awt.print.PrinterException;
+import java.awt.print.PrinterJob;
 
 import javax.swing.KeyStroke;
 
 import org.scilab.modules.gui.menuitem.MenuItem;
 import org.scilab.modules.gui.pushbutton.PushButton;
+import org.scilab.modules.gui.utils.PrinterWriter;
 import org.scilab.modules.xpad.Xpad;
 
 public class PrintAction extends DefaultAction {
@@ -26,7 +29,11 @@ public class PrintAction extends DefaultAction {
        private PrintAction(Xpad editor) {
                super("Print...", editor);
        }
-
+       
+    public void doAction() {
+       printXpadDocument(getEditor());
+    }
+    
        public static MenuItem createMenu(Xpad editor) {
            return createMenu("Print...", null, new PrintAction(editor), KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));
        }
@@ -34,5 +41,20 @@ public class PrintAction extends DefaultAction {
        public static PushButton createButton(Xpad editor) {
            return createButton("Print...", "document-print.png", new PrintAction(editor));
        }
-       
+
+       public static boolean printXpadDocument(Xpad editor) {
+
+               PrinterJob printTask = PrinterJob.getPrinterJob();
+               printTask.setPrintable(new PrinterWriter(editor.getTextPane()));
+
+               if (printTask.printDialog()) { 
+                       try{
+                               printTask.print();
+                       }catch (PrinterException e) {
+                               e.printStackTrace();
+                               return false;
+                       }
+               }
+               return true;
+       }
 }
index 439b996..ab0c3ec 100644 (file)
@@ -14,7 +14,9 @@ package org.scilab.modules.xpad.actions;
 
 import java.awt.event.ActionEvent;
 import java.awt.event.KeyEvent;
+import java.awt.print.PrinterJob;
 
+import javax.print.attribute.HashPrintRequestAttributeSet;
 import javax.swing.KeyStroke;
 
 import org.scilab.modules.gui.menuitem.MenuItem;
@@ -27,6 +29,10 @@ public class PrintPreviewAction extends DefaultAction {
        super("Print Preview...", editor);
     }
     
+//    public void doAction() {
+//
+//    }
+
     public static MenuItem createMenu(Xpad editor) {
        return createMenu("Print Preview...", null, new PrintPreviewAction(editor), KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK+ActionEvent.SHIFT_MASK));
     }