Java tutorial
/* * 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 pl.piotrsukiennik.jbrain.gui; import java.io.ByteArrayOutputStream; import org.springframework.stereotype.Component; import pl.piotrsukiennik.jbrain.dto.BrainDTO; import pl.piotrsukiennik.jbrain.dto.ConnectionsDTO; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import pl.piotrsukiennik.jbrain.provider.Serializer; /** * * @author Piotr Sukiennik */ public class ModelPanel extends javax.swing.JPanel { private Serializer<BrainDTO> brainSerializer; private BrainDTO brain; /** * Creates new form ModelPanel */ public ModelPanel(Serializer<BrainDTO> brainSerializer, BrainDTO brain) { this.brainSerializer = brainSerializer; initComponents(); setActiveBrain(brain); } /** * 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">//GEN-BEGIN:initComponents private void initComponents() { mainTabbedPane = new javax.swing.JTabbedPane(); jsonPanel = new javax.swing.JPanel(); jScrollPane2 = new javax.swing.JScrollPane(); jsonTextArea = new javax.swing.JTextArea(); connectionsPanel = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); connectionsTable = new javax.swing.JTable(); mainTabbedPane.setBackground(new java.awt.Color(255, 255, 255)); jsonTextArea.setColumns(20); jsonTextArea.setRows(5); jsonTextArea.addPropertyChangeListener(new java.beans.PropertyChangeListener() { public void propertyChange(java.beans.PropertyChangeEvent evt) { jsonTextAreaPropertyChange(evt); } }); jScrollPane2.setViewportView(jsonTextArea); javax.swing.GroupLayout jsonPanelLayout = new javax.swing.GroupLayout(jsonPanel); jsonPanel.setLayout(jsonPanelLayout); jsonPanelLayout .setHorizontalGroup(jsonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)); jsonPanelLayout .setVerticalGroup(jsonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)); mainTabbedPane.addTab("json", jsonPanel); jScrollPane1.setBackground(new java.awt.Color(255, 255, 255)); connectionsTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { })); jScrollPane1.setViewportView(connectionsTable); javax.swing.GroupLayout connectionsPanelLayout = new javax.swing.GroupLayout(connectionsPanel); connectionsPanel.setLayout(connectionsPanelLayout); connectionsPanelLayout.setHorizontalGroup( connectionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)); connectionsPanelLayout.setVerticalGroup( connectionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)); mainTabbedPane.addTab("connections table", connectionsPanel); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(mainTabbedPane)); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(mainTabbedPane)); }// </editor-fold>//GEN-END:initComponents private void jsonTextAreaPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jsonTextAreaPropertyChange }//GEN-LAST:event_jsonTextAreaPropertyChange protected void setActiveBrain(BrainDTO brain) { this.brain = brain; ByteArrayOutputStream baos = new ByteArrayOutputStream(); brainSerializer.write(baos, brain); String json = new String(baos.toByteArray()); jsonTextArea.setText(json); TableModel connectionsModel = convert2Model(brain.getConnections()); connectionsTable.setModel(connectionsModel); } protected TableModel convert2Model(ConnectionsDTO connectionsDTO) { String[] layers = connectionsDTO.getLayers(); double[][] connections = connectionsDTO.getConnections(); DefaultTableModel tableModel = new DefaultTableModel(layers.length, layers.length + 1); for (int i = 0; i < layers.length; i++) { tableModel.setValueAt(layers[i], i, 0); } for (int i = 0; i < connections.length; i++) { for (int j = 0; j < connections.length; j++) { double connection = connections[i][j]; if (connection != 0) { tableModel.setValueAt(connections[i][j], i, j + 1); } } } List<String> labels = new ArrayList<String>(); labels.add(""); labels.addAll(Arrays.asList(connectionsDTO.getLayers())); tableModel.setColumnIdentifiers(labels.toArray()); return tableModel; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel connectionsPanel; private javax.swing.JTable connectionsTable; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JPanel jsonPanel; private javax.swing.JTextArea jsonTextArea; private javax.swing.JTabbedPane mainTabbedPane; // End of variables declaration//GEN-END:variables }