@Nullable public MoveDestination selectDirectory( final PackageWrapper targetPackage, final boolean showChooserWhenDefault) { final DirectoryChooser.ItemWrapper selectedItem = (DirectoryChooser.ItemWrapper) getComboBox().getSelectedItem(); if (selectedItem == null || selectedItem == NULL_WRAPPER) { return new MultipleRootsMoveDestination(targetPackage); } final PsiDirectory selectedPsiDirectory = selectedItem.getDirectory(); VirtualFile selectedDestination = selectedPsiDirectory.getVirtualFile(); if (showChooserWhenDefault && selectedDestination == myInitialTargetDirectory.getVirtualFile() && mySourceRoots.length > 1) { selectedDestination = MoveClassesOrPackagesUtil.chooseSourceRoot( targetPackage, mySourceRoots, myInitialTargetDirectory); } if (selectedDestination == null) return null; return new AutocreatingSingleSourceRootMoveDestination(targetPackage, selectedDestination); }
private void setComboboxModel( final JComboBox comboBox, final VirtualFile initialTargetDirectorySourceRoot, final VirtualFile oldSelection, final ProjectFileIndex fileIndex, final VirtualFile[] sourceRoots, final Project project, final boolean forceIncludeAll, final Pass<String> updateErrorMessage) { final LinkedHashSet<PsiDirectory> targetDirectories = new LinkedHashSet<PsiDirectory>(); final HashMap<PsiDirectory, String> pathsToCreate = new HashMap<PsiDirectory, String>(); MoveClassesOrPackagesUtil.buildDirectoryList( new PackageWrapper(PsiManager.getInstance(project), getTargetPackage()), sourceRoots, targetDirectories, pathsToCreate); if (!forceIncludeAll && targetDirectories.size() > pathsToCreate.size()) { targetDirectories.removeAll(pathsToCreate.keySet()); } final ArrayList<DirectoryChooser.ItemWrapper> items = new ArrayList<DirectoryChooser.ItemWrapper>(); DirectoryChooser.ItemWrapper initial = null; DirectoryChooser.ItemWrapper oldOne = null; for (PsiDirectory targetDirectory : targetDirectories) { DirectoryChooser.ItemWrapper itemWrapper = new DirectoryChooser.ItemWrapper(targetDirectory, pathsToCreate.get(targetDirectory)); items.add(itemWrapper); final VirtualFile sourceRootForFile = fileIndex.getSourceRootForFile(targetDirectory.getVirtualFile()); if (sourceRootForFile == initialTargetDirectorySourceRoot) { initial = itemWrapper; } else if (sourceRootForFile == oldSelection) { oldOne = itemWrapper; } } items.add(NULL_WRAPPER); final DirectoryChooser.ItemWrapper selection = chooseSelection(initialTargetDirectorySourceRoot, fileIndex, items, initial, oldOne); final ComboBoxModel model = comboBox.getModel(); if (model instanceof CollectionComboBoxModel) { boolean sameModel = model.getSize() == items.size(); if (sameModel) { for (int i = 0; i < items.size(); i++) { final DirectoryChooser.ItemWrapper oldItem = (DirectoryChooser.ItemWrapper) model.getElementAt(i); final DirectoryChooser.ItemWrapper itemWrapper = items.get(i); if (!areItemsEquivalent(oldItem, itemWrapper)) { sameModel = false; break; } } } if (sameModel) { if (areItemsEquivalent( (DirectoryChooser.ItemWrapper) comboBox.getSelectedItem(), selection)) { return; } } } updateErrorMessage(updateErrorMessage, fileIndex, selection); Collections.sort( items, new Comparator<DirectoryChooser.ItemWrapper>() { @Override public int compare(DirectoryChooser.ItemWrapper o1, DirectoryChooser.ItemWrapper o2) { if (o1 == NULL_WRAPPER) return -1; if (o2 == NULL_WRAPPER) return 1; return o1.getRelativeToProjectPath().compareToIgnoreCase(o2.getRelativeToProjectPath()); } }); comboBox.setModel(new CollectionComboBoxModel(items, selection)); }