com.bsoft.baseframe.baseframe_utils.beanUtils.Classgenerator.java Source code

Java tutorial

Introduction

Here is the source code for com.bsoft.baseframe.baseframe_utils.beanUtils.Classgenerator.java

Source

/*
 * @(#)Classgenerator.java        1.0 2013-4-7
 *
 * Copyright (c) 2007-2013 Shanghai BSOFT IT, Co., Ltd.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of 
 * Shanghai BSFOT IT Co., Ltd. ("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 BSOFT.
 */

package com.bsoft.baseframe.baseframe_utils.beanUtils;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;

/**
 * ?
 *
 * @version    1.0 2013-4-7
 * @author      caie
 * @history   
 *      
 */
public class Classgenerator extends JFrame {

    private static final long serialVersionUID = 8101820900910920111L;
    /***/
    private static final int WIDTH = 400;
    /***/
    private static final int HEIGHT = 300;
    /**??*/
    private static String msg = "";
    /**??*/
    private JTextField _tb = null;
    /**??*/
    private JTextField _stb = null;
    /**ibatis??*/
    private JTextField _ibatis = null;
    /**service??*/
    private JTextField _sv = null;
    //   private JTextField _zwmc=null;   //???

    /**
     * ???
     */
    public Classgenerator() {
        setTitle("?");
        setSize(WIDTH, HEIGHT);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2, 1));

        JPanel _beanPanel = new JPanel();
        _beanPanel.setBorder(BorderFactory.createTitledBorder("?"));

        JPanel _otherPanel = new JPanel();
        _otherPanel.setBorder(BorderFactory.createTitledBorder("?"));

        JLabel _tbLabel = new JLabel("??*");
        _tb = new JTextField(25);
        _beanPanel.add(_tbLabel);
        _beanPanel.add(_tb);

        JLabel _stbbLabel = new JLabel("??*");
        _stb = new JTextField(25);
        _beanPanel.add(_stbbLabel);
        _beanPanel.add(_stb);

        JLabel _ibatisLabel = new JLabel("IBTS*");
        _ibatis = new JTextField(25);
        _beanPanel.add(_ibatisLabel);
        _beanPanel.add(_ibatis);

        JLabel _svLabel = new JLabel("SERVICE");
        _sv = new JTextField(23);
        _otherPanel.add(_svLabel);
        _otherPanel.add(_sv);

        //      JLabel _sviLabel = new JLabel("??*");
        //      _zwmc = new JTextField("?",25);
        //      _zwmc.setEditable(false);
        //      _otherPanel.add(_sviLabel);
        //      _otherPanel.add(_zwmc);

        JLabel _detail = new JLabel("(?ServiceService??,");
        JLabel _detail2 = new JLabel("??Service???.impl)");
        _detail.setFont(new Font("", Font.ITALIC, 12));
        _detail2.setFont(new Font("", Font.ITALIC, 12));
        _otherPanel.add(_detail);
        _otherPanel.add(_detail2);
        panel.add(_beanPanel);
        panel.add(_otherPanel);

        add(panel, BorderLayout.CENTER);

        JPanel btnPanel = new JPanel();
        JButton _saveBtn = new JButton("?");

        _saveBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int msg_code = clickSaveBtn(e);
                showMsg(msg_code);
            }
        });

        JButton _restBtn = new JButton("?");

        //?
        _restBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                _tb.setText(null);
                _stb.setText(null);
                _ibatis.setText(null);
                _sv.setText(null);
                //            _svi.setText(null);
            }
        });
        btnPanel.add(_saveBtn);
        btnPanel.add(_restBtn);
        add(btnPanel, BorderLayout.SOUTH);
    }

    /**
     * ??
     * @return ??
     */
    private boolean validateText() {
        String tbName = _tb.getText().trim(), stPath = _stb.getText().trim(), ibtsPath = _ibatis.getText().trim();
        if (StringUtils.isBlank(tbName)) {
            msg = "???";
            _tb.requestFocus();
            return false;
        }

        if (StringUtils.isBlank(stPath)) {
            msg = "?";
            _stb.requestFocus();
            return false;
        }

        if (StringUtils.isBlank(ibtsPath)) {
            msg = "ibatis?";
            _ibatis.requestFocus();
            return false;
        }

        return true;
    }

    /**
     * ?
     * @param e 
     * @return int
     */
    private int clickSaveBtn(ActionEvent e) {

        int msg_code = 0;
        //?
        if (!validateText()) {
            JOptionPane.showMessageDialog(this, msg, "", JOptionPane.WARNING_MESSAGE);
            return msg_code;
        }

        String tbName = _tb.getText().trim(), stPath = _stb.getText().trim(), ibtsPath = _ibatis.getText().trim(),
                svcPath = _sv.getText().trim();

        //?bean
        String filePath = FileUtils.getFilePathName(tbName, stPath);
        CreateBeanFile createFile = new CreateBeanFile(stPath, tbName);
        String fileCode = createFile.createBeanCode();
        if (StringUtils.isBlank(fileCode)) {
            msg_code = 1;
            return msg_code;
        }
        if (!FileUtils.writeData(fileCode, filePath)) {
            msg_code = 2;
            return msg_code;
        }

        //?XML
        String xmlPath = FileUtils.getXMLFilePath("src/main/resources", ibtsPath);
        CreateXMLFile xmlFile = new CreateXMLFile(tbName, stPath, xmlPath);
        if (!xmlFile.createXMLFile()) {
            msg_code = 3;
            return msg_code;
        }

        //?Service&ServiceImpl
        if (StringUtils.isNotBlank(svcPath)) {
            CreateServiceFile csf = new CreateServiceFile(svcPath, tbName);
            if (!csf.createService()) {
                msg_code = 4;
                return msg_code;
            }
            if (!csf.createServiceImpl()) {
                msg_code = 5;
                return msg_code;
            }
        }
        return msg_code;
    }

    /**
     * ?
     * @param msg_code int
     */
    private void showMsg(int msg_code) {
        String message = "";
        boolean flag = false;
        switch (msg_code) {
        case 0:
            flag = true;
            message = "???";
            break;
        case 1:
            message = "?????";
            break;
        case 2:
            message = "??";
            break;
        case 3:
            message = "XML??";
            break;
        case 4:
            message = "????";
            break;
        case 5:
            message = "???";
            break;
        }
        JOptionPane.showMessageDialog(this, message, "", JOptionPane.WARNING_MESSAGE);
        if (flag) {
            int closable = JOptionPane.showConfirmDialog(null,
                    "???\n??", "??", JOptionPane.YES_NO_OPTION,
                    JOptionPane.INFORMATION_MESSAGE);
            if (JOptionPane.YES_OPTION == closable) {
                System.exit(0);
            }
            return;
        }
    }

    /**
     * ??
     * @param args
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new Classgenerator();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                frame.setResizable(false);
            }
        });
    }

    public static void init() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new Classgenerator();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                frame.setResizable(false);
            }
        });
    }
}