Java Code Examples for javax.swing.text.JTextComponent#hasFocus()

The following examples show how to use javax.swing.text.JTextComponent#hasFocus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void paint(Graphics g, JComponent c) {
  super.paint(g, c);
  if (c instanceof JLayer) {
    JLayer<?> jlayer = (JLayer<?>) c;
    JTextComponent tc = (JTextComponent) jlayer.getView();
    if (tc.getText().isEmpty() && !tc.hasFocus()) {
      Graphics2D g2 = (Graphics2D) g.create();
      g2.setPaint(hint.getBackground());
      Insets i = tc.getInsets();
      Dimension d = hint.getPreferredSize();
      SwingUtilities.paintComponent(g2, hint, tc, i.left, i.top, d.width, d.height);
      // int baseline = tc.getBaseline(tc.getWidth(), tc.getHeight());
      // Font font = tc.getFont();
      // FontRenderContext frc = g2.getFontRenderContext();
      // TextLayout tl = new TextLayout(hintMessage, font, frc);
      // tl.draw(g2, i.left + 2, baseline);
      g2.dispose();
    }
  }
}
 
Example 2
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void paint(Graphics g, JComponent c) {
  super.paint(g, c);
  if (c instanceof JLayer) {
    JTextComponent tc = (JTextComponent) ((JLayer<?>) c).getView();
    if (tc.getText().length() == 0 && !tc.hasFocus()) {
      Graphics2D g2 = (Graphics2D) g.create();
      g2.setPaint(INACTIVE);
      // System.out.println("getInsets: " + tc.getInsets());
      // System.out.println("getMargin: " + tc.getMargin());
      Insets i = tc.getMargin();
      Dimension d = hint.getPreferredSize();
      SwingUtilities.paintComponent(g2, hint, tc, i.left, i.top, d.width, d.height);
      g2.dispose();
    }
  }
}
 
Example 3
Source File: EditorCaret.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void modifiedUpdate(DocumentEvent evt, int offset, int endOffset, int setDotOffset) {
    // For typing modification ensure that the last caret will be visible after the typing modification.
    // Otherwise the caret would go off the screen when typing at the end of a long line.
    // It might make sense to make an implicit dot setting to end of the modification
    // but that would break existing tests like TypingCompletionUnitTest wihch expects
    // that even typing modifications do not influence the caret position (not only the non-typing ones).
    boolean typingModification = DocumentUtilities.isTypingModification(evt.getDocument());
    JTextComponent c = component;
    scrollToLastCaret |= typingModification && (c != null && c.hasFocus());

    if (!implicitSetDot(evt, setDotOffset)) {
        // Ensure that a valid atomicSectionImplicitSetDotOffset value
        // will be updated by the just performed document modification
        if (atomicSectionImplicitSetDotOffset != Integer.MAX_VALUE) {
            atomicSectionImplicitSetDotOffset = DocumentUtilities.fixOffset(
                    atomicSectionImplicitSetDotOffset, evt);
        }
    }

    if (inAtomicSection) {
        extendAtomicSectionChangeArea(offset, endOffset);
    } else { // Not in atomic section
        invalidateCaretBounds(offset, endOffset);
        updateAndFireChange();
    }
}
 
Example 4
Source File: HintTextFieldUI.java    From gdx-texture-packer-gui with Apache License 2.0 6 votes vote down vote up
@Override
protected void paintSafely(Graphics g) {
    super.paintSafely(g);
    JTextComponent comp = getComponent();
    if (hint != null && comp.getText().length() == 0 && (!(hideOnFocus && comp.hasFocus()))) {
        if (color != null) {
            g.setColor(color);
        } else {
            g.setColor(comp.getForeground().brighter().brighter().brighter());
        }
        Insets margin = comp.getMargin();
        int padding = (comp.getHeight() - comp.getFont().getSize()) / 2;
        //TODO Apply vertical margin as well
        g.drawString(hint, margin.left + 2, comp.getHeight() - padding - 1);
    }
}
 
Example 5
Source File: AquaTextFieldSearch.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
static void updatePromptLabelOnEDT(final JLabel label, final JTextComponent text) {
    String promptText = " ";
    if (!text.hasFocus() && "".equals(text.getText())) {
        final Object prompt = text.getClientProperty(PROMPT_KEY);
        if (prompt != null) promptText = prompt.toString();
    }
    label.setText(promptText);
}
 
Example 6
Source File: AquaTextFieldSearch.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void updatePromptLabelOnEDT(final JLabel label, final JTextComponent text) {
    String promptText = " ";
    if (!text.hasFocus() && "".equals(text.getText())) {
        final Object prompt = text.getClientProperty(PROMPT_KEY);
        if (prompt != null) promptText = prompt.toString();
    }
    label.setText(promptText);
}
 
Example 7
Source File: AquaTextFieldSearch.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void updatePromptLabelOnEDT(final JLabel label, final JTextComponent text) {
    String promptText = " ";
    if (!text.hasFocus() && "".equals(text.getText())) {
        final Object prompt = text.getClientProperty(PROMPT_KEY);
        if (prompt != null) promptText = prompt.toString();
    }
    label.setText(promptText);
}
 
Example 8
Source File: HintTextFieldUI.java    From azure-devops-intellij with MIT License 5 votes vote down vote up
@Override
protected void paintSafely(Graphics g) {
    super.paintSafely(g);
    JTextComponent component = getComponent();
    if (hintText != null && component.getText().length() == 0 && !component.hasFocus()) {
        g.setColor(JBColor.GRAY);
        final int fontSize = component.getFont().getSize();
        final int padding = (component.getHeight() - fontSize) / 2;
        final int x = component.getInsets().left;
        final int y = component.getHeight() - padding - 1;
        g.drawString(hintText, x, y);
    }
}
 
Example 9
Source File: Preferences.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
void grab(JTextComponent cmp)  { 
     if(firstGrab == 0)
firstGrab = System.currentTimeMillis();
     if(applied || System.currentTimeMillis() - firstGrab > (5 * SWING_DELAY)) {
dot  = cmp.getCaret().getDot(); 
mark = cmp.getCaret().getMark();
len  = cmp.getDocument().getLength();
     }
     focus = cmp.hasFocus();
   }
 
Example 10
Source File: AquaTextFieldSearch.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void updatePromptLabelOnEDT(final JLabel label, final JTextComponent text) {
    String promptText = " ";
    if (!text.hasFocus() && "".equals(text.getText())) {
        final Object prompt = text.getClientProperty(PROMPT_KEY);
        if (prompt != null) promptText = prompt.toString();
    }
    label.setText(promptText);
}
 
Example 11
Source File: AquaTextFieldSearch.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
static void updatePromptLabelOnEDT(final JLabel label, final JTextComponent text) {
    String promptText = " ";
    if (!text.hasFocus() && "".equals(text.getText())) {
        final Object prompt = text.getClientProperty(PROMPT_KEY);
        if (prompt != null) promptText = prompt.toString();
    }
    label.setText(promptText);
}
 
Example 12
Source File: AquaTextFieldSearch.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void updatePromptLabelOnEDT(final JLabel label, final JTextComponent text) {
    String promptText = " ";
    if (!text.hasFocus() && "".equals(text.getText())) {
        final Object prompt = text.getClientProperty(PROMPT_KEY);
        if (prompt != null) promptText = prompt.toString();
    }
    label.setText(promptText);
}
 
Example 13
Source File: AquaTextFieldSearch.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static void updatePromptLabelOnEDT(final JLabel label, final JTextComponent text) {
    String promptText = " ";
    if (!text.hasFocus() && "".equals(text.getText())) {
        final Object prompt = text.getClientProperty(PROMPT_KEY);
        if (prompt != null) promptText = prompt.toString();
    }
    label.setText(promptText);
}
 
Example 14
Source File: AquaTextFieldSearch.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void updatePromptLabelOnEDT(final JLabel label, final JTextComponent text) {
    String promptText = " ";
    if (!text.hasFocus() && "".equals(text.getText())) {
        final Object prompt = text.getClientProperty(PROMPT_KEY);
        if (prompt != null) promptText = prompt.toString();
    }
    label.setText(promptText);
}
 
Example 15
Source File: DarculaTextBorder.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void paintDarculaSearchArea(Graphics2D g, Rectangle r, JTextComponent c, boolean fillBackground) {
  Graphics2D g2 = (Graphics2D)g.create();
  try {
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, MacUIUtil.USE_QUARTZ ? RenderingHints.VALUE_STROKE_PURE : RenderingHints.VALUE_STROKE_NORMALIZE);

    JBInsets.removeFrom(r, JBUI.insets(1));
    g2.translate(r.x, r.y);

    float arc = COMPONENT_ARC.get();
    float lw = LW.getFloat();
    float bw = BW.getFloat();
    Shape outerShape = new RoundRectangle2D.Float(bw, bw, r.width - bw * 2, r.height - bw * 2, arc, arc);
    if (fillBackground) {
      g2.setColor(c.getBackground());
      g2.fill(outerShape);
    }

    if (c.getClientProperty("JTextField.Search.noBorderRing") != Boolean.TRUE) {
      if (c.hasFocus()) {
        paintFocusBorder(g2, r.width, r.height, arc, true);
      }
      Path2D path = new Path2D.Float(Path2D.WIND_EVEN_ODD);
      path.append(outerShape, false);

      arc = arc > lw ? arc - lw : 0.0f;
      path.append(new RoundRectangle2D.Float(bw + lw, bw + lw, r.width - (bw + lw) * 2, r.height - (bw + lw) * 2, arc, arc), false);

      g2.setColor(DarculaUIUtil.getOutlineColor(c.isEnabled() && c.isEditable(), c.hasFocus()));
      g2.fill(path);
    }
  }
  finally {
    g2.dispose();
  }
}
 
Example 16
Source File: AquaTextFieldSearch.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static void updatePromptLabelOnEDT(final JLabel label, final JTextComponent text) {
    String promptText = " ";
    if (!text.hasFocus() && "".equals(text.getText())) {
        final Object prompt = text.getClientProperty(PROMPT_KEY);
        if (prompt != null) promptText = prompt.toString();
    }
    label.setText(promptText);
}
 
Example 17
Source File: DarculaPasswordFieldUI.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void paintBackground(Graphics graphics) {
  Graphics2D g = (Graphics2D)graphics;
  final JTextComponent c = getComponent();
  final Container parent = c.getParent();
  if (parent != null) {
    g.setColor(parent.getBackground());
    g.fillRect(0, 0, c.getWidth(), c.getHeight());
  }
  final Border border = c.getBorder();
  if (border instanceof DarculaTextBorder) {
    g.setColor(c.getBackground());
    final int width = c.getWidth();
    final int height = c.getHeight();
    final Insets i = border.getBorderInsets(c);
    if (c.hasFocus()) {
      final GraphicsConfig config = new GraphicsConfig(g);
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);

      g.fillRoundRect(i.left - 5, i.top - 2, width - i.left - i.right + 10, height - i.top - i.bottom + 6, 5, 5);
      config.restore();
    }
    else {
      g.fillRect(i.left - 5, i.top - 2, width - i.left - i.right + 12, height - i.top - i.bottom + 6);
    }
  } else {
    super.paintBackground(g);
  }
}
 
Example 18
Source File: ModernPasswordFieldUI.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void paintBackground(Graphics graphics) {
  Graphics2D g = (Graphics2D)graphics;
  final JTextComponent c = getComponent();
  final Container parent = c.getParent();
  if (parent != null) {
    g.setColor(parent.getBackground());
    g.fillRect(0, 0, c.getWidth(), c.getHeight());
  }
  final Border border = c.getBorder();
  if (border instanceof ModernTextBorder) {
    g.setColor(c.getBackground());
    final int width = c.getWidth();
    final int height = c.getHeight();
    final Insets i = border.getBorderInsets(c);
    if (c.hasFocus()) {
      final GraphicsConfig config = new GraphicsConfig(g);
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);

      g.fillRect(i.left - 5, i.top - 2, width - i.left - i.right + 10, height - i.top - i.bottom + 6);
      config.restore();
    }
    else {
      g.fillRect(i.left - 5, i.top - 2, width - i.left - i.right + 12, height - i.top - i.bottom + 6);
    }
  }
  else {
    super.paintBackground(g);
  }
}
 
Example 19
Source File: BaseCaret.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void mousePressed(MouseEvent evt) {
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("mousePressed: " + logMouseEvent(evt) + ", state=" + mouseState + '\n'); // NOI18N
    }

    JTextComponent c = component;
    if (c != null && isLeftMouseButtonExt(evt)) {
        // Expand fold if offset is in collapsed fold
        int offset = mouse2Offset(evt);
        switch (evt.getClickCount()) {
            case 1: // Single press
                if (c.isEnabled() && !c.hasFocus()) {
                    c.requestFocus();
                }
                c.setDragEnabled(true);
                if (evt.isShiftDown()) { // Select till offset
                    moveDot(offset);
                    adjustRectangularSelectionMouseX(evt.getX(), evt.getY()); // also fires state change
                    mouseState = MouseState.CHAR_SELECTION;
                } else { // Regular press
                    // check whether selection drag is possible
                    if (isDragPossible(evt) && mapDragOperationFromModifiers(evt) != TransferHandler.NONE) {
                        mouseState = MouseState.DRAG_SELECTION_POSSIBLE;
                    } else { // Drag not possible
                        mouseState = MouseState.CHAR_SELECTION;
                        setDot(offset);
                    }
                }
                break;

            case 2: // double-click => word selection
                mouseState = MouseState.WORD_SELECTION;
                // Disable drag which would otherwise occur when mouse would be over text
                c.setDragEnabled(false);
                // Check possible fold expansion
                try {
                    // hack, to get knowledge of possible expansion. Editor depends on Folding, so it's not really possible
                    // to have Folding depend on BaseCaret (= a cycle). If BaseCaret moves to editor.lib2, this contract
                    // can be formalized as an interface.
                    Callable<Boolean> cc = (Callable<Boolean>)c.getClientProperty("org.netbeans.api.fold.expander");
                    if (cc == null || !cc.equals(this)) {
                        if (selectWordAction == null) {
                            selectWordAction = ((BaseKit) c.getUI().getEditorKit(
                                    c)).getActionByName(BaseKit.selectWordAction);
                        }
                        if (selectWordAction != null) {
                            selectWordAction.actionPerformed(null);
                        }
                        // Select word action selects forward i.e. dot > mark
                        minSelectionStartOffset = getMark();
                        minSelectionEndOffset = getDot();
                    }
                } catch (Exception ex) {
                    Exceptions.printStackTrace(ex);
                }
                break;
                
            case 3: // triple-click => line selection
                mouseState = MouseState.LINE_SELECTION;
                // Disable drag which would otherwise occur when mouse would be over text
                c.setDragEnabled(false);
                if (selectLineAction == null) {
                    selectLineAction = ((BaseKit) c.getUI().getEditorKit(
                            c)).getActionByName(BaseKit.selectLineAction);
                }
                if (selectLineAction != null) {
                    selectLineAction.actionPerformed(null);
                    // Select word action selects forward i.e. dot > mark
                    minSelectionStartOffset = getMark();
                    minSelectionEndOffset = getDot();
                }
                break;

            default: // multi-click
        }
    }
}
 
Example 20
Source File: MacIntelliJTextFieldUI.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected void paintSearchField(Graphics2D g, JTextComponent c, Rectangle r) {
  Graphics2D g2 = (Graphics2D)g.create();
  try {
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, MacUIUtil.USE_QUARTZ ? RenderingHints.VALUE_STROKE_PURE : RenderingHints.VALUE_STROKE_NORMALIZE);
    g2.translate(r.x, r.y);

    int arc = JBUI.scale(6);
    double lw = UIUtil.isRetina(g2) ? 0.5 : 1.0;
    Shape outerShape = new RoundRectangle2D.Double(JBUI.scale(3), JBUI.scale(3),
                                                   r.width - JBUI.scale(6),
                                                   r.height - JBUI.scale(6),
                                                   arc, arc);
    g2.setColor(c.getBackground());
    g2.fill(outerShape);

    Path2D path = new Path2D.Double(Path2D.WIND_EVEN_ODD);
    path.append(outerShape, false);
    path.append(new RoundRectangle2D.Double(JBUI.scale(3) + lw, JBUI.scale(3) + lw,
                                            r.width - JBUI.scale(6) - lw*2,
                                            r.height - JBUI.scale(6) - lw*2,
                                            arc-lw, arc-lw), false);

    g2.setColor(Gray.xBC);
    g2.fill(path);

    if (c.hasFocus() && c.getClientProperty("JTextField.Search.noBorderRing") != Boolean.TRUE) {
      DarculaUIUtilPart.paintFocusBorder(g2, r.width, r.height, arc, true);
    }

    g2.translate(-r.x, -r.y);

    boolean withHistoryPopup = isSearchFieldWithHistoryPopup(c);
    Icon label = getSearchIcon(c);
    boolean isEmpty = !hasText();
    Point point = getSearchIconCoord();
    if (isEmpty && !c.hasFocus() && !withHistoryPopup) {
      label.paintIcon(c, g2, point.x, point.y);
    } else {
      Graphics ig = g2.create(0, 0, c.getWidth(), c.getHeight());

      Area area = new Area(new Rectangle2D.Double(point.x, point.y, isEmpty ? label.getIconWidth() : 16, label.getIconHeight()));
      area.intersect(new Area(ig.getClip()));
      ig.setClip(area);
      label.paintIcon(c, ig, point.x, point.y);
      ig.dispose();
    }

    if (!isEmpty) {
      Point ic = getClearIconCoord();
      MacIntelliJIconCache.getIcon("searchFieldClear").paintIcon(c, g2, ic.x, ic.y);
    }

    AbstractAction newLineAction = getNewLineAction(c);
    if (newLineAction != null) {
      Icon newLineIcon = (Icon)newLineAction.getValue(Action.SMALL_ICON);
      if (newLineIcon != null) {
        newLineIcon.paintIcon(c, g2, getAddNewLineIconCoord().x, r.y);
      }
    }
  } finally {
    g2.dispose();
  }
}