Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_895 / libraries / libUI / src / org / gvsig / gui / beans / swing / JButton.java @ 10328

History | View | Annotate | Download (4.47 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
        // TODO this should be initialized from a properties file or so.
68
        private static int[][] buttonSizes = new int[][] {
69
                        new int[] { 90, 23},
70
                        new int[] {110, 23},
71
                        new int[] {135, 23},
72
                        new int[] {160, 23}
73
        };
74

    
75
        private String enableText;
76
        private String toolTip;
77

    
78

    
79

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

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

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

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

    
114

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

    
130

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

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

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

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

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

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

    
168
}