public class PrintSetup extends JDialog implements ItemListener, ActionListener
{
final static JLabel label = new JLabel ("Welche Daten des Fahrzeugs sollen gedruckt werden?");
final static JButton print_button = new JButton ("Drucken");
final static JCheckBox oil_check = new JCheckBox ("Öl-Tabelle", true);
final static JCheckBox parts_check = new JCheckBox ("Teile-Tabelle", true);
final static JCheckBox tüv_check = new JCheckBox ("TÜV-Tabelle", true);
// Vorschau-Panel erzeugen
Preview preview = new Preview ();
final static BasicStroke finestroke = new BasicStroke(1.0f);
final static BasicStroke stroke = new BasicStroke(2.0f);
final static BasicStroke wideStroke = new BasicStroke(8.0f);
final static Color BACKGROUND = Color.white;
final static Color FOREGROUND = Color.black;
final static Color VEHICLE_TABLE= Color.gray;
final static Color TABLE = Color.black;
final static Color ROW_HEADER = Color.lightGray;
final static Color ROW_BLUE = new Color(79, 167, 249); // leichtes Blau
final static Color ROW_GREY = new Color(230, 230, 230);
final static Color WHITE = Color.white;
final static Font VEHICLE_TABLE_FONT = new Font("Arial", Font.BOLD, 12);
final static Font TABLE_HEADER_FONT = new Font("Arial", Font.BOLD, 10);
private JTable vehicleTable;
private JTable oilTable;
private JTable partTable;
private JTable tüvTable;
public PrintSetup(JFrame owner, String title, boolean modal, Vector data)
{
super(owner, title, modal);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.vehicleTable = (VehicleTable)data.elementAt(0);
this.oilTable = (OilTable)data.elementAt(1);
this.partTable = (PartTable)data.elementAt(2);
this.tüvTable = (TÜV_Table)data.elementAt(3);
getContentPane().setLayout(null);
setSize(600, 500);
setResizable(false);
label.setBounds ( 30, 10, 300, 50);
oil_check.setBounds ( 30, 60, 100, 30);
parts_check.setBounds (150, 60, 100, 30);
tüv_check.setBounds (270, 60, 100, 30);
print_button.setBounds (400, 60, 100, 30);
oil_check.addItemListener(this);
parts_check.addItemListener(this);
tüv_check.addItemListener(this);
print_button.addActionListener(this);
JScrollPane scrollPane = new JScrollPane(preview);
scrollPane.setBounds(0, 100, 600, 400);
scrollPane.createVerticalScrollBar();
getContentPane().add(label);
getContentPane().add(oil_check);
getContentPane().add(parts_check);
getContentPane().add(tüv_check);
getContentPane().add(scrollPane);
getContentPane().add(print_button);
}// end constructor
/**
* Wenn eine CheckBox geändert wird wird sofort die paintComponent-
* Methode des Preview-Panels aufgerufen.
*/
public void itemStateChanged(ItemEvent e)
{
preview.paintComponent((Graphics2D)preview.getGraphics());
}// endmethod
public void actionPerformed(ActionEvent e)
{
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(preview);
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}// endmethod
private class Preview extends JPanel implements Printable
{
public Preview ()
{
setBackground(BACKGROUND);
setSize(600, 800);
}// end constructor
/**
* überschrieben
* @see JComponent#paintComponent(java.awt.Graphics)
*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// Aufruf von printing mit explizitem Cast
printing((Graphics2D)g);
}// endmethod
/**
* Es wird ein Dialog zum Einstellen des Druckers angezeigt, danach
* wird das Preview-Panel gedruckt.
*/
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)throws PrinterException
{
if (pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}
printing((Graphics2D)graphics);
return Printable.PAGE_EXISTS;
}// endmethod
/**
* Diese Methode wird von der überschriebene paintComponents-Methode und auch von
* der print-Methode aufgerufen.
* @param g2 Übergebenes Graphics2D-Object
*/
public void printing(Graphics2D g2)
{
int ySpace = 250;
// Zeichnen der Tabelle für die Fahrzeugdaten
g2.setPaint(VEHICLE_TABLE);
g2.setStroke(stroke);
// Zeile 1
g2.drawRect( 50, 50, 150, 35);
g2.drawRect(200, 50, 300, 35);
// Zeile 2
g2.drawRect( 50, 85, 150, 35);
g2.drawRect(200, 85, 300, 35);
// Zeile 3
g2.drawRect( 50, 120, 150, 35);
g2.drawRect(200, 120, 300, 35);
// Zeile 4
g2.drawRect( 50, 155, 150, 35);
g2.drawRect(200, 155, 300, 35);
// Ausmalen der 1. und 3. Zeile
g2.setPaint(ROW_BLUE);
// Zeile 1
g2.fill(new Rectangle( 51, 51, 148, 33));
g2.fill(new Rectangle(201, 51, 298, 33));
// Zeile 3
g2.fill(new Rectangle( 51, 121, 148, 33));
g2.fill(new Rectangle(201, 121, 298, 33));
// Zeichnen des Textes der Fahrzeugtabelle
g2.setPaint(FOREGROUND);
g2.setFont(VEHICLE_TABLE_FONT);
g2.drawString("Hersteller", 55, 70);
g2.drawString((String)vehicleTable.getValueAt(0, 1), 205, 70);
g2.drawString("Kategorie", 55, 105);
g2.drawString((String)vehicleTable.getValueAt(0, 1), 205, 105);
g2.drawString("Typ", 55, 140);
g2.drawString((String)vehicleTable.getValueAt(2, 1), 205, 140);
g2.drawString("Fahrgestellnummer", 55, 175);
g2.drawString((String)vehicleTable.getValueAt(3, 1), 205, 175);
// Zeichnen der Öl-Tabelle
if(oil_check.isSelected())
{
... bla bla bla
}// endmethod
// Zeichnen der Teile-Tabelle
if(parts_check.isSelected())
{
g2.setStroke(finestroke);
// Tabellenheader zeichnen
g2.setPaint(TABLE);
g2.drawRect( 70, ySpace, 250, 20);
g2.drawRect(320, ySpace, 250, 20);
g2.setPaint(ROW_HEADER);
g2.fill3DRect( 70, ySpace, 250, 20, true);
g2.fill3DRect(320, ySpace, 250, 20, true);
g2.setPaint(FOREGROUND);
g2.setFont(TABLE_HEADER_FONT);
g2.drawString("Teil", 75, ySpace+15);
g2.drawString("Nummer", 325, ySpace+15);
ySpace += 20;
// Tabelleninhalt zeichnen
for(int i=0; i<partTable.getRowCount(); i++)
{
if(i%2 == 0)
{
g2.setPaint(TABLE);
g2.drawRect( 70, ySpace, 250, 20);
g2.drawRect(320, ySpace, 250, 20);
g2.setPaint(ROW_BLUE);
g2.fill(new Rectangle( 70+1, ySpace, 250-1, 20));
g2.fill(new Rectangle(320+1, ySpace, 250-1, 20));
g2.setPaint(FOREGROUND);
g2.drawString((String)partTable.getValueAt(i, 0), 75, ySpace + 15);
g2.drawString((String)partTable.getValueAt(i, 1), 325, ySpace + 15);
}
else
{
g2.setPaint(TABLE);
g2.drawRect( 70, ySpace, 250, 20);
g2.drawRect(320, ySpace, 250, 20);
g2.setPaint(FOREGROUND);
g2.drawString((String)partTable.getValueAt(i, 0), 75, ySpace + 15);
g2.drawString((String)partTable.getValueAt(i, 1), 325, ySpace + 15);
}
ySpace += 20;
}// end for
ySpace += 50;
}
// Zeichnen der TÜV-Tabelle
if(tüv_check.isSelected())
{
}
}// endmethod
}// end class Preview
}// endclass PrintSetup