package com.ytec.jdap.panel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.ytec.jdap.common.Constants;
import com.ytec.jdap.common.FileUtil;
import com.ytec.jdap.common.StringUtil;
import com.ytec.jdap.common.TimeUtil;
import com.ytec.jdap.config.ConfigMamager;
import com.ytec.jdap.model.Application;
import com.ytec.jdap.panel.util.AppTable;
/**
*
* <pre>
* Title: Ӧ�÷�������
* Description:Ӧ�÷�������
* </pre>
*
* @author CaiJiuFa
* @version 1.00.00
*
* <pre>
* �ļ�¼
* �ĺ�汾: ���ˣ� ������: ������:
* </pre>
*/
public class AppPublishFrame extends BaseFrame {
private static final long serialVersionUID = -9216782544864438618L;
private Application appliaction;
private String appId = null;
private String jarName = "";
public AppTable table;
JLabel name = new JLabel("Ӧ������(����)��", JLabel.RIGHT);
JLabel path = new JLabel("JAR�ļ�(����)��", JLabel.RIGHT);
JLabel order = new JLabel("����˳��", JLabel.RIGHT);
JLabel jvm = new JLabel("JVM������", JLabel.RIGHT);
JLabel args = new JLabel("����������", JLabel.RIGHT);
JLabel javaHome = new JLabel("JRE·����", JLabel.RIGHT);
JTextField nameField = new JTextField(20);
JTextField jarField = new JTextField(20);
JTextField orderField = new JTextField(20);
JTextField jvmField = new JTextField(20);
JTextField argsField = new JTextField(20);
JTextField javaHomeField = new JTextField(20);
JButton save = new JButton("ȷ��");
JButton cancel = new JButton("ȡ��");
JButton view = new JButton("���");
JButton javaHomeView = new JButton("���");
public AppPublishFrame(String appId, final AppTable table) {
this.table = table;
this.appId = appId;
this.initFrame();
final JFrame thiz = this;
view.setPreferredSize(new Dimension(28, 21));
view.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == view) {
JFileChooser dialog = new JFileChooser();
if (StringUtil.isNotBlank(appliaction.getPath()))
dialog.setCurrentDirectory(new File(appliaction.getPath()));
int result = dialog.showOpenDialog(thiz);
if (result == JFileChooser.APPROVE_OPTION) {
File file = dialog.getSelectedFile();
jarName = file.getName();
jarField.setText(file.getPath());
appliaction.setPath(file.getParentFile().getPath());
}
}
}
});
javaHomeView.setPreferredSize(new Dimension(28, 21));
javaHomeView.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == javaHomeView) {
JFileChooser javaDialog = new JFileChooser();
javaDialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (StringUtil.isNotBlank(javaHomeField.getText()))
javaDialog.setCurrentDirectory(new File(javaHomeField.getText()));
int result = javaDialog.showOpenDialog(thiz);
if (result == JFileChooser.APPROVE_OPTION) {
File file = javaDialog.getSelectedFile();
javaHomeField.setText(file.getPath());
}
}
}
});
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (checker()) {
saveApplication();
table.repaintTable();
thiz.dispose();
}
}
});
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
thiz.dispose();
}
});
}
private void initFrame() {
getAppliaction();
orderField.setText("1");
this.setTitle(Constants.APP_PUBLIC_TITLE);
this.setIconImage(new ImageIcon(Constants.IMAGE_TITLE).getImage());
JPanel formPanel = new JPanel();
GridBagLayout gridBagLayout = new GridBagLayout();
formPanel.setLayout(gridBagLayout);
formPanel.setBorder(BorderFactory.createTitledBorder("������Ϣ"));
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0; // 0��0��
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.weighty = 0;
formPanel.add(name, c);
c.gridx++;
c.weightx = 1;
formPanel.add(nameField, c);
c.gridx = 0; // 1��0��
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.weighty = 0;
formPanel.add(path, c);
c.gridx++;
c.weightx = 1;
formPanel.add(jarField, c);
c.gridx++;
c.weightx = 2;
formPanel.add(view, c);
c.gridx = 0; // 2��0��
c.gridy = 2;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.weighty = 0;
formPanel.add(order, c);
c.gridx++;
c.weightx = 1;
formPanel.add(orderField, c);
c.gridx = 0; // 3��0��
c.gridy = 3;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.weighty = 0;
formPanel.add(jvm, c);
c.gridx++;
c.weightx = 1;
formPanel.add(jvmField, c);
c.gridx = 0; // 4��0��
c.gridy = 4;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.weighty = 0;
formPanel.add(args, c);
c.gridx++;
c.weightx = 1;
formPanel.add(argsField, c);
c.gridx = 0; // 5��0��
c.gridy = 5;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.weighty = 0;
formPanel.add(javaHome, c);
c.gridx++;
c.weightx = 1;
formPanel.add(javaHomeField, c);
c.gridx++;
c.weightx = 2;
formPanel.add(javaHomeView, c);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(save);
buttonPanel.add(cancel);
buttonPanel.setSize(400, 100);
getContentPane().add(formPanel, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
if (appliaction.getId() != null) {
jarName = appliaction.getJar();
nameField.setText(appliaction.getName());
nameField.setEditable(false);
jarField.setText(appliaction.getPath() + File.separator + appliaction.getJar());
orderField.setText(appliaction.getOrder());
javaHomeField.setText(appliaction.getJavaHome());
}
if (appliaction.getParams() != null) {
jvmField.setText(appliaction.getParams().get(Constants.APP_PARAM_JVM));
argsField.setText(appliaction.getParams().get(Constants.APP_PARAM_ARGS));
}
}
public Application getAppliaction() {
try {
if (appId == null) {
appliaction = new Application();
} else {
appliaction = ConfigMamager.getReader().getAppById(appId);
}
} catch (Exception e) {
BaseFrame.alert(e.getMessage());
}
return appliaction;
}
/**
* ����Ӧ��
*/
public void saveApplication() {
try {
Map<String, String> params = new HashMap<String, String>();
params.put(Constants.APP_PARAM_JVM, jvmField.getText());
params.put(Constants.APP_PARAM_ARGS, argsField.getText());
appliaction.setJar(jarName);
appliaction.setName(nameField.getText());
appliaction.setOrder(orderField.getText());
appliaction.setJavaHome(javaHomeField.getText());
appliaction.setParams(params);
if (StringUtil.isBlank(appliaction.getState()))
appliaction.setState(Application.STOPED);
if (appId == null) {
appliaction.setId(System.currentTimeMillis() + "");
appliaction.setDeployDate(TimeUtil.getCurDateTime());
ConfigMamager.getReader().addApp(appliaction);
} else {
ConfigMamager.getReader().modifyApp(appliaction);
}
} catch (Exception e) {
BaseFrame.alert(e.getMessage());
}
}
/**
* �Ϸ��Լ��
*
* @return
*/
public boolean checker() {
String path = jarField.getText();
if (StringUtil.isBlank(nameField.getText())) {
alert("����дӦ�����ƣ� ");
return false;
}
if (appId == null) {
String appName = nameField.getText();
try {
Application app = ConfigMamager.getReader().getAppByName(appName);
if (app.getId() != null) {
alert("Ӧ�������Ѿ����ڣ� ");
return false;
}
} catch (Exception e) {
BaseFrame.alert(e.getMessage());
return false;
}
}
if (StringUtil.isBlank(path)) {
alert("��ѡ��JAR����");
return false;
} else {
int index = path.lastIndexOf(".");
String type = path.substring(index + 1, path.length());
if (!type.equalsIgnoreCase(Constants.FILE_TYPE)) {
alert("�ļ�����������ѡ��jar�ļ���");
return false;
}
if (!FileUtil.isExit(path)) {
alert("JAR�ļ�·������");
return false;
}
}
if (StringUtil.isNotBlank(javaHomeField.getText()) && !FileUtil.isExit(javaHomeField.getText())) {
alert("JRE·������");
return false;
}
return true;
}
}