private void createNewConfiguration() {
   if (!saveSelectedConfiguration()) return;
   myList.setSelectedValue(null, false);
   CvsRootConfiguration newConfig =
       CvsApplicationLevelConfiguration.createNewConfiguration(
           CvsApplicationLevelConfiguration.getInstance());
   myModel.addElement(newConfig);
   myList.setSelectedValue(newConfig, true);
 }
 private void removeSelectedConfiguration() {
   int oldSelection = myList.getSelectedIndex();
   myModel.removeElement(mySelection);
   int size = myList.getModel().getSize();
   int newSelection = oldSelection < size ? oldSelection : size - 1;
   if (newSelection >= 0 && newSelection < size) {
     myList.setSelectedIndex(newSelection);
   }
 }
    public FlatWelcomeScreen() {
      super(new BorderLayout());
      mySlidingPanel.add("root", this);
      setBackground(getMainBackground());
      if (RecentProjectsManager.getInstance()
              .getRecentProjectsActions(false, isUseProjectGroups())
              .length
          > 0) {
        final JComponent recentProjects = createRecentProjects();
        add(recentProjects, BorderLayout.WEST);
        final JList projectsList = UIUtil.findComponentOfType(recentProjects, JList.class);
        if (projectsList != null) {
          projectsList
              .getModel()
              .addListDataListener(
                  new ListDataListener() {
                    @Override
                    public void intervalAdded(ListDataEvent e) {}

                    @Override
                    public void intervalRemoved(ListDataEvent e) {
                      removeIfNeeded();
                    }

                    private void removeIfNeeded() {
                      if (RecentProjectsManager.getInstance()
                              .getRecentProjectsActions(false, isUseProjectGroups())
                              .length
                          == 0) {
                        FlatWelcomeScreen.this.remove(recentProjects);
                        FlatWelcomeScreen.this.revalidate();
                        FlatWelcomeScreen.this.repaint();
                      }
                    }

                    @Override
                    public void contentsChanged(ListDataEvent e) {
                      removeIfNeeded();
                    }
                  });
          projectsList.addFocusListener(
              new FocusListener() {
                @Override
                public void focusGained(FocusEvent e) {
                  projectsList.repaint();
                }

                @Override
                public void focusLost(FocusEvent e) {
                  projectsList.repaint();
                }
              });
        }
      }
      add(createBody(), BorderLayout.CENTER);
    }
  protected JComponent createCenterPanel() {
    myList.setCellRenderer(new CvsListCellRenderer());

    myCenterPanelLayout.setHgap(6);

    myCenterPanel.add(createActionsPanel(), BorderLayout.NORTH);
    JComponent listPanel = createListPanel();

    myCenterPanel.add(listPanel, BorderLayout.CENTER);
    myCenterPanel.add(createCvsConfigurationPanel(), BorderLayout.EAST);
    myCenterPanel.add(new JSeparator(JSeparator.HORIZONTAL), BorderLayout.SOUTH);

    myList.setModel(myModel);

    addSelectionListener();

    int minWidth = myList.getFontMetrics(myList.getFont()).stringWidth(SAMPLE_CVSROOT) + 40;
    Dimension minSize = new Dimension(minWidth, myList.getMaximumSize().height);
    listPanel.setMinimumSize(minSize);
    listPanel.setPreferredSize(minSize);
    return myCenterPanel;
  }
  static List<Bookmark> getSelectedBookmarks(JList list) {
    List<Bookmark> answer = new ArrayList<Bookmark>();

    //noinspection deprecation
    for (Object value : list.getSelectedValues()) {
      if (value instanceof BookmarkItem) {
        answer.add(((BookmarkItem) value).getBookmark());
      } else {
        return Collections.emptyList();
      }
    }

    return answer;
  }
  public CvsConfigurationsListEditor(List<CvsRootConfiguration> configs, Project project) {
    super(true);
    myCvs2SettingsEditPanel = new Cvs2SettingsEditPanel(project);
    setTitle(CvsBundle.message("operation.name.edit.configurations"));
    myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    selectNone();
    fillModel(configs);

    myCvs2SettingsEditPanel.addCvsRootChangeListener(
        new CvsRootChangeListener() {
          public void onCvsRootChanged() {
            if (mySelection == null) return;
            myCvs2SettingsEditPanel.saveTo(mySelection, false);
            myList.repaint();
          }
        });

    setTitle(CvsBundle.message("dialog.title.cvs.roots"));

    if (!configs.isEmpty()) {
      myList.setSelectedIndex(0);
    }
    init();
  }
 private void addSelectionListener() {
   myList
       .getSelectionModel()
       .addListSelectionListener(
           new ListSelectionListener() {
             public void valueChanged(ListSelectionEvent e) {
               int selectedIndex = myList.getSelectedIndex();
               if (selectedIndex < 0 || selectedIndex >= myModel.getSize()) {
                 selectNone();
               } else {
                 CvsRootConfiguration newSelection =
                     (CvsRootConfiguration) myModel.getElementAt(selectedIndex);
                 if (newSelection == mySelection) return;
                 if (!select(newSelection)) {
                   myList.setSelectedValue(mySelection, true);
                 }
               }
             }
           });
 }
 static boolean notFiltered(JList list) {
   ListModel model1 = list.getModel();
   if (!(model1 instanceof FilteringListModel)) return true;
   final FilteringListModel model = (FilteringListModel) model1;
   return model.getOriginalModel().getSize() == model.getSize();
 }
 private void setReadOnly() {
   myIsReadOnly = true;
   myList.setEnabled(false);
   myCvs2SettingsEditPanel.setReadOnly();
 }
 public void selectConfiguration(CvsRootConfiguration selectedConfiguration) {
   myList.setSelectedValue(selectedConfiguration, true);
 }
 private void editSelectedConfiguration() {
   editConfiguration(mySelection);
   myList.repaint();
 }
 private void copySelectedConfiguration() {
   if (!saveSelectedConfiguration()) return;
   CvsRootConfiguration newConfig = mySelection.getMyCopy();
   myModel.addElement(newConfig);
   myList.setSelectedValue(newConfig, true);
 }
  @NotNull
  @Override
  public RelativePoint guessBestPopupLocation(@NotNull final JComponent component) {
    Point popupMenuPoint = null;
    final Rectangle visibleRect = component.getVisibleRect();
    if (component instanceof JList) { // JList
      JList list = (JList) component;
      int firstVisibleIndex = list.getFirstVisibleIndex();
      int lastVisibleIndex = list.getLastVisibleIndex();
      int[] selectedIndices = list.getSelectedIndices();
      for (int index : selectedIndices) {
        if (firstVisibleIndex <= index && index <= lastVisibleIndex) {
          Rectangle cellBounds = list.getCellBounds(index, index);
          popupMenuPoint =
              new Point(visibleRect.x + visibleRect.width / 4, cellBounds.y + cellBounds.height);
          break;
        }
      }
    } else if (component instanceof JTree) { // JTree
      JTree tree = (JTree) component;
      int[] selectionRows = tree.getSelectionRows();
      if (selectionRows != null) {
        Arrays.sort(selectionRows);
        for (int i = 0; i < selectionRows.length; i++) {
          int row = selectionRows[i];
          Rectangle rowBounds = tree.getRowBounds(row);
          if (visibleRect.contains(rowBounds)) {
            popupMenuPoint = new Point(rowBounds.x + 2, rowBounds.y + rowBounds.height - 1);
            break;
          }
        }
        if (popupMenuPoint == null) { // All selected rows are out of visible rect
          Point visibleCenter =
              new Point(
                  visibleRect.x + visibleRect.width / 2, visibleRect.y + visibleRect.height / 2);
          double minDistance = Double.POSITIVE_INFINITY;
          int bestRow = -1;
          Point rowCenter;
          double distance;
          for (int i = 0; i < selectionRows.length; i++) {
            int row = selectionRows[i];
            Rectangle rowBounds = tree.getRowBounds(row);
            rowCenter =
                new Point(rowBounds.x + rowBounds.width / 2, rowBounds.y + rowBounds.height / 2);
            distance = visibleCenter.distance(rowCenter);
            if (minDistance > distance) {
              minDistance = distance;
              bestRow = row;
            }
          }

          if (bestRow != -1) {
            Rectangle rowBounds = tree.getRowBounds(bestRow);
            tree.scrollRectToVisible(
                new Rectangle(
                    rowBounds.x,
                    rowBounds.y,
                    Math.min(visibleRect.width, rowBounds.width),
                    rowBounds.height));
            popupMenuPoint = new Point(rowBounds.x + 2, rowBounds.y + rowBounds.height - 1);
          }
        }
      }
    } else if (component instanceof JTable) {
      JTable table = (JTable) component;
      int column = table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
      int row =
          Math.max(
              table.getSelectionModel().getLeadSelectionIndex(),
              table.getSelectionModel().getAnchorSelectionIndex());
      Rectangle rect = table.getCellRect(row, column, false);
      if (!visibleRect.intersects(rect)) {
        table.scrollRectToVisible(rect);
      }
      popupMenuPoint = new Point(rect.x, rect.y + rect.height);
    } else if (component instanceof PopupOwner) {
      popupMenuPoint = ((PopupOwner) component).getBestPopupPosition();
    }
    if (popupMenuPoint == null) {
      popupMenuPoint =
          new Point(visibleRect.x + visibleRect.width / 2, visibleRect.y + visibleRect.height / 2);
    }

    return new RelativePoint(component, popupMenuPoint);
  }