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