package org.hwo.pulscounter; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; import java.awt.BorderLayout; import javax.swing.JButton; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import javax.swing.JLabel; import javax.swing.JComboBox; import org.hwo.csv.CSV; import org.hwo.io.SerialPort; import org.hwo.models.TableMapper.AbstractTableMapperListener; import org.hwo.models.TableMapper.TableMapper; import java.awt.Insets; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.prefs.BackingStoreException; import java.util.prefs.Preferences; import javax.swing.JTable; import javax.swing.JScrollPane; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.border.TitledBorder; import javax.swing.JSpinner; import javax.swing.SpinnerNumberModel; import javax.swing.border.BevelBorder; import javax.swing.SwingConstants; import javax.swing.event.ChangeListener; import javax.swing.event.ChangeEvent; import javax.swing.JTabbedPane; import javax.swing.BoxLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import org.hwo.datetime.JTimeOfDay; public class PulsCounterWindow extends JFrame{ private JComboBox cbInterfaces; private JTable tCounter; private TableMapper tmCounter; private Preferences prefs; private List counterChannels; private final JPanel panel_7 = new JPanel(); private JLabel lDeviceType; private JLabel lDeviceTime; private JLabel lActivity; Thread threadUpdater; Timer updateTimer; private JSpinner spUpdateIntervall; boolean applicationExiting; private PulsCounterDevice pulsCounterDevice; private JComboBox cbTimeBarriers; private JTable tTBChannels; private JTimeOfDay todTimebarrier; private TableMapper tmTBChannels; public PulsCounterWindow() { addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent arg0) { applicationExiting = true; } }); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 930, 592); setTitle("PulsCounter"); GridBagLayout gridBagLayout = new GridBagLayout(); gridBagLayout.columnWidths = new int[]{0, 0}; gridBagLayout.rowHeights = new int[]{0, 0, 0, 0}; gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE}; gridBagLayout.rowWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE}; getContentPane().setLayout(gridBagLayout); JPanel panel = new JPanel(); GridBagConstraints gbc_panel = new GridBagConstraints(); gbc_panel.insets = new Insets(0, 0, 5, 0); gbc_panel.fill = GridBagConstraints.BOTH; gbc_panel.gridx = 0; gbc_panel.gridy = 0; getContentPane().add(panel, gbc_panel); GridBagLayout gbl_panel = new GridBagLayout(); gbl_panel.columnWidths = new int[]{0, 0, 0, 0, 0, 0}; gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0}; gbl_panel.columnWeights = new double[]{1.0, 1.0, 0.0, 1.0, 0.0, Double.MIN_VALUE}; gbl_panel.rowWeights = new double[]{1.0, 1.0, 1.0, 0.0, 1.0, Double.MIN_VALUE}; panel.setLayout(gbl_panel); JPanel panel_1 = new JPanel(); panel_1.setBorder(new TitledBorder(null, "Anschluss", TitledBorder.LEADING, TitledBorder.TOP, null, null)); GridBagConstraints gbc_panel_1 = new GridBagConstraints(); gbc_panel_1.gridwidth = 3; gbc_panel_1.insets = new Insets(0, 0, 5, 5); gbc_panel_1.fill = GridBagConstraints.BOTH; gbc_panel_1.gridx = 0; gbc_panel_1.gridy = 0; panel.add(panel_1, gbc_panel_1); GridBagLayout gbl_panel_1 = new GridBagLayout(); gbl_panel_1.columnWidths = new int[]{0, 0, 0}; gbl_panel_1.rowHeights = new int[]{0, 0}; gbl_panel_1.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE}; gbl_panel_1.rowWeights = new double[]{0.0, Double.MIN_VALUE}; panel_1.setLayout(gbl_panel_1); JLabel lblNewLabel = new JLabel("Schnittstelle:"); GridBagConstraints gbc_lblNewLabel = new GridBagConstraints(); gbc_lblNewLabel.insets = new Insets(0, 0, 0, 5); gbc_lblNewLabel.gridx = 0; gbc_lblNewLabel.gridy = 0; panel_1.add(lblNewLabel, gbc_lblNewLabel); cbInterfaces = new JComboBox(); GridBagConstraints gbc_cbInterfaces = new GridBagConstraints(); gbc_cbInterfaces.fill = GridBagConstraints.HORIZONTAL; gbc_cbInterfaces.gridx = 1; gbc_cbInterfaces.gridy = 0; panel_1.add(cbInterfaces, gbc_cbInterfaces); cbInterfaces.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { System.err.println("Save Pref PORT"); prefs.put("io.port", cbInterfaces.getSelectedItem().toString()); try { prefs.sync(); } catch (Exception e) { System.err.println("Exception: " + e); e.printStackTrace(); } setSerialPort(); } }); JPanel panel_2 = new JPanel(); panel_2.setBorder(new TitledBorder(null, "Einstellungen", TitledBorder.LEADING, TitledBorder.TOP, null, null)); GridBagConstraints gbc_panel_2 = new GridBagConstraints(); gbc_panel_2.gridwidth = 2; gbc_panel_2.insets = new Insets(0, 0, 5, 0); gbc_panel_2.fill = GridBagConstraints.BOTH; gbc_panel_2.gridx = 3; gbc_panel_2.gridy = 0; panel.add(panel_2, gbc_panel_2); GridBagLayout gbl_panel_2 = new GridBagLayout(); gbl_panel_2.columnWidths = new int[]{0, 0, 0}; gbl_panel_2.rowHeights = new int[]{0, 0}; gbl_panel_2.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE}; gbl_panel_2.rowWeights = new double[]{0.0, Double.MIN_VALUE}; panel_2.setLayout(gbl_panel_2); JLabel lblAktualisierungsintervall = new JLabel("Aktualisierungsintervall:"); GridBagConstraints gbc_lblAktualisierungsintervall = new GridBagConstraints(); gbc_lblAktualisierungsintervall.insets = new Insets(0, 0, 0, 5); gbc_lblAktualisierungsintervall.gridx = 0; gbc_lblAktualisierungsintervall.gridy = 0; panel_2.add(lblAktualisierungsintervall, gbc_lblAktualisierungsintervall); spUpdateIntervall = new JSpinner(); spUpdateIntervall.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent arg0) { if (updateTimer != null) { Integer updateIntervall = (Integer)spUpdateIntervall.getValue(); updateTimer.stop(); updateTimer.setDelay(updateIntervall * 1000); updateTimer.start(); prefs.putInt("update.intervall", updateIntervall); try { prefs.flush(); } catch (BackingStoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); spUpdateIntervall.setModel(new SpinnerNumberModel(15, 5, 3600, 1)); GridBagConstraints gbc_spUpdateIntervall = new GridBagConstraints(); gbc_spUpdateIntervall.anchor = GridBagConstraints.EAST; gbc_spUpdateIntervall.gridx = 1; gbc_spUpdateIntervall.gridy = 0; panel_2.add(spUpdateIntervall, gbc_spUpdateIntervall); JPanel panel_3 = new JPanel(); panel_3.setBorder(new TitledBorder(null, "Aktionen", TitledBorder.LEADING, TitledBorder.TOP, null, null)); GridBagConstraints gbc_panel_3 = new GridBagConstraints(); gbc_panel_3.gridwidth = 5; gbc_panel_3.insets = new Insets(0, 0, 5, 5); gbc_panel_3.fill = GridBagConstraints.BOTH; gbc_panel_3.gridx = 0; gbc_panel_3.gridy = 3; panel.add(panel_3, gbc_panel_3); GridBagLayout gbl_panel_3 = new GridBagLayout(); gbl_panel_3.columnWidths = new int[]{0, 0, 0, 0, 0}; gbl_panel_3.rowHeights = new int[]{0, 0}; gbl_panel_3.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; gbl_panel_3.rowWeights = new double[]{0.0, Double.MIN_VALUE}; panel_3.setLayout(gbl_panel_3); JButton btnNewButton = new JButton("Jetzt aktualisieren..."); GridBagConstraints gbc_btnNewButton = new GridBagConstraints(); gbc_btnNewButton.insets = new Insets(0, 0, 0, 5); gbc_btnNewButton.gridx = 1; gbc_btnNewButton.gridy = 0; panel_3.add(btnNewButton, gbc_btnNewButton); JButton btnTagesdateiSchreiben = new JButton("Tagesdatei schreiben..."); GridBagConstraints gbc_btnTagesdateiSchreiben = new GridBagConstraints(); gbc_btnTagesdateiSchreiben.insets = new Insets(0, 0, 0, 5); gbc_btnTagesdateiSchreiben.gridx = 2; gbc_btnTagesdateiSchreiben.gridy = 0; panel_3.add(btnTagesdateiSchreiben, gbc_btnTagesdateiSchreiben); btnTagesdateiSchreiben.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { writeDayFile(); } }); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { synchronized (threadUpdater) { threadUpdater.notifyAll(); } } }); JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); GridBagConstraints gbc_tabbedPane = new GridBagConstraints(); gbc_tabbedPane.insets = new Insets(0, 0, 5, 0); gbc_tabbedPane.fill = GridBagConstraints.BOTH; gbc_tabbedPane.gridx = 0; gbc_tabbedPane.gridy = 1; getContentPane().add(tabbedPane, gbc_tabbedPane); JPanel panel_8 = new JPanel(); tabbedPane.addTab("Aktuelle Werte", null, panel_8, null); panel_8.setLayout(new BoxLayout(panel_8, BoxLayout.X_AXIS)); JScrollPane scrollPane = new JScrollPane(); panel_8.add(scrollPane); tCounter = new JTable(); scrollPane.setViewportView(tCounter); JPanel panel_9 = new JPanel(); tabbedPane.addTab("Tageszeiterfassung", null, panel_9, null); GridBagLayout gbl_panel_9 = new GridBagLayout(); gbl_panel_9.columnWidths = new int[]{0, 0, 0}; gbl_panel_9.rowHeights = new int[]{0, 0, 0, 0}; gbl_panel_9.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE}; gbl_panel_9.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE}; panel_9.setLayout(gbl_panel_9); JLabel lblNewLabel_1 = new JLabel("Speicher:"); GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints(); gbc_lblNewLabel_1.anchor = GridBagConstraints.WEST; gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5); gbc_lblNewLabel_1.gridx = 0; gbc_lblNewLabel_1.gridy = 0; panel_9.add(lblNewLabel_1, gbc_lblNewLabel_1); cbTimeBarriers = new JComboBox(); cbTimeBarriers.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { updateTimeBarrierView(); } }); GridBagConstraints gbc_cbTimeBarriers = new GridBagConstraints(); gbc_cbTimeBarriers.insets = new Insets(0, 0, 5, 0); gbc_cbTimeBarriers.fill = GridBagConstraints.HORIZONTAL; gbc_cbTimeBarriers.gridx = 1; gbc_cbTimeBarriers.gridy = 0; panel_9.add(cbTimeBarriers, gbc_cbTimeBarriers); JPanel panel_10 = new JPanel(); panel_10.setBorder(new TitledBorder(null, "Einstellungen", TitledBorder.LEADING, TitledBorder.TOP, null, null)); GridBagConstraints gbc_panel_10 = new GridBagConstraints(); gbc_panel_10.insets = new Insets(0, 0, 5, 0); gbc_panel_10.gridwidth = 2; gbc_panel_10.fill = GridBagConstraints.BOTH; gbc_panel_10.gridx = 0; gbc_panel_10.gridy = 1; panel_9.add(panel_10, gbc_panel_10); GridBagLayout gbl_panel_10 = new GridBagLayout(); gbl_panel_10.columnWidths = new int[]{0, 0, 0}; gbl_panel_10.rowHeights = new int[]{0, 0}; gbl_panel_10.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE}; gbl_panel_10.rowWeights = new double[]{1.0, Double.MIN_VALUE}; panel_10.setLayout(gbl_panel_10); JLabel lblDdd = new JLabel("Werte erfassen bis:"); GridBagConstraints gbc_lblDdd = new GridBagConstraints(); gbc_lblDdd.anchor = GridBagConstraints.WEST; gbc_lblDdd.insets = new Insets(0, 0, 0, 5); gbc_lblDdd.fill = GridBagConstraints.VERTICAL; gbc_lblDdd.gridx = 0; gbc_lblDdd.gridy = 0; panel_10.add(lblDdd, gbc_lblDdd); todTimebarrier = new JTimeOfDay(); todTimebarrier.setSecondsHidden(true); todTimebarrier.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { repaint(); } }); GridBagConstraints gbc_todTimebarrier = new GridBagConstraints(); gbc_todTimebarrier.anchor = GridBagConstraints.WEST; gbc_todTimebarrier.fill = GridBagConstraints.VERTICAL; gbc_todTimebarrier.gridx = 1; gbc_todTimebarrier.gridy = 0; panel_10.add(todTimebarrier, gbc_todTimebarrier); JPanel panel_11 = new JPanel(); panel_11.setBorder(new TitledBorder(null, "Aktuelle Werte", TitledBorder.LEADING, TitledBorder.TOP, null, null)); GridBagConstraints gbc_panel_11 = new GridBagConstraints(); gbc_panel_11.gridwidth = 2; gbc_panel_11.fill = GridBagConstraints.BOTH; gbc_panel_11.gridx = 0; gbc_panel_11.gridy = 2; panel_9.add(panel_11, gbc_panel_11); GridBagLayout gbl_panel_11 = new GridBagLayout(); gbl_panel_11.columnWidths = new int[]{0, 0}; gbl_panel_11.rowHeights = new int[]{0, 0}; gbl_panel_11.columnWeights = new double[]{1.0, Double.MIN_VALUE}; gbl_panel_11.rowWeights = new double[]{1.0, Double.MIN_VALUE}; panel_11.setLayout(gbl_panel_11); JScrollPane scrollPane_1 = new JScrollPane(); GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints(); gbc_scrollPane_1.fill = GridBagConstraints.BOTH; gbc_scrollPane_1.gridx = 0; gbc_scrollPane_1.gridy = 0; panel_11.add(scrollPane_1, gbc_scrollPane_1); tTBChannels = new JTable(); scrollPane_1.setViewportView(tTBChannels); JPanel panel_4 = new JPanel(); GridBagConstraints gbc_panel_4 = new GridBagConstraints(); gbc_panel_4.fill = GridBagConstraints.BOTH; gbc_panel_4.gridx = 0; gbc_panel_4.gridy = 2; getContentPane().add(panel_4, gbc_panel_4); GridBagLayout gbl_panel_4 = new GridBagLayout(); gbl_panel_4.columnWidths = new int[]{0, 0, 0, 0}; gbl_panel_4.rowHeights = new int[]{0, 0}; gbl_panel_4.columnWeights = new double[]{1.0, 0.0, 0.0, Double.MIN_VALUE}; gbl_panel_4.rowWeights = new double[]{0.0, Double.MIN_VALUE}; panel_4.setLayout(gbl_panel_4); JPanel panel_6 = new JPanel(); panel_6.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null)); GridBagConstraints gbc_panel_6 = new GridBagConstraints(); gbc_panel_6.insets = new Insets(0, 0, 0, 5); gbc_panel_6.fill = GridBagConstraints.BOTH; gbc_panel_6.gridx = 0; gbc_panel_6.gridy = 0; panel_4.add(panel_6, gbc_panel_6); GridBagLayout gbl_panel_6 = new GridBagLayout(); gbl_panel_6.columnWidths = new int[]{348, 0}; gbl_panel_6.rowHeights = new int[]{16, 0}; gbl_panel_6.columnWeights = new double[]{1.0, Double.MIN_VALUE}; gbl_panel_6.rowWeights = new double[]{1.0, Double.MIN_VALUE}; panel_6.setLayout(gbl_panel_6); lActivity = new JLabel("Verbindungsaktivit\u00E4t"); lActivity.setToolTipText("Aktuelle Aufgabe"); lActivity.setHorizontalAlignment(SwingConstants.LEFT); GridBagConstraints gbc_lActivity = new GridBagConstraints(); gbc_lActivity.insets = new Insets(0, 5, 0, 0); gbc_lActivity.anchor = GridBagConstraints.WEST; gbc_lActivity.gridx = 0; gbc_lActivity.gridy = 0; panel_6.add(lActivity, gbc_lActivity); JPanel panel_5 = new JPanel(); panel_5.setToolTipText("Angeschlossener Ger\u00E4tetyp"); panel_5.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null)); GridBagConstraints gbc_panel_5 = new GridBagConstraints(); gbc_panel_5.insets = new Insets(0, 0, 0, 5); gbc_panel_5.fill = GridBagConstraints.VERTICAL; gbc_panel_5.gridx = 1; gbc_panel_5.gridy = 0; panel_4.add(panel_5, gbc_panel_5); lDeviceType = new JLabel("----"); panel_5.add(lDeviceType); GridBagConstraints gbc_panel_7 = new GridBagConstraints(); gbc_panel_7.gridx = 2; gbc_panel_7.gridy = 0; panel_7.setToolTipText("Ger\u00E4tezeit bei letztem Kontakt"); panel_4.add(panel_7, gbc_panel_7); panel_7.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null)); lDeviceTime = new JLabel("----"); panel_7.add(lDeviceTime); initialize(); } private void initialize() { applicationExiting = false; pulsCounterDevice = new PulsCounterDevice(); prefs = Preferences.userRoot(); String defaultPort = prefs.get("io.port", "COM1"); for (String portName:SerialPort.getPortNames()) cbInterfaces.addItem(portName); // cbInterfaces.addItem("dummy:"); System.err.println("Default Port: " + defaultPort); for (int i=0;i < cbInterfaces.getItemCount();i++) { if (cbInterfaces.getItemAt(i).equals(defaultPort)) cbInterfaces.setSelectedIndex(i); } tmCounter = new TableMapper(CounterChannel.class, tCounter); tmCounter.setRows(pulsCounterDevice.getChannels()); tmCounter.getColumnInfo().get(0).setReadOnly(true); tmCounter.getColumnInfo().get(1).setReadOnly(true); tmCounter.getColumnInfo().get(3).setReadOnly(true); tmCounter.addTableMapperListener(new AbstractTableMapperListener() { @Override public void ValueChanged(int row, int column) { if (column == 2) { saveCorrection(row); } } }); tmTBChannels = new TableMapper(CounterChannel.class, tTBChannels); tmTBChannels.getColumnInfo().get(0).setReadOnly(true); tmTBChannels.getColumnInfo().get(1).setReadOnly(true); tmTBChannels.getColumnInfo().get(3).setReadOnly(true); threadUpdater = new Thread(new Runnable() { @Override public void run() { while (true) { if (applicationExiting) return; try { synchronized (threadUpdater) { threadUpdater.wait(); } updateCounter(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); threadUpdater.start(); spUpdateIntervall.setValue(prefs.getInt("update.intervall", 15)); updateTimer = new Timer(prefs.getInt("update.intervall", 15)*1000, new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { synchronized (threadUpdater) { threadUpdater.notifyAll(); } } }); updateTimer.setRepeats(true); updateTimer.start(); } private void updateCounter() { lActivity.setText("Werte aktuelisieren..."); pulsCounterDevice.update(); lDeviceType.setText(pulsCounterDevice.getDeviceName()); lDeviceTime.setText(pulsCounterDevice.getDeviceTime().toLocaleString()); if (cbTimeBarriers.getItemCount()==0) { for (TimeBarrier tb: pulsCounterDevice.getTimeBarriers()) { cbTimeBarriers.addItem(tb); } } lActivity.setText("warten..."); tCounter.repaint(); } private void saveCorrection(int row) { System.err.println(String.format("Korrekturwert fŸr Kanal %d = %d",row + 1,tmCounter.getRow(row,CounterChannel.class).getCorrect())); } private void setSerialPort() { String serialPortName = cbInterfaces.getSelectedItem().toString(); SerialPort sport = SerialPort.newInstance(); sport.setPortName(serialPortName); pulsCounterDevice.setSerialPort(sport); } private void writeDayFile() { Date date = new Date(); /* CSV csv = new CSV(); String[] row = new String[this.timeBarriers.size() + 1]; row[0] = ""; for (int n=1;n