/* ==============================================
* Simtools : The tools library used in JSynoptic
* ==============================================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2003, by :
* Corporate:
* Astrium SAS
* EADS CRC
* Individual:
* Nicolas Brodu
*
*
* $Id: PrintDialog.java,v 1.9 2009/02/04 11:06:24 ogor Exp $
*
* Changes
* -------
* 25-Sep-2003 : Initial public release (NB);
*
*/
package simtools.ui;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.Vector;
import java.awt.Container;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRootPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
/**
* Display a dialog for printing, with standard options.
* @author zxpletran007
*
*/
public class PrintDialog extends JDialog implements ActionListener{
public static MenuResourceBundle resources = ResourceFinder.getMenu(PrintDialog.class);
protected static final Paper[] papers;
protected static final Vector paperNames;
protected static final PrinterJob printerJob;
protected static final Vector printServices;
// PrintDialog named properties
public static final String PRINT_SELECTION = "PRINT_SELECTION";
public static final String ORIENTATION = "ORIENTATION";
public static final String PAPER_SIZE = "PAPER_SIZE";
public static final String NB_COPIES = "NB_COPIES";
public static final String PRINT_BACKGROUND = "PRINT_BACKGROUND";
public static final String SCALE_SHEET = "SCALE_SHEET";
public static final String SHEET_NAME = "SHEET_NAME";
static {
int paperNumber = resources.getIntValue("paperNumber");
paperNames = new Vector(paperNumber);
papers = new Paper[paperNumber];
for (int i=0; i<paperNumber; i++) {
paperNames.add(resources.getStringValue("paper"+i+"Name"));
papers[i] = new Paper();
papers[i].setSize(
resources.getDoubleValue("paper"+i+"Width"),
resources.getDoubleValue("paper"+i+"Height")
);
papers[i].setImageableArea(
resources.getDoubleValue("paper"+i+"ImageableX"),
resources.getDoubleValue("paper"+i+"ImageableY"),
resources.getDoubleValue("paper"+i+"ImageableWidth"),
resources.getDoubleValue("paper"+i+"ImageableHeight")
);
}
printerJob = PrinterJob.getPrinterJob();
printServices = new Vector();
PrintService[] ps = PrinterJob.lookupPrintServices();
for(int i=0;i< ps.length;i++){
printServices.add(new Printer(ps[i]) );
}
}
protected JRadioButton printAllButton;
protected JRadioButton printSelectedButton;
protected JRadioButton portraitButton;
protected JRadioButton landscapeButton;
protected JRadioButton reverseLandscapeButton;
protected JButton printerPropertiesButton;
protected JButton closeButton;
protected JButton okButton;
protected JComboBox paperNameList;
protected JSpinner nbCopies;
protected JCheckBox cbzoom;
protected JCheckBox cbbackground;
protected HeaderPanel headerPanel;
protected JComboBox printerList;
protected boolean printOK;
public PrintDialog(Frame parent) {
super(parent, resources.getString("print"), true);
// Create contents
createContent();
if (JDialog.isDefaultLookAndFeelDecorated()) {
boolean supportsWindowDecorations =
UIManager.getLookAndFeel().getSupportsWindowDecorations();
if (supportsWindowDecorations) {
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.QUESTION_DIALOG);
}
}
pack();
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}
protected void createContent(){
Container content = getContentPane();
content.setLayout(new BorderLayout());
// HeaderPanel
headerPanel = new HeaderPanel(true);
headerPanel.setTitle(resources.getString("print"));
content.add(headerPanel, BorderLayout.NORTH);
// Inner panel
GridBagPanel printPanel = new GridBagPanel();
GridBagPanel printerPanel = new GridBagPanel( resources.getStringValue("printer") );
GridBagPanel selectionPanel = new GridBagPanel( resources.getStringValue("selection") );
GridBagPanel copiesPanel = new GridBagPanel(resources.getStringValue("copies") );
GridBagPanel paperSizePanel = new GridBagPanel( resources.getStringValue("paperSize"));
GridBagPanel paperOrientationPanel = new GridBagPanel( resources.getStringValue("paperOrientation"));
// printer
printerList = new JComboBox(printServices);
printerPropertiesButton = resources.getButton("propertiesButton", this);
// TODO fix the bug when editing printer properties on Linux OS
printerPropertiesButton.setEnabled(false);
printerList.addActionListener(this);
JLabel lname = resources.getLabel("name");
printerPanel.addOnCurrentRow(lname);
printerPanel.addOnCurrentRow(printerList, 2);
printerPanel.addOnCurrentRow(printerPropertiesButton);
printerPanel.carriageReturn();
// Selection
printAllButton = resources.getRadioButton("printAll", null);
printSelectedButton = resources.getRadioButton("printSelected", null);
ButtonGroup printButtonGroup = new ButtonGroup();
printButtonGroup.add(printAllButton);
printButtonGroup.add(printSelectedButton);
selectionPanel.addOnCurrentRow(printAllButton);
selectionPanel.carriageReturn();
selectionPanel.addOnCurrentRow(printSelectedButton);
selectionPanel.carriageReturn();
// Number of copies
JLabel lnbCopies = resources.getLabel("nbCopies");
SpinnerNumberModel spiNbCopies = new SpinnerNumberModel();
spiNbCopies.setStepSize(new Integer(1));
spiNbCopies.setMinimum(new Integer(1));
spiNbCopies.setMaximum(new Integer(100));
spiNbCopies.setValue(new Integer(1));
nbCopies = new JSpinner(spiNbCopies);
copiesPanel.addOnCurrentRow(lnbCopies);
copiesPanel.addOnCurrentRow(nbCopies, 1, true, false, true);
copiesPanel.carriageReturn();
// Paper size
paperNameList = new JComboBox(paperNames);
paperNameList.setSelectedIndex(0);
JLabel lpaper = resources.getLabel("paper");
paperSizePanel.addOnCurrentRow(lpaper);
paperSizePanel.addOnCurrentRow(paperNameList,1, true, false, true);
paperSizePanel.addOnCurrentRow( cbzoom = resources.getCheckBox("updateToPaper", null), 2, false, false, true);
cbzoom.setSelected(true);
// Orientation
portraitButton = resources.getRadioButton("portraitOrientation", null);
landscapeButton = resources.getRadioButton("landscapeOrientation", null);
reverseLandscapeButton = resources.getRadioButton("reverseLandscapeOrientation", null);
ButtonGroup paperOrientationGroup = new ButtonGroup();
paperOrientationGroup.add(portraitButton);
paperOrientationGroup.add(landscapeButton);
paperOrientationGroup.add(reverseLandscapeButton);
portraitButton.setSelected(true);
landscapeButton.setSelected(false);
reverseLandscapeButton.setSelected(false);
paperOrientationPanel.addOnCurrentRow(portraitButton);
paperOrientationPanel.carriageReturn();
paperOrientationPanel.addOnCurrentRow(landscapeButton);
paperOrientationPanel.carriageReturn();
paperOrientationPanel.addOnCurrentRow(reverseLandscapeButton);
paperOrientationPanel.carriageReturn();
// Add all panels
printPanel.addOnCurrentRow(printerPanel, 4, true, true, true);
printPanel.addOnCurrentRow(selectionPanel, 2, true, true, false);
printPanel.addOnCurrentRow(copiesPanel, 2, true, true, true);
printPanel.addOnCurrentRow(paperSizePanel, 1, true, true, false);
printPanel.addOnCurrentRow(paperOrientationPanel, 1, true, true, true);
printPanel.addOnCurrentRow( cbbackground = resources.getCheckBox("printBackground", null), 2, false, false, true);
cbbackground.setSelected(false);
content.add(printPanel, BorderLayout.CENTER);
// Buttons
JPanel navigationBarPanel = new JPanel(new BorderLayout());
JPanel navigationBar = new JPanel();
closeButton = resources.getButton("closeButton", this);
okButton = resources.getButton("okButton", this);
navigationBar.add(okButton);
navigationBar.add(closeButton);
navigationBarPanel.add(navigationBar, BorderLayout.EAST);
content.add(navigationBarPanel,BorderLayout.SOUTH);
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource()==printerPropertiesButton) {
Printer selectedPrinter = (Printer)printerList.getSelectedItem();
if (selectedPrinter != null){
// TODO
}
} else if (e.getSource()==closeButton) {
dispose();
} else if (e.getSource()==okButton) {
dispose();
printOK = true;
} else if (e.getSource()==printerList) {
try {
printerJob.setPrintService( ((Printer)printerList.getSelectedItem()).ps);
} catch (PrinterException pe) { // do nothing
}
}
}
public void dispose(){
super.dispose();
printOK = false;
}
/**
* @return printerJob
*/
public PrinterJob getPrinterJob() {
return printerJob;
}
/**
* @return true if the user wants to print, false if dialog cancelled.
*/
public boolean getResult() {
return printOK;
}
/**
* Get the value of one property given its name
* @param name the name of the property to get
* @return the property value
*/
public Object getPropertyValue(String name) {
Object res = null;
if (name.equalsIgnoreCase(PRINT_SELECTION)) {
res = new Boolean( printSelectedButton.isSelected());
} else if (name.equalsIgnoreCase(ORIENTATION)) {
if (portraitButton.isSelected()){
res = new Integer(PageFormat.PORTRAIT);
} else if (landscapeButton.isSelected()){
res = new Integer(PageFormat.LANDSCAPE);
} else if (reverseLandscapeButton.isSelected()){
res = new Integer(PageFormat.REVERSE_LANDSCAPE);
}
} else if (name.equalsIgnoreCase(PAPER_SIZE)) {
res = new Integer( paperNameList.getSelectedIndex());
} else if (name.equalsIgnoreCase(NB_COPIES)) {
res = nbCopies.getModel().getValue();
} else if (name.equalsIgnoreCase(PRINT_BACKGROUND)) {
res = new Boolean( cbbackground.isSelected());
} else if (name.equalsIgnoreCase(SCALE_SHEET)) {
res = new Boolean( cbzoom.isSelected());
}
return res;
}
/**
* Set the value of one property given its name
* @see simtools.shapes.AbstractShape#setPropertyValue(String, Object)
* @param name the name of the property to get
* @param value its new value
*/
public void setPropertyValue(String name, Object value){
if (name.equalsIgnoreCase(PRINT_SELECTION)) {
if (value instanceof Boolean){
printSelectedButton.setSelected(((Boolean)value).booleanValue());
printAllButton.setSelected(!((Boolean)value).booleanValue());
}
} else if (name.equalsIgnoreCase(ORIENTATION)) {
if (value instanceof Integer){
int orientation = ((Integer)value).intValue();
portraitButton.setSelected( orientation == PageFormat.PORTRAIT);
landscapeButton.setSelected( orientation == PageFormat.LANDSCAPE);
reverseLandscapeButton.setSelected( orientation == PageFormat.REVERSE_LANDSCAPE);
}
} else if (name.equalsIgnoreCase(PAPER_SIZE)) {
if (value instanceof Integer){
int paperIndex = ((Integer)value).intValue();
if (( paperIndex >=0) && (paperIndex < papers.length)){
paperNameList.setSelectedIndex(paperIndex);
}
}
} else if (name.equalsIgnoreCase(NB_COPIES)) {
if ( ( value instanceof Integer) && ((Integer)value).intValue() > 0) {
nbCopies.getModel().setValue(value);
}
} else if (name.equalsIgnoreCase(PRINT_BACKGROUND)) {
if (value instanceof Boolean){
cbbackground.setSelected(((Boolean)value).booleanValue());
}
} else if (name.equalsIgnoreCase(SCALE_SHEET)) {
if (value instanceof Boolean){
cbzoom.setSelected(((Boolean)value).booleanValue());
}
} else if (name.equalsIgnoreCase(SHEET_NAME)) {
if (value instanceof String){
headerPanel.displayInfo(resources.getString("print") + " " + (String)value);
}
}
}
/**
* @param paperIndex
* @return The paper corresponding to the given index
*/
public static Paper getPaper(int paperIndex){
if (( paperIndex >=0) && (paperIndex < papers.length)){
return papers[paperIndex];
}
return null;
}
/**
* @return true if it is possible to print
*/
public static boolean canPrint(){
return !printServices.isEmpty();
}
/**
* A internal class to allow printServices combo list to display real printer service names
* @author zxpletran007
*/
protected static class Printer{
public PrintService ps;
public Printer(PrintService ps) {
super();
this.ps = ps;
}
public String toString(){
return ps.getName();
}
}
}