/** File: CvtGUI.java */ import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import O2PlibSS.gui.*; /** * Class CvtGUI is used to create a popup GUI to run the converter. *
* List of Methods*
* =================== * CvtGUI() - Constructor * initGUI() - create the popup GUI * addMenuTree() - create pull down menus in menu bar for the GUI * processGuiStateData() - Process data specified by the GUI state * setCancelFlag() - set the Cancel flag value * getCancelFlag() - get the Cancel flag value * setEnableAssignButtonsAndMenus() - set Assign buttons & menus state * setEnableProcessingButtonsAndMenus() - when are able to start processing state * setEnableCancelButtonsAndMenus() - when are able to do processing state * setEnableViewingButtonsAndMenus() - when can view processed HTML files state * positionReportWindow() - position report window on the screen * setShow() - set the show/hide popup status * updateReportStatusLine() - clear text in popup window * clearText() - clear text in popup window * getReportText() - get the text from the Report popup window * setReportText() - update text in popup window * setGUItitle() - update title in popup window * setSaveAsFile() - set SaveAs file default file name * close() - close this popup and reset flags if needed * testAndSetProjDir() - [DEPRICATE] test and set project directory cvt.userDir. * actionPerformed() - Handle menu actions and button clicks * printGuiFSM() - print GuiFSM if printFlag set * printGuiFSMlf() - print GuiFSM if printFlag set followed by extra line feed * getGuiFSM() - Get the GUI FSM state string. * clearGuiProcessingState() - clear the GUI state * setCancelFlag() - set the Cancel flag value * getCancelFlag() - get the Cancel flag value * setEnableAssignState() - set Assign buttons & menus state. * setEnableProcessingState() - when are able to start processing state. * setEnableCancelState() - when are able to do processing state. * getListGeneratedHTMLfileNames() - list the generated HTML file names. * pickGeneratedHTMLfileName() - list the generated HTML file names. * quit() - closing down the window, get rid of the frame. * itemStateChanged() - handle item state changed events * viewHTMLfile() - popup browser on guiActiveHTMLfile file. * windowClosing() - closing down the window, get rid of the frame. *
* This code is available at the HTMLtools project on SourceForge at * http://htmltools.sourceforge.net/ * under the "Common Public License Version 1.0" * * http://www.opensource.org/licenses/cpl1.0.php.
** It was derived and refactored from the open source * MAExplorer (http://maexplorer.sourceforge.net/), and * Open2Dprot (http://Open2Dprot.sourceforge.net/) Table modules. *
* $Date: 2009/08/18 11:45:56 $ $Revision: 1.31 $
*
* Copyright 2008, 2009 by Peter Lemkin
* E-Mail: lemkin@users.sourceforge.net
* http://lemkingroup.com/
*
*/
public class CvtGUI extends JFrame
implements ItemListener, ActionListener, WindowListener
{ /* CvtGUI */
public final static long
serialVersionUID= 0;
/** May need to define so can pass to InvokeLater methods */
private CvtGUI
gui= this;
/* Note all global variables are in Globals.java except the help
* messages which are in HelpMsgs.java.
*/
public HTMLtools
cvt;
/** string for title of frame */
private String
title;
/** The name of the program */
private String
programName;
/** version number of the program */
private String
version;
/* --------- GUI components ----- */
/** menu bar for the frame */
private MenuBar
mbar;
/** "File" pull down Menu list */
private Menu
fileMenu;
/** "Help" pull down Menu list */
private Menu
helpMenu;
/** Menu support functions using Hashtables to track what is active */
private MenuSupport
ms;
/** Menu checkbox item */
private CheckboxMenuItem
verboseMCB= null;
/** copy of text in textArea */
private String
textReport= "";
/** default SAVE AS .txt file for textReport data. */
private String
reportFile;
/* showStatusFlag to show (true), hide(false) */
public boolean
showStatusFlag;
/** Text area GUI for main report */
private JTextArea
textarea;
/** font size */
private int
textAreaFontSize= 12;
/** # of character rows to show */
private int
nRows= 24;
/** # of character cols to show */
private int
nCols= 80;
/** Text area font family */
private String
textFontFamily= "Helvetica";
/** button for "Process" button - may enable/disable button */
public JButton
processButton;
/** button for "Cancel" button - may enable/disable button */
public JButton
cancelButton;
/** button for "Reset" GUI button */
private JButton
resetButton;
/* List of generated HTML files to view */
private Choice
viewHTMLchoice;
/** Status line label updated with UtilCM.updateReportStatusLine() */
private JLabel
statusLineLabel;
/* ---------- GUI State ----------- */
/** Thread for converter processing */
public ProcessData
pd= null;
/** "Processing" data in the GUI */
public volatile boolean
processFlag= false;
/** "Process was ok" if true. Set to false if any problems. */
public volatile boolean
processedOKflag= false;
/** "Done" processing data in the GUI */
public volatile boolean
doneFlag= false;
/** "Close" the GUI */
public volatile boolean
closeGuiFlag= false;
/** "Cancel" the processing in the GUI */
public volatile boolean
cancelFlag= false;
/** GUI FSM state */
public volatile String
guiFSMstate= "reset";
/** print FSM debug statements in printGuiFSM() */
public volatile boolean
printFSMflag= false;
/** GUI Converter -batch DoIt file */
public volatile String
guiBatchDoItFile= null;
/** GUI Converter params map file */
public volatile String
guiParamsMapFile= null;
/* GUI Converter active HTML file that was selected for viewing. */
public volatile String
guiActiveHTMLfile= null;
/** Command line string created to use when running the converter
* used by ProcessData */
public volatile String
sGuiArgV= null;
/** Command line string array created to use when running the
* converter used by ProcessData */
public volatile String
guiArgV[]= null;
/**
* CvtGUI() - Constructor
* @param cvt is an instance of the HTMLtools class
* @param programName name of the program
* @param version - of program
* @param textReport is initial text string for buffer.
* @param nRows is maximum size of window
* @param nCols, is maximum size of window
* @param title of the window
* @param String reportFile is default SAVE AS .txt file. Default
* "log.txt"
* @param showStatusFlag to show (true), hide(false).
*/
public CvtGUI(HTMLtools cvt,
String programName, String version,
String textReport, int nRows, int nCols, String title,
String reportFile, boolean showStatusFlag)
{ /* CvtGUI */
super("CvtGUI");
/* [1] Setup copies of args */
this.gui= this; /* may need for InvokeLater method calls */
this.cvt= cvt;
this.programName= programName;
this.version= version;
this.title= title;
this.textReport= ((textReport==null)
? "" : textReport);
this.nCols= nCols;
this.nRows= nRows;
this.reportFile= (reportFile!=null)
? reportFile : programName+"-Report.txt";
this.showStatusFlag= showStatusFlag;
closeGuiFlag= false; /* Only set true by "Close" menu or button */
/* [2] Reset the GUI state flags to the initial "Assign" data state. */
clearGuiProcessingState(); /* clear the GUI state */
/* [3] Create and startup the popup GUI */
initGUI();
} /* CvtGUI */
/**
* initGUI() - create the popup GUI
* @return true if succeed
*/
public boolean initGUI()
{ /* initGUI */
/* [1] Setup the GUI */
mbar= new MenuBar();
this.setMenuBar(mbar); /* activate menu bar even if menus
* are not active*/
Font mbarFont= new Font("Helvetica", Font.PLAIN, 12);
mbar.setFont(mbarFont); /* make it a larger font */
/* [2] Create pull down menus in menu bar */
addMenuTree(this, mbar);
/* Create a TextArea to display the contents of the file/string.
* Should use equi-space font such as courier.
*/
//textFontFamily= "Helvetica";
textFontFamily= "Courier";
textAreaFontSize= 12;
textarea= new JTextArea(nRows,nCols);
textarea.setFont(new Font(textFontFamily,Font.PLAIN,textAreaFontSize));
textarea.setEditable(false);
textarea.setBackground(Color.white);
JScrollPane taPane= new JScrollPane(textarea);
taPane.setWheelScrollingEnabled(true);
textarea.setToolTipText("Results of processing. Scroll to bottom for "+
"lastest output.");
this.add("Center", taPane);
/* Notify UtilCM that the logMsg() text area exists */
UtilCM.setReportTextArea(textarea);
textarea.setText(textReport);
/* Create a bottom panel to hold a couple of buttons, the
* HTML chooser and the status line.
*/
FlowLayout cpFlowLayout= new FlowLayout(FlowLayout.LEFT, 6,1);
JPanel controlSubPanel= new JPanel();
controlSubPanel.setLayout(cpFlowLayout);
GridLayout cpGridLayout= new GridLayout(2,1,1,1);
JPanel controlPanel= new JPanel();
controlPanel.setLayout(cpGridLayout);
controlPanel.add(controlSubPanel);
this.add(controlPanel, "South");
/* Create the buttons and arrange to handle button clicks */
Font buttonFont= new Font("Helvetica", Font.PLAIN /*BOLD*/, 12);
JButton b= new JButton("Clear report");
b.setActionCommand("ClearReport");
b.addActionListener(this);
b.setFont(buttonFont);
b.setToolTipText("Clear the Report-Log.");
controlSubPanel.add(b);
b= new JButton("Save as report");
b.setActionCommand("SaveAsReport");
b.addActionListener(this);
b.setFont(buttonFont);
b.setToolTipText("Save the current report log in a text file.");
controlSubPanel.add(b);
/* Note: we set some button objects to variables so we can
* enable/disable them.
*/
processButton= new JButton("Process");
processButton.setActionCommand("Process");
processButton.addActionListener(this);
processButton.setFont(buttonFont);
processButton.setToolTipText("Start processing after you have specified "+
"the batch '.doit' or params '.map' script file.");
controlSubPanel.add(processButton);
cancelButton= new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
cancelButton.addActionListener(this);
cancelButton.setFont(buttonFont);
cancelButton.setToolTipText("Cancel processing. Processing intermediate "+
"results appear in the Report log window.");
controlSubPanel.add(cancelButton);
viewHTMLchoice= new Choice();
viewHTMLchoice.add("This pull-down menu will contain computed HTML files.");
viewHTMLchoice.addItemListener(this);
controlSubPanel.add(viewHTMLchoice);
resetButton= new JButton("Reset");
resetButton.setActionCommand("resetConverterer");
resetButton.addActionListener(this);
resetButton.setFont(buttonFont);
resetButton.setToolTipText("Reset the converter to the initial state. "+
"You must then specify a batch '.doit' or "+
"params '.map' script file");
controlSubPanel.add(resetButton);
/* Add status line on 2nd Grid line of control panel that is
* updated with UtilCM.updateReportStatusLine().
*/
String statusLineStr= programName+" converter program";
statusLineLabel= new JLabel(statusLineStr);
statusLineLabel.setFont(new Font(textFontFamily, Font.PLAIN,10));
statusLineLabel.setBackground(Color.white);
statusLineLabel.setToolTipText("Status line show what the converter is "+
"doing or if it is waiting for user input.");
controlPanel.add(statusLineLabel);
UtilCM.setReportStatusLineLabel(statusLineLabel);
/* Setup the listener thread to "Process" data */
processButton.addActionListener(new ActionListener()
{ /* add new listener thread to start ProcessData */
public void actionPerformed(ActionEvent evt)
{ /* actionPerformed processButton */
if(processFlag)
{
UtilCM.logMsg("\nCan't start a new conversion process until "+
"the current one is finished or canceled.\n");
return;
}
/* Start the conversion process in a new ProcessData thread */
cvt.setAbortProcessingFlag(false); /* clear flag */
if(!processGuiStateData())
return; /* Don't start processing */
/* Start Processing */
pd= new ProcessData(gui,sGuiArgV, guiArgV);
pd.start();
processButton.setEnabled(false);
cancelButton.setEnabled(true);
gui.requestFocus();
} /* actionPerformed processButton */
} /* add new listener thread to start ProcessData */
);
/* Setup the listener thread to "Cancel" ProcessData */
cancelButton.addActionListener(new ActionListener()
{ /* add new listener thread to start ProcessData */
public void actionPerformed(ActionEvent evt)
{ /* actionPerformed cancelButton */
if(!processFlag)
{
UtilCM.logMsg("\nCan't cancel conversion since no "+
"conversion process is running.\n");
return;
}
pd.interrupt();
/* kill the processing */
UtilCM.logMsg("\nInterrupting processing....\n");
UtilCM.updateReportStatusLine("Canceled processing...");
cvt.setAbortProcessingFlag(true); /* set flag */
/* clear Generated HTML path List cvt.genHTMLpathList[].
* Will get list of converted HTML files.
*/
cvt.clearGenHTMLpathList(true);
gui.setEnableAssignButtonsAndMenus();
gui.requestFocus();
} /* actionPerformed cancelButton */
} /* add new listener thread to start ProcessData */
);
this.addWindowListener(this); /* listener for window events */
this.setGUItitle(title+" - "+version);
UtilCM.updateReportStatusLine("Assign file");
/* Display the GUI */
this.validate();
this.pack();
/* Center frame on the screen, position report window on the screen */
positionReportWindow();
/* Popup the window */
this.setVisible(showStatusFlag);
/* Reset the GUI state so no files specified and set Assign buttons
* & menus state. This sets the flags to the initial "Assign" data
* state.
*/
setEnableAssignButtonsAndMenus();
return(true);
} /* initGUI */
/**
* addMenuTree() - create pull down menus in menu bar for the GUI
* @param f is frame
* @param mbar is MenuBar
*/
private void addMenuTree(Frame f, MenuBar mbar)
{ /* addMenuTree */
Menu smnu= null;
/* Setup menu support hash tables */
ms= new MenuSupport();
/* [1] Create menus to put in the tree */
fileMenu= new Menu("File");
helpMenu= new Menu("Help");
/* [1.1] add submenus to menu bar frame */
mbar.add(fileMenu);
mbar.add(helpMenu);
/* [2] Add "File" pulldown menu */
ms.makeMenuItem(this,fileMenu, "Assign batch .doit file",
"AssignBatchDoItfile", 0);
ms.makeMenuItem(this,fileMenu, "Assign params map file",
"AssignParamsMapFile", 0);
//ms.makeMenuItem(this,fileMenu,"Set project directory","SetProjDir",0);
fileMenu.addSeparator(); /* "__________" */
ms.makeMenuItem(this,fileMenu, "Set report file name", "SetReportFile", 0);
ms.makeMenuItem(this,fileMenu, "Clear report", "ClearReport", 0);
ms.makeMenuItem(this,fileMenu, "Save report as", "SaveAsReport", 0);
fileMenu.addSeparator(); /* "__________" */
verboseMCB= ms.makeChkBoxMenuItem(this, fileMenu, "Verbose reporting",
"Verbose", 0, cvt.verboseLoggingFlag);
fileMenu.addSeparator(); /* "__________" */
ms.makeMenuItem(this, fileMenu, "Reset Converter", "resetConverter",0);
fileMenu.addSeparator(); /* "__________" */
ms.makeMenuItem(this, fileMenu, "Quit", "Close", 0);
/* [4] Add "Help" pulldown menu */
ms.makeMenuItem(this,helpMenu,"Full Reference Manual","ReferenceManual",0);
ms.makeMenuItem(this,helpMenu,"Graphical User Interface","GUIusage",0);
ms.makeMenuItem(this,helpMenu,"Search Database GUI","SearchGUIusage",0);
ms.makeMenuItem(this,helpMenu, "Summary", "Summary",0);
ms.makeMenuItem(this,helpMenu,"License", "License",0);
ms.makeMenuItem(this,helpMenu,"About", "About",0);
} /* addMenuTree */
/**
* processGuiStateData() - Process data specified by the GUI state.
* This is called from the "Process" event-handler for the button
* and menu handler.
* @return true if succeed, false if any problems after write
* err msg to window
*/
private boolean processGuiStateData()
{ /* processGuiStateData */
/* [1] Make sure not currently processing */
if(processFlag)
{
UtilCM.logMsg("\nCan't start a new process until the current "+
"one is finished or canceled.\n");
return(false);
}
/* [1.2] Make sure there is file data to process */
if(guiBatchDoItFile==null && guiParamsMapFile==null)
{
UtilCM.logMsg("\nProcessing Problem: you need to assign either:\n"+
" 1) a batch .doit file, or\n"+
" 2) a paramerter .map script file.\n"+
"Set them with the file menu and try again.\n");
return(false);
}
/* [2] Set the "Processing" State. */
this.setEnableCancelButtonsAndMenus("Processing file ...");
/* [3] Build the batch processing string. If we can't schedule this
* now, we will use it later after ensure pd thread is running.
*/
sGuiArgV= "";
String
extraSW= " -logging:CvtGUI.log";
if(guiBatchDoItFile!=null)
sGuiArgV= "-batchProcess:"+guiBatchDoItFile+extraSW;
else if(guiParamsMapFile!=null)
sGuiArgV= guiParamsMapFile+extraSW;
/* [3.1] Make it an arg list */
guiArgV= UtilCM.cvs2Array(sGuiArgV, " ");
return(true);
} /* processGuiStateData */
/**
* setEnableAssignButtonsAndMenus() - set Assign buttons & menus state.
* This sets the flags to the initial "Assign" data state.
*/
public void setEnableAssignButtonsAndMenus()
{ /* setEnableAssignButtonsAndMenus */
processButton.setEnabled(false);
cancelButton.setEnabled(false);
viewHTMLchoice.removeAll();
viewHTMLchoice.add("This pull-down menu will contain computed HTML files.");
viewHTMLchoice.setEnabled(false);
setEnableAssignState(); /* clear the GUI state */
UtilCM.updateReportStatusLine("Assign file");
printGuiFSMlf("\nAfter setEnableAssignButtonsAndMenus",printFSMflag);
UtilCM.logMsg("\nAssign a batch '.doit' or parameter '.map' script "+
"file to process.\n");
this.repaint();
} /* setEnableAssignButtonsAndMenus */
/**
* setEnableProcessingButtonsAndMenus() - when able to start processing state.
* This sets the flags to the "Process-enable" data state.
* @param statusLabel to use for Report status line.
*/
public void setEnableProcessingButtonsAndMenus(String statusLabel)
{ /* setEnableProcessingButtonsAndMenus */
processButton.setEnabled(true);
cancelButton.setEnabled(false);
viewHTMLchoice.removeAll();
viewHTMLchoice.add("This pull-down menu will contain computed HTML files.");
viewHTMLchoice.setEnabled(false);
setEnableProcessingState();
UtilCM.updateReportStatusLine(statusLabel);
printGuiFSMlf("\nAfter setEnableProcessingButtonsAndMenus",printFSMflag);
UtilCM.logMsg("\nPress the 'Process' button to process the script "+
"file to continue.\n");
this.repaint();
} /* setEnableProcessingButtonsAndMenus */
/**
* setEnableCancelButtonsAndMenus() - when are able to do processing state.
* This sets the flags to the "Process" data state. It also
* clears the list of generated HTML files.
* @param statusLabel to use for Report status line.
*/
public void setEnableCancelButtonsAndMenus(String statusLabel)
{ /* setEnableCancelButtonsAndMenus */
processButton.setEnabled(true);
cancelButton.setEnabled(true);
viewHTMLchoice.removeAll();
viewHTMLchoice.add("This pull-down menu will contain computed HTML files.");
viewHTMLchoice.setEnabled(false);
setEnableCancelState();
UtilCM.updateReportStatusLine(statusLabel);
printGuiFSMlf("\nAfter setEnableCancelButtonsAndMenus", printFSMflag);
UtilCM.logMsg("\nWait until processing is finished or press the "+
"'Cancel' button to abort the processing.\n");
this.repaint();
} /* setEnableCancelButtonsAndMenus */
/**
* setEnableViewingButtonsAndMenus() - when can view processed HTML files state.
* This sets the flags to the "Viewing-enable" data state.
*/
public void setEnableViewingButtonsAndMenus()
{ /* setEnableViewingButtonsAndMenus */
processButton.setEnabled(false);
cancelButton.setEnabled(false);
/* assign the list of HTML files */
String genHTMLpathList[]= cvt.getGenHTMLpathList();
if(genHTMLpathList!=null)
{ /* update the choice menu */
int nFiles= genHTMLpathList.length;
viewHTMLchoice.removeAll();
for(int i=0;i