Changeset 7505 in josm
- Timestamp:
- 2014-09-05T16:28:05+02:00 (11 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPresetItems.java
r7411 r7505 592 592 private JComponent value; 593 593 594 @Override public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) { 594 @Override 595 public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) { 595 596 596 597 // find out if our key is already used in the selection. … … 602 603 initAutoCompletionField(textField, key); 603 604 } 605 textField.setHint(key); 604 606 if (length != null && !length.isEmpty()) { 605 607 textField.setMaxChars(Integer.valueOf(length)); … … 1219 1221 AutoCompletingTextField tf = new AutoCompletingTextField(); 1220 1222 initAutoCompletionField(tf, key); 1223 tf.setHint(key); 1221 1224 if (length != null && !length.isEmpty()) { 1222 1225 tf.setMaxChars(Integer.valueOf(length)); -
trunk/src/org/openstreetmap/josm/gui/widgets/JosmTextField.java
r7375 r7505 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.gui.widgets; 3 4 import java.awt.Color; 5 import java.awt.FontMetrics; 6 import java.awt.Graphics; 7 import java.awt.Graphics2D; 8 import java.awt.Insets; 9 import java.awt.RenderingHints; 10 import java.awt.event.FocusEvent; 11 import java.awt.event.FocusListener; 3 12 4 13 import javax.swing.JTextField; … … 6 15 7 16 /** 8 * Subclass of {@link JTextField} that adds a "native" context menu (cut/copy/paste/select all). 17 * Subclass of {@link JTextField} that adds a "native" context menu (cut/copy/paste/select all) 18 * and an optional "hint" displayed when no text has been entered. 9 19 * @since 5886 10 20 */ 11 public class JosmTextField extends JTextField { 21 public class JosmTextField extends JTextField implements FocusListener { 22 23 private String hint; 12 24 13 25 /** … … 34 46 setMinimumSize(getPreferredSize()); 35 47 } 48 addFocusListener(this); 36 49 } 37 50 … … 84 97 this(null, null, 0); 85 98 } 99 100 /** 101 * Replies the hint displayed when no text has been entered. 102 * @return the hint 103 * @since 7505 104 */ 105 public final String getHint() { 106 return hint; 107 } 108 109 /** 110 * Sets the hint to display when no text has been entered. 111 * @param hint the hint to set 112 * @since 7505 113 */ 114 public final void setHint(String hint) { 115 this.hint = hint; 116 } 117 118 @Override 119 public void paint(Graphics g) { 120 super.paint(g); 121 if (hint != null && !hint.isEmpty() && getText().isEmpty() && !isFocusOwner()) { 122 // Taken from http://stackoverflow.com/a/24571681/2257172 123 int h = getHeight(); 124 ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 125 Insets ins = getInsets(); 126 FontMetrics fm = g.getFontMetrics(); 127 int c0 = getBackground().getRGB(); 128 int c1 = getForeground().getRGB(); 129 int m = 0xfefefefe; 130 int c2 = ((c0 & m) >>> 1) + ((c1 & m) >>> 1); 131 g.setColor(new Color(c2, true)); 132 g.drawString(hint, ins.left, h / 2 + fm.getAscent() / 2 - 2); 133 } 134 } 135 136 @Override 137 public void focusGained(FocusEvent e) { 138 repaint(); 139 } 140 141 @Override 142 public void focusLost(FocusEvent e) { 143 repaint(); 144 } 86 145 }
Note:
See TracChangeset
for help on using the changeset viewer.