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 / base / FormSetButtonBar.java @ 2039
History | View | Annotate | Download (21.1 KB)
1 | 954 | jbadia | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | 1362 | jjdelcerro | * This program is free software; you can redistribute it and/or modify it under
|
7 | * the terms of the GNU General Public License as published by the Free Software
|
||
8 | * Foundation; either version 3 of the License, or (at your option) any later
|
||
9 | * version.
|
||
10 | 954 | jbadia | *
|
11 | 1362 | jjdelcerro | * This program is distributed in the hope that it will be useful, but WITHOUT
|
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||
13 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||
14 | * details.
|
||
15 | 954 | jbadia | *
|
16 | 1362 | jjdelcerro | * You should have received a copy of the GNU General Public License along with
|
17 | * this program; if not, write to the Free Software Foundation, Inc., 51
|
||
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
19 | 954 | jbadia | *
|
20 | 1362 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 954 | jbadia | */
|
23 | 1880 | jjdelcerro | package org.gvsig.tools.dynform.services.dynformset.base; |
24 | 919 | jjdelcerro | |
25 | import java.awt.Cursor; |
||
26 | import java.awt.Dimension; |
||
27 | import java.awt.FlowLayout; |
||
28 | 1362 | jjdelcerro | import java.awt.event.ActionEvent; |
29 | import java.awt.event.ActionListener; |
||
30 | 1880 | jjdelcerro | import java.awt.event.KeyEvent; |
31 | 919 | jjdelcerro | import java.net.URL; |
32 | 1286 | jjdelcerro | import java.util.ArrayList; |
33 | 1862 | jjdelcerro | import java.util.HashMap; |
34 | 919 | jjdelcerro | import java.util.HashSet; |
35 | import java.util.Iterator; |
||
36 | 1286 | jjdelcerro | import java.util.List; |
37 | 1862 | jjdelcerro | import java.util.Map; |
38 | import java.util.Objects; |
||
39 | 919 | jjdelcerro | import java.util.Set; |
40 | 1286 | jjdelcerro | import javax.swing.Action; |
41 | import javax.swing.BorderFactory; |
||
42 | 1233 | jbadia | |
43 | 919 | jjdelcerro | import javax.swing.Icon; |
44 | import javax.swing.ImageIcon; |
||
45 | 1286 | jjdelcerro | import javax.swing.JButton; |
46 | 919 | jjdelcerro | import javax.swing.JComponent; |
47 | 1362 | jjdelcerro | import javax.swing.JOptionPane; |
48 | 919 | jjdelcerro | import javax.swing.JPanel; |
49 | import javax.swing.SwingConstants; |
||
50 | 1362 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
51 | 919 | jjdelcerro | |
52 | 1233 | jbadia | import org.gvsig.tools.ToolsLocator; |
53 | 2023 | jjdelcerro | import static org.gvsig.tools.dynform.JDynFormSet.ACTION_CANCEL_NEW; |
54 | import static org.gvsig.tools.dynform.JDynFormSet.ACTION_CLOSE; |
||
55 | import static org.gvsig.tools.dynform.JDynFormSet.ACTION_DELETE; |
||
56 | import static org.gvsig.tools.dynform.JDynFormSet.ACTION_FIRST; |
||
57 | import static org.gvsig.tools.dynform.JDynFormSet.ACTION_NEW; |
||
58 | import static org.gvsig.tools.dynform.JDynFormSet.ACTION_NEXT; |
||
59 | import static org.gvsig.tools.dynform.JDynFormSet.ACTION_PREVIOUS; |
||
60 | 2039 | jjdelcerro | import static org.gvsig.tools.dynform.JDynFormSet.ACTION_LAST; |
61 | 2023 | jjdelcerro | import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SAVE; |
62 | import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SEARCH; |
||
63 | import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SET_CURRENT_RECORD; |
||
64 | 1233 | jbadia | import org.gvsig.tools.i18n.I18nManager; |
65 | 919 | jjdelcerro | import org.gvsig.tools.swing.api.Component; |
66 | import org.slf4j.Logger; |
||
67 | import org.slf4j.LoggerFactory; |
||
68 | |||
69 | public class FormSetButtonBar implements Component { |
||
70 | |||
71 | 2039 | jjdelcerro | protected static final Logger LOGGER = LoggerFactory |
72 | 1362 | jjdelcerro | .getLogger(FormSetButtonBar.class); |
73 | 919 | jjdelcerro | |
74 | 2039 | jjdelcerro | public static final int ID_ACTION_FIRST = 1; |
75 | public static final int ID_ACTION_PREVIOUS = 2; |
||
76 | public static final int ID_ACTION_NEXT = 3; |
||
77 | public static final int ID_ACTION_LAST = 4; |
||
78 | public static final int ID_ACTION_SAVE = 5; |
||
79 | public static final int ID_ACTION_NEW = 6; |
||
80 | public static final int ID_ACTION_CANCEL_NEW = 7; |
||
81 | public static final int ID_ACTION_DELETE = 8; |
||
82 | public static final int ID_ACTION_SEARCH = 9; |
||
83 | public static final int ID_ACTION_CLOSE = 10; |
||
84 | public static final int ID_ACTION_SET_CURRENT_RECORD = 11; |
||
85 | 1140 | jbadia | |
86 | 1405 | jjdelcerro | private static final int MAX_ACTIONS = 11; |
87 | 1140 | jbadia | |
88 | 1362 | jjdelcerro | private static final int COUNTER_HEIGHT = 50; |
89 | private static final int COUNTER_WIDTH = 100; |
||
90 | 1140 | jbadia | |
91 | 1362 | jjdelcerro | public interface FormSetListener { |
92 | 1140 | jbadia | |
93 | 1362 | jjdelcerro | public boolean doActionFirst(); |
94 | 1140 | jbadia | |
95 | 1362 | jjdelcerro | public boolean doActionPrevious(); |
96 | 919 | jjdelcerro | |
97 | 1362 | jjdelcerro | public boolean doActionNext(); |
98 | 1140 | jbadia | |
99 | 1362 | jjdelcerro | public boolean doActionLast(); |
100 | 919 | jjdelcerro | |
101 | 1362 | jjdelcerro | public boolean doActionSave(); |
102 | 919 | jjdelcerro | |
103 | 1362 | jjdelcerro | public boolean doActionNew(); |
104 | 1140 | jbadia | |
105 | 1405 | jjdelcerro | public boolean doActionCancelNew(); |
106 | |||
107 | 1362 | jjdelcerro | public boolean doActionDelete(); |
108 | 919 | jjdelcerro | |
109 | 1362 | jjdelcerro | public boolean doActionSearch(); |
110 | 919 | jjdelcerro | |
111 | 1362 | jjdelcerro | public boolean doActionClose(); |
112 | 1140 | jbadia | |
113 | 1362 | jjdelcerro | public void doSetCurrentRecord(int current); |
114 | } |
||
115 | 1140 | jbadia | |
116 | 1362 | jjdelcerro | private JPanel contents = null; |
117 | private Set listeners = null; |
||
118 | private int current = 0; |
||
119 | 1405 | jjdelcerro | private String currentLabel = null; |
120 | 1362 | jjdelcerro | private int numrecords = 0; |
121 | 1140 | jbadia | |
122 | 1362 | jjdelcerro | private JPanel othersButtons; |
123 | private final List<Action> otherActions; |
||
124 | 2039 | jjdelcerro | private final Map<String, JButton> otherButtons; |
125 | |||
126 | private final Map<String,Integer> actionNameToId; |
||
127 | 1140 | jbadia | |
128 | 1362 | jjdelcerro | private boolean readOnly = false; |
129 | 1286 | jjdelcerro | |
130 | 1362 | jjdelcerro | private JButton records; |
131 | private final JButton buttons[] = new JButton[MAX_ACTIONS]; |
||
132 | 2039 | jjdelcerro | private final Boolean activatedActions[] = new Boolean[MAX_ACTIONS]; |
133 | 1140 | jbadia | |
134 | 2039 | jjdelcerro | |
135 | 1362 | jjdelcerro | public FormSetButtonBar() {
|
136 | // Activamos todas las acciones por defecto
|
||
137 | for (int i = 0; i < this.activatedActions.length; i++) { |
||
138 | this.activatedActions[i] = true; |
||
139 | } |
||
140 | 2039 | jjdelcerro | this.actionNameToId = new HashMap(); |
141 | this.actionNameToId.put(ACTION_FIRST, ID_ACTION_FIRST);
|
||
142 | this.actionNameToId.put(ACTION_PREVIOUS, ID_ACTION_PREVIOUS);
|
||
143 | this.actionNameToId.put(ACTION_NEXT, ID_ACTION_NEXT);
|
||
144 | this.actionNameToId.put(ACTION_LAST, ID_ACTION_LAST);
|
||
145 | this.actionNameToId.put(ACTION_SAVE, ID_ACTION_SAVE);
|
||
146 | this.actionNameToId.put(ACTION_NEW, ID_ACTION_NEW);
|
||
147 | this.actionNameToId.put(ACTION_CANCEL_NEW, ID_ACTION_CANCEL_NEW);
|
||
148 | this.actionNameToId.put(ACTION_DELETE, ID_ACTION_DELETE);
|
||
149 | this.actionNameToId.put(ACTION_SEARCH, ID_ACTION_SEARCH);
|
||
150 | this.actionNameToId.put(ACTION_CLOSE, ID_ACTION_CLOSE);
|
||
151 | this.actionNameToId.put(ACTION_SET_CURRENT_RECORD, ID_ACTION_SET_CURRENT_RECORD);
|
||
152 | |||
153 | 1362 | jjdelcerro | this.listeners = new HashSet(); |
154 | this.otherActions = new ArrayList<>(); |
||
155 | 1862 | jjdelcerro | this.otherButtons = new HashMap<>(); |
156 | 1362 | jjdelcerro | } |
157 | |||
158 | @Override
|
||
159 | public JComponent asJComponent() { |
||
160 | if (this.contents == null) { |
||
161 | this.initComponents();
|
||
162 | } |
||
163 | return this.contents; |
||
164 | } |
||
165 | |||
166 | public void addListener(FormSetListener listener) { |
||
167 | this.listeners.add(listener);
|
||
168 | } |
||
169 | |||
170 | public void removeListener(FormSetListener listener) { |
||
171 | this.listeners.remove(listener);
|
||
172 | } |
||
173 | |||
174 | 2039 | jjdelcerro | public void fireEvent(String action, Object value) { |
175 | |||
176 | Integer actionid = this.actionNameToId.get(action); |
||
177 | if( actionid==null ) { |
||
178 | JButton button = this.otherButtons.get(action); |
||
179 | if( button != null && button.getAction()!=null ) { |
||
180 | button.getAction().actionPerformed(new ActionEvent(value, 1, action)); |
||
181 | 1362 | jjdelcerro | } |
182 | 2039 | jjdelcerro | |
183 | 1362 | jjdelcerro | } else {
|
184 | 2039 | jjdelcerro | if (this.readOnly) { |
185 | Iterator it = this.listeners.iterator(); |
||
186 | while (it.hasNext()) {
|
||
187 | FormSetListener listener = (FormSetListener) it.next(); |
||
188 | try {
|
||
189 | switch (actionid) {
|
||
190 | case ID_ACTION_FIRST:
|
||
191 | listener.doActionFirst(); |
||
192 | break;
|
||
193 | case ID_ACTION_PREVIOUS:
|
||
194 | listener.doActionPrevious(); |
||
195 | break;
|
||
196 | case ID_ACTION_NEXT:
|
||
197 | listener.doActionNext(); |
||
198 | break;
|
||
199 | case ID_ACTION_LAST:
|
||
200 | listener.doActionLast(); |
||
201 | break;
|
||
202 | case ID_ACTION_SEARCH:
|
||
203 | listener.doActionSearch(); |
||
204 | break;
|
||
205 | case ID_ACTION_CLOSE:
|
||
206 | listener.doActionClose(); |
||
207 | break;
|
||
208 | case ID_ACTION_SET_CURRENT_RECORD:
|
||
209 | listener.doSetCurrentRecord(((Integer) value));
|
||
210 | break;
|
||
211 | } |
||
212 | } catch (Exception ex) { |
||
213 | LOGGER.info("Error calling listener " + listener.toString()
|
||
214 | + "(" + listener.getClass().getName() + ") from " |
||
215 | + this.toString() + "(" + this.getClass().getName() |
||
216 | + ").", ex);
|
||
217 | 1362 | jjdelcerro | } |
218 | } |
||
219 | 2039 | jjdelcerro | } else {
|
220 | Iterator it = this.listeners.iterator(); |
||
221 | while (it.hasNext()) {
|
||
222 | FormSetListener listener = (FormSetListener) it.next(); |
||
223 | try {
|
||
224 | switch (actionid) {
|
||
225 | case ID_ACTION_FIRST:
|
||
226 | listener.doActionFirst(); |
||
227 | break;
|
||
228 | case ID_ACTION_PREVIOUS:
|
||
229 | listener.doActionPrevious(); |
||
230 | break;
|
||
231 | case ID_ACTION_NEXT:
|
||
232 | listener.doActionNext(); |
||
233 | break;
|
||
234 | case ID_ACTION_LAST:
|
||
235 | listener.doActionLast(); |
||
236 | break;
|
||
237 | case ID_ACTION_SAVE:
|
||
238 | listener.doActionSave(); |
||
239 | break;
|
||
240 | case ID_ACTION_NEW:
|
||
241 | listener.doActionNew(); |
||
242 | break;
|
||
243 | case ID_ACTION_CANCEL_NEW:
|
||
244 | listener.doActionCancelNew(); |
||
245 | break;
|
||
246 | case ID_ACTION_DELETE:
|
||
247 | listener.doActionDelete(); |
||
248 | break;
|
||
249 | case ID_ACTION_SEARCH:
|
||
250 | listener.doActionSearch(); |
||
251 | break;
|
||
252 | case ID_ACTION_CLOSE:
|
||
253 | listener.doActionClose(); |
||
254 | break;
|
||
255 | case ID_ACTION_SET_CURRENT_RECORD:
|
||
256 | listener.doSetCurrentRecord(((Integer) value));
|
||
257 | break;
|
||
258 | } |
||
259 | } catch (Exception ex) { |
||
260 | LOGGER.info("Error calling listener " + listener.toString()
|
||
261 | + "(" + listener.getClass().getName() + ") from " |
||
262 | + this.toString() + "(" + this.getClass().getName() |
||
263 | + ").", ex);
|
||
264 | } |
||
265 | } |
||
266 | 1362 | jjdelcerro | } |
267 | } |
||
268 | } |
||
269 | 1140 | jbadia | |
270 | 1362 | jjdelcerro | private void updateRecords() { |
271 | 2023 | jjdelcerro | setEnabled(ACTION_NEW, true);
|
272 | 1362 | jjdelcerro | if (numrecords <= 0) { |
273 | this.records.setText("0 / 0"); |
||
274 | 2039 | jjdelcerro | setEnabled(ID_ACTION_FIRST, false);
|
275 | setEnabled(ID_ACTION_PREVIOUS, false);
|
||
276 | setEnabled(ID_ACTION_NEXT, false);
|
||
277 | 2023 | jjdelcerro | setEnabled(ACTION_LAST, false);
|
278 | 1362 | jjdelcerro | return;
|
279 | } |
||
280 | 2039 | jjdelcerro | if (this.currentLabel == null) { |
281 | 1405 | jjdelcerro | this.records.setText((current + 1) + " / " + numrecords); |
282 | } else {
|
||
283 | this.records.setText(this.currentLabel + " / " + numrecords); |
||
284 | } |
||
285 | 1362 | jjdelcerro | if (current <= 0) { |
286 | current = 0;
|
||
287 | 2039 | jjdelcerro | setEnabled(ID_ACTION_FIRST, false);
|
288 | setEnabled(ID_ACTION_PREVIOUS, false);
|
||
289 | setEnabled(ID_ACTION_NEXT, true);
|
||
290 | 2023 | jjdelcerro | setEnabled(ACTION_LAST, true);
|
291 | 1362 | jjdelcerro | } else if (current >= numrecords - 1) { |
292 | current = numrecords - 1;
|
||
293 | 2039 | jjdelcerro | setEnabled(ID_ACTION_NEXT, false);
|
294 | 2023 | jjdelcerro | setEnabled(ACTION_LAST, false);
|
295 | 2039 | jjdelcerro | setEnabled(ID_ACTION_FIRST, true);
|
296 | setEnabled(ID_ACTION_PREVIOUS, true);
|
||
297 | 1362 | jjdelcerro | } else {
|
298 | 2039 | jjdelcerro | setEnabled(ID_ACTION_NEXT, true);
|
299 | 2023 | jjdelcerro | setEnabled(ACTION_LAST, true);
|
300 | 2039 | jjdelcerro | setEnabled(ID_ACTION_FIRST, true);
|
301 | setEnabled(ID_ACTION_PREVIOUS, true);
|
||
302 | 1362 | jjdelcerro | } |
303 | } |
||
304 | 1140 | jbadia | |
305 | 1362 | jjdelcerro | private void initComponents() { |
306 | this.contents = new JPanel(); |
||
307 | this.contents.setLayout(new FlowLayout(FlowLayout.LEADING, 4, 4)); |
||
308 | |||
309 | this.contents.add(getStandardButtonBar());
|
||
310 | |||
311 | this.othersButtons = new JPanel(); |
||
312 | this.othersButtons.setLayout(new FlowLayout(FlowLayout.LEADING, 4, 4)); |
||
313 | this.contents.add(this.othersButtons); |
||
314 | |||
315 | for (Action action : this.otherActions) { |
||
316 | this.othersButtons.add(new JButton(action)); |
||
317 | 1286 | jjdelcerro | } |
318 | 1140 | jbadia | |
319 | 2023 | jjdelcerro | this.contents.add(createButton("Close", "close.png", ACTION_CLOSE, KeyEvent.VK_UNDEFINED)); |
320 | 1362 | jjdelcerro | this.initButtons();
|
321 | this.updateRecords();
|
||
322 | } |
||
323 | 919 | jjdelcerro | |
324 | 1362 | jjdelcerro | private JPanel getStandardButtonBar() { |
325 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
326 | JPanel contents = new JPanel(); |
||
327 | contents.setLayout(new FlowLayout(FlowLayout.LEADING, 4, 4)); |
||
328 | 919 | jjdelcerro | |
329 | 2039 | jjdelcerro | JComponent firstButton = createButton(i18nManager.getTranslation("_first"), "first.png", ACTION_FIRST, KeyEvent.CTRL_MASK + KeyEvent.VK_PAGE_UP); |
330 | 1362 | jjdelcerro | contents.add(firstButton); |
331 | 2023 | jjdelcerro | contents.add(createButton(i18nManager.getTranslation("_back"), "previous.png", ACTION_PREVIOUS, KeyEvent.VK_PAGE_UP)); |
332 | 919 | jjdelcerro | |
333 | 1362 | jjdelcerro | this.records = new JButton(); |
334 | this.records.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
||
335 | this.records.setBorder(BorderFactory.createEmptyBorder()); |
||
336 | this.records.setBorderPainted(false); |
||
337 | this.records.setFocusPainted(false); |
||
338 | this.records.setContentAreaFilled(false); |
||
339 | this.records.addActionListener(new ActionListener() { |
||
340 | 1140 | jbadia | |
341 | 1362 | jjdelcerro | @Override
|
342 | public void actionPerformed(ActionEvent ae) { |
||
343 | doGoRecord(); |
||
344 | } |
||
345 | }); |
||
346 | int height = this.records.getFont().getSize(); |
||
347 | if (height < 16) { |
||
348 | height = 16;
|
||
349 | } |
||
350 | this.records.setPreferredSize(new Dimension(COUNTER_WIDTH, height)); |
||
351 | this.records.setHorizontalAlignment(SwingConstants.CENTER); |
||
352 | contents.add(this.records);
|
||
353 | 1140 | jbadia | |
354 | 2023 | jjdelcerro | contents.add(createButton(i18nManager.getTranslation("_next"), "next.png", ACTION_NEXT, KeyEvent.VK_PAGE_DOWN)); |
355 | 2039 | jjdelcerro | contents.add(createButton(i18nManager.getTranslation("_last"), "last.png", ACTION_LAST, KeyEvent.CTRL_MASK + KeyEvent.VK_PAGE_DOWN)); |
356 | 1140 | jbadia | |
357 | 2039 | jjdelcerro | contents.add(createButton(i18nManager.getTranslation("guardar"), "save.png", ACTION_SAVE, KeyEvent.CTRL_MASK + KeyEvent.VK_S)); |
358 | 2023 | jjdelcerro | contents.add(createButton(i18nManager.getTranslation("nuevo"), "new.png", ACTION_NEW, KeyEvent.VK_F6)); |
359 | contents.add(createButton(i18nManager.getTranslation("cancelar nuevo"), "cancelnew.png", ACTION_CANCEL_NEW, KeyEvent.VK_F12)); |
||
360 | 2039 | jjdelcerro | contents.add(createButton(i18nManager.getTranslation("borrar"), "delete.png", ACTION_DELETE, KeyEvent.SHIFT_MASK + KeyEvent.VK_F2)); |
361 | contents.add(createButton(i18nManager.getTranslation("search"), "search.png", ACTION_SEARCH, KeyEvent.CTRL_MASK + KeyEvent.VK_F)); |
||
362 | 1140 | jbadia | |
363 | 1362 | jjdelcerro | return contents;
|
364 | } |
||
365 | |||
366 | private void doGoRecord() { |
||
367 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
368 | String value = JOptionPane.showInputDialog( |
||
369 | this.records,
|
||
370 | i18nManager.getTranslation("_Enter_the_registration_number_you_want_to_go"),
|
||
371 | i18nManager.getTranslation("_Go_to_record"),
|
||
372 | JOptionPane.QUESTION_MESSAGE
|
||
373 | ); |
||
374 | if (StringUtils.isEmpty(value)) {
|
||
375 | return;
|
||
376 | } |
||
377 | int record;
|
||
378 | try {
|
||
379 | record = Integer.parseInt(value);
|
||
380 | } catch (Exception ex) { |
||
381 | JOptionPane.showMessageDialog(
|
||
382 | this.records,
|
||
383 | i18nManager.getTranslation("_invalid_record_number"),
|
||
384 | i18nManager.getTranslation("_Go_to_record"),
|
||
385 | JOptionPane.WARNING_MESSAGE
|
||
386 | ); |
||
387 | return;
|
||
388 | } |
||
389 | 2023 | jjdelcerro | fireEvent(ACTION_SET_CURRENT_RECORD, record); |
390 | 1362 | jjdelcerro | } |
391 | |||
392 | private void initButtons() { |
||
393 | 2039 | jjdelcerro | setActionActive(ID_ACTION_SAVE, isActionActive(ID_ACTION_SAVE)); |
394 | setActionActive(ID_ACTION_NEW, isActionActive(ID_ACTION_NEW)); |
||
395 | setActionActive(ID_ACTION_SEARCH, isActionActive(ID_ACTION_SEARCH)); |
||
396 | setActionActive(ID_ACTION_DELETE, isActionActive(ID_ACTION_DELETE)); |
||
397 | setActionActive(ID_ACTION_CLOSE, isActionActive(ID_ACTION_CLOSE)); |
||
398 | 1362 | jjdelcerro | } |
399 | |||
400 | 2039 | jjdelcerro | private JComponent createButton(final String tip, final String image, final String action, int mnemonic) { |
401 | int actionid = this.actionNameToId.get(action); |
||
402 | |||
403 | 1362 | jjdelcerro | URL url = this.getClass().getClassLoader().getResource("org/gvsig/tools/dynform/impl/" + image); |
404 | Icon icon = new ImageIcon(url); |
||
405 | JButton button = new JButton(icon); |
||
406 | 2039 | jjdelcerro | if (mnemonic != KeyEvent.VK_UNDEFINED) { |
407 | 1880 | jjdelcerro | button.setMnemonic(mnemonic); |
408 | } |
||
409 | 1362 | jjdelcerro | button.setBorder(BorderFactory.createEmptyBorder());
|
410 | button.setBorderPainted(false);
|
||
411 | button.setFocusPainted(false);
|
||
412 | button.setContentAreaFilled(false);
|
||
413 | button.setToolTipText(tip); |
||
414 | button.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
||
415 | button.addActionListener(new ActionListener() { |
||
416 | |||
417 | @Override
|
||
418 | public void actionPerformed(ActionEvent ae) { |
||
419 | fireEvent(action, null);
|
||
420 | 1286 | jjdelcerro | } |
421 | 1362 | jjdelcerro | }); |
422 | 2039 | jjdelcerro | this.buttons[actionid] = button;
|
423 | 1362 | jjdelcerro | return button;
|
424 | } |
||
425 | |||
426 | public void setEnabled(int action, boolean enabled) { |
||
427 | JButton button = this.buttons[action]; |
||
428 | if (enabled) {
|
||
429 | button.setEnabled(true);
|
||
430 | } else {
|
||
431 | button.setEnabled(false);
|
||
432 | 1286 | jjdelcerro | } |
433 | 1362 | jjdelcerro | } |
434 | 1140 | jbadia | |
435 | 1405 | jjdelcerro | public void setVisible(int action, boolean visible) { |
436 | JButton button = this.buttons[action]; |
||
437 | if (visible) {
|
||
438 | button.setVisible(true);
|
||
439 | } else {
|
||
440 | button.setVisible(false);
|
||
441 | } |
||
442 | } |
||
443 | |||
444 | 1862 | jjdelcerro | public void setVisible(String id, boolean visible) { |
445 | 2039 | jjdelcerro | Integer x = this.actionNameToId.get(id); |
446 | if( x!=null ) { |
||
447 | this.buttons[x].setVisible(visible);
|
||
448 | return;
|
||
449 | } |
||
450 | 1862 | jjdelcerro | JButton button = this.otherButtons.get(id); |
451 | 2039 | jjdelcerro | if (button == null) { |
452 | 1862 | jjdelcerro | return;
|
453 | } |
||
454 | button.setVisible(visible); |
||
455 | } |
||
456 | 2039 | jjdelcerro | |
457 | 1862 | jjdelcerro | public void setEnabled(String id, boolean enabled) { |
458 | 2039 | jjdelcerro | Integer x = this.actionNameToId.get(id); |
459 | if( x!=null ) { // Ojo, si quito el intValue a veces falla |
||
460 | this.buttons[x.intValue()].setEnabled(enabled);
|
||
461 | return;
|
||
462 | } |
||
463 | 1862 | jjdelcerro | JButton button = this.otherButtons.get(id); |
464 | 2039 | jjdelcerro | if (button == null) { |
465 | 1862 | jjdelcerro | return;
|
466 | } |
||
467 | button.setEnabled(enabled); |
||
468 | } |
||
469 | 2039 | jjdelcerro | |
470 | 1862 | jjdelcerro | public boolean isEnabled(String id) { |
471 | 2039 | jjdelcerro | Integer x = this.actionNameToId.get(id); |
472 | if( x!=null ) { |
||
473 | return this.buttons[x].isEnabled(); |
||
474 | } |
||
475 | 1862 | jjdelcerro | JButton button = this.otherButtons.get(id); |
476 | 2039 | jjdelcerro | if (button == null) { |
477 | throw new IllegalArgumentException("Don't exists action '" + id + "'."); |
||
478 | 1862 | jjdelcerro | } |
479 | return button.isEnabled();
|
||
480 | } |
||
481 | 2039 | jjdelcerro | |
482 | 1891 | jjdelcerro | public JComponent getActionButton(String id) { |
483 | 2039 | jjdelcerro | Integer x = this.actionNameToId.get(id); |
484 | if( x!=null ) { |
||
485 | return this.buttons[x]; |
||
486 | } |
||
487 | 1891 | jjdelcerro | JButton button = this.otherButtons.get(id); |
488 | return button;
|
||
489 | } |
||
490 | 2039 | jjdelcerro | |
491 | 1862 | jjdelcerro | public boolean isVisible(String id) { |
492 | 2039 | jjdelcerro | Integer x = this.actionNameToId.get(id); |
493 | if( x!=null ) { |
||
494 | return this.buttons[x].isVisible(); |
||
495 | } |
||
496 | 1862 | jjdelcerro | JButton button = this.otherButtons.get(id); |
497 | 2039 | jjdelcerro | if (button == null) { |
498 | throw new IllegalArgumentException("Don't exists action '" + id + "'."); |
||
499 | 1862 | jjdelcerro | } |
500 | return button.isVisible();
|
||
501 | } |
||
502 | 2039 | jjdelcerro | |
503 | 1362 | jjdelcerro | public void setCurrent(int current) { |
504 | this.current = current;
|
||
505 | updateRecords(); |
||
506 | } |
||
507 | |||
508 | 1405 | jjdelcerro | public void setCurrentLabel(String label) { |
509 | this.currentLabel = label;
|
||
510 | updateRecords(); |
||
511 | } |
||
512 | |||
513 | 1362 | jjdelcerro | public void setNumrecords(int numrecords) { |
514 | this.numrecords = numrecords;
|
||
515 | updateRecords(); |
||
516 | } |
||
517 | |||
518 | public void setActionActive(int action, boolean active) { |
||
519 | 2039 | jjdelcerro | if (action == ID_ACTION_SAVE || action == ID_ACTION_DELETE
|
520 | || action == ID_ACTION_NEW || action == ID_ACTION_SEARCH |
||
521 | || action == ID_ACTION_CLOSE) { |
||
522 | 1362 | jjdelcerro | if (this.contents == null) { |
523 | this.initComponents();
|
||
524 | } |
||
525 | this.activatedActions[action] = active;
|
||
526 | this.buttons[action].setVisible(active);
|
||
527 | } |
||
528 | } |
||
529 | |||
530 | public boolean isActionActive(int action) { |
||
531 | return this.activatedActions[action]; |
||
532 | } |
||
533 | |||
534 | public void setReadOnly(boolean readOnly) { |
||
535 | this.readOnly = readOnly;
|
||
536 | 2039 | jjdelcerro | |
537 | this.setEnabled(ID_ACTION_SAVE, !readOnly);
|
||
538 | 2023 | jjdelcerro | this.setEnabled(ACTION_NEW, !readOnly);
|
539 | 2039 | jjdelcerro | this.setEnabled(ID_ACTION_CANCEL_NEW, !readOnly);
|
540 | 2023 | jjdelcerro | this.setEnabled(ACTION_DELETE, !readOnly);
|
541 | 2039 | jjdelcerro | this.setEnabled(ID_ACTION_SEARCH, true); |
542 | 2023 | jjdelcerro | this.setEnabled(ACTION_CLOSE, true); |
543 | 2039 | jjdelcerro | |
544 | 1862 | jjdelcerro | updateRecords(); |
545 | 1362 | jjdelcerro | } |
546 | |||
547 | public void addAction(Action action) { |
||
548 | this.otherActions.add(action);
|
||
549 | if (this.contents == null) { |
||
550 | return;
|
||
551 | } |
||
552 | JButton button = new JButton(action); |
||
553 | button.setBorder(BorderFactory.createEmptyBorder());
|
||
554 | button.setBorderPainted(false);
|
||
555 | button.setFocusPainted(false);
|
||
556 | button.setContentAreaFilled(false);
|
||
557 | button.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
||
558 | 1862 | jjdelcerro | String id = Objects.toString(action.getValue(Action.ACTION_COMMAND_KEY), null); |
559 | 2039 | jjdelcerro | if (!StringUtils.isEmpty(id)) {
|
560 | 1862 | jjdelcerro | this.otherButtons.put(id, button);
|
561 | } |
||
562 | 1362 | jjdelcerro | this.othersButtons.add(button);
|
563 | } |
||
564 | 919 | jjdelcerro | } |