svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.about / org.gvsig.about.impl / src / main / java / org / gvsig / about / swing / JAboutBrowser.java @ 42024
History | View | Annotate | Download (14.3 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.about.swing; |
23 |
|
24 |
import java.awt.Color; |
25 |
import java.awt.Component; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.Graphics; |
28 |
import java.awt.GridLayout; |
29 |
import java.awt.event.ActionEvent; |
30 |
import java.awt.event.ActionListener; |
31 |
import java.awt.event.MouseEvent; |
32 |
import java.awt.event.MouseListener; |
33 |
import java.util.List; |
34 |
|
35 |
import javax.swing.Icon; |
36 |
import javax.swing.ImageIcon; |
37 |
import javax.swing.JLabel; |
38 |
import javax.swing.JPanel; |
39 |
import javax.swing.JScrollPane; |
40 |
import javax.swing.JTree; |
41 |
import javax.swing.UIManager; |
42 |
import javax.swing.tree.DefaultMutableTreeNode; |
43 |
import javax.swing.tree.DefaultTreeModel; |
44 |
import javax.swing.tree.TreeCellRenderer; |
45 |
import javax.swing.tree.TreePath; |
46 |
import javax.swing.tree.TreeSelectionModel; |
47 |
|
48 |
import org.gvsig.about.AboutManager; |
49 |
import org.gvsig.about.AboutParticipant; |
50 |
import org.gvsig.about.AboutProject; |
51 |
import org.gvsig.about.impl.DefaultAboutManager; |
52 |
|
53 |
/**
|
54 |
* @author gvSIG Team
|
55 |
* @version $Id$
|
56 |
*
|
57 |
*/
|
58 |
public class JAboutBrowser extends JPanel { |
59 |
|
60 |
public static final int DEFAULT_ACTION = 1; |
61 |
public static final int SELECTION_ACTION = 2; |
62 |
public static final int DROPDOWN_ACTION = 3; |
63 |
/**
|
64 |
*
|
65 |
*/
|
66 |
private static final long serialVersionUID = 1L; |
67 |
|
68 |
protected DefaultAboutManager manager;
|
69 |
protected DefaultMutableTreeNode rootNode; |
70 |
protected DefaultTreeModel treeModel; |
71 |
protected JTree tree; |
72 |
protected AboutProject root;
|
73 |
protected ActionListener defaultActionlistener = null; |
74 |
protected ActionListener selectionActionlistener = null; |
75 |
protected ActionListener dropDownActionlistener = null; |
76 |
|
77 |
public static class BrowserActionEvent extends ActionEvent { |
78 |
|
79 |
/**
|
80 |
*
|
81 |
*/
|
82 |
private static final long serialVersionUID = -1474410768278633364L; |
83 |
private Object participant; |
84 |
|
85 |
public BrowserActionEvent(Object source, int id, String command, |
86 |
Object participant) {
|
87 |
super(source, id, command);
|
88 |
this.participant = participant;
|
89 |
} |
90 |
|
91 |
public Object getParticipant() { |
92 |
return this.participant; |
93 |
} |
94 |
} |
95 |
|
96 |
public JAboutBrowser(DefaultAboutManager manager, AboutProject root) {
|
97 |
super(new GridLayout(1, 2)); |
98 |
this.manager = manager;
|
99 |
this.root = root;
|
100 |
this.makeUI();
|
101 |
} |
102 |
|
103 |
public AboutManager getManager() {
|
104 |
return this.manager; |
105 |
} |
106 |
|
107 |
public void addDefaultActionListener(ActionListener actionlistener) { |
108 |
this.defaultActionlistener = actionlistener;
|
109 |
} |
110 |
|
111 |
public void addSelectionActionListener(ActionListener actionlistener) { |
112 |
this.selectionActionlistener = actionlistener;
|
113 |
} |
114 |
|
115 |
public void addDropDownActionListener(ActionListener actionlistener) { |
116 |
this.dropDownActionlistener = actionlistener;
|
117 |
} |
118 |
|
119 |
private void makeUI() { |
120 |
// Creamos el nodo raíz
|
121 |
|
122 |
ImageIcon icon =
|
123 |
this.manager.getImageIcon(this.root.getIcon(), "gvSIG.png"); |
124 |
rootNode = |
125 |
new DefaultMutableTreeNode(new NodeData(icon, icon, this.root)); |
126 |
|
127 |
// Lo asignamos al modelo del árbol e iniciamos el árbol con ese
|
128 |
// modelo
|
129 |
treeModel = new DefaultTreeModel(rootNode); |
130 |
tree = new JTree(treeModel); |
131 |
|
132 |
// Cargamos la información inicial del árbol
|
133 |
this.initializeTree();
|
134 |
|
135 |
// Para asignar iconos especiales según el tipo de objeto
|
136 |
TreeCellRenderer renderer = new IconCellRenderer(); |
137 |
tree.setCellRenderer(renderer); |
138 |
|
139 |
// Para marcar que no son editables sus propiedades o nodos
|
140 |
tree.setEditable(false);
|
141 |
this.expandAll();
|
142 |
|
143 |
// Determina que sólo se puede seleccionar una linea
|
144 |
tree.getSelectionModel().setSelectionMode( |
145 |
TreeSelectionModel.SINGLE_TREE_SELECTION);
|
146 |
|
147 |
tree.addMouseListener(new MouseListener() { |
148 |
|
149 |
public void mouseClicked(MouseEvent arg0) { |
150 |
if (arg0.getClickCount() == 1) { |
151 |
JTree tree = (JTree) arg0.getSource(); |
152 |
if (!tree.isSelectionEmpty()) {
|
153 |
throwEventElementSelected(tree.getSelectionPath()); |
154 |
} |
155 |
|
156 |
} |
157 |
} |
158 |
|
159 |
public void mouseEntered(MouseEvent arg0) { |
160 |
} |
161 |
|
162 |
public void mouseExited(MouseEvent arg0) { |
163 |
} |
164 |
|
165 |
public void mousePressed(MouseEvent arg0) { |
166 |
} |
167 |
|
168 |
public void mouseReleased(MouseEvent arg0) { |
169 |
} |
170 |
}); |
171 |
|
172 |
// Indica si el nodo raíz actúa como un nodo más o queda desplegado
|
173 |
// de
|
174 |
// manera fija
|
175 |
tree.setShowsRootHandles(true);
|
176 |
|
177 |
// Añadimos el Scroll
|
178 |
JScrollPane scrollPane = new JScrollPane(tree); |
179 |
add(scrollPane); |
180 |
|
181 |
setPreferredSize(new Dimension(200, 300)); |
182 |
|
183 |
} |
184 |
|
185 |
public Object getSelectedNode() { |
186 |
DefaultMutableTreeNode node;
|
187 |
if (tree.getSelectionPath() != null) { |
188 |
node = |
189 |
(DefaultMutableTreeNode) tree.getSelectionPath()
|
190 |
.getLastPathComponent(); |
191 |
} else {
|
192 |
node = this.rootNode;
|
193 |
} |
194 |
NodeData nodeData = (NodeData) node.getUserObject(); |
195 |
if (nodeData.getParticipant() != null) { |
196 |
return nodeData.getParticipant();
|
197 |
} else
|
198 |
if (nodeData.getProject() != null) { |
199 |
return nodeData.getProject();
|
200 |
} |
201 |
return null; |
202 |
} |
203 |
|
204 |
private void throwEventElementSelected(TreePath path) { |
205 |
DefaultMutableTreeNode node =
|
206 |
(DefaultMutableTreeNode) path.getLastPathComponent();
|
207 |
NodeData nodeData = (NodeData) node.getUserObject(); |
208 |
if (nodeData.getParticipant() != null) { |
209 |
if (this.defaultActionlistener != null) { |
210 |
ActionEvent event =
|
211 |
new BrowserActionEvent(this, DEFAULT_ACTION, "default", |
212 |
nodeData.getParticipant()); |
213 |
this.defaultActionlistener.actionPerformed(event);
|
214 |
} |
215 |
} else
|
216 |
if (nodeData.getProject() != null) { |
217 |
if (this.defaultActionlistener != null) { |
218 |
ActionEvent event =
|
219 |
new BrowserActionEvent(this, DEFAULT_ACTION, "default", |
220 |
nodeData.getProject()); |
221 |
this.defaultActionlistener.actionPerformed(event);
|
222 |
} |
223 |
} |
224 |
} |
225 |
|
226 |
/**
|
227 |
* Función para la inicialización de los datos del árbol.
|
228 |
**/
|
229 |
@SuppressWarnings("rawtypes") |
230 |
private void initializeTree() { |
231 |
|
232 |
AboutParticipant ap = null;
|
233 |
List participants;
|
234 |
ImageIcon icon;
|
235 |
NodeData data; |
236 |
|
237 |
NodeData sponsors = |
238 |
new NodeData(this.manager.getImageIcon("folder.png"), |
239 |
this.manager.getImageIcon("folder-drag-accept.png"), "Sponsors"); |
240 |
DefaultMutableTreeNode sponsorsNode =
|
241 |
this.addObject(this.rootNode, sponsors); |
242 |
participants = this.manager.getSponsors();
|
243 |
for (int i = 0; i < participants.size(); i++) { |
244 |
ap = (AboutParticipant) participants.get(i); |
245 |
icon = |
246 |
(this.manager).getImageIcon(ap.getIcon(),
|
247 |
"applications-internet.png");
|
248 |
data = new NodeData(icon, icon, ap);
|
249 |
this.addObject(sponsorsNode, data);
|
250 |
} |
251 |
|
252 |
NodeData developers = |
253 |
new NodeData(this.manager.getImageIcon("edit.png"), |
254 |
this.manager.getImageIcon("edit.png"), "Developers"); |
255 |
DefaultMutableTreeNode developersNode =
|
256 |
this.addObject(this.rootNode, developers); |
257 |
participants = this.manager.getDevelopers();
|
258 |
for (int i = 0; i < participants.size(); i++) { |
259 |
ap = (AboutParticipant) participants.get(i); |
260 |
icon = |
261 |
this.manager.getImageIcon(ap.getIcon(),
|
262 |
"preferences-system.png");
|
263 |
data = new NodeData(icon, icon, ap);
|
264 |
this.addObject(developersNode, data);
|
265 |
} |
266 |
|
267 |
NodeData translators = |
268 |
new NodeData(this.manager.getImageIcon("system-users.png"), |
269 |
this.manager.getImageIcon("system-users.png"), "Contributors"); |
270 |
DefaultMutableTreeNode translatorsNode =
|
271 |
this.addObject(this.rootNode, translators); |
272 |
participants = this.manager.getTranslators();
|
273 |
|
274 |
for (int i = 0; i < participants.size(); i++) { |
275 |
ap = (AboutParticipant) participants.get(i); |
276 |
icon = this.manager.getImageIcon(ap.getIcon(), "people.png"); |
277 |
data = new NodeData(icon, icon, ap);
|
278 |
this.addObject(translatorsNode, data);
|
279 |
} |
280 |
|
281 |
} |
282 |
|
283 |
public void expandAll() { |
284 |
int row = 0; |
285 |
while (row < this.tree.getRowCount()) { |
286 |
this.tree.expandRow(row);
|
287 |
row++; |
288 |
} |
289 |
} |
290 |
|
291 |
/**
|
292 |
* Función para añadir un hijo al nodo actual.
|
293 |
**/
|
294 |
// private DefaultMutableTreeNode addObject(Object child) {
|
295 |
// DefaultMutableTreeNode parentNode = rootNode;
|
296 |
// return addObject(parentNode, child, true);
|
297 |
// }
|
298 |
|
299 |
private DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, |
300 |
Object child) {
|
301 |
return addObject(parent, child, false); |
302 |
} |
303 |
|
304 |
private DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, |
305 |
Object child, boolean shouldBeVisible) { |
306 |
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child); |
307 |
|
308 |
// Si no especificamos padre, lo colgará del raíz
|
309 |
if (parent == null) { |
310 |
parent = rootNode; |
311 |
} |
312 |
|
313 |
// Insertamos el nuevo nodo en el modelo del árbol
|
314 |
treeModel.insertNodeInto(childNode, parent, parent.getChildCount()); |
315 |
|
316 |
// Para que el usuario pueda ver el nuevo nodo
|
317 |
if (shouldBeVisible) {
|
318 |
tree.scrollPathToVisible(new TreePath(childNode.getPath())); |
319 |
} |
320 |
|
321 |
return childNode;
|
322 |
} |
323 |
|
324 |
/**
|
325 |
* Clase IconData permite asociar hasta dos iconos (para cuando está
|
326 |
* expandido y cuando no) a un objeto del árbol
|
327 |
**/
|
328 |
static private class NodeData { |
329 |
|
330 |
private Icon m_icon; |
331 |
private Icon m_expandedIcon; |
332 |
private Object m_data; |
333 |
|
334 |
public NodeData(Icon icon, Icon expandedIcon, Object data) { |
335 |
m_icon = icon; |
336 |
m_expandedIcon = expandedIcon; |
337 |
m_data = data; |
338 |
} |
339 |
|
340 |
public Icon getIcon() { |
341 |
return m_icon;
|
342 |
} |
343 |
|
344 |
public Icon getExpandedIcon() { |
345 |
return m_expandedIcon != null ? m_expandedIcon : m_icon; |
346 |
} |
347 |
|
348 |
public AboutParticipant getParticipant() {
|
349 |
try {
|
350 |
return (AboutParticipant) m_data;
|
351 |
} catch (Exception e) { |
352 |
return null; |
353 |
} |
354 |
} |
355 |
|
356 |
public AboutProject getProject() {
|
357 |
try {
|
358 |
return (AboutProject) m_data;
|
359 |
} catch (Exception e) { |
360 |
return null; |
361 |
} |
362 |
} |
363 |
|
364 |
@Override
|
365 |
public String toString() { |
366 |
return m_data.toString();
|
367 |
} |
368 |
} |
369 |
|
370 |
private class IconCellRenderer extends JLabel implements TreeCellRenderer { |
371 |
|
372 |
/**
|
373 |
*
|
374 |
*/
|
375 |
private static final long serialVersionUID = 1L; |
376 |
protected Color m_textSelectionColor; |
377 |
protected Color m_textNonSelectionColor; |
378 |
protected Color m_bkSelectionColor; |
379 |
protected Color m_bkNonSelectionColor; |
380 |
protected Color m_borderSelectionColor; |
381 |
|
382 |
protected boolean m_selected; |
383 |
|
384 |
public IconCellRenderer() {
|
385 |
super();
|
386 |
m_textSelectionColor = |
387 |
UIManager.getColor("Tree.selectionForeground"); |
388 |
m_textNonSelectionColor = UIManager.getColor("Tree.textForeground"); |
389 |
m_bkSelectionColor = UIManager.getColor("Tree.selectionBackground"); |
390 |
m_bkNonSelectionColor = UIManager.getColor("Tree.textBackground"); |
391 |
m_borderSelectionColor = |
392 |
UIManager.getColor("Tree.selectionBorderColor"); |
393 |
setOpaque(false);
|
394 |
} |
395 |
|
396 |
public Component getTreeCellRendererComponent(JTree tree, Object value, |
397 |
boolean sel, boolean expanded, boolean leaf, int row, |
398 |
boolean hasFocus) {
|
399 |
|
400 |
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; |
401 |
Object obj = node.getUserObject();
|
402 |
setText(obj.toString()); |
403 |
|
404 |
if (obj instanceof Boolean) { |
405 |
setText("Retrieving data...");
|
406 |
} |
407 |
if (obj instanceof NodeData) { |
408 |
NodeData nodeData = (NodeData) obj; |
409 |
if (expanded) {
|
410 |
setIcon(nodeData.getExpandedIcon()); |
411 |
} else {
|
412 |
setIcon(nodeData.getIcon()); |
413 |
} |
414 |
} else {
|
415 |
setIcon(null);
|
416 |
} |
417 |
|
418 |
setFont(tree.getFont()); |
419 |
setForeground(sel ? m_textSelectionColor : m_textNonSelectionColor); |
420 |
setBackground(sel ? m_bkSelectionColor : m_bkNonSelectionColor); |
421 |
m_selected = sel; |
422 |
return this; |
423 |
} |
424 |
|
425 |
@Override
|
426 |
public void paintComponent(Graphics g) { |
427 |
Color bColor = getBackground();
|
428 |
Icon icon = getIcon();
|
429 |
|
430 |
g.setColor(bColor); |
431 |
int offset = 0; |
432 |
if ((icon != null) && (getText() != null)) { |
433 |
offset = (icon.getIconWidth() + getIconTextGap()); |
434 |
} |
435 |
g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1); |
436 |
|
437 |
if (m_selected) {
|
438 |
g.setColor(m_borderSelectionColor); |
439 |
g.drawRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1); |
440 |
} |
441 |
super.paintComponent(g);
|
442 |
} |
443 |
} |
444 |
|
445 |
} |