com.smart.aqimonitor.client.AqiSettingDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.smart.aqimonitor.client.AqiSettingDialog.java

Source

/*
 * Copyright 2014 dxp.alibaba-inc.com All right reserved. This software is the
 * confidential and proprietary information of dxp.alibaba-inc.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with dxp.alibaba-inc.com.
 */
package com.smart.aqimonitor.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.smart.aqimonitor.paser.impl.AbstractAqiParser;

/**
 * AqiSettingDialog.java
 * 
 * @author ShenDayong 2014-1-19 9:16:18
 */
@SuppressWarnings("serial")
public class AqiSettingDialog extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextField tfRetryTimes;
    private JTextField tfRetryGap;
    private JTextField tfExportPath;
    private JFileChooser fDialog;
    private Resource propertiesResource;
    private Properties props;
    @SuppressWarnings("unused")
    private AbstractAqiParser aqiParser;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            AqiSettingDialog dialog = new AqiSettingDialog(null, null);
            dialog.init();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void init() {
        String workDir = System.getProperty("user.dir");
        File configFile = new File(workDir + File.separator + "config.properties");
        propertiesResource = new FileSystemResource(configFile);
        try {
            props = PropertiesLoaderUtils.loadProperties(propertiesResource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (props != null) {
            tfRetryTimes.setText(props.getProperty("query.retryTimes"));
            tfRetryGap.setText(props.getProperty("query.retryGap"));
            File file = new File(props.getProperty("result.path"));
            tfExportPath.setText(file.getAbsolutePath());
            tfExportPath.setCaretPosition(0);
        }
    }

    /**
     * Create the dialog.
     */
    public AqiSettingDialog(Frame owner, final AbstractAqiParser aqiParser) {
        super(owner);
        this.aqiParser = aqiParser;
        setTitle("\u8BBE\u7F6E");
        setResizable(false);
        setBounds(100, 100, 450, 135);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        GridBagLayout gbl_contentPanel = new GridBagLayout();
        gbl_contentPanel.columnWidths = new int[] { 33, 48, 66, 41, 48, 66, 0, 0 };
        gbl_contentPanel.rowHeights = new int[] { 21, 0, 0 };
        gbl_contentPanel.columnWeights = new double[] { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
        gbl_contentPanel.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
        contentPanel.setLayout(gbl_contentPanel);
        {
            JLabel lblRetryTimes = new JLabel("\u91CD\u8BD5\u6B21\u6570\uFF1A");
            GridBagConstraints gbc_lblRetryTimes = new GridBagConstraints();
            gbc_lblRetryTimes.anchor = GridBagConstraints.WEST;
            gbc_lblRetryTimes.insets = new Insets(0, 0, 5, 5);
            gbc_lblRetryTimes.gridx = 1;
            gbc_lblRetryTimes.gridy = 0;
            contentPanel.add(lblRetryTimes, gbc_lblRetryTimes);
        }
        {
            tfRetryTimes = new JTextField();
            tfRetryTimes.setPreferredSize(new Dimension(6, 29));
            tfRetryTimes.setMinimumSize(new Dimension(60, 29));
            GridBagConstraints gbc_tfRetryTimes = new GridBagConstraints();
            gbc_tfRetryTimes.anchor = GridBagConstraints.NORTHWEST;
            gbc_tfRetryTimes.insets = new Insets(0, 0, 5, 5);
            gbc_tfRetryTimes.gridx = 2;
            gbc_tfRetryTimes.gridy = 0;
            contentPanel.add(tfRetryTimes, gbc_tfRetryTimes);
            tfRetryTimes.setColumns(10);
        }
        {
            JLabel lblRetryGap = new JLabel("\u91CD\u8BD5\u95F4\u9694\uFF1A");
            GridBagConstraints gbc_lblRetryGap = new GridBagConstraints();
            gbc_lblRetryGap.anchor = GridBagConstraints.WEST;
            gbc_lblRetryGap.insets = new Insets(0, 0, 5, 5);
            gbc_lblRetryGap.gridx = 4;
            gbc_lblRetryGap.gridy = 0;
            contentPanel.add(lblRetryGap, gbc_lblRetryGap);
        }
        {
            tfRetryGap = new JTextField();
            tfRetryGap.setMinimumSize(new Dimension(60, 29));
            tfRetryGap.setPreferredSize(new Dimension(60, 29));
            GridBagConstraints gbc_tfRetryGap = new GridBagConstraints();
            gbc_tfRetryGap.insets = new Insets(0, 0, 5, 5);
            gbc_tfRetryGap.anchor = GridBagConstraints.NORTHWEST;
            gbc_tfRetryGap.gridx = 5;
            gbc_tfRetryGap.gridy = 0;
            contentPanel.add(tfRetryGap, gbc_tfRetryGap);
            tfRetryGap.setColumns(10);
        }
        {
            JLabel label = new JLabel("\u5206\u949F");
            GridBagConstraints gbc_label = new GridBagConstraints();
            gbc_label.insets = new Insets(0, 0, 5, 0);
            gbc_label.gridx = 6;
            gbc_label.gridy = 0;
            contentPanel.add(label, gbc_label);
        }
        {
            JLabel lblExportPath = new JLabel("\u8F93\u51FA\u8DEF\u5F84\uFF1A");
            GridBagConstraints gbc_lblExportPath = new GridBagConstraints();
            gbc_lblExportPath.anchor = GridBagConstraints.EAST;
            gbc_lblExportPath.insets = new Insets(0, 0, 0, 5);
            gbc_lblExportPath.gridx = 1;
            gbc_lblExportPath.gridy = 1;
            contentPanel.add(lblExportPath, gbc_lblExportPath);
        }
        {
            tfExportPath = new JTextField();
            tfExportPath.setEditable(false);
            tfExportPath.setPreferredSize(new Dimension(180, 29));
            tfExportPath.setMinimumSize(new Dimension(180, 29));
            GridBagConstraints gbc_tfExportPath = new GridBagConstraints();
            gbc_tfExportPath.gridwidth = 4;
            gbc_tfExportPath.insets = new Insets(0, 0, 0, 5);
            gbc_tfExportPath.fill = GridBagConstraints.HORIZONTAL;
            gbc_tfExportPath.gridx = 2;
            gbc_tfExportPath.gridy = 1;
            contentPanel.add(tfExportPath, gbc_tfExportPath);
            tfExportPath.setColumns(10);
        }
        {
            JButton button = new JButton("...");
            button.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    fDialog = new JFileChooser(); // 
                    fDialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                    int result = fDialog.showOpenDialog(contentPanel);
                    if (result == JFileChooser.APPROVE_OPTION) {
                        tfExportPath.setText(fDialog.getSelectedFile().getAbsolutePath());
                    }
                }
            });
            GridBagConstraints gbc_button = new GridBagConstraints();
            gbc_button.gridx = 6;
            gbc_button.gridy = 1;
            contentPanel.add(button, gbc_button);
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("\u786E\u5B9A");
                okButton.setName("settingOk");
                okButton.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {

                        String retryTimes = tfRetryTimes.getText();
                        if (StringUtils.isEmpty(retryTimes) || !isNumeric(retryTimes)) {
                            JOptionPane.showMessageDialog(contentPanel, "", "",
                                    JOptionPane.ERROR_MESSAGE);
                            return;
                        }

                        String retryGap = tfRetryGap.getText();
                        if (StringUtils.isEmpty(retryGap) || !isNumeric(retryGap)) {
                            JOptionPane.showMessageDialog(contentPanel, "", "",
                                    JOptionPane.ERROR_MESSAGE);
                            return;
                        }

                        String exportPath = tfExportPath.getText();
                        if (StringUtils.isEmpty(exportPath)) {
                            JOptionPane.showMessageDialog(contentPanel, "", "",
                                    JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                        File testFile = new File(exportPath);
                        if (!testFile.exists()) {
                            JOptionPane.showMessageDialog(contentPanel, "", "",
                                    JOptionPane.ERROR_MESSAGE);
                            return;
                        }

                        props.put("query.retryTimes", retryTimes);
                        props.put("query.retryGap", retryGap);
                        props.put("result.path", exportPath);

                        try {
                            props.store(new FileOutputStream(propertiesResource.getFile()), "");
                            aqiParser.setRetryTimes(Integer.parseInt(retryTimes));
                            aqiParser.setRetryGap(Integer.parseInt(retryGap));
                            aqiParser.setFilePath(exportPath);
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        } finally {
                            dispose();
                        }
                    }
                });
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton cancelButton = new JButton("\u53D6\u6D88");
                cancelButton.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        dispose();
                    }
                });
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }
}