|
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.refactoring.ui; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.BeanInfo; import java.io.File; import java.io.IOException; import java.util.*; import javax.swing.*; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.plaf.basic.BasicComboBoxRenderer; import org.netbeans.api.java.project.JavaProjectConstants; import org.netbeans.api.project.*; import org.netbeans.api.project.ui.OpenProjects; import org.netbeans.modules.refactoring.api.ui.ParametersPanel; import org.netbeans.spi.java.project.support.ui.PackageView; import org.netbeans.spi.project.ActionProvider; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; import org.openide.loaders.DataObject; import org.openide.nodes.Children; import org.openide.nodes.Node; import org.openide.util.Lookup; import org.openide.util.NbBundle; import org.openide.util.Utilities; import org.openide.util.Lookup.Template; /** * * @author Jan Becicka */ public class MoveClassPanel extends JPanel implements ActionListener, DocumentListener { private static final ListCellRenderer CELL_RENDERER = new NodeCellRenderer(); private static final ListCellRenderer PROJECT_CELL_RENDERER = new ProjectCellRenderer(); private Project project; private ModelItem[] groupItems; private ParametersPanel parent; private FileObject fo; /** Creates new form SimpleTargetChooserGUI */ public MoveClassPanel(final ParametersPanel parent, String title, String headline, String startPackage, FileObject f) { setName(title); this.fo = f; this.parent = parent; initComponents(); setCombosEnabled(true); setThisClassVisible(false); labelHeadLine.setText(headline); rootComboBox.setRenderer( CELL_RENDERER ); packageComboBox.setRenderer( CELL_RENDERER ); projectsComboBox.setRenderer( PROJECT_CELL_RENDERER ); rootComboBox.addActionListener( this ); packageComboBox.addActionListener( this ); projectsComboBox.addActionListener( this ); Object textField = packageComboBox.getEditor().getEditorComponent(); if (textField instanceof JTextField) { ((JTextField) textField).getDocument().addDocumentListener(this); } project = preselectedProject(); initValues(startPackage); } public void initValues(String preselectedFolder ) { Project openProjects[] = OpenProjects.getDefault().getOpenProjects(); DefaultComboBoxModel projectsModel = new DefaultComboBoxModel( openProjects ); projectsComboBox.setModel( projectsModel ); projectsComboBox.setSelectedItem( project ); updateRoots(); updatePackages(); if (preselectedFolder != null) { packageComboBox.setSelectedItem(preselectedFolder); } // Determine the extension } public void requestFocus() { packageComboBox.requestFocus(); } public FileObject getRootFolder() { return ((ModelItem)rootComboBox.getSelectedItem()).group.getRootFolder(); } public String getPackageName() { String packageName = packageComboBox.getEditor().getItem().toString().trim(); if (NbBundle.getMessage(MoveClassPanel.class, "LBL_DefaultPackage").equals (packageName)) packageName = ""; if (NbBundle.getMessage(PackageView.class, "LBL_DefaultPackage").equals(packageName)) { packageName = ""; } return packageName.replace( '.', '/' ); // NOI18N } private void fireChange() { parent.stateChanged(null); } /** 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; labelProject = new javax.swing.JLabel(); projectsComboBox = new javax.swing.JComboBox(); labelLocation = new javax.swing.JLabel(); rootComboBox = new javax.swing.JComboBox(); labelPackage = new javax.swing.JLabel(); packageComboBox = new javax.swing.JComboBox(); labelHeadLine = new javax.swing.JLabel(); bottomPanel = new javax.swing.JPanel(); thisClassLabel = new javax.swing.JLabel(); thisClassTextField = new javax.swing.JTextField(); setLayout(new java.awt.GridBagLayout()); labelProject.setLabelFor(projectsComboBox); org.openide.awt.Mnemonics.setLocalizedText(labelProject, org.openide.util.NbBundle.getMessage(MoveClassPanel.class, "LBL_Project")); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0); add(labelProject, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(0, 6, 6, 0); add(projectsComboBox, gridBagConstraints); projectsComboBox.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/refactoring/ui/Bundle").getString("ACSD_projectsCombo")); labelLocation.setLabelFor(rootComboBox); org.openide.awt.Mnemonics.setLocalizedText(labelLocation, org.openide.util.NbBundle.getMessage(MoveClassPanel.class, "LBL_Location")); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0); add(labelLocation, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 2; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(0, 6, 6, 0); add(rootComboBox, gridBagConstraints); rootComboBox.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/refactoring/ui/Bundle").getString("ACSD_rootCombo")); labelPackage.setLabelFor(packageComboBox); org.openide.awt.Mnemonics.setLocalizedText(labelPackage, org.openide.util.NbBundle.getMessage(MoveClassPanel.class, "LBL_ToPackage")); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 3; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0); add(labelPackage, gridBagConstraints); packageComboBox.setEditable(true); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 3; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(0, 6, 6, 0); add(packageComboBox, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0); add(labelHeadLine, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 5; gridBagConstraints.gridwidth = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; add(bottomPanel, gridBagConstraints); org.openide.awt.Mnemonics.setLocalizedText(thisClassLabel, org.openide.util.NbBundle.getMessage(MoveClassPanel.class, "LBL_ThisClass")); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 4; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0); add(thisClassLabel, gridBagConstraints); thisClassTextField.setEditable(false); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 4; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 1.0; gridBagConstraints.insets = new java.awt.Insets(0, 6, 12, 0); add(thisClassTextField, gridBagConstraints); }//GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables protected javax.swing.JPanel bottomPanel; private javax.swing.JLabel labelHeadLine; private javax.swing.JLabel labelLocation; private javax.swing.JLabel labelPackage; private javax.swing.JLabel labelProject; private javax.swing.JComboBox packageComboBox; private javax.swing.JComboBox projectsComboBox; private javax.swing.JComboBox rootComboBox; private javax.swing.JLabel thisClassLabel; private javax.swing.JTextField thisClassTextField; // End of variables declaration//GEN-END:variables // ActionListener implementation ------------------------------------------- public void actionPerformed(ActionEvent e) { if (projectsComboBox == e.getSource()) { project = (Project) projectsComboBox.getSelectedItem(); updateRoots(); updatePackages(); } else if ( rootComboBox == e.getSource() ) { updatePackages(); } else if ( packageComboBox == e.getSource() ) { } } // DocumentListener implementation ----------------------------------------- public void changedUpdate(DocumentEvent e) { fireChange(); } public void insertUpdate(DocumentEvent e) { fireChange(); } public void removeUpdate(DocumentEvent e) { fireChange(); } // Private methods --------------------------------------------------------- private void updatePackages() { packageComboBox.setModel( new DefaultComboBoxModel( ((ModelItem)rootComboBox.getSelectedItem()).getChildren() ) ); } void setCombosEnabled(boolean enabled) { packageComboBox.setEnabled(enabled); rootComboBox.setEnabled(enabled); projectsComboBox.setEnabled(enabled); } void setThisClassVisible(boolean visible) { thisClassLabel.setVisible(visible); thisClassTextField.setVisible(visible); } void setThisClassName(String name) { thisClassTextField.setText(name); } private void updateRoots() { Sources sources = ProjectUtils.getSources(project); SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); if (groups == null) { groups = sources.getSourceGroups( Sources.TYPE_GENERIC ); } this.groupItems = new ModelItem[ groups.length ]; int preselectedItem = 0; for( int i = 0; i < groups.length; i++ ) { this.groupItems[i] = new ModelItem( groups[i] ); if (fo!=null) { try { if (groups[i].contains(fo)) { preselectedItem = i; } } catch (IllegalArgumentException e) { } } } // Setup comboboxes rootComboBox.setModel( new DefaultComboBoxModel( this.groupItems ) ); rootComboBox.setSelectedIndex(preselectedItem); } public File getFolder() { FileObject rootFo = getRootFolder(); File rootFile = FileUtil.toFile( rootFo ); if ( rootFile == null ) { return null; } String packageFileName = getPackageName().replace('.','/'); File folder = new File( rootFile, packageFileName ); // try { // folder.mkdir(); // } catch (SecurityException e) { // // } return folder; } // Private innerclasses ---------------------------------------------------- private static class ModelItem { private Node node; private SourceGroup group; private Icon icon; private ModelItem[] children; // For source groups public ModelItem( SourceGroup group ) { this.group = group; this.icon = null; // XXX Should be group getIcon() } // For packages public ModelItem( Node node ) { this.node = node; this.icon = new ImageIcon( node.getIcon( BeanInfo.ICON_COLOR_16x16 ) ); } public String getDisplayName() { if ( group != null ) { return group.getDisplayName(); } else { return node.getName(); } } public Icon getIcon() { return icon; } public String toString() { return getDisplayName(); } public ModelItem[] getChildren() { if ( group == null ) { return null; } else { if ( children == null ) { Children ch = PackageView.createPackageView(group).getChildren(); Node nodes[] = ch.getNodes( true ); children = new ModelItem[ nodes.length ]; for( int i = 0; i < nodes.length; i++ ) { children[i] = new ModelItem( nodes[i] ); } } return children; } } } private Project preselectedProject() { Lookup context = Utilities.actionsGlobalContext(); Project preselectedProject = null; // if ( activatedNodes != null && activatedNodes.length != 0 ) { Project[] projects = getProjectsFromLookup( context, null ); if ( projects.length > 0 ) { preselectedProject = projects[0]; } if ( preselectedProject == null ) { preselectedProject = OpenProjects.getDefault().getOpenProjects()[0]; } if ( preselectedProject == null ) { assert false : "Action should be disabled"; // NOI18N } return preselectedProject; } private static Project[] getProjectsFromLookup( Lookup lookup, String command ) { Set result = new HashSet(); // First find out whether there is a project directly in the Lookup Collection projects = lookup.lookup( new Template( Project.class ) ).allInstances(); for( Iterator it = projects.iterator(); it.hasNext(); ) { Project p = (Project)it.next(); if (command == null || commandSupported(p, command, lookup)) { result.add(p); } } // Now try to guess the project from dataobjects Collection dataObjects = lookup.lookup( new Template( DataObject.class ) ).allInstances(); for( Iterator it = dataObjects.iterator(); it.hasNext(); ) { DataObject dObj = (DataObject)it.next(); FileObject fObj = dObj.getPrimaryFile(); Project p = FileOwnerQuery.getOwner(fObj); if ( p != null ) { if ( command != null && !commandSupported( p, command, lookup ) ) { continue; } result.add( p ); } } Project[] projectsArray = new Project[ result.size() ]; result.toArray( projectsArray ); return projectsArray; } /** * Tests whether given command is available on the project and whether * the action as to be enabled in current Context * @param project Project to test * @param command Command for test * @param context Lookup representing current context or null if context * does not matter. */ private static boolean commandSupported( Project project, String command, Lookup context ) { //We have to look whether the command is supported by the project ActionProvider ap = (ActionProvider)project.getLookup().lookup( ActionProvider.class ); if ( ap != null ) { List commands = Arrays.asList( ap.getSupportedActions() ); if ( commands.contains( command ) ) { if (context == null || ap.isActionEnabled(command, context)) { return true; } } } return false; } private static class NodeCellRenderer extends BasicComboBoxRenderer implements ListCellRenderer { public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { BasicComboBoxRenderer cbr = (BasicComboBoxRenderer)super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus ); if ( value != null ) { ModelItem item = (ModelItem)value; cbr.setText( item.getDisplayName() ); cbr.setIcon( item.getIcon() ); } return cbr; } } private static class ProjectCellRenderer extends BasicComboBoxRenderer implements ListCellRenderer { public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { BasicComboBoxRenderer cbr = (BasicComboBoxRenderer)super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus ); if ( value != null ) { ProjectInformation pi = ProjectUtils.getInformation((Project)value); cbr.setText(pi.getDisplayName()); cbr.setIcon(pi.getIcon()); } return cbr; } } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 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.