gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.services / src / main / java / org / gvsig / tools / dynform / services / dynformset / subform / FormSetButtonBar.java @ 1233
History | View | Annotate | Download (10.1 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.tools.dynform.services.dynformset.subform; |
25 |
|
26 |
import com.sun.java.swing.plaf.motif.MotifBorders; |
27 |
|
28 |
import java.awt.Color; |
29 |
import java.awt.Cursor; |
30 |
import java.awt.Dimension; |
31 |
import java.awt.FlowLayout; |
32 |
import java.awt.event.MouseAdapter; |
33 |
import java.awt.event.MouseEvent; |
34 |
import java.net.URL; |
35 |
import java.util.HashSet; |
36 |
import java.util.Iterator; |
37 |
import java.util.Set; |
38 |
|
39 |
import javax.swing.BorderFactory; |
40 |
import javax.swing.Icon; |
41 |
import javax.swing.ImageIcon; |
42 |
import javax.swing.JComponent; |
43 |
import javax.swing.JLabel; |
44 |
import javax.swing.JPanel; |
45 |
import javax.swing.SwingConstants; |
46 |
import javax.swing.border.BevelBorder; |
47 |
|
48 |
import org.gvsig.tools.ToolsLocator; |
49 |
import org.gvsig.tools.i18n.I18nManager; |
50 |
import org.gvsig.tools.swing.api.Component; |
51 |
import org.slf4j.Logger; |
52 |
import org.slf4j.LoggerFactory; |
53 |
|
54 |
public class FormSetButtonBar implements Component { |
55 |
|
56 |
protected static final Logger logger = LoggerFactory |
57 |
.getLogger(FormSetButtonBar.class); |
58 |
|
59 |
|
60 |
public static final int ActionFirst = 1; |
61 |
public static final int ActionPrevious = 2; |
62 |
public static final int ActionNext = 3; |
63 |
public static final int ActionLast = 4; |
64 |
public static final int ActionSave = 5; |
65 |
public static final int ActionNew = 6; |
66 |
public static final int ActionDelete = 7; |
67 |
public static final int ActionSearch = 8; |
68 |
public static final int ActionClose = 9; |
69 |
|
70 |
private static final int MAX_ACTIONS = 10; |
71 |
|
72 |
public Boolean activatedActions[] = new Boolean[MAX_ACTIONS]; |
73 |
|
74 |
private static final int COUNTER_HEIGHT = 50; |
75 |
private static final int COUNTER_WIDTH = 100; |
76 |
|
77 |
public interface FormSetListener { |
78 |
public boolean doActionFirst(); |
79 |
public boolean doActionPrevious(); |
80 |
public boolean doActionNext(); |
81 |
public boolean doActionLast(); |
82 |
public boolean doActionSave(); |
83 |
public boolean doActionNew(); |
84 |
public boolean doActionDelete(); |
85 |
public boolean doActionSearch(); |
86 |
public boolean doActionClose(); |
87 |
} |
88 |
|
89 |
private JPanel contents = null; |
90 |
private Set listeners = null; |
91 |
private int current = 0; |
92 |
private int numrecords = 0; |
93 |
|
94 |
private boolean readOnly = false; |
95 |
|
96 |
|
97 |
private JLabel records; |
98 |
private JComponent buttons[] = new JLabel[MAX_ACTIONS]; |
99 |
|
100 |
|
101 |
public FormSetButtonBar() {
|
102 |
// Activamos todas las acciones por defecto
|
103 |
for(int i = 0; i<this.activatedActions.length; i++){ |
104 |
this.activatedActions[i]=true; |
105 |
} |
106 |
this.listeners = new HashSet(); |
107 |
} |
108 |
|
109 |
public JComponent asJComponent() { |
110 |
if( this.contents == null ) { |
111 |
this.initComponents();
|
112 |
} |
113 |
return this.contents; |
114 |
} |
115 |
|
116 |
public void addListener(FormSetListener listener) { |
117 |
this.listeners.add(listener);
|
118 |
} |
119 |
|
120 |
public void removeListener(FormSetListener listener) { |
121 |
this.listeners.remove(listener);
|
122 |
} |
123 |
|
124 |
protected void fireEvent(int eventid) { |
125 |
if(this.readOnly){ |
126 |
Iterator it = this.listeners.iterator(); |
127 |
while (it.hasNext()) {
|
128 |
FormSetListener listener = (FormSetListener) it.next(); |
129 |
try {
|
130 |
switch(eventid) {
|
131 |
case ActionFirst:
|
132 |
listener.doActionFirst(); |
133 |
break;
|
134 |
case ActionPrevious:
|
135 |
listener.doActionPrevious(); |
136 |
break;
|
137 |
case ActionNext:
|
138 |
listener.doActionNext(); |
139 |
break;
|
140 |
case ActionLast:
|
141 |
listener.doActionLast(); |
142 |
break;
|
143 |
case ActionSearch:
|
144 |
listener.doActionSearch(); |
145 |
break;
|
146 |
case ActionClose:
|
147 |
listener.doActionClose(); |
148 |
break;
|
149 |
} |
150 |
} catch (Exception ex) { |
151 |
logger.info("Error calling listener " + listener.toString()
|
152 |
+ "(" + listener.getClass().getName() + ") from " |
153 |
+ this.toString() + "(" + this.getClass().getName() |
154 |
+ ").", ex);
|
155 |
} |
156 |
} |
157 |
} else {
|
158 |
Iterator it = this.listeners.iterator(); |
159 |
while (it.hasNext()) {
|
160 |
FormSetListener listener = (FormSetListener) it.next(); |
161 |
try {
|
162 |
switch(eventid) {
|
163 |
case ActionFirst:
|
164 |
listener.doActionFirst(); |
165 |
break;
|
166 |
case ActionPrevious:
|
167 |
listener.doActionPrevious(); |
168 |
break;
|
169 |
case ActionNext:
|
170 |
listener.doActionNext(); |
171 |
break;
|
172 |
case ActionLast:
|
173 |
listener.doActionLast(); |
174 |
break;
|
175 |
case ActionSave:
|
176 |
listener.doActionSave(); |
177 |
break;
|
178 |
case ActionNew:
|
179 |
listener.doActionNew(); |
180 |
break;
|
181 |
case ActionDelete:
|
182 |
listener.doActionDelete(); |
183 |
break;
|
184 |
case ActionSearch:
|
185 |
listener.doActionSearch(); |
186 |
break;
|
187 |
case ActionClose:
|
188 |
listener.doActionClose(); |
189 |
break;
|
190 |
} |
191 |
} catch (Exception ex) { |
192 |
logger.info("Error calling listener " + listener.toString()
|
193 |
+ "(" + listener.getClass().getName() + ") from " |
194 |
+ this.toString() + "(" + this.getClass().getName() |
195 |
+ ").", ex);
|
196 |
} |
197 |
} |
198 |
} |
199 |
} |
200 |
|
201 |
private void updateRecords() { |
202 |
setEnabled(ActionNew, true);
|
203 |
if( numrecords <= 0 ) { |
204 |
this.records.setText( "0 / 0"); |
205 |
setEnabled(ActionFirst, false);
|
206 |
setEnabled(ActionPrevious, false);
|
207 |
setEnabled(ActionNext, false);
|
208 |
setEnabled(ActionLast, false);
|
209 |
return;
|
210 |
} |
211 |
this.records.setText((current+1) + " / " + numrecords); |
212 |
if( current<=0 ) { |
213 |
current = 0;
|
214 |
setEnabled(ActionFirst, false);
|
215 |
setEnabled(ActionPrevious, false);
|
216 |
setEnabled(ActionNext, true);
|
217 |
setEnabled(ActionLast, true);
|
218 |
} |
219 |
else if( current >= numrecords -1) { |
220 |
current = numrecords-1;
|
221 |
setEnabled(ActionNext, false);
|
222 |
setEnabled(ActionLast, false);
|
223 |
setEnabled(ActionFirst, true);
|
224 |
setEnabled(ActionPrevious, true);
|
225 |
}else{
|
226 |
setEnabled(ActionNext, true);
|
227 |
setEnabled(ActionLast, true);
|
228 |
setEnabled(ActionFirst, true);
|
229 |
setEnabled(ActionPrevious, true);
|
230 |
} |
231 |
} |
232 |
|
233 |
private void initComponents() { |
234 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
235 |
this.contents = new JPanel(); |
236 |
//this.contents.setBorder(BorderFactory.createLineBorder(Color.GRAY));
|
237 |
this.contents.setLayout( new FlowLayout(FlowLayout.LEADING, 4,4)); |
238 |
// this.contents.setBorder( BorderFactory.createLineBorder(Color.GRAY, 1));
|
239 |
|
240 |
JComponent firstButton = createButton(i18nManager.getTranslation("first"), "first.png", ActionFirst); |
241 |
this.contents.add(firstButton);
|
242 |
this.contents.add(createButton(i18nManager.getTranslation("back"), "previous.png", ActionPrevious)); |
243 |
|
244 |
this.records = new JLabel(); |
245 |
int height = this.records.getFont().getSize(); |
246 |
if( height <16 ) { |
247 |
height = 16;
|
248 |
} |
249 |
this.records.setPreferredSize(new Dimension(COUNTER_WIDTH, height)); |
250 |
this.records.setHorizontalAlignment( SwingConstants.CENTER ); |
251 |
//this.records.setBorder(BorderFactory.createLineBorder(Color.WHITE));
|
252 |
this.contents.add(this.records); |
253 |
|
254 |
this.contents.add(createButton(i18nManager.getTranslation("next"), "next.png", ActionNext)); |
255 |
this.contents.add(createButton(i18nManager.getTranslation("last"), "last.png", ActionLast)); |
256 |
|
257 |
this.contents.add(createButton(i18nManager.getTranslation("guardar"), "save.png", ActionSave)); |
258 |
this.contents.add(createButton(i18nManager.getTranslation("nuevo"), "new.png", ActionNew)); |
259 |
this.contents.add(createButton(i18nManager.getTranslation("borrar"), "delete.png", ActionDelete)); |
260 |
this.contents.add(createButton(i18nManager.getTranslation("search"), "search.png", ActionSearch)); |
261 |
|
262 |
this.contents.add(createButton("Close", "close.png", ActionClose)); |
263 |
this.initButtons();
|
264 |
this.updateRecords();
|
265 |
} |
266 |
|
267 |
private void initButtons() { |
268 |
setActionActive(ActionSave, isActionActive(ActionSave)); |
269 |
setActionActive(ActionNew, isActionActive(ActionNew)); |
270 |
setActionActive(ActionSearch, isActionActive(ActionSearch)); |
271 |
setActionActive(ActionDelete, isActionActive(ActionDelete)); |
272 |
setActionActive(ActionClose, isActionActive(ActionClose)); |
273 |
} |
274 |
|
275 |
private JComponent createButton(final String tip, final String image, final int action) { |
276 |
JLabel jlabel = new JLabel(); |
277 |
jlabel.setToolTipText(tip); |
278 |
URL url = this.getClass().getClassLoader().getResource("org/gvsig/tools/dynform/impl/"+image); |
279 |
Icon icon = new ImageIcon(url); |
280 |
jlabel.setIcon(icon); |
281 |
jlabel.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
282 |
jlabel.addMouseListener(new MouseAdapter() { |
283 |
public void mouseClicked(MouseEvent evt) { |
284 |
int count = evt.getClickCount();
|
285 |
if (count == 1) { |
286 |
fireEvent(action); |
287 |
} |
288 |
} |
289 |
}); |
290 |
this.buttons[action] = jlabel;
|
291 |
return jlabel;
|
292 |
} |
293 |
|
294 |
public void setEnabled(int action, boolean enabled) { |
295 |
JLabel label = (JLabel) this.buttons[action]; |
296 |
if( enabled ) {
|
297 |
label.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
298 |
label.setEnabled(true);
|
299 |
} else {
|
300 |
label.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); |
301 |
label.setEnabled(false);
|
302 |
} |
303 |
} |
304 |
|
305 |
public void setCurrent(int current) { |
306 |
this.current = current;
|
307 |
updateRecords(); |
308 |
} |
309 |
|
310 |
public void setNumrecords(int numrecords) { |
311 |
this.numrecords = numrecords;
|
312 |
updateRecords(); |
313 |
} |
314 |
|
315 |
public void setActionActive(int action, boolean active){ |
316 |
if(action == ActionSave || action == ActionDelete ||
|
317 |
action == ActionNew || action == ActionSearch|| |
318 |
action == ActionClose){ |
319 |
if( this.contents == null ) { |
320 |
this.initComponents();
|
321 |
} |
322 |
this.activatedActions[action] = active;
|
323 |
this.buttons[action].setVisible(active);
|
324 |
} |
325 |
} |
326 |
|
327 |
public boolean isActionActive(int action){ |
328 |
return this.activatedActions[action]; |
329 |
} |
330 |
|
331 |
public void setReadOnly(boolean readOnly) { |
332 |
this.readOnly = readOnly;
|
333 |
for(int i=0; i<buttons.length; i++){ |
334 |
if(isActionActive(i) && this.buttons[i] != null){ |
335 |
this.buttons[i].setEnabled(!readOnly);
|
336 |
} |
337 |
if(!readOnly){
|
338 |
updateRecords(); |
339 |
} |
340 |
} |
341 |
} |
342 |
|
343 |
} |