package periman;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import com.thoughtworks.xstream.InitializationException;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.XStreamException;
import com.thoughtworks.xstream.io.xml.DomDriver;
@SuppressWarnings("serial")
public class ScreenerManager extends JFrame {
ScreenerConfList serializableConfList = new ScreenerConfList();
ScreenerManager me = this;
JList confList;
ScreenerManager()
{
setTitle("Screener Manager");
setLocation(400, 300);
createMainMain();
createGui();
}
private void createMainMain() {
JMenuBar menuBar = new JMenuBar();
// File Menu, F - Mnemonic
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
JMenuItem openMenuItem = new JMenuItem("Open", KeyEvent.VK_O);
openMenuItem.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loadScreenerConfiguration();
}
});
fileMenu.add(openMenuItem);
// File->Save, S - Mnemonic
JMenuItem saveMenuItem = new JMenuItem("Save", KeyEvent.VK_S);
saveMenuItem.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
saveScreenerConfiguration();
}
}
);
fileMenu.add(saveMenuItem);
}
private void createGui() {
setLayout( new BoxLayout( getContentPane(), BoxLayout.Y_AXIS ) );
Box confListButtonsLayout = new Box( BoxLayout.X_AXIS );
JButton addBtn = new JButton("Add");
addBtn.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ScreenerConfigurationDialog d = new ScreenerConfigurationDialog(me);
serializableConfList.confListModel.addElement(d.getConf());
}
});
JButton removeBtn = new JButton("Remove");
confListButtonsLayout.add(addBtn);
confListButtonsLayout.add(removeBtn);
/*JButton modifyBtn = new JButton("Modify");
confListButtonsLayout.add(modifyBtn);*/
getContentPane().add(confListButtonsLayout);
confList = new JList( serializableConfList.confListModel );
JScrollPane confsScrollPane = new JScrollPane(confList);
getContentPane().add(confsScrollPane);
removeBtn.addActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
int i = confList.getSelectedIndex();
if(-1==i)
return;
serializableConfList.confListModel.remove(i);
}
});
pack();
setVisible(true);
}
private void saveScreenerConfiguration() {
String p = System.getProperty("user.dir") + "/Screener/";
String inptStr = JOptionPane.showInputDialog(this,
"Name the configuration (omit the extension, it will be added later).",
"Screener Manager - Save As",
JOptionPane.QUESTION_MESSAGE);
if(inptStr == null || inptStr.isEmpty())
return;
p+= inptStr;
Util.writeObject(p+".screener", serializableConfList);
}
private void loadScreenerConfiguration() {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Choose a Configuration file to load.");
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Screener Configuration Files (\"screener\")", "screener");
chooser.setFileFilter(filter);
if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION)
{
return;
}
try{
String filePath = chooser.getSelectedFile().getPath();
XStream xstream = new XStream( new DomDriver() );
FileReader fr = new FileReader(filePath);
serializableConfList = (ScreenerConfList)xstream.fromXML(fr);
confList.setModel( serializableConfList.confListModel);
}
catch(InitializationException e)
{
JOptionPane.showMessageDialog( this, e.getMessage());
}
catch(XStreamException e)
{
JOptionPane.showMessageDialog( this, e.getMessage());
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog( this, e.getMessage());
}
}
/**
* @param args
*/
public static void main(String[] args) {
ScreenerManager sm = new ScreenerManager();
sm.setVisible(true);
}
}