javax.swing.JEditorPane#scrollRectToVisible ( )源码实例Demo

下面列出了javax.swing.JEditorPane#scrollRectToVisible ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: marathonv5   文件: JEditorPaneTagJavaElement.java
@Override
public Object _makeVisible() {
    JEditorPane editor = (JEditorPane) parent.getComponent();
    Iterator iterator = findTag((HTMLDocument) editor.getDocument());
    int startOffset = iterator.getStartOffset();
    int endOffset = iterator.getEndOffset();
    try {
        Rectangle bounds = editor.modelToView(startOffset + (endOffset - startOffset) / 2);
        if (bounds != null) {
            bounds.height = editor.getVisibleRect().height;
            editor.scrollRectToVisible(bounds);
        }
    } catch (BadLocationException e) {
        throw new InvalidElementStateException("Unable to get text for tag " + tag + " in document with index " + index, e);
    }
    return null;
}
 
源代码2 项目: netbeans   文件: OpenAction.java
private void performOpen(JEditorPane[] panes) {
    if (panes == null || panes.length == 0) {
        StatusDisplayer.getDefault().setStatusText(Bundle.ERR_ShellConsoleClosed());
        return;
    }
    JEditorPane p = panes[0];
    Rng[] fragments = theHandle.getFragments();
    
    int to = fragments[0].start;
    p.requestFocus();
    int pos = Math.min(p.getDocument().getLength() - 1, to);
    p.setCaretPosition(pos);
    try {
        Rectangle r = p.modelToView(pos);
        p.scrollRectToVisible(r);
    } catch (BadLocationException ex) {
    }
}
 
源代码3 项目: marathonv5   文件: JEditorPanePosJavaElement.java
@Override
public Object _makeVisible() {
    JEditorPane editor = (JEditorPane) parent.getComponent();
    try {
        Rectangle bounds = editor.modelToView(pos);
        if (bounds != null) {
            bounds.height = editor.getVisibleRect().height;
            editor.scrollRectToVisible(bounds);
        }
    } catch (BadLocationException e) {
        throw new InvalidElementStateException("Invalid position " + pos + "(" + e.getMessage() + ")", e);
    }
    return null;
}
 
源代码4 项目: triplea   文件: DownloadMapsWindow.java
private static JList<String> newGameSelectionList(
    final DownloadFileDescription selectedMap,
    final List<DownloadFileDescription> maps,
    final JEditorPane descriptionPanel,
    final DefaultListModel<String> model) {
  final JList<String> gamesList = new JList<>(model);
  final int selectedIndex = maps.indexOf(selectedMap);
  gamesList.setSelectedIndex(selectedIndex);

  final String text = maps.get(selectedIndex).toHtmlString();
  descriptionPanel.setText(text);
  descriptionPanel.scrollRectToVisible(new Rectangle(0, 0, 0, 0));
  return gamesList;
}
 
源代码5 项目: triplea   文件: DownloadMapsWindow.java
private static ListSelectionListener newDescriptionPanelUpdatingSelectionListener(
    final JEditorPane descriptionPanel,
    final JList<String> gamesList,
    final List<DownloadFileDescription> maps,
    final MapAction action,
    final JLabel mapSizeLabelToUpdate) {
  return e -> {
    if (!e.getValueIsAdjusting()) {
      final int index = gamesList.getSelectedIndex();

      final boolean somethingIsSelected = index >= 0;
      if (somethingIsSelected) {
        final String mapName = gamesList.getModel().getElementAt(index);

        // find the map description by map name and update the map download detail panel
        final Optional<DownloadFileDescription> map =
            maps.stream()
                .filter(mapDescription -> mapDescription.getMapName().equals(mapName))
                .findFirst();
        if (map.isPresent()) {
          final String text = map.get().toHtmlString();
          descriptionPanel.setText(text);
          descriptionPanel.scrollRectToVisible(new Rectangle(0, 0, 0, 0));
          updateMapUrlAndSizeLabel(map.get(), action, mapSizeLabelToUpdate);
        }
      }
    }
  };
}
 
源代码6 项目: netbeans   文件: CloneableEditorSupport.java
/** Forcibly create one editor component. Then set the caret
* to the given position.
* @param pos where to place the caret
* @param column where to place the caret
* @param reuse if true, the infrastructure tries to reuse other, already opened editor
 * for the purpose of opening this file/line. 
* @return always non-<code>null</code> editor
*/
private final Pane openAtImpl(final PositionRef pos, final int column, boolean reuse) {
    CloneableEditorSupport redirect = CloneableEditorSupportRedirector.findRedirect(this);
    if (redirect != null) {
        return redirect.openAtImpl(pos, column, reuse);
    }
    final Pane e = openPane(reuse);
    final Task t = prepareDocument();
    e.ensureVisible();
    class Selector implements TaskListener, Runnable {
        private boolean documentLocked = false;

        public void taskFinished(org.openide.util.Task t2) {
            javax.swing.SwingUtilities.invokeLater(this);
            t2.removeTaskListener(this);
        }

        public void run() {
            // #25435. Pane can be null.
            JEditorPane ePane = e.getEditorPane();

            if (ePane == null) {
                return;
            }

            StyledDocument doc = getDocument();

            if (doc == null) {
                return; // already closed or error loading
            }

            if (!documentLocked) {
                documentLocked = true;
                doc.render(this);
            } else {
                Caret caret = ePane.getCaret();

                if (caret == null) {
                    return;
                }

                int offset;

                // Pane's document may differ - see #204980
                Document paneDoc = ePane.getDocument();
                if (paneDoc instanceof StyledDocument && paneDoc != doc) {
                    if (ERR.isLoggable(Level.FINE)) {
                        ERR.fine("paneDoc=" + paneDoc + "\n !=\ndoc=" + doc); // NOI18N
                    }
                    doc = (StyledDocument) paneDoc;
                }

                javax.swing.text.Element el = NbDocument.findLineRootElement(doc);
                el = el.getElement(el.getElementIndex(pos.getOffset()));
                offset = el.getStartOffset() + Math.max(0, column);

                if (offset > el.getEndOffset()) {
                    offset = Math.max(el.getStartOffset(), el.getEndOffset() - 1);
                }

                caret.setDot(offset);

                try { // scroll to show reasonable part of the document
                    Rectangle r = ePane.modelToView(offset);
                    if (r != null) {
                        r.height *= 5;
                        ePane.scrollRectToVisible(r);
                    }
                } catch (BadLocationException ex) {
                    ERR.log(Level.WARNING, "Can't scroll to text: pos.getOffset=" + pos.getOffset() //NOI18N
                        + ", column=" + column + ", offset=" + offset //NOI18N
                        + ", doc.getLength=" + doc.getLength(), ex); //NOI18N
                }
            }
        }
    }
    t.addTaskListener(new Selector());

    return e;
}
 
源代码7 项目: netbeans   文件: FmtOptions.java
@Override
public void refreshPreview() {
    JEditorPane pane = (JEditorPane) getPreviewComponent();
    try {
        int rm = previewPrefs.getInt(rightMargin, provider.getDefaultAsInt(rightMargin));
        pane.putClientProperty("TextLimitLine", rm); //NOI18N
    }
    catch( NumberFormatException e ) {
        // Ignore it
    }

    Rectangle visibleRectangle = pane.getVisibleRect();
    pane.setText(previewText);
    pane.setIgnoreRepaint(true);

    final Document doc = pane.getDocument();
    if (doc instanceof BaseDocument) {
        final Reformat reformat = Reformat.get(doc);
        reformat.lock();
        try {
            ((BaseDocument) doc).runAtomic(new Runnable() {
                @Override
                public void run() {

                    try {
                        reformat.reformat(0, doc.getLength());
                    } catch (BadLocationException ble) {
                        LOGGER.log(Level.WARNING, null, ble);
                    }
                }
            });
        } finally {
            reformat.unlock();
        }
    } else {
        LOGGER.warning(String.format("Can't format %s; it's not BaseDocument.", doc)); //NOI18N
    }
    pane.setIgnoreRepaint(false);
    pane.scrollRectToVisible(visibleRectangle);
    pane.repaint(100);

}
 
源代码8 项目: netbeans   文件: FmtOptions.java
@Override
public void refreshPreview() {
    JEditorPane pane = (JEditorPane) getPreviewComponent();
    try {
        int rm = previewPrefs.getInt(RIGHT_MARGIN, getDefaultAsInt(RIGHT_MARGIN));
        pane.putClientProperty("TextLimitLine", rm); //NOI18N
    } catch (NumberFormatException e) {
        // Ignore it
    }

    Rectangle visibleRectangle = pane.getVisibleRect();
    pane.setText(previewText);
    pane.setIgnoreRepaint(true);

    final Document doc = pane.getDocument();
    if (doc instanceof BaseDocument) {
        final Reformat reformat = Reformat.get(doc);
        reformat.lock();
        try {
            ((BaseDocument) doc).runAtomic(new Runnable() {
                @Override
                public void run() {

                    try {
                        reformat.reformat(0, doc.getLength());
                    } catch (BadLocationException ble) {
                        LOGGER.log(Level.WARNING, null, ble);
                    }
                }
            });
        } finally {
            reformat.unlock();
        }
    } else {
        LOGGER.warning(String.format("Can't format %s; it's not BaseDocument.", doc)); //NOI18N
    }
    pane.setIgnoreRepaint(false);
    pane.scrollRectToVisible(visibleRectangle);
    pane.repaint(100);

}
 
源代码9 项目: netbeans   文件: FmtOptions.java
@Override
public void refreshPreview() {
    JEditorPane pane = (JEditorPane) getPreviewComponent();
    try {
        int rm = previewPrefs.getInt(rightMargin, getDefaultAsInt(rightMargin));
        pane.putClientProperty("TextLimitLine", rm); //NOI18N
    }
    catch( NumberFormatException e ) {
        // Ignore it
    }

    Rectangle visibleRectangle = pane.getVisibleRect();
    pane.setText(previewText);
    pane.setIgnoreRepaint(true);

    final Document doc = pane.getDocument();
    if (doc instanceof BaseDocument) {
        final Reformat reformat = Reformat.get(doc);
        reformat.lock();
        try {
            ((BaseDocument) doc).runAtomic(new Runnable() {
                @Override
                public void run() {

                    try {
                        reformat.reformat(0, doc.getLength());
                    } catch (BadLocationException ble) {
                        LOGGER.log(Level.WARNING, null, ble);
                    }
                }
            });
        } finally {
            reformat.unlock();
        }
    } else {
        LOGGER.warning(String.format("Can't format %s; it's not BaseDocument.", doc)); //NOI18N
    }
    pane.setIgnoreRepaint(false);
    pane.scrollRectToVisible(visibleRectangle);
    pane.repaint(100);

}