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

Java tutorial

Introduction

Here is the source code for nz.govt.natlib.ndha.manualdeposit.dialogs.ContentExists.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.awt.Desktop;
import java.io.IOException;
import java.net.URI;

import javax.swing.JOptionPane;

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

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

public class ContentExists extends javax.swing.JDialog {

    /**
     * 
     */
    private static final long serialVersionUID = -5946242296660519220L;
    private final static Log LOG = LogFactory.getLog(ContentExists.class);
    private final String theSettingsPath;
    private boolean makeDeposit = false;
    private final String theContentAggregatorURL;
    private final String cmsSystem;
    private final String theCmsID;
    @SuppressWarnings("unused")
    private FormControl frmControl; //NOPMD Won't work as a local variable

    /** Creates new form ContentExists */
    public ContentExists(final java.awt.Frame parent, final boolean modal, final String settingsPath,
            final String contentAggregatorURL, final String system, final String cmsID) {
        super(parent, modal);
        theSettingsPath = settingsPath;
        theContentAggregatorURL = contentAggregatorURL;
        cmsSystem = system;
        theCmsID = cmsID;
        initComponents();
    }

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

    public boolean isMakeDeposit() {
        return makeDeposit;
    }

    /** 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();
        btnViewContent = new javax.swing.JButton();
        btnContinueDeposit = new javax.swing.JButton();
        btnCancelDeposit = new javax.swing.JButton();

        setTitle("CMS ID Exists In DPS");
        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("The specified CMS ID already exists in the DPS");

        btnViewContent.setText("View DPS Content");
        btnViewContent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewContentActionPerformed(evt);
            }
        });

        btnContinueDeposit.setText("Deposit Anyway");
        btnContinueDeposit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnContinueDepositActionPerformed(evt);
            }
        });

        btnCancelDeposit.setText("Cancel Deposit");
        btnCancelDeposit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelDepositActionPerformed(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(btnContinueDeposit)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23,
                                        Short.MAX_VALUE)
                                .addComponent(btnCancelDeposit))
                        .addGroup(layout.createSequentialGroup().addGap(18, 18, 18).addComponent(jLabel1))
                        .addGroup(layout.createSequentialGroup().addGap(70, 70, 70).addComponent(btnViewContent)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1).addGap(30, 30, 30)
                        .addComponent(btnViewContent).addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(btnContinueDeposit).addComponent(btnCancelDeposit))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

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

    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);
        }
    }

    private void btnCancelDepositActionPerformed(final java.awt.event.ActionEvent evt) {
        makeDeposit = false;
        allFinished();
    }

    private void btnContinueDepositActionPerformed(final java.awt.event.ActionEvent evt) {
        makeDeposit = true;
        allFinished();
    }

    private void btnViewContentActionPerformed(final java.awt.event.ActionEvent evt) {
        final Desktop desktop = Desktop.getDesktop();
        try {
            final StringBuilder url = new StringBuilder();
            url.append(theContentAggregatorURL);
            if (!theContentAggregatorURL.endsWith("?")) {
                url.append("?");
            }
            url.append("system=");
            url.append(cmsSystem);
            url.append("&id=");
            url.append(theCmsID);
            desktop.browse(new URI(url.toString()));
        } catch (IOException ex) {
            //This error can occur if the default browser isn't set up properly.
            //It mostly occurs when the URL has been successfully opened, but it
            // can also happen when the URL has NOT been opened.
            // This makes it hard to determine whether it is actually an error or not
            // As the more common occurrence is a successful outcome we don't
            // want to pop up a message box, so just log the error instead.
            LOG.error("Error loading form parameters", ex);
        } catch (Exception ex) {
            final String header = "Error occurred";
            final String message = "An error occurred when viewing content\n" + ex.getMessage();
            JOptionPane.showMessageDialog(this, message, header, JOptionPane.ERROR_MESSAGE);
        }
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton btnCancelDeposit;
    private javax.swing.JButton btnContinueDeposit;
    private javax.swing.JButton btnViewContent;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

}