/*
* Copyright (C) 2012 FiveHT Media Ltd.
*
* 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/>.
*/
package com.fiveht.tick.ui;
import com.fiveht.tick.data.Properties;
import com.fiveht.tick.ui.event.CloseEvent;
import com.fiveht.tick.ui.event.CloseListener;
import com.fiveht.tick.ui.event.ExitEvent;
import com.fiveht.tick.ui.event.ExitListener;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.event.EventListenerList;
/**
* This class is for frame-based implementations, such as the deaktop
* application and the webstart.
*
* @author Nathan Crause
*/
public class Frame extends JFrame implements UIRoot, ExitListener, WindowListener {
/**
* The data view within this frame
*/
private View view;
/**
* Creates a new visual frame (window)
*
* @throws SQLException
*/
public Frame() throws SQLException {
super("Tick Java Desktop");
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
view = new View(this);
init();
}
private void init() throws SQLException {
Properties p = Properties.getInstance();
setLocation(p.getInt("x"), p.getInt("y"));
setSize(p.getInt("width", 360), p.getInt("height", 480));
setIconImage(Toolkit.getDefaultToolkit().getImage(Frame.class.getResource("/com/fiveht/tick/icons/clock.png")));
addWindowListener(this);
view.addExitListener(this);
// add the actual view now
setLayout(new BorderLayout());
add(view, BorderLayout.CENTER);
}
/**
* Gets the current View inside the frame.
*
* @return the current active view
*/
public View getView() {
return view;
}
@Override
public void exitSelected(ExitEvent event) {
try {
Properties p = Properties.getInstance();
p.set("x", getLocation().x);
p.set("y", getLocation().y);
p.set("width", getSize().width);
p.set("height", getSize().height);
}
catch (Throwable thrown) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, thrown);
JOptionPane.showMessageDialog(this, thrown.getMessage());
}
dispose();
System.exit(0);
}
private EventListenerList listeners = new EventListenerList();
public void addCloseListener(CloseListener l) {
listeners.add(CloseListener.class, l);
}
public void removeCloseListener(CloseListener l) {
listeners.remove(CloseListener.class, l);
}
protected void fireClose(CloseEvent event) {
// pack the listeners into a list
List<CloseListener> list = Arrays.asList(listeners.getListeners(CloseListener.class));
// reverse the order run the last first
Collections.reverse(list);
// loop through them all
for (CloseListener l : list) {
l.frameCloseSelected(event);
}
}
public void fireShow() {
if (isVisible())
requestFocus();
else
setVisible(true);
}
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
fireClose(new CloseEvent(this));
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
}