svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / buttonbar / ButtonBarContainer.java @ 40561
History | View | Annotate | Download (4.7 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.buttonbar; |
25 |
|
26 |
import java.awt.FlowLayout; |
27 |
import java.util.ArrayList; |
28 |
|
29 |
import javax.swing.ImageIcon; |
30 |
import javax.swing.JButton; |
31 |
import javax.swing.JPanel; |
32 |
|
33 |
public class ButtonBarContainer extends JPanel { |
34 |
private static final long serialVersionUID = -2556987128553063939L; |
35 |
|
36 |
private ArrayList<JButton> buttons = new ArrayList<JButton>(); |
37 |
|
38 |
private int wComp = 400; |
39 |
private int hComp = 26; |
40 |
private String pathToImages = "images/"; |
41 |
private boolean disableAllControls = false; |
42 |
private boolean[] buttonsState = null; |
43 |
|
44 |
|
45 |
/**
|
46 |
* This is the default constructor
|
47 |
*/
|
48 |
public ButtonBarContainer() {
|
49 |
super();
|
50 |
initialize(); |
51 |
} |
52 |
|
53 |
/**
|
54 |
* This method initializes this
|
55 |
*
|
56 |
* @return void
|
57 |
*/
|
58 |
private void initialize() { |
59 |
FlowLayout flowLayout = new FlowLayout(); |
60 |
flowLayout.setHgap(0);
|
61 |
flowLayout.setVgap(0);
|
62 |
this.setLayout(flowLayout);
|
63 |
this.setSize(wComp, hComp);
|
64 |
} |
65 |
|
66 |
|
67 |
/**
|
68 |
* A?ade un boton al ArrayList de los botones.
|
69 |
*
|
70 |
* @param iconName: nombre del icono asignado al boton. La imagen tendr?a que
|
71 |
* estar dentro de la carpeta "images/"
|
72 |
* @param tip: tip del boton;
|
73 |
* @param order: orden que ocupar? el boton dentro del control
|
74 |
*/
|
75 |
public void addButton(String iconName, String tip, int order){ |
76 |
JButton bt = new JButton(); |
77 |
|
78 |
bt.setPreferredSize(new java.awt.Dimension(22, 22)); |
79 |
try{
|
80 |
if (iconName != null) |
81 |
bt.setIcon(new ImageIcon(getClass().getResource(pathToImages + iconName))); |
82 |
}catch(NullPointerException exc){ |
83 |
//El icono no existe -> No se a?ade ninguno
|
84 |
} |
85 |
|
86 |
if(tip != null) |
87 |
bt.setToolTipText(tip); |
88 |
|
89 |
buttons.add(order, bt); |
90 |
addList(); |
91 |
|
92 |
} |
93 |
|
94 |
/**
|
95 |
* Elimina el bot?n correspondiente al indice que le pasamos.
|
96 |
* @param index
|
97 |
*/
|
98 |
public void delButton(int index){ |
99 |
buttons.remove(index); |
100 |
this.removeAll();
|
101 |
addList(); |
102 |
} |
103 |
|
104 |
/**
|
105 |
* A?ade en el panel los botones que tenemos en el ArrayList.
|
106 |
*
|
107 |
*/
|
108 |
public void addList(){ |
109 |
for(int i = 0 ; i < buttons.size() ; i++){ |
110 |
this.add((JButton)buttons.get(i)); |
111 |
} |
112 |
} |
113 |
|
114 |
|
115 |
/**
|
116 |
* Esta funci?n deshabilita todos los controles y guarda sus valores
|
117 |
* de habilitado o deshabilitado para que cuando se ejecute restoreControlsValue
|
118 |
* se vuelvan a quedar como estaba
|
119 |
*/
|
120 |
public void disableAllControls(){ |
121 |
if(!disableAllControls){
|
122 |
disableAllControls = true;
|
123 |
|
124 |
buttonsState = new boolean[buttons.size()]; |
125 |
|
126 |
|
127 |
|
128 |
for (int i = 0 ; i < buttons.size() ; i++){ |
129 |
|
130 |
//Salvamos los estados
|
131 |
buttonsState[i] = ((JButton)buttons.get(i)).isEnabled();
|
132 |
//Desactivamos controles
|
133 |
((JButton)buttons.get(i)).setEnabled(false); |
134 |
} |
135 |
} |
136 |
} |
137 |
|
138 |
/**
|
139 |
* Esta funci?n deja los controles como estaban al ejecutar la funci?n
|
140 |
* disableAllControls
|
141 |
*/
|
142 |
public void restoreControlsValue(){ |
143 |
if(disableAllControls){
|
144 |
disableAllControls = false;
|
145 |
|
146 |
for(int i = 0 ; i < buttons.size() ; i++){ |
147 |
((JButton)buttons.get(i)).setEnabled(buttonsState[i]);
|
148 |
} |
149 |
} |
150 |
} |
151 |
|
152 |
/**
|
153 |
* M?todo para acceder a los botones del control;
|
154 |
* @param index
|
155 |
* @return
|
156 |
*/
|
157 |
public JButton getButton(int index){ |
158 |
return (JButton)buttons.get(index); |
159 |
} |
160 |
|
161 |
/**
|
162 |
* M?todo para establecer la posici?n de los botones dentro del control.
|
163 |
* @param align: "left" o "right"
|
164 |
*/
|
165 |
public void setButtonAlignment(String align){ |
166 |
FlowLayout layout = new FlowLayout(); |
167 |
layout.setHgap(2);
|
168 |
layout.setVgap(0);
|
169 |
|
170 |
if (align.equals("right")) |
171 |
layout.setAlignment(FlowLayout.RIGHT);
|
172 |
else
|
173 |
layout.setAlignment(FlowLayout.LEFT);
|
174 |
|
175 |
this.setLayout(layout);
|
176 |
} |
177 |
|
178 |
public void setComponentBorder(boolean br){ |
179 |
if(br)
|
180 |
this.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); |
181 |
if(!br)
|
182 |
this.setBorder(javax.swing.BorderFactory.createEmptyBorder());
|
183 |
} |
184 |
|
185 |
} |