SWT高级控件之SWT的高级应用


SWT高级控件之SWT的高级应用
2011年04月20日
  13.1 打印支持
  SWT通过Printer类可以实现打印的功能。Printer类与Display对象都属于设备的一种,都继承自Device类。
  SWT中涉及打印的类都在org.eclipse.swt.printing包中,包括打印对话框类(PrintDialog)、打印类(Printer)、打印数据类(PrinterData)。
  1. 打印类(Printer)和打印数据类(PrinterData)
  PrinterData对象通常通过打印对话框获得。也可以通过Printer.getDefaultPrinterData()获得默认的PrinterData对象。
  创建打印类Printer有两种方法:
  ◆ Printer():创建默认的打印类。例如:
  Printer printer = new Printer();
  ◆ Printer(PrinterData data):通过PrinterData对象创建打印类。例如:
  PrinterData data = Printer.getDefaultPrinterData();
  Printer printer = new Printer(data);
  打印的流程:startJob()->[第一页startPage()->endPage(),……第N页startPage()->endPage()]->cancelJob()或endJob()。
  举例如下:
  package www.swt.com;
  import org.eclipse.swt.SWT;
  import org.eclipse.swt.graphics.Color;
  import org.eclipse.swt.graphics.Font;
  import org.eclipse.swt.graphics.GC;
  import org.eclipse.swt.graphics.Point;
  import org.eclipse.swt.graphics.Rectangle;
  import org.eclipse.swt.printing.Printer;
  import org.eclipse.swt.printing.PrinterData;
  public class SimplePrint {
  /**
  * @param args
  */
  public static void main(String[] args) {
  PrinterData data = Printer.getDefaultPrinterData();
  if (data == null) {
  System.out.println("failed");
  return;
  }
  Printer printer = new Printer(data);
  // 开始打印任务
  if (printer.startJob("Simple Print")) {
  Color black = printer.getSystemColor(SWT.COLOR_BLACK);
  Color gray = printer.getSystemColor(SWT.COLOR_GRAY);
  Color red = printer.getSystemColor(SWT.COLOR_RED);
  // 计算左边距上边距的位置
  Rectangle trim = printer.computeTrim(0, 0, 0, 0);
  Point dpi = printer.getDPI();
  int leftMargin = dpi.x + trim.x;
  int topMargin = dpi.y / 2 + trim.y;
  // 创建图形上下文对象
  GC gc = new GC(printer);
  Font font = gc.getFont();
  // 打印第一页
  if (printer.startPage()) {
  gc.setBackground(gray);
  gc.setForeground(black);
  String testString = "This is donkey 1.";
  // 使用GC对象画字符串的方法显示字符
  gc.drawString(testString, leftMargin, topMargin+font.getFontData()[0].getHeight());
  printer.endPage();
  }
  // 打印第二页
  if (printer.startPage()) {
  gc.setBackground(gray);
  gc.setForeground(black);
  String testString = "This is donkey 2.";
  Point extent = gc.stringExtent(testString);
  // 使用GC对象画字符串的方法显示字符
  gc.drawString(testString, leftMargin, topMargin+font.getFontData()[0].getHeight());
  gc.setForeground(red);
  gc.drawRectangle(leftMargin, topMargin, extent.x, extent.y);
  printer.endPage();
  }
  // 释放GC对象
  gc.dispose();
  // 结束打印任务
  printer.endJob();
  }
  printer.dispose();
  }
  }
  第一页为灰色底黑字,第二页为黑色底黑字带红色边框。
  2. 打印程序示例
  该程序的功能大致是:有一个文本框和菜单,可以通过菜单项打开一个文件,并将内容显示在文本框中。同时也可以设置显示在文本框中文字的字体、前景色、背景色,设置好后,可以选择打印菜单项,按照当前的格式打印出来。
  PrintGUI.java:
  package www.swt.com.ch13;
  import java.io.*;
  public class PrintGUI {
  private Shell sShell ;
  private Menu menu ;
  private Text content ;
  Font font;
  Color foregroundColor, backgroundColor;
  private void createMenu() {
  menu = new Menu ( sShell , SWT.BAR);
  sShell.setMenuBar( menu );
  MenuItem item = new MenuItem(menu, SWT.CASCADE);
  item.setText("文件(&F)");
  Menu fileMenu = new Menu(sShell, SWT.DROP_DOWN);
  item.setMenu(fileMenu);
  item = new MenuItem(fileMenu, SWT.PUSH);
  item.setText("打开(&O)");
  item.setAccelerator(SWT.CTRL + 'O');
  item.addSelectionListener(new SelectionAdapter() {
  public void widgetSelected(SelectionEvent event) {
  menuOpen();
  }
  });
  item = new MenuItem(fileMenu, SWT.PUSH);
  item.setText("选择字体");
  item.addSelectionListener(new SelectionAdapter() {
  public void widgetSelected(SelectionEvent event) {
  menuFont();
  }
  });
  item = new MenuItem(fileMenu, SWT.PUSH);
  item.setText("选择前景色");
  item.addSelectionListener(new SelectionAdapter() {
  public void widgetSelected(SelectionEvent event) {
  menuForegroundColor();
  }
  });
  item = new MenuItem(fileMenu, SWT.PUSH);
  item.setText("选择背景色");
  item.addSelectionListener(new SelectionAdapter() {
  public void widgetSelected(SelectionEvent event) {
  menuBackgroundColor();
  }
  });
  item = new MenuItem(fileMenu, SWT.PUSH);
  item.setText("打印(@P)");
  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)");
  item.addSelectionListener(new SelectionAdapter() {
  public void widgetSelected(SelectionEvent event) {
  font.dispose();
  foregroundColor.dispose();
  backgroundColor.dispose();
  System.exit(0);
  }
  });
  }
  protected void menuPrint() {
  PrintDialog dialog = new PrintDialog(sShell, SWT.NONE);
  PrinterData data = dialog.open();
  if (data == null) return;
  if (data.printToFile) {
  data.fileName = "print.out";
  } 
  Printer printer = new Printer(data);
  TextPrinter textPrinter = new TextPrinter( "Print Job", content , printer);
  textPrinter.start();
  }
  protected void menuBackgroundColor() {
  ColorDialog colorDialog = new ColorDialog(sShell);
  colorDialog.setRGB(content.getBackground().getRGB( ));
  RGB rgb = colorDialog.open();
  if (rgb != null) {
  if (backgroundColor != null) backgroundColor.dispose();
  backgroundColor = new Color(sShell.getDisplay(), rgb);
  content.setBackground(backgroundColor);
  }
  }
  protected void menuForegroundColor() {
  ColorDialog colorDialog = new ColorDialog(sShell);
  colorDialog.setRGB(content.getForeground().getRGB( ));
  RGB rgb = colorDialog.open();
  if (rgb != null) {
  if (foregroundColor != null) foregroundColor.dispose();
  foregroundColor = new Color(sShell.getDisplay(), rgb);
  content.setForeground(foregroundColor);
  }
  }
  protected void menuFont() {
  FontDialog fontDialog = new FontDialog(sShell);
  fontDialog.setFontList(content.getFont().getFontDa ta());
  FontData fontData = fontDialog.open();
  if (fontData != null) {
  if (font != null) font.dispose();
  font = new Font(sShell.getDisplay(), fontData);
  content.setFont(font);
  }
  }
  protected void menuOpen() {
  final String textString;
  FileDialog dialog = new FileDialog(sShell, 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(sShell, SWT.ICON_ERROR);
  box.setMessage("读文件出错:\n" + name);
  box.open();
  return;
  }
  } catch (FileNotFoundException e) {
  MessageBox box = new MessageBox(sShell, SWT.ICON_ERROR);
  box.setMessage("文件未找到:\n" + name);
  box.open();
  return;
  } 
  content.setText(textString);
  }
  private void createSShell() {
  sShell = new Shell();
  sShell.setText("Shell");
  sShell.setLayout(new FillLayout());
  sShell.setSize(new Point(300, 200));
  content = new Text(sShell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
  createMenu();
  }
  public static void main(String[] args) {
  Display display = Display.getDefault();
  PrintGUI thisClass = new PrintGUI();
  thisClass.createSShell();
  thisClass.sShell.open();
  while (!thisClass.sShell.isDisposed()) {
  if (!display.readAndDispatch())
  display.sleep();
  }
  display.dispose();
  }
  }
  TextPrinter.java:
  package www.swt.com.ch13;
  import org.eclipse.swt.graphics.*;
  public class TextPrinter extends Thread {
  private Text content;//显示字符的文本框对象
  private Printer printer;//打印对象
  private GC gc;//用于绘制的图形上下文对象
  private FontData[] printerFontData;//打印的字体数据
  private RGB printerForeground, printerBackground;//打印的前景色和背景色
  private int lineHeight = 0;//打印的行高
  private int tabWidth = 0;//Tab键的大小
  private int leftMargin, rightMargin, topMargin, bottomMargin;//上下左右的页边距
  private int x, y;//打印的行和列的值
  private int index, end;//字符当前打印的索引值和总长度
  private String textToPrint;//需要打印的字符
  private String tabs;//Tab键
  private StringBuffer wordBuffer;//打印字符的临时变量
  public TextPrinter(String name, Text text, Printer printer) {
  super(name);
  this.content = text;
  this.printer = printer;
  init();
  }
  //初始化属性的方法
  public void init() {
  textToPrint = content.getText();
  printerFontData = content.getFont().getFontData();
  printerForeground = content.getForeground().getRGB();
  printerBackground = content.getBackground().getRGB();
  //默认一个Tab键表示4个空格
  int tabSize = 4; 
  StringBuffer tabBuffer = new StringBuffer(tabSize);
  for (int i = 0; i 不能正常启动则返回
  if (!printer.startJob("Text Print"))
  return;
  //开始打印
  print();
  }
  public void print() {
  //创建图形上下文对象
  gc = new GC(printer);
  //创建字体,前景色,背景色对象
  Font printerFont = new Font(printer, printerFontData);
  Color printerForegroundColor = new Color(printer, printerForeground);
  Color printerBackgroundColor = new Color(printer, printerBackground);
  //将字体,前景色,背景色设置给gc对象
  gc.setFont(printerFont);
  gc.setForeground(printerForegroundColor);
  gc.setBackground(printerBackgroundColor);
  tabWidth = gc.stringExtent(tabs).x;
  lineHeight = gc.getFontMetrics().getHeight();
  //打印字符
  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  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 窗口菜单:setWindowMenus(MenuItem[] windowMenus)和getWindowMenus()。
  ◆ 既然可以获取OLE控件的菜单,就可以对菜单项进行控制,例如设置可见和设置加速键等。
  创建一个OleFrame对象的方法:
  frame = new OleFrame(sShell, SWT.NONE);
  // 为控件设置菜单项
  frame.setFileMenus(fileItem);
  2. OLE控件类(OleClientSite和OleControlSite)
  OleClientSite对象和OleControlSite对象都是OLE控件,其中OleClientSite为OLE控件,OleControlSite为ActiveX控件,因为OleControlSite类继承自OleClientSite类。
  若想创建OLE对象,有两种常用的构造方法:
  ◆ OleClientSite(Composite parent, int style, String progId):progId为标示应用系统的字符,例如,Word的progId为"Word.Document",Excel的为"Excel.Sheet",IE的为"Shell.Explorer"。若要查看其他应用程序的progId,可以查看系统注册表。
  OleClientSite clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document");
  ◆ OleClientSite(Composite parent, int style, File file):file为保存的某一个文件。用这种方法创建的OLE控件,系统会根据文件自动查找对应打开的应用程序。
  File file = new File("F:\\Temp.doc");
  OleClientSite clientSite = new OleClientSite(frame, SWT.NONE, file);
  创建了一个OLE控件,接下来需要打开控件,才可以显示控件。使用doVerb(int verb)方法。其中verb有以下可以选择的参数:
  ◇ OLE.OLEIVERB_PRIMARY:打开编辑状态的OLE控件。
  ◇ OLE.OLEIVERB_SHOW:显示OLE控件。
  ◇ OLE.OLEIVERB_OPEN:在另外一个窗口中打开OLE控件。
  ◇ OLE.OLEIVERB_HIDE:隐藏OLE控件。
  ◇ OLE.OLEIVERB_INPLACEACTIVATE:不带有工具栏和菜单栏的方式。
  ◇ OLE.OLEIVERB_UIACTIVATE:激活OLE的菜单栏和工具栏。
  ◇ OLE.OLEIVERB_DISCARDUNDOSTATE:关闭OLE控件。
  例如,以下代码可以打开编辑状态的OLE控件:
  clientSite.doVerb(OLE.OLEIVERB_PRIMARY);
  当OLE对打开的文件修改后,通过isDirty()方法可以判断是否已经修改过。如果修改后,可以使用save(File file, boolean includeOleInfo)方法进行保存。例如:
  if(clientSite.isDirty()) {
  clientSite.save(file, true);
  }
  创建ActiveX控件对象要使用OleControlSite类,该类只有一个构造方法:
  OleControlSite(Composite parent, int style, String progId):只能根据progId来创建。例如,创建一个Word的ActiveX控件对象的代码如下:
  OleControlSite controlSite = new OleControlSite(frame, SWT.NONE, "Word.Document");
  3. OLE程序示例:
  该程序的功能是:可以选择打开Word文档,然后进行编辑后保存该文档。
  package www.swt.com.ch13;
  import java.io.File;
  public class OleSample {
  private Shell sShell;
  private MenuItem[] fileItem;//OLE的菜单项
  private OleClientSite clientSite;//OLE控件对象
  private OleFrame frame;//OLE的面板的对象
  private File openFile;//打开的文件
  public static void main(String[] args) {
  Display display = Display.getDefault();
  OleSample thisClass = new OleSample();
  thisClass.createSShell();
  thisClass.sShell.open();
  while (!thisClass.sShell.isDisposed()) {
  if (!display.readAndDispatch())
  display.sleep();
  }
  thisClass.clientSite.dispose();
  display.dispose();
  }
  private void createSShell() {
  sShell = new Shell();
  sShell.setText("OLE Sample");
  sShell.setLayout(new FillLayout());
  createMenu();
  sShell.setSize(new Point(300, 200));
  }
  //创建OLE控件对象
  private void createOle() {
  frame = new OleFrame(sShell, SWT.NONE);
  frame.setFileMenus(fileItem); // 设置文件菜单
  if (openFile != null)
  clientSite = new OleClientSite(frame, SWT.NONE, openFile);
  clientSite.doVerb(OLE.OLEIVERB_PRIMARY);
  }
  private void createMenu() {
  Menu main = new Menu(sShell, SWT.BAR);
  MenuItem file = new MenuItem(main, SWT.CASCADE);
  file.setText("文件(&F)");
  Menu fileMenu = new Menu(file);
  fileItem = new MenuItem[2];
  fileItem[0] = new MenuItem(fileMenu, SWT.PUSH);
  fileItem[0].setText("打开");
  fileItem[1] = new MenuItem(fileMenu, SWT.PUSH);
  fileItem[1].setText("保存");
  file.setMenu(fileMenu);
  sShell.setMenuBar(main);
  fileItem[0].addSelectionListener(new SelectionAdapter() {
  public void widgetSelected(SelectionEvent e) {
  FileDialog dialog = new FileDialog(sShell, SWT.OPEN);
  dialog.setFilterExtensions(new String[] { "*.doc", "*.*" });
  String file = dialog.open();
  if (file != null) {
  openFile = new File(file);
  //打开OLE控件
  createOle();
  }
  }
  });
  fileItem[1].addSelectionListener(new SelectionAdapter() {
  public void widgetSelected(SelectionEvent e) {
  //如果打开文件被修改过
  if (clientSite.isDirty()) {
  //创建一个临时文件
  File tempFile = new File(openFile.getAbsolutePath() + ".tmp");
  openFile.renameTo(tempFile);
  //如果保存成功,则删除临时文件,否则恢复到临时文件保存的状态
  if (clientSite.save(openFile, true))
  tempFile.delete();
  else
  tempFile.renameTo(openFile);
  }
  }
  });
  }
  }
  显示效果:
  打开一个Word文档后:
  "文件"菜单为在代码中定义的菜单,其他菜单为Word的菜单。
  13.5 Pocket PC应用
  在移动设备中使用的SWT程序与之前学习的SWT程序编写代码的方式没有什么不同。但是与之前SWT程序所不同的是,SWT程序运行的环境不同,要在移动设备上运行SWT程序,需要作以下配置:
  (1)确保移动设备上安装了虚拟机(VM),例如J2ME的运行环境,来确保能运行Java程序。
  (2)下载并安装SWT所需的类库swt.jar和与本地系统相关的.dll文件。因为移动设备的内存和存储的资源有限,所以与桌面应用的SWT类库相比,移动设备上使用的SWT类库大小上少很多。
  (3)最后运行编译好的类文件。在移动设备上不可能安装Eclipse,所以要使用命令行来运行程序。
  13.6 Web应用SWT
  通过第三方厂商的开发可以实现将SWT程序应用于Web开发。例如SmartSWT:http://www.smartswt.net/smartswt/index.htm。
  安装了SmartSWT服务器和客户端,就可以通过IE来访问SWT程序,与我们平时浏览网页没什么区别了。

猜你喜欢

转载自brf63brf.iteye.com/blog/1361930
swt
今日推荐