Package pdfrobot.gui

Source Code of pdfrobot.gui.RuleConfig

/*
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/
package pdfrobot.gui;

import java.awt.Window;
import java.io.File;
import java.io.IOException;
import java.util.Observable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.BadLocationException;
import pdfrobot.Application;
import pdfrobot.RuleAlreadyExistsException;
import pdfrobot.engine.config.FolderFileFilter;
import pdfrobot.engine.config.PdfFileFilter;
import pdfrobot.engine.parser.PdfFileParser;
import pdfrobot.engine.parser.PdfFileRule;

/**
*
* @author hedsttor
*/
public class RuleConfig extends javax.swing.JPanel {

    private Application application;    
    private Window parent;
    private int line;
    private String pattern;
    private File destinationFolder;
    private File sourceFolder;

    private Observable obs;
    /**
     * Creates new form RuleConfig
     * @param application
     */
    public RuleConfig(Application application, Observable obs, Window parent) {
        this.application = application;
        init();
        initComponents();       
        this.obs = obs;
        this.parent = parent;
    }

    private void init() {
        pattern = null;
        destinationFolder = null;
        sourceFolder = null;               
    }
   
    public void setRuleData(PdfFileRule rule) {
        pathTextField.setText(rule.getSourceFolder().getAbsolutePath());
        outputDirTextField.setText(rule.getDestinationSubFolder().getAbsolutePath());
        patternTextField.setText(rule.getPattern());
        lineTextField.setText(Integer.toString(rule.getLine()));
    }

    private File getSingleFileFromUser(FileFilter filter, int fileSelectionMode) {
        JFileChooser jFileChooser = new JFileChooser(application.getStartDirectory());
        jFileChooser.setFileFilter(filter);
        jFileChooser.setMultiSelectionEnabled(false);
        jFileChooser.setFileSelectionMode(fileSelectionMode);
        int result = jFileChooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            return jFileChooser.getSelectedFile();
        }
        return null;
    }

    private boolean isFileDataOk() {
        Logger.getLogger(FileChooserTab.class.getName()).log(Level.INFO, "Checking data from UI");
        boolean isOK = line > 0;
        Logger.getLogger(FileChooserTab.class.getName()).log(Level.INFO, "Selected line in PDF document {0}", line);
        isOK = pattern != null && pattern.equals("");
        Logger.getLogger(FileChooserTab.class.getName()).log(Level.INFO, "Selected pattern {0}", pattern);
        isOK = destinationFolder != null;
        Logger.getLogger(FileChooserTab.class.getName()).log(Level.INFO, "Selected destination folder {0}", destinationFolder);
        return isOK;
    }

    /** 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();
        okButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        pathTextField = new javax.swing.JTextField();
        ChooseFile = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        patternLabel = new javax.swing.JLabel();
        lineLabel = new javax.swing.JLabel();
        patternTextField = new javax.swing.JTextField();
        lineTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        outputDirTextField = new javax.swing.JTextField();
        ChooseFile1 = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(602, 451));

        jButton1.setText("Fortryd");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        okButton.setText("Ok");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("Fi/ind folderl");

        pathTextField.setEditable(false);
        pathTextField.setText("Ingen sti valgt");

        ChooseFile.setText("Vælg fil");
        ChooseFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChooseFileActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setRows(5);
        jTextArea1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTextArea1MouseReleased(evt);
            }
        });
        jTextArea1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jTextArea1MouseDragged(evt);
            }
        });
        jScrollPane1.setViewportView(jTextArea1);

        patternLabel.setText("Mønster");

        lineLabel.setText("Linie");

        patternTextField.setEditable(false);
        patternTextField.setText("Intet mønster valgt");

        lineTextField.setEditable(false);
        lineTextField.setText("???");

        jLabel3.setText("Ud - folder");

        outputDirTextField.setEditable(false);
        outputDirTextField.setText("Ingen sti valgt");

        ChooseFile1.setText("Vælg fil");
        ChooseFile1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChooseFile1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(patternLabel)
                                .addGap(18, 18, 18)
                                .addComponent(patternTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lineLabel)
                                .addGap(34, 34, 34)
                                .addComponent(lineTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 408, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(okButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(outputDirTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
                            .addComponent(pathTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ChooseFile)
                            .addComponent(ChooseFile1, javax.swing.GroupLayout.Alignment.TRAILING))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(pathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(ChooseFile))
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(outputDirTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(ChooseFile1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(patternLabel)
                        .addComponent(patternTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(okButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lineLabel)
                    .addComponent(jButton1)
                    .addComponent(lineTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void ChooseFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChooseFileActionPerformed
        File file = getSingleFileFromUser(new PdfFileFilter(), JFileChooser.FILES_AND_DIRECTORIES);
        if (file == null) {
            return;
        }
        String content = "";
        try {
            content = new PdfFileParser().parsePdf(file);
        } catch (IOException ex) {
            Logger.getLogger(RuleConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        String sourceFolderPath = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(System.getProperty("file.separator")));
        pathTextField.setText(sourceFolderPath);
        jTextArea1.setText(content);
        this.sourceFolder = new File(sourceFolderPath);
    }//GEN-LAST:event_ChooseFileActionPerformed

    private void jTextArea1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextArea1MouseDragged
        pattern = jTextArea1.getSelectedText();
        patternTextField.setText(pattern);
    }//GEN-LAST:event_jTextArea1MouseDragged

    private void jTextArea1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextArea1MouseReleased
        int lineNumber=0;
        try {
            lineNumber = jTextArea1.getLineOfOffset(jTextArea1.getCaret().getMark());                   
        } catch (BadLocationException ex) {
            Logger.getLogger(RuleConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        lineTextField.setText(Integer.toString(lineNumber));
        line=lineNumber;
    }//GEN-LAST:event_jTextArea1MouseReleased

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        if (fieldsAreOk()) {
            PdfFileRule pdfFileRule = new PdfFileRule(Integer.toString(line), pattern, destinationFolder, sourceFolder);
            try {
                application.savePdfRule(pdfFileRule);
            } catch (RuleAlreadyExistsException ex) {
                Logger.getLogger(RuleConfig.class.getName()).log(Level.SEVERE, "Regel kan ikke gemmes... "+ex.getMessage(), ex);
                JOptionPane.showMessageDialog(this, "Denne regel findes allerede.");
            }
            obs.notifyObservers();                
            parent.dispose();
        }
    }//GEN-LAST:event_okButtonActionPerformed

    private void ChooseFile1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChooseFile1ActionPerformed
        File outputDir = getSingleFileFromUser(new FolderFileFilter(), JFileChooser.DIRECTORIES_ONLY);
        outputDirTextField.setText(outputDir.getAbsolutePath());  
        destinationFolder = outputDir;
    }//GEN-LAST:event_ChooseFile1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        parent.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ChooseFile;
    private javax.swing.JButton ChooseFile1;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel lineLabel;
    private javax.swing.JTextField lineTextField;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField outputDirTextField;
    private javax.swing.JTextField pathTextField;
    private javax.swing.JLabel patternLabel;
    private javax.swing.JTextField patternTextField;
    // End of variables declaration//GEN-END:variables

    private boolean fieldsAreOk() {
        return sourceFolder != null              
               & destinationFolder != null
               & pattern != null;        
    }
}
TOP

Related Classes of pdfrobot.gui.RuleConfig

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.