gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.util / org.gvsig.tools.util.impl / src / main / java / org / gvsig / propertypage / impl / DefaultPropertiesPageManager.java @ 1746
History | View | Annotate | Download (5.4 KB)
1 | 1746 | jjdelcerro | package org.gvsig.propertypage.impl; |
---|---|---|---|
2 | |||
3 | import java.awt.event.ActionEvent; |
||
4 | import java.util.ArrayList; |
||
5 | import java.util.Collection; |
||
6 | import java.util.Collections; |
||
7 | import java.util.Comparator; |
||
8 | import java.util.HashMap; |
||
9 | import java.util.List; |
||
10 | import java.util.Map; |
||
11 | import org.gvsig.propertypage.PropertiesPage; |
||
12 | import org.gvsig.propertypage.PropertiesPage.SetPageEnabledEvent; |
||
13 | import org.gvsig.propertypage.PropertiesPageFactory; |
||
14 | import org.gvsig.propertypage.PropertiesPageManager; |
||
15 | import org.gvsig.tools.ToolsLocator; |
||
16 | import org.gvsig.tools.identitymanagement.SimpleIdentity; |
||
17 | import org.gvsig.tools.identitymanagement.SimpleIdentityManager; |
||
18 | import org.gvsig.tools.util.Invocable; |
||
19 | import org.slf4j.Logger; |
||
20 | import org.slf4j.LoggerFactory; |
||
21 | |||
22 | public class DefaultPropertiesPageManager implements PropertiesPageManager { |
||
23 | |||
24 | private static class DefaultSetPageEnabledEvent extends ActionEvent { |
||
25 | |||
26 | private Invocable filter;
|
||
27 | private boolean enabled; |
||
28 | |||
29 | public DefaultSetPageEnabledEvent(Object source, Invocable filter, boolean enabled ) { |
||
30 | super(source, SetPageEnabledEvent.ID, SetPageEnabledEvent.ACTION_COMMAND);
|
||
31 | this.filter = filter;
|
||
32 | this.enabled = enabled;
|
||
33 | } |
||
34 | |||
35 | public Invocable getFilter() {
|
||
36 | return filter;
|
||
37 | } |
||
38 | |||
39 | public boolean isEnabled() { |
||
40 | return enabled;
|
||
41 | } |
||
42 | |||
43 | } |
||
44 | |||
45 | public static final String ACCESS_PAGE_AUTHORIZATION = "propertypage-access"; |
||
46 | |||
47 | private static final Logger logger = LoggerFactory.getLogger(DefaultPropertiesPageManager.class); |
||
48 | |||
49 | private final Map<String, Map<String, PropertiesPageFactory>> groups = new HashMap<>(); |
||
50 | |||
51 | public DefaultPropertiesPageManager() {
|
||
52 | |||
53 | } |
||
54 | |||
55 | private String getFactoryName(PropertiesPageFactory factory) { |
||
56 | String factoryName = factory.getClass().getName();
|
||
57 | try {
|
||
58 | factoryName = factory.getName(); |
||
59 | } catch(Throwable ex) { |
||
60 | |||
61 | } |
||
62 | return factoryName;
|
||
63 | } |
||
64 | |||
65 | @Override
|
||
66 | public void registerFactory(PropertiesPageFactory factory) { |
||
67 | logger.info("Register PropertisPageFactory " + factory.getClass().getName() + " , groupID=" + factory.getGroupID() + " (" + factory.toString() + ")."); |
||
68 | Map<String, PropertiesPageFactory> group = this.getFactories(factory.getGroupID()); |
||
69 | group.put(getFactoryName(factory), factory); |
||
70 | } |
||
71 | |||
72 | private Map<String, PropertiesPageFactory> getFactories(String groupID) { |
||
73 | logger.info("get propeties page factories for groupID '" + groupID + "'"); |
||
74 | Map<String,PropertiesPageFactory> group = this.groups.get(groupID); |
||
75 | if (group == null) { |
||
76 | group = new HashMap<>(); |
||
77 | this.groups.put(groupID, group);
|
||
78 | } |
||
79 | return group;
|
||
80 | } |
||
81 | |||
82 | @Override
|
||
83 | public List<PropertiesPage> getPages(String groupID, Object container, Object data) { |
||
84 | logger.info("get propeties page for groupID '" + groupID + "', and object '" + ((data==null)?"null":data.toString()) + "'."); |
||
85 | List<PropertiesPage> pages = new ArrayList<>(); |
||
86 | SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager(); |
||
87 | SimpleIdentity currentUser = identityManager.getCurrentIdentity(); |
||
88 | if (!currentUser.isAuthorized(ACCESS_PAGE_AUTHORIZATION,null,groupID ) ) { |
||
89 | return pages; // Empty list, no access to pages authoriced |
||
90 | } |
||
91 | |||
92 | Collection<PropertiesPageFactory> factories = this.getFactories(groupID).values(); |
||
93 | String authorizationId = ACCESS_PAGE_AUTHORIZATION+"-"+groupID; |
||
94 | for (PropertiesPageFactory factory : factories) {
|
||
95 | String factoryName = getFactoryName(factory);
|
||
96 | try {
|
||
97 | if (factory.isVisible(data)) {
|
||
98 | PropertiesPage page = factory.create(container, data); |
||
99 | if (currentUser.isAuthorized(authorizationId,page, page.getTitle()) ) {
|
||
100 | pages.add(page); |
||
101 | } |
||
102 | } |
||
103 | } catch(Throwable ex) { |
||
104 | logger.warn("Can't get properties page from factory '"+factoryName+"'.",ex); |
||
105 | } |
||
106 | } |
||
107 | Collections.sort(pages, new Comparator<PropertiesPage>() { |
||
108 | @Override
|
||
109 | public int compare(PropertiesPage f1, PropertiesPage f2) { |
||
110 | int n = f2.getPriority() - f1.getPriority();
|
||
111 | if (n != 0) { |
||
112 | return n;
|
||
113 | } |
||
114 | if (f1.getTitle() == null || f2.getTitle() == null) { |
||
115 | // Mas que nada para que no pete, y aunque el orden
|
||
116 | // no sea correcto muestre algo al usuario.
|
||
117 | return 0; |
||
118 | } |
||
119 | return f1.getTitle().compareTo(f2.getTitle());
|
||
120 | } |
||
121 | }); |
||
122 | return pages;
|
||
123 | } |
||
124 | |||
125 | @Override
|
||
126 | public ActionEvent createSetPageEnableEvent(PropertiesPage source, Invocable filter, boolean enabled) { |
||
127 | return new DefaultSetPageEnabledEvent(source, filter, enabled); |
||
128 | } |
||
129 | |||
130 | @Override
|
||
131 | public ActionEvent createSetPageEnableEvent(final PropertiesPage source, final Class filterClass, final boolean enabled) { |
||
132 | return new DefaultSetPageEnabledEvent( |
||
133 | source, |
||
134 | new Invocable() {
|
||
135 | @Override
|
||
136 | public Object call(Object... propertiesPages) { |
||
137 | return filterClass.isInstance(propertiesPages[0]); |
||
138 | } |
||
139 | }, |
||
140 | enabled |
||
141 | ); |
||
142 | } |
||
143 | |||
144 | } |