Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / swing / JButton.java @ 18663

History | View | Annotate | Download (4.54 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.gui.beans.swing;
42

    
43
import java.awt.Dimension;
44

    
45
import javax.swing.Icon;
46

    
47
/**
48
 * According to the gvSIG's GUI style sheet all the buttons in the application will
49
 * have a normative size. No smaller than a concrete size, and big enough to contain
50
 * the text and avoiding the "..." characters. The button will grow up in width by
51
 * a set of widths defined in this style sheet, always choosing the smallest width that
52
 * can contain the text. If the biggest width is not enought for this purpose then the
53
 * button will automatically grow up to the smallest necessary width to fit the text.<br>
54
 * <p>
55
 * The button resizing is based on the <b>setText(String txt)</b> method. However,
56
 * it is possible to use a custom size if you invoke one of <b>setSize(..)</b>,
57
 * <b>setBorders(...)</b> or <b>setPreferredSize(...)</b> after invoking the
58
 * <b>setText(...)<b> method.
59
 * <p>
60
 * This class is just a standard javax.swing.JButton that handles this issue.
61
 * </p>
62
 *
63
 * @author jaume dominguez faus - jaume.dominguez@iver.es
64
 *
65
 */
66
public class JButton extends javax.swing.JButton {
67
  private static final long serialVersionUID = -1635879317292710725L;
68

    
69
        // TODO this should be initialized from a properties file or so.
70
        private static int[][] buttonSizes = new int[][] {
71
                        new int[] { 90, 23},
72
                        new int[] {110, 23},
73
                        new int[] {135, 23},
74
                        new int[] {160, 23}
75
        };
76

    
77
        private String enableText;
78
        private String toolTip;
79

    
80

    
81

    
82
        /**
83
         * Creates a new empty instance of org.gvsig.gui.beans.swing.JButton.
84
         */
85
        public JButton() {
86
                super();
87
        }
88

    
89
        /**
90
         * Creates a new instance of org.gvsig.gui.beans.swing.JButton containing a text.
91
         * @param text
92
         */
93
        public JButton(String text) {
94
                super();
95
                setText(text);
96
        }
97

    
98
        /**
99
         * Creates a new instance of org.gvsig.gui.beans.swing.JButton containing an image and
100
         * a text.
101
         * @param text
102
         * @param icon
103
         */
104
        public JButton(String text, Icon icon) {
105
                super(icon);
106
                setText(text);
107
        }
108

    
109
        /**
110
         * Creates a new instance of org.gvsig.gui.beans.swing.JButton containing an image.
111
         */
112
        public JButton(Icon icon) {
113
                super(icon);
114
        }
115

    
116

    
117
        /**
118
         * Gets the text that appears in the tooltip when the button is disabled.
119
         * @return String
120
         */
121
        public String getEnableText() {
122
                return enableText;
123
        }
124
        /**
125
         * Sets the text that appears in the tooltip when the button is disabled.
126
         * @param enableText The enableText to set.
127
         */
128
        public void setEnableText(String enableText) {
129
                this.enableText = enableText;
130
        }
131

    
132

    
133
        public void setEnabled(boolean aFlag) {
134
                super.setEnabled(aFlag);
135
                if (aFlag){
136
                        setToolTipText(toolTip);
137
                }else{
138
                        setToolTipText(enableText);
139
                }
140
        }
141

    
142
        /**
143
         * Sets the text that appears in the tooltip when the button is enabled.
144
         */
145
        public void setToolTip(String text) {
146
                toolTip = text;
147
        }
148

    
149
        public void setText(String text) {
150
                super.setText(text);
151
                Dimension d = getUI().getMinimumSize(this);
152
                int oldWidth = (int) d.getWidth(), newWidth = oldWidth;
153
                int oldHeight = (int) d.getHeight(), newHeight = oldHeight;
154

    
155
                // figure out the suitable width
156
                for (int i = buttonSizes.length-1; i >= 0 ; i--)
157
                        if (oldWidth < buttonSizes[i][0])
158
                                newWidth = buttonSizes[i][0];
159

    
160
                // figure out the suitable height
161
                for (int i = buttonSizes.length-1; i >= 0 ; i--)
162
                        if (oldHeight < buttonSizes[i][1])
163
                                newHeight = buttonSizes[i][1];
164

    
165
                Dimension sz = new Dimension(newWidth, newHeight);
166
                super.setSize(sz);
167
                super.setPreferredSize(sz);
168
        }
169

    
170
}