Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / swing / JButton.java @ 40561

History | View | Annotate | Download (4.58 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gui.beans.swing;
25

    
26
import java.awt.Dimension;
27

    
28
import javax.swing.Icon;
29

    
30
/**
31
 * According to the gvSIG's GUI style sheet all the buttons in the application
32
 * will have a normative size. No smaller than a concrete size, and big enough
33
 * to contain the text and avoiding the "..." characters. The button will grow
34
 * up in width by a set of widths defined in this style sheet, always choosing
35
 * the smallest width that can contain the text. If the biggest width is not
36
 * enought for this purpose then the button will automatically grow up to the
37
 * smallest necessary width to fit the text.<br>
38
 * <p>
39
 * The button resizing is based on the <b>setText(String txt)</b> method.
40
 * However, it is possible to use a custom size if you invoke one of
41
 * <b>setSize(..)</b>, <b>setBorders(...)</b> or <b>setPreferredSize(...)</b>
42
 * after invoking the <b>setText(...)<b> method.
43
 * <p>
44
 * This class is just a standard javax.swing.JButton that handles this issue.
45
 * </p>
46
 * 
47
 * @author jaume dominguez faus - jaume.dominguez@iver.es
48
 * 
49
 * @deprecated use the
50
 *             org.gvsig.tools.swing.api.usability.UsabilitySwingManager.
51
 *             createJButton() methods instead. You can get the instance of the
52
 *             UsabilitySwingManager through the
53
 *             org.gvsig.tools.swing.api.ToolsSwingLocator class.
54
 * 
55
 */
56
public class JButton extends javax.swing.JButton {
57
  private static final long serialVersionUID = -1635879317292710725L;
58

    
59
        // TODO this should be initialized from a properties file or so.
60
        private static int[][] buttonSizes = new int[][] {
61
                        new int[] { 90, 23},
62
                        new int[] {110, 23},
63
                        new int[] {135, 23},
64
                        new int[] {160, 23}
65
        };
66

    
67
        private String enableText;
68
        private String toolTip;
69

    
70

    
71

    
72
        /**
73
         * Creates a new empty instance of org.gvsig.gui.beans.swing.JButton.
74
         */
75
        public JButton() {
76
                super();
77
        }
78

    
79
        /**
80
         * Creates a new instance of org.gvsig.gui.beans.swing.JButton containing a text.
81
         * @param text
82
         */
83
        public JButton(String text) {
84
                super();
85
                setText(text);
86
        }
87

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

    
99
        /**
100
         * Creates a new instance of org.gvsig.gui.beans.swing.JButton containing an image.
101
         */
102
        public JButton(Icon icon) {
103
                super(icon);
104
        }
105

    
106

    
107
        /**
108
         * Gets the text that appears in the tooltip when the button is disabled.
109
         * @return String
110
         */
111
        public String getEnableText() {
112
                return enableText;
113
        }
114
        /**
115
         * Sets the text that appears in the tooltip when the button is disabled.
116
         * @param enableText The enableText to set.
117
         */
118
        public void setEnableText(String enableText) {
119
                this.enableText = enableText;
120
        }
121

    
122

    
123
        public void setEnabled(boolean aFlag) {
124
                super.setEnabled(aFlag);
125
                if (aFlag){
126
                        setToolTipText(toolTip);
127
                }else{
128
                        setToolTipText(enableText);
129
                }
130
        }
131

    
132
        /**
133
         * Sets the text that appears in the tooltip when the button is enabled.
134
         */
135
        public void setToolTip(String text) {
136
                toolTip = text;
137
        }
138

    
139
        public void setText(String text) {
140
                super.setText(text);
141
                Dimension d = getUI().getMinimumSize(this);
142
                int oldWidth = (int) d.getWidth(), newWidth = oldWidth;
143
                int oldHeight = (int) d.getHeight(), newHeight = oldHeight;
144

    
145
                // figure out the suitable width
146
                for (int i = buttonSizes.length-1; i >= 0 ; i--)
147
                        if (oldWidth < buttonSizes[i][0])
148
                                newWidth = buttonSizes[i][0];
149

    
150
                // figure out the suitable height
151
                for (int i = buttonSizes.length-1; i >= 0 ; i--)
152
                        if (oldHeight < buttonSizes[i][1])
153
                                newHeight = buttonSizes[i][1];
154

    
155
                Dimension sz = new Dimension(newWidth, newHeight);
156
                super.setSize(sz);
157
                super.setPreferredSize(sz);
158
        }
159

    
160
}