SWT (JFace) printing function

Source: Internet
Author: User

The Demo code is as follows: Copy codeThe Code is as follows: package swt_jface.demo11;
Import org. eclipse. swt. graphics. GC;
Import org. eclipse. swt. printing. PrintDialog;
Import org. eclipse. swt. printing. Printer;
Import org. eclipse. swt. printing. PrinterData;
Import org. eclipse. swt. widgets. Display;
Import org. eclipse. swt. widgets. Shell;
Public class SimplePrint {
Display display = new Display ();
Shell shell = new Shell (display );
Public SimplePrint (){
Shell. pack ();
Shell. open ();
PrintDialog dialog = new PrintDialog (shell );
PrinterData printerData = dialog. open ();
If (printerData! = Null ){
Printer printer = new Printer (printerData );
If (printer. startJob ("Text ")){
GC gc = new GC (printer );
If (printer. startPage ()){
Gc. drawString ("Eclipse", 200,200 );
Printer. endPage ();
}
Gc. dispose ();
Printer. endJob ();
}
Printer. dispose ();
System. out. println ("Print job done .");
}
While (! Shell. isDisposed ()){
If (! Display. readAndDispatch ()){
Display. sleep ();
}
}
Display. dispose ();
}
Public static void main (String [] args ){
New SimplePrint ();
}
}

Add the print button to the General Main Menu:Copy codeThe Code is as follows: package swt_jface.demo11;
Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. Reader;
Import org. eclipse. jface. window. ApplicationWindow;
Import org. eclipse. jface. window. WindowManager;
Import org. eclipse. swt. SWT;
Import org. eclipse. swt. custom. StyledText;
Import org. eclipse. swt. custom. StyledTextPrintOptions;
Import org. eclipse. swt. events. SelectionAdapter;
Import org. eclipse. swt. events. SelectionEvent;
Import org. eclipse. swt. graphics. Color;
Import org. eclipse. swt. graphics. Font;
Import org. eclipse. swt. graphics. FontData;
Import org. eclipse. swt. graphics. GC;
Import org. eclipse. swt. graphics. Point;
Import org. eclipse. swt. graphics. RGB;
Import org. eclipse. swt. graphics. Rectangle;
Import org. eclipse. swt. layout. FillLayout;
Import org. eclipse. swt. printing. PrintDialog;
Import org. eclipse. swt. printing. Printer;
Import org. eclipse. swt. printing. PrinterData;
Import org. eclipse. swt. widgets. ColorDialog;
Import org. eclipse. swt. widgets. Display;
Import org. eclipse. swt. widgets. FileDialog;
Import org. eclipse. swt. widgets. FontDialog;
Import org. eclipse. swt. widgets. Menu;
Import org. eclipse. swt. widgets. MenuItem;
Import org. eclipse. swt. widgets. MessageBox;
Import org. eclipse. swt. widgets. Shell;
Import org. eclipse. swt. widgets. Text;
Public class Main {

ApplicationWindow applicationWindow;
WindowManager manager;

StyledTextPrintOptions styledTextPrintOptions;
StyledText styledText;

Display display;
Shell shell;
Text text;
Font font;
Color foregroundColor, backgroundColor;

Printer printer;
GC;
Font printerFont;
Color printerForegroundColor, printerBackgroundColor;
Int lineHeight = 0;
Int tabWidth = 0;
Int leftMargin, rightMargin, topMargin, bottomMargin;
Int x, y;
Int index, end;
String textToPrint;
String tabs;
StringBuffer wordBuffer;
Public static void main (String [] args ){
New Main (). open ();
}

Void open (){
Display = new Display ();
Font = new Font (display, "Courier", 10, SWT. NORMAL );
ForegroundColor = display. getSystemColor (SWT. COLOR_BLACK );
BackgroundColor = display. getSystemColor (SWT. COLOR_WHITE );
Shell = new Shell (display );
Shell. setLayout (new FillLayout ());
Shell. setText ("Print Text ");
Shell. setMaximized (true );
Text = new Text (shell, SWT. BORDER | SWT. MULTI | SWT. V_SCROLL | SWT. H_SCROLL );
Text. setFont (font );
Text. setForeground (foregroundColor );
Text. setBackground (backgroundColor );

Menu menuBar = new Menu (shell, SWT. BAR );
Shell. setMenuBar (menuBar );
MenuItem item = new MenuItem (menuBar, SWT. CASCADE );
Item. setText ("& File ");
Menu fileMenu = new Menu (shell, SWT. DROP_DOWN );
Item. setMenu (fileMenu );
Item = new MenuItem (fileMenu, SWT. PUSH );
Item. setText ("& Open ...");
Item. setAccelerator (SWT. CTRL + 'O ');
Item. addSelectionListener (new SelectionAdapter (){
Public void widgetSelected (SelectionEvent event ){
MenuOpen ();
}
});
Item = new MenuItem (fileMenu, SWT. PUSH );
Item. setText ("Font ...");
Item. addSelectionListener (new SelectionAdapter (){
Public void widgetSelected (SelectionEvent event ){
MenuFont ();
}
});
Item = new MenuItem (fileMenu, SWT. PUSH );
Item. setText ("Foreground Color ...");
Item. addSelectionListener (new SelectionAdapter (){
Public void widgetSelected (SelectionEvent event ){
MenuForegroundColor ();
}
});
Item = new MenuItem (fileMenu, SWT. PUSH );
Item. setText ("Background Color ...");
Item. addSelectionListener (new SelectionAdapter (){
Public void widgetSelected (SelectionEvent event ){
MenuBackgroundColor ();
}
});
Item = new MenuItem (fileMenu, SWT. PUSH );
Item. setText ("& Print ...");
Item. setAccelerator (SWT. CTRL + 'P ');
Item. addSelectionListener (new SelectionAdapter (){
Public void widgetSelected (SelectionEvent event ){
MenuPrint ();
}
});
New MenuItem (fileMenu, SWT. SEPARATOR );
Item = new MenuItem (fileMenu, SWT. PUSH );
Item. setText ("E & xit ");
Item. addSelectionListener (new SelectionAdapter (){
Public void widgetSelected (SelectionEvent event ){
System. exit (0 );
}
});

Shell. pack ();
Shell. open ();
While (! Shell. isDisposed ()){
If (! Display. readAndDispatch () display. sleep ();
}
If (font! = Null) font. dispose ();
If (foregroundColor! = Null) foregroundColor. dispose ();
If (backgroundColor! = Null) backgroundColor. dispose ();
}

Void menuOpen (){
Final String textString;
FileDialog dialog = new FileDialog (shell, SWT. OPEN );
Dialog. setFilterExtensions (new String [] {"*. java ","*.*"});
String name = dialog. open ();
If (name = null) | (name. length () = 0) return;

Try {
File file = new File (name );
FileInputStream stream = new FileInputStream (file. getPath ());
Try {
Reader in = new BufferedReader (new InputStreamReader (stream ));
Char [] readBuffer = new char [2048];
StringBuffer buffer = new StringBuffer (int) file. length ());
Int n;
While (n = in. read (readBuffer)> 0 ){
Buffer. append (readBuffer, 0, n );
}
TextString = buffer. toString ();
Stream. close ();
} Catch (IOException e ){
MessageBox box = new MessageBox (shell, SWT. ICON_ERROR );
Box. setMessage ("Error reading file: \ n" + name );
Box. open ();
Return;
}
} Catch (FileNotFoundException e ){
MessageBox box = new MessageBox (shell, SWT. ICON_ERROR );
Box. setMessage ("File not found: \ n" + name );
Box. open ();
Return;
}
Text. setText (textString );
}
Void menuFont (){
FontDialog fontDialog = new FontDialog (shell );
FontDialog. setFontList (font. getFontData ());
FontData fontData = fontDialog. open ();
If (fontData! = Null ){
If (font! = Null) font. dispose ();
Font = new Font (display, fontData );
Text. setFont (font );
}
}
Void menuForegroundColor (){
ColorDialog colorDialog = new ColorDialog (shell );
ColorDialog. setRGB (foregroundColor. getRGB ());
RGB rgb = colorDialog. open ();
If (rgb! = Null ){
If (foregroundColor! = Null) foregroundColor. dispose ();
ForegroundColor = new Color (display, rgb );
Text. setForeground (foregroundColor );
}
}
Void menuBackgroundColor (){
ColorDialog colorDialog = new ColorDialog (shell );
ColorDialog. setRGB (backgroundColor. getRGB ());
RGB rgb = colorDialog. open ();
If (rgb! = Null ){
If (backgroundColor! = Null) backgroundColor. dispose ();
BackgroundColor = new Color (display, rgb );
Text. setBackground (backgroundColor );
}
}
Void menuPrint (){
PrintDialog dialog = new PrintDialog (shell, SWT. NULL );
PrinterData data = dialog. open ();
If (data = null) return;
If (data. printToFile ){
Data. fileName = "print. out ";
}

TextToPrint = text. getText ();
Printer = new Printer (data );
Thread printingThread = new Thread ("Printing "){
Public void run (){
Print (printer );
Printer. dispose ();
}
};
PrintingThread. start ();
}

Void print (Printer printer ){
If (printer. startJob ("Text ")){
Rectangle clientArea = printer. getClientArea ();
Rectangle trim = printer. computeTrim (0, 0, 0, 0 );
Point dpi = printer. getDPI ();
LeftMargin = dpi. x + trim. x;
RightMargin = clientArea. width-dpi. x + trim. x + trim. width;
TopMargin = dpi. y + trim. y;
BottomMargin = clientArea. height-dpi. y + trim. y + trim. height;

Int tabSize = 4;
StringBuffer tabBuffer = new StringBuffer (tabSize );
For (int I = 0; I <tabSize; I ++) tabBuffer. append ('');
Tabs = tabBuffer. toString ();
Gc = new GC (printer );

FontData fontData = font. getFontData () [0];
PrinterFont = new Font (printer, fontData. getName (), fontData. getHeight (), fontData. getStyle ());
Gc. setFont (printerFont );
TabWidth = gc. stringExtent (tabs). x;
LineHeight = gc. getFontMetrics (). getHeight ();

RGB rgb = foregroundColor. getRGB ();
PrinterForegroundColor = new Color (printer, rgb );
Gc. setForeground (printerForegroundColor );

Rgb = backgroundColor. getRGB ();
PrinterBackgroundColor = new Color (printer, rgb );
Gc. setBackground (printerBackgroundColor );

PrintText ();
Printer. endJob ();
PrinterFont. dispose ();
PrinterForegroundColor. dispose ();
PrinterBackgroundColor. dispose ();
Gc. dispose ();
}
}

Void printText (){
Printer. startPage ();
WordBuffer = new StringBuffer ();
X = leftMargin;
Y = topMargin;
Index = 0;
End = textToPrint. length ();
While (index <end ){
Char c = textToPrint. charAt (index );
Index ++;
If (c! = 0 ){
If (c = 0x0a | c = 0x0d ){
If (c = 0x0d & index <end & textToPrint. charAt (index) = 0x0a ){
Index ++;
}
PrintWordBuffer ();
Newline ();
} Else {
If (c! = '\ T '){
WordBuffer. append (c );
}
If (Character. isWhitespace (c )){
PrintWordBuffer ();
If (c = '\ t '){
X + = tabWidth;
}
}
}
}
}
If (y + lineHeight <= bottomMargin ){
Printer. endPage ();
}
}
Void printWordBuffer (){
If (wordBuffer. length ()> 0 ){
String word = wordBuffer. toString ();
Int wordWidth = gc. stringExtent (word). x;
If (x + wordWidth> rightMargin ){
Newline ();
}
Gc. drawString (word, x, y, false );
X + = wordWidth;
WordBuffer = new StringBuffer ();
}
}
Void newline (){
X = leftMargin;
Y + = lineHeight;
If (y + lineHeight> bottomMargin ){
Printer. endPage ();
If (index + 1 <end ){
Y = topMargin;
Printer. startPage ();
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.