package frontend.dialogs;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import plugin.controller.PluginController;
import plugin.controller.PluginPropertyParser;
import skomp.io.ColorInputStream;
import skomp.io.ColorMode;
/**
* @author Foo Inc
*/
@SuppressWarnings("serial")
public class RAWImportPanel extends JPanel {
private JTextField m_statusField = new JTextField();
public RAWImportPanel() {
this.initComponents();
}
private JLabel colorDepthLabel, heightLabel, widthLabel;
private JTextField colorDepthTextField, heightTextField, pathOfImage,
statusTextField;
private JPanel imagePanel, inputs;
private JButton startConvertingButton;
private javax.swing.JTextField widthTextField;
private RenderPanel toDrawto;
private RenderMachine renderer;
private JFrame renderFrame;
private static RAWImportPanel master = new RAWImportPanel();
private void initComponents() {
pathOfImage = new javax.swing.JTextField();
colorDepthLabel = new javax.swing.JLabel("Farbtiefe (in Bit):");
widthLabel = new javax.swing.JLabel("Breite (in Pixel):");
heightLabel = new javax.swing.JLabel("H\u00f6he (in Pixel):");
colorDepthTextField = new javax.swing.JTextField("16");
widthTextField = new javax.swing.JTextField();
heightTextField = new javax.swing.JTextField();
startConvertingButton = new javax.swing.JButton();
statusTextField = new JTextField("Warte auf Eingabe");
imagePanel = new javax.swing.JPanel();
setName("mainFrame");
pathOfImage.setText("001/COCKPIT.R16");
PluginPropertyParser.getInstance();
widthTextField.setText("640");
heightTextField.setText("480");
startConvertingButton.setText("Let's do this!");
startConvertingButton
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startConvertingButtonActionPerformed(evt);
}
});
JPanel contentPane = this;
contentPane.setLayout(new BorderLayout());
JPanel upper = new JPanel();
upper.setLayout(new BorderLayout());
inputs = new JPanel();
inputs.setLayout(new GridLayout(4, 2));
inputs.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
inputs.add(colorDepthLabel);
inputs.add(colorDepthTextField);
inputs.add(widthLabel);
inputs.add(widthTextField);
inputs.add(heightLabel);
inputs.add(heightTextField);
upper.add(inputs, BorderLayout.WEST);
upper.add(startConvertingButton, BorderLayout.CENTER);
upper.add(pathOfImage, BorderLayout.SOUTH);
contentPane.add(upper, BorderLayout.NORTH);
imagePanel.setSize(640, 480);
imagePanel.setMinimumSize(new Dimension(640, 480));
contentPane.add(imagePanel, BorderLayout.CENTER);
statusTextField.setEditable(false);
contentPane.add(statusTextField, BorderLayout.SOUTH);
this.setSize(800, 600);
if (System.currentTimeMillis() % 10 == 0) {
JOptionPane.showConfirmDialog(this, "", "Watch Out",
JOptionPane.YES_NO_CANCEL_OPTION);
}
this.setVisible(true);
}
private void startConvertingButtonActionPerformed(
java.awt.event.ActionEvent evt) {
File imageFile = new File(pathOfImage.getText());
Color green = new Color(127, 255, 127);
Color red = new Color(255, 127, 127);
ColorMode colorDepth = null;
if (!imageFile.exists()) {
statusTextField
.setText(">>> Datei nicht gefunden, Eingabe pr�fen.");
statusTextField.setBackground(red);
return;
} else {
if (!imageFile.isFile()) {
statusTextField
.setText(">>> Keine Datei angegeben, Eingabe pr�fen.");
statusTextField.setBackground(red);
return;
} else {
if (!imageFile.canRead()) {
statusTextField
.setText(">>> Datei nicht lesbar, Zustand und Berechtigung pr�fen.");
statusTextField.setBackground(red);
return;
} else {
if (colorDepthTextField.getText().length() == 0) {
statusTextField
.setText(">>> Farbtiefe nicht erkannt, Eingabe pr�fen.");
statusTextField.setBackground(red);
return;
} else {
switch (Integer.valueOf(colorDepthTextField.getText())) {
case 8: {
colorDepth = ColorMode.TRUE8BIT;
break;
}
case 15: {
colorDepth = ColorMode.HICOLOR15bit;
break;
}
case 16: {
colorDepth = ColorMode.HICOLOR16BIT;
break;
}
case 24: {
colorDepth = ColorMode.TRUECOLOR24BIT;
break;
}
case 32: {
colorDepth = ColorMode.TRUECOLOR32BIT;
break;
}
}
if (widthTextField.getText().length() == 0) {
statusTextField
.setText(">>> Breite nicht erkannt, Eingabe pr�fen.");
statusTextField.setBackground(red);
return;
} else {
if (heightTextField.getText().length() == 0) {
statusTextField
.setText(">>> H�he nicht erkannt, Eingabe pr�fen.");
statusTextField.setBackground(red);
return;
}
}
}
}
}
}
statusTextField.setText(">>> Datei erfasst.");
statusTextField.setBackground(green);
try {
FileInputStream fis = new FileInputStream(imageFile);
ColorInputStream cis = new ColorInputStream(fis, colorDepth, true);
renderFrame = new JFrame("Renderer");
renderFrame.setUndecorated(true);
toDrawto = new RenderPanel(new Dimension(Integer
.parseInt(widthTextField.getText()), Integer
.parseInt(heightTextField.getText())));
int width = Integer.parseInt(widthTextField.getText());
int height = Integer.parseInt(heightTextField.getText());
renderFrame.setSize(width, height);
renderFrame.setContentPane(toDrawto);
renderFrame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
renderFrame.dispose();
RAWImportPanel.getMaster().setStatus("Warte auf eingabe",
Color.LIGHT_GRAY);
}
});
renderFrame.setVisible(true);
renderer = new RenderMachine(toDrawto, cis, width, height);
renderer.start();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
protected void setStatus(String string, Color color) {
this.statusTextField.setText(string);
this.statusTextField.setBackground(color);
}
public static void main(String args[]) {
}
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
synchronized (renderer) {
renderer.interrupt();
synchronized (this) {
try {
this.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
System.exit(0);
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public JFrame getRenderFrame() {
return renderFrame;
}
public static RAWImportPanel getMaster() {
return master;
}
}
class RenderPanel extends JPanel {
private int y = 0, x = 0;
private Color nextColor;
private BufferedImage image;
private Graphics _graph;
private JDialog progressDialog = new JDialog(RAWImportPanel.getMaster()
.getRenderFrame());
private JProgressBar progress;
public RenderPanel(Dimension size) {
super(true);
this.setSize(size);
image = new BufferedImage((int) size.getWidth(),
(int) size.getHeight(), BufferedImage.TYPE_INT_ARGB);
progress = progress = new JProgressBar(0,
(int) (size.getHeight() * size.getWidth()));
progress.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10,
Color.LIGHT_GRAY));
progressDialog.add(progress);
progressDialog.setTitle("Rendering...");
progressDialog.pack();
progressDialog.setModal(false);
}
@Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, this);
}
public synchronized void setNextPixel(int x, int y, Color col) {
this.x = x;
this.y = y;
this.nextColor = col;
_graph = getGraphics();
image.setRGB(x, y, nextColor.getRGB());
}
public void display() {
paintComponent(_graph);
}
public JProgressBar getProgress() {
return progress;
}
public JDialog getProgressFrame() {
return progressDialog;
}
public BufferedImage getImage() {
return this.image;
}
}
class RenderMachine extends Thread {
ColorInputStream cis;
RenderPanel view;
int width, height;
public RenderMachine(RenderPanel toDrawTo, ColorInputStream cis, int width,
int height) {
this.view = toDrawTo;
this.cis = cis;
this.width = width;
this.height = height;
view.getProgressFrame().setLocation(
(int) RAWImportPanel.getMaster().getRenderFrame()
.getLocationOnScreen().getX()
+ width / 2,
(int) (RAWImportPanel.getMaster().getRenderFrame()
.getLocationOnScreen().getY() + height / 2));
}
public void run() {
Color in;
view.getProgressFrame().setVisible(true);
for (int i = 0; i < 17500; i++) {
try {
cis.readNextColor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
for (int i = 0; i<height; i++) {
for (int j = 0; j < width; j++) {
if((in=cis.readNextColor())==null){
return;
}
try {
view.setNextPixel(j, i, in);
view.getProgress().setValue((int) i);
} catch (Exception e) {
e.printStackTrace();
System.out.println(i % width + " " + i / width);
}}
}
view.display();
} catch (IOException e) {
e.printStackTrace();
try {
cis.close();
} catch (IOException e1) {
}
}
view.getProgressFrame().dispose();
synchronized (this) {
notifyAll();
}
}
}