/** * Selects a range of text in a text component. If the new selection is outside of the previous * viewable rectangle, then the view is centered around the new selection. * * @param textArea The text component whose selection is to be centered. * @param start The start of the range to select. * @param end The end of the range to select. */ private static void selectAndPossiblyCenter(JTextArea textArea, int start, int end) { textArea.setSelectionStart(start); textArea.setSelectionEnd(end); Rectangle r = null; try { r = textArea.modelToView(start); if (r == null) { // Not yet visible; i.e. JUnit tests return; } if (end != start) { r = r.union(textArea.modelToView(end)); } } catch (BadLocationException ble) { // Never happens ble.printStackTrace(); textArea.setSelectionStart(start); textArea.setSelectionEnd(end); return; } Rectangle visible = textArea.getVisibleRect(); // If the new selection is already in the view, don't scroll, // as that is visually jarring. if (visible.contains(r)) { textArea.setSelectionStart(start); textArea.setSelectionEnd(end); return; } visible.x = r.x - (visible.width - r.width) / 2; visible.y = r.y - (visible.height - r.height) / 2; Rectangle bounds = textArea.getBounds(); Insets i = textArea.getInsets(); bounds.x = i.left; bounds.y = i.top; bounds.width -= i.left + i.right; bounds.height -= i.top + i.bottom; if (visible.x < bounds.x) { visible.x = bounds.x; } if (visible.x + visible.width > bounds.x + bounds.width) { visible.x = bounds.x + bounds.width - visible.width; } if (visible.y < bounds.y) { visible.y = bounds.y; } if (visible.y + visible.height > bounds.y + bounds.height) { visible.y = bounds.y + bounds.height - visible.height; } textArea.scrollRectToVisible(visible); }
public void update() { memView.setText(machine.memToString()); r0.setText(String.format("%04X", machine.readReg(0) & 0xFFFF)); r1.setText(String.format("%04X", machine.readReg(1) & 0xFFFF)); r2.setText(String.format("%04X", machine.readReg(2) & 0xFFFF)); r3.setText(String.format("%04X", machine.readReg(3) & 0xFFFF)); r4.setText(String.format("%04X", machine.readReg(4) & 0xFFFF)); r5.setText(String.format("%04X", machine.readReg(5) & 0xFFFF)); r6.setText(String.format("%04X", machine.readReg(6) & 0xFFFF)); r7.setText(String.format("%04X", machine.readReg(7) & 0xFFFF)); d0.setText(String.format("%d", machine.readReg(0))); d1.setText(String.format("%d", machine.readReg(1))); d2.setText(String.format("%d", machine.readReg(2))); d3.setText(String.format("%d", machine.readReg(3))); d4.setText(String.format("%d", machine.readReg(4))); d5.setText(String.format("%d", machine.readReg(5))); d6.setText(String.format("%d", machine.readReg(6))); d7.setText(String.format("%d", machine.readReg(7))); cbNeg.setSelected(machine.neg()); cbZero.setSelected(machine.zero()); cbPos.setSelected(machine.pos()); status.setText(machine.status()); int line = machine.pcLine(); try { int start = memView.getLineStartOffset(line); int end = memView.getLineEndOffset(line); memView.setCaretPosition(start); memView.select(start, end); Rectangle viewRect = memView.modelToView(start); // Scroll to make the rectangle visible memView.scrollRectToVisible(viewRect); } catch (Exception e) { } btnStep.setEnabled(true); btnStepOver.setEnabled(true); btnRun.setEnabled(true); btnStop.setEnabled(false); }
protected void showSuggestion() { hideSuggestion(); final int position = textarea.getCaretPosition(); Point location; try { location = textarea.modelToView(position).getLocation(); } catch (BadLocationException e2) { e2.printStackTrace(); return; } String text = textarea.getText(); int start = Math.max(0, position - 1); while (start > 0) { if (!Character.isWhitespace(text.charAt(start))) { start--; } else { start++; break; } } if (start > position) { return; } final String subWord = text.substring(start, position); if (subWord.length() < 2) { return; } suggestion = new SuggestionPanel(textarea, position, subWord, location); SwingUtilities.invokeLater( new Runnable() { @Override public void run() { textarea.requestFocusInWindow(); } }); }
/** * Selects a range of text in a text component. If the new selection is outside of the previous * viewable rectangle, then the view is centered around the new selection. * * @param textArea The text component whose selection is to be centered. * @param start The start of the range to select. * @param end The end of the range to select. */ private static void selectAndPossiblyCenter(JTextArea textArea, int start, int end) { boolean foldsExpanded = false; if (textArea instanceof RSyntaxTextArea) { RSyntaxTextArea rsta = (RSyntaxTextArea) textArea; FoldManager fm = rsta.getFoldManager(); if (fm.isCodeFoldingSupportedAndEnabled()) { foldsExpanded = fm.ensureOffsetNotInClosedFold(start); foldsExpanded |= fm.ensureOffsetNotInClosedFold(end); } } textArea.setSelectionStart(start); textArea.setSelectionEnd(end); Rectangle r = null; try { r = textArea.modelToView(start); if (r == null) { // Not yet visible; i.e. JUnit tests return; } if (end != start) { r = r.union(textArea.modelToView(end)); } } catch (BadLocationException ble) { // Never happens ble.printStackTrace(); textArea.setSelectionStart(start); textArea.setSelectionEnd(end); return; } Rectangle visible = textArea.getVisibleRect(); // If the new selection is already in the view, don't scroll, // as that is visually jarring. if (!foldsExpanded && visible.contains(r)) { textArea.setSelectionStart(start); textArea.setSelectionEnd(end); return; } visible.x = r.x - (visible.width - r.width) / 2; visible.y = r.y - (visible.height - r.height) / 2; Rectangle bounds = textArea.getBounds(); Insets i = textArea.getInsets(); bounds.x = i.left; bounds.y = i.top; bounds.width -= i.left + i.right; bounds.height -= i.top + i.bottom; if (visible.x < bounds.x) { visible.x = bounds.x; } if (visible.x + visible.width > bounds.x + bounds.width) { visible.x = bounds.x + bounds.width - visible.width; } if (visible.y < bounds.y) { visible.y = bounds.y; } if (visible.y + visible.height > bounds.y + bounds.height) { visible.y = bounds.y + bounds.height - visible.height; } textArea.scrollRectToVisible(visible); }