📄 timecardpanel.java
字号:
package com.mwq.frame.personnel;
import java.awt.BorderLayout;
import java.awt.Color;
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 java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Date;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import com.mwq.frame.common.DeptTreeDialog;
import com.mwq.hibernate.Dao;
import com.mwq.hibernate.HibernateSessionFactory;
import com.mwq.hibernate.mapping.TbAccountItem;
import com.mwq.hibernate.mapping.TbDept;
import com.mwq.hibernate.mapping.TbDutyInfo;
import com.mwq.hibernate.mapping.TbRecord;
import com.mwq.hibernate.mapping.TbTimecard;
import com.mwq.tool.Today;
public class TimecardPanel extends JPanel {
private JTextField inDeptTextField;
private JTextField deptTextField;
private JTextArea explainTextArea;
private JTextField endDateTextField;
private JTextField startDateTextField;
private JTextField ratifierDateTextField;
private JComboBox ratifierPersonComboBox;
private JComboBox timecardTypeComboBox;
private JComboBox personnalComboBox;
private Dao dao = Dao.getInstance();
/**
* Create the panel
*/
public TimecardPanel() {
super();
setLayout(new BorderLayout());
final JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.WHITE);
add(buttonPanel, BorderLayout.NORTH);
final JButton createButton = new JButton();
createButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
inDeptTextField.setText(null);
personnalComboBox.removeAllItems();
personnalComboBox.addItem("请选择");
Iterator recordsIt = dao.queryRecord().iterator();
while (recordsIt.hasNext()) {
TbRecord record = (TbRecord) recordsIt.next();
personnalComboBox.addItem(record.getRecordNumber() + " "
+ record.getName());
}
HibernateSessionFactory.closeSession();
timecardTypeComboBox.setSelectedIndex(0);
explainTextArea.setText("");
startDateTextField.setText(Today.TODAY_DATE);
endDateTextField.setText(Today.TODAY_DATE);
deptTextField.setText("");
ratifierPersonComboBox.removeAllItems();
ratifierPersonComboBox.setEnabled(false);
ratifierDateTextField.setText(Today.TODAY_DATE);
}
});
createButton.setText("新建");
buttonPanel.add(createButton);
final JButton saveButton = new JButton();
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
TbTimecard timecard = new TbTimecard();
String personnal = personnalComboBox.getSelectedItem()
.toString();
if (personnal.equals("请选择")) {
JOptionPane.showMessageDialog(null, "请选择欲考勤的职员!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
TbRecord record = (TbRecord) dao.queryRecordByNum(personnal
.substring(0, 6));
timecard.setTbRecordByRecordId(record);
String timecardType = timecardTypeComboBox.getSelectedItem()
.toString();
if (timecardType.equals("请选择")) {
JOptionPane.showMessageDialog(null, "请选择考勤类型!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
TbAccountItem accountItem = (TbAccountItem) dao
.queryAccountItemByName(timecardType);
timecard.setTbAccountItem(accountItem);
if (explainTextArea.getText().equals("")) {
JOptionPane.showMessageDialog(null, "请填写考勤说明!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
timecard.setExplain(explainTextArea.getText());
try {
timecard.setStartDate(Date.valueOf(startDateTextField
.getText()));
timecard.setEndDate(Date
.valueOf(endDateTextField.getText()));
} catch (RuntimeException e) {
JOptionPane.showMessageDialog(null, "开始日期或结束日期填写错误,请重新填写!",
"友情提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
if (deptTextField.getText().equals("")) {
JOptionPane.showMessageDialog(null, "请选择批准部门!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
TbDept dept = (TbDept) dao.queryDeptByName(deptTextField
.getText());
timecard.setTbDept(dept);
String ratifierPerson = ratifierPersonComboBox
.getSelectedItem().toString();
if (ratifierPerson.equals("请选择")) {
JOptionPane.showMessageDialog(null, "请选择批准人!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
TbRecord ratifierRecord = (TbRecord) dao
.queryRecordByNum(ratifierPerson.substring(0, 6));
timecard.setTbRecordByRatifierRecordId(ratifierRecord);
try {
timecard.setRatifierDate(Date.valueOf(ratifierDateTextField
.getText()));
} catch (RuntimeException e) {
JOptionPane.showMessageDialog(null, "批准日期填写错误,请重新填写!",
"友情提示", JOptionPane.INFORMATION_MESSAGE);
return;
}
// 持久化考勤信息
dao.saveObject(timecard);
HibernateSessionFactory.closeSession();
//
JOptionPane.showMessageDialog(null, "已经成功保存此次考勤信息!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);
}
});
saveButton.setText("保存");
buttonPanel.add(saveButton);
final JPanel contentPanel = new JPanel();
contentPanel.setBackground(Color.WHITE);
contentPanel.setLayout(new GridBagLayout());
add(contentPanel, BorderLayout.CENTER);
final JLabel inDeptLabel = new JLabel();
inDeptLabel.setText("所在部门:");
final GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridy = 0;
gridBagConstraints.gridx = 0;
contentPanel.add(inDeptLabel, gridBagConstraints);
inDeptTextField = new JTextField();
inDeptTextField.setEditable(false);
inDeptTextField.setColumns(11);
final GridBagConstraints gridBagConstraints_9 = new GridBagConstraints();
gridBagConstraints_9.gridy = 0;
gridBagConstraints_9.gridx = 1;
contentPanel.add(inDeptTextField, gridBagConstraints_9);
final JButton inDeptTreeButton = new JButton();// 创建按钮对象
inDeptTreeButton.setMargin(new Insets(0, 6, 0, 3));
inDeptTreeButton.addActionListener(new ActionListener() {// 捕获按钮事件
public void actionPerformed(ActionEvent e) {
DeptTreeDialog deptTree = new DeptTreeDialog(
inDeptTextField);// 创建部门选取对话框
deptTree.setBounds(375, 317, 101, 175);// 设置部门选取对话框的显示位置
deptTree.setVisible(true);// 弹出部门选取对话框
TbDept dept = (TbDept) dao
.queryDeptByName(inDeptTextField.getText());// 检索选中的部门对象
personnalComboBox.removeAllItems();// 清空“考勤员工”下拉菜单中的所有选项
personnalComboBox.addItem("请选择"); // 添加提示项
// 通过Hibernate的一对多关联获得与该部门关联的职务信息对象
Iterator dutyInfoIt = dept.getTbDutyInfos().iterator();
while (dutyInfoIt.hasNext()) {// 遍历职务信息对象
TbDutyInfo dutyInfo = (TbDutyInfo) dutyInfoIt
.next();// 获得职务信息对象
TbRecord tbRecord = dutyInfo.getTbRecord();// 通过Hibernate的一对一关联获得与职务信息对象关联的档案信息对象
personnalComboBox.addItem(tbRecord
.getRecordNumber()
+ " " + tbRecord.getName());// 将该员工添加到“考勤员工”下拉菜单中
}
}
});
inDeptTreeButton.setText("...");
final GridBagConstraints gridBagConstraints_21 = new GridBagConstraints();
gridBagConstraints_21.gridy = 0;
gridBagConstraints_21.gridx = 2;
contentPanel.add(inDeptTreeButton, gridBagConstraints_21);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -