/*
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/
/*
* FileChooserTab.java
*
* Created on 24-12-2010, 14:14:07
*/
package pdfrobot.gui;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pdfrobot.Application;
import pdfrobot.engine.parser.PdfFileRule;
import pdfrobot.engine.parser.SerializedFileRules;
/**
*
* @author hedsttor
*/
public class FileChooserTab extends javax.swing.JPanel implements Observer {
private Application application;
private List<PdfFileRule> ruleModel;
/** Creates new form FileChooserTab */
public FileChooserTab() {
initComponents();
initCustom();
}
private void initCustom() {
this.application = GuiManager.getInstance().getApplication();
setFileList(application.getFileRules());
}
public void updateRules() {
setFileList(new SerializedFileRules().getRules());
}
private void setFileList(List<PdfFileRule> data) {
ruleModel = data;
DefaultTableModel model = (DefaultTableModel) ruleTable.getModel();
model.setRowCount(0);
int rowIndex = 0;
for (PdfFileRule pdfFileRule : data) {
model.insertRow(rowIndex, new Object[]{Integer.toString(pdfFileRule.getLine()), pdfFileRule.getPattern(), pdfFileRule.getDestinationSubFolder(), pdfFileRule.getSourceFolder()});
}
}
private void createRuleDialog(PdfFileRule rule) {
Observable ruleObserver = new Observable() {
@Override
public void notifyObservers() {
setChanged();
super.notifyObservers();
}
};
RuleEditorDialog ruleEditorDialog = rule == null ? new RuleEditorDialog(GuiManager.getInstance().getMainWindow(), ruleObserver) : new RuleEditorDialog(GuiManager.getInstance().getMainWindow(), ruleObserver, rule);
ruleObserver.addObserver(this);
ruleEditorDialog.setVisible(true);
}
/** 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() {
SaveButton = new javax.swing.JButton();
jScrollPane3 = new javax.swing.JScrollPane();
ruleTable = new javax.swing.JTable();
DeleteButton = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
SaveButton.setLabel("Ny filtype");
SaveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SaveButtonActionPerformed(evt);
}
});
jScrollPane3.setPreferredSize(new java.awt.Dimension(452, 202));
ruleTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null},
{null, null, null},
{null, null, null},
{null, null, null}
},
new String [] {
"Linie", "Mønster", "Ud-folder"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
ruleTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_NEXT_COLUMN);
ruleTable.setDoubleBuffered(true);
ruleTable.setRowHeight(20);
jScrollPane3.setViewportView(ruleTable);
DeleteButton.setText("Slet filtype");
DeleteButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DeleteButtonActionPerformed(evt);
}
});
jButton1.setText("Ret filtype");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(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(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(SaveButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(DeleteButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(SaveButton)
.addComponent(DeleteButton)
.addComponent(jButton1))
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveButtonActionPerformed
createRuleDialog(null);
}//GEN-LAST:event_SaveButtonActionPerformed
private void DeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteButtonActionPerformed
Logger.getLogger(FileChooserTab.class.getName()).log(Level.FINEST, "Preparing to delete rule.");
Logger.getLogger(FileChooserTab.class.getName()).log(Level.FINEST, "Retrieving selected rules.");
int[] selectedRecords = ruleTable.getSelectedRows();
Logger.getLogger(FileChooserTab.class.getName()).log(Level.FINEST, "Selected rule indexes are...");
for (int i = 0; i < selectedRecords.length; i++) {
try {
Logger.getLogger(FileChooserTab.class.getName()).log(Level.FINEST, Integer.toString(selectedRecords[i]));
} catch (Exception e) {
Logger.getLogger(FileChooserTab.class.getName()).log(Level.SEVERE, e.getCause().getMessage());
}
}
Logger.getLogger(FileChooserTab.class.getName()).log(Level.FINEST, "Retrieving Table model.");
DefaultTableModel model = (DefaultTableModel) ruleTable.getModel();
for (int i = selectedRecords.length-1; i >=0; i--) {
Logger.getLogger(FileChooserTab.class.getName()).log(Level.FINEST, "Deleting rule {0} from model.", selectedRecords[i]);
ruleModel.remove(selectedRecords[i]);
Logger.getLogger(FileChooserTab.class.getName()).log(Level.FINEST, "Saving complete rule model.");
application.saveRules(ruleModel);
Logger.getLogger(FileChooserTab.class.getName()).log(Level.FINEST, "Updating rule list.");
setFileList(ruleModel);
}
}//GEN-LAST:event_DeleteButtonActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
if(ruleTable.getSelectedRows().length < 1) {
JOptionPane.showMessageDialog(this, "Du skal vælge en regel at rette.");
return;
}
if(ruleTable.getSelectedRows().length >1) {
JOptionPane.showMessageDialog(this, "Du må kun vælge én regel til rettelse.");
return;
}
createRuleDialog(ruleModel.get(ruleTable.getSelectedRow()));
}//GEN-LAST:event_jButton1ActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton DeleteButton;
private javax.swing.JButton SaveButton;
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTable ruleTable;
// End of variables declaration//GEN-END:variables
public void update(Observable o, Object arg) {
updateRules();
}
}