plainview.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 648 行 · 第 1/2 页
JAVA
648 行
{ Element child = el.getElement(i); int start = child.getStartOffset(); int end = child.getEndOffset(); try { el.getDocument().getText(start, end - start, seg); } catch (BadLocationException ex) { AssertionError ae = new AssertionError("Unexpected bad location"); ae.initCause(ex); throw ae; } if (seg == null || seg.array == null || seg.count == 0) continue; int width = metrics.charsWidth(seg.array, seg.offset, seg.count); if (width > span) { longestLine = child; span = width; } } maxLineLength = span; return maxLineLength; } public float getPreferredSpan(int axis) { if (axis != X_AXIS && axis != Y_AXIS) throw new IllegalArgumentException(); // make sure we have the metrics updateMetrics(); Element el = getElement(); float span; switch (axis) { case X_AXIS: span = determineMaxLineLength(); break; case Y_AXIS: default: span = metrics.getHeight() * el.getElementCount(); break; } return span; } /** * Maps coordinates from the <code>View</code>'s space into a position * in the document model. * * @param x the x coordinate in the view space * @param y the y coordinate in the view space * @param a the allocation of this <code>View</code> * @param b the bias to use * * @return the position in the document that corresponds to the screen * coordinates <code>x, y</code> */ public int viewToModel(float x, float y, Shape a, Position.Bias[] b) { Rectangle rec = a.getBounds(); Document doc = getDocument(); Element root = doc.getDefaultRootElement(); // PlainView doesn't support line-wrapping so we can find out which // Element was clicked on just by the y-position. // Since the coordinates may be outside of the coordinate space // of the allocation area (e.g. user dragged mouse outside // the component) we have to limit the values. // This has the nice effect that the user can drag the // mouse above or below the component and it will still // react to the x values (e.g. when selecting). int lineClicked = Math.min(Math.max((int) (y - rec.y) / metrics.getHeight(), 0), root.getElementCount() - 1); Element line = root.getElement(lineClicked); Segment s = getLineBuffer(); int start = line.getStartOffset(); // We don't want the \n at the end of the line. int end = line.getEndOffset() - 1; try { doc.getText(start, end - start, s); } catch (BadLocationException ble) { AssertionError ae = new AssertionError("Unexpected bad location"); ae.initCause(ble); throw ae; } int pos = Utilities.getTabbedTextOffset(s, metrics, rec.x, (int)x, this, start); return Math.max (0, pos); } /** * Since insertUpdate and removeUpdate each deal with children * Elements being both added and removed, they both have to perform * the same checks. So they both simply call this method. * @param changes the DocumentEvent for the changes to the Document. * @param a the allocation of the View. * @param f the ViewFactory to use for rebuilding. */ protected void updateDamage(DocumentEvent changes, Shape a, ViewFactory f) { float oldMaxLineLength = maxLineLength; Rectangle alloc = a.getBounds(); Element el = getElement(); ElementChange ec = changes.getChange(el); // If ec is null then no lines were added or removed, just // repaint the changed line if (ec == null) { int line = el.getElementIndex(changes.getOffset()); // If characters have been removed from the current longest line // we have to find out which one is the longest now otherwise // the preferred x-axis span will not shrink. if (changes.getType() == DocumentEvent.EventType.REMOVE && el.getElement(line) == longestLine) { maxLineLength = -1; if (determineMaxLineLength() != alloc.width) preferenceChanged(this, true, false); } damageLineRange(line, line, a, getContainer()); return; } Element[] removed = ec.getChildrenRemoved(); Element[] newElements = ec.getChildrenAdded(); // If no Elements were added or removed, we just want to repaint // the area containing the line that was modified if (removed == null && newElements == null) { int line = getElement().getElementIndex(changes.getOffset()); damageLineRange(line, line, a, getContainer()); return; } // Check to see if we removed the longest line, if so we have to // search through all lines and find the longest one again. if (removed != null) { for (int i = 0; i < removed.length; i++) if (removed[i].equals(longestLine)) { // reset maxLineLength and search through all lines for longest one maxLineLength = -1; if (determineMaxLineLength() != alloc.width) preferenceChanged(this, true, removed.length != newElements.length); ((JTextComponent)getContainer()).repaint(); return; } } // If we've reached here, that means we haven't removed the longest line if (newElements == null) { // No lines were added, just repaint the container and exit ((JTextComponent)getContainer()).repaint(); return; } // Make sure we have the metrics updateMetrics(); // If we've reached here, that means we haven't removed the longest line // and we have added at least one line, so we have to check if added lines // are longer than the previous longest line Segment seg = getLineBuffer(); float longestNewLength = 0; Element longestNewLine = null; // Loop through the added lines to check their length for (int i = 0; i < newElements.length; i++) { Element child = newElements[i]; int start = child.getStartOffset(); int end = child.getEndOffset(); try { el.getDocument().getText(start, end - start, seg); } catch (BadLocationException ex) { AssertionError ae = new AssertionError("Unexpected bad location"); ae.initCause(ex); throw ae; } if (seg == null || seg.array == null || seg.count == 0) continue; int width = metrics.charsWidth(seg.array, seg.offset, seg.count); if (width > longestNewLength) { longestNewLine = child; longestNewLength = width; } } // Check if the longest of the new lines is longer than our previous // longest line, and if so update our values if (longestNewLength > maxLineLength) { maxLineLength = longestNewLength; longestLine = longestNewLine; } // Report any changes to the preferred sizes of the view // which may cause the underlying component to be revalidated. boolean widthChanged = oldMaxLineLength != maxLineLength; boolean heightChanged = removed.length != newElements.length; if (widthChanged || heightChanged) preferenceChanged(this, widthChanged, heightChanged); // Repaint the container ((JTextComponent)getContainer()).repaint(); } /** * This method is called when something is inserted into the Document * that this View is displaying. * * @param changes the DocumentEvent for the changes. * @param a the allocation of the View * @param f the ViewFactory used to rebuild */ public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f) { updateDamage(changes, a, f); } /** * This method is called when something is removed from the Document * that this View is displaying. * * @param changes the DocumentEvent for the changes. * @param a the allocation of the View * @param f the ViewFactory used to rebuild */ public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f) { updateDamage(changes, a, f); } /** * This method is called when attributes were changed in the * Document in a location that this view is responsible for. */ public void changedUpdate (DocumentEvent changes, Shape a, ViewFactory f) { updateDamage(changes, a, f); } /** * Repaint the given line range. This is called from insertUpdate, * changedUpdate, and removeUpdate when no new lines were added * and no lines were removed, to repaint the line that was * modified. * * @param line0 the start of the range * @param line1 the end of the range * @param a the rendering region of the host * @param host the Component that uses this View (used to call repaint * on that Component) * * @since 1.4 */ protected void damageLineRange (int line0, int line1, Shape a, Component host) { if (a == null) return; Rectangle rec0 = lineToRect(a, line0); Rectangle rec1 = lineToRect(a, line1); if (rec0 == null || rec1 == null) // something went wrong, repaint the entire host to be safe host.repaint(); else { Rectangle repaintRec = SwingUtilities.computeUnion(rec0.x, rec0.y, rec0.width, rec0.height, rec1); host.repaint(repaintRec.x, repaintRec.y, repaintRec.width, repaintRec.height); } } /** * Provides a {@link Segment} object, that can be used to fetch text from * the document. * * @returna {@link Segment} object, that can be used to fetch text from * the document */ protected Segment getLineBuffer() { if (lineBuffer == null) lineBuffer = new Segment(); return lineBuffer; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?