/* * TextEditor.java * * Created on 29 de Março de 2006, 22:12 */ package gui; import controllers.DocController; import java.io.IOException; import javax.swing.JFileChooser; import javax.swing.JOptionPane; /** * * @author lchernicharo */ public class TextEditor extends javax.swing.JFrame { private DocController controller; /** Creates new form TextEditor */ public TextEditor() { initComponents(); controller = new DocController(); controller.criarNovo(); } /** 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. */ // //GEN-BEGIN:initComponents private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); txaMemo = new javax.swing.JTextArea(); jMenuBar1 = new javax.swing.JMenuBar(); mnuArquivo = new javax.swing.JMenu(); mniNovo = new javax.swing.JMenuItem(); mniAbrir = new javax.swing.JMenuItem(); mniSalvar = new javax.swing.JMenuItem(); separator1 = new javax.swing.JSeparator(); mniSair = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); setTitle("Sem T\u00edtulo - JPad"); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { verificaFechamento(evt); } }); txaMemo.setColumns(80); txaMemo.setFont(new java.awt.Font("Courier New", 0, 14)); txaMemo.setRows(30); jScrollPane1.setViewportView(txaMemo); getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER); mnuArquivo.setText("Arquivo"); mniNovo.setText("Novo"); mniNovo.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { criarNovo(evt); } }); mnuArquivo.add(mniNovo); mniAbrir.setText("Abrir..."); mniAbrir.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { abrirDocumento(evt); } }); mnuArquivo.add(mniAbrir); mniSalvar.setText("Salvar"); mniSalvar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { salvarArquivo(evt); } }); mnuArquivo.add(mniSalvar); mnuArquivo.add(separator1); mniSair.setText("Sair"); mniSair.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fecharJanela(evt); } }); mnuArquivo.add(mniSair); jMenuBar1.add(mnuArquivo); setJMenuBar(jMenuBar1); pack(); }// //GEN-END:initComponents private void verificaFechamento(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_verificaFechamento if( txaMemo.getText().compareTo( controller.getConteudo() ) == 0 ){ dispose(); return; } int result = JOptionPane.showConfirmDialog( this, "O arquivo foi alterado e não foi salvo. \n\nDeseja salvar as alterações?", "Confirmação", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE ); switch( result ){ case JOptionPane.YES_OPTION : salvarArquivo(null); case JOptionPane.NO_OPTION : dispose(); } }//GEN-LAST:event_verificaFechamento private void salvarArquivo(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarArquivo //Se é um novo if( controller.getNome().compareTo("") == 0 ){ JFileChooser salvar = new JFileChooser(); salvar.setFileHidingEnabled( true ); salvar.setFileSelectionMode( JFileChooser.FILES_ONLY ); if( salvar.showSaveDialog( this ) == JFileChooser.CANCEL_OPTION ){ return; } controller.setNome(salvar.getSelectedFile().getAbsolutePath()); } controller.setConteudo( txaMemo.getText() ); try { controller.salvarArquivo(); } catch (IOException ex) { ex.printStackTrace(); } }//GEN-LAST:event_salvarArquivo private void abrirDocumento(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrirDocumento int result = JOptionPane.NO_OPTION; if( txaMemo.getText().compareTo( controller.getConteudo() ) != 0 ){ result = JOptionPane.showConfirmDialog( this, "O arquivo foi alterado e não foi salvo. \n\nDeseja salvar as alterações?", "Confirmação", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE ); } switch( result ){ case JOptionPane.YES_OPTION : salvarArquivo(null); case JOptionPane.NO_OPTION : JFileChooser abrir = new JFileChooser(); abrir.setFileHidingEnabled( false ); abrir.setFileSelectionMode( JFileChooser.FILES_ONLY ); if( abrir.showOpenDialog( this ) == JFileChooser.APPROVE_OPTION ){ setTitle( abrir.getSelectedFile().getName() + " - JPad" ); try { controller.abrirArquivo( abrir.getSelectedFile().getAbsolutePath() ); txaMemo.setText( controller.getConteudo() ); txaMemo.setCaretPosition(0); } catch (IOException ex) { ex.printStackTrace(); } } } }//GEN-LAST:event_abrirDocumento private void criarNovo(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_criarNovo int result = JOptionPane.NO_OPTION; if( txaMemo.getText().compareTo( controller.getConteudo() ) != 0 ){ result = JOptionPane.showConfirmDialog( this, "O arquivo foi alterado e não foi salvo. \n\nDeseja salvar as alterações?", "Confirmação", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE ); } switch( result ){ case JOptionPane.YES_OPTION : salvarArquivo(null); case JOptionPane.NO_OPTION : txaMemo.setText(""); setTitle( "Sem Título - JPad" ); controller.criarNovo(); } }//GEN-LAST:event_criarNovo private void fecharJanela(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fecharJanela verificaFechamento(null); }//GEN-LAST:event_fecharJanela /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TextEditor().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JMenuBar jMenuBar1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JMenuItem mniAbrir; private javax.swing.JMenuItem mniNovo; private javax.swing.JMenuItem mniSair; private javax.swing.JMenuItem mniSalvar; private javax.swing.JMenu mnuArquivo; private javax.swing.JSeparator separator1; private javax.swing.JTextArea txaMemo; // End of variables declaration//GEN-END:variables }