下面列出了javax.swing.JEditorPane#modelToView ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
public void testSimpleUndoRedo() throws Exception {
loggingOn();
JEditorPane pane = ViewUpdatesTesting.createPane();
Document doc = pane.getDocument();
UndoManager undoManager = ViewUpdatesTesting.getUndoManager(doc);
doc.insertString(0, "abc\ndef\nghi\n", null);
ViewUpdatesTesting.setViewBounds(pane, 4, 8);
pane.modelToView(0);
doc.insertString(4, "o", null);
doc.remove(3, 3);
doc.insertString(4, "ab", null);
doc.remove(7, 2);
pane.modelToView(0);
undoManager.undo(); // insert(7,2)
undoManager.undo(); // remove(4,2)
undoManager.undo(); // insert(3,3)
undoManager.undo();
undoManager.redo();
undoManager.redo();
undoManager.redo();
undoManager.redo();
}
public void testLongLineUndo() throws Exception {
loggingOn();
JEditorPane pane = ViewUpdatesTesting.createPane();
Document doc = pane.getDocument();
UndoManager undoManager = ViewUpdatesTesting.getUndoManager(doc);
int lineLen = 4000;
StringBuilder sb = new StringBuilder(lineLen + 10);
for (int i = 0; i < lineLen; i++) {
sb.append('a');
}
sb.append('\n');
doc.insertString(0, sb.toString(), null);
pane.modelToView(0);
doc.remove(0, lineLen);
pane.modelToView(0);
undoManager.undo();
undoManager.redo();
}
@Override
public void _moveto() {
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);
getDriver().getDevices().moveto(parent.getComponent(), bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
} catch (BadLocationException e) {
throw new InvalidElementStateException("Unable to get text for tag " + tag + " in document with index " + index, e);
}
}
@Override
public Point _getMidpoint() {
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);
return new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
} catch (BadLocationException e) {
throw new InvalidElementStateException("Unable to get text for tag " + tag + " in document with index " + index + "("
+ "StartOffset: " + startOffset + " EndOffset: " + endOffset + ")", e);
}
}
public void testEmptyCustomBounds() throws Exception {
loggingOn();
JEditorPane pane = ViewUpdatesTesting.createPane();
Document doc = pane.getDocument();
pane.modelToView(0);
doc.insertString(0, "hello\nworld\ngood\nmorning", null);
Position startPos = doc.createPosition(3);
pane.putClientProperty(DocumentView.START_POSITION_PROPERTY, startPos);
pane.putClientProperty(DocumentView.END_POSITION_PROPERTY, startPos);
pane.modelToView(0); // Force rebuild of VH
Position endPos = doc.createPosition(2);
pane.putClientProperty(DocumentView.END_POSITION_PROPERTY, endPos);
pane.modelToView(0); // Force rebuild of VH
}
@Override
public Point _getMidpoint() {
JEditorPane editor = (JEditorPane) parent.getComponent();
try {
Rectangle bounds = editor.modelToView(pos);
return new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
} catch (BadLocationException e) {
throw new InvalidElementStateException("Invalid position " + pos + "(" + e.getMessage() + ")", e);
}
}
@Override
public void _moveto() {
JEditorPane editor = (JEditorPane) parent.getComponent();
try {
Rectangle bounds = editor.modelToView(pos);
getDriver().getDevices().moveto(parent.getComponent(), bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
} catch (BadLocationException e) {
throw new InvalidElementStateException("Invalid position " + pos + "(" + e.getMessage() + ")", e);
}
}
public void testLongLineInsert() throws Exception {
loggingOn();
ViewUpdatesTesting.setTestValues(ViewUpdatesTesting.NO_OP_TEST_VALUE);
JEditorPane pane = ViewUpdatesTesting.createPane();
Document doc = pane.getDocument();
DocumentView docView = DocumentView.get(pane);
pane.modelToView(0); // Cause initialization
int offset;
String text;
offset = 0;
int lineLen = 4000;
StringBuilder sb = new StringBuilder(lineLen + 10);
for (int i = 0; i < lineLen; i++) {
sb.append('a');
}
sb.append('\n');
text = sb.toString();
ViewUpdatesTesting.setTestValues(
/*rebuildCause*/ ViewBuilder.RebuildCause.MOD_UPDATE,
/*createLocalViews*/ false,
/*startCreationOffset*/ 0,
/*matchOffset*/ offset + text.length() + 1, // createLocalViews == false
/*endCreationOffset*/ offset + text.length() + 1,
/*bmReuseOffset*/ 0,
/*bmReusePView*/ docView.getParagraphView(0),
/*bmReuseLocalIndex*/ 0,
/*amReuseOffset*/ Integer.MAX_VALUE, // createLocalViews == false
/*amReusePIndex*/ 1,
/*amReusePView*/ null,
/*amReuseLocalIndex*/ 0
);
doc.insertString(offset, text, null);
ViewUpdatesTesting.checkViews(docView, 0, -1,
4001, P_VIEW /* e:4001 */,
1, P_VIEW /* e:4002 */
);
ViewUpdatesTesting.setTestValues();
}
protected void clickForTextPopup(EditorOperator eo, String menu) {
JEditorPaneOperator txt = eo.txtEditorPane();
JEditorPane epane = (JEditorPane) txt.getSource();
try {
Rectangle rct = epane.modelToView(epane.getCaretPosition());
txt.clickForPopup(rct.x, rct.y);
JPopupMenuOperator popup = new JPopupMenuOperator();
popup.pushMenu(menu);
} catch (BadLocationException ex) {
System.out.println("=== Bad location");
}
}
protected void clickForTextPopup(EditorOperator eo, String menu) {
JEditorPaneOperator txt = eo.txtEditorPane();
JEditorPane epane = (JEditorPane) txt.getSource();
try {
Rectangle rct = epane.modelToView(epane.getCaretPosition());
txt.clickForPopup(rct.x, rct.y);
JPopupMenuOperator popup = new JPopupMenuOperator();
popup.pushMenu(menu);
} catch (BadLocationException ex) {
System.out.println("=== Bad location");
}
}
protected void clickForTextPopup(EditorOperator eo, String menu) {
JEditorPaneOperator txt = eo.txtEditorPane();
JEditorPane epane = (JEditorPane) txt.getSource();
try {
Rectangle rct = epane.modelToView(epane.getCaretPosition());
txt.clickForPopup(rct.x, rct.y);
JPopupMenuOperator popup = new JPopupMenuOperator();
popup.pushMenu(menu);
} catch (BadLocationException ex) {
System.out.println("=== Bad location");
}
}
protected void clickForTextPopup(EditorOperator eo, String menu) {
JEditorPaneOperator txt = eo.txtEditorPane();
JEditorPane epane = (JEditorPane) txt.getSource();
try {
Rectangle rct = epane.modelToView(epane.getCaretPosition());
txt.clickForPopup(rct.x, rct.y);
JPopupMenuOperator popup = new JPopupMenuOperator();
popup.pushMenu(menu);
} catch (BadLocationException ex) {
System.out.println("=== Bad location");
}
}
@Override
protected void run(Context context) throws Exception {
List<JEditorPane> paneList = getPaneList(context);
Random random = context.container().random();
StringBuilder log = context.logOpBuilder();
if (CREATE_PANE == name()) { // Just use ==
if (log != null) {
log.append("CREATE_ROOT_VIEW");
}
boolean createBounded = !paneList.isEmpty();
JEditorPane pane = ViewUpdatesTesting.createPane(DocumentTesting.getDocument(context));
paneList.add(pane);
if (createBounded) {
Document doc = pane.getDocument();
int startOffset = random.nextInt(doc.getLength());
int endOffset = startOffset + random.nextInt(doc.getLength() - startOffset) + 1;
ViewUpdatesTesting.setViewBounds(pane, startOffset, endOffset);
if (log != null) {
log.append("(").append(startOffset).append(",").append(endOffset).append(")");
}
}
if (log != null) {
log.append("\n");
context.logOp(log);
}
pane.modelToView(0);
} else if (RELEASE_PANE == name()) { // Just use ==
if (!paneList.isEmpty()) {
int index = random.nextInt(paneList.size());
paneList.remove(index);
if (log != null) {
log.append("DESTROY_ROOT_VIEW[" + index + "]\n");
}
}
}
}
protected void clickForTextPopup(EditorOperator eo, String menu) {
JEditorPaneOperator txt = eo.txtEditorPane();
JEditorPane epane = (JEditorPane) txt.getSource();
try {
Rectangle rct = epane.modelToView(epane.getCaretPosition());
txt.clickForPopup(rct.x, rct.y);
JPopupMenuOperator popup = new JPopupMenuOperator();
popup.pushMenu(menu);
} catch (BadLocationException ex) {
System.out.println("=== Bad location");
}
}
protected void ClickForTextPopup(EditorOperator eo, String menu) {
JEditorPaneOperator txt = eo.txtEditorPane();
JEditorPane epane = (JEditorPane) txt.getSource();
try {
Rectangle rct = epane.modelToView(epane.getCaretPosition());
txt.clickForPopup(rct.x, rct.y);
JPopupMenuOperator popup = new JPopupMenuOperator();
popup.pushMenu(menu);
} catch (BadLocationException ex) {
System.out.println("=== Bad location");
}
}
public void testRemoveNewline() throws Exception {
loggingOn();
JEditorPane pane = ViewUpdatesTesting.createPane();
Document doc = pane.getDocument();
UndoManager undoManager = ViewUpdatesTesting.getUndoManager(doc);
doc.insertString(0, "a\nb", null);
doc.remove(1, 1);
undoManager.undo();
pane.modelToView(0);
}
/** 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;
}
private void DragSomething(
String sFile,
String sLocation,
String sComponent
)
{
// Locate coords of HTML code
EditorOperator eoPHP = new EditorOperator( sFile );
eoPHP.setCaretPosition( sLocation, false );
int iX = 0, iY = 0;
JEditorPaneOperator txt = eoPHP.txtEditorPane( );
JEditorPane epane = ( JEditorPane )txt.getSource( );
try
{
Rectangle rct = epane.modelToView( epane.getCaretPosition( ) );
iX = rct.x;
iY = rct.y;
}
catch( BadLocationException ex )
{
fail( "Unable to detect destination location." );
}
//TopComponentOperator top = new TopComponentOperator( "EmptyPHPWebPage.php" );
TopComponentOperator pal = new TopComponentOperator( "Palette" );
JListOperator list = new JListOperator( pal, 0 );
ListModel lmd = list.getModel( );
int iIndex = list.findItemIndex( sComponent );
list.selectItem( iIndex );
Point pt = list.getClickPoint( iIndex );
MouseRobotDriver m_mouseDriver = new MouseRobotDriver(new Timeout("", 500));
m_mouseDriver.moveMouse( list, pt.x, pt.y );
m_mouseDriver.pressMouse( InputEvent.BUTTON1_MASK, 0 );
m_mouseDriver.enterMouse( txt );
m_mouseDriver.dragMouse( txt, iX, iY, InputEvent.BUTTON1_MASK, 0 );
m_mouseDriver.releaseMouse( InputEvent.BUTTON1_MASK, 0 );
return;
}
private void checkModelToViewCorrectness(JEditorPane jep) throws Exception {
Document doc = jep.getDocument();
assertTrue("Expecting BaseTextUI", jep.getUI() instanceof BaseTextUI);
BaseTextUI btui = (BaseTextUI) jep.getUI();
Insets margin = btui.getEditorUI().getTextMargin();
int charWidth = btui.getEditorUI().defaultSpaceWidth;
int charHeight = btui.getEditorUI().getLineHeight();
// System.out.println("### charWidth = " + charWidth + ", charHeight = " + charHeight
// + ", docLen = " + doc.getLength()
// + ", jep.width = " + jep.getWidth()
// + ", jep.height = " + jep.getHeight());
for(int offset = 0; offset <= doc.getLength(); offset++) {
// model-to-view translation
Rectangle r = jep.modelToView(offset);
assertNotNull("No m2v translation: offset = " + offset + ", docLen = " + doc.getLength(), r);
View rootView = Utilities.getRootView(jep, DrawEngineDocView.class);
int line = rootView.getViewIndex(offset, Position.Bias.Forward);
int col = offset - rootView.getView(line).getStartOffset();
// XXX: this would be necessary for handling tabs, but it uses DrawEngineLineView
// and therefore is not independent from the tested code, the inverse transformation
// will be needed in checkViewToModel
// int col = Utilities.getVisualColumn((BaseDocument)doc, offset);
// int nextCol = offset >= rootView.getView(line).getEndOffset() - 1 ? col + 1 : Utilities.getVisualColumn((BaseDocument)doc, offset + 1);
// System.out.println("### offset = " + offset + ", col = " + col + ", nextCol = " + nextCol + ", docLen = " + doc.getLength());
Rectangle r2 = new Rectangle(
margin.left + col * charWidth,
margin.top + line * charHeight,
// XXX: see above comment about the tabs handling
// (nextCol - col) * charWidth,
charWidth,
charHeight
);
assertEquals("Incorrect m2v translation: offset = " + offset + ", docLen = " + doc.getLength(), r2, r);
}
}