package org.pasif.gui;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
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.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
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.log.LogHandlerImpl;
import org.xmlpull.v1.XmlPullParserException;
public class SelectIndexView extends javax.swing.JInternalFrame {
private static final long serialVersionUID = 1L;
private XPP3Reader reader = new XPP3Reader();
private String exportPath;
public Vector list = new Vector(0);
private String host;
private int port;
private LogHandlerImpl log;
public SelectIndexView(String exportPath, String host, int port, LogHandlerImpl log) {
initComponents();
this.exportPath = exportPath;
this.host = host;
this.port = port;
this.log = log;
File dir = new File(this.exportPath);
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());
}
@Action
public void exit() {
dispose();
}
@Action
public void send() {
sendFiles = true;
dispose();
}
public void load(String file) {
list.clear();
jList1.removeAll();
list.add(file);
try {
Document document = reader.read(new File(exportPath + 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();
setClosable(true);
setIconifiable(true);
setMaximizable(true);
setResizable(true);
ResourceMap resourceMap = Application.getInstance(Client.class).getContext().getResourceMap(SelectIndexView.class);
setTitle(resourceMap.getString("Form.title")); // NOI18N
setName("Form"); // NOI18N
addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
formPropertyChange(evt);
}
});
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(Client.class).getContext().getActionMap(SelectIndexView.class, this);
jButton1.setAction(actionMap.get("send")); // 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 = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
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, 371, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(GroupLayout.LEADING)
.add(jScrollPane1, GroupLayout.DEFAULT_SIZE, 95, 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, 274, 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, 80, 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()
.addContainerGap()
.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
private void formPropertyChange(PropertyChangeEvent evt) {//GEN-FIRST:event_formPropertyChange
if (evt.getPropertyName().equals("icon") && evt.getOldValue().equals(Boolean.FALSE) && evt.getNewValue().equals(Boolean.TRUE)) {
JInternalFrame j = (JInternalFrame) evt.getSource();
j.setVisible(true);
j.moveToFront();
}
}//GEN-LAST:event_formPropertyChange
// 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 boolean sendFiles = false;
}