de.bley.word.menu.ClickedGuiMenu.java Source code

Java tutorial

Introduction

Here is the source code for de.bley.word.menu.ClickedGuiMenu.java

Source

/*
 * 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 de.bley.word.menu;

import de.bley.word.propertiehandling.PropertieManager;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

/**
 * Bildet eine Graphische User Oberflaeche.
 *
 * @author rbley
 *
 */
@Service
public class ClickedGuiMenu extends javax.swing.JFrame implements MenueInterface {

    /**
     * Creates new form NewJFrame
     */
    String path;
    private final static transient Logger log = Logger.getLogger(ClickedGuiMenu.class);
    private static ClickedGuiMenu instance;

    /**
     * Initialisiuerung der Komponenten.
     */
    private void setup() {
        path = PropertieManager.getInstance().getZuordnung().getPath().getFilepath();
        initComponents();
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                if (!jEditorPane1.getText().equals("")) {

                    if (!jEditorPane1.getText()
                            .equals(PropertieManager.getInstance().getZuordnung().getReader().readFile(path))) {
                        int confirmed = JOptionPane.showConfirmDialog(null, "Speichern der Daten?", "Beenden",
                                JOptionPane.YES_NO_OPTION);

                        if (confirmed == JOptionPane.YES_OPTION) {
                            saveData();
                            dispose();
                        }
                    }
                }
            }
        });
        read();
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }

        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | javax.swing.UnsupportedLookAndFeelException ex) {
            log.debug("NewJFrame" + ex);
        }

    }

    public static MenueInterface getInstance() {
        if (instance == null) {
            instance = new ClickedGuiMenu();
        }
        return instance;
    }

    /**
     * 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() {

        fileChooser = new javax.swing.JFileChooser();
        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane2 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();
        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        fileItemOpen = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem2 = new javax.swing.JRadioButtonMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane2.setViewportView(jEditorPane1);

        jMenu3.setText("File");

        fileItemOpen.setText("Open");
        fileItemOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileItemOpenActionPerformed(evt);
            }
        });
        jMenu3.add(fileItemOpen);

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S,
                java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Save");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem1);

        jMenuBar2.add(jMenu3);

        jMenu4.setText("Edit");

        buttonGroup1.add(jRadioButtonMenuItem1);
        jRadioButtonMenuItem1.setText("Datenbank");
        jRadioButtonMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem1ActionPerformed(evt);
            }
        });
        jMenu4.add(jRadioButtonMenuItem1);

        buttonGroup1.add(jRadioButtonMenuItem2);
        jRadioButtonMenuItem2.setSelected(true);
        jRadioButtonMenuItem2.setText("Textdatei");
        jRadioButtonMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonMenuItem2ActionPerformed(evt);
            }
        });
        jMenu4.add(jRadioButtonMenuItem2);

        jMenuBar2.add(jMenu4);

        setJMenuBar(jMenuBar2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 277,
                Short.MAX_VALUE));

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void fileItemOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileItemOpenActionPerformed
        int returnVal = fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            PropertieManager.getInstance().changePropertie("filename", file.getName());
            String[] splitPath = file.getAbsolutePath().split("/");
            StringBuilder buffer = new StringBuilder();
            for (int i = 0; i < splitPath.length; i++) {
                buffer.append("/").append(splitPath[i]);
                if (i == splitPath.length - 2) {
                    break;
                }
            }
            PropertieManager.getInstance().changePropertie("filedirectory", buffer.toString());
            path = file.getAbsolutePath();

            try {
                // What to do with the file, e.g. display it in a TextArea
                jEditorPane1.read(new FileReader(file.getAbsolutePath()), null);
            } catch (IOException ex) {
                log.fatal("Input fehler beim lesen der ausgewaehlten Datei", ex);
            }
        } else {
        }
    }//GEN-LAST:event_fileItemOpenActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        saveData();

    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jRadioButtonMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem1ActionPerformed

        PropertieManager.getInstance().changePropertie("zuordnung", "bank");
        read();
        fileItemOpen.setEnabled(false);

    }//GEN-LAST:event_jRadioButtonMenuItem1ActionPerformed

    private void jRadioButtonMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem2ActionPerformed
        PropertieManager.getInstance().changePropertie("zuordnung", "file");
        read();
        fileItemOpen.setEnabled(true);
    }//GEN-LAST:event_jRadioButtonMenuItem2ActionPerformed

    /**
     * Sorgt fuer die Speicherung der eingegebenen Daten.
     */
    public void saveData() {
        PropertieManager.getInstance().getZuordnung().getWriter().overwriteFile(path, jEditorPane1.getText());
    }

    /**
     * Uebergibt dem Textfeld der Oberflache den Inhalt einer ausgewaehlten
     * Datei.
     */
    public void read() {
        jEditorPane1.setText(PropertieManager.getInstance().getZuordnung().getReader()
                .readFile(PropertieManager.getInstance().getZuordnung().getPath().getFilepath()));
    }

    /**
     * @param args the command line arguments
     */
    //    public static void main(String args[]) {
    //        /* Set the Nimbus look and feel */
    //        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    //        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
    //         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    //         */
    //        try {
    //            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    //                if ("Nimbus".equals(info.getName())) {
    //                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
    //                    break;
    //                }
    //            }
    //
    //        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
    //            log.debug("NewJFrame" + ex);
    //        }
    //        //</editor-fold>
    //
    //        /* Create and display the form */
    //        java.awt.EventQueue.invokeLater(new Runnable() {
    //            public void run() {
    //                new NewJFrame().setVisible(true);
    //            }
    //        });
    //    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JMenuItem fileItemOpen;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem2;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

    @Override
    public void showMenue() {
        setup();
        setVisible(true);
    }
}