Swing学习之学生表简单功能实现

//前台窗体页面

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.bsw.jgz.demo;

import com.bsw.client.component.ToolbarDataSource;
import com.bsw.client.table.JTableListColumn;
import com.bsw.client.table.JTableListModel;
import com.bsw.client.table.JTableListModelAdapter;
import com.bsw.client.table.constant.ButtonColumnExtProperty;
import com.bsw.client.table.constant.TableCellEditerType;
import com.bsw.client.table.constant.TableCellStyleType;
import com.bsw.client.table.editor.TableCellActionListener;
import com.bsw.client.table.editor.TableCellEditorParameter;
import com.bsw.client.table.render.TableCellStyleListener;
import com.bsw.client.table.render.TableCellStyleParameter;
import com.bsw.client.windows.base.JInternalFrameBase;
import com.bsw.core.base.Request;
import com.bsw.core.client.common.CoreClientUtils;
import com.bsw.core.client.common.DgImportExcel;
import com.bsw.core.client.common.FileChooserUtils;
import com.bsw.core.common.ValidateMessage;
import com.bsw.esp.client.common.CommonModel;
import com.bsw.esp.constant.DataStatus;
import com.bsw.esp.constant.StudentSexType;
import com.bsw.jgz.demo.entity.Student;
import com.bsw.jgz.demo.entity.vo.StudentVo;
import com.bsw.jgz.demo.logic.StuParamLogic;
import java.awt.Event;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import net.sf.jasperreports.engine.JRException;
import org.apache.commons.io.FileUtils;

/**
*
* @author mpz
*/
public class FnEmptyTransportBuild extends JInternalFrameBase {

private JTableListModel tableModel;
private final StuParamLogic stuLogic = CoreClientUtils.getBean(StuParamLogic.class);
private Student student;
private final Request request = CoreClientUtils.getRequest();
private String impExpFlag;
private int dataStatus = DataStatus.BROWSE;
private JLabel label;

/**
* 窗口绑定实体名
*/
protected Class entityClass = Student.class;

/**
* Creates new form FnEmptyTransportBuild
*/
public FnEmptyTransportBuild() {
initComponents();
this.queryData();
}

private final ToolbarDataSource toolbarDataSource = new ToolbarDataSource() {
@Override
public JTableListModel initTable(List dataSource) {
return FnEmptyTransportBuild.this.initTable(dataSource);
}

@Override
public List getDataSource(int index, int length) {
return findPageData(index, length);
}

@Override
public Long getCountNumber() {
return findPageCount();
}
};

private List findPageData(int index, int length) {
String name = null;
Integer age = null;
Date begindate = this.stuBeginDate.getDate();
Date enddate = this.stuEndDate.getDate();
if (!tfName.getText().trim().equals("")) {
name = tfName.getText().trim();
}
if (!tnfAge.getText().trim().equals("")) {
age = Integer.parseInt(this.tnfAge.getText().trim());
}
return stuLogic.findStudentBaseEntity(request, entityClass.getSimpleName(), name, age, begindate, enddate, true, index, length);
}

protected Long findPageCount() {
String name = null;
Integer age = null;
Date begindate = this.stuBeginDate.getDate();
Date enddate = this.stuEndDate.getDate();
if (!tfName.getText().trim().equals("")) {
name = tfName.getText().trim();
}
if (!tnfAge.getText().trim().equals("")) {
age = Integer.parseInt(this.tnfAge.getText().trim());
}
return stuLogic.getCountStudentBaseEntity(request, entityClass.getSimpleName(), name, age, begindate, enddate, true);
}

private void loadData() {
String name = this.tfName.getText();
// Integer age = (Integer) this.tnfAge.getValue();
// List list = stuLogic.getStudents(request, name, age);
List list = stuLogic.getStudentss(request, name);

}

private JTableListModel initTable(List list) {
tableModel = new JTableListModel(jTable1, list, new JTableListModelAdapter() {
@Override
public List<JTableListColumn> initColumns() {
List<JTableListColumn> list = new Vector();
list.add(new JTableListColumn("", "", 20) {
@Override
public void setParameters() {
this.setEditerType(TableCellEditerType.MULTI_CHECK);
}
});
list.add(new JTableListColumn("姓名", "stuName", 100));
list.add(new JTableListColumn("性别", "stuSex", 100));
list.add(new JTableListColumn("年龄", "stuage", 120));
list.add(new JTableListColumn("地址", "stuaddress", 120));
list.add(new JTableListColumn("图片", "图片", 80) {
@Override
protected void setParameters() {
setEditerType(TableCellEditerType.BUTTON);
setExtendsProperty(ButtonColumnExtProperty.CONSTANT_TEXT, "上传图片");
setExtendsProperty(ButtonColumnExtProperty.IS_SHOW_UNDERLINE, true);
setExtendsProperty(ButtonColumnExtProperty.IS_SHOW_CONSTANT, true);
}
});
// list.add(new JTableListColumn("浏览", "查看图片", 80) {
// @Override
// protected void setParameters() {
// setEditerType(TableCellEditerType.BUTTON);
// setExtendsProperty(ButtonColumnExtProperty.CONSTANT_TEXT, "查看图片");
// setExtendsProperty(ButtonColumnExtProperty.IS_SHOW_UNDERLINE, true);
// setExtendsProperty(ButtonColumnExtProperty.IS_SHOW_CONSTANT, true);
// }
// });
return list;
}

@Override
protected String getColumnPersistentId() {
return "353gefd-9e08-4524-aa8d-e14ae8028e03";
}

@Override
protected Class getModelEntityClass() {
return Student.class;
}
}, ListSelectionModel.SINGLE_SELECTION);

//单元格渲染
tableModel.setTableCellStyleListener(new String[]{"stuSex"}, new TableCellStyleListener() {
@Override
public Map<Integer, Object> callBack(TableCellStyleParameter parm) {
Map<Integer, Object> map = new HashMap();
Object value = parm.getCellValue();
map.put(TableCellStyleType.SHOW_TEXT, value == null ? "" : StudentSexType.getImpStudentDesc(value.toString()));
return map;
}
});
tableModel.setTableCellActionListener(new String[]{"图片"}, new TableCellActionListener() {
@Override
public void run(TableCellEditorParameter param) {
uploadPhoto();
}
});
tableModel.setTableCellActionListener(new String[]{"查看图片"}, new TableCellActionListener() {
@Override
public void run(TableCellEditorParameter param) {
downloadPhoto();
}

});
return tableModel;
}

/**
* 浏览图片
*/
private void downloadPhoto() {
student = (Student) this.tableModel.getSelectedRow();
byte[] b = this.stuLogic.download(request, student);
if (b != null && b.length > 0) {
try {
String path = System.getProperty("java.io.tmpdir");//默认的临时文件路径
File image = new File(path + File.separator + student.getStuFileName());
if (image.exists()) {
image.deleteOnExit();
}
image.createNewFile();
FileUtils.writeByteArrayToFile(image, b);
DgStudentEdit fm = new DgStudentEdit(tableModel, DataStatus.BROWSE, image);
fm.setVisible(true);
//btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bsw/esp/client/images/01_新增.png"))); // NOI18N
} catch (IOException ex) {
Logger.getLogger(FnEmptyTransportBuild.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

private void uploadPhoto() {
try {
JFileChooser jc = FileChooserUtils.createUploadDialog();
jc.setFileFilter(imageFilter);
jc.showOpenDialog(this);
File f = jc.getSelectedFile();
if (f != null && f.length() > 0) {
student = (Student) this.tableModel.getSelectedRow();
boolean isSuccess = this.stuLogic.uploadPhoto(request, FileUtils.readFileToByteArray(f), student, f.getName());
if (isSuccess) {
JOptionPane.showMessageDialog(rootPane, "上传成功!");
jQueryToolbar1.setInitState();
}
}
} catch (IOException ex) {
Logger.getLogger(FnEmptyTransportBuild.class.getName()).log(Level.SEVERE, null, ex);
}
}

FileFilter imageFilter = new FileNameExtensionFilter(
"Image files", ImageIO.getReaderFileSuffixes());

@Override
public boolean action(Event evt, Object what) {
return super.action(evt, what); //To change body of generated methods, choose Tools | Templates.
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPopupMenu1 = new javax.swing.JPopupMenu();
jToolBar1 = new javax.swing.JToolBar();
btnAdd = new javax.swing.JButton();
btnModify = new javax.swing.JButton();
btnDel = new javax.swing.JButton();
btnCopy = new com.bsw.client.component.JBButton();
btnImport = new com.bsw.client.component.JBButton();
btnClose = new com.bsw.client.component.JBButton();
btnPrint = new com.bsw.client.component.JBButton();
jBButton1 = new com.bsw.client.component.JBButton();
jPanel1 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jQueryToolbar1 = new com.bsw.client.component.JQueryToolbar();
jPanel2 = new javax.swing.JPanel();
tfName = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
tnfAge = new com.bsw.client.component.JNumberTextField();
btnQuery = new com.bsw.client.component.JBButton();
jLabel3 = new javax.swing.JLabel();
stuBeginDate = new com.bsw.client.component.calendar.JCalendarComboBox();
jLabel4 = new javax.swing.JLabel();
stuEndDate = new com.bsw.client.component.calendar.JCalendarComboBox();
qfStudent = new com.bsw.client.component.JQueryField();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

jToolBar1.setBorder(null);
jToolBar1.setFloatable(false);
jToolBar1.setRollover(true);
jToolBar1.setOpaque(false);
jToolBar1.setPreferredSize(new java.awt.Dimension(100, 45));

btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bsw/esp/client/images/01_新增.png"))); // NOI18N
btnAdd.setText("新增");
btnAdd.setBorderPainted(false);
btnAdd.setFocusable(false);
btnAdd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
btnAdd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});
jToolBar1.add(btnAdd);

btnModify.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bsw/esp/client/images/02_修改.png"))); // NOI18N
btnModify.setText("修改");
btnModify.setBorderPainted(false);
btnModify.setFocusable(false);
btnModify.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
btnModify.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
btnModify.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnModifyActionPerformed(evt);
}
});
jToolBar1.add(btnModify);

btnDel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bsw/esp/client/images/03_删除.png"))); // NOI18N
btnDel.setText("删除");
btnDel.setBorderPainted(false);
btnDel.setFocusable(false);
btnDel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
btnDel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
btnDel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDelActionPerformed(evt);
}
});
jToolBar1.add(btnDel);

btnCopy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bsw/esp/client/images/25_转抄.png"))); // NOI18N
btnCopy.setBorderPainted(false);
btnCopy.setFocusable(false);
btnCopy.setHorizontalTextPosition(0);
btnCopy.setText("转抄");
btnCopy.setVerticalTextPosition(3);
btnCopy.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCopyActionPerformed(evt);
}
});
jToolBar1.add(btnCopy);

btnImport.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bsw/esp/client/images/18_导入Excel.png"))); // NOI18N
btnImport.setBorderPainted(false);
btnImport.setFocusable(false);
btnImport.setHorizontalTextPosition(0);
btnImport.setText("导入Excel");
btnImport.setVerticalTextPosition(3);
btnImport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnImportActionPerformed(evt);
}
});
jToolBar1.add(btnImport);

btnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bsw/esp/client/images/29_关闭.png"))); // NOI18N
btnClose.setBorderPainted(false);
btnClose.setFocusable(false);
btnClose.setHorizontalTextPosition(0);
btnClose.setText("关闭");
btnClose.setVerticalTextPosition(3);
btnClose.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCloseActionPerformed(evt);
}
});
jToolBar1.add(btnClose);

btnPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bsw/esp/client/images/24_打印.png"))); // NOI18N
btnPrint.setBorderPainted(false);
btnPrint.setFocusable(false);
btnPrint.setHorizontalTextPosition(0);
btnPrint.setText("预览打印");
btnPrint.setVerticalTextPosition(3);
btnPrint.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnPrintActionPerformed(evt);
}
});
jToolBar1.add(btnPrint);

jBButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bsw/esp/client/images/04_浏览.png"))); // NOI18N
jBButton1.setBorderPainted(false);
jBButton1.setFocusable(false);
jBButton1.setHorizontalTextPosition(0);
jBButton1.setText("浏览");
jBButton1.setVerticalTextPosition(3);
jBButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBButton1ActionPerformed(evt);
}
});
jToolBar1.add(jBButton1);

getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_START);

jPanel1.setLayout(new java.awt.BorderLayout());

jPanel3.setLayout(new java.awt.BorderLayout());

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null}
},
new String [] {
"姓名", "年龄", "地址"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.Integer.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
true, true, false
};

public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}

public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTable1MouseClicked(evt);
}
});
jScrollPane1.setViewportView(jTable1);

jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);

jQueryToolbar1.setToolbarDataSource(toolbarDataSource);
jPanel3.add(jQueryToolbar1, java.awt.BorderLayout.PAGE_END);

jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);

jPanel2.setPreferredSize(new java.awt.Dimension(729, 35));

tfName.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tfNameActionPerformed(evt);
}
});

jLabel2.setText("姓名");

jLabel1.setText("年龄");

tnfAge.setText("jNumberTextField1");

btnQuery.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bsw/esp/client/images/16_查询.png"))); // NOI18N
btnQuery.setText("查询");
btnQuery.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnQueryActionPerformed(evt);
}
});

jLabel3.setText("录入日期");

stuBeginDate.setDate(null);

jLabel4.setText("至");

qfStudent.setFieldName("student.stuName");
qfStudent.setTextFieldName("stuName");
qfStudent.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
qfStudentActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfName, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(tnfAge, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(stuBeginDate, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(stuEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(qfStudent, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnQuery, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(20, 20, 20))
);

jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {stuBeginDate, stuEndDate, tfName, tnfAge});

jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(2, 2, 2)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(qfStudent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnQuery, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(stuEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(stuBeginDate, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(tfName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(tnfAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(jLabel4))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {stuBeginDate, stuEndDate, tfName, tnfAge});

jPanel1.add(jPanel2, java.awt.BorderLayout.PAGE_START);

getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

pack();
}// </editor-fold>

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// DgStudentEdit dgStudentEdit = new DgStudentEdit(this.tableModel);
// dgStudentEdit.setVisible(true);
// this.showEditForm(DataStatus.ADD);
DgStudentEdit fm = new DgStudentEdit(tableModel, DataStatus.ADD);
fm.setVisible(true);

}

private void btnDelActionPerformed(java.awt.event.ActionEvent evt) {
List curRows = tableModel.getCheckedRows();
if (curRows == null || curRows.size() <= 0) {
JOptionPane.showMessageDialog(FnEmptyTransportBuild.this, "请选择你要删除的资料", "确认", 1);
return;
}

if (JOptionPane.showConfirmDialog(FnEmptyTransportBuild.this, "你确定要删除记录吗?", "确认", 0) == 0) {
try {
stuLogic.deleteStuEntities(CoreClientUtils.getRequest(), curRows);
tableModel.deleteRows(curRows);

} catch (Exception r) {
JOptionPane.showMessageDialog(FnEmptyTransportBuild.this, "该资料已被引用,您不可以删除!", "提示信息", 2);
}
}
}

private void btnModifyActionPerformed(java.awt.event.ActionEvent evt) {
Student sd = (Student) tableModel.getCheckedRow();
if (sd == null) {
JOptionPane.showMessageDialog(this, "请选择要修改的数据!");
return;
}
this.showEditForm(DataStatus.EDIT);
}

private void showEditForm(int dataState) {
student = (Student) this.tableModel.getSelectedRow();
byte[] b = this.stuLogic.download(request, student);
if (b != null && b.length > 0) {
try {
String path = System.getProperty("java.io.tmpdir");//默认的临时文件路径
File image = new File(path + File.separator + student.getStuFileName());
if (image.exists()) {
image.deleteOnExit();
}
image.createNewFile();
FileUtils.writeByteArrayToFile(image, b);
DgStudentEdit fm = new DgStudentEdit(tableModel, dataState, image);
fm.setVisible(true);
//btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bsw/esp/client/images/01_新增.png"))); // NOI18N
} catch (IOException ex) {
Logger.getLogger(FnEmptyTransportBuild.class.getName()).log(Level.SEVERE, null, ex);
}
} else {

DgStudentEdit fm = new DgStudentEdit(tableModel, dataState);
fm.setVisible(true);
}
}
private void btnQueryActionPerformed(java.awt.event.ActionEvent evt) {
queryData();
}

private void tfNameActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

/**
* 鼠标监听事件,双击打开对话框
*
* @param evt
*/
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
int count = evt.getClickCount();
if (count == 2) {
this.showEditForm(DataStatus.EDIT);
}
}

private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
dispose();
}

private void btnCopyActionPerformed(java.awt.event.ActionEvent evt) {
Student sourEdi = (Student) tableModel.getCheckedRow();
if (sourEdi == null) {
JOptionPane.showMessageDialog(this, "请选择要转抄的数据!");
return;
}
Student emptyTransportDefiniteEdi = stuLogic.copyEmptyTransportDefiniteEdi(CoreClientUtils.getRequest(), sourEdi);
if (emptyTransportDefiniteEdi != null) {
tableModel.addRow(emptyTransportDefiniteEdi);
this.showEditForm(DataStatus.EDIT);
jQueryToolbar1.setInitState();
} else {
JOptionPane.showMessageDialog(this, "你选择要转抄的数据在数据库不存在!");
}
}

/**
* 学生信息导入,实现验证和保存
*
* @param evt
*/
private void btnImportActionPerformed(java.awt.event.ActionEvent evt) {
DgImportExcel dg = new DgImportExcel("学生信息导入", StudentVo.class) {
@Override
protected boolean doSave(List data, String importType) {
stuLogic.doSaveStudents(request, data);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
loadData();
}
});
return true;
}

@Override
protected void doValidate(List data, String importType) {
List<ValidateMessage> result = new ArrayList();

result = stuLogic.validateStudent(request, data, importType);
result = result == null ? new ArrayList<ValidateMessage>() : result;
super.appendErrorMsg(result);
}
};
dg.setVisible(true);
}

private void qfStudentActionPerformed(java.awt.event.ActionEvent evt) {
CommonModel.initQfStudent(qfStudent);
}

/**
* 打印报表
*
* @param evt
*/
private void btnPrintActionPerformed(java.awt.event.ActionEvent evt) {
Student st = getSingleCheckedRow();
if (st == null) {
return;
}
try {
new StudentReportHelper().print(st);
} catch (JRException ex) {
Logger.getLogger(FnEmptyTransportBuild.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void jBButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Student sd = (Student) tableModel.getCheckedRow();
if (sd == null) {
JOptionPane.showMessageDialog(this, "请选择要浏览的数据!");
return;
}
this.showEditForm(DataStatus.EDIT);
}

// Variables declaration - do not modify
private javax.swing.JButton btnAdd;
private com.bsw.client.component.JBButton btnClose;
private com.bsw.client.component.JBButton btnCopy;
private javax.swing.JButton btnDel;
private com.bsw.client.component.JBButton btnImport;
private javax.swing.JButton btnModify;
private com.bsw.client.component.JBButton btnPrint;
private com.bsw.client.component.JBButton btnQuery;
private com.bsw.client.component.JBButton jBButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPopupMenu jPopupMenu1;
private com.bsw.client.component.JQueryToolbar jQueryToolbar1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JToolBar jToolBar1;
private com.bsw.client.component.JQueryField qfStudent;
private com.bsw.client.component.calendar.JCalendarComboBox stuBeginDate;
private com.bsw.client.component.calendar.JCalendarComboBox stuEndDate;
private javax.swing.JTextField tfName;
private com.bsw.client.component.JNumberTextField tnfAge;
// End of variables declaration

private void queryData() {
jQueryToolbar1.setInitState();
}

/**
* 选择单条学生数据
*
* @return
*/
private Student getSingleCheckedRow() {
Student decHead = (Student) tableModel.getSelectedRow();
List<Student> billHeadList = tableModel.getCheckedRows();

if (!billHeadList.isEmpty()) {
decHead = (Student) billHeadList.get(0);
} else if (!billHeadList.contains(decHead)) {
billHeadList.add(decHead);
}
if (decHead == null) {
JOptionPane.showMessageDialog(this, "请选择学生资料!");
return null;
} // } else if (billHeadList.size() != 1) {
// JOptionPane.showMessageDialog(this, "只能选择单条资料!");
// return null;
// }
else {
return billHeadList.get(0);
}
}
}

-----------------------------------------------

//2.接口类

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.bsw.jgz.demo.logic;

import com.bsw.core.base.Request;
import com.bsw.core.common.ValidateMessage;
import com.bsw.jgz.demo.entity.Student;
import com.bsw.jgz.demo.entity.vo.StudentVo;
import java.util.Date;
import java.util.List;
import javax.ejb.Local;

/**
*
* @author mpz
*/
@Local
public interface StuParamLogic {

/**
* 保存相关参数
*
* @param request
* @param student
* @return
*/
Student saveDecAgtHeadDefault(Request request, Student student);

// List getStudents(Request request, String name, Integer age);
List getStudentss(Request request, String name);

/**
* 学生信息删除
*
* @param request
* @param entities
*/
public void deleteStuEntities(Request request, List entities);

// public List findEmptyTransportStudentEdi(Request request, String name, Integer age);
public List findEmptyTransportStudentsEdi(Request request, String name, Integer age, Date beginDate, Date endDate);

/**
* 学生资料转抄
*
* @param request
* @param sourEdi
* @return
*/
public Student copyEmptyTransportDefiniteEdi(Request request, Student sourEdi);

public Boolean doSaveStudents(Request request, List<StudentVo> data);

/**
* 学生资料导入
*
* @param request
* @param data
* @param importType
* @return
*/
public List importAgtBrokerCorpStudent(Request request, List data, String importType);

/**
* 学生资料导入时检查数据
*
* @param request
* @param data
* @param importType
* @return
*/
public List<ValidateMessage> validateStudent(Request request, List<StudentVo> data, String importType);

public List findStudentBaseEntity(Request request, String simpleName, String name, Integer age, Date begindate, Date enddate, boolean b, int index, int length);

public Long getCountStudentBaseEntity(Request request, String simpleName, String name, Integer age, Date begindate, Date enddate, boolean b);

/**
* 上传图片
*
* @param request
* @param b
* @param student
* @param fileName
* @return
*/
boolean uploadPhoto(Request request, byte[] b, Student student, String fileName);

// public String downloadPhoto(Request request, Student student, int Offset, int length, String filePath, String code);
public String geturl(Request request, Student student, String fileName);

public byte[] download(Request request, Student student);

public List findCentralBillItemEdiByHead(Request request);

}

---------------------------

//3.实现类

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.bsw.jgz.demo.logic;

import com.bsw.core.base.Request;
import com.bsw.core.common.ValidateMessage;
import com.bsw.core.conversion.Conversion;
import com.bsw.core.conversion.ConversionContext;
import com.bsw.core.conversion.DefaultConversion;
import com.bsw.core.dataimport.ImportType;
import com.bsw.core.utils.DataObjectUtils;
import com.bsw.esp.aop.EspServerAop;
import com.bsw.esp.common.DirConstant;
import com.bsw.esp.logic.EspLogic;
import com.bsw.jgz.demo.dao.EspStudentDao;
import com.bsw.jgz.demo.entity.Student;
import com.bsw.jgz.demo.entity.vo.StudentVo;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.interceptor.Interceptors;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;

/**
*
* @author mpz
*/
@Stateless
@Interceptors(EspServerAop.class)
@TransactionManagement(value = TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class StuParamLogicImpl implements StuParamLogic {

@EJB
private EspStudentDao espStudentDao;

@EJB(lookup = "java:global/esp/esp-ejb/EspLogicImpl")
protected EspLogic espLogic;

@Override
public Student saveDecAgtHeadDefault(Request request, Student student) {
return espStudentDao.saveOrUpdate(student);
}

// @Override
// public List getStudents(Request request, String name, Integer age) {
// return espStudentDao.getStudents(name, age);
// }
@Override
public List getStudentss(Request request, String name) {
return espStudentDao.getStudentss(name);
}

/**
* 删除学生信息
*
* @param request
* @param entities
*/
@Override
public void deleteStuEntities(Request request, List entities) {
espStudentDao.delete(entities);

}

// @Override
// public List findEmptyTransportStudentEdi(Request request, String name, Integer age) {
// return this.espStudentDao.findEmptyTransportStudentEdi(name, age);
// }
@Override
public List findEmptyTransportStudentsEdi(Request request, String name, Integer age, Date beginDate, Date endDate) {
return this.espStudentDao.findEmptyTransportStudentsEdi(name, age, beginDate, endDate);
}

/**
* 转抄学生信息
*
* @param request
* @param sourEdi
* @return
*/
@Override
public Student copyEmptyTransportDefiniteEdi(Request request, Student sourEdi) {
if (sourEdi != null) {

sourEdi = this.espStudentDao.load(Student.class, sourEdi.getId());
}
if (sourEdi == null) {
return null;
}
Student emptyTransportDefiniteEdi = new Student();
DataObjectUtils.copyProperties(emptyTransportDefiniteEdi, sourEdi, new String[]{"id", "optLock", "createDate", "createUser", "modifyDate", "modifyUser", "stuCode"});
return this.espStudentDao.saveOrUpdate(emptyTransportDefiniteEdi);
}

@Override
public Boolean doSaveStudents(Request request, List<StudentVo> data) {
if (data != null) {
List<Student> students = new ArrayList();
for (StudentVo studentVo : data) {
if (studentVo.getStuSex().equals("男")) {
studentVo.setStuSex("0");
} else if (studentVo.getStuSex().equals("女")) {
studentVo.setStuSex("1");
}
Student student = new Student();
DataObjectUtils.copyProperties(student, studentVo);
students.add(student);
}
this.espStudentDao.saveOrUpdate(students);
}
return true;
}

/**
* 学生资料导入
*
* @param request
* @param data
* @param importType
* @return
*/
@Override
public List importAgtBrokerCorpStudent(Request request, List data, String importType) {
ConversionContext context = new ConversionContext();
Conversion c = new DefaultConversion();
context.setIgnoreValid(false);
//缓存ApplyInfoItem

Map result = c.transform(context, data, AgtBrokerCorpStudentContex.class);
List resultList = new ArrayList();
List<Student> agtCoStudents = (List) result.get(Student.class);
//覆盖导入资料
if (ImportType.COVER_IMPORT.equals(importType)) {
List<Student> colist = espStudentDao.findStudents(null, null);
Map<String, Student> plMap = new HashMap<String, Student>();
for (Student driver : colist) {
if (StringUtils.isNotEmpty(driver.getStuaddress())) {
String key = driver.getStuaddress();
plMap.put(key, driver);
}
}
}

return espStudentDao.saveOrUpdate(resultList);
}

/**
* 学生资料导入时检查数据
*
* @param request
* @param data
* @param importType
* @return
*/
@Override
public List<ValidateMessage> validateStudent(Request request, List<StudentVo> data, String importType) {
if (data == null) {
return null;
}
//错误信息
List errorList = new ArrayList();
List<Student> students = espStudentDao.findStudents(null, null);
Map<String, Student> sysStuMaps = new HashMap<String, Student>();
Map<String, StudentVo> selfMaps = new HashMap<String, StudentVo>();
for (Student student : students) {
if (StringUtils.isNotBlank(student.getStuCode())) {
String key = student.getStuCode();
sysStuMaps.put(key, student);
}
}

//判断当前导入的资料与系统中的是否有重复
for (StudentVo studentVo : data) {
//判断当前导入的资料中是否有重复
if (StringUtils.isNotBlank(studentVo.getStuCode())) {
Student stu = sysStuMaps.get(studentVo.getStuCode());
if (stu != null) {
errorList.add(new ValidateMessage(data.indexOf(studentVo), " “學生編碼”在系统中已存在; "));
}
if (selfMaps.get(studentVo.getStuCode()) != null) {
errorList.add(new ValidateMessage(data.indexOf(studentVo), " “學生編碼”在导入文件中已存在; "));
}
}

selfMaps.put(studentVo.getStuCode(), studentVo);
}
return errorList;
}

public List findStudentBaseEntity(Request request, String entityClassName, String name, Integer age, Date begindate, Date enddate, boolean b, int start, int limit) {
return espStudentDao.findStudentBaseEntity(entityClassName, name, age, begindate, enddate, b, start, limit);
}

/**
* 查询总条数
*
* @param request
* @param simpleName
* @param name
* @param age
* @param b
* @return
*/
@Override
public Long getCountStudentBaseEntity(Request request, String simpleName, String name, Integer age, Date begindate, Date enddate, boolean b) {
return espStudentDao.findCountStudentEmtity(simpleName, name, age, begindate, enddate, b);
}

/**
* 上传图片
*
* @param request
* @param b
* @param student
* @param fileName
* @return
*/
public boolean uploadPhoto(Request request, byte[] b, Student student, String fileName) {
boolean isSuccess = true;
if (b != null && b.length > 0 && student != null) {
try {
File jbossPath = new File(getJbossHomeDir());
if (!jbossPath.exists()) {
jbossPath.mkdir();
}
String photoFileName = getSaveFileName(fileName, student);
fileName = jbossPath + File.separator + photoFileName;
File saveFile = new File(fileName);
if (saveFile.exists()) {
saveFile.delete();
}
saveFile.createNewFile();
FileUtils.writeByteArrayToFile(saveFile, b);
student = this.espStudentDao.load(Student.class, student.getId());
student.setStuFileName(photoFileName);
this.espStudentDao.saveOrUpdate(student);
} catch (IOException ex) {
isSuccess = false;
Logger.getLogger(StuParamLogicImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
return isSuccess;
}

private String getJbossHomeDir() {
String pathDir = DirConstant.JBOSS_HOME + File.separator
+ "bin" + File.separator + "photo";
return pathDir;
}

/**
* 获取文件保存名称
*
* @param fileName
* @return
*/
public static String getSaveFileName(String fileName, Student student) {
if (StringUtils.isBlank(fileName)) {
return String.valueOf(Math.abs(UUID.randomUUID().getMostSignificantBits()));
}
//获得文件的扩展名,不包含"."
String extension = FilenameUtils.getExtension(fileName);
// String name = FilenameUtils.getBaseName(fileName);
fileName = student.getStuName();
if (extension != null) {
fileName += "." + extension;
}
return fileName;
}

/**
* 获取文件保存路径
*
* @param request
* @param student
* @return
*/
@Override
public String geturl(Request request, Student student, String fileName) {
File jbossPath = new File(getJbossHomeDir());
fileName = jbossPath + File.separator + student.getStuFileName();
System.out.println("tupian" + fileName);
return fileName;
}

@Override
public byte[] download(Request request, Student student) {
String filePath = DirConstant.JBOSS_HOME + File.separator + "bin" + File.separator + "photo";
File file = new File(filePath + File.separator + student.getStuFileName());
if (file.exists()) {
try {
return FileUtils.readFileToByteArray(file);
} catch (IOException ex) {
Logger.getLogger(StuParamLogicImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
return null;
}

public static BufferedImage convertToBufferedImage(Image image) {
BufferedImage newImage = new BufferedImage(80, 80, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = newImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return newImage;
}

@Override
public List findCentralBillItemEdiByHead(Request request) {
return espStudentDao.findlist();
}

}

-------------------------------------------

//4.dao层(sql语句)

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.bsw.jgz.demo.logic;

import com.bsw.core.base.Request;
import com.bsw.core.common.ValidateMessage;
import com.bsw.core.conversion.Conversion;
import com.bsw.core.conversion.ConversionContext;
import com.bsw.core.conversion.DefaultConversion;
import com.bsw.core.dataimport.ImportType;
import com.bsw.core.utils.DataObjectUtils;
import com.bsw.esp.aop.EspServerAop;
import com.bsw.esp.common.DirConstant;
import com.bsw.esp.logic.EspLogic;
import com.bsw.jgz.demo.dao.EspStudentDao;
import com.bsw.jgz.demo.entity.Student;
import com.bsw.jgz.demo.entity.vo.StudentVo;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.interceptor.Interceptors;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;

/**
*
* @author mpz
*/
@Stateless
@Interceptors(EspServerAop.class)
@TransactionManagement(value = TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class StuParamLogicImpl implements StuParamLogic {

@EJB
private EspStudentDao espStudentDao;

@EJB(lookup = "java:global/esp/esp-ejb/EspLogicImpl")
protected EspLogic espLogic;

@Override
public Student saveDecAgtHeadDefault(Request request, Student student) {
return espStudentDao.saveOrUpdate(student);
}

// @Override
// public List getStudents(Request request, String name, Integer age) {
// return espStudentDao.getStudents(name, age);
// }
@Override
public List getStudentss(Request request, String name) {
return espStudentDao.getStudentss(name);
}

/**
* 删除学生信息
*
* @param request
* @param entities
*/
@Override
public void deleteStuEntities(Request request, List entities) {
espStudentDao.delete(entities);

}

// @Override
// public List findEmptyTransportStudentEdi(Request request, String name, Integer age) {
// return this.espStudentDao.findEmptyTransportStudentEdi(name, age);
// }
@Override
public List findEmptyTransportStudentsEdi(Request request, String name, Integer age, Date beginDate, Date endDate) {
return this.espStudentDao.findEmptyTransportStudentsEdi(name, age, beginDate, endDate);
}

/**
* 转抄学生信息
*
* @param request
* @param sourEdi
* @return
*/
@Override
public Student copyEmptyTransportDefiniteEdi(Request request, Student sourEdi) {
if (sourEdi != null) {

sourEdi = this.espStudentDao.load(Student.class, sourEdi.getId());
}
if (sourEdi == null) {
return null;
}
Student emptyTransportDefiniteEdi = new Student();
DataObjectUtils.copyProperties(emptyTransportDefiniteEdi, sourEdi, new String[]{"id", "optLock", "createDate", "createUser", "modifyDate", "modifyUser", "stuCode"});
return this.espStudentDao.saveOrUpdate(emptyTransportDefiniteEdi);
}

@Override
public Boolean doSaveStudents(Request request, List<StudentVo> data) {
if (data != null) {
List<Student> students = new ArrayList();
for (StudentVo studentVo : data) {
if (studentVo.getStuSex().equals("男")) {
studentVo.setStuSex("0");
} else if (studentVo.getStuSex().equals("女")) {
studentVo.setStuSex("1");
}
Student student = new Student();
DataObjectUtils.copyProperties(student, studentVo);
students.add(student);
}
this.espStudentDao.saveOrUpdate(students);
}
return true;
}

/**
* 学生资料导入
*
* @param request
* @param data
* @param importType
* @return
*/
@Override
public List importAgtBrokerCorpStudent(Request request, List data, String importType) {
ConversionContext context = new ConversionContext();
Conversion c = new DefaultConversion();
context.setIgnoreValid(false);
//缓存ApplyInfoItem

Map result = c.transform(context, data, AgtBrokerCorpStudentContex.class);
List resultList = new ArrayList();
List<Student> agtCoStudents = (List) result.get(Student.class);
//覆盖导入资料
if (ImportType.COVER_IMPORT.equals(importType)) {
List<Student> colist = espStudentDao.findStudents(null, null);
Map<String, Student> plMap = new HashMap<String, Student>();
for (Student driver : colist) {
if (StringUtils.isNotEmpty(driver.getStuaddress())) {
String key = driver.getStuaddress();
plMap.put(key, driver);
}
}
}

return espStudentDao.saveOrUpdate(resultList);
}

/**
* 学生资料导入时检查数据
*
* @param request
* @param data
* @param importType
* @return
*/
@Override
public List<ValidateMessage> validateStudent(Request request, List<StudentVo> data, String importType) {
if (data == null) {
return null;
}
//错误信息
List errorList = new ArrayList();
List<Student> students = espStudentDao.findStudents(null, null);
Map<String, Student> sysStuMaps = new HashMap<String, Student>();
Map<String, StudentVo> selfMaps = new HashMap<String, StudentVo>();
for (Student student : students) {
if (StringUtils.isNotBlank(student.getStuCode())) {
String key = student.getStuCode();
sysStuMaps.put(key, student);
}
}

//判断当前导入的资料与系统中的是否有重复
for (StudentVo studentVo : data) {
//判断当前导入的资料中是否有重复
if (StringUtils.isNotBlank(studentVo.getStuCode())) {
Student stu = sysStuMaps.get(studentVo.getStuCode());
if (stu != null) {
errorList.add(new ValidateMessage(data.indexOf(studentVo), " “學生編碼”在系统中已存在; "));
}
if (selfMaps.get(studentVo.getStuCode()) != null) {
errorList.add(new ValidateMessage(data.indexOf(studentVo), " “學生編碼”在导入文件中已存在; "));
}
}

selfMaps.put(studentVo.getStuCode(), studentVo);
}
return errorList;
}

public List findStudentBaseEntity(Request request, String entityClassName, String name, Integer age, Date begindate, Date enddate, boolean b, int start, int limit) {
return espStudentDao.findStudentBaseEntity(entityClassName, name, age, begindate, enddate, b, start, limit);
}

/**
* 查询总条数
*
* @param request
* @param simpleName
* @param name
* @param age
* @param b
* @return
*/
@Override
public Long getCountStudentBaseEntity(Request request, String simpleName, String name, Integer age, Date begindate, Date enddate, boolean b) {
return espStudentDao.findCountStudentEmtity(simpleName, name, age, begindate, enddate, b);
}

/**
* 上传图片
*
* @param request
* @param b
* @param student
* @param fileName
* @return
*/
public boolean uploadPhoto(Request request, byte[] b, Student student, String fileName) {
boolean isSuccess = true;
if (b != null && b.length > 0 && student != null) {
try {
File jbossPath = new File(getJbossHomeDir());
if (!jbossPath.exists()) {
jbossPath.mkdir();
}
String photoFileName = getSaveFileName(fileName, student);
fileName = jbossPath + File.separator + photoFileName;
File saveFile = new File(fileName);
if (saveFile.exists()) {
saveFile.delete();
}
saveFile.createNewFile();
FileUtils.writeByteArrayToFile(saveFile, b);
student = this.espStudentDao.load(Student.class, student.getId());
student.setStuFileName(photoFileName);
this.espStudentDao.saveOrUpdate(student);
} catch (IOException ex) {
isSuccess = false;
Logger.getLogger(StuParamLogicImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
return isSuccess;
}

private String getJbossHomeDir() {
String pathDir = DirConstant.JBOSS_HOME + File.separator
+ "bin" + File.separator + "photo";
return pathDir;
}

/**
* 获取文件保存名称
*
* @param fileName
* @return
*/
public static String getSaveFileName(String fileName, Student student) {
if (StringUtils.isBlank(fileName)) {
return String.valueOf(Math.abs(UUID.randomUUID().getMostSignificantBits()));
}
//获得文件的扩展名,不包含"."
String extension = FilenameUtils.getExtension(fileName);
// String name = FilenameUtils.getBaseName(fileName);
fileName = student.getStuName();
if (extension != null) {
fileName += "." + extension;
}
return fileName;
}

/**
* 获取文件保存路径
*
* @param request
* @param student
* @return
*/
@Override
public String geturl(Request request, Student student, String fileName) {
File jbossPath = new File(getJbossHomeDir());
fileName = jbossPath + File.separator + student.getStuFileName();
System.out.println("tupian" + fileName);
return fileName;
}

@Override
public byte[] download(Request request, Student student) {
String filePath = DirConstant.JBOSS_HOME + File.separator + "bin" + File.separator + "photo";
File file = new File(filePath + File.separator + student.getStuFileName());
if (file.exists()) {
try {
return FileUtils.readFileToByteArray(file);
} catch (IOException ex) {
Logger.getLogger(StuParamLogicImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
return null;
}

public static BufferedImage convertToBufferedImage(Image image) {
BufferedImage newImage = new BufferedImage(80, 80, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = newImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return newImage;
}

@Override
public List findCentralBillItemEdiByHead(Request request) {
return espStudentDao.findlist();
}

}

-----------------------------

//5.弹窗编辑界面

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.bsw.jgz.demo;

import com.bsw.client.table.JTableListModel;
import com.bsw.client.utils.BeanBindUtil;
import com.bsw.client.utils.CoreUIUtils;
import com.bsw.client.windows.base.JDialogBase;
import com.bsw.core.base.Request;
import com.bsw.core.client.common.CoreClientUtils;
import com.bsw.core.client.constant.DataStatus;
import com.bsw.core.utils.AppException;
import com.bsw.esp.client.common.ItemProperty;
import com.bsw.esp.constant.StudentSexType;
import com.bsw.jgz.demo.entity.Student;
import com.bsw.jgz.demo.logic.StuParamLogic;
import com.bsw.rms.client.common.RmsModel;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;

/**
*
* @author mpz
*/
public class DgStudentEdit extends JDialogBase {

private Student student;
private final StuParamLogic stuLogic = CoreClientUtils.getBean(StuParamLogic.class);
private JTableListModel tableModel;
private int dataStatus = DataStatus.BROWSE;
private final Request request = CoreClientUtils.getRequest();
private File imageFile;
private ImageIcon image;

/**
* Creates new form DgStudentEdit1
*/
public DgStudentEdit() {
initComponents();

}

private void initUI() {
this.jFilterComboBox1.setModel(RmsModel.getImpStudentModel());
setlbText();
}

public DgStudentEdit(JTableListModel tableModel, int dataStatus) {
initComponents();
initUI();
this.tableModel = tableModel;
this.dataStatus = dataStatus;
bindForm();
setStatus();
}

public DgStudentEdit(JTableListModel tableModel, int dataStatus, File imageFile) {
initComponents();
initUI();
this.tableModel = tableModel;
this.dataStatus = dataStatus;
this.imageFile = imageFile;
bindForm();
setIcon();
setStatus();
}

private void setIcon() {
image = new ImageIcon(imageFile.getPath());
Image img = image.getImage();
img = img.getScaledInstance(124, 164, Image.SCALE_DEFAULT);
image.setImage(img);
this.jbPhoto.setIcon(image); // NOI18N
// this.jbPhoto.setIcon(new javax.swing.ImageIcon(imageFile.getPath())); // NOI18N
}

private void bindForm() {
if (getTableModel() == null) {
throw new AppException("TableModel为空!");
}

this.student = (Student) this.getTableModel().getCheckedRow();
if (this.dataStatus != DataStatus.ADD) {
if (this.student != null) {
//绑定窗口,把对象的值赋值给窗口显示
BeanBindUtil.bindForm(rootPane, this.student);
this.jFilterComboBox1.setValue(new ItemProperty(student.getStuSex(), StudentSexType.getImpStudentDesc(student.getStuSex())));
this.dataStatus = DataStatus.EDIT;
}
}
}

private void setlbText() {
String tx = "<html><body>照<br/><br/>片</body></html>";
this.jbPhoto.setText(tx);
}

/**
* 状态设置
*/
private void setStatus() {
this.btnModify.setEnabled(DataStatus.BROWSE == dataStatus);
//保存数据
this.btnSave.setEnabled(DataStatus.EDIT == dataStatus || DataStatus.ADD == dataStatus);
//处理文本框上的控件是否可用
CoreUIUtils.setFormComponentsEditable(rootPane, DataStatus.EDIT == dataStatus || DataStatus.ADD == dataStatus);
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel6 = new javax.swing.JLabel();
jToolBar1 = new javax.swing.JToolBar();
btnModify = new com.bsw.client.component.JBButton();
btnSave = new com.bsw.client.component.JBButton();
btnCancel = new com.bsw.client.component.JBButton();
btnClose = new com.bsw.client.component.JBButton();
jLayeredPane1 = new javax.swing.JLayeredPane();
jLabel1 = new javax.swing.JLabel();
tfTaxRate = new com.bsw.client.component.JBTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
tfTaxRate1 = new com.bsw.client.component.JBTextField();
jNumberTextField1 = new com.bsw.client.component.JNumberTextField();
jLabel4 = new javax.swing.JLabel();
jFilterComboBox1 = new com.bsw.client.component.combobox.JFilterComboBox();
jLabel5 = new javax.swing.JLabel();
jBTextField1 = new com.bsw.client.component.JBTextField();
jbPhoto = new javax.swing.JLabel();

jLabel6.setText("jLabel6");

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

jToolBar1.setFloatable(false);
jToolBar1.setRollover(true);
jToolBar1.setOpaque(false);

btnModify.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bsw/esp/client/images/02_修改.png"))); // NOI18N
btnModify.setAuthorityID("ies.dec.headparam_edit");
btnModify.setBorderPainted(false);
btnModify.setFocusable(false);
btnModify.setHorizontalTextPosition(0);
btnModify.setIconTextGap(0);
btnModify.setText("修改");
btnModify.setVerticalTextPosition(3);
btnModify.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnModifyActionPerformed(evt);
}
});
jToolBar1.add(btnModify);

btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bsw/esp/client/images/15_保存.png"))); // NOI18N
btnSave.setAuthorityID("ies.dec.headparam_edit");
btnSave.setBorderPainted(false);
btnSave.setFocusable(false);
btnSave.setHorizontalTextPosition(0);
btnSave.setIconTextGap(0);
btnSave.setText("保存");
btnSave.setVerticalTextPosition(3);
btnSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSaveActionPerformed(evt);
}
});
jToolBar1.add(btnSave);

btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bsw/esp/client/images/36_取消.png"))); // NOI18N
btnCancel.setBorderPainted(false);
btnCancel.setFocusable(false);
btnCancel.setHorizontalTextPosition(0);
btnCancel.setIconTextGap(0);
btnCancel.setText("取消");
btnCancel.setVerticalTextPosition(3);
btnCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCancelActionPerformed(evt);
}
});
jToolBar1.add(btnCancel);

btnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/bsw/esp/client/images/29_关闭.png"))); // NOI18N
btnClose.setBorderPainted(false);
btnClose.setFocusable(false);
btnClose.setHorizontalTextPosition(0);
btnClose.setIconTextGap(0);
btnClose.setText("关闭");
btnClose.setVerticalTextPosition(3);
btnClose.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCloseActionPerformed(evt);
}
});
jToolBar1.add(btnClose);

jLabel1.setText("学生姓名");

tfTaxRate.setFieldName("stuName");
tfTaxRate.setPreferredSize(new java.awt.Dimension(129, 22));
tfTaxRate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tfTaxRateActionPerformed(evt);
}
});

jLabel2.setText("学生年龄");

jLabel3.setText("学生地址");

tfTaxRate1.setFieldName("stuaddress");
tfTaxRate1.setPreferredSize(new java.awt.Dimension(129, 22));
tfTaxRate1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tfTaxRate1ActionPerformed(evt);
}
});

jNumberTextField1.setText("jNumberTextField1");
jNumberTextField1.setFieldName("stuage");

jLabel4.setText("学生性别");

jFilterComboBox1.setFieldName("stuSex");
jFilterComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jFilterComboBox1ActionPerformed(evt);
}
});

jLabel5.setText("学生编号");

jBTextField1.setFieldName("stuCode");

jLayeredPane1.setLayer(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(tfTaxRate, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(tfTaxRate1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jNumberTextField1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jFilterComboBox1, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);
jLayeredPane1.setLayer(jBTextField1, javax.swing.JLayeredPane.DEFAULT_LAYER);

javax.swing.GroupLayout jLayeredPane1Layout = new javax.swing.GroupLayout(jLayeredPane1);
jLayeredPane1.setLayout(jLayeredPane1Layout);
jLayeredPane1Layout.setHorizontalGroup(
jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel5)
.addComponent(jLabel3))
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)
.addComponent(jLabel2))
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(tfTaxRate, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addGap(17, 17, 17)
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(tfTaxRate1, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
.addComponent(jNumberTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
.addComponent(jBTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jLayeredPane1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jFilterComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

jLayeredPane1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jBTextField1, jNumberTextField1, tfTaxRate, tfTaxRate1});

jLayeredPane1Layout.setVerticalGroup(
jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jLayeredPane1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(tfTaxRate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jFilterComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jNumberTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tfTaxRate1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(jBTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);

jLayeredPane1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jBTextField1, jNumberTextField1, tfTaxRate, tfTaxRate1});

jLayeredPane1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3, jLabel4});

jbPhoto.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jbPhoto.setBorder(javax.swing.BorderFactory.createEtchedBorder());

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jbPhoto, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jbPhoto, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(160, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void btnModifyActionPerformed(java.awt.event.ActionEvent evt) {
BeanBindUtil.bindBean(this, student);
setStatus();
}

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
if (student == null) {
student = new Student();
}
//绑定对象,把容器内的值赋值给对象
BeanBindUtil.bindBean(this, student);
ItemProperty ip = (ItemProperty) this.jFilterComboBox1.getValue();
student.setStuSex(ip.getCode());
student = stuLogic.saveDecAgtHeadDefault(request, student);
if (DataStatus.ADD == dataStatus) {
tableModel.addRow(student);
} else {
tableModel.updateRow(student);
}
this.dispose();

}

private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
// BeanBindUtil.bindForm(this, student);
//setStatus(DataStatus.BROWSE);
this.dispose();
}

private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}

private void tfTaxRate1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void tfTaxRateActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jFilterComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

public Student getStudent() {
return student;
}

public void setStudent(Student student) {
this.student = student;
}

public JTableListModel getTableModel() {
return tableModel;
}

public void setTableModel(JTableListModel tableModel) {
this.tableModel = tableModel;
}

// Variables declaration - do not modify
private com.bsw.client.component.JBButton btnCancel;
private com.bsw.client.component.JBButton btnClose;
private com.bsw.client.component.JBButton btnModify;
private com.bsw.client.component.JBButton btnSave;
private com.bsw.client.component.JBTextField jBTextField1;
private com.bsw.client.component.combobox.JFilterComboBox jFilterComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLayeredPane jLayeredPane1;
private com.bsw.client.component.JNumberTextField jNumberTextField1;
private javax.swing.JToolBar jToolBar1;
private javax.swing.JLabel jbPhoto;
private com.bsw.client.component.JBTextField tfTaxRate;
private com.bsw.client.component.JBTextField tfTaxRate1;
// End of variables declaration
}

原文地址:https://www.cnblogs.com/tiansan/p/7373235.html