1 19 20 package org.netbeans.modules.refactoring.java.ui; 21 22 import java.awt.Component ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.text.Collator ; 26 import java.util.Arrays ; 27 import java.util.Comparator ; 28 import javax.swing.DefaultComboBoxModel ; 29 import javax.swing.JLabel ; 30 import javax.swing.JList ; 31 import javax.swing.JPanel ; 32 import javax.swing.JTextField ; 33 import javax.swing.ListCellRenderer ; 34 import javax.swing.event.ChangeListener ; 35 import javax.swing.event.DocumentEvent ; 36 import javax.swing.event.DocumentListener ; 37 import javax.swing.plaf.UIResource ; 38 import org.netbeans.api.java.project.JavaProjectConstants; 39 import org.netbeans.api.project.FileOwnerQuery; 40 import org.netbeans.api.project.Project; 41 import org.netbeans.api.project.ProjectInformation; 42 import org.netbeans.api.project.ProjectUtils; 43 import org.netbeans.api.project.SourceGroup; 44 import org.netbeans.api.project.Sources; 45 import org.netbeans.api.project.ui.OpenProjects; 46 import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel; 47 import org.netbeans.spi.java.project.support.ui.PackageView; 48 import org.openide.filesystems.FileObject; 49 50 54 public class MoveClassPanel extends JPanel implements ActionListener , DocumentListener ,CustomRefactoringPanel { 55 56 private static final ListCellRenderer GROUP_CELL_RENDERER = new GroupCellRenderer(); 57 private static final ListCellRenderer PROJECT_CELL_RENDERER = new ProjectCellRenderer(); 58 59 private Project project; 60 private ChangeListener parent; 61 private FileObject fo; 62 private SourceGroup[] groups; 63 64 public MoveClassPanel(final ChangeListener parent, String startPackage, String headLine, FileObject f) { 65 this.fo = f; 66 this.parent = parent; 67 initComponents(); 68 setCombosEnabled(true); 69 labelHeadLine.setText(headLine); 70 rootComboBox.setRenderer(GROUP_CELL_RENDERER); 71 packageComboBox.setRenderer(PackageView.listRenderer()); 72 projectsComboBox.setRenderer( PROJECT_CELL_RENDERER ); 73 74 rootComboBox.addActionListener( this ); 75 packageComboBox.addActionListener( this ); 76 projectsComboBox.addActionListener( this ); 77 78 Object textField = packageComboBox.getEditor().getEditorComponent(); 79 if (textField instanceof JTextField ) { 80 ((JTextField ) textField).getDocument().addDocumentListener(this); 81 } 82 83 project = fo != null ? FileOwnerQuery.getOwner(fo):OpenProjects.getDefault().getOpenProjects()[0]; 84 initValues(startPackage); 85 } 86 87 private boolean initialized = false; 88 public void initialize() { 89 if (initialized) 90 return ; 91 initialized = true; 93 } 94 95 public void initValues(String preselectedFolder ) { 96 97 Project openProjects[] = OpenProjects.getDefault().getOpenProjects(); 98 Arrays.sort( openProjects, new ProjectByDisplayNameComparator()); 99 DefaultComboBoxModel projectsModel = new DefaultComboBoxModel ( openProjects ); 100 projectsComboBox.setModel( projectsModel ); 101 projectsComboBox.setSelectedItem( project ); 102 103 updateRoots(); 104 updatePackages(); 105 if (preselectedFolder != null) { 106 packageComboBox.setSelectedItem(preselectedFolder); 107 } 108 } 110 111 public void requestFocus() { 112 packageComboBox.requestFocus(); 113 } 114 115 public FileObject getRootFolder() { 116 return ((SourceGroup) rootComboBox.getSelectedItem()).getRootFolder(); 117 } 118 119 public String getPackageName() { 120 String packageName = packageComboBox.getEditor().getItem().toString(); 121 return packageName; } 123 124 private void fireChange() { 125 parent.stateChanged(null); 126 } 127 128 133 private void initComponents() { 135 java.awt.GridBagConstraints gridBagConstraints; 136 137 labelProject = new javax.swing.JLabel (); 138 projectsComboBox = new javax.swing.JComboBox (); 139 labelLocation = new javax.swing.JLabel (); 140 rootComboBox = new javax.swing.JComboBox (); 141 labelPackage = new javax.swing.JLabel (); 142 packageComboBox = new javax.swing.JComboBox (); 143 bottomPanel = new javax.swing.JPanel (); 144 labelHeadLine = new javax.swing.JLabel (); 145 updateReferencesCheckBox = new javax.swing.JCheckBox (); 146 147 setLayout(new java.awt.GridBagLayout ()); 148 149 labelProject.setLabelFor(projectsComboBox); 150 org.openide.awt.Mnemonics.setLocalizedText(labelProject, org.openide.util.NbBundle.getMessage(MoveClassPanel.class, "LBL_Project")); labelProject.setToolTipText("null"); 152 gridBagConstraints = new java.awt.GridBagConstraints (); 153 gridBagConstraints.gridx = 0; 154 gridBagConstraints.gridy = 1; 155 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 156 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 157 add(labelProject, gridBagConstraints); 158 159 gridBagConstraints = new java.awt.GridBagConstraints (); 160 gridBagConstraints.gridx = 1; 161 gridBagConstraints.gridy = 1; 162 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 163 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 164 gridBagConstraints.weightx = 1.0; 165 gridBagConstraints.insets = new java.awt.Insets (0, 6, 6, 0); 166 add(projectsComboBox, gridBagConstraints); 167 java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/refactoring/java/ui/Bundle"); projectsComboBox.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_projectsCombo")); 170 labelLocation.setLabelFor(rootComboBox); 171 org.openide.awt.Mnemonics.setLocalizedText(labelLocation, org.openide.util.NbBundle.getMessage(MoveClassPanel.class, "LBL_Location")); gridBagConstraints = new java.awt.GridBagConstraints (); 173 gridBagConstraints.gridx = 0; 174 gridBagConstraints.gridy = 2; 175 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 176 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 177 add(labelLocation, gridBagConstraints); 178 179 gridBagConstraints = new java.awt.GridBagConstraints (); 180 gridBagConstraints.gridx = 1; 181 gridBagConstraints.gridy = 2; 182 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 183 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 184 gridBagConstraints.weightx = 1.0; 185 gridBagConstraints.insets = new java.awt.Insets (0, 6, 6, 0); 186 add(rootComboBox, gridBagConstraints); 187 rootComboBox.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_rootCombo")); 189 labelPackage.setLabelFor(packageComboBox); 190 org.openide.awt.Mnemonics.setLocalizedText(labelPackage, org.openide.util.NbBundle.getMessage(MoveClassPanel.class, "LBL_ToPackage")); gridBagConstraints = new java.awt.GridBagConstraints (); 192 gridBagConstraints.gridx = 0; 193 gridBagConstraints.gridy = 3; 194 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 195 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 196 add(labelPackage, gridBagConstraints); 197 198 packageComboBox.setEditable(true); 199 gridBagConstraints = new java.awt.GridBagConstraints (); 200 gridBagConstraints.gridx = 1; 201 gridBagConstraints.gridy = 3; 202 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 203 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 204 gridBagConstraints.weightx = 1.0; 205 gridBagConstraints.insets = new java.awt.Insets (0, 6, 6, 0); 206 add(packageComboBox, gridBagConstraints); 207 208 gridBagConstraints = new java.awt.GridBagConstraints (); 209 gridBagConstraints.gridx = 0; 210 gridBagConstraints.gridy = 4; 211 gridBagConstraints.gridwidth = 2; 212 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 213 gridBagConstraints.weightx = 1.0; 214 gridBagConstraints.weighty = 1.0; 215 add(bottomPanel, gridBagConstraints); 216 217 labelHeadLine.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 6, 1)); 218 gridBagConstraints = new java.awt.GridBagConstraints (); 219 gridBagConstraints.gridx = 0; 220 gridBagConstraints.gridy = 0; 221 gridBagConstraints.gridwidth = 2; 222 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 223 add(labelHeadLine, gridBagConstraints); 224 225 org.openide.awt.Mnemonics.setLocalizedText(updateReferencesCheckBox, org.openide.util.NbBundle.getBundle(MoveClassPanel.class).getString("LBL_MoveWithoutReferences")); updateReferencesCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 4, 0, 4)); 227 updateReferencesCheckBox.setMargin(new java.awt.Insets (2, 2, 0, 2)); 228 gridBagConstraints = new java.awt.GridBagConstraints (); 229 gridBagConstraints.gridx = 0; 230 gridBagConstraints.gridy = 5; 231 gridBagConstraints.gridwidth = 2; 232 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 233 add(updateReferencesCheckBox, gridBagConstraints); 234 235 } 237 238 239 protected javax.swing.JPanel bottomPanel; 241 private javax.swing.JLabel labelHeadLine; 242 private javax.swing.JLabel labelLocation; 243 private javax.swing.JLabel labelPackage; 244 private javax.swing.JLabel labelProject; 245 private javax.swing.JComboBox packageComboBox; 246 private javax.swing.JComboBox projectsComboBox; 247 private javax.swing.JComboBox rootComboBox; 248 private javax.swing.JCheckBox updateReferencesCheckBox; 249 251 253 public void actionPerformed(ActionEvent e) { 254 if (projectsComboBox == e.getSource()) { 255 project = (Project) projectsComboBox.getSelectedItem(); 256 updateRoots(); 257 updatePackages(); 258 } else 259 if ( rootComboBox == e.getSource() ) { 260 updatePackages(); 261 } 262 else if ( packageComboBox == e.getSource() ) { 263 } 264 } 265 266 268 public void changedUpdate(DocumentEvent e) { 269 fireChange(); 270 } 271 272 public void insertUpdate(DocumentEvent e) { 273 fireChange(); 274 } 275 276 public void removeUpdate(DocumentEvent e) { 277 fireChange(); 278 } 279 280 282 private void updatePackages() { 283 SourceGroup g = (SourceGroup) rootComboBox.getSelectedItem(); 284 packageComboBox.setModel(PackageView.createListView(g)); 285 } 286 287 void setCombosEnabled(boolean enabled) { 288 packageComboBox.setEnabled(enabled); 289 rootComboBox.setEnabled(enabled); 290 projectsComboBox.setEnabled(enabled); 291 updateReferencesCheckBox.setVisible(!enabled); 292 } 293 294 public boolean isUpdateReferences() { 295 if (updateReferencesCheckBox.isVisible() && updateReferencesCheckBox.isSelected()) 296 return false; 297 return true; 298 } 299 300 private void updateRoots() { 301 Sources sources = ProjectUtils.getSources(project); 302 groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 303 if (groups.length == 0) { 304 groups = sources.getSourceGroups( Sources.TYPE_GENERIC ); 307 } 308 309 int preselectedItem = 0; 310 for( int i = 0; i < groups.length; i++ ) { 311 if (fo!=null) { 312 try { 313 if (groups[i].contains(fo)) { 314 preselectedItem = i; 315 } 316 } catch (IllegalArgumentException e) { 317 } 319 } 320 } 321 322 rootComboBox.setModel(new DefaultComboBoxModel (groups)); 324 rootComboBox.setSelectedIndex(preselectedItem); 325 } 326 327 private abstract static class BaseCellRenderer extends JLabel implements ListCellRenderer , UIResource { 328 329 public BaseCellRenderer () { 330 setOpaque(true); 331 } 332 333 public String getName() { 335 String name = super.getName(); 336 return name == null ? "ComboBox.renderer" : name; } 338 } 339 340 341 static class GroupCellRenderer extends BaseCellRenderer { 342 343 public Component getListCellRendererComponent( 344 JList list, 345 Object value, 346 int index, 347 boolean isSelected, 348 boolean cellHasFocus) { 349 350 setName("ComboBox.listRenderer"); 353 SourceGroup g = (SourceGroup) value; 354 setText(g.getDisplayName()); 355 setIcon(g.getIcon(false)); 356 357 if ( isSelected ) { 358 setBackground(list.getSelectionBackground()); 359 setForeground(list.getSelectionForeground()); 360 } 361 else { 362 setBackground(list.getBackground()); 363 setForeground(list.getForeground()); 364 } 365 366 return this; 367 } 368 } 369 370 371 static class ProjectCellRenderer extends BaseCellRenderer { 372 373 public Component getListCellRendererComponent( 374 JList list, 375 Object value, 376 int index, 377 boolean isSelected, 378 boolean cellHasFocus) { 379 380 setName("ComboBox.listRenderer"); 383 if ( value != null ) { 384 ProjectInformation pi = ProjectUtils.getInformation((Project)value); 385 setText(pi.getDisplayName()); 386 setIcon(pi.getIcon()); 387 } 388 389 if ( isSelected ) { 390 setBackground(list.getSelectionBackground()); 391 setForeground(list.getSelectionForeground()); 392 } 393 else { 394 setBackground(list.getBackground()); 395 setForeground(list.getForeground()); 396 } 397 398 return this; 399 } 400 } 401 private static class ProjectByDisplayNameComparator implements Comparator { 404 405 private static Comparator COLLATOR = Collator.getInstance(); 406 407 public int compare(Object o1, Object o2) { 408 409 if ( !( o1 instanceof Project ) ) { 410 return 1; 411 } 412 if ( !( o2 instanceof Project ) ) { 413 return -1; 414 } 415 416 Project p1 = (Project)o1; 417 Project p2 = (Project)o2; 418 419 return COLLATOR.compare(ProjectUtils.getInformation(p1).getDisplayName(), ProjectUtils.getInformation(p2).getDisplayName()); 420 } 421 } 422 423 public Component getComponent() { 424 return this; 425 } 426 } 427 | Popular Tags |