Standalone Frame

The standalone frame displays the user interface pane in a window. It is also the mediator be-tween the pane and the query interpreter. The initialization steps of the main scenario are directly done in the constructor of the standalone frame. The standalone frame has instance fields that point to the knowledge base and the pane. During the construction of the standalone frame these two fields will be initialized. The constructor can be made private, since it will be only called by us. The corresponding code reads as follows.

    /**
     * <p>Setup the knowledgebase and init the pane.</p>
     */
    private Standalone() {
/* init the pane */
pane.initPane(getRootPane(), this);
setTitle("Deployment Study - Standalone");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

/* load the Prolog */
pane.startJob(() -> {
initKnowledgebase();
}, new Runnable() {
public void run() {
}
});
    }

Note the setting of the default close operation to dispose, which will remove the window from the screen and not simply hide it. The initialization is not finished by the constructor only. The standalone frame will also have a static main method. This method can be called from the outside and it will make visible one single standalone frame. Although the main method will return, the Java virtual machine will wait till all windows have been disposed. Details of the main method code can be found in the appendix.

The two buttons are handled by the same action listener. In both cases we will first retrieve the search criteria values and hand them over to the query interpreter. We will then let the query interpreter build the query term. In case of the search button we will not show the query term, but directly proceed to executing the query and then displaying the result. Whether the search button was pressed or not is detected by the action command of the action event. The corresponding code reads as follows:

    /**
     * <p>Handle the search and the debug button.</p>
     *
     * @param e The action event.
     */
    public void actionPerformed(ActionEvent e) {
        try {
Interpreter inter = know.iterable();
final Query query = new Query(inter);
/* retrieve the search criteria and build the query */
query.setFirstname(pane.getFirstName());
query.setName(pane.getName());
query.setAgeFrom(pane.getAgeFrom());
query.setAgeTo(pane.getAgeTo());
query.setSalaryFrom(pane.getSalaryFrom());
query.setSalaryTo(pane.getSalaryTo());
final String[] colids = query.listColumnIdentifiers();
final TermVar[] vars = query.makeVars();
final AbstractTerm queryTerm = query.makeQuery(vars);
if ("search".equals(e.getActionCommand())) {
/* execute the query and populate the results */
pane.disableButtons();
pane.startJob(() -> {
query.listRows(vars, queryTerm);
}, () -> {
Object[][] rows = query.getRows();
pane.setResult(colids, rows);
});

The action listener also contains code to handle the debug button and exceptions. The debug button should show the built query. An exception can happen when the end-user enters a wrongly formatted value as a number search criterion. We use a Swing option pane to display the built query or to display an eventual exception. The details of the corresponding code can be found in the appendix.

Kommentare