Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.app / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / raster / bean / buttonbar / ButtonBarContainer.java @ 875

History | View | Annotate | Download (4.6 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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 2
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
*/
22
package org.gvsig.raster.tools.app.basic.raster.bean.buttonbar;
23

    
24
import java.awt.FlowLayout;
25
import java.util.ArrayList;
26

    
27
import javax.swing.JButton;
28
import javax.swing.JPanel;
29

    
30
import org.gvsig.raster.tools.app.basic.RasterToolsUtil;
31

    
32
public class ButtonBarContainer extends JPanel {
33
        private static final long serialVersionUID = -2556987128553063939L;
34

    
35
        private ArrayList<JButton> buttons = new ArrayList<JButton>();
36

    
37
        private int       wComp              = 400;
38
        private int       hComp              = 26;
39
        private boolean   disableAllControls = false;
40
        private boolean[] buttonsState       = null;
41

    
42

    
43
        /**
44
         * This is the default constructor
45
         */
46
        public ButtonBarContainer() {
47
                super();
48
                initialize();
49
        }
50

    
51
        /**
52
         * This method initializes this
53
         *
54
         * @return void
55
         */
56
        private void initialize() {
57
                FlowLayout flowLayout = new FlowLayout();
58
                flowLayout.setHgap(0);
59
                flowLayout.setVgap(0);
60
                this.setLayout(flowLayout);
61
                this.setSize(wComp, hComp);
62
                }
63

    
64

    
65
        /**
66
         * A?ade un boton al ArrayList de los botones.
67
         *
68
         * @param iconName: nombre del icono asignado al boton. La imagen tendr?a que
69
         *                                         estar dentro de la carpeta "images/"
70
         * @param tip: tip del boton;
71
         * @param order: orden que ocupar? el boton dentro del control
72
         */
73
        public void addButton(String iconName, String tip, int order){
74
                JButton bt = new JButton();
75

    
76
                bt.setPreferredSize(new java.awt.Dimension(22, 22));
77
                try{
78
                        if (iconName != null)
79
                                bt.setIcon(RasterToolsUtil.getIcon(iconName));
80
                }catch(NullPointerException exc){
81
                        //El icono no existe -> No se a?ade ninguno
82
                }
83

    
84
                if(tip != null)
85
                        bt.setToolTipText(tip);
86

    
87
                buttons.add(order, bt);
88
                addList();
89

    
90
        }
91

    
92
        /**
93
         * Elimina el bot?n correspondiente al indice que le pasamos.
94
         * @param index
95
         */
96
        public void delButton(int index){
97
                buttons.remove(index);
98
                this.removeAll();
99
                addList();
100
        }
101

    
102
        /**
103
         * A?ade en el panel los botones que tenemos en el ArrayList.
104
         *
105
         */
106
        public void addList(){
107
                for(int i = 0 ; i < buttons.size() ; i++){
108
                        this.add((JButton)buttons.get(i));
109
                }
110
        }
111

    
112

    
113
        /**
114
         * Esta funci?n deshabilita todos los controles y guarda sus valores
115
         * de habilitado o deshabilitado para que cuando se ejecute restoreControlsValue
116
         * se vuelvan a quedar como estaba
117
         */
118
        public void disableAllControls(){
119
                if(!disableAllControls){
120
                        disableAllControls = true;
121

    
122
                        buttonsState = new boolean[buttons.size()];
123

    
124

    
125

    
126
                        for (int i = 0 ; i < buttons.size() ; i++){
127

    
128
                                //Salvamos los estados
129
                                buttonsState[i] = ((JButton)buttons.get(i)).isEnabled();
130
                                //Desactivamos controles
131
                                ((JButton)buttons.get(i)).setEnabled(false);
132
                        }
133
                }
134
        }
135

    
136
        /**
137
         * Esta funci?n deja los controles como estaban al ejecutar la funci?n
138
         * disableAllControls
139
         */
140
        public void restoreControlsValue(){
141
                if(disableAllControls){
142
                        disableAllControls = false;
143

    
144
                        for(int i = 0 ; i < buttons.size() ; i++){
145
                                ((JButton)buttons.get(i)).setEnabled(buttonsState[i]);
146
                        }
147
                }
148
        }
149

    
150
        /**
151
         * M?todo para acceder a los botones del control;
152
         * @param index
153
         * @return
154
         */
155
        public JButton getButton(int index){
156
                return (JButton)buttons.get(index);
157
        }
158

    
159
        /**
160
         * M?todo para establecer la posici?n de los botones dentro del control.
161
         * @param align: "left" o "right"
162
         */
163
        public void setButtonAlignment(String align){
164
                FlowLayout layout = new FlowLayout();
165
                layout.setHgap(2);
166
                layout.setVgap(0);
167

    
168
                if (align.equals("right"))
169
                        layout.setAlignment(FlowLayout.RIGHT);
170
                else
171
                        layout.setAlignment(FlowLayout.LEFT);
172

    
173
                this.setLayout(layout);
174
        }
175

    
176
        public void setComponentBorder(boolean br){
177
                if(br)
178
                        this.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
179
                if(!br)
180
                        this.setBorder(javax.swing.BorderFactory.createEmptyBorder());
181
        }
182

    
183
}