nz.govt.natlib.ndha.manualdeposit.CMSSearchResults.java Source code

Java tutorial

Introduction

Here is the source code for nz.govt.natlib.ndha.manualdeposit.CMSSearchResults.java

Source

/**
 * Software License
 *
 * Copyright 2007/2010 National Library of New Zealand.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *
 * or the file "LICENSE.txt" included with the software.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 */

package nz.govt.natlib.ndha.manualdeposit;

import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.table.TableColumn;

import nz.govt.natlib.ndha.common.guiutilities.FormControl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CMSSearchResults extends javax.swing.JDialog implements ICMSSearchResults {

    private static final long serialVersionUID = 330994571070332061L;
    private final static Log LOG = LogFactory.getLog(CMSSearchResults.class);
    private final String theSettingsPath;
    private final JPanel glass;
    private CMSSearchResultsPresenter thePresenter;
    private FormControl theFormControl;
    private final static String SPLIT_NAME = "splitMainDivider";

    /** Creates new form CMS2SearchResults */
    public CMSSearchResults(final java.awt.Frame parent, final boolean modal, final String settingsPath) {
        super(parent, modal);
        theSettingsPath = settingsPath;
        initComponents(); // NOPMD
        glass = (JPanel) this.getGlassPane();
    }

    public void showView() {
        thePresenter.addResultsTableModelAndHandlers(tblResults);
        setVisible(true);
    }

    public void setPresenter(final CMSSearchResultsPresenter presenter) {
        thePresenter = presenter;
    }

    public void showDetail(final String detail) {
        txtDetail.setText(detail);
    }

    public void closeForm() {
        this.setVisible(false);
    }

    public void setFormFont(final Font theFont) {
        FormUtilities.setFormFont(this, theFont);
    }

    public void showError(final String header, final String message) {
        JOptionPane.showMessageDialog(this, message, header, JOptionPane.ERROR_MESSAGE);
    }

    public void showMessage(final String header, final String message) {
        JOptionPane.showMessageDialog(this, message, header, JOptionPane.INFORMATION_MESSAGE);
    }

    public void setStatus(final boolean canSelect, final boolean canPageForward, final boolean canPageBackwards,
            final String pagingMessage) {
        btnSelect.setEnabled(canSelect);
        btnNextPage.setEnabled(canPageForward);
        btnPreviousPage.setEnabled(canPageBackwards);
        lblpagingMessage.setText(pagingMessage);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("PMD")

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        splitMain = new javax.swing.JSplitPane();
        scrlResults = new javax.swing.JScrollPane();
        tblResults = new javax.swing.JTable();
        scrlDetail = new javax.swing.JScrollPane();
        txtDetail = new javax.swing.JTextArea();
        pnlButtons = new javax.swing.JPanel();
        btnSelect = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnPreviousPage = new javax.swing.JButton();
        btnNextPage = new javax.swing.JButton();
        lblpagingMessage = new javax.swing.JLabel();

        setTitle("CMS Search Results");
        setModal(true);
        setName("CMS2SearchResults");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }

            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        splitMain.setDividerLocation(500);

        tblResults
                .setModel(new javax.swing.table.DefaultTableModel(
                        new Object[][] { { null, null, null, null }, { null, null, null, null },
                                { null, null, null, null }, { null, null, null, null } },
                        new String[] { "Title 1", "Title 2", "Title 3", "Title 4" }));
        scrlResults.setViewportView(tblResults);

        splitMain.setLeftComponent(scrlResults);

        txtDetail.setColumns(20);
        txtDetail.setEditable(false);
        txtDetail.setLineWrap(true);
        txtDetail.setRows(5);
        scrlDetail.setViewportView(txtDetail);

        splitMain.setRightComponent(scrlDetail);

        btnSelect.setText("Select Record");
        btnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectActionPerformed(evt);
            }
        });

        btnCancel.setOpaque(false);
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnPreviousPage.setText("Previous Page");
        btnPreviousPage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPreviousPageActionPerformed(evt);
            }
        });

        btnNextPage.setText("Next Page");
        btnNextPage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextPageActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlButtonsLayout = new javax.swing.GroupLayout(pnlButtons);
        pnlButtons.setLayout(pnlButtonsLayout);
        pnlButtonsLayout.setHorizontalGroup(pnlButtonsLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlButtonsLayout.createSequentialGroup().addContainerGap().addGroup(pnlButtonsLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnlButtonsLayout.createSequentialGroup().addComponent(btnPreviousPage)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnNextPage).addGap(582, 582, 582)
                                .addComponent(btnSelect, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(21, 21, 21).addComponent(btnCancel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addComponent(lblpagingMessage)).addContainerGap()));
        pnlButtonsLayout.setVerticalGroup(pnlButtonsLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlButtonsLayout.createSequentialGroup()
                        .addComponent(lblpagingMessage)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(btnNextPage).addComponent(btnPreviousPage)
                                .addComponent(btnSelect, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnCancel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap()));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(pnlButtons, javax.swing.GroupLayout.Alignment.TRAILING,
                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(splitMain, javax.swing.GroupLayout.Alignment.TRAILING,
                        javax.swing.GroupLayout.DEFAULT_SIZE, 985, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(splitMain, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlButtons, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNextPageActionPerformed(java.awt.event.ActionEvent evt) { // NOPMD
        thePresenter.pageForward();
    }

    private void btnPreviousPageActionPerformed(java.awt.event.ActionEvent evt) { // NOPMD
        thePresenter.pageBackwards();
    }

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) { // NOPMD
        closeForm();
    }

    private void btnSelectActionPerformed(java.awt.event.ActionEvent evt) { // NOPMD
        thePresenter.selectRecord();
    }

    private void formWindowClosing(java.awt.event.WindowEvent evt) { // NOPMD
        theFormControl.setExtra(SPLIT_NAME, splitMain.getDividerLocation());
        for (int i = 0; i < tblResults.getColumnModel().getColumnCount(); i++) {
            final TableColumn col = tblResults.getColumnModel().getColumn(i);
            final String colName = "Col" + i;
            theFormControl.setExtra(colName, col.getWidth());
        }
    }

    public void setWaitCursor(boolean isWaiting) { // NOPMD
        glass.setVisible(isWaiting);
        if (isWaiting) {
            final Cursor hourglass = new Cursor(Cursor.WAIT_CURSOR);
            setCursor(hourglass);
        } else {
            final Cursor normal = new Cursor(Cursor.DEFAULT_CURSOR);
            setCursor(normal);
        }
    }

    @SuppressWarnings("serial")
    private void formWindowOpened(java.awt.event.WindowEvent evt) { // NOPMD
        try {
            theFormControl = new FormControl(this, theSettingsPath);
        } catch (Exception ex) {
            LOG.error("Error loading form parameters", ex);
        }
        if (theFormControl != null) {
            final Action updateDividersAction = new AbstractAction() {
                public void actionPerformed(final ActionEvent e) {
                    Timer t = (Timer) e.getSource();
                    t.stop();
                    splitMain.setDividerLocation(theFormControl.getExtra(SPLIT_NAME, 320));
                    splitMain.repaint();
                    for (int i = 0; i < tblResults.getColumnModel().getColumnCount(); i++) {
                        TableColumn col = tblResults.getColumnModel().getColumn(i);
                        String colName = "Col" + i;
                        int width = theFormControl.getExtra(colName, 150);
                        col.setPreferredWidth(width);
                        col.setWidth(width);
                    }
                }
            };
            new Timer(200, updateDividersAction).start();
        }
        thePresenter.showResults();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnNextPage;
    private javax.swing.JButton btnPreviousPage;
    private javax.swing.JButton btnSelect;
    private javax.swing.JLabel lblpagingMessage;
    private javax.swing.JPanel pnlButtons;
    private javax.swing.JScrollPane scrlDetail;
    private javax.swing.JScrollPane scrlResults;
    private javax.swing.JSplitPane splitMain;
    private javax.swing.JTable tblResults;
    private javax.swing.JTextArea txtDetail;
    // End of variables declaration//GEN-END:variables

}