gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / dynobject / set / DefaultJDynObjectSetComponent.java @ 245
History | View | Annotate | Download (9.95 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.tools.swing.impl.dynobject.set; |
23 |
|
24 |
import java.awt.BorderLayout; |
25 |
import java.awt.event.ActionEvent; |
26 |
|
27 |
import javax.swing.AbstractAction; |
28 |
import javax.swing.Action; |
29 |
import javax.swing.Box; |
30 |
import javax.swing.Icon; |
31 |
import javax.swing.JComponent; |
32 |
import javax.swing.JLabel; |
33 |
import javax.swing.JPanel; |
34 |
import javax.swing.JToolBar; |
35 |
|
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
import org.gvsig.tools.dynobject.DynObject; |
40 |
import org.gvsig.tools.dynobject.DynObjectSet; |
41 |
import org.gvsig.tools.exception.BaseException; |
42 |
import org.gvsig.tools.observer.Observable; |
43 |
import org.gvsig.tools.observer.Observer; |
44 |
import org.gvsig.tools.service.Manager; |
45 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
46 |
import org.gvsig.tools.swing.api.dynobject.DynObjectSwingManager; |
47 |
import org.gvsig.tools.swing.api.dynobject.JDynObjectComponent; |
48 |
import org.gvsig.tools.swing.api.dynobject.set.JDynObjectSetComponent; |
49 |
|
50 |
/**
|
51 |
* Default {@link JDynObjectSetComponent} implementation using a
|
52 |
* {@link DynObjectSetModel}.
|
53 |
*
|
54 |
* @author gvSIG Team
|
55 |
* @version $Id$
|
56 |
*/
|
57 |
public class DefaultJDynObjectSetComponent extends JPanel implements |
58 |
JDynObjectSetComponent, Observer {
|
59 |
|
60 |
private static final long serialVersionUID = -741238766386344850L; |
61 |
|
62 |
private static final Logger LOG = LoggerFactory |
63 |
.getLogger(DefaultJDynObjectSetComponent.class); |
64 |
|
65 |
private final DynObjectSetModel model; |
66 |
private final DynObjectSwingManager manager; |
67 |
|
68 |
private AbstractEnableAction firstAction;
|
69 |
private AbstractEnableAction previousAction;
|
70 |
|
71 |
private JLabel pageCountLabel; |
72 |
|
73 |
private AbstractEnableAction nextAction;
|
74 |
|
75 |
private AbstractEnableAction lastAction;
|
76 |
|
77 |
private JDynObjectComponent component;
|
78 |
|
79 |
/**
|
80 |
* Creates a new {@link DefaultJDynObjectSetComponent}.
|
81 |
*
|
82 |
* @param set
|
83 |
* to render
|
84 |
* @param manager
|
85 |
* which manages this component
|
86 |
* @throws BaseException
|
87 |
* if there is an error creating the component
|
88 |
*/
|
89 |
public DefaultJDynObjectSetComponent(DynObjectSet set,
|
90 |
DynObjectSwingManager manager) throws BaseException {
|
91 |
this(set, manager, true); |
92 |
} |
93 |
|
94 |
/**
|
95 |
* Creates a new {@link DefaultJDynObjectSetComponent}.
|
96 |
*
|
97 |
* @param set
|
98 |
* to render
|
99 |
* @param manager
|
100 |
* which manages this component
|
101 |
* @param isDoubleBuffered
|
102 |
* a boolean, true for double-buffering, which
|
103 |
* uses additional memory space to achieve fast, flicker-free
|
104 |
* updates
|
105 |
* @throws BaseException
|
106 |
* if there is an error creating the component
|
107 |
*/
|
108 |
public DefaultJDynObjectSetComponent(DynObjectSet set,
|
109 |
DynObjectSwingManager manager, boolean isDoubleBuffered)
|
110 |
throws BaseException {
|
111 |
super(isDoubleBuffered);
|
112 |
this.model = new DynObjectSetModel(set); |
113 |
model.addObserver(this);
|
114 |
this.manager = manager;
|
115 |
initializeGUI(); |
116 |
} |
117 |
|
118 |
/**
|
119 |
* Initializes the GUI.
|
120 |
*/
|
121 |
private void initializeGUI() { |
122 |
setLayout(new BorderLayout()); |
123 |
addDynObjectViewer(); |
124 |
addDynObjectPager(); |
125 |
} |
126 |
|
127 |
private void addDynObjectPager() { |
128 |
JToolBar toolBar = new JToolBar(); |
129 |
toolBar.setRollover(true);
|
130 |
toolBar.setFloatable(false);
|
131 |
|
132 |
// Add horizontal glues to center the buttons
|
133 |
toolBar.add(Box.createHorizontalGlue());
|
134 |
toolBar.add(getFirstAction()); |
135 |
toolBar.add(getPreviousAction()); |
136 |
toolBar.addSeparator(); |
137 |
toolBar.add(getPageCount()); |
138 |
toolBar.addSeparator(); |
139 |
toolBar.add(getNextAction()); |
140 |
toolBar.add(getLastAction()); |
141 |
toolBar.add(Box.createHorizontalGlue());
|
142 |
|
143 |
add(toolBar, BorderLayout.SOUTH);
|
144 |
} |
145 |
|
146 |
@SuppressWarnings("serial") |
147 |
private Action getFirstAction() { |
148 |
if (firstAction == null) { |
149 |
// TODO: replace the button text with an Icon
|
150 |
firstAction = new AbstractEnableAction("<<") { |
151 |
|
152 |
public void actionPerformed(ActionEvent e) { |
153 |
model.first(); |
154 |
} |
155 |
|
156 |
public boolean isEnabled() { |
157 |
return model.hasPrevious();
|
158 |
}; |
159 |
}; |
160 |
firstAction.putValue(Action.SHORT_DESCRIPTION, "Go to first value"); |
161 |
firstAction.checkEnabled(); |
162 |
} |
163 |
return firstAction;
|
164 |
} |
165 |
|
166 |
@SuppressWarnings("serial") |
167 |
private Action getPreviousAction() { |
168 |
if (previousAction == null) { |
169 |
// TODO: replace the button text with an Icon
|
170 |
previousAction = new AbstractEnableAction("<") { |
171 |
|
172 |
public void actionPerformed(ActionEvent e) { |
173 |
model.previous(); |
174 |
} |
175 |
|
176 |
public boolean isEnabled() { |
177 |
return model.hasPrevious();
|
178 |
}; |
179 |
}; |
180 |
previousAction.checkEnabled(); |
181 |
previousAction.putValue(Action.SHORT_DESCRIPTION,
|
182 |
"Go to previous value");
|
183 |
} |
184 |
return previousAction;
|
185 |
} |
186 |
|
187 |
private JLabel getPageCount() { |
188 |
if (pageCountLabel == null) { |
189 |
pageCountLabel = new JLabel(); |
190 |
updatePageCountLabel(); |
191 |
} |
192 |
return pageCountLabel;
|
193 |
} |
194 |
|
195 |
/**
|
196 |
*
|
197 |
*/
|
198 |
private void updatePageCountLabel() { |
199 |
StringBuffer buffer = new StringBuffer(); |
200 |
buffer.append(model.getCurrentPosition() + 1).append('/') |
201 |
.append(model.getSize()); |
202 |
pageCountLabel.setText(buffer.toString()); |
203 |
} |
204 |
|
205 |
@SuppressWarnings("serial") |
206 |
private Action getNextAction() { |
207 |
if (nextAction == null) { |
208 |
// TODO: replace the button text with an Icon
|
209 |
nextAction = new AbstractEnableAction(">") { |
210 |
|
211 |
public void actionPerformed(ActionEvent e) { |
212 |
model.next(); |
213 |
} |
214 |
|
215 |
public boolean isEnabled() { |
216 |
return model.hasNext();
|
217 |
}; |
218 |
}; |
219 |
nextAction.checkEnabled(); |
220 |
nextAction.putValue(Action.SHORT_DESCRIPTION,
|
221 |
"Go to following value");
|
222 |
} |
223 |
return nextAction;
|
224 |
} |
225 |
|
226 |
@SuppressWarnings("serial") |
227 |
private Action getLastAction() { |
228 |
if (lastAction == null) { |
229 |
// TODO: replace the button text with an Icon
|
230 |
lastAction = new AbstractEnableAction(">>") { |
231 |
|
232 |
public void actionPerformed(ActionEvent e) { |
233 |
model.last(); |
234 |
} |
235 |
|
236 |
public boolean isEnabled() { |
237 |
return model.hasNext();
|
238 |
}; |
239 |
|
240 |
}; |
241 |
lastAction.checkEnabled(); |
242 |
lastAction.putValue(Action.SHORT_DESCRIPTION, "Go to last value"); |
243 |
} |
244 |
return lastAction;
|
245 |
} |
246 |
|
247 |
private void addDynObjectViewer() { |
248 |
try {
|
249 |
component = |
250 |
ToolsSwingLocator.getDynObjectSwingManager() |
251 |
.createJDynObjectComponent(model.getCurrentDynObject()); |
252 |
|
253 |
add(component.asJComponent(), BorderLayout.CENTER);
|
254 |
} catch (BaseException e) {
|
255 |
LOG.error( |
256 |
"Error creating the component to view the current DynObject in"
|
257 |
+ " position: " + model.getCurrentPosition(), e);
|
258 |
} |
259 |
|
260 |
} |
261 |
|
262 |
public Manager getManager() {
|
263 |
return manager;
|
264 |
} |
265 |
|
266 |
public JComponent asJComponent() { |
267 |
return this; |
268 |
} |
269 |
|
270 |
public DynObjectSet getDynObjectSet() {
|
271 |
return model.getDynObjectSet();
|
272 |
} |
273 |
|
274 |
public void update(Observable observable, Object notification) { |
275 |
// When the model changes the current DynObject, update the contained
|
276 |
// components status
|
277 |
updateDynObjectPager(); |
278 |
updateDynObjectViewer(); |
279 |
} |
280 |
|
281 |
/**
|
282 |
* Updates the status of the {@link DynObject} viewer when the model changes
|
283 |
* the current DynObject.
|
284 |
*/
|
285 |
private void updateDynObjectViewer() { |
286 |
remove(component.asJComponent()); |
287 |
addDynObjectViewer(); |
288 |
} |
289 |
|
290 |
/**
|
291 |
* Updates the status of the tool bar when the model changes the
|
292 |
* current DynObject.
|
293 |
*/
|
294 |
private void updateDynObjectPager() { |
295 |
firstAction.checkEnabled(); |
296 |
previousAction.checkEnabled(); |
297 |
nextAction.checkEnabled(); |
298 |
lastAction.checkEnabled(); |
299 |
|
300 |
StringBuffer buffer = new StringBuffer(); |
301 |
buffer.append(model.getCurrentPosition() + 1).append('/') |
302 |
.append(model.getSize()); |
303 |
pageCountLabel.setText(buffer.toString()); |
304 |
} |
305 |
|
306 |
/**
|
307 |
* Utility Action extension which adds a new checkEnabled() method to
|
308 |
* set enabled or not depending on the isEnabled() method implementation.
|
309 |
* That will only work as expected if the isEnabled() method implementation
|
310 |
* is changed.
|
311 |
*
|
312 |
* @author gvSIG Team
|
313 |
* @version $Id$
|
314 |
*/
|
315 |
@SuppressWarnings("serial") |
316 |
private static abstract class AbstractEnableAction extends AbstractAction { |
317 |
|
318 |
public AbstractEnableAction(Icon icon) { |
319 |
super(null, icon); |
320 |
} |
321 |
|
322 |
public AbstractEnableAction(String name) { |
323 |
super(name);
|
324 |
} |
325 |
|
326 |
public void checkEnabled() { |
327 |
setEnabled(isEnabled()); |
328 |
} |
329 |
|
330 |
} |
331 |
|
332 |
} |