下面列出了javax.swing.text.JTextComponent#hasFocus ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
}
@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);
}
}
@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();
}
}
}
@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();
}
}
}
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);
}
@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);
}
}
@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);
}
}
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);
}
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();
}
}
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);
}
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);
}
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);
}
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);
}
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);
}
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();
}
@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);
}
}
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);
}
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);
}
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();
}
}
@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
}
}
}