Java Code Examples for com.intellij.ui.components.JBScrollPane#getViewport()

The following examples show how to use com.intellij.ui.components.JBScrollPane#getViewport() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnalyzeSizeToolWindowTest.java    From size-analyzer with Apache License 2.0 6 votes vote down vote up
@Test
public void categoryNodesHaveCorrectExtraInformation() {
  JPanel toolPanel = toolWindow.getContent();
  Component[] components = toolPanel.getComponents();
  OnePixelSplitter splitter = (OnePixelSplitter) components[0];
  JBScrollPane leftPane = (JBScrollPane) splitter.getFirstComponent();
  JViewport leftView = leftPane.getViewport();
  Tree suggestionTree = (Tree) leftView.getView();

  TreePath webPPath = getTreePathWithString(suggestionTree, webPCategoryData.toString());
  TreeCellRenderer treeCellRenderer = suggestionTree.getCellRenderer();
  Component renderedNode =
      treeCellRenderer.getTreeCellRendererComponent(
          suggestionTree,
          webPPath.getLastPathComponent(),
          false,
          false,
          false,
          suggestionTree.getRowForPath(webPPath),
          false);
  assertThat(renderedNode.toString()).contains("2 recommendations");
  assertThat(renderedNode.toString()).contains("29.30 KB");
}
 
Example 2
Source File: AnalyzeSizeToolWindowTest.java    From size-analyzer with Apache License 2.0 6 votes vote down vote up
@Test
public void multipleSuggestionsCreateIssueTypeNode() {
  JPanel toolPanel = toolWindow.getContent();
  Component[] components = toolPanel.getComponents();
  OnePixelSplitter splitter = (OnePixelSplitter) components[0];
  JBScrollPane leftPane = (JBScrollPane) splitter.getFirstComponent();
  JViewport leftView = leftPane.getViewport();
  Tree suggestionTree = (Tree) leftView.getView();

  TreePath webPIssuePath =
      getTreePathWithString(
          suggestionTree, SuggestionDataFactory.issueTypeNodeNames.get(IssueType.WEBP));
  DefaultMutableTreeNode webPIssueNode =
      (DefaultMutableTreeNode) webPIssuePath.getLastPathComponent();
  assertThat(webPIssueNode.getChildCount()).isEqualTo(2);
}
 
Example 3
Source File: AnalyzeSizeToolWindowTest.java    From size-analyzer with Apache License 2.0 6 votes vote down vote up
@Test
public void autofixIsNotShownWhenNull() {
  JPanel toolPanel = toolWindow.getContent();
  Component[] components = toolPanel.getComponents();
  OnePixelSplitter splitter = (OnePixelSplitter) components[0];
  JBScrollPane leftPane = (JBScrollPane) splitter.getFirstComponent();
  JViewport leftView = leftPane.getViewport();
  Tree suggestionTree = (Tree) leftView.getView();
  JPanel rightPane = (JPanel) splitter.getSecondComponent();

  TreePath streamingPath =
      getTreePathWithString(suggestionTree, streamingSuggestionData.toString());
  suggestionTree.addSelectionPath(streamingPath);

  for (Component component : rightPane.getComponents()) {
    if (component instanceof JButton) {
      assertThat(component.isVisible()).isFalse();
    }
  }
}
 
Example 4
Source File: ViewManager.java    From leetcode-editor with Apache License 2.0 6 votes vote down vote up
public static void pick(JTree tree, JBScrollPane scrollPane) {

        DefaultTreeModel treeMode = (DefaultTreeModel) tree.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeMode.getRoot();
        if (root.isLeaf()) {
            return;
        }
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(0);
        if (node.isLeaf()) {
            return;
        }
        int i = (int) (Math.random() * node.getChildCount());
        DefaultMutableTreeNode select = (DefaultMutableTreeNode) node.getChildAt(i);

        TreePath toShowPath = new TreePath(select.getPath());
        tree.setSelectionPath(toShowPath);
        Rectangle bounds = tree.getPathBounds(toShowPath);
        Point point = new Point(0, (int) bounds.getY());
        JViewport viewport = scrollPane.getViewport();
        viewport.setViewPosition(point);
        return;
    }
 
Example 5
Source File: AnalyzeSizeToolWindowTest.java    From size-analyzer with Apache License 2.0 5 votes vote down vote up
@Test
public void selectingCategoryNodeChangesCategoryPanel() {
  JPanel toolPanel = toolWindow.getContent();
  Component[] components = toolPanel.getComponents();
  OnePixelSplitter splitter = (OnePixelSplitter) components[0];
  JBScrollPane leftPane = (JBScrollPane) splitter.getFirstComponent();
  JViewport leftView = leftPane.getViewport();
  Tree suggestionTree = (Tree) leftView.getView();

  assertThat(suggestionTree.getSelectionCount()).isEqualTo(0);

  TreePath webPPath = getTreePathWithString(suggestionTree, webPCategoryData.toString());
  suggestionTree.addSelectionPath(webPPath);
  JPanel rightPane = (JPanel) splitter.getSecondComponent();

  assertThat(suggestionTree.getSelectionCount()).isEqualTo(1);
  for (Component component : rightPane.getComponents()) {
    if (component instanceof SimpleColoredComponent) {
      assertThat(component.toString()).contains(webPCategoryData.toString());
      assertThat(component.toString()).contains("Estimated savings: 29.30 KB");
    } else if (component instanceof JPanel) {
      Component[] suggestionPanelComponents = ((JPanel) component).getComponents();
      assertThat(suggestionPanelComponents).hasLength(1);
      for (Component linkLabel : suggestionPanelComponents) {
        assertThat(((LinkLabel<?>) linkLabel).getText()).isEqualTo(SuggestionDataFactory.issueTypeNodeNames.get(IssueType.WEBP));
      }
    }
  }
}
 
Example 6
Source File: ViewManager.java    From leetcode-editor with Apache License 2.0 5 votes vote down vote up
public static void position(JTree tree, JBScrollPane scrollPane, Question question) {

        DefaultTreeModel treeMode = (DefaultTreeModel) tree.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeMode.getRoot();
        if (root.isLeaf()) {
            return;
        }
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(0);
        if (node.isLeaf()) {
            return;
        }

        for (int i = 0, j = node.getChildCount(); i < j; i++) {
            DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) node.getChildAt(i);
            Question nodeData = (Question) childNode.getUserObject();
            if (nodeData.getQuestionId().equals(question.getQuestionId())) {
                TreePath toShowPath = new TreePath(childNode.getPath());
                tree.setSelectionPath(toShowPath);
                Rectangle bounds = tree.getPathBounds(toShowPath);
                Point point = new Point(0, (int) bounds.getY());
                JViewport viewport = scrollPane.getViewport();
                viewport.setViewPosition(point);
                return;
            }

        }
    }
 
Example 7
Source File: ArrangementRuleAliasesPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
public ArrangementRuleAliasesPanel(@Nonnull ArrangementStandardSettingsManager settingsManager,
                                   @Nonnull ArrangementColorsProvider colorsProvider) {
  super(new GridBagLayout());
  setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
  JBScrollPane scrollPane = new JBScrollPane();
  final JViewport viewport = scrollPane.getViewport();
  ArrangementMatchingRulesControl.RepresentationCallback callback = new ArrangementMatchingRulesControl.RepresentationCallback() {
    @Override
    public void ensureVisible(@Nonnull Rectangle r) {
      Rectangle visibleRect = viewport.getViewRect();
      if (r.y <= visibleRect.y) {
        return;
      }

      int excessiveHeight = r.y + r.height - (visibleRect.y + visibleRect.height);
      if (excessiveHeight <= 0) {
        return;
      }

      int verticalShift = Math.min(r.y - visibleRect.y, excessiveHeight);
      if (verticalShift > 0) {
        viewport.setViewPosition(new Point(visibleRect.x, visibleRect.y + verticalShift));
      }
    }
  };
  myControl = new ArrangementRuleAliasControl(settingsManager, colorsProvider, callback);
  scrollPane.setViewportView(myControl);
  CustomizationUtil.installPopupHandler(
          myControl, ArrangementConstants.ALIAS_RULE_CONTEXT_MENU, ArrangementConstants.ALIAS_RULE_CONTROL_PLACE
  );

  TitleWithToolbar top = new TitleWithToolbar(
          ApplicationBundle.message("arrangement.settings.section.rule.sequence"),
          ArrangementConstants.ALIAS_RULE_CONTROL_TOOLBAR,
          ArrangementConstants.ALIAS_RULE_CONTROL_TOOLBAR_PLACE,
          myControl
  );
  add(top, new GridBag().coverLine().fillCellHorizontally().weightx(1));
  add(scrollPane, new GridBag().fillCell().weightx(1).weighty(1).insets(0, ArrangementConstants.HORIZONTAL_PADDING, 0, 0));
}
 
Example 8
Source File: ArrangementMatchingRulesPanel.java    From consulo with Apache License 2.0 4 votes vote down vote up
public ArrangementMatchingRulesPanel(@Nonnull Language language,
                                     @Nonnull ArrangementStandardSettingsManager settingsManager,
                                     @Nonnull ArrangementColorsProvider colorsProvider)
{
  super(new GridBagLayout());

  JBScrollPane scrollPane = new JBScrollPane();
  final JViewport viewport = scrollPane.getViewport();
  ArrangementSectionRulesControl.RepresentationCallback callback = new ArrangementSectionRulesControl.RepresentationCallback() {
    @Override
    public void ensureVisible(@Nonnull Rectangle r) {
      Rectangle visibleRect = viewport.getViewRect();
      if (r.y <= visibleRect.y) {
        return;
      }

      int excessiveHeight = r.y + r.height - (visibleRect.y + visibleRect.height);
      if (excessiveHeight <= 0) {
        return;
      }

      int verticalShift = Math.min(r.y - visibleRect.y, excessiveHeight);
      if (verticalShift > 0) {
        viewport.setViewPosition(new Point(visibleRect.x, visibleRect.y + verticalShift));
      }
    }
  };
  myControl = createRulesControl(language, settingsManager, colorsProvider, callback);
  scrollPane.setViewportView(myControl);
  CustomizationUtil.installPopupHandler(
          myControl, ArrangementConstants.ACTION_GROUP_MATCHING_RULES_CONTEXT_MENU, ArrangementConstants.MATCHING_RULES_CONTROL_PLACE
  );

  TitleWithToolbar top = new TitleWithToolbar(
          ApplicationBundle.message("arrangement.settings.section.match"),
          ArrangementConstants.ACTION_GROUP_MATCHING_RULES_CONTROL_TOOLBAR,
          ArrangementConstants.MATCHING_RULES_CONTROL_TOOLBAR_PLACE,
          myControl
  );
  add(top, new GridBag().coverLine().fillCellHorizontally().weightx(1));
  add(scrollPane, new GridBag().fillCell().weightx(1).weighty(1).insets(0, ArrangementConstants.HORIZONTAL_PADDING, 0, 0));
}