packagegenerateXML;importjava.awt.Dimension;importjava.awt.Font;importjava.awt.Toolkit;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.text.ParseException;importjava.util.Arrays;importjava.util.Date;importjava.util.HashMap;importjava.util.List;importjava.util.Locale;importjava.util.Map;importjavax.swing.ImageIcon;importjavax.swing.JButton;importjavax.swing.JComboBox;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JPanel;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax.swing.JTextField;importorg.apache.log4j.Logger;importcom.eltima.components.ui.DatePicker;importcn.sanxing.cim.payload.MeterConfig;public class BuildXMLFrame extends JFrame implementsActionListener{private static final long serialVersionUID = 1L;private static Logger logger = Logger.getLogger(BuildXMLFrame.class.getName());
JFrame mainFrame= new JFrame("Build XML Tool");
JPanel panel;//类型
JLabel typeLabel;
JComboBoxtypeComBox;
String[] typeString= { "MeterReadings", "EndDeviceEvents", "EndDeviceControls"};//场景
JLabel scenariosLabel;
JComboBoxscenariosComBox;
String[] scenariosString= { "Billing Data", "Meter Reading", "Instantaneous Data", "LoadProfile1", "LoadProfile2"};
String[] meterNos= {"Billing Data", "LoadProfile1", "LoadProfile2", "Events"};//只需校验多表的scenarios
String[] meterNo = {"Meter Reading", "Instantaneous Data", "Connect/Disconnect"};//只需校验单表+抄读项的scenarios
List scenariosList =Arrays.asList(meterNos);
List scenariosList1 =Arrays.asList(meterNo);//表号
JLabel meterNoLable;
JTextField meterNoTextField;
JButton addMeterNoButton;
ImageIcon addMeterNoIcon= new ImageIcon(BuildXMLFrame.class.getClassLoader().getResource("image/add.jpg"));//抄读项
JLabel readingTypeLabel;
JTextField readingTypeTextField;
JButton addReadTypeButton;
ImageIcon addReadTypeIcon= new ImageIcon(BuildXMLFrame.class.getClassLoader().getResource("image/add.jpg"));//时间
JLabel startTimeLabel;
JLabel endTimeLabel;
DatePicker startTimeDatePick;//时间控件
DatePicker endTimeDatePick;//生成XML按钮
JButton generateButton;//显示XML
JTextArea xmlTextArea;
JScrollPane xmlScroll;
MyUtil util= newMyUtil();
MeterConfig meterConfig= newMeterConfig();public voidshowFrame(){
initPanel();//初始化Panel
initFrame(); //初始化Frame
}//初始化窗体
public voidinitFrame(){
mainFrame.setSize(770, 700); //窗体大小
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗体点击关闭按钮,即终止程序
mainFrame.setResizable(false); //取消窗体的固定大小设置
Toolkit kit = Toolkit.getDefaultToolkit(); //工具包
Dimension screenSize = kit.getScreenSize(); //屏幕的尺寸
int screenWidth = screenSize.width / 2; //屏幕的宽
int screenHeight = screenSize.height / 2; //屏幕的高
int height = mainFrame.getHeight(); //窗口高度
int width = mainFrame.getWidth(); //窗口宽度
mainFrame.setLocation(screenWidth - width / 2, screenHeight - height / 2); //将窗口设置到屏幕的中部
mainFrame.add(panel); //panel加入到窗体中
mainFrame.setVisible(true); //窗体设置可见
}//初始化面板
public voidinitPanel(){
panel= newJPanel();
panel.setLayout(null);//类型Label
typeLabel = new JLabel("Type :");
typeLabel.setBounds(50, 20, 100, 25);this.panel.add(typeLabel);//类型下拉框
typeComBox = new JComboBox(typeString);
typeComBox.setBounds(90, 20, 150, 25);this.panel.add(typeComBox);//场景Label
scenariosLabel = new JLabel("Scenarios :");
scenariosLabel.setBounds(270, 20, 100, 25);this.panel.add(scenariosLabel);//场景下拉框
scenariosComBox = new JComboBox(scenariosString);
scenariosComBox.setBounds(340, 20, 150, 25);this.panel.add(scenariosComBox);//表号Label
meterNoLable = new JLabel("Meter No :");
meterNoLable.setBounds(530, 20, 100, 25);this.panel.add(meterNoLable);//表号文本框
meterNoTextField = newJTextField();
meterNoTextField.setBounds(595, 20, 150, 25);this.panel.add(meterNoTextField);//添加表号按钮
addMeterNoButton = newJButton(addMeterNoIcon);
addMeterNoButton.setBounds(125, 0, 24, 24);
meterNoTextField.add(addMeterNoButton);//开始时间Label
startTimeLabel = new JLabel("Start Time : ");
startTimeLabel.setBounds(20, 55, 100, 25);this.panel.add(startTimeLabel);//开始时间控件
startTimeDatePick = getDatePicker("startTime");this.panel.add(startTimeDatePick);//结束时间Label
endTimeLabel = new JLabel("End Time : ");
endTimeLabel.setBounds(275, 55, 100, 25);this.panel.add(endTimeLabel);//结束时间控件
endTimeDatePick = getDatePicker("endTime");this.panel.add(endTimeDatePick);//抄读项Label
readingTypeLabel = new JLabel("ReadingType :");
readingTypeLabel.setBounds(510, 55, 100, 25);this.panel.add(readingTypeLabel);//抄读项文本框
readingTypeTextField = newJTextField();
readingTypeTextField.setBounds(595, 55, 150, 25);this.panel.add(readingTypeTextField);//添加抄读项按钮
addReadTypeButton = newJButton(addReadTypeIcon);
addReadTypeButton.setBounds(125, 0, 24, 24);
readingTypeTextField.add(addReadTypeButton);//生成XML按钮
generateButton = new JButton("Generate");
generateButton.setBounds(645, 95, 100, 25);this.panel.add(generateButton);//xml文本框
xmlTextArea = newJTextArea();
xmlScroll= new JScrollPane(xmlTextArea); //xmlTextArea添加滚动条
xmlScroll.setBounds(15, 130, 735, 530);this.panel.add(xmlScroll);//按钮加入监听
generateButton.addActionListener(this);
typeComBox.addActionListener(this);
addMeterNoButton.addActionListener(this);
addMeterNoButton.addMouseListener(newMouseAdapter() {
@Overridepublic voidmouseEntered(MouseEvent e) {
addMeterNoButton.setToolTipText("Add meterNo."); //添加表号按钮的提示
}
});
addReadTypeButton.addActionListener(this);
addReadTypeButton.addMouseListener(newMouseAdapter() {
@Overridepublic voidmouseEntered(MouseEvent e) {
addReadTypeButton.setToolTipText("Add readingType"); //添加抄读项按钮的提示
}
});
}//时间控件
publicDatePicker getDatePicker(String flag) {
DatePicker datepick;
String DefaultFormat= "yyyy-MM-dd HH:mm:ss"; //格式
Date date = new Date(); //当前时间
Font font = new Font("Times New Roman", Font.BOLD, 14); //字体
Dimension dimension = new Dimension(150, 24); //控件宽、高//int[] hilightDays = { 1, 3, 5, 7 };//int[] disabledDays = { 4, 6, 5, 9 };//构造方法(初始时间,时间显示格式,字体,控件大小)
datepick = newDatePicker(date, DefaultFormat, font, dimension);if("startTime".equals(flag)){
datepick.setLocation(90, 55);
}if("endTime".equals(flag)){
datepick.setLocation(340, 55);
}//datepick.setBounds(137, 15, 177, 24);//setBounds()直接设置大小与位置//datepick.setHightlightdays(hilightDays, Color.red);//设置一个月份中需要高亮显示的日子//datepick.setDisableddays(disabledDays);//设置一个月份中不需要的日子,呈灰色显示
datepick.setLocale(Locale.US); //设置国家
datepick.setTimePanleVisible(true); //设置时、分、秒面板可见
returndatepick;
}//click触发按钮事件
@Overridepublic voidactionPerformed(ActionEvent e) {//type级联scenarios
if (e.getSource() ==typeComBox) {
cascade();
}//添加表号
if (e.getSource() ==addMeterNoButton) {
addMeterNo();
}//添加抄读项
if (e.getSource() ==addReadTypeButton) {
addReadType();
}//生成XML
if (e.getSource() ==generateButton) {if (verifyData()) { //数据校验
showXML(); //生成XML
}
}
}//级联
public voidcascade() {
String typeSelected=util.getString(typeComBox.getSelectedItem());if ("MeterReadings".equals(typeSelected)) {
scenariosComBox.removeAllItems();for(String scenarios : scenariosString) {
scenariosComBox.addItem(scenarios);
}
}if ("EndDeviceEvents".equals(typeSelected)) {
scenariosComBox.removeAllItems();
scenariosComBox.addItem("Events");
}if ("EndDeviceControls".equals(typeSelected)) {
scenariosComBox.removeAllItems();
scenariosComBox.addItem("Connect/Disconnect");
}
}//添加表号
public voidaddMeterNo() {
String showInput= JOptionPane.showInputDialog(this.panel, "Add meter No.", "Tips", 3);if (!util.isEmptyString(showInput)) {if (!showInput.matches("[0-9]+")) {
JOptionPane.showMessageDialog(this.panel, "The input must be a number.", "Tips", 2);//表号错误
return;
}if(util.isEmptyString(meterNoTextField.getText())) {
meterNoTextField.setText(showInput);
}else{
meterNoTextField.setText(meterNoTextField.getText()+ "," +showInput);
}
}
}//添加抄读项
public voidaddReadType() {
String showInput= JOptionPane.showInputDialog(this.panel, "Add readingType", "Tips", 3);if (!util.isEmptyString(showInput)) {//抄读项校验
if (!showInput.matches("[0-9A-Z]+")) {
JOptionPane.showMessageDialog(this.panel, "The input must be a number or letter.", "Tips", 2);//抄读项错误
return;
}//赋值
if(util.isEmptyString(readingTypeTextField.getText())) {
readingTypeTextField.setText(showInput);
}else{
readingTypeTextField.setText(readingTypeTextField.getText()+ "," +showInput);
}
}
}//数据校验
public booleanverifyData(){
String typeSelected=util.getString(typeComBox.getSelectedItem());
String scenariosSelected=util.getString(scenariosComBox.getSelectedItem());
String meterNoText=util.getString(meterNoTextField.getText());
String readTypeText=util.getString(readingTypeTextField.getText());
Map map = new HashMap();
map.put("type", typeSelected);
map.put("scenarios", scenariosSelected);
map.put("meterNo", meterNoText);
map.put("readType", readTypeText);//MeterNo校验
if(meterNoisEmpty(meterNoText)){return false;
}//type
switch(typeSelected) {case "MeterReadings":returnMeterReadings(map);case "EndDeviceEvents":returnEndDeviceEvents(map);case "EndDeviceControls":returnEndDeviceControls(map);default:return true;
}
}public boolean MeterReadings(Mapmap){
String scenarios= map.get("scenarios");if(scenariosList.contains(scenarios)) {//多表表号,需以逗号隔开
return meterNosFormat(map.get("meterNo"));
}if(scenariosList1.contains(scenarios)) {//单表表号校验
if(!meterNoFormat(map.get("meterNo"))){return false;
}//readType校验
return readTypeisEmpty(map.get("readType"));
}return true;
}public boolean EndDeviceEvents(Mapmap) {
String scenarios= map.get("scenarios");if(scenariosList.contains(scenarios)) {//多表表号,需以逗号隔开
return meterNosFormat(map.get("meterNo"));
}return true;
}public boolean EndDeviceControls(Mapmap) {
String scenarios= map.get("scenarios");if(scenariosList1.contains(scenarios)) {//单表表号校验
if (!meterNoFormat(map.get("meterNo"))) {return false;
}//readType校验
return readTypeisEmpty(map.get("readType"));
}return true;
}//表号为空
public booleanmeterNoisEmpty(String meterNo){if(util.isEmptyString(meterNo)){
JOptionPane.showMessageDialog(this.panel, "MeterNo is empty!", "Tips", 2);//MeterNo为空,warning
return true;
}return false;
}//单表表号格式校验
public booleanmeterNoFormat(String meterNo){if (!meterNo.matches("^\\d+$")) {
JOptionPane.showMessageDialog(this.panel, "The format of the meterNo number must be a number!", "Tips", 2);//表号必须数字格式,warning
return false;
}return true;
}//多表表号格式校验
public booleanmeterNosFormat(String meterNo){if (!meterNo.matches("^\\d+($|,\\d+$)")) {
JOptionPane.showMessageDialog(this.panel, "Multiple meterNos must be comma separated!", "Tips", 2);//多表表号必须是数字格式且逗号分隔,warning
return false;
}return true;
}//readType为空
public booleanreadTypeisEmpty(String readType){if(util.isEmptyString(readType)) {
JOptionPane.showMessageDialog(this.panel, "ReadingType is empty!", "Tips", 2);//ReadingType为空,warning
return false;
}return true;
}//生成XML
public voidshowXML(){
Map map = new HashMap();
String type=util.getString(typeComBox.getSelectedItem());
String scenarios=util.getString(scenariosComBox.getSelectedItem());
String startTime= null;
String endTime= null;
String meterNo=util.getString(meterNoTextField.getText());
String readingType=util.getString(readingTypeTextField.getText());try{
startTime=util.sdfCim.format(util.sdfJava.parse(startTimeDatePick.getText().trim()));
endTime=util.sdfCim.format(util.sdfJava.parse(endTimeDatePick.getText().trim()));
}catch(ParseException e) {
logger.error(e);
}
map.put("type", type);
map.put("scenarios", scenarios);
map.put("meterNo", meterNo);
map.put("startTime", startTime);
map.put("endTime", endTime);
map.put("readType", readingType);
String xml=util.createRequest(map);
xmlTextArea.setText(xml);
}public static voidmain(String[] args) {
Locale.setDefault(Locale.ENGLISH);//设置控件为英文
BuildXMLFrame xmlFrame = newBuildXMLFrame();
xmlFrame.showFrame();
}
}