1 package fr.ifremer.quadrige3.ui.swing.component;
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 org.apache.commons.lang3.StringUtils;
28
29 import javax.swing.Icon;
30 import javax.swing.JLabel;
31 import javax.swing.UIManager;
32 import javax.swing.plaf.nimbus.NimbusLookAndFeel;
33 import javax.swing.plaf.synth.SynthContext;
34 import javax.swing.plaf.synth.SynthLabelUI;
35 import java.awt.Dimension;
36 import java.awt.Graphics;
37
38 /**
39 * JLabel that allows (or forces) a ellipsis at the end of the text if container size is smaller than the rendered text size
40 * When input text is shrink, a automatic tooltip is generated
41 *
42 * Note: an HTML text won't be affect by this behavior
43 * Note2: Nimbus LAF must be used
44 *
45 * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
46 */
47 public class ShrinkableLabel extends JLabel {
48
49 private ShrinkableLabelUI shrinkableLabelUI;
50
51 /**
52 * <p>Constructor for ShrinkableLabel.</p>
53 *
54 * @param text a {@link java.lang.String} object.
55 * @param icon a {@link javax.swing.Icon} object.
56 * @param horizontalAlignment a int.
57 */
58 public ShrinkableLabel(String text, Icon icon, int horizontalAlignment) {
59 super(text, icon, horizontalAlignment);
60 init();
61 }
62
63 /**
64 * <p>Constructor for ShrinkableLabel.</p>
65 *
66 * @param text a {@link java.lang.String} object.
67 * @param horizontalAlignment a int.
68 */
69 public ShrinkableLabel(String text, int horizontalAlignment) {
70 super(text, horizontalAlignment);
71 init();
72 }
73
74 /**
75 * <p>Constructor for ShrinkableLabel.</p>
76 *
77 * @param text a {@link java.lang.String} object.
78 */
79 public ShrinkableLabel(String text) {
80 super(text);
81 init();
82 }
83
84 /**
85 * <p>Constructor for ShrinkableLabel.</p>
86 *
87 * @param image a {@link javax.swing.Icon} object.
88 * @param horizontalAlignment a int.
89 */
90 public ShrinkableLabel(Icon image, int horizontalAlignment) {
91 super(image, horizontalAlignment);
92 init();
93 }
94
95 /**
96 * <p>Constructor for ShrinkableLabel.</p>
97 *
98 * @param image a {@link javax.swing.Icon} object.
99 */
100 public ShrinkableLabel(Icon image) {
101 super(image);
102 init();
103 }
104
105 /**
106 * <p>Constructor for ShrinkableLabel.</p>
107 */
108 public ShrinkableLabel() {
109 super();
110 init();
111 }
112
113 private void init() {
114
115 setPreferredSize(new Dimension());
116 setToolTipText("");
117
118 if (UIManager.getLookAndFeel() instanceof NimbusLookAndFeel) {
119 shrinkableLabelUI = new ShrinkableLabelUI();
120 setUI(shrinkableLabelUI);
121 }
122 }
123
124 /** {@inheritDoc} */
125 @Override
126 public String getToolTipText() {
127 String toolTip = super.getToolTipText();
128 if (StringUtils.isNotBlank(toolTip)) {
129 return toolTip;
130 }
131 if (shrinkableLabelUI != null && shrinkableLabelUI.shrink) {
132 return super.getText();
133 }
134 return null;
135
136 }
137
138 class ShrinkableLabelUI extends SynthLabelUI {
139
140 boolean shrink;
141
142 @Override
143 protected void paint(SynthContext context, Graphics g) {
144 super.paint(context, g);
145 Dimension drawDimension = g.getClipBounds().getSize();
146 Dimension componentDimension = getPreferredSize(context.getComponent());
147 shrink = drawDimension.width < componentDimension.width || drawDimension.height < componentDimension.height;
148 }
149
150 }
151 }