nz.govt.natlib.ndha.manualdeposit.dialogs.EnterDirectory.java Source code

Java tutorial

Introduction

Here is the source code for nz.govt.natlib.ndha.manualdeposit.dialogs.EnterDirectory.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.dialogs;

import java.io.File;

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

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

public class EnterDirectory extends javax.swing.JDialog {

    /**
     * 
     */
    private static final long serialVersionUID = -6689750892955764396L;
    private final static Log LOG = LogFactory.getLog(EnterDirectory.class);
    private final String theSettingsPath;
    @SuppressWarnings("unused")
    private FormControl frmControl; // NOPMD Has to be here - doesn't work as a
    // local variable
    private final ManualDepositPresenter manualDepositPresenter;

    /** Creates new form EnterDirectory */
    public EnterDirectory(final java.awt.Frame parent, final boolean modal, final String settingsPath,
            final ManualDepositPresenter manualDeposit) {
        super(parent, modal);
        theSettingsPath = settingsPath;
        manualDepositPresenter = manualDeposit;
        initComponents();
    }

    private void checkForDirectoryLoad() {
        final File testFile = new File(txtEnterDirectory.getText());
        LOG.debug("Test file: " + txtEnterDirectory.getText() + ", Exists: " + testFile.exists() + ", Directory? "
                + testFile.isDirectory());
        LOG.debug("Absolute path: " + testFile.getAbsolutePath());
        try {
            LOG.debug("Canonical path: " + testFile.getCanonicalPath());
            if (testFile.exists() && testFile.isDirectory()) {
                manualDepositPresenter.loadPath(testFile.getCanonicalPath());
            }
        } catch (Exception ex) {
            LOG.error("Error checking for directory", ex);
        }
    }

    private void allFinished() {
        this.setVisible(false);
    }

    /**
     * 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.
     */
    // GEN-BEGIN:initComponents
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtEnterDirectory = new javax.swing.JTextField();
        cmdDone = new javax.swing.JButton();

        setTitle("Enter Directory");
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setText("Enter Directory");

        txtEnterDirectory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEnterDirectoryActionPerformed(evt);
            }
        });
        txtEnterDirectory.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtEnterDirectoryCaretUpdate(evt);
            }
        });

        cmdDone.setText("Finished");
        cmdDone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdDoneActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtEnterDirectory, javax.swing.GroupLayout.DEFAULT_SIZE, 267,
                                                Short.MAX_VALUE))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGap(151, 151, 151).addComponent(cmdDone).addGap(136, 136, 136)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1)
                        .addComponent(txtEnterDirectory, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, Short.MAX_VALUE).addComponent(cmdDone).addContainerGap()));

        pack();
    }// </editor-fold>

    // GEN-END:initComponents

    private void txtEnterDirectoryActionPerformed(final java.awt.event.ActionEvent evt) {
        allFinished();
    }

    private void txtEnterDirectoryCaretUpdate(final javax.swing.event.CaretEvent evt) {
        checkForDirectoryLoad();
    }

    private void cmdDoneActionPerformed(final java.awt.event.ActionEvent evt) {
        allFinished();
    }

    private void formWindowOpened(final java.awt.event.WindowEvent evt) {
        try {
            frmControl = new FormControl(this, theSettingsPath);
        } catch (Exception ex) {
            LOG.error("Error loading form parameters", ex);
        }
    }

    // GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton cmdDone;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField txtEnterDirectory;
    // End of variables declaration//GEN-END:variables
}