/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * FilesCustomizer.java * * Created on 03-May-2011, 20:02:15 */ package net.neilcsmith.praxis.live.project.ui; import java.awt.Image; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.swing.Action; import javax.swing.ListSelectionModel; import net.neilcsmith.praxis.live.project.DefaultPraxisProject; import net.neilcsmith.praxis.live.project.api.ExecutionLevel; import net.neilcsmith.praxis.live.project.api.PraxisProjectProperties; import org.openide.explorer.ExplorerManager; import org.openide.explorer.view.ListView; import org.openide.filesystems.FileChooserBuilder; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; import org.openide.loaders.DataObject; import org.openide.loaders.DataObjectNotFoundException; import org.openide.nodes.AbstractNode; import org.openide.nodes.Children; import org.openide.nodes.Node; import org.openide.util.Exceptions; /** * * @author Neil C Smith (http://neilcsmith.net) */ class FilesCustomizer extends javax.swing.JPanel implements ExplorerManager.Provider { private ExplorerManager manager; private DefaultPraxisProject project; private ExecutionLevel level; private PraxisProjectProperties props; private List<FileObject> files; private Files children; private Node root; /** Creates new form FilesCustomizer */ FilesCustomizer(DefaultPraxisProject project, ExecutionLevel level) { if (project == null || level == null) { throw new NullPointerException(); } this.project = project; this.level = level; props = project.getLookup().lookup(PraxisProjectProperties.class); manager = new ExplorerManager(); files = new ArrayList<FileObject>(); children = new Files(); root = new AbstractNode(children); refreshList(); manager.setRootContext(root); manager.addPropertyChangeListener(new ManagerListener()); initComponents(); ((ListView) fileList).setSelectionMode(ListSelectionModel.SINGLE_SELECTION); } final void refreshList() { files.clear(); if (props != null) { FileObject[] f = props.getProjectFiles(level); files.addAll(Arrays.asList(f)); } refreshView(); } private void refreshView() { children.setFiles(files); } FileObject[] getFiles() { return files.toArray(new FileObject[files.size()]); } /** 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() { fileList = new ListView(); addButton = new javax.swing.JButton(); removeButton = new javax.swing.JButton(); addButton.setText(org.openide.util.NbBundle.getMessage(FilesCustomizer.class, "FilesCustomizer.addButton.text")); // NOI18N addButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addButtonActionPerformed(evt); } }); removeButton.setText(org.openide.util.NbBundle.getMessage(FilesCustomizer.class, "FilesCustomizer.removeButton.text")); // NOI18N removeButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { removeButtonActionPerformed(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() .addComponent(fileList, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(addButton, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE) .addComponent(removeButton, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(fileList, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(addButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(removeButton) .addContainerGap(224, Short.MAX_VALUE)) ); }// </editor-fold>//GEN-END:initComponents private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed FileChooserBuilder fcb = new FileChooserBuilder(FilesCustomizer.class); // use project specific String? fcb.setFilesOnly(true); fcb.setTitle("Add file"); fcb.setApproveText("Add"); fcb.setDefaultWorkingDirectory(FileUtil.toFile(project.getProjectDirectory())); fcb.forceUseOfDefaultWorkingDirectory(true); File add = fcb.showOpenDialog(); if (add != null) { FileObject file = FileUtil.toFileObject(add); if (validateFile(file)) { files.add(file); refreshView(); } } }//GEN-LAST:event_addButtonActionPerformed private boolean validateFile(FileObject file) { return FileUtil.isParentOf(project.getProjectDirectory(), file); // && !isConfigFile(file); } private boolean isConfigFile(FileObject file) { FileObject configDir = project.getProjectDirectory().getFileObject("config"); if (configDir == null) { return false; } else { return FileUtil.isParentOf(configDir, file); } } private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed Node[] nodes = manager.getSelectedNodes(); for (Node node : nodes) { FileObject file = ((FileNode) node).getFile(); // if (!isConfigFile(file)) { files.remove(file); // } } refreshView(); }//GEN-LAST:event_removeButtonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton addButton; private javax.swing.JScrollPane fileList; private javax.swing.JButton removeButton; // End of variables declaration//GEN-END:variables @Override public ExplorerManager getExplorerManager() { return manager; } private class ManagerListener implements PropertyChangeListener { @Override public void propertyChange(PropertyChangeEvent evt) { if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) { if (manager.getSelectedNodes().length > 0) { removeButton.setEnabled(true); } else { removeButton.setEnabled(false); } // for (Node node : manager.getSelectedNodes()) { // if (!((FileNode)node).isConfig()) { // removeButton.setEnabled(true); // return; // } // } // removeButton.setEnabled(false); } } } private class Files extends Children.Keys<FileObject> { void setFiles(List<FileObject> keys) { setKeys(keys); } @Override protected Node[] createNodes(FileObject key) { Node node; if (isConfigFile(key)) { node = new FileNode(key, true); } else { node = new FileNode(key, false); } return new Node[]{node}; } } private class FileNode extends AbstractNode { private FileObject file; private boolean config; private FileNode(FileObject file, boolean config) { super(Children.LEAF); this.file = file; this.config = config; } @Override public Action[] getActions(boolean context) { return new Action[0]; } @Override public Image getIcon(int type) { try { return DataObject.find(file).getNodeDelegate().getIcon(type); } catch (DataObjectNotFoundException ex) { Exceptions.printStackTrace(ex); } return super.getIcon(type); } FileObject getFile() { return file; } boolean isConfig() { return config; } @Override public String getName() { return file.getPath(); } @Override public String getDisplayName() { return FileUtil.getRelativePath(project.getProjectDirectory(), file); } @Override public String getHtmlDisplayName() { if (config) { return "<i>" + getDisplayName() +"</i>"; } else { return null; } } } }