View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.plaf;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import javax.swing.*;
28  import javax.swing.plaf.synth.SynthTextFieldUI;
29  import javax.swing.text.JTextComponent;
30  import java.awt.*;
31  import java.awt.event.FocusEvent;
32  import java.awt.event.FocusListener;
33  
34  /**
35   * <p>HintSynthTextFieldUI class.</p>
36   *
37   */
38  public class HintSynthTextFieldUI extends SynthTextFieldUI implements FocusListener {
39  
40      private String hint;
41      private boolean hideOnFocus;
42      private Color color;
43  
44      /**
45       * <p>Getter for the field <code>color</code>.</p>
46       *
47       * @return a {@link java.awt.Color} object.
48       */
49      public Color getColor() {
50          return color;
51      }
52  
53      /**
54       * <p>Setter for the field <code>color</code>.</p>
55       *
56       * @param color a {@link java.awt.Color} object.
57       */
58      public void setColor(Color color) {
59          this.color = color;
60          repaint();
61      }
62  
63      private void repaint() {
64          if (getComponent() != null) {
65              getComponent().repaint();
66          }
67      }
68  
69      /**
70       * <p>isHideOnFocus.</p>
71       *
72       * @return a boolean.
73       */
74      public boolean isHideOnFocus() {
75          return hideOnFocus;
76      }
77  
78      /**
79       * <p>Setter for the field <code>hideOnFocus</code>.</p>
80       *
81       * @param hideOnFocus a boolean.
82       */
83      public void setHideOnFocus(boolean hideOnFocus) {
84          this.hideOnFocus = hideOnFocus;
85          repaint();
86      }
87  
88      /**
89       * <p>Getter for the field <code>hint</code>.</p>
90       *
91       * @return a {@link java.lang.String} object.
92       */
93      public String getHint() {
94          return hint;
95      }
96  
97      /**
98       * <p>Setter for the field <code>hint</code>.</p>
99       *
100      * @param hint a {@link java.lang.String} object.
101      */
102     public void setHint(String hint) {
103         this.hint = hint;
104         repaint();
105     }
106 
107     /**
108      * <p>Constructor for HintSynthTextFieldUI.</p>
109      *
110      * @param hint a {@link java.lang.String} object.
111      */
112     public HintSynthTextFieldUI(String hint) {
113         this(hint, false);
114     }
115 
116     /**
117      * <p>Constructor for HintSynthTextFieldUI.</p>
118      *
119      * @param hint a {@link java.lang.String} object.
120      * @param hideOnFocus a boolean.
121      */
122     public HintSynthTextFieldUI(String hint, boolean hideOnFocus) {
123         this(hint, hideOnFocus, null);
124     }
125 
126     /**
127      * <p>Constructor for HintSynthTextFieldUI.</p>
128      *
129      * @param hint a {@link java.lang.String} object.
130      * @param hideOnFocus a boolean.
131      * @param color a {@link java.awt.Color} object.
132      */
133     public HintSynthTextFieldUI(String hint, boolean hideOnFocus, Color color) {
134         this.hint = hint;
135         this.hideOnFocus = hideOnFocus;
136         this.color = color;
137     }
138 
139     /** {@inheritDoc} */
140     @Override
141     public void installUI(JComponent c) {
142         super.installUI(c);
143         c.setDoubleBuffered(true);
144     }
145 
146     /** {@inheritDoc} */
147     @Override
148     protected void paintSafely(Graphics g) {
149         super.paintSafely(g);
150         JTextComponent comp = getComponent();
151         if (hint != null && comp.getText().length() == 0 && (!(hideOnFocus && comp.hasFocus()))) {
152             if (color != null) {
153                 g.setColor(color);
154             } else {
155                 g.setColor(comp.getForeground().brighter().brighter().brighter());
156             }
157             g.setFont(comp.getFont().deriveFont(Font.ITALIC));
158             FontMetrics fm = g.getFontMetrics();
159             Rectangle clip = getVisibleEditorRect();
160             int x = (int) clip.getMinX();
161             int y = (int) (clip.getCenterY() + fm.getHeight() / 2. - fm.getDescent());
162             g.drawString(hint, x, y);
163         }
164     }
165 
166     /** {@inheritDoc} */
167     @Override
168     public void focusGained(FocusEvent e) {
169         if (hideOnFocus) {
170             repaint();
171         }
172     }
173 
174     /** {@inheritDoc} */
175     @Override
176     public void focusLost(FocusEvent e) {
177         if (hideOnFocus) {
178             repaint();
179         }
180     }
181 
182     /** {@inheritDoc} */
183     @Override
184     protected void installListeners() {
185         super.installListeners();
186         getComponent().addFocusListener(this);
187     }
188 
189     /** {@inheritDoc} */
190     @Override
191     protected void uninstallListeners() {
192         super.uninstallListeners();
193         getComponent().removeFocusListener(this);
194     }
195 }