/*
* (c) Copyright 2007-2008 by Edgar Kalkowski (eMail@edgar-kalkowski.de)
*
* This file is part of the ErkiTalk Java Client.
*
* The ErkiTalk Java Client 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/>.
*/
package erki.talk.clients.ed;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ComponentListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
public class GUIThread extends Thread {
private static final String ICON_FILE = "res"
+ System.getProperty("file.separator") + "chat.png";
private int width, height, posx, posy;
private JFrame frame = new JFrame();
private JTextField input = new JTextField();
private JTextPane output = new JTextPane();
private JScrollPane scroll = new JScrollPane(output);
private String buffer = "";
private boolean sound = false, amarok = true;
private int alarm = 1;
private boolean isReady = false;
private boolean pingRequest = false, nickRequest = false;
private boolean blockSound = false;
private MyKeyListener listener;
private BufferedImage icon;
private TrayIcon trayIcon;
public GUIThread(int width, int height, int posx, int posy) {
super();
this.width = width;
this.height = height;
this.posx = posx;
this.posy = posy;
}
private void initIcon() {
try {
icon = ImageIO.read(new File(ICON_FILE));
frame.setIconImage(icon);
if (SystemTray.isSupported()) {
trayIcon = new TrayIcon(icon);
// trayIcon.setImageAutoSize(true);
trayIcon.setToolTip("ErkiTalk Java Client "
+ ErkiTalkClient.VERSION);
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if (frame.isVisible()) {
frame.setVisible(false);
} else {
frame.setState(JFrame.NORMAL);
frame.setVisible(true);
frame.toFront();
}
}
});
// SystemTray tray = SystemTray.getSystemTray();
// tray.add(trayIcon);
}
} catch (IOException e) {
// } catch (AWTException e) {
}
}
@Override
public void run() {
super.run();
initIcon();
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("ErkiTalk Java Client " + ErkiTalkClient.VERSION);
if (this.posx == -1 || this.posy == -1) {
frame.setLocationRelativeTo(null);
} else {
frame.setLocation(posx, posy);
}
WindowAdapter windowListener = new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
ErkiTalkClient.saveSettings(frame.getWidth(),
frame.getHeight(), frame.getLocation().x, frame
.getLocation().y);
}
@Override
public void windowGainedFocus(WindowEvent e) {
super.windowGainedFocus(e);
if (icon != null) {
frame.setIconImage(icon);
trayIcon.setImage(icon);
}
}
@Override
public void windowIconified(WindowEvent e) {
super.windowIconified(e);
if (SystemTray.isSupported()) {
// frame.setVisible(false);
}
}
};
frame.addWindowFocusListener(windowListener);
frame.addWindowListener(windowListener);
frame.addWindowStateListener(windowListener);
frame.setVisible(true);
Container cp = frame.getContentPane();
cp.setBackground(new Color(0, 0, 0));
cp.setLayout(null);
Font font = new Font(Font.MONOSPACED, Font.PLAIN, 16);
int fontHeight = input.getFontMetrics(font).getHeight();
input.setFont(font);
output.setFont(font);
output.setFocusable(false);
output.setBackground(new Color(0, 0, 0));
output.setForeground(new Color(127, 200, 255));
scroll.setBorder(null);
scroll.setSize(cp.getWidth(), cp.getHeight() - fontHeight);
scroll.setLocation(0, 0);
scroll
.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
input.setBorder(null);
input.setSize(cp.getWidth(), fontHeight);
input.setLocation(0, cp.getHeight() - fontHeight);
input.setBackground(new Color(0, 0, 0));
input.setForeground(new Color(127, 200, 255));
input.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
super.keyTyped(e);
if (icon != null) {
frame.setIconImage(icon);
trayIcon.setImage(icon);
}
}
});
cp.add(scroll);
cp.add(input);
frame.addComponentListener(new MyComponentListener(cp, input, output,
scroll));
isReady = true;
input.requestFocus();
}
public boolean isReady() {
return isReady;
}
public boolean isScroll() {
if (scroll.getVerticalScrollBarPolicy() == JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) {
return true;
} else {
return false;
}
}
public void setScroll(boolean scroll) {
if (scroll) {
this.scroll
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
} else {
this.scroll
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
}
}
public void reset() {
frame.setSize(width, height);
frame.setLocationRelativeTo(null);
int fontHeight = input.getFontMetrics(input.getFont()).getHeight();
Container cp = frame.getContentPane();
input.setSize(cp.getWidth(), fontHeight);
input.setLocation(0, cp.getHeight() - fontHeight);
scroll.setSize(cp.getWidth(), cp.getHeight() - fontHeight);
scroll.setLocation(0, 0);
output.revalidate();
}
public void center() {
frame.setLocationRelativeTo(null);
}
public boolean isAmarok() {
return amarok;
}
public void setAmarok(boolean amarok) {
this.amarok = amarok;
}
public String getPosition() {
return "(" + frame.getX() + ", " + frame.getY() + ")";
}
public String getSize() {
return frame.getWidth() + " x " + frame.getHeight();
}
public MyKeyListener getInputKeyListener() {
return listener;
}
public void addInputKeyListener(MyKeyListener listener) {
this.listener = listener;
input.addKeyListener(listener);
}
public void addComponentListener(ComponentListener listener) {
frame.addComponentListener(listener);
}
public void blockInput(boolean block) {
input.setEditable(block);
}
public void appendLineToOutputBuffer(String txt) {
synchronized (buffer) {
buffer += System.getProperty("line.separator") + "["
+ ErkiTalkClient.getTime() + "] " + txt;
}
}
public boolean isPingRequest() {
return pingRequest;
}
public void pongSent() {
pingRequest = false;
}
public boolean isNickRequest() {
return nickRequest;
}
public void nickSent() {
nickRequest = false;
}
public void clearInput() {
input.setText("");
}
public synchronized String getBuffer() {
return buffer;
}
public synchronized void setBuffer(String txt) {
buffer = txt;
}
public void setInputText(String text) {
input.setText(text);
}
public String getInputText() {
return input.getText();
}
public JTextPane getOutput() {
return output;
}
public boolean hasFocus() {
return input.hasFocus();
}
public void setIcon(BufferedImage icon) {
frame.setIconImage(icon);
if (trayIcon != null) {
trayIcon.setImage(icon);
}
}
public boolean isSound() {
return sound;
}
public void setSound(boolean value) {
sound = value;
}
public void setAlarm(int value) {
alarm = value;
}
public int getAlarm() {
return alarm;
}
public void saveSettings() {
ErkiTalkClient.saveSettings(frame.getWidth(), frame.getHeight(), frame
.getLocation().x, frame.getLocation().y);
}
public void switchColour() {
if (input.getForeground().equals(new Color(127, 200, 255))) {
input.setForeground(new Color(0, 200, 0));
output.setForeground(new Color(0, 200, 0));
appendLineToOutputBuffer("#I: Colour switched to green.");
} else {
input.setForeground(new Color(127, 200, 255));
output.setForeground(new Color(127, 200, 255));
appendLineToOutputBuffer("#I: Colour switched to blue.");
}
}
public void setBlockSound(boolean blockSound) {
this.blockSound = blockSound;
}
public boolean isBlockSound() {
return blockSound;
}
}