package myTest;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
public class MyTest {
static class MyCellRenderer extends JLabel implements
ListCellRenderer<Object> {
private static final long serialVersionUID = 577071018465376381L;
public MyCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(JList<?> list,
Object value, int index, boolean isSelected,
boolean cellHasFocus) {
if (value.getClass().equals(JLabel.class)) {
JLabel label = JLabel.class.cast(value);
setText(label.getText());
setIcon(label.getIcon());
setBackground(label.getBackground());
setForeground(label.getForeground());
} else {
setText(value.toString());
setBackground(Color.WHITE);
setForeground(Color.BLACK);
}
return this;
}
}
static final Map<String, String> fileToPicture = new HashMap<String, String>() {
private static final long serialVersionUID = 1L;
{
put("binary1", "binary1.jpg");
put("binary2", "binary2.jpg");
}
};
static boolean endProcess;
static boolean guiStarted;
static DefaultListModel<Object> listModel;
static JList<Object> list;
static public void startGui() {
JFrame frame = new JFrame("Building...");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
listModel = new DefaultListModel<Object>();
list = new JList<Object>(listModel);
list.setCellRenderer(new MyCellRenderer());
list.setLayoutOrientation(JList.VERTICAL);
list.setFixedCellHeight(-1);
JScrollPane scrollPane = new JScrollPane(list);
scrollPane.setPreferredSize(new Dimension(300, 500));
frame.getContentPane().add(scrollPane, BorderLayout.NORTH);
JButton ok = new JButton("CLOSE");
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
endProcess = true;
};
});
frame.getContentPane().add(ok, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
guiStarted = true;
}
static private void addElem(String string, Color color) {
String fileName = fileToPicture.get(string);
ImageIcon icon = null;
if (null != fileName) {
File file = new File(new File(".").getAbsolutePath() + "/"
+ fileName);
try {
icon = new ImageIcon(ImageIO.read(file));
} catch (IOException e) {
System.err
.println("Exception: IOException for trying to read file '"
+ file + "'");
e.printStackTrace();
}
}
JLabel label = new JLabel(string);
Color darker = color.darker();
label.setForeground(darker);
label.setBackground(Color.WHITE);
label.setIcon(icon);
listModel.addElement(label);
list.ensureIndexIsVisible(list.getModel().getSize() - 1);
}
public static void main(String[] args) {
endProcess = false;
guiStarted = false;
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
startGui();
}
});
while (!guiStarted) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
try {
while (!endProcess) {
line = br.readLine();
if (line == null) {
break;
}
addElem(line, Color.BLACK);
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
if (!endProcess) {
addElem("...DONE", Color.RED);
while (!endProcess) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.exit(0);
}
}