package net.sf.jpluck.apps.jpluckx.ui;
import com.jgoodies.forms.builder.ButtonStackBuilder;
import com.jgoodies.forms.layout.FormLayout;
import net.sf.jpluck.jxl.JXL;
import net.sf.jpluck.swing.JXLFileFilter;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.AbstractListModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class AutoUpdatePanel extends JPanel {
private JFileChooser fileChooser = new JFileChooser();
private List jxlList = new ArrayList();
private Model model;
private JList list = new JList(model = new Model());
private RemoveAction removeAction = new RemoveAction();
public AutoUpdatePanel() {
initComponents();
}
public void setFiles(File[] files) {
model.set(files);
list.clearSelection();
}
public File[] getFiles() {
return (File[]) jxlList.toArray(new File[jxlList.size()]);
}
private void initComponents() {
list.addListSelectionListener(new ListListener());
list.setCellRenderer(new ListRenderer());
JScrollPane scrollPane = new JScrollPane(list);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
fileChooser.setDialogTitle("Add JXL file");
fileChooser.setApproveButtonText("Add JXL");
fileChooser.setAcceptAllFileFilterUsed(false);
JXLFileFilter jxlFilter = new JXLFileFilter();
fileChooser.addChoosableFileFilter(jxlFilter);
fileChooser.setFileFilter(jxlFilter);
fileChooser.setFileHidingEnabled(false);
/*fileChooser.setDialogTitle("Add JXL file");
fileChooser.setApproveButtonText("Add JXL");
JXLFileFilter jxlFilter = new JXLFileFilter();
fileChooser.addChoosableFileFilter(jxlFilter);
fileChooser.setFileFilter(jxlFilter);
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.setFileHidingEnabled(false);*/
ButtonStackBuilder buttonBuilder = new ButtonStackBuilder();
buttonBuilder.addGridded(new JButton(new AddAction()));
buttonBuilder.addRelatedGap();
buttonBuilder.addGridded(new JButton(removeAction));
buttonBuilder.addUnrelatedGap();
buttonBuilder.addGridded(new JButton(moveUpAction));
buttonBuilder.addRelatedGap();
buttonBuilder.addGridded(new JButton(moveDownAction));
FormLayout formLayout = new FormLayout("fill:150dlu:grow, 4dlu, pref", "fill:pref:grow");
setLayout(formLayout);
add(scrollPane, "1,1");
add(buttonBuilder.getPanel(), "3,1");
}
private class AddAction extends AbstractAction {
AddAction() {
super("Add...");
}
public void actionPerformed(ActionEvent ev) {
fileChooser.setSelectedFile(null);
if (fileChooser.showOpenDialog(AutoUpdatePanel.this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
int idx = jxlList.indexOf(file);
if (idx == -1) {
try {
new JXL(file);
model.add(file);
list.setSelectedIndex(jxlList.size() - 1);
} catch (Exception e) {
JOptionPane.showMessageDialog(AutoUpdatePanel.this,
"Not a valid JXL file: " + e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}
} else {
list.setSelectedIndex(idx);
}
}
}
}
private class ListListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {
int count = list.getSelectedIndices().length;
if (count == 1) {
list.setToolTipText(((File) list.getSelectedValue()).getAbsolutePath());
} else {
list.setToolTipText(null);
}
removeAction.setEnabled(count > 0);
moveUpAction.setEnabled(count==1);
moveDownAction.setEnabled(count==1);
}
}
private class ListRenderer extends DefaultListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
Component comp = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
File file = (File) value;
setText(file.getName());
return comp;
}
}
private class Model extends AbstractListModel {
public Object getElementAt(int index) {
return jxlList.get(index);
}
public int getSize() {
return jxlList.size();
}
void add(File jxl) {
jxlList.add(jxl);
fireIntervalAdded(this, jxlList.size() - 1, jxlList.size() - 1);
}
void remove(File jxl) {
int idx = jxlList.indexOf(jxl);
jxlList.remove(jxl);
fireIntervalRemoved(this, idx, idx);
}
void set(File[] jxls) {
if (jxlList.size() > 0) {
jxlList.clear();
fireIntervalRemoved(this, 0, jxlList.size() - 1);
}
jxlList.addAll(Arrays.asList(jxls));
if (jxlList.size() > 0) {
fireIntervalAdded(this, 0, jxlList.size() - 1);
}
}
void swap(int idx1, int idx2) {
Object o = jxlList.get(idx1);
jxlList.set(idx1, jxlList.get(idx2));
jxlList.set(idx2, o);
fireContentsChanged(this, idx1, idx2);
}
}
private MoveUpAction moveUpAction= new MoveUpAction();
private class MoveUpAction extends AbstractAction {
MoveUpAction() {
super("Move Up");
setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
int idx = list.getSelectedIndex();
if (idx > 0) {
model.swap(idx - 1, idx);
list.setSelectedIndex(idx - 1);
}
}
}
private MoveDownAction moveDownAction= new MoveDownAction();
private class MoveDownAction extends AbstractAction {
MoveDownAction() {
super("Move Down");
setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
int idx = list.getSelectedIndex();
if (idx < (jxlList.size() - 1)) {
model.swap(idx, idx + 1);
list.setSelectedIndex(idx + 1);
}
}
}
private class RemoveAction extends AbstractAction {
RemoveAction() {
super("Remove");
setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showConfirmDialog(AutoUpdatePanel.this,
"Remove selected JXL(s) from AutoUpdate?", "Remove JXLs",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.YES_OPTION) {
Object[] objects = list.getSelectedValues();
for (int i = 0; i < objects.length; i++) {
model.remove((File) objects[i]);
}
list.clearSelection();
}
}
}
}