/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.snippets;
/*
* Printing example snippet: print text to printer, with word wrap and
* pagination
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*/
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.swt.SWT;
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 Snippet133 {
Display display;
Shell shell;
Text text;
Font font;
Color foregroundColor, backgroundColor;
Printer printer;
GC 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 Snippet133().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.NONE);
PrinterData data = dialog.open();
if (data == null)
return;
if (data.printToFile) {
data.fileName = "print.out"; // you probably want to ask the user
// for a filename
}
/*
* Get the text to print from the Text widget (you could get it from
* anywhere, i.e. your java model)
*/
textToPrint = text.getText();
/*
* Do the printing in a background thread so that spooling does not
* freeze the UI.
*/
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")) { // the string is the job name - shows up
// in the
没有合适的资源?快使用搜索试试~ 我知道了~
SWT全接触,提供全部的组件测试小程序,机会难得
共661个文件
class:446个
java:209个
html:4个
4星 · 超过85%的资源 需积分: 6 14 下载量 158 浏览量
2008-11-20
09:09:07
上传
评论
收藏 668KB RAR 举报
温馨提示
SWT, 组件应用全接触,提供全部的测试小程序,欢迎指正
资源推荐
资源详情
资源评论
收起资源包目录
SWT全接触,提供全部的组件测试小程序,机会难得 (661个子文件)
Snippet133.class 11KB
Snippet156.class 7KB
Snippet135.class 7KB
Snippet186.class 6KB
Snippet81.class 6KB
Snippet139.class 4KB
Snippet83.class 4KB
Snippet128.class 4KB
Snippet194.class 4KB
Snippet199.class 4KB
EventDispatch.class 4KB
Snippet162.class 4KB
Snippet193.class 4KB
Snippet25.class 4KB
Snippet119.class 4KB
Snippet79$MyTransfer.class 4KB
Snippet75.class 4KB
Snippet205.class 3KB
Snippet150.class 3KB
Snippet111$1$2.class 3KB
Snippet171$MyTransfer.class 3KB
Snippet208.class 3KB
Snippet141$1.class 3KB
Snippet125$2.class 3KB
Snippet171$MyTransfer2.class 3KB
Snippet165.class 3KB
Snippet204.class 3KB
Snippet154.class 3KB
Snippet91$2.class 3KB
Snippet152.class 3KB
Snippet48.class 3KB
Snippet132.class 3KB
Snippet96.class 3KB
Snippet91.class 3KB
Snippet181.class 3KB
Snippet179$1.class 3KB
Snippet65.class 3KB
Snippet140.class 3KB
Snippet143.class 3KB
Snippet205$1.class 3KB
Snippet203.class 3KB
Snippet111$1.class 3KB
Snippet188.class 3KB
Snippet124$1.class 3KB
Snippet140$1.class 3KB
Snippet134.class 3KB
Snippet187.class 3KB
Snippet135$2.class 3KB
Snippet196$1.class 3KB
Snippet174.class 3KB
Snippet104.class 3KB
Snippet171.class 3KB
Snippet70.class 3KB
Snippet200.class 3KB
Snippet92.class 3KB
Snippet10.class 3KB
Snippet185.class 3KB
Snippet123.class 3KB
Snippet126.class 3KB
Snippet207.class 3KB
Snippet192.class 3KB
Snippet157$1.class 2KB
Snippet185$2.class 2KB
Snippet129.class 2KB
Snippet141.class 2KB
Snippet187$1.class 2KB
Snippet94.class 2KB
Snippet107.class 2KB
Snippet162$1.class 2KB
Snippet122.class 2KB
Snippet106.class 2KB
Snippet167.class 2KB
Snippet191.class 2KB
Snippet172.class 2KB
Snippet38.class 2KB
Snippet110.class 2KB
Snippet3.class 2KB
Snippet69.class 2KB
Snippet47.class 2KB
Snippet95$1.class 2KB
Snippet2.class 2KB
Snippet88.class 2KB
Snippet149.class 2KB
Snippet170.class 2KB
Snippet145.class 2KB
Snippet111.class 2KB
Snippet68.class 2KB
Snippet61.class 2KB
Snippet10$1.class 2KB
Snippet166.class 2KB
Snippet82.class 2KB
Snippet77.class 2KB
Snippet207$1.class 2KB
Snippet204$1.class 2KB
Snippet78.class 2KB
Snippet103.class 2KB
Snippet124.class 2KB
Snippet21.class 2KB
Snippet79.class 2KB
Snippet198.class 2KB
共 661 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
- water173512014-03-13不错,美中不足的是代码命名没有意义,需要一个一个打开查看,不方便查找.而且没有相应的文档说明
zjian2002
- 粉丝: 4
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功