svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / gui / DocumentContextMenu.java @ 36443
History | View | Annotate | Download (3.61 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.app.project.documents.gui; |
23 |
|
24 |
import java.awt.event.ActionEvent; |
25 |
import java.awt.event.ActionListener; |
26 |
import java.util.ArrayList; |
27 |
import java.util.Arrays; |
28 |
import java.util.List; |
29 |
|
30 |
import javax.swing.JMenuItem; |
31 |
import javax.swing.JPopupMenu; |
32 |
|
33 |
import org.gvsig.app.project.ProjectManager; |
34 |
import org.gvsig.app.project.documents.Document; |
35 |
import org.gvsig.app.project.documents.DocumentAction; |
36 |
import org.gvsig.app.project.documents.DocumentActionGroup; |
37 |
|
38 |
public class DocumentContextMenu extends JPopupMenu { |
39 |
|
40 |
/**
|
41 |
*
|
42 |
*/
|
43 |
private static final long serialVersionUID = -8153469580038240864L; |
44 |
|
45 |
private Document item; |
46 |
private List<Document> seleteds; |
47 |
private List<DocumentAction> actions = null; |
48 |
|
49 |
public DocumentContextMenu(String doctype, Document document, |
50 |
Document[] seletedDocuments) { |
51 |
super();
|
52 |
|
53 |
List<DocumentAction> allActions =
|
54 |
ProjectManager.getInstance().getDocumentActions(doctype); |
55 |
List<DocumentAction> actions = new ArrayList<DocumentAction>(); |
56 |
DocumentActionGroup group = null;
|
57 |
|
58 |
this.item = document;
|
59 |
this.seleteds = Arrays.asList(seletedDocuments); |
60 |
if (allActions != null) { |
61 |
for (DocumentAction action : allActions) {
|
62 |
if (action.isVisible(document, this.seleteds)) { |
63 |
actions.add(action); |
64 |
// Create menu item
|
65 |
MenuItem item = new MenuItem(action.getTitle(), action); |
66 |
|
67 |
item.setEnabled(action |
68 |
.isAvailable(this.item, this.seleteds)); |
69 |
if (!action.getGroup().equals(group)) {
|
70 |
if (group != null) { |
71 |
this.addSeparator();
|
72 |
} |
73 |
group = action.getGroup(); |
74 |
} |
75 |
this.add(item);
|
76 |
|
77 |
} |
78 |
} |
79 |
} |
80 |
this.actions = actions;
|
81 |
} |
82 |
|
83 |
public boolean hasActions() { |
84 |
return actions != null || actions.size() > 0; |
85 |
} |
86 |
|
87 |
private class MenuItem extends JMenuItem implements ActionListener { |
88 |
|
89 |
/**
|
90 |
*
|
91 |
*/
|
92 |
private static final long serialVersionUID = -752624469241001299L; |
93 |
private DocumentAction action;
|
94 |
|
95 |
public MenuItem(String text, DocumentAction action) { |
96 |
super(text);
|
97 |
this.action = action;
|
98 |
String tip = this.action.getDescription(); |
99 |
if (tip != null && tip.length() > 0) { |
100 |
this.setToolTipText(tip);
|
101 |
} |
102 |
this.addActionListener(this); |
103 |
} |
104 |
|
105 |
public void actionPerformed(ActionEvent e) { |
106 |
this.action.execute(DocumentContextMenu.this.item,
|
107 |
DocumentContextMenu.this.seleteds); |
108 |
} |
109 |
} |
110 |
|
111 |
} |