/** Attempt to find the editor keystroke for the given action. */ private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey) { KeyStroke[] ret = new KeyStroke[] {defaultKey}; JTextComponent comp = getComponent(); if (editorActionName != null && comp != null) { TextUI textUI = comp.getUI(); Keymap km = comp.getKeymap(); if (textUI != null && km != null) { EditorKit kit = textUI.getEditorKit(comp); if (kit instanceof BaseKit) { Action a = ((BaseKit) kit).getActionByName(editorActionName); if (a != null) { KeyStroke[] keys = km.getKeyStrokesForAction(a); if (keys != null && keys.length > 0) { ret = keys; } else { // try kit's keymap Keymap km2 = ((BaseKit) kit).getKeymap(); KeyStroke[] keys2 = km2.getKeyStrokesForAction(a); if (keys2 != null && keys2.length > 0) { ret = keys2; } } } } } } return ret; }
/** Paints one view that corresponds to a line (or multiple lines if folding takes effect). */ private void paintView(View view, Graphics g, int yBase) { JTextComponent component = editorUI.getComponent(); if (component == null) return; BaseTextUI textUI = (BaseTextUI) component.getUI(); Element rootElem = textUI.getRootView(component).getElement(); int line = rootElem.getElementIndex(view.getStartOffset()); String annotation = ""; // NOI18N AnnotateLine al = null; if (!elementAnnotations.isEmpty()) { al = getAnnotateLine(line); if (al != null) { annotation = getDisplayName(al); // NOI18N } } else { annotation = elementAnnotationsSubstitute; } if (al != null && al.getRevision().equals(recentRevision)) { g.setColor(selectedColor()); } else { g.setColor(foregroundColor()); } int texty = yBase + editorUI.getLineAscent(); int textx = 2; g.drawString(annotation, textx, texty); }
/* */ public void paint(Graphics paramGraphics) /* */ { /* 127 */ if (isVisible()) /* */ try { /* 129 */ JTextComponent localJTextComponent = getComponent(); /* 130 */ Color localColor = localJTextComponent.hasFocus() ? localJTextComponent.getCaretColor() : localJTextComponent.getDisabledTextColor(); /* */ /* 132 */ TextUI localTextUI = localJTextComponent.getUI(); /* 133 */ int i = getDot(); /* 134 */ Rectangle localRectangle = localTextUI.modelToView(localJTextComponent, i); /* 135 */ int j = localRectangle.x - 2; /* 136 */ int k = localRectangle.x + 2; /* 137 */ int m = localRectangle.y + 1; /* 138 */ int n = localRectangle.y + localRectangle.height - 2; /* 139 */ paramGraphics.setColor(localColor); /* 140 */ paramGraphics.drawLine(localRectangle.x, m, localRectangle.x, n); /* 141 */ paramGraphics.drawLine(j, m, k, m); /* 142 */ paramGraphics.drawLine(j, n, k, n); /* */ } /* */ catch (BadLocationException localBadLocationException) /* */ { /* */ } /* */ }
/** GlyphGutter copy pasted utility method. */ private int getLineFromMouseEvent(MouseEvent e) { int line = -1; if (editorUI != null) { try { JTextComponent component = editorUI.getComponent(); BaseTextUI textUI = (BaseTextUI) component.getUI(); int clickOffset = textUI.viewToModel(component, new Point(0, e.getY())); line = Utilities.getLineOffset(doc, clickOffset); } catch (BadLocationException ble) { } } return line; }
public static void paintAboveline(Graphics g, JTextComponent tc, int pos0, int pos1) throws BadLocationException { g.setColor(Color.GREEN.darker()); TextUI ui = tc.getUI(); Rectangle r = ui.modelToView(tc, pos1 + 1); int h = r.y; int max = r.x - 2 + 1; int current = ui.modelToView(tc, pos0).x - 1; g.drawLine(current, h, max, h); g.drawLine(current, h + 1, current, h + 2); g.drawLine(max, h + 1, max, h + 2); }
/** * GlyphGutter copy pasted bolerplate method. It invokes {@link #paintView} that contains actual * business logic. */ @Override public void paintComponent(Graphics g) { super.paintComponent(g); Rectangle clip = g.getClipBounds(); JTextComponent component = editorUI.getComponent(); if (component == null) return; BaseTextUI textUI = (BaseTextUI) component.getUI(); View rootView = Utilities.getDocumentView(component); if (rootView == null) return; g.setColor(backgroundColor()); g.fillRect(clip.x, clip.y, clip.width, clip.height); AbstractDocument doc = (AbstractDocument) component.getDocument(); doc.readLock(); try { foldHierarchy.lock(); try { int startPos = textUI.getPosFromY(clip.y); int startViewIndex = rootView.getViewIndex(startPos, Position.Bias.Forward); int rootViewCount = rootView.getViewCount(); if (startViewIndex >= 0 && startViewIndex < rootViewCount) { int clipEndY = clip.y + clip.height; for (int i = startViewIndex; i < rootViewCount; i++) { View view = rootView.getView(i); Rectangle rec = component.modelToView(view.getStartOffset()); if (rec == null) { break; } int y = rec.y; paintView(view, g, y); if (y >= clipEndY) { break; } } } } finally { foldHierarchy.unlock(); } } catch (BadLocationException ble) { Mercurial.LOG.log(Level.WARNING, null, ble); } finally { doc.readUnlock(); } }
@Override public void propertyChange(final PropertyChangeEvent evt) { final String propertyName = evt.getPropertyName(); if (AquaFocusHandler.FRAME_ACTIVE_PROPERTY.equals(propertyName)) { final JTextComponent comp = ((JTextComponent) evt.getSource()); if (evt.getNewValue() == Boolean.TRUE) { setVisible(comp.hasFocus()); } else { setVisible(false); } if (getDot() != getMark()) comp.getUI().damageRange(comp, getDot(), getMark()); } }
public static void paintUnderline(Graphics g, JTextComponent tc, int pos0, int pos1) throws BadLocationException { g.setColor(Color.RED); TextUI ui = tc.getUI(); Rectangle r = ui.modelToView(tc, pos1 + 1); int h = r.y + r.height - 2; int max = r.x - 2; int current = ui.modelToView(tc, pos0).x; while (current <= max) { g.drawLine(current, h, Math.min(current + 2, max), h + 1); current += 3; if (current <= max) { g.drawLine(current, h, Math.min(current + 2, max), h); current += 3; } } }
/** Returns the baseline for single line text components, like <code>JTextField</code>. */ private static int getSingleLineTextBaseline(JTextComponent textComponent, int h) { View rootView = textComponent.getUI().getRootView(textComponent); if (rootView.getViewCount() > 0) { Insets insets = textComponent.getInsets(); int height = h - insets.top - insets.bottom; int y = insets.top; View fieldView = rootView.getView(0); int vspan = (int) fieldView.getPreferredSpan(View.Y_AXIS); if (height != vspan) { int slop = height - vspan; y += slop / 2; } FontMetrics fm = textComponent.getFontMetrics(textComponent.getFont()); y += fm.getAscent(); return y; } return -1; }
public void _paint(Graphics2D g, int offs0, int offs1, Shape bounds, JTextComponent c) { Rectangle alloc = bounds.getBounds(); try { // --- determine locations --- TextUI mapper = c.getUI(); Rectangle p0 = mapper.modelToView(c, offs0); Rectangle p1 = mapper.modelToView(c, offs1); // --- render --- g.setColor(color); if (p0.y == p1.y) { // same line, render a rectangle Rectangle r = p0.union(p1); g.fillRect(r.x, r.y, r.width, r.height); if (border != null) { border.paintBorder(c, g, r.x, r.y, r.width, r.height); } } else { // different lines int p0ToMarginWidth = alloc.x + alloc.width - p0.x; g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height); if ((p0.y + p0.height) != p1.y) { g.fillRect(alloc.x, p0.y + p0.height, alloc.width, p1.y - (p0.y + p0.height)); } g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height); } } catch (BadLocationException e) { // can't render } }
/** * Add the presenters (usually buttons) for the contents of the toolbar contained in the base and * mime folders. * * @param baseFolder folder that corresponds to "text/base" * @param mimeFolder target mime type folder. * @param toolbar toolbar being constructed. */ private void addPresenters() { JTextComponent c = getComponent(); String mimeType = c == null ? null : NbEditorUtilities.getMimeType(c); if (mimeType == null) { return; // Probably no component or it's not loaded properly } List<? extends MultiKeyBinding> keybindings = null; Lookup actionContext = null; List items = ToolbarActionsProvider.getToolbarItems(mimeType); // COMPAT: The ToolbarsActionsProvider treats 'text/base' in a special way. It // will list only items registered for this particular mime type, but won't // inherit anything else. The 'text/base' is normally empty, but could be // used by some legacy code. List oldTextBaseItems = ToolbarActionsProvider.getToolbarItems("text/base"); // NOI18N if (oldTextBaseItems.size() > 0) { items = new ArrayList(items); items.add(new JSeparator()); items.addAll(oldTextBaseItems); } for (Object item : items) { LOG.log(Level.FINE, "Adding item {0}", item); // NOI18N if (item == null || item instanceof JSeparator) { addSeparator(); continue; } if (item instanceof String) { EditorKit kit = c.getUI().getEditorKit(c); if (kit instanceof BaseKit) { Action a = ((BaseKit) kit).getActionByName((String) item); if (a != null) { if (LOG.isLoggable(Level.FINE)) { LOG.log( Level.FINE, "Item {0} converted to an editor action {1}", new Object[] {item, s2s(a)}); // NOI18N } item = a; } else { // unknown action continue; } } } if (item instanceof ContextAwareAction) { if (actionContext == null) { Lookup context = createActionContext(c); actionContext = context == null ? NO_ACTION_CONTEXT : context; } if (actionContext != NO_ACTION_CONTEXT) { Action caa = ((ContextAwareAction) item).createContextAwareInstance(actionContext); // use the context aware instance only if it implements Presenter.Toolbar // or is a Component else fall back to the original object if (caa instanceof Presenter.Toolbar || caa instanceof Component) { if (LOG.isLoggable(Level.FINE)) { LOG.log( Level.FINE, "Item {0} converted to a context-aware Action {1}", new Object[] {s2s(item), s2s(caa)}); // NOI18N } item = caa; } } } if (item instanceof Presenter.Toolbar) { Component presenter = ((Presenter.Toolbar) item).getToolbarPresenter(); if (presenter != null) { if (LOG.isLoggable(Level.FINE)) { LOG.log( Level.FINE, "Item {0} converted to a Presenter.Toolbar {1}", new Object[] {s2s(item), s2s(presenter)}); // NOI18N } item = presenter; } } if (item instanceof Component) { add((Component) item); if (LOG.isLoggable(Level.FINE)) { LOG.log(Level.FINE, "Adding component {0}", s2s(item)); // NOI18N } } else if (item instanceof Action) { // Wrap action to execute on the proper text component // because the default fallback in TextAction.getTextComponent() // might not work properly if the focus was switched // to e.g. a JTextField and then toolbar was clicked. Action a = new WrapperAction(componentRef, (Action) item); // Try to find an icon if not present updateIcon(a); // Add the action and let the JToolbar to creat a presenter for it item = add(a); if (LOG.isLoggable(Level.FINE)) { LOG.log( Level.FINE, "Adding action {0} as {1}", new Object[] {s2s(a), s2s(item)}); // NOI18N } } else { // Some sort of crappy item -> ignore if (LOG.isLoggable(Level.FINE)) { LOG.log(Level.FINE, "Ignoring item {0}", s2s(item)); // NOI18N } continue; } if (item instanceof AbstractButton) { AbstractButton button = (AbstractButton) item; processButton(button); if (keybindings == null) { List<? extends MultiKeyBinding> l = getKeyBindingList(); keybindings = l == null ? Collections.<MultiKeyBinding>emptyList() : l; } updateTooltip(button, keybindings); if (LOG.isLoggable(Level.FINE)) { LOG.log(Level.FINE, "Special treatment for button {0}", s2s(item)); // NOI18N } } } }
@Override public void actionPerformed(ActionEvent e) { JTextComponent textArea = (JTextComponent) e.getSource(); Caret caret = textArea.getCaret(); int dot = caret.getDot(); /* * Move to the beginning/end of selection on a "non-shifted" * left- or right-keypress. We shouldn't have to worry about * navigation filters as, if one is being used, it let us get * to that position before. */ if (!select) { switch (direction) { case SwingConstants.EAST: int mark = caret.getMark(); if (dot != mark) { caret.setDot(Math.max(dot, mark)); return; } break; case SwingConstants.WEST: mark = caret.getMark(); if (dot != mark) { caret.setDot(Math.min(dot, mark)); return; } break; default: } } Position.Bias[] bias = new Position.Bias[1]; Point magicPosition = caret.getMagicCaretPosition(); try { if (magicPosition == null && (direction == SwingConstants.NORTH || direction == SwingConstants.SOUTH)) { Rectangle r = textArea.modelToView(dot); magicPosition = new Point(r.x, r.y); } NavigationFilter filter = textArea.getNavigationFilter(); if (filter != null) { dot = filter.getNextVisualPositionFrom( textArea, dot, Position.Bias.Forward, direction, bias); } else { if (direction == SwingConstants.NORTH || direction == SwingConstants.SOUTH) { dot = getNSVisualPosition((EditorPane) textArea, dot, direction); } else { dot = textArea .getUI() .getNextVisualPositionFrom( textArea, dot, Position.Bias.Forward, direction, bias); } } if (select) { caret.moveDot(dot); } else { caret.setDot(dot); } if (magicPosition != null && (direction == SwingConstants.NORTH || direction == SwingConstants.SOUTH)) { caret.setMagicCaretPosition(magicPosition); } } catch (BadLocationException ble) { Debug.error(me + "Problem while trying to move caret\n%s", ble.getMessage()); } }