com.apatar.ui.JFeatureRequestHelpDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.apatar.ui.JFeatureRequestHelpDialog.java

Source

/*
_______________________
Apatar Open Source Data Integration
Copyright (C) 2005-2007, Apatar, Inc.
info@apatar.com
195 Meadow St., 2nd Floor
Chicopee, MA 01013
    
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 2 of the License, or
### (at your option) any later version.
    
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.# See the
### GNU General Public License for more details.
    
### You should have received a copy of the GNU General Public License along
### with this program; if not, write to the Free Software Foundation, Inc.,
### 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
________________________
    
*/

package com.apatar.ui;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;

import com.apatar.core.ApplicationData;
import com.apatar.core.CoreUtils;

@SuppressWarnings("serial")
public class JFeatureRequestHelpDialog extends JDialog {

    JTextArea text = new JTextArea();
    JButton sendButton = new JButton("Send");
    JButton cancel = new JButton("Cancel");

    JTextField firstNameField = new JTextField();
    JTextField lastNameField = new JTextField();
    JTextField emailField = new JTextField();

    public JFeatureRequestHelpDialog() throws HeadlessException {
        super();
        setTitle(ApplicationData.VERSION + " - Feature Request");
        createDialog();
        addListeners();
    }

    public JFeatureRequestHelpDialog(Dialog arg0, boolean arg1) throws HeadlessException {
        super(arg0, arg1);
        setTitle(ApplicationData.VERSION + " - Feature Request");
        createDialog();
        addListeners();
    }

    public JFeatureRequestHelpDialog(Dialog arg0, String arg1, boolean arg2) throws HeadlessException {
        super(arg0, arg1, arg2);
        setTitle(ApplicationData.VERSION + " - Feature Request");
        createDialog();
        addListeners();
    }

    public JFeatureRequestHelpDialog(Dialog arg0, String arg1) throws HeadlessException {
        super(arg0, arg1);
        setTitle(ApplicationData.VERSION + " - Feature Request");
        createDialog();
        addListeners();
    }

    public JFeatureRequestHelpDialog(Dialog arg0) throws HeadlessException {
        super(arg0);
        setTitle(ApplicationData.VERSION + " - Feature Request");
        createDialog();
        addListeners();
    }

    public JFeatureRequestHelpDialog(Frame arg0, boolean arg1) throws HeadlessException {
        super(arg0, arg1);
        setTitle(ApplicationData.VERSION + " - Feature Request");
        createDialog();
        addListeners();
    }

    public JFeatureRequestHelpDialog(Frame arg0, String arg1, boolean arg2) throws HeadlessException {
        super(arg0, arg1, arg2);
        setTitle(ApplicationData.VERSION + " - Feature Request");
        createDialog();
        addListeners();
    }

    public JFeatureRequestHelpDialog(Frame arg0, String arg1) throws HeadlessException {
        super(arg0, arg1);
        setTitle(ApplicationData.VERSION + " - Feature Request");
        createDialog();
        addListeners();
    }

    public JFeatureRequestHelpDialog(Frame arg0) throws HeadlessException {
        super(arg0);
        setTitle(ApplicationData.VERSION + " - Feature Request");
        createDialog();
        addListeners();
    }

    private void createDialog() {
        setLayout(new BorderLayout(5, 5));

        setSize(400, 400);

        JPanel textPanel = new JPanel(new BorderLayout(5, 5));
        textPanel.setBorder(new EmptyBorder(10, 5, 5, 5));
        text.setLineWrap(true);
        text.setWrapStyleWord(true);
        text.setFont(UiUtils.NORMAL_SIZE_12_FONT);

        textPanel.add(new JLabel("What new Apatar features would you like to request? Please describe them:"),
                BorderLayout.NORTH);
        textPanel.add(new JScrollPane(text), BorderLayout.CENTER);

        JPanel contactPanel = new JPanel();
        contactPanel.setLayout(new BoxLayout(contactPanel, BoxLayout.Y_AXIS));

        JPanel firstNamePanel = new JPanel();
        firstNamePanel.setLayout(new BoxLayout(firstNamePanel, BoxLayout.X_AXIS));
        firstNamePanel.add(new JLabel("Your First Name:"));
        firstNamePanel.add(Box.createHorizontalStrut(5));
        firstNamePanel.add(firstNameField);

        JPanel lastNamePanel = new JPanel();
        lastNamePanel.setLayout(new BoxLayout(lastNamePanel, BoxLayout.X_AXIS));
        lastNamePanel.add(new JLabel("Your Last Name:"));
        lastNamePanel.add(Box.createHorizontalStrut(5));
        lastNamePanel.add(lastNameField);

        JPanel emailPanel = new JPanel();
        emailPanel.setLayout(new BoxLayout(emailPanel, BoxLayout.X_AXIS));
        emailPanel.add(new JLabel("Your E-mail:"));
        emailPanel.add(Box.createHorizontalStrut(28));
        emailPanel.add(emailField);

        contactPanel.add(firstNamePanel);
        contactPanel.add(Box.createVerticalStrut(5));
        contactPanel.add(lastNamePanel);
        contactPanel.add(Box.createVerticalStrut(5));
        contactPanel.add(emailPanel);

        textPanel.add(contactPanel, BorderLayout.SOUTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
        buttonPanel.add(Box.createHorizontalGlue());
        buttonPanel.add(sendButton);
        buttonPanel.add(Box.createHorizontalStrut(5));
        buttonPanel.add(cancel);
        buttonPanel.add(Box.createHorizontalStrut(5));
        buttonPanel.setBorder(new EmptyBorder(0, 0, 5, 0));

        getContentPane().add(textPanel, BorderLayout.CENTER);
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    }

    private void addListeners() {
        sendButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                PostMethod filePost = new PostMethod(getUrl());

                // filePost.getParameters().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
                try {

                    if (!CoreUtils.validEmail(emailField.getText())) {
                        JOptionPane.showMessageDialog(ApatarUiMain.MAIN_FRAME,
                                "E-mail address is invalid! Please, write a valid e-mail.");
                        return;
                    }

                    Part[] parts;
                    parts = new Part[4];

                    parts[0] = new StringPart("FeatureRequest", text.getText());
                    parts[1] = new StringPart("FirstName", firstNameField.getText());
                    parts[2] = new StringPart("LastName", lastNameField.getText());
                    parts[3] = new StringPart("Email", emailField.getText());

                    filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
                    HttpClient client = new HttpClient();
                    client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
                    int status = client.executeMethod(filePost);

                    if (status != HttpStatus.SC_OK)
                        JOptionPane.showMessageDialog(ApatarUiMain.MAIN_FRAME,
                                "Upload failed, response=" + HttpStatus.getStatusText(status));
                    else {
                        JOptionPane.showMessageDialog(ApatarUiMain.MAIN_FRAME,
                                "Your message has been sent. Thank you!");
                        dispose();
                    }

                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    filePost.releaseConnection();
                }

            }

        });
        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                dispose();
            }
        });
    }

    protected String getSubject() {
        return "FeatureRequest";
    }

    protected String getUrl() {
        return "http://apatar.com/featurerequest";

    }

}