package org.pasif.gui;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.List;
import java.util.Vector;
import javax.swing.AbstractListModel;
import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.XPP3Reader;
import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.layout.GroupLayout;
import org.jdesktop.layout.LayoutStyle;
import org.pas.database.DbHelper;
import org.xmlpull.v1.XmlPullParserException;
public class IndexView extends javax.swing.JDialog {
public IndexView(java.awt.Frame parent, boolean modal, String importFolder, DbHelper dbHelper) {
super(parent, modal);
initComponents();
this.importFolder = importFolder.endsWith("/") ? importFolder : importFolder + "/";
this.dbHelper = dbHelper;
File dir = new File(this.importFolder);
if (!dir.exists()) {
dir.mkdirs();
} else {
jComboBox1.setModel(new DefaultComboBoxModel(dir.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().trim().contains("index");
}
})));
load(jComboBox1.getSelectedItem().toString());
}
}
public void setDir(String importDir) {
this.importFolder = importDir.endsWith("/") ? importDir : importDir + "/";
}
@Action
public void exit() {
dispose();
}
@Action
public void imortData() {
if (importview == null) {
importview = new ImportView(null, true, importFolder, dbHelper, jComboBox1.getSelectedItem().toString());
}
Server.getApplication().show(importview);
}
public void load(String file) {
list.clear();
jList1.removeAll();
try {
Document document = reader.read(new File(importFolder + file));
List items = document.getRootElement().selectNodes("//file");
for (Object object : items) {
Element item = (Element) object;
list.add(item.getText());
}
jList1.setListData(list);
jList1.repaint();
} catch (IOException ex) {
ex.printStackTrace();
} catch (XmlPullParserException ex) {
ex.printStackTrace();
} catch (DocumentException ex) {
ex.printStackTrace();
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new JLabel();
jComboBox1 = new JComboBox();
jButton1 = new JButton();
jButton2 = new JButton();
jPanel1 = new JPanel();
jScrollPane1 = new JScrollPane();
jList1 = new JList();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setName("Form"); // NOI18N
ResourceMap resourceMap = Application.getInstance(Server.class).getContext().getResourceMap(IndexView.class);
jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
jLabel1.setName("jLabel1"); // NOI18N
jComboBox1.setModel(new DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jComboBox1.setName("jComboBox1"); // NOI18N
jComboBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
ActionMap actionMap = Application.getInstance(Server.class).getContext().getActionMap(IndexView.class, this);
jButton1.setAction(actionMap.get("imortData")); // NOI18N
jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
jButton1.setName("jButton1"); // NOI18N
jButton2.setAction(actionMap.get("exit")); // NOI18N
jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
jButton2.setName("jButton2"); // NOI18N
jPanel1.setBorder(BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
jPanel1.setName("jPanel1"); // NOI18N
jScrollPane1.setName("jScrollPane1"); // NOI18N
jList1.setModel(new AbstractListModel() {
String[] strings = { "file1","file2" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jList1.setName("jList1"); // NOI18N
jScrollPane1.setViewportView(jList1);
GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(GroupLayout.LEADING)
.add(jScrollPane1, GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(GroupLayout.LEADING)
.add(jScrollPane1, GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(jLabel1)
.addPreferredGap(LayoutStyle.RELATED)
.add(jComboBox1, 0, 346, Short.MAX_VALUE))
.add(jPanel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
.add(GroupLayout.TRAILING, layout.createSequentialGroup()
.add(80, 80, 80)
.add(jButton1)
.addPreferredGap(LayoutStyle.RELATED, 147, Short.MAX_VALUE)
.add(jButton2)
.add(89, 89, 89))
);
layout.linkSize(new Component[] {jButton1, jButton2}, GroupLayout.HORIZONTAL);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(25, 25, 25)
.add(layout.createParallelGroup(GroupLayout.BASELINE)
.add(jLabel1, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
.add(jComboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.RELATED)
.add(jPanel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(18, 18, 18)
.add(layout.createParallelGroup(GroupLayout.BASELINE)
.add(jButton2)
.add(jButton1))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jComboBox1ActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
load(jComboBox1.getSelectedItem().toString());
}//GEN-LAST:event_jComboBox1ActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private JButton jButton1;
private JButton jButton2;
private JComboBox jComboBox1;
private JLabel jLabel1;
private JList jList1;
private JPanel jPanel1;
private JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables
private XPP3Reader reader = new XPP3Reader();
private String importFolder;
private Vector list = new Vector();
private DbHelper dbHelper;
private ImportView importview;
}