package org.hwo.pulscounter.ui; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.FlowLayout; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.border.EmptyBorder; import java.awt.Dialog.ModalExclusionType; import java.awt.Dialog.ModalityType; import java.awt.GridBagLayout; import javax.swing.JSplitPane; import java.awt.GridBagConstraints; import org.hwo.ObjectTable; import org.hwo.i18n.Messages; import org.hwo.pulscounter.PulsCounterApplication; import org.hwo.pulscounter.SnapShot; import org.hwo.ui.JMappedTable; import org.hwo.ui.MousePopupListener; import org.hwo.ui.diagram.AnnotatedPlotProvider; import org.hwo.ui.diagram.LinePlotPainter; import org.hwo.ui.diagram.PlotLabeler; import org.hwo.ui.diagram.SimplePlotProvider; import javax.swing.JScrollPane; import javax.swing.JList; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.lang.management.GarbageCollectorMXBean; import java.awt.event.ActionEvent; import javax.swing.ScrollPaneConstants; import javax.swing.ListSelectionModel; import java.awt.Insets; import java.awt.MenuItem; import javax.swing.JTabbedPane; import javax.swing.BoxLayout; import org.hwo.ui.JDiagram; import javax.swing.JCheckBox; import java.awt.Dimension; public class SnapshotManager extends JDialog { public static Color[] plotColors = new Color[32]; public static void show(Component parent){ SnapshotManager sm = new SnapshotManager(); if (parent != null) { sm.setLocationRelativeTo(parent); } sm.setVisible(true); } private final JPanel contentPanel = new JPanel(); private JList lDevices; private JMappedTable mtSnapshots; private JScrollPane scrollPane_1; private JDiagram diaSnapshots; private JCheckBox cbDifferenzen; private JPanel paIndex; private JLabel[] graphLabels; private AnnotatedPlotProvider annotatedPlotProvider; /** * Create the dialog. */ public SnapshotManager() { setTitle(Messages.getString("Aufgezeichnete Werte verwalten")); setModal(true); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setModalityType(ModalityType.APPLICATION_MODAL); setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE); setBounds(100, 100, 991, 547); getContentPane().setLayout(new BorderLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); GridBagLayout gbl_contentPanel = new GridBagLayout(); gbl_contentPanel.columnWidths = new int[]{0, 0}; gbl_contentPanel.rowHeights = new int[]{0, 0}; gbl_contentPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE}; gbl_contentPanel.rowWeights = new double[]{1.0, Double.MIN_VALUE}; contentPanel.setLayout(gbl_contentPanel); { JSplitPane splitPane = new JSplitPane(); splitPane.setResizeWeight(0.2); GridBagConstraints gbc_splitPane = new GridBagConstraints(); gbc_splitPane.fill = GridBagConstraints.BOTH; gbc_splitPane.gridx = 0; gbc_splitPane.gridy = 0; contentPanel.add(splitPane, gbc_splitPane); { JPanel panel = new JPanel(); splitPane.setLeftComponent(panel); GridBagLayout gbl_panel = new GridBagLayout(); gbl_panel.columnWidths = new int[]{0, 0}; gbl_panel.rowHeights = new int[]{0, 0}; gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE}; gbl_panel.rowWeights = new double[]{1.0, Double.MIN_VALUE}; panel.setLayout(gbl_panel); { JScrollPane scrollPane = new JScrollPane(); GridBagConstraints gbc_scrollPane = new GridBagConstraints(); gbc_scrollPane.fill = GridBagConstraints.BOTH; gbc_scrollPane.gridx = 0; gbc_scrollPane.gridy = 0; panel.add(scrollPane, gbc_scrollPane); { lDevices = new JList(); lDevices.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { showSnapshots(); } }); scrollPane.setViewportView(lDevices); } } } { JPanel panel = new JPanel(); splitPane.setRightComponent(panel); GridBagLayout gbl_panel = new GridBagLayout(); gbl_panel.columnWidths = new int[]{0, 0}; gbl_panel.rowHeights = new int[] {0}; gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE}; gbl_panel.rowWeights = new double[]{1.0}; panel.setLayout(gbl_panel); { JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); GridBagConstraints gbc_tabbedPane = new GridBagConstraints(); gbc_tabbedPane.fill = GridBagConstraints.BOTH; gbc_tabbedPane.gridx = 0; gbc_tabbedPane.gridy = 0; panel.add(tabbedPane, gbc_tabbedPane); { JPanel panel_1 = new JPanel(); tabbedPane.addTab("Tabelle", null, panel_1, null); panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS)); { scrollPane_1 = new JScrollPane(); panel_1.add(scrollPane_1); scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); { mtSnapshots = new JMappedTable(SnapShot.class); mtSnapshots.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); mtSnapshots.setFillsViewportHeight(true); mtSnapshots.setColumnSelectionAllowed(true); scrollPane_1.setViewportView(mtSnapshots); } } } { JPanel panel_1 = new JPanel(); tabbedPane.addTab("Diagramm", null, panel_1, null); tabbedPane.setBackgroundAt(1, Color.WHITE); panel_1.setLayout(new BorderLayout(0, 0)); { diaSnapshots = new JDiagram(); diaSnapshots.setBackground(Color.WHITE); diaSnapshots.setPreferLabelHints(true); diaSnapshots.setDrawVerticalGrid(true); diaSnapshots.setAutoScale(true); panel_1.add(diaSnapshots); } { JPanel panel_2 = new JPanel(); panel_1.add(panel_2, BorderLayout.NORTH); GridBagLayout gbl_panel_2 = new GridBagLayout(); gbl_panel_2.columnWidths = new int[]{0, 0}; gbl_panel_2.rowHeights = new int[]{0, 0}; gbl_panel_2.columnWeights = new double[]{0.0, Double.MIN_VALUE}; gbl_panel_2.rowWeights = new double[]{0.0, Double.MIN_VALUE}; panel_2.setLayout(gbl_panel_2); { cbDifferenzen = new JCheckBox(Messages.getString("Differenzieren")); cbDifferenzen.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { for (int i=0;i<32;i++){ annotatedPlotProvider.setPlotDifferentiated(i, cbDifferenzen.isSelected()); } diaSnapshots.repaint(); } }); GridBagConstraints gbc_cbDifferenzen = new GridBagConstraints(); gbc_cbDifferenzen.gridx = 0; gbc_cbDifferenzen.gridy = 0; panel_2.add(cbDifferenzen, gbc_cbDifferenzen); } } { paIndex = new JPanel(); paIndex.setBackground(Color.WHITE); paIndex.setPreferredSize(new Dimension(80, 10)); panel_1.add(paIndex, BorderLayout.EAST); paIndex.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 5)); } } } } } { JPanel buttonPane = new JPanel(); buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); getContentPane().add(buttonPane, BorderLayout.SOUTH); { JButton cancelButton = new JButton(Messages.getString("schliessen")); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); } }); cancelButton.setActionCommand(Messages.getString("Cancel")); buttonPane.add(cancelButton); } } initialize(); } private void initializeDeviceList(){ initializeDeviceList(null); } private void initializeDeviceList(Integer preSelected){ Integer[] deviceSerials = PulsCounterApplication.getApplication().getDatabase().getKnownDevices(); DefaultListModel lm = new DefaultListModel<>(); for (Integer serial: deviceSerials){ lm.addElement(serial); } lDevices.setModel(lm); lDevices.setSelectedValue(preSelected, true); } private void initialize(){ graphLabels = new JLabel[32]; initializeDeviceList(); mtSnapshots.getTableMapper().setReadOnly(true); mtSnapshots.getTableMapper().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { updateDiagram(); } }); for (int i=0;i<32;i++){ final int ii = i; JLabel l = new JLabel(String.format("#%02d", i)); l.setForeground(plotColors[i]); l.addMouseListener(new MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent e) { if (l.isEnabled()){ l.setEnabled(false); annotatedPlotProvider.setPlotEnabled(ii, false); } else { l.setEnabled(true); annotatedPlotProvider.setPlotEnabled(ii, true); } diaSnapshots.repaint(); }; }); l.addMouseListener(new MousePopupListener() { @Override public void popupTriggered(int x, int y) { JPopupMenu popup = new JPopupMenu(); JMenuItem mi = new JMenuItem(Messages.getString("Nur diesen Graph zeigen")); mi.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for (int g=0;g<32;g++){ graphLabels[g].setEnabled(false); annotatedPlotProvider.setPlotEnabled(g, false); } graphLabels[ii].setEnabled(true); annotatedPlotProvider.setPlotEnabled(ii,true); diaSnapshots.repaint(); } }); popup.add(mi); mi = new JMenuItem(Messages.getString("Alle ausser diesem Graph zeigen")); mi.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for (int g=0;g<32;g++){ graphLabels[g].setEnabled(true); annotatedPlotProvider.setPlotEnabled(g, true); } graphLabels[ii].setEnabled(false); annotatedPlotProvider.setPlotEnabled(ii, false); diaSnapshots.repaint(); } }); popup.add(mi); popup.show(l, x, y); } }); paIndex.add(l); graphLabels[i] = l; } mtSnapshots.addMouseListener(new MousePopupListener() { @Override public void popupTriggered(int x, int y) { SnapShot[] selRows = mtSnapshots.getTableMapper().getSelectedRows(new SnapShot[0]); if ((selRows != null)&&(selRows.length>0)){ JPopupMenu popupMenu = new JPopupMenu(); JMenuItem mi = new JMenuItem(Messages.getString("Entfernen...")); mi.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (JOptionPane.showConfirmDialog(SnapshotManager.this, Messages.getString("Sollen die markierten Snapshots wirklich gelöscht werden?"),Messages.getString("Aufzeichnungen entfernen"),JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){ for (SnapShot ss: selRows){ mtSnapshots.getTableMapper().removeRow(ss); PulsCounterApplication.getApplication().getDatabase().deleteSnapshot(ss); } if (mtSnapshots.getTableMapper().getRows().size()==0){ initializeDeviceList(); } mtSnapshots.repaint(); } } }); popupMenu.add(mi); mi = new JMenuItem(Messages.getString("(Ent-)Parken")); mi.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for (SnapShot ss: selRows){ mtSnapshots.getTableMapper().removeRow(ss); PulsCounterApplication.getApplication().getDatabase().deleteSnapshot(ss); ss.setDeviceSerial(-ss.getDeviceSerial()); PulsCounterApplication.getApplication().getDatabase().storeSnapshots(new SnapShot[]{ss}); } initializeDeviceList((Integer)lDevices.getSelectedValue()); } }); popupMenu.add(mi); popupMenu.show(mtSnapshots, x, y); } } }); annotatedPlotProvider = new AnnotatedPlotProvider(SnapShot.class); for (int i=0;i<32;i++){ annotatedPlotProvider.setColors(i, plotColors[i]); } diaSnapshots.setPlotProvider(annotatedPlotProvider); diaSnapshots.setAbszissLabeler(new PlotLabeler() { @Override public String getOrdinateLabel(JDiagram diagram, int ordinate, Double value) { return null; } @Override public String getAbzisseLabel(JDiagram diagram, Double pos) { return ((SnapShot)annotatedPlotProvider.getValues().get(pos.intValue())).getTimeStampFormated(); } }); if (lDevices.getModel().getSize()>0) lDevices.setSelectedIndex(0); } private void showSnapshots(){ Integer deviceSerial = (Integer)lDevices.getSelectedValue(); if (deviceSerial != null){ SnapShot[] snapshots = PulsCounterApplication.getApplication().getDatabase().loadSnapshots(0, deviceSerial); mtSnapshots.getTableMapper().setRows(snapshots); } else { mtSnapshots.getTableMapper().clear(); } updateDiagram(); } private void updateDiagram(){ SnapShot[] rows = mtSnapshots.getTableMapper().getSelectedRows(new SnapShot[0]); if ((rows == null)||(rows.length==0)){ rows = mtSnapshots.getTableMapper().getRows(SnapShot.class).toArray(new SnapShot[0]); } annotatedPlotProvider.setValues(rows); diaSnapshots.repaint(); } static { plotColors[0] = new Color(255,0,0); plotColors[1] = new Color(0,0,255); plotColors[2] = new Color(0,255,0); plotColors[3] = new Color(255,0,255); plotColors[4] = new Color(0,255,255); plotColors[5] = new Color(255,255,0); plotColors[6] = new Color(192,192,0); plotColors[7] = new Color(0,192,192); int a = 2, b = 4; for (int i=0;i<3;i++){ for (int j=0;j<8;j++){ plotColors[8 + (i*8) + j] = new Color( plotColors[j].getRed() * a / b, plotColors[j].getBlue() * a / b, plotColors[j].getGreen() * a / b ); } } } }