package periman;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import java.io.*;
/**
*
* @author kroiz
* Builds the GUI.
* Has states (pattern) for when there is no document/configuration loaded and
* for when there is. the state classes change the state themselves.
*
* Holds THE Configuration instance.
* Holds the Working Directory.
*/
@SuppressWarnings("serial")
public class MainWindow extends JFrame implements WindowListener, ActionListener
{
private final String CREATE_QUESTIONNAIRE_FORM = "CREATE_QUESTIONNAIRE_FORM_ACTION";
private final String CREATE_HTML_FORM = "CREATE_HTML_FORM_ACTION";
private final String CREATE_IMG_FORM = "CREATE_IMAGE_FORM_ACTION";
private JMenuItem newMenuItem;
private JMenuItem openMenuItem;
private JMenuItem closeMenuItem;
private JMenuItem saveMenuItem;
private JMenuItem saveAsMenuItem;
private JMenu formMenu;
private JButton selectFormBtn;
private JButton unselectFormBtn;
private JButton moveSelectionFormUpBtn;
private JButton moveSelectionFormDownBtn;
private JLabel selectLbl;
private JLabel avaiLbl;
private JTextField outPathField;
private JPanel outputPanel;
private JLabel outputLbl;
/**
* currentConf, Holds the currently opened Configuration instance.
* lastSavedConf, holds an instance of the last saved Configuration to compare against
* to find out if any changes were made ( before closing or exiting ).
*/
private Configuration currentConf;
private Configuration lastSavedConf;
// holds the current state (see state pattern)
private DocumentState document;
JList selectedFormsList;
DefaultListModel availableFormsModel;
JList availableFormsList;
/** Holds the Working Directory.
* In the Working directory you could find the:
* Configuration file(s)
* The forms for the Configuration file(s)
*/
private String workingDir;
public void setWorkingDir( String workingDir) {
this.workingDir = workingDir;
loadForms();
}
public String getWorkingDir() {
return workingDir;
}
public Configuration getConfiguration(){
return currentConf;
}
public void setConf(Configuration configuration) {
currentConf = configuration;
selectedFormsList.setModel( currentConf.getForms() );
outPathField.setText(currentConf.getOutputDestination());
}
public void setLastSavedConfToNew()
{
lastSavedConf = null;
}
public void setLastSavedConfToOpened()
{
lastSavedConf = currentConf;
}
public void setConfigurationFileName(String configurationFileName) {
if(configurationFileName.isEmpty())
{
setTitle("Periman");
availableFormsModel.clear();
if(currentConf != null)
currentConf.clearFormsModel();
return;
}
setTitle(configurationFileName + " - Periman");
}
public void changeDocState(DocumentState docState) {
document = docState;
}
public void enableOpenMenuItem(Boolean b)
{
openMenuItem.setEnabled(b);
}
public void enableNewMenuItem(Boolean b)
{
newMenuItem.setEnabled(b);
}
public void enableCloseMenuItem(Boolean b)
{
closeMenuItem.setEnabled(b);
}
public void enableSaveMenuItem(Boolean b)
{
saveMenuItem.setEnabled(b);
}
public void enableSaveAsMenuItem(Boolean b)
{
saveAsMenuItem.setEnabled(b);
}
public MainWindow()
{
createMainMain();
layComponents();
document = new NoDocumentState(this);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(this);
setSize(350, 250);
setLocation(400,400);
validate();
pack();
setVisible(true);
}
public void loadForms()
{
if(workingDir==null)
return;
FileFilter dirsOnlyPlease = new FileFilter() {
@Override
public boolean accept(File pathName)
{
if(pathName.isDirectory())
return true;
return false;
}
};
File f = new File(workingDir);
File files[]=f.listFiles(dirsOnlyPlease);
availableFormsModel.clear();
for (File itf : files)
availableFormsModel.addElement(itf.getName());
}
// setting up the layout of the component in the frame.
private void layComponents() {
setLayout( new BoxLayout( getContentPane(), BoxLayout.Y_AXIS) );
JPanel formSelectPanel = new JPanel();
add(formSelectPanel);
BoxLayout formSelectLayout = new BoxLayout(formSelectPanel, BoxLayout.X_AXIS);
formSelectPanel.setLayout(formSelectLayout);
// Create list of selected forms
Box availableVBox = new Box(BoxLayout.Y_AXIS);
formSelectPanel.add(availableVBox);
avaiLbl = new JLabel("Available Forms");
availableVBox.add(avaiLbl);
availableFormsModel = new DefaultListModel();
availableFormsList = new JList(availableFormsModel);
availableFormsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
availableFormsList.setLayoutOrientation(JList.VERTICAL);
JScrollPane availableFormsScrollPane = new JScrollPane(availableFormsList);
availableVBox.add(availableFormsScrollPane);
/*..................................*/
// create a Vertical box for the buttons for selecting and unselecting a form
selectFormBtn = new JButton("->");
unselectFormBtn = new JButton("<-");
Box selectBtnsPanel = new Box( BoxLayout.Y_AXIS);
selectBtnsPanel.add(selectFormBtn);
selectBtnsPanel.add(unselectFormBtn);
formSelectPanel.add(selectBtnsPanel);
selectFormBtn.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
final int NOTING_SELECTED = -1;
if(availableFormsList.getSelectedIndex() == NOTING_SELECTED)
return;
Object currForm = availableFormsList.getSelectedValue();
currentConf.addForm( currForm.toString() );
}
});
unselectFormBtn.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
final int NOTING_SELECTED = -1;
if(selectedFormsList.getSelectedIndex() == NOTING_SELECTED)
return;
int sel = selectedFormsList.getSelectedIndex();
currentConf.removeForm( sel );
}
});
/*..................................*/
Box selectedVBox = new Box(BoxLayout.Y_AXIS);
formSelectPanel.add(selectedVBox);
selectLbl = new JLabel("Selected Forms");
selectedVBox.add(selectLbl);
selectedFormsList = new JList();
selectedFormsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
selectedFormsList.setLayoutOrientation(JList.VERTICAL);
JScrollPane selectedFormsScrollPane = new JScrollPane(selectedFormsList);
selectedVBox.add( selectedFormsScrollPane);
/*..................................*/
// vertical box of up and down btns
moveSelectionFormUpBtn = new JButton("Up");
moveSelectionFormDownBtn = new JButton("Dn");
// Create a Vertical box for holding the up and down button
Box orderSelectionPanel = new Box( BoxLayout.Y_AXIS);
orderSelectionPanel.add(moveSelectionFormUpBtn);
orderSelectionPanel.add(moveSelectionFormDownBtn);
formSelectPanel.add(orderSelectionPanel);
/*..................................*/
JSeparator separator = new JSeparator();
add(separator);
outputPanel = new JPanel();
BoxLayout outLayout= new BoxLayout(outputPanel, BoxLayout.X_AXIS);
outputPanel.setLayout(outLayout);
outputPanel.setBorder(BorderFactory.createTitledBorder("Output destination path"));
add(outputPanel);
outputLbl = new JLabel("Path");
outputPanel.add(outputLbl);
outPathField = new JTextField();
outPathField.addFocusListener( new FocusListener() {
@Override
public void focusLost(FocusEvent arg0) {
if(currentConf == null)
return;
currentConf.setOutputDestination( outPathField.getText() );
}
@Override
public void focusGained(FocusEvent arg0) {}
});
outputPanel.add(outPathField);
}
private void createMainMain() {
JMenuBar menuBar = new JMenuBar();
// File Menu, F - Mnemonic
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
formMenu = new JMenu("Form");
formMenu.setMnemonic(KeyEvent.VK_O);
menuBar.add(formMenu);
JMenu formCreateMenu = new JMenu("Create");
formCreateMenu.setMnemonic(KeyEvent.VK_C);
formMenu.add(formCreateMenu);
JMenuItem createHTMLForm = new JMenuItem("HTML", KeyEvent.VK_H);
createHTMLForm.addActionListener(this);
createHTMLForm.setActionCommand( CREATE_HTML_FORM);
formCreateMenu.add(createHTMLForm);
JMenuItem createQuestionnaireForm = new JMenuItem("Questionnaire", KeyEvent.VK_Q);
createQuestionnaireForm.addActionListener(this);
createQuestionnaireForm.setActionCommand( CREATE_QUESTIONNAIRE_FORM );
formCreateMenu.add(createQuestionnaireForm);
JMenuItem createImageForm = new JMenuItem("Image", KeyEvent.VK_I);
createImageForm.addActionListener(this);
createImageForm.setActionCommand(CREATE_IMG_FORM);
formCreateMenu.add(createImageForm);
// File->New, N - Mnemonic
newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
newMenuItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
document.newDoc();
}
}
);
fileMenu.add(newMenuItem);
// File->Open, O - Mnemonic
openMenuItem = new JMenuItem("Open", KeyEvent.VK_O);
openMenuItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
document.open();
}
}
);
fileMenu.add(openMenuItem);
// File->Close, C - Mnemonic
closeMenuItem = new JMenuItem("Close", KeyEvent.VK_C);
closeMenuItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
document.close();
}
}
);
fileMenu.add(closeMenuItem);
// Separator
fileMenu.addSeparator();
// File->Save, S - Mnemonic
saveMenuItem = new JMenuItem("Save", KeyEvent.VK_S);
saveMenuItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
document.save();
}
}
);
fileMenu.add(saveMenuItem);
saveAsMenuItem = new JMenuItem("Save As", KeyEvent.VK_A);
saveAsMenuItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
document.saveAs();
}
}
);
fileMenu.add(saveAsMenuItem);
// Separator
fileMenu.addSeparator();
// File->Exit, X - Mnemonic
JMenuItem exitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
exitMenuItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
document.exit();
}
}
);
fileMenu.add(exitMenuItem);
setJMenuBar(menuBar);
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("WindowClosed Event");
}
/**
* this override from WindowListner.
*/
@Override
public void windowClosing(WindowEvent e) {
if(closing() == true)
{
// no unsaved changes to the current file - exiting.
releaseApplicationResources();
System.exit(0);
}
}
@Override
public void windowActivated(WindowEvent e) {}
@Override
public void windowDeactivated(WindowEvent e) {}
@Override
public void windowDeiconified(WindowEvent e) {}
@Override
public void windowIconified(WindowEvent e) {}
@Override
public void windowOpened(WindowEvent e) {}
/**
* prompt the user if unsaved changes where made
* by not doing anything the closing of the application is canceled.
* (yeah really, look in the documentation of WindowListner::windowClosing
* return false if user decided not to exit after all.
* return true if user chose to exit.
*
* function is called for both closing the application and just closing the current doc
*/
public boolean closing() {
if(currentConf!=null)
if(currentConf.equals( lastSavedConf ) == false)
{
int r =
JOptionPane.showConfirmDialog(this,
"There are some unsaved changes.\n Are you sure you want to close?",
"Close?",
JOptionPane.YES_NO_OPTION);
if(r==JOptionPane.YES_OPTION)
return true;
return false;
}
return true;
}
// currently nothing to release.
private void releaseApplicationResources() {
}
public void setThingsEnabled(boolean b) {
selectFormBtn.setEnabled(b);
unselectFormBtn.setEnabled(b);
moveSelectionFormUpBtn.setEnabled(b);
moveSelectionFormDownBtn.setEnabled(b);
selectedFormsList.setEnabled(b);
availableFormsList.setEnabled(b);
formMenu.setEnabled(b);
outPathField.setEnabled(b);
outputPanel.setEnabled(b);
outputLbl.setEnabled(b);
selectLbl.setEnabled(b);
avaiLbl.setEnabled(b);
}
@Override
public void actionPerformed(ActionEvent fp_command) {
if (fp_command.getActionCommand().equals( CREATE_QUESTIONNAIRE_FORM) )
{
new QuestionnaireFormCreator(this);
}
if(fp_command.getActionCommand().equals( CREATE_HTML_FORM))
{
new HTMLFormCreator(this);
}
if(fp_command.getActionCommand().equals( CREATE_IMG_FORM))
{
new ImageFormCreator(this);
}
}
}