|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.project.ui; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.io.File; import java.net.URI; import java.text.Collator; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import javax.swing.DefaultListModel; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFileChooser; import javax.swing.SwingUtilities; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileSystemView; import javax.swing.filechooser.FileView; import org.netbeans.api.project.Project; import org.netbeans.api.project.ProjectManager; import org.netbeans.api.project.ProjectUtils; import org.netbeans.api.queries.CollocationQuery; import org.netbeans.modules.project.ui.OpenProjectListSettings; import org.netbeans.spi.project.SubprojectProvider; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; import org.openide.util.NbBundle; import org.openide.util.RequestProcessor; import org.openide.util.Utilities; /** * * @author phrebejk */ public class ProjectChooserAccessory extends javax.swing.JPanel implements ActionListener, PropertyChangeListener { private RequestProcessor.Task updateSubprojectsTask; private ModelUpdater modelUpdater; /** Creates new form ProjectChooserAccessory */ public ProjectChooserAccessory( JFileChooser chooser, boolean isOpenSubprojects, boolean isOpenAsMain ) { initComponents(); modelUpdater = new ModelUpdater(); updateSubprojectsTask = new RequestProcessor( "ProjectChooserAccesoryModelUpdater" ).create( modelUpdater ); updateSubprojectsTask.setPriority( Thread.MIN_PRIORITY ); // Listen on the subproject checkbox to change the option accordingly jCheckBoxSubprojects.setSelected( isOpenSubprojects ); jCheckBoxSubprojects.addActionListener( this ); // Listen on the main checkbox to change the option accordingly jCheckBoxMain.setSelected( isOpenAsMain ); jCheckBoxMain.addActionListener( this ); // Listen on the chooser to update the Accessory chooser.addPropertyChangeListener( this ); // Set default list model for the subprojects list jListSubprojects.setModel( new DefaultListModel() ); // Disable the Accessory. JFileChooser does not select a file // by default setAccessoryEnablement( false ); } /** 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. */ private void initComponents() {//GEN-BEGIN:initComponents java.awt.GridBagConstraints gridBagConstraints; jLabelProjectName = new javax.swing.JLabel(); jTextFieldProjectName = new javax.swing.JTextField(); jCheckBoxMain = new javax.swing.JCheckBox(); jCheckBoxSubprojects = new javax.swing.JCheckBox(); jScrollPaneSubprojects = new javax.swing.JScrollPane(); jListSubprojects = new javax.swing.JList(); setLayout(new java.awt.GridBagLayout()); setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(0, 12, 0, 0))); jLabelProjectName.setLabelFor(jTextFieldProjectName); org.openide.awt.Mnemonics.setLocalizedText(jLabelProjectName, org.openide.util.NbBundle.getMessage(ProjectChooserAccessory.class, "LBL_PrjChooser_ProjectName_Label")); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0); add(jLabelProjectName, gridBagConstraints); jTextFieldProjectName.setEditable(false); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0); add(jTextFieldProjectName, gridBagConstraints); org.openide.awt.Mnemonics.setLocalizedText(jCheckBoxMain, org.openide.util.NbBundle.getMessage(ProjectChooserAccessory.class, "LBL_PrjChooser_Main_CheckBox")); jCheckBoxMain.setMargin(new java.awt.Insets(2, 0, 2, 2)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0); add(jCheckBoxMain, gridBagConstraints); jCheckBoxMain.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ProjectChooserAccessory.class, "ACSD_ProjectChooserAccessory_jCheckBoxMain")); org.openide.awt.Mnemonics.setLocalizedText(jCheckBoxSubprojects, org.openide.util.NbBundle.getMessage(ProjectChooserAccessory.class, "LBL_PrjChooser_Subprojects_CheckBox")); jCheckBoxSubprojects.setMargin(new java.awt.Insets(2, 0, 2, 2)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0); add(jCheckBoxSubprojects, gridBagConstraints); jCheckBoxSubprojects.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ProjectChooserAccessory.class, "ACSD_ProjectChooserAccessory_jCheckBoxSubprojects")); jListSubprojects.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); jListSubprojects.setEnabled(false); jScrollPaneSubprojects.setViewportView(jListSubprojects); jListSubprojects.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ProjectChooserAccessory.class, "ACSN_ProjectChooserAccessory_jListSubprojects")); jListSubprojects.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ProjectChooserAccessory.class, "ACSD_ProjectChooserAccessory_jListSubprojects")); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; add(jScrollPaneSubprojects, gridBagConstraints); }//GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JCheckBox jCheckBoxMain; private javax.swing.JCheckBox jCheckBoxSubprojects; private javax.swing.JLabel jLabelProjectName; private javax.swing.JList jListSubprojects; private javax.swing.JScrollPane jScrollPaneSubprojects; private javax.swing.JTextField jTextFieldProjectName; // End of variables declaration//GEN-END:variables // Implementation of action listener --------------------------------------- public void actionPerformed( ActionEvent e ) { if ( e.getSource() == jCheckBoxSubprojects ) { OpenProjectListSettings.getInstance().setOpenSubprojects( jCheckBoxSubprojects.isSelected() ); } else if ( e.getSource() == jCheckBoxMain ) { OpenProjectListSettings.getInstance().setOpenAsMain( jCheckBoxMain.isSelected() ); } } // Implementayion of PropertyChange listener public void propertyChange( PropertyChangeEvent e ) { if ( JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals( e.getPropertyName() ) ) { // We have to update the Accessory JFileChooser chooser = (JFileChooser)e.getSource(); File rawdir = chooser.getSelectedFile(); File dir = rawdir != null ? FileUtil.normalizeFile(rawdir) : null; DefaultListModel spListModel = (DefaultListModel)jListSubprojects.getModel(); Project project = null; if ( isProjectDir( dir ) ) { project = getProject( dir ); // may still be null } if (project != null) { // Enable all components acessory setAccessoryEnablement( true ); jTextFieldProjectName.setText(ProjectUtils.getInformation(project).getDisplayName()); spListModel.clear(); modelUpdater.project = project; updateSubprojectsTask.schedule( 100 ); } else { // Clear the accessory data if the dir is not project dir jTextFieldProjectName.setText( "" ); // NOI18N modelUpdater.project = null; spListModel.clear(); // Disable all components in accessory setAccessoryEnablement( false ); } } else if ( JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals( e.getPropertyName() ) ) { // Selection lost => disable accessory setAccessoryEnablement( false ); } } // Private methods --------------------------------------------------------- private static boolean isProjectDir( File dir ) { boolean retVal = false; if (dir != null) { FileObject fo = convertToValidDir(dir); if (fo != null) { if ( Utilities.isUnix() && fo.getParent() != null && fo.getParent().getParent() == null ) { retVal = false; // Ignore all subfolders of / on unixes (e.g. /net, /proc) } else { retVal = ProjectManager.getDefault().isProject( fo ); } } } return retVal; } private static FileObject convertToValidDir(File f) { FileObject fo; File testFile = new File( f.getPath() ); if ( testFile == null || testFile.getParent() == null ) { // BTW this means that roots of file systems can't be project // directories. return null; } /**ATTENTION: on Windows may occure dir.isDirectory () == dir.isFile () == true then * its used testFile instead of dir. */ if ( !testFile.isDirectory() ) { return null; } fo = FileUtil.toFileObject(FileUtil.normalizeFile(f)); return fo; } private static Project getProject( File dir ) { return OpenProjectList.fileToProject( dir ); } private void setAccessoryEnablement( boolean enable ) { jLabelProjectName.setEnabled( enable ); jTextFieldProjectName.setEnabled( enable ); jCheckBoxMain.setEnabled( enable ); jCheckBoxSubprojects.setEnabled( enable ); jScrollPaneSubprojects.setEnabled( enable ); } /** Gets all subprojects recursively */ private void addSubprojects( Project p, List result ) { SubprojectProvider spp = (SubprojectProvider)p.getLookup().lookup( SubprojectProvider.class ); if ( spp == null ) { return; } for( Iterator/* |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.