Пример создания своего интерфейса в клиенте
Материал из BiTel WiKi
Пример создания своего интерфейса в клиенте
Содержание |
Введение
Рассажу как сделать свой интерфейс в клиенте биллинга на примере создания заготовки для обмена с некой сторонней абстрактной системой.
Задача стоит так. В клиенте должны быть: менюшка для выбора месаца, типа обмена, строка для указания почтового ящика (для уведомления о результате обмена) и кнопочки "Запуск". Выгрузка и загрузка данных должны быть разделены. При нажатии на кнопку "Запуск" вызывается экшен на сервере.
Редактируем data/menu.xml
в раздел "Договор" добавляем:
<menuItem id="myModule" className="ru.lda.billing.client.myModule.Action" title="Мой Модуль"/>
После этого в меню клиента появится новый пункт "Мой Модуль".
Создание библиотеки для клиента
Срабатывает при выборе пункта меню "Мой Модуль".
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ru.lda.billing.client.myModule;
import bitel.billing.ShellFrame;
import bitel.billing.module.common.BillingAction;
import javax.swing.JFrame;
/**
*
* @author lda
*/
public class Action implements BillingAction{
public void doAction(JFrame frame, String key){
ShellFrame.getFrame().getTabbedPane().addTab(new Manager());
}
}
Создает окошко и вставляет в него 2 таба.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ru.lda.billing.client.myModule;
import bitel.billing.ShellFrame;
import bitel.billing.module.common.BGTabPanel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JTabbedPane;
/**
*
* @author lda
*/
public class Manager extends BGTabPanel{
private JTabbedPane tabbed = new JTabbedPane();
private Export ex;
private Import imp;
public Manager()
{
super("myModule", "Мой модуль");
this.parentFrame = ShellFrame.getFrame();
this.module = "myModule.manager";
this.ex = new Export();
this.imp = new Import();
try{
jbInit();
}catch (Exception ex){
ex.printStackTrace();
}
}
private void jbInit() throws Exception{
this.tabbed.addTab("Вызрузка данных", this.ex);
this.tabbed.addTab("Загрузка данных", this.imp);
setLayout(new GridBagLayout());
add(this.tabbed, new GridBagConstraints(0, 0, 1, 1, 1.0D, 1.0D, 10, 1, new Insets(0, 0, 0, 0), 0, 0));
}
}
Таб для выгрузки данных. При нажатии на кнопку "запуск" вызывается экшен на сервере bitel.billing.server.myModule.action.ActionExport.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ru.lda.billing.client.myModule;
import bitel.billing.module.admin.TransferManager;
import bitel.billing.module.common.BGComboBox;
import bitel.billing.module.common.BGControlPanelMonth;
import bitel.billing.module.common.BGTitleBorder;
import bitel.billing.module.common.ComboBoxItem;
import bitel.billing.module.common.Request;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.w3c.dom.Document;
import ru.bitel.bgbilling.client.common.BGUPanel;
import ru.bitel.bgbilling.client.util.ClientUtils;
import ru.bitel.common.Utils;
/**
*
* @author lda
*/
public class Export extends BGUPanel{
private BGControlPanelMonth period_P = new BGControlPanelMonth();
private JTextField protoEmail_TF = new JTextField();
private String module = "myModule.Export";
private BGComboBox TypeCombo = new BGComboBox();
public Export() {
}
protected void jbInit(){
JPanel jPanel1 = new JPanel();
BGTitleBorder bGTitleBorder1 = new BGTitleBorder();
BGTitleBorder bGTitleBorder4 = new BGTitleBorder();
BGTitleBorder bGTitleBorder5 = new BGTitleBorder();
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement(new ComboBoxItem("1", "Подготовить реестр документов"));
model.addElement(new ComboBoxItem("2", "Выгрузить реестр документов"));
model.addElement(new ComboBoxItem("3", "Очистить реестр документов"));
TypeCombo.setModel(model);
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JLabel jLabel1 = new JLabel();
JButton jButton1 = new JButton();
Component component1 = Box.createHorizontalStrut(8);
Component component2 = Box.createGlue();
setLayout(new GridBagLayout());
bGTitleBorder1.setTitle(" Месяц ");
jPanel1.setLayout(new GridBagLayout());
bGTitleBorder4.setTitleName(" Пуск ");
bGTitleBorder5.setTitleName(" Тип ");
jPanel2.setBorder(bGTitleBorder5);
jPanel2.setLayout(new GridBagLayout());
jPanel2.add(TypeCombo, new GridBagConstraints(0, 0, 1, 1, 1.0D, 0.0D, 10, 2, new Insets(0, 5, 5, 5), 0, 0));
jPanel3.setBorder(bGTitleBorder4);
jPanel3.setLayout(new GridBagLayout());
jLabel1.setText("Выслать протокол на:");
this.protoEmail_TF.setMaximumSize(new Dimension(2147483647, 2147483647));
this.protoEmail_TF.setMinimumSize(new Dimension(4, 24));
this.protoEmail_TF.setPreferredSize(new Dimension(200, 24));
this.protoEmail_TF.setToolTipText("");
this.protoEmail_TF.setText("");
jButton1.setText("Запуск");
jButton1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
saveUserEmail(module, protoEmail_TF.getText().trim());
sendRequest();
}
});
jPanel1.setBorder(null);
this.period_P.setBorder(bGTitleBorder1);
this.period_P.setDebugGraphicsOptions(0);
jPanel3.add(jLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0D, 0.0D, 17, 0, new Insets(0, 5, 5, 0), 0, 0));
jPanel3.add(jButton1, new GridBagConstraints(3, 0, 1, 1, 0.0D, 0.0D, 10, 0, new Insets(0, 0, 5, 5), 0, 0));
jPanel3.add(component1, new GridBagConstraints(2, 0, 1, 1, 1.0D, 0.0D, 10, 2, new Insets(0, 0, 0, 0), 0, 0));
add(component2, new GridBagConstraints(1, 1, 1, 1, 1.0D, 1.0D, 10, 1, new Insets(0, 0, 0, 0), 0, 0));
jPanel3.add(this.protoEmail_TF, new GridBagConstraints(1, 0, 1, 1, 0.0D, 0.0D, 10, 0, new Insets(0, 6, 5, 0), 0, 0));
jPanel1.add(this.period_P, new GridBagConstraints(0, 0, 1, 1, 1.0D, 1.0D, 10, 1, new Insets(0, 0, 0, 0), 0, 0));
add(jPanel3, new GridBagConstraints(2, 0, 1, 1, 1.0D, 0.0D, 11, 2, new Insets(0, 0, 0, 0), 0, 0));
add(jPanel2, new GridBagConstraints(1, 0, 1, 1, 1.0D, 0.0D, 11, 2, new Insets(0, 0, 0, 0), 0, 0));
add(jPanel1, new GridBagConstraints(0, 0, 1, 1, 0.0D, 0.0D, 11, 1, new Insets(0, 0, 0, 0), 0, 0));
setData();
}
public void setData(){
this.protoEmail_TF.setText(getUserEmail(this.module));
}
private void sendRequest(){
Request request = new Request();
request.setModule("myModule");
request.setAction("Export");
String email = protoEmail_TF.getText();
request.setAttribute("mail", email);
request.setAttribute("type", ClientUtils.getIdFromComboBox(TypeCombo));
request.setAttribute("date", ((BGControlPanelMonth)period_P).getDateString());
if (Utils.isBlankString(email)){
JOptionPane.showMessageDialog(this, "Укажите почтовый ящик!", "Ошибка", 0);
return;
}
Document doc = TransferManager.getDocument(request);
if (ClientUtils.checkStatus(this, doc)){
JOptionPane.showMessageDialog(this, "Задача запущена, результат будет \nотправлен на указанный EMail", "Задача запущена", 1);
}
}
}
Таб для загрузки данных. При нажатии на кнопку "запуск" вызывается экшен на сервере bitel.billing.server.myModule.action.ActionImport.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ru.lda.billing.client.myModule;
import bitel.billing.module.admin.TransferManager;
import bitel.billing.module.common.BGComboBox;
import bitel.billing.module.common.BGControlPanelMonth;
import bitel.billing.module.common.BGTitleBorder;
import bitel.billing.module.common.ComboBoxItem;
import bitel.billing.module.common.Request;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.w3c.dom.Document;
import ru.bitel.bgbilling.client.common.BGUPanel;
import ru.bitel.bgbilling.client.util.ClientUtils;
import ru.bitel.common.Utils;
/**
*
* @author lda
*/
public class Import extends BGUPanel{
private BGControlPanelMonth period_P = new BGControlPanelMonth();
private JTextField protoEmail_TF = new JTextField();
private String module = "myModule.Import";
private BGComboBox TypeCombo = new BGComboBox();
public Import() {
}
protected void jbInit(){
JPanel jPanel1 = new JPanel();
BGTitleBorder bGTitleBorder1 = new BGTitleBorder();
BGTitleBorder bGTitleBorder4 = new BGTitleBorder();
BGTitleBorder bGTitleBorder5 = new BGTitleBorder();
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement(new ComboBoxItem("1", "Загрузить данных"));
TypeCombo.setModel(model);
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JLabel jLabel1 = new JLabel();
JButton jButton1 = new JButton();
Component component1 = Box.createHorizontalStrut(8);
Component component2 = Box.createGlue();
setLayout(new GridBagLayout());
bGTitleBorder1.setTitle(" Месяц ");
jPanel1.setLayout(new GridBagLayout());
bGTitleBorder4.setTitleName(" Пуск ");
bGTitleBorder5.setTitleName(" Тип ");
jPanel2.setBorder(bGTitleBorder5);
jPanel2.setLayout(new GridBagLayout());
jPanel2.add(TypeCombo, new GridBagConstraints(0, 0, 1, 1, 1.0D, 0.0D, 10, 2, new Insets(0, 5, 5, 5), 0, 0));
jPanel3.setBorder(bGTitleBorder4);
jPanel3.setLayout(new GridBagLayout());
jLabel1.setText("Выслать протокол на:");
this.protoEmail_TF.setMaximumSize(new Dimension(2147483647, 2147483647));
this.protoEmail_TF.setMinimumSize(new Dimension(4, 24));
this.protoEmail_TF.setPreferredSize(new Dimension(200, 24));
this.protoEmail_TF.setToolTipText("");
this.protoEmail_TF.setText("");
jButton1.setText("Запуск");
jButton1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
saveUserEmail(module, protoEmail_TF.getText().trim());
sendRequest();
}
});
jPanel1.setBorder(null);
this.period_P.setBorder(bGTitleBorder1);
this.period_P.setDebugGraphicsOptions(0);
jPanel3.add(jLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0D, 0.0D, 17, 0, new Insets(0, 5, 5, 0), 0, 0));
jPanel3.add(jButton1, new GridBagConstraints(3, 0, 1, 1, 0.0D, 0.0D, 10, 0, new Insets(0, 0, 5, 5), 0, 0));
jPanel3.add(component1, new GridBagConstraints(2, 0, 1, 1, 1.0D, 0.0D, 10, 2, new Insets(0, 0, 0, 0), 0, 0));
add(component2, new GridBagConstraints(1, 1, 1, 1, 1.0D, 1.0D, 10, 1, new Insets(0, 0, 0, 0), 0, 0));
jPanel3.add(this.protoEmail_TF, new GridBagConstraints(1, 0, 1, 1, 0.0D, 0.0D, 10, 0, new Insets(0, 6, 5, 0), 0, 0));
jPanel1.add(this.period_P, new GridBagConstraints(0, 0, 1, 1, 1.0D, 1.0D, 10, 1, new Insets(0, 0, 0, 0), 0, 0));
add(jPanel3, new GridBagConstraints(2, 0, 1, 1, 1.0D, 0.0D, 11, 2, new Insets(0, 0, 0, 0), 0, 0));
add(jPanel2, new GridBagConstraints(1, 0, 1, 1, 1.0D, 0.0D, 11, 2, new Insets(0, 0, 0, 0), 0, 0));
add(jPanel1, new GridBagConstraints(0, 0, 1, 1, 0.0D, 0.0D, 11, 1, new Insets(0, 0, 0, 0), 0, 0));
setData();
}
public void setData(){
this.protoEmail_TF.setText(getUserEmail(this.module));
}
private void sendRequest(){
Request request = new Request();
request.setModule("myModule");
request.setAction("Import");
String email = protoEmail_TF.getText();
request.setAttribute("mail", email);
request.setAttribute("type", ClientUtils.getIdFromComboBox(TypeCombo));
request.setAttribute("date", ((BGControlPanelMonth)period_P).getDateString());
if (Utils.isBlankString(email)){
JOptionPane.showMessageDialog(this, "Укажите почтовый ящик!", "Ошибка", 0);
return;
}
Document doc = TransferManager.getDocument(request);
if (ClientUtils.checkStatus(this, doc)){
JOptionPane.showMessageDialog(this, "Задача запущена, результат будет \nотправлен на указанный EMail", "Задача запущена", 1);
}
}
}
Все это надо скомпилить и положить в папку lib клиента.
Тля тех кому лень компилить: Файл:My clien lib.jar.zip
Создание библиотеки для сервера
Экшен для выгрузки.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bitel.billing.server.myModule.action;
import bitel.billing.common.TimeUtils;
import bitel.billing.server.admin.action.base.ActionBase;
import bitel.billing.server.util.MailMsg;
import java.sql.SQLException;
import java.util.Date;
import ru.bitel.bgbilling.common.BGException;
/**
*
* @author lda
*/
public class ActionExport extends ActionBase{
@Override
public void doAction()
throws SQLException, BGException
{
int type = getIntParameter( "type", -1 );
String mail = getParameter("mail");
Date date = getDateParameter("date", new Date());
StringBuilder message = new StringBuilder( 300 );
message.append( "type: "+type+"\n" );
message.append( "date: "+TimeUtils.formatDate(date)+"\n" );
new MailMsg( setup ).sendMessageEx( mail, "Export", message.toString(), "text/plain" );
}
}
Экшен для загрузки.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bitel.billing.server.myModule.action;
import bitel.billing.common.TimeUtils;
import bitel.billing.server.admin.action.base.ActionBase;
import bitel.billing.server.util.MailMsg;
import java.sql.SQLException;
import java.util.Date;
import ru.bitel.bgbilling.common.BGException;
/**
*
* @author lda
*/
public class ActionImport extends ActionBase{
@Override
public void doAction()
throws SQLException, BGException
{
int type = getIntParameter( "type", -1 );
String mail = getParameter("mail");
Date date = getDateParameter("date", new Date());
StringBuilder message = new StringBuilder( 300 );
message.append( "type: "+type+"\n" );
message.append( "date: "+TimeUtils.formatDate(date)+"\n" );
new MailMsg( setup ).sendMessageEx( mail, "Import", message.toString(), "text/plain" );
}
}
Думаю если поместить эти экшены в дин. код тоже заработает.
Но мне больше нравится все запихать в библиотеку и подкинуть ее серверу Файл:My server lib.jar.zip
Результат
В результате получаем:
Если указать почтовый ящик и нажать на кнопку "Запуск", то на указанный почтовый ящик придет тестовое письмо.
Смотрим и радуемся!!!
Механизм обновления библиотеки клиента
Единственная проблема с которой я столкнулся так это как автоматически обновлять библиотеку клиента.
Разработчики обещали этот механизм реализовать.
А пока его нет, я придумал небольшой костыль:
На сервере в webroot создал папку custom_lib, в нее кинул мою библиотеку my_clien_lib.jar.
Скачал виндовый wget wget.exe
Положил его в папку с клиентом.
В bgbilling_w2k.bat, добавил строчку (после @echo off):
wget.exe -N http://X.X.X.X:8080/bgbilling/custom_lib/my_clien_lib.jar -P .\lib

