Index: src/org/openide/text/PositionRef.java =================================================================== RCS file: /cvs/openide/src/org/openide/text/PositionRef.java,v retrieving revision 1.49 diff -c -r1.49 PositionRef.java *** src/org/openide/text/PositionRef.java 2 Apr 2003 09:46:48 -0000 1.49 --- src/org/openide/text/PositionRef.java 15 May 2003 08:35:39 -0000 *************** *** 418,431 **** /** Converts the kind to representation in memory */ public PositionKind toMemory (boolean insertAfter) { ! // try to find the right position ! Position p; ! try { ! p = NbDocument.createPosition (doc, getOffset (), insertAfter ? Position.Bias.Forward : Position.Bias.Backward); ! } catch (BadLocationException e) { ! p = doc.getEndPosition (); ! } ! return new PositionKind (p); } /** Converts the kind to representation out from memory */ --- 418,425 ---- /** Converts the kind to representation in memory */ public PositionKind toMemory (boolean insertAfter) { ! return (PositionKind)new DocumentRenderer(this, ! DocumentRenderer.KIND_TO_MEMORY, insertAfter).renderToObject(); } /** Converts the kind to representation out from memory */ *************** *** 453,471 **** /** Get the line number */ public int getLine() { ! return NbDocument.findLineNumber(doc, getOffset()); } /** Get the column number */ public int getColumn() { ! return NbDocument.findLineColumn(doc, getOffset()); } /** Writes the kind to stream */ public void write (DataOutput os) throws IOException { ! int offset = getOffset(); ! int line = getLine(); ! int column = getColumn(); if(offset < 0 || line < 0 || column < 0) { throw new IOException( --- 447,470 ---- /** Get the line number */ public int getLine() { ! return new DocumentRenderer(this, ! DocumentRenderer.POSITION_KIND_GET_LINE).renderToInt(); } /** Get the column number */ public int getColumn() { ! return new DocumentRenderer(this, ! DocumentRenderer.POSITION_KIND_GET_COLUMN).renderToInt(); } /** Writes the kind to stream */ public void write (DataOutput os) throws IOException { ! DocumentRenderer renderer = new DocumentRenderer(this, ! DocumentRenderer.POSITION_KIND_WRITE); ! ! int offset = renderer.renderToIntIOE(); ! int line = renderer.getLine(); ! int column = renderer.getColumn(); if(offset < 0 || line < 0 || column < 0) { throw new IOException( *************** *** 505,513 **** /** Constructs the out kind from the position kind. */ public OutKind (PositionKind kind) { ! int offset = kind.getOffset(); ! int line = kind.getLine(); ! int column = kind.getColumn(); if(offset < 0 || line < 0 || column < 0) { throw new IndexOutOfBoundsException( --- 504,516 ---- /** Constructs the out kind from the position kind. */ public OutKind (PositionKind kind) { ! DocumentRenderer renderer = new DocumentRenderer(this, ! DocumentRenderer.OUT_KIND_CONSTRUCTOR); ! ! int offset = renderer.renderToInt(); ! int line = renderer.getLine(); ! int column = renderer.getColumn(); ! if(offset < 0 || line < 0 || column < 0) { throw new IndexOutOfBoundsException( *************** *** 590,601 **** /** Get the line number */ public int getLine() throws IOException { ! return NbDocument.findLineNumber(getCloneableEditorSupport().openDocument(), offset); } /** Get the column number */ public int getColumn() throws IOException { ! return NbDocument.findLineColumn (getCloneableEditorSupport().openDocument(), offset); } /** Writes the kind to stream */ --- 593,606 ---- /** Get the line number */ public int getLine() throws IOException { ! return new DocumentRenderer(this, DocumentRenderer.OFFSET_KIND_GET_LINE, ! offset).renderToIntIOE(); } /** Get the column number */ public int getColumn() throws IOException { ! return new DocumentRenderer(this, DocumentRenderer.OFFSET_KIND_GET_COLUMN, ! offset).renderToIntIOE(); } /** Writes the kind to stream */ *************** *** 638,653 **** /** Offset */ public int getOffset () { ! try { ! StyledDocument doc = getCloneableEditorSupport().getDocument(); ! if (doc == null) { ! doc = getCloneableEditorSupport().openDocument(); ! } ! return NbDocument.findLineOffset (doc, line) + column; ! } catch (IOException e) { ! // what to do? hopefully unlikelly ! return 0; ! } } /** Get the line number */ --- 643,650 ---- /** Offset */ public int getOffset () { ! return new DocumentRenderer(this, DocumentRenderer.LINE_KIND_GET_OFFSET, ! line, column).renderToInt(); } /** Get the line number */ *************** *** 677,690 **** /** Converts the kind to representation in memory */ public PositionKind toMemory (boolean insertAfter) { ! // try to find the right position ! Position p; try { ! p = NbDocument.createPosition (doc, NbDocument.findLineOffset (doc, line) + column, insertAfter ? Position.Bias.Forward : Position.Bias.Backward); ! } catch (BadLocationException e) { ! p = doc.getEndPosition (); } - return new PositionKind (p); } } --- 674,862 ---- /** Converts the kind to representation in memory */ public PositionKind toMemory (boolean insertAfter) { ! Position p = (Position)new DocumentRenderer(this, ! DocumentRenderer.LINE_KIND_TO_MEMORY, ! line, column, insertAfter).renderToObject(); ! ! return new PositionKind (p); ! } ! ! } ! ! /** ! * Helper class ensuring that critical parts will run under document's read lock ! * by using {@link javax.swing.text.Document#render(Runnable)}. ! */ ! private final class DocumentRenderer implements Runnable { ! ! private static final int KIND_TO_MEMORY = 0; ! ! private static final int POSITION_KIND_GET_LINE = KIND_TO_MEMORY + 1; ! ! private static final int POSITION_KIND_GET_COLUMN = POSITION_KIND_GET_LINE + 1; ! ! private static final int POSITION_KIND_WRITE = POSITION_KIND_GET_COLUMN + 1; ! ! private static final int OUT_KIND_CONSTRUCTOR = POSITION_KIND_WRITE + 1; ! ! private static final int OFFSET_KIND_GET_LINE = OUT_KIND_CONSTRUCTOR + 1; ! ! private static final int OFFSET_KIND_GET_COLUMN = OFFSET_KIND_GET_LINE + 1; ! ! private static final int LINE_KIND_GET_OFFSET = OFFSET_KIND_GET_COLUMN + 1; ! ! private static final int LINE_KIND_TO_MEMORY = LINE_KIND_GET_OFFSET + 1; ! ! private final Kind kind; ! ! private final int opCode; ! ! private boolean insertAfter; ! ! private int argInt; ! ! private Object retObject; ! ! private int retInt; ! ! private int line; ! ! private int column; ! ! private IOException ioException; ! ! DocumentRenderer(Kind kind, int opCode) { ! this.kind = kind; ! this.opCode = opCode; ! } ! ! DocumentRenderer(Kind kind, int opCode, boolean insertAfter) { ! this(kind, opCode); ! this.insertAfter = insertAfter; ! } ! ! DocumentRenderer(Kind kind, int opCode, int argInt) { ! this(kind, opCode); ! this.argInt = argInt; ! } ! ! DocumentRenderer(Kind kind, int opCode, int line, int column) { ! this(kind, opCode); ! this.line = line; ! this.column = column; ! } ! ! DocumentRenderer(Kind kind, int opCode, int line, int column, boolean insertAfter) { ! this(kind, opCode, line, column); ! this.insertAfter = insertAfter; ! } ! ! void render() { ! doc.render(this); ! } ! ! Object renderToObjectIOE() throws IOException { ! if (ioException != null) { ! throw ioException; ! } ! ! return renderToObject(); ! } ! ! Object renderToObject() { ! render(); ! return retObject; ! } ! ! int renderToIntIOE() throws IOException { ! if (ioException != null) { ! throw ioException; ! } ! ! return renderToInt(); ! } ! ! int renderToInt() { ! render(); ! return retInt; ! } ! ! int getLine() { ! return line; ! } ! ! int getColumn() { ! return column; ! } ! ! public void run() { try { ! switch (opCode) { ! case KIND_TO_MEMORY: ! // try to find the right position ! Position p; ! try { ! p = NbDocument.createPosition (doc, kind.getOffset (), ! insertAfter ? Position.Bias.Forward : Position.Bias.Backward); ! } catch (BadLocationException e) { ! p = doc.getEndPosition (); ! } ! retObject = (PositionKind)new PositionKind (p); ! break; ! ! case POSITION_KIND_GET_LINE: ! retInt = NbDocument.findLineNumber(doc, kind.getOffset()); ! break; ! ! case POSITION_KIND_GET_COLUMN: ! retInt = NbDocument.findLineColumn(doc, kind.getOffset()); ! break; ! ! case POSITION_KIND_WRITE: ! case OUT_KIND_CONSTRUCTOR: ! retInt = kind.getOffset(); ! line = kind.getLine(); ! column = kind.getColumn(); ! break; ! ! case OFFSET_KIND_GET_LINE: ! retInt = NbDocument.findLineNumber(getCloneableEditorSupport().openDocument(), argInt); ! break; ! ! case OFFSET_KIND_GET_COLUMN: ! retInt = NbDocument.findLineColumn (getCloneableEditorSupport().openDocument(), argInt); ! break; ! ! case LINE_KIND_GET_OFFSET: ! try { ! StyledDocument doc = getCloneableEditorSupport().getDocument(); ! if (doc == null) { ! doc = getCloneableEditorSupport().openDocument(); ! } ! argInt = NbDocument.findLineOffset (doc, line) + column; ! } catch (IOException e) { ! // what to do? hopefully unlikelly ! argInt = 0; ! } ! break; ! ! case LINE_KIND_TO_MEMORY: ! // try to find the right position ! try { ! retObject = NbDocument.createPosition (doc, ! NbDocument.findLineOffset (doc, line) + column, ! insertAfter ? Position.Bias.Forward : Position.Bias.Backward); ! } catch (BadLocationException e) { ! retObject = doc.getEndPosition (); ! } ! break; ! ! default: ! throw new IllegalStateException(); // Unknown opcode ! } ! } catch (IOException e) { ! ioException = e; } } }