package net.tqm.view;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.tqm.control.TimeRecording;
import net.tqm.util.DayChooser;
public class TimeRecordRespective extends JFrame implements ActionListener {
/**
* @author tqm
*/
private static final long serialVersionUID = 8831501534638274287L;
private JLabel lblday, lblendTime;
private JTextField jtfDay;
private String cardFeedingTime[] = { "7:30", "7:45", "8:00", "8:30",
"8:45", "9:00", "9:45" };
private JComboBox jcbCardFeedingTime;
private JTextArea jtaHistoryData;
private JButton jbtnSubmit,jbtnQuery;
private DayChooser chooser;
public TimeRecordRespective() {
this.getLayout();
this.setLayout(new BorderLayout());
JPanel timeRecordPanel = new JPanel();
JPanel timeRecordViewPanel = new JPanel();
timeRecordPanel.setLayout(new FlowLayout());
timeRecordViewPanel.setLayout(new FlowLayout());
jbtnSubmit = new JButton("提 交");
jbtnQuery = new JButton("历史数据");
jtaHistoryData = new JTextArea(10, 20);
lblday = new JLabel("overtime日期:");
lblendTime = new JLabel("终 时:");
jcbCardFeedingTime = new JComboBox(cardFeedingTime);
chooser = DayChooser.getInstance(new Date());
jtfDay = new JTextField();
jtfDay.setText(" ");
chooser.register(jtfDay);
timeRecordPanel.add(lblday);
timeRecordPanel.add(chooser);
timeRecordPanel.add(lblendTime);
timeRecordPanel.add(jcbCardFeedingTime);
timeRecordPanel.add(jbtnSubmit);
timeRecordPanel.add(jbtnQuery);
timeRecordViewPanel.add(jtaHistoryData);
jbtnSubmit.addActionListener(this);
jbtnQuery.addActionListener(this);
add(timeRecordPanel,BorderLayout.CENTER);
add(timeRecordViewPanel,BorderLayout.EAST);
setLocation(400, 300);
setSize(400, 400);
setTitle("overtime日期及时间记录");
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int flag = JOptionPane.showConfirmDialog(null,
"确认关闭?", "确认!", JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if (JOptionPane.YES_OPTION == flag) {
System.exit(0);
} else {
return;
}
}
});
pack();
setVisible(true);
}
public static void main(String[] args) {
new TimeRecordRespective();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == this.jbtnSubmit) {
TimeRecording.insertRecord(this.jtfDay.getText(),
(String) this.jcbCardFeedingTime.getSelectedItem());
this.jtfDay.setText("");
}
else if (e.getSource() == this.jbtnQuery) {
TimeRecording.queryHistoryRecord(jtaHistoryData);
jtfDay.setText(" ");
}
}
}
package net.tqm.control;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import net.tqm.util.DBUtil;
import net.tqm.util.OrderedLinkedHashMap;
public class TimeRecording {
static File file = new File("db/timeRecord");
@SuppressWarnings("unchecked")
static HashMap<String, String> historyRecord = (HashMap<String, String>) DBUtil.readFromFile(file);
public static void insertRecord(String key, String value){
if(key.trim().equals("")){
JOptionPane.showMessageDialog(null, "请选择日期!");
return;
}
if(historyRecord==null){
historyRecord = new LinkedHashMap<String, String>();
}
historyRecord.put(key, value);
historyRecord = OrderedLinkedHashMap.getOrder(historyRecord);
DBUtil.writeToFile(historyRecord, file);
}
public static void queryHistoryRecord(JTextArea textArea) {
if(historyRecord.isEmpty())
{
textArea.setText("");
JOptionPane.showMessageDialog(null, "没有历史数据!");
return;
}
textArea.setText("");
Iterator<String> dayIterator = historyRecord.keySet().iterator();
while (dayIterator.hasNext()) {
String key = dayIterator.next();
textArea.append(key + " ==" + historyRecord.get(key)+"\n");
}
}
public static void timeAmount(){//TODO 统计overtime总时间,分月份统计,修改时间,删除时间等
}
}
package net.tqm.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class DBUtil {
public static void writeToFile(Object o,File file) {
try {
ObjectOutputStream w = new ObjectOutputStream(new FileOutputStream(
file));
w.writeObject(o);
w.flush();
w.close();
} catch (Exception e) {
}
}
public static Object readFromFile(File file) {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
file));
Object o = in.readObject();
in.close();
return o;
} catch (Exception e) {
}
return null;
}
}
package net.tqm.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class OrderedLinkedHashMap {
public static LinkedHashMap<String, String> getOrder(Map<String, String> map){
List<Map.Entry<String, String>> infoIds =new ArrayList<Map.Entry<String, String>>(map.entrySet());
//排序
Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
String p1 = (String) o1.getKey();
String p2 = (String) o2.getKey();
return p1.compareTo(p2);
}
});
/*转换成新map输出*/
LinkedHashMap<String, String> newMap = new LinkedHashMap <String, String>();
for(Map.Entry<String,String> entity : infoIds){
newMap.put(entity.getKey(), entity.getValue());
}
return newMap;
}
}