/**
  * Prepare branch descriptors for existing configuration
  *
  * @param target the target
  * @param roots the vcs root
  * @return the list of branch descriptors
  * @throws VcsException in case of error
  */
 private List<BranchDescriptor> prepareBranchDescriptors(
     BranchConfiguration target, List<VirtualFile> roots) throws VcsException {
   Map<String, String> map =
       target == null ? Collections.<String, String>emptyMap() : target.getReferences();
   List<BranchDescriptor> rc = new ArrayList<BranchDescriptor>();
   for (VirtualFile root : roots) {
     BranchDescriptor d = new BranchDescriptor();
     d.root = root;
     d.storedReference = map.remove(root.getPath());
     if (d.storedReference != null) {
       d.storedRoot = d.root.getPath();
     }
     d.currentReference = myConfig.describeRoot(root);
     if (d.storedReference != null && !myModify) {
       d.referenceToCheckout = d.storedReference;
     } else {
       d.referenceToCheckout = d.currentReference;
     }
     Branch.listAsStrings(myProject, root, false, true, d.existingBranches, null);
     Branch.listAsStrings(myProject, root, true, true, d.referencesToSelect, null);
     d.updateStatus();
     rc.add(d);
   }
   for (Map.Entry<String, String> m : map.entrySet()) {
     String root = m.getKey();
     String ref = m.getValue();
     BranchDescriptor d = new BranchDescriptor();
     d.storedReference = ref;
     d.storedRoot = root;
     d.referenceToCheckout = ref;
     d.updateStatus();
     rc.add(d);
   }
   return rc;
 }
 /** Verify dialog state */
 private void verify() {
   String text = myNameTextField.getText().trim();
   if (text.length() == 0) {
     setError("Empty configuration name is not allowed.");
     return;
   } else if (myTarget != null && text.equals(myTarget.getName())) {
   } else if (myExistingConfigNames.contains(text)) {
     setError("There is another configuration with the same name");
     return;
   }
   for (BranchDescriptor d : myBranches) {
     switch (d.status) {
       case BRANCH_NAME_EXISTS:
         setError("Duplicate branch name for root " + d.getRoot());
         return;
       case INVALID_BRANCH_NAME:
         setError("Invalid branch name for root " + d.getRoot());
         return;
       case BAD_REVISION:
         setError("Invalid revision for root " + d.getRoot());
         return;
       case MISSING_REVISION:
         setError("The revision must be specified for root " + d.getRoot());
         return;
       case CHECKOUT_NEEDED:
       case NO_ACTION:
       case REMOVED_ROOT:
         break;
       default:
         throw new RuntimeException("Unexpected status: " + d.status);
     }
   }
   setError(null);
 }
  /**
   * The constructor
   *
   * @param project the project
   * @param target the target configuration
   * @param allChanges the all changes
   * @param roots the collection of roots
   * @param remoteBranch the remote branch
   * @param config the configuration
   * @param isModify the modify flag
   * @throws VcsException if there is a problem with detecting the current state
   */
  protected SwitchBranchesDialog(
      Project project,
      final BranchConfiguration target,
      final List<Change> allChanges,
      List<VirtualFile> roots,
      String remoteBranch,
      final BranchConfigurations config,
      boolean isModify)
      throws VcsException {
    super(project, true);
    setTitle(isModify ? "Modify Branch Configuration" : "Checkout Branch Configuration");
    assert (remoteBranch == null) || (target == null)
        : "There should be no target for remote branch";
    myTarget = target;
    myConfig = config;
    myModify = isModify;
    myProject = project;
    VirtualFile baseDir = project.getBaseDir();
    myBaseFile = baseDir == null ? null : new File(baseDir.getPath());
    myExistingConfigNames = myConfig.getConfigurationNames();
    myChangesTree =
        new ChangesTreeList<Change>(
            myProject,
            Collections.<Change>emptyList(),
            !myModify,
            true,
            null,
            RemoteRevisionsCache.getInstance(project).getChangesNodeDecorator()) {
          protected DefaultTreeModel buildTreeModel(
              final List<Change> changes, ChangeNodeDecorator changeNodeDecorator) {
            TreeModelBuilder builder = new TreeModelBuilder(myProject, false);
            return builder.buildModel(changes, changeNodeDecorator);
          }

          protected List<Change> getSelectedObjects(final ChangesBrowserNode<Change> node) {
            return node.getAllChangesUnder();
          }

          @Nullable
          protected Change getLeadSelectedObject(final ChangesBrowserNode node) {
            final Object o = node.getUserObject();
            if (o instanceof Change) {
              return (Change) o;
            }
            return null;
          }
        };
    if (remoteBranch != null) {
      myBranches = prepareBranchesForRemote(remoteBranch, roots);
    } else {
      myBranches = prepareBranchDescriptors(target, roots);
    }
    Collections.sort(
        myBranches,
        new Comparator<BranchDescriptor>() {
          @Override
          public int compare(BranchDescriptor o1, BranchDescriptor o2) {
            return o1.getRoot().compareTo(o2.getRoot());
          }
        });
    if (target == null) {
      myNameTextField.setText(generateNewConfigurationName());
    } else {
      myNameTextField.setText(target.getName());
    }
    myChangesTree.setChangesToDisplay(allChanges);
    myChangesTree.setIncludedChanges(Collections.<Change>emptyList());
    myChangesPanel.add(myChangesTree, BorderLayout.CENTER);
    myChangesLabel.setLabelFor(myChangesTree);
    if (myModify) {
      myChangesLabel.setText("Changes in the current configuration");
    }
    RootTableModel tableModel = new RootTableModel();
    myBranchesTable.setModel(tableModel);
    myBranchesTable.setDefaultRenderer(Pair.class, new PairTableRenderer());
    final TableColumnModel columns = myBranchesTable.getColumnModel();
    final PairTableRenderer renderer = new PairTableRenderer();
    for (Enumeration<TableColumn> cs = columns.getColumns(); cs.hasMoreElements(); ) {
      cs.nextElement().setCellRenderer(renderer);
    }
    TableColumn revisionColumn = columns.getColumn(RootTableModel.REVISION_COLUMN);
    revisionColumn.setCellEditor(new ReferenceEditor());
    TableColumn branchColumn = columns.getColumn(RootTableModel.NEW_BRANCH_COLUMN);
    branchColumn.setCellEditor(new BranchNameEditor());
    myNameTextField
        .getDocument()
        .addDocumentListener(
            new DocumentAdapter() {
              @Override
              protected void textChanged(DocumentEvent e) {
                verify();
              }
            });
    tableModel.addTableModelListener(
        new TableModelListener() {
          @Override
          public void tableChanged(TableModelEvent e) {
            verify();
          }
        });
    verify();
    init();
  }