/**
* [Description from Programm]
* Copyright (C) 2011 Walter Wiedmann, woidda@users.sourceforge.net
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
* This program 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 General
* Public License for more details.
* You should have received a copy of the GNU General Public License along
* with this program; if not, see <http://www.gnu.org/licenses/>.
* Auf Deutsch:
* [eine Zeile mit dem Programmnamen und einer kurzen Beschreibung]
* Copyright (C) 2008 Walter Wiedmann, woidda@users.sourceforge.net
* Dieses Programm ist freie Software. Sie können es unter den Bedingungen
* der GNU General Public License, wie von der Free Software Foundation
* veröffentlicht, weitergeben und/oder modifizieren, entweder gemäß
* Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
* Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es
* Ihnen von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne
* die implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN
* BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License.
* Sie sollten ein Exemplar der GNU General Public License zusammen mit
* diesem Programm erhalten haben. Falls nicht, siehe
* <http://www.gnu.org/licenses/>.
* *
*/
package de.wiedmann.drilloverimage;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Date;
/**
* @author walter
*
*/
public class DrillOverImage extends JFrame implements ActionListener, ComponentListener {
/**
*
*/
private static final long serialVersionUID = 1L;
/* Color COLOR_NEEDFUL = new Color(0xB0E0E6);
Color COLOR_OK = Color.GREEN;
Color COLOR_FAIL = Color.RED;
Color COLOR_DO = new Color(0x00BFFF);*/
static DrillOverImage fenster;
private String version;
private String revision;
private Date compileDate;
private String pathImage;
private String fileImage;
private String pathDrill;
private String fileDrill;
private JLabel jLabelImage;
private JLabel jLabelDrill;
/*private JButton jToolButtonSelectPl;
private JButton jToolButtonSelectParallel;
private JButton jToolButtonSelectLog;
private JButton jToolButtonReadPl;
private JButton jToolButtonReadParallel;
private JButton jToolButtonReadLog;
private JButton jToolButtonTar;
private JButton jToolButtonTpg;*/
private JMenuBar jMenuBarMain;
private JMenuItem jMenuItemSelectImage;
private JMenuItem jMenuItemSelectDrill;
private JMenuItem jMenuItemZoomPlus;
private JMenuItem jMenuItemZoomNormal;
private JMenuItem jMenuItemZoomMinus;
/*private JMenuItem jMenuItemSelectLog;
private JMenuItem jMenuItemReadPl;
private JMenuItem jMenuItemReadParallel;
private JMenuItem jMenuItemReadLog;
private JMenuItem jMenuItemTpg;
private JMenuItem jMenuItemTar;
private JTabbedPane mainPane;*/
private JScrollPane jScrollPane;
private double zoom = 1;
Image bild = null;
private String ausgabe = "Bitte Baugruppenbild auswahlen!";
//innere Klasse für Zeichenfläche
class Leinwand extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private int breite = 0;
private int hoehe = 0;
private double breiteZoom = 1;
private double hoeheZoom = 1;
public void paintComponent(Graphics g) {
super.paintComponents(g);
int x, y;
//hier Zeichencode einfügen.
if(bild == null) {
g.setColor(Color.RED);
g.setFont(new Font("Georgia", Font.ITALIC, 30));
FontMetrics fm = g.getFontMetrics();
x = (this.getWidth() - fm.stringWidth(ausgabe)) / 2;
if (x < 0)
x=0;
y = (this.getHeight() - fm.getLeading()) / 2;
if (y < 0)
y=0;
g.drawString(ausgabe, x, y);
} else {
int tempBreite = (int)(bild.getWidth(this)*breiteZoom*zoom);
int tempHoehe = (int)(bild.getHeight(this)*breiteZoom*zoom);
if(tempBreite != breite || tempHoehe != hoehe) {
breite = tempBreite;
hoehe = tempHoehe;
this.setPreferredSize(new Dimension(breite,hoehe));
jScrollPane.revalidate();
}
g.drawImage(bild, 0, 0 , breite, hoehe, this);
}
}
}
Leinwand leinwand;
/**
* @param args
*/
public static void main(String[] args) {
fenster = new DrillOverImage();
// Show the window
fenster.setSize(800, 600);
// Set windows on default Location
fenster.setLocationByPlatform(true);
//jMainFrame.setResizable(false);
fenster.setVisible(true);
}
public DrillOverImage() {
super();
//fillTableHeaders();
setTitle("Drill over Image");
try {
Image logo;
logo = ImageIO.read(ClassLoader.getSystemResource("images/logo.gif"));
this.setIconImage(logo);
} catch (IOException e) {
System.err.println("Can't load programm - icon!");
} catch (Exception e) {
System.err.println("Can't load programm - icon!");
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jPanelMain = new JPanel();
jMenuBarMain = new JMenuBar();
jMenuBarMain.add(buildFileMenu());
jMenuBarMain.add(buildZoomMenu());
jMenuBarMain.add(buildHelpMenu());
JPanel jPanelMenu = new JPanel();
jPanelMenu.setLayout(new BorderLayout());
jPanelMenu.add(jMenuBarMain,BorderLayout.NORTH);
jPanelMenu.add(buildToolBar(),BorderLayout.CENTER);
jPanelMain.setLayout(new BorderLayout());
jPanelMain.add(jPanelMenu,BorderLayout.NORTH);
leinwand = new Leinwand();
jScrollPane = new JScrollPane();
jScrollPane.getViewport().add(leinwand);
jPanelMain.add(jScrollPane,BorderLayout.CENTER);
jScrollPane.getVerticalScrollBar().addComponentListener(this);
jScrollPane.getHorizontalScrollBar().addComponentListener(this);
JPanel jPanelStatus = new JPanel(new GridLayout(3,0));
jLabelImage = new JLabel("Selected image file: ?");
jPanelStatus.add(jLabelImage);
jLabelDrill = new JLabel("Selected drill file: ?");
jPanelStatus.add(jLabelDrill);
jPanelMain.add(jPanelStatus,BorderLayout.SOUTH);
getContentPane().add(jPanelMain);
}
public void actionPerformed(ActionEvent e) {
System.out.println("actionPerformed: " + e.getActionCommand());
if("quit".equals(e.getActionCommand())) {
System.exit(0);
return;
}
if("zoomin".equals(e.getActionCommand())) {
zoom *= 1.1;
System.out.println("Zoomfaktor: " + zoom);
jScrollPane.repaint();
return;
}
if("zoomnormal".equals(e.getActionCommand())) {
zoom = 1;
System.out.println("Zoomfaktor: " + zoom);
jScrollPane.repaint();
return;
}
if("zoomout".equals(e.getActionCommand())) {
zoom /= 1.1;
System.out.println("Zoomfaktor: " + zoom);
jScrollPane.repaint();
return;
}
if("help".equals(e.getActionCommand())) {
readVersion();
//View Version
String message = "Version: " + version + "\n" +
"Rev.: " + revision + "\n" +
"Date: " + compileDate.toString() + "\n" +
"Default file encoding: " + System.getProperty("file.encoding");
JOptionPane.showMessageDialog(this,message,"Programminfos",
JOptionPane.PLAIN_MESSAGE,
new ImageIcon(ClassLoader.getSystemResource("images/logo.gif")));
return;
} else {
FileDialog dialog = new FileDialog(this,"Choose File",FileDialog.LOAD);
if("image...".equals(e.getActionCommand())) {
if(pathImage != null) {
dialog.setDirectory(pathImage);
}
else {
if(pathDrill != null) {
dialog.setDirectory(pathDrill);
}
}
if(fileImage != null) {
dialog.setFile(fileImage);
}
dialog.setTitle("Choose Image - File");
/*JOptionPane.showMessageDialog(this, "Der Stücklistenoutput muss in ProAlpha\n" +
"mit der Codierung ISO-8859-1 ausgegeben worden sein!",
"Hinweis zur Codierung der Stückliste",JOptionPane.INFORMATION_MESSAGE);*/
}
if("drill...".equals(e.getActionCommand())) {
if(pathDrill != null) {
dialog.setDirectory(pathDrill);
}
else {
if(pathImage != null) {
dialog.setDirectory(pathImage);
}
}
if(fileDrill != null) {
dialog.setFile(fileDrill);
}
dialog.setTitle("Choose Drill - File");
}
/*if("log...".equals(e.getActionCommand())) {
if(pathLog != null) {
dialog.setDirectory(pathLog);
}
else {
if(pathPl != null) {
dialog.setDirectory(pathPl);
} else {
if(pathParallel != null) {
dialog.setDirectory(pathParallel);
}
}
}
if(fileLog != null) {
dialog.setFile(fileLog);
}
dialog.setTitle("Choose Log - File");
}*/
dialog.setVisible(true);
if("image...".equals(e.getActionCommand())){
if(dialog.getFile() != null) {
fileImage = dialog.getFile();
pathImage = dialog.getDirectory();
jLabelImage.setText("Selected image file: " + pathImage + fileImage);
}
try {
bild = ImageIO.read(new File(pathImage + fileImage));
zoom = 1;
leinwand.repaint();
} catch ( IOException exp) {
System.err.println(exp.getMessage());
bild = null;
}
}
if("drill...".equals(e.getActionCommand())){
if(dialog.getFile() != null) {
fileDrill = dialog.getFile();
pathDrill = dialog.getDirectory();
jLabelDrill.setText("Selected drill file: " + pathDrill + fileDrill);
}
}
/* if("log...".equals(e.getActionCommand())){
if(dialog.getFile() != null) {
fileLog = dialog.getFile();
pathLog = dialog.getDirectory();
jLabelLog.setText("Selected genrad log: " + pathLog + fileLog);
}
}*/
//setButtonStatus();
}
/*
if("readPartList".equals(e.getActionCommand())) {
//System.out.println("Should create the testability report.");
if(filePl != null) {
File partsList = null;
partsList = new File(pathPl + filePl);
if(partsList.canRead()) {
ProAlphaPartsList pList = new ProAlphaPartsList();
board = new Board (pList.importPartList(partsList));
tarTable = new JTable(board);
tarTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>();
tarTable.setRowSorter( sorter );
sorter.setModel( board );
// set comparator for the 1. and 23. column (Integer)
IntegerComparator integerComparator = new IntegerComparator();
sorter.setComparator( 1, integerComparator);
sorter.setComparator(23, integerComparator);
// set comparator for column 4-9 and 11-22 (Double)
DoubleComparator doubleComparator = new DoubleComparator();
for(int i = 4; i<=22; i++) {
//jump over column 10
if(i == 10) {
continue;
}
sorter.setComparator(i, doubleComparator);
}
mainPane.addTab("DrillOverImage", new JScrollPane(tarTable));
//board.changedWholeBoard();
//board.printParts();
readyPl = true;
} else {
JOptionPane.showMessageDialog(this, "Can't not read file: " + pathPl + filePl + ".",
"File error", JOptionPane.ERROR_MESSAGE);
}
}
setButtonStatus();
return;
}
if("readParallel".equals(e.getActionCommand())) {
if(fileParallel != null) {
File parallel = null;
parallel = new File(pathParallel + fileParallel);
if(parallel.canRead()) {
ParallelOutput parallelParser = new ParallelOutput();
parallelParser.addParallelParts(parallel, board);
readyParallel = true;
} else {
JOptionPane.showMessageDialog(this, "Can't not read file: " + pathParallel + fileParallel + ".",
"File error", JOptionPane.ERROR_MESSAGE);
}
}
setButtonStatus();
return;
}
if("readLogFile".equals(e.getActionCommand())) {
File log = null;
log = new File(pathLog + fileLog);
if(log.canRead()) {
board.readGenradLogFile(log);
//board.printDrillOverImage();
readyLog = true;
} else {
JOptionPane.showMessageDialog(this, "Can't not read file: " + pathLog + fileLog + ".",
"File error", JOptionPane.ERROR_MESSAGE);
}
setButtonStatus();
return;
}
if("printNewTpgValues".equals(e.getActionCommand())) {
board.printNewTpgValues();
return;
}
if("printTar".equals(e.getActionCommand())) {
board.printDrillOverImage();
}
*/
}
/**
* Function set the buttons
*/
/* private void setButtonStatus() {
if(filePl != null) {
jToolButtonSelectPl.setBackground(COLOR_OK);
jToolButtonReadPl.setEnabled(true);
jToolButtonReadPl.setBackground(readyPl?COLOR_OK:COLOR_NEEDFUL);
jMenuItemReadPl.setEnabled(true);
} else {
jToolButtonReadPl.setEnabled(false);
jToolButtonReadPl.setBackground(null);
jMenuItemReadPl.setEnabled(false);
}
if(fileParallel != null) {
jToolButtonSelectParallel.setBackground(COLOR_OK);
jToolButtonReadParallel.setEnabled(readyPl);
jToolButtonReadParallel.setBackground(readyPl?(readyParallel?COLOR_OK:COLOR_NEEDFUL):null);
jMenuItemReadParallel.setEnabled(readyPl);
} else {
jToolButtonReadParallel.setEnabled(false);
jToolButtonReadParallel.setBackground(null);
jMenuItemReadParallel.setEnabled(false);
}
if(fileLog != null) {
jToolButtonSelectLog.setBackground(COLOR_OK);
//later only possible after read the Parallel - File
jToolButtonReadLog.setEnabled(readyParallel);
//possible without Parallel - File
//jToolButtonReadLog.setEnabled(readyPl);
jToolButtonReadLog.setBackground(readyParallel?(readyLog?COLOR_OK:COLOR_NEEDFUL):null);
jMenuItemReadLog.setEnabled(readyParallel);
} else {
jToolButtonReadLog.setEnabled(false);
jToolButtonReadLog.setBackground(null);
jMenuItemReadLog.setEnabled(false);
}
jToolButtonTpg.setEnabled(readyParallel);
jToolButtonTpg.setBackground(readyParallel?COLOR_DO:null);
jMenuItemTpg.setEnabled(readyParallel);
jToolButtonTar.setEnabled(readyLog);
jToolButtonTar.setBackground(readyLog?COLOR_DO:null);
jMenuItemTar.setEnabled(readyLog);
}*/
private void readVersion() {
URL url = ClassLoader.getSystemResource("version");
if(url == null) {
return;
}
BufferedReader reader = null;
String line = null;
try {
reader = new BufferedReader(new InputStreamReader(url.openStream()));
while ((line = reader.readLine()) != null) {
if (line.startsWith("Version=")) {
version = (line.split("="))[1];
}
if (line.startsWith("Revision=")) {
revision = (line.split("="))[1];
}
if (line.startsWith("Date=")) {
String sSec = (line.split("="))[1];
Long lSec = Long.valueOf(sSec);
compileDate = new Date(lSec);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return;
}
private JMenu buildFileMenu() {
JMenu jMenu = new JMenu("File");
jMenu.setMnemonic('F');
jMenuItemSelectImage = new JMenuItem("Select image file", 'i');
jMenuItemSelectImage.setAccelerator(KeyStroke.getKeyStroke('I', Event.CTRL_MASK));
jMenuItemSelectImage.setActionCommand("image...");
jMenuItemSelectImage.addActionListener(this);
jMenu.add(jMenuItemSelectImage);
jMenuItemSelectDrill = new JMenuItem("Select drill file", 'd');
jMenuItemSelectDrill.setAccelerator(KeyStroke.getKeyStroke('D', Event.CTRL_MASK));
jMenuItemSelectDrill.setActionCommand("drill...");
jMenuItemSelectDrill.addActionListener(this);
jMenu.add(jMenuItemSelectDrill);
jMenu.addSeparator();
JMenuItem jMenuItem = new JMenuItem("Quit",'q');
jMenuItem.setAccelerator(KeyStroke.getKeyStroke('Q',Event.CTRL_MASK));
jMenuItem.setActionCommand("quit");
jMenuItem.addActionListener(this);
jMenu.add(jMenuItem);
return jMenu;
}
private JMenu buildZoomMenu() {
JMenu jMenu = new JMenu("Zoom");
jMenu.setMnemonic('Z');
jMenuItemZoomPlus = new JMenuItem("Zoom IN", 'O');
jMenuItemZoomPlus.setAccelerator(KeyStroke.getKeyStroke('O', Event.CTRL_MASK));
jMenuItemZoomPlus.setActionCommand("zoomin");
jMenuItemZoomPlus.addActionListener(this);
jMenu.add(jMenuItemZoomPlus);
jMenuItemZoomNormal = new JMenuItem("Zoom Normal", '0');
jMenuItemZoomNormal.setAccelerator(KeyStroke.getKeyStroke('0', Event.CTRL_MASK));
jMenuItemZoomNormal.setActionCommand("zoomnormal");
jMenuItemZoomNormal.addActionListener(this);
jMenu.add(jMenuItemZoomNormal);
jMenuItemZoomMinus = new JMenuItem("Zoom OUT", 'P');
jMenuItemZoomMinus.setAccelerator(KeyStroke.getKeyStroke('P', Event.CTRL_MASK));
jMenuItemZoomMinus.setActionCommand("zoomout");
jMenuItemZoomMinus.addActionListener(this);
jMenu.add(jMenuItemZoomMinus);
return jMenu;
}
private JMenu buildHelpMenu() {
JMenu jMenu = new JMenu("Help");
jMenu.setMnemonic('H');
JMenuItem jMenuItem = new JMenuItem("Help", 'h');
jMenuItem.setAccelerator(KeyStroke.getKeyStroke('H', Event.CTRL_MASK));
jMenuItem.setActionCommand("help");
jMenuItem.addActionListener(this);
jMenu.add(jMenuItem);
return jMenu;
}
/*
* private JButton jToolButtonReadParallel;
private JButton jToolButtonReadLog;
private JButton jToolButtonTar;
private JButton jToolButtonTpg;
*/
private JToolBar buildToolBar() {
JToolBar toolBar = new JToolBar();
/* jToolButtonSelectPl = new JButton(new ImageIcon(ClassLoader.getSystemResource("images/select_pl.gif")));
jToolButtonSelectPl.setActionCommand("partlist...");
jToolButtonSelectPl.addActionListener(this);
jToolButtonSelectPl.setBackground(COLOR_NEEDFUL);
toolBar.add(jToolButtonSelectPl);
jToolButtonSelectParallel = new JButton(new ImageIcon(ClassLoader.getSystemResource("images/select_parallel.gif")));
//jButtonParallel.setEnabled(false);
jToolButtonSelectParallel.setActionCommand("parallel...");
jToolButtonSelectParallel.addActionListener(this);
jToolButtonSelectParallel.setBackground(COLOR_NEEDFUL);
toolBar.add(jToolButtonSelectParallel);
jToolButtonSelectLog = new JButton(new ImageIcon(ClassLoader.getSystemResource("images/select_log.gif")));
//jButtonGenradLog.setEnabled(false);
jToolButtonSelectLog.setActionCommand("log...");
jToolButtonSelectLog.addActionListener(this);
jToolButtonSelectLog.setBackground(COLOR_NEEDFUL);
toolBar.add(jToolButtonSelectLog);
toolBar.addSeparator();
jToolButtonReadPl = new JButton(new ImageIcon(ClassLoader.getSystemResource("images/read_pl.gif")));
jToolButtonReadPl.setEnabled(false);
jToolButtonReadPl.setActionCommand("readPartList");
jToolButtonReadPl.addActionListener(this);
toolBar.add(jToolButtonReadPl);
jToolButtonReadParallel = new JButton(new ImageIcon(ClassLoader.getSystemResource("images/read_parallel.gif")));
jToolButtonReadParallel.setEnabled(false);
jToolButtonReadParallel.setActionCommand("readParallel");
jToolButtonReadParallel.addActionListener(this);
toolBar.add(jToolButtonReadParallel);
jToolButtonReadLog = new JButton(new ImageIcon(ClassLoader.getSystemResource("images/read_log.gif")));
jToolButtonReadLog.setEnabled(false);
jToolButtonReadLog.setActionCommand("readLogFile");
jToolButtonReadLog.addActionListener(this);
toolBar.add(jToolButtonReadLog);
toolBar.addSeparator();
jToolButtonTpg = new JButton(new ImageIcon(ClassLoader.getSystemResource("images/write_values.gif")));
jToolButtonTpg.setEnabled(false);
jToolButtonTpg.setActionCommand("printNewTpgValues");
jToolButtonTpg.addActionListener(this);
toolBar.add(jToolButtonTpg);
jToolButtonTar = new JButton(new ImageIcon(ClassLoader.getSystemResource("images/write_tar.gif")));
jToolButtonTar.setEnabled(false);
jToolButtonTar.setActionCommand("printTar");
jToolButtonTar.addActionListener(this);
toolBar.add(jToolButtonTar);*/
return toolBar;
}
@Override
public void componentHidden(ComponentEvent arg0) {
// TODO Auto-generated method stub
System.out.println("componentHidden");
//Zoomout verhindern, wenn Scrollbalken nicht mehr sichtbar sind.
this.jMenuItemZoomMinus.setEnabled(jScrollPane.getVerticalScrollBar().isVisible() ||
jScrollPane.getHorizontalScrollBar().isVisible());
}
@Override
public void componentMoved(ComponentEvent arg0) {
// TODO Auto-generated method stub
System.out.println("componentMoved");
}
@Override
public void componentResized(ComponentEvent arg0) {
// TODO Auto-generated method stub
System.out.println("componentResized");
}
@Override
public void componentShown(ComponentEvent arg0) {
// TODO Auto-generated method stub
System.out.println("componentShown");
//Zoomout verhindern, wenn Scrollbalken nicht mehr sichtbar sind.
this.jMenuItemZoomMinus.setEnabled((jScrollPane.getVerticalScrollBar().isVisible() ||
jScrollPane.getHorizontalScrollBar().isVisible()));
}
}