package tk.amberide.ide.gui.dialogs.res;
import tk.amberide.Amber;
import tk.amberide.ide.data.io.FileIO;
import tk.amberide.ide.swing.UIUtil;
import tk.amberide.ide.swing.list.CheckBoxList;
import java.awt.BorderLayout;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
/**
*
* @author Tudor
*/
public class ImportResourceDirectoryDialog extends javax.swing.JDialog {
private CheckBoxList cbl;
private File relative;
private boolean ok = false;
private static JFileChooser browser;
static {
browser = new JFileChooser("Import directory...");
browser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // ech
}
/**
* Creates new form ImportResourceDirectoryDialog
*/
public ImportResourceDirectoryDialog(java.awt.Frame parent, File root, String... accepted) {
super(parent);
initComponents();
relative = root;
cbl = new CheckBoxList();
populateList(cbl, root, accepted);
filesGroup.setLayout(new BorderLayout());
filesGroup.add(new JScrollPane(cbl), BorderLayout.CENTER);
}
public static File[] showResourceDirectoryDialog(java.awt.Frame parent, String... files) {
if (browser.showOpenDialog(Amber.getUI()) == JFileChooser.APPROVE_OPTION) {
File dir = browser.getSelectedFile();
ImportResourceDirectoryDialog irdd = new ImportResourceDirectoryDialog(Amber.getUI(), dir, files);
irdd.setVisible(true);
return irdd.getSelectedFiles();
}
return new File[0];
}
private void populateList(CheckBoxList cbl, File root, final String... accepted) {
for (File f : root.listFiles(new FileFilter() {
List<String> acc = Arrays.asList(accepted);
public boolean accept(File pathname) {
System.out.println(acc);
System.out.println("listing " + pathname + "::" + (pathname.isDirectory() ||
acc.contains(FileIO.getFileExtension(pathname))) + FileIO.getFileExtension(pathname));
return pathname.isDirectory() || acc.contains(FileIO.getFileExtension(pathname));
}
})) {
if (f.isDirectory()) {
populateList(cbl, f, accepted);
} else {
String path = f.getAbsolutePath();
path = path.replace(relative.getAbsolutePath(), "");
JCheckBox box = new JCheckBox(".." + path, true);
cbl.addCheckbox(box);
}
}
}
public File[] getSelectedFiles() {
if (!ok) {
return new File[0];
}
ArrayList<File> files = new ArrayList<File>();
ListModel m = cbl.getModel();
int size = m.getSize();
for (int i = 0; i != size; i++) {
JCheckBox box = (JCheckBox) m.getElementAt(i);
if (box.isSelected()) {
files.add(new File(box.getText().replace("..", relative.getAbsolutePath())));
}
}
return files.toArray(new File[0]);
}
/**
* 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() {
filesGroup = new javax.swing.JPanel();
okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Import resource directory...");
setModal(true);
setResizable(false);
filesGroup.setBorder(javax.swing.BorderFactory.createTitledBorder("Files"));
javax.swing.GroupLayout filesGroupLayout = new javax.swing.GroupLayout(filesGroup);
filesGroup.setLayout(filesGroupLayout);
filesGroupLayout.setHorizontalGroup(
filesGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
filesGroupLayout.setVerticalGroup(
filesGroupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 238, Short.MAX_VALUE)
);
okButton.setText("OK");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
cancelButton.setText("Cancel");
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(filesGroup, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(filesGroup, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(okButton)
.addComponent(cancelButton))
.addGap(10, 10, 10))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>//GEN-END:initComponents
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
dispose();
}//GEN-LAST:event_cancelButtonActionPerformed
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
dispose();
ok = true;
}//GEN-LAST:event_okButtonActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
UIUtil.makeNative();
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
final ImportResourceDirectoryDialog dialog = new ImportResourceDirectoryDialog(new javax.swing.JFrame(), new File("C:\\Users\\Tudor\\Desktop\\rs"));
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
dialog.getSelectedFiles();
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton cancelButton;
private javax.swing.JPanel filesGroup;
private javax.swing.JButton okButton;
// End of variables declaration//GEN-END:variables
}