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.edu.pw.appt; import java.awt.image.BufferedImage; import java.awt.image.RenderedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Paths; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.DefaultComboBoxModel; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.filechooser.FileNameExtensionFilter; import net.sourceforge.plantuml.SourceStringReader; import org.apache.commons.io.FilenameUtils; import pl.edu.pw.appt.network.UdpServer; import pl.edu.pw.appt.objects.Message; import pl.edu.pw.appt.objects.Messages; import pl.edu.pw.appt.utils.FileHandler; /** * * @author bestp */ public class GUI extends javax.swing.JFrame { Thread t; UdpServer server; BufferedImage image; public boolean pauseOn = false; /** * Creates new form GUI */ public GUI() { // Gui initComponents(); server = new UdpServer(this); server.start(); } /** * 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() { jButton1 = new javax.swing.JButton(); pause = new javax.swing.JToggleButton(); startMsg = new javax.swing.JTextField(); stopMsg = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); script = new javax.swing.JTextArea(); jButton2 = new javax.swing.JButton(); jScrollPane2 = new javax.swing.JScrollPane(); imagePanel = new ImagePanel(); selectSystem = new javax.swing.JComboBox(); jLabel3 = new javax.swing.JLabel(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenuItem2 = new javax.swing.JMenuItem(); jMenuItem3 = new javax.swing.JMenuItem(); jMenuItem1 = new javax.swing.JMenuItem(); jMenu2 = new javax.swing.JMenu(); jMenuItem4 = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("APPT"); setMinimumSize(new java.awt.Dimension(880, 531)); jButton1.setText("Wyczy"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); pause.setText("Pauza"); pause.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { pauseActionPerformed(evt); } }); startMsg.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { startMsgActionPerformed(evt); } }); stopMsg.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { stopMsgActionPerformed(evt); } }); jLabel1.setText("Rysowanie rozpoczynamy od zdarzenia:"); jLabel2.setText("Rysowanie koczymy na zdarzeniu:"); script.setColumns(20); script.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N script.setRows(5); script.setMaximumSize(new java.awt.Dimension(10, 10)); jScrollPane1.setViewportView(script); jButton2.setText("Rysuj"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); javax.swing.GroupLayout imagePanelLayout = new javax.swing.GroupLayout(imagePanel); imagePanel.setLayout(imagePanelLayout); imagePanelLayout.setHorizontalGroup(imagePanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 656, Short.MAX_VALUE)); imagePanelLayout.setVerticalGroup(imagePanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 486, Short.MAX_VALUE)); jScrollPane2.setViewportView(imagePanel); selectSystem.setModel(new javax.swing.DefaultComboBoxModel(new String[] {})); selectSystem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { selectSystemActionPerformed(evt); } }); jLabel3.setText("System"); jMenu1.setText("Plik"); jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK)); jMenuItem2.setText("Zapisz"); jMenuItem2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem2ActionPerformed(evt); } }); jMenu1.add(jMenuItem2); jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK)); jMenuItem3.setText("Zapisz obraz"); jMenuItem3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem3ActionPerformed(evt); } }); jMenu1.add(jMenuItem3); jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK)); jMenuItem1.setText("Wczytaj"); jMenuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem1ActionPerformed(evt); } }); jMenu1.add(jMenuItem1); jMenuBar1.add(jMenu1); jMenu2.setText("Narzdzia"); jMenuItem4.setText("Badanie zalenoci"); jMenuItem4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem4ActionPerformed(evt); } }); jMenu2.add(jMenuItem4); jMenuBar1.add(jMenu2); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel2).addComponent(jScrollPane1) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(jButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1)) .addComponent(stopMsg).addComponent(startMsg) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(pause, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(selectSystem, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(selectSystem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(startMsg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(stopMsg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1).addComponent(jButton2)) .addGap(7, 7, 7).addComponent(pause)) .addComponent(jScrollPane2)).addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void stopMsgActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopMsgActionPerformed // TODO add your handling code here: }//GEN-LAST:event_stopMsgActionPerformed private void startMsgActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startMsgActionPerformed // TODO add your handling code here: }//GEN-LAST:event_startMsgActionPerformed public void updateUml(String umlString) { if (pause.isSelected()) { return; } script.setText(umlString); String source = "@startuml\n"; source += umlString; source += "\n@enduml\n"; try { SourceStringReader streamReader = new SourceStringReader(source); ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); String desc = streamReader.generateImage(byteArray); InputStream in = new ByteArrayInputStream(byteArray.toByteArray()); image = ImageIO.read(in); ((ImagePanel) imagePanel).updateImage(image); System.out.println("Wynik rysowania: " + desc); } catch (IOException ex) { Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex); } if (pauseOn) { pause.setSelected(true); } } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed updateUml(script.getText()); }//GEN-LAST:event_jButton2ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed ((ImagePanel) imagePanel).updateImage(null); script.setText(null); if (selectSystem.getSelectedIndex() == -1) { server.messages = new Messages(this); } else { server.messages.clear(selectSystem.getSelectedItem().toString()); } }//GEN-LAST:event_jButton1ActionPerformed // zapisz do pliku private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Zapisz plik jako..."); FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter("Pliki UMLDump (*.umldump)", "umldump"); fileChooser.setAcceptAllFileFilterUsed(false); fileChooser.setFileFilter(xmlfilter); DateFormat df = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss"); fileChooser.setSelectedFile(new File(df.format(new Date()))); int userSelection = fileChooser.showSaveDialog(this); if (userSelection == JFileChooser.APPROVE_OPTION) { File fileToSave = fileChooser.getSelectedFile(); if (!FilenameUtils.getExtension(fileToSave.getName()).equalsIgnoreCase("umldump")) { fileToSave = new File(fileToSave.getParentFile(), FilenameUtils.getBaseName(fileToSave.getName()) + ".umldump"); } try { FileHandler.saveFile(Paths.get(fileToSave.getAbsolutePath()), server.messages.toString(selectSystem.getSelectedItem().toString())); } catch (IOException ex) { JOptionPane.showMessageDialog(this, "Nie udao si zapisa pliku.", "Wystpi bd", JOptionPane.ERROR_MESSAGE); } } }//GEN-LAST:event_jMenuItem2ActionPerformed // wczytaj z pliku private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed JFileChooser fileChooser = new JFileChooser(); FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter("Pliki UMLDump (*.umldump)", "umldump"); fileChooser.setAcceptAllFileFilterUsed(false); fileChooser.setFileFilter(xmlfilter); int returnValue = fileChooser.showOpenDialog(this); String data = null; if (returnValue == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); try { data = FileHandler.getFile(Paths.get(selectedFile.getAbsolutePath())); } catch (IOException ex) { JOptionPane.showMessageDialog(this, "Nie udao si otworzy pliku.", "Wystpi bd", JOptionPane.ERROR_MESSAGE); } server.messages.clear(selectedFile.getName()); String[] lines = data.split("[\\r?\\n]+"); for (String line : lines) { String[] parts = line.split("-"); if (parts.length < 8) { break; } Message message = new Message(parts[0], parts[1], parts[2], parts[3], parts[5], parts[4], parts[6], parts[7]); server.messages.add(selectedFile.getName(), message); } if (((DefaultComboBoxModel) selectSystem.getModel()).getIndexOf(selectedFile.getName()) == -1) { selectSystem.addItem(selectedFile.getName()); selectSystem.setSelectedItem(selectedFile.getName()); } else { selectSystem.setSelectedItem(selectedFile.getName()); } updateUml(server.messages.toUml(selectedFile.getName(), startMsg.getText(), stopMsg.getText())); } }//GEN-LAST:event_jMenuItem1ActionPerformed // zmiana systemu (wybr combobox) private void selectSystemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectSystemActionPerformed String wybrany = selectSystem.getSelectedItem().toString(); if (server != null) { updateUml(server.messages.toUml(wybrany, startMsg.getText(), stopMsg.getText())); } }//GEN-LAST:event_selectSystemActionPerformed private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Zapisz obraz jako..."); FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter("Pliki BMP (*.bmp)", "bmp"); fileChooser.setAcceptAllFileFilterUsed(false); fileChooser.setFileFilter(xmlfilter); DateFormat df = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss"); fileChooser.setSelectedFile(new File(df.format(new Date()))); int userSelection = fileChooser.showSaveDialog(this); if (userSelection == JFileChooser.APPROVE_OPTION) { File fileToSave = fileChooser.getSelectedFile(); if (!FilenameUtils.getExtension(fileToSave.getName()).equalsIgnoreCase("bmp")) { fileToSave = new File(fileToSave.getParentFile(), FilenameUtils.getBaseName(fileToSave.getName()) + ".bmp"); } try { ImageIO.write((RenderedImage) image, "bmp", fileToSave); } catch (IOException ex) { JOptionPane.showMessageDialog(this, "Nie udao si zapisa obrazu.", "Wystpi bd", JOptionPane.ERROR_MESSAGE); } } }//GEN-LAST:event_jMenuItem3ActionPerformed private void pauseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pauseActionPerformed if (!pause.isSelected()) { pauseOn = false; } if (selectSystem.getSelectedIndex() != -1) { updateUml(server.messages.toUml(selectSystem.getSelectedItem().toString(), startMsg.getText(), stopMsg.getText())); } }//GEN-LAST:event_pauseActionPerformed private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed boolean zal = false; JTextField z1 = new JTextField(); JTextField z2 = new JTextField(); Object[] message = { "Zdarzenie 1", z1, "Zdarzenie 2", z2 }; JOptionPane pane = new JOptionPane(message, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION); pane.createDialog(null, "Badanie zdarze").setVisible(true); if (selectSystem.getSelectedIndex() != -1) { zal = server.messages.isDependent1(Integer.parseInt(z1.getText()), Integer.parseInt(z2.getText()), selectSystem.getSelectedItem().toString()); JOptionPane.showMessageDialog(null, zal ? "Zdarzenia s zalene" : "Zdarzenia s niezalene", "Badanie zdarze", JOptionPane.WARNING_MESSAGE); } }//GEN-LAST:event_jMenuItem4ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new GUI().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel imagePanel; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JMenuItem jMenuItem2; private javax.swing.JMenuItem jMenuItem3; private javax.swing.JMenuItem jMenuItem4; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; public javax.swing.JToggleButton pause; public javax.swing.JTextArea script; public javax.swing.JComboBox selectSystem; public javax.swing.JTextField startMsg; public javax.swing.JTextField stopMsg; // End of variables declaration//GEN-END:variables }