Revision 42511
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/extension/DefaultFeatureFormDefinitionsProvider.java | ||
---|---|---|
1 |
package org.gvsig.app.extension; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.FileInputStream; |
|
5 |
import java.io.FileNotFoundException; |
|
6 |
import java.io.IOException; |
|
7 |
import java.io.InputStream; |
|
8 |
import java.nio.charset.Charset; |
|
9 |
import java.util.ArrayList; |
|
10 |
import java.util.Collections; |
|
11 |
import java.util.Iterator; |
|
12 |
import java.util.List; |
|
13 |
import java.util.Map; |
|
14 |
import java.util.zip.CRC32; |
|
15 |
import org.apache.commons.io.FileUtils; |
|
16 |
import org.apache.commons.io.IOUtils; |
|
17 |
import org.gvsig.andami.PluginServices; |
|
18 |
import org.gvsig.andami.PluginsLocator; |
|
19 |
import org.gvsig.andami.PluginsManager; |
|
20 |
import org.gvsig.featureform.swing.FeatureFormDefinitionsProvider; |
|
21 |
import org.gvsig.featureform.swing.FeatureFormDefinitionsProvider.Entry; |
|
22 |
import org.gvsig.fmap.dal.exception.DataException; |
|
23 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
24 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
25 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
26 |
import org.gvsig.tools.ToolsLocator; |
|
27 |
import org.gvsig.tools.dynobject.DynClass; |
|
28 |
import org.gvsig.tools.dynobject.DynObjectManager; |
|
29 |
import org.slf4j.Logger; |
|
30 |
import org.slf4j.LoggerFactory; |
|
31 |
|
|
32 |
public class DefaultFeatureFormDefinitionsProvider implements FeatureFormDefinitionsProvider { |
|
33 |
|
|
34 |
private class DefaultEntry implements Entry { |
|
35 |
|
|
36 |
private String key; |
|
37 |
private DynClass dynClass; |
|
38 |
private String xml; |
|
39 |
|
|
40 |
public DefaultEntry(String key, DynClass dynClass, String xml) { |
|
41 |
this.key = key; |
|
42 |
this.dynClass = dynClass; |
|
43 |
this.xml = xml; |
|
44 |
} |
|
45 |
|
|
46 |
public DefaultEntry(String key, DynClass dynClass) { |
|
47 |
this(key, dynClass, null); |
|
48 |
} |
|
49 |
|
|
50 |
public DefaultEntry(FeatureStore store) throws DataException { |
|
51 |
this(null, store.getDefaultFeatureType(), null); |
|
52 |
this.key = getFilename(store); |
|
53 |
} |
|
54 |
|
|
55 |
@Override |
|
56 |
public DynClass getDynClass() { |
|
57 |
return dynClass; |
|
58 |
} |
|
59 |
|
|
60 |
@Override |
|
61 |
public String getKey() { |
|
62 |
return key; |
|
63 |
} |
|
64 |
|
|
65 |
@Override |
|
66 |
public String getXml() { |
|
67 |
if (this.xml == null) { |
|
68 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
|
69 |
this.xml = dynObjectManager.exportSimpleDynClassDefinitions(this.getDynClass()); |
|
70 |
} |
|
71 |
return this.xml; |
|
72 |
} |
|
73 |
|
|
74 |
public boolean setXml(String xml) { |
|
75 |
if (xml == null) { |
|
76 |
this.xml = null; |
|
77 |
return true; |
|
78 |
} |
|
79 |
Map classes; |
|
80 |
DynClass dynClass; |
|
81 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
|
82 |
try { |
|
83 |
InputStream is = IOUtils.toInputStream(xml, Charset.forName("UTF-8")); |
|
84 |
classes = dynObjectManager.importDynClassDefinitions(is, this.getClass().getClassLoader()); |
|
85 |
} catch (Exception ex) { |
|
86 |
logger.warn("Can't parse xml definition.", ex); |
|
87 |
return false; |
|
88 |
} |
|
89 |
if (!classes.containsKey(this.getDynClass().getName())) { |
|
90 |
logger.warn("Can't load definition of '" + this.getDynClass().getName() + "' from xml."); |
|
91 |
return false; |
|
92 |
} |
|
93 |
this.xml = xml; |
|
94 |
return true; |
|
95 |
} |
|
96 |
|
|
97 |
public void setDynClass(DynClass dynClass) { |
|
98 |
this.dynClass = dynClass; |
|
99 |
} |
|
100 |
|
|
101 |
@Override |
|
102 |
public int compareTo(Entry t) { |
|
103 |
return this.getKey().compareTo(t.getKey()); |
|
104 |
} |
|
105 |
|
|
106 |
} |
|
107 |
|
|
108 |
private static final Logger logger = LoggerFactory.getLogger(DefaultFeatureFormDefinitionsProvider.class); |
|
109 |
|
|
110 |
private File definitionsFolder = null; |
|
111 |
|
|
112 |
public File getDefinitionsFolder() throws IOException { |
|
113 |
if (this.definitionsFolder == null) { |
|
114 |
PluginsManager pluginManager = PluginsLocator.getManager(); |
|
115 |
PluginServices plugin = pluginManager.getPlugin(CreateDefaultFormDefinitionExtension.class); |
|
116 |
File homeFolder = plugin.getPluginHomeFolder(); |
|
117 |
File definitionsFolder = new File(homeFolder, "forms"); |
|
118 |
if (!definitionsFolder.exists()) { |
|
119 |
FileUtils.forceMkdir(definitionsFolder); |
|
120 |
} |
|
121 |
this.definitionsFolder = definitionsFolder; |
|
122 |
} |
|
123 |
return this.definitionsFolder; |
|
124 |
} |
|
125 |
|
|
126 |
public long getCRC(FeatureType type) { |
|
127 |
StringBuilder buffer = new StringBuilder(); |
|
128 |
for (int i = 0; i < type.size(); i++) { |
|
129 |
FeatureAttributeDescriptor x = type.getAttributeDescriptor(i); |
|
130 |
buffer.append(x.getName()); |
|
131 |
buffer.append(x.getDataTypeName()); |
|
132 |
buffer.append(x.getSize()); |
|
133 |
} |
|
134 |
CRC32 crc = new CRC32(); |
|
135 |
byte[] data = buffer.toString().getBytes(); |
|
136 |
crc.update(data); |
|
137 |
return crc.getValue(); |
|
138 |
} |
|
139 |
|
|
140 |
public String getFilename(FeatureStore store) throws DataException { |
|
141 |
FeatureType type = store.getDefaultFeatureType(); |
|
142 |
|
|
143 |
return store.getName() + "-" + Long.toHexString(getCRC(type)) + ".xml"; |
|
144 |
} |
|
145 |
|
|
146 |
public Entry get(File definitionFile) { |
|
147 |
String xml; |
|
148 |
Map classes; |
|
149 |
DynClass dynClass; |
|
150 |
|
|
151 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
|
152 |
if (definitionFile == null || !definitionFile.exists()) { |
|
153 |
return null; |
|
154 |
} |
|
155 |
try { |
|
156 |
xml = FileUtils.readFileToString(definitionFile); |
|
157 |
} catch (Exception ex) { |
|
158 |
return null; |
|
159 |
} |
|
160 |
try { |
|
161 |
InputStream is = IOUtils.toInputStream(xml, Charset.forName("UTF-8")); |
|
162 |
classes = dynObjectManager.importDynClassDefinitions(is, this.getClass().getClassLoader()); |
|
163 |
} catch (Exception ex) { |
|
164 |
logger.warn("Can't parse xml definition.", ex); |
|
165 |
return null; |
|
166 |
} |
|
167 |
try { |
|
168 |
dynClass = (DynClass) classes.values().iterator().next(); |
|
169 |
} catch (Exception ex) { |
|
170 |
return null; |
|
171 |
} |
|
172 |
if (dynClass == null) { |
|
173 |
return null; |
|
174 |
} |
|
175 |
|
|
176 |
InputStream is; |
|
177 |
try { |
|
178 |
is = new FileInputStream(definitionFile); |
|
179 |
} catch (FileNotFoundException ex) { |
|
180 |
return null; |
|
181 |
} |
|
182 |
return new DefaultEntry(definitionFile.getName(), dynClass, xml); |
|
183 |
} |
|
184 |
|
|
185 |
@Override |
|
186 |
public Entry get(FeatureStore store) throws DataException { |
|
187 |
FeatureType type = store.getDefaultFeatureType(); |
|
188 |
|
|
189 |
File f; |
|
190 |
try { |
|
191 |
f = new File(getDefinitionsFolder(), getFilename(store)); |
|
192 |
} catch (IOException ex) { |
|
193 |
return new DefaultEntry(store); |
|
194 |
} |
|
195 |
Entry entry = this.get(f); |
|
196 |
return entry; |
|
197 |
} |
|
198 |
|
|
199 |
@Override |
|
200 |
public void put(Entry entry) throws DataException, IOException { |
|
201 |
if (!getDefinitionsFolder().exists()) { |
|
202 |
FileUtils.forceMkdir(getDefinitionsFolder()); |
|
203 |
} |
|
204 |
File f = new File(getDefinitionsFolder(), entry.getKey()); |
|
205 |
FileUtils.write(f, entry.getXml()); |
|
206 |
} |
|
207 |
|
|
208 |
@Override |
|
209 |
public Iterator<Entry> iterator() { |
|
210 |
try { |
|
211 |
List<Entry> definitions = new ArrayList(); |
|
212 |
File folder; |
|
213 |
folder = this.getDefinitionsFolder(); |
|
214 |
for (File f : folder.listFiles()) { |
|
215 |
Entry entry = this.get(f); |
|
216 |
if (entry != null) { |
|
217 |
definitions.add(entry); |
|
218 |
} |
|
219 |
} |
|
220 |
Collections.sort(definitions); |
|
221 |
return definitions.iterator(); |
|
222 |
} catch (IOException ex) { |
|
223 |
logger.warn("Can't retrieve list of forms definitions.", ex); |
|
224 |
return null; |
|
225 |
} |
|
226 |
} |
|
227 |
|
|
228 |
@Override |
|
229 |
public void put(FeatureStore store) throws DataException, IOException { |
|
230 |
DefaultEntry entry = new DefaultEntry(store); |
|
231 |
this.put(entry); |
|
232 |
} |
|
233 |
|
|
234 |
@Override |
|
235 |
public DynClass getDynClass(FeatureStore store) throws DataException { |
|
236 |
Entry entry = this.get(store); |
|
237 |
if (entry == null) { |
|
238 |
return null; |
|
239 |
} |
|
240 |
return entry.getDynClass(); |
|
241 |
} |
|
242 |
|
|
243 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/extension/CreateDefaultFormDefinitionExtension.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.app.extension; |
|
3 |
|
|
4 |
import javax.swing.JOptionPane; |
|
5 |
import org.slf4j.Logger; |
|
6 |
import org.slf4j.LoggerFactory; |
|
7 |
|
|
8 |
import org.gvsig.andami.plugins.Extension; |
|
9 |
import org.gvsig.app.ApplicationLocator; |
|
10 |
import org.gvsig.app.ApplicationManager; |
|
11 |
import org.gvsig.app.project.documents.view.ViewDocument; |
|
12 |
import org.gvsig.app.project.documents.view.ViewManager; |
|
13 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
|
14 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
|
15 |
import org.gvsig.fmap.mapcontext.MapContext; |
|
16 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
|
17 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
|
18 |
|
|
19 |
/** |
|
20 |
* |
|
21 |
* @author jjdelcerro |
|
22 |
*/ |
|
23 |
public class CreateDefaultFormDefinitionExtension extends Extension { |
|
24 |
|
|
25 |
private static final Logger logger = LoggerFactory.getLogger(CreateDefaultFormDefinitionExtension.class); |
|
26 |
|
|
27 |
|
|
28 |
@Override |
|
29 |
public void initialize() { |
|
30 |
DataSwingManager featureFormManager = DALSwingLocator.getSwingManager(); |
|
31 |
featureFormManager.registerFeatureFormDefinitionsProvider(new DefaultFeatureFormDefinitionsProvider()); |
|
32 |
} |
|
33 |
|
|
34 |
@Override |
|
35 |
public void execute(String actionCommand) { |
|
36 |
if( "layer-create-default-form-definition".equalsIgnoreCase(actionCommand) ) { |
|
37 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
38 |
ViewDocument viewDoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
|
39 |
if( viewDoc == null ) { |
|
40 |
return; |
|
41 |
} |
|
42 |
MapContext mapContext = viewDoc.getMapContext(); |
|
43 |
if( !mapContext.hasActiveVectorLayers() ) { |
|
44 |
return; |
|
45 |
} |
|
46 |
DataSwingManager featureFormManager = DALSwingLocator.getSwingManager(); |
|
47 |
FLayer[] layers = mapContext.getLayers().getActives(); |
|
48 |
for (FLayer layer1 : layers) { |
|
49 |
if (layer1 instanceof FLyrVect) { |
|
50 |
FLyrVect layer = (FLyrVect) layer1; |
|
51 |
try { |
|
52 |
featureFormManager.getFeatureFormDefinitionsProvider().put(layer.getFeatureStore()); |
|
53 |
application.messageDialog( |
|
54 |
"Se creado la definicion de formulario asociada a la capa '"+layer.getName()+"'.", |
|
55 |
"Definicion de formulario", |
|
56 |
JOptionPane.INFORMATION_MESSAGE |
|
57 |
); |
|
58 |
} catch (Exception ex) { |
|
59 |
logger.warn("Can't save form definition for layer '"+layer.getName()+"'.",ex); |
|
60 |
application.messageDialog( |
|
61 |
"No se ha podido crear la definicion de formulario asociada a la capa '"+layer.getName()+"'.\n\n(Consulte el registro de errores si desea mas informacion)", |
|
62 |
"Definicion de formulario", |
|
63 |
JOptionPane.INFORMATION_MESSAGE |
|
64 |
); |
|
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
71 |
@Override |
|
72 |
public boolean isEnabled() { |
|
73 |
return true; |
|
74 |
} |
|
75 |
|
|
76 |
@Override |
|
77 |
public boolean isVisible() { |
|
78 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
79 |
ViewDocument viewDoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
|
80 |
if( viewDoc == null ) { |
|
81 |
return false; |
|
82 |
} |
|
83 |
MapContext mapContext = viewDoc.getMapContext(); |
|
84 |
return mapContext.hasActiveVectorLayers(); |
|
85 |
} |
|
86 |
|
|
87 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/resources-plugin/config.xml | ||
---|---|---|
1217 | 1217 |
/> |
1218 | 1218 |
|
1219 | 1219 |
</extension> |
1220 |
|
|
1221 |
<extension |
|
1222 |
class-name="org.gvsig.app.extension.CreateDefaultFormDefinitionExtension" |
|
1223 |
description="" active="true" priority="1"> |
|
1220 | 1224 |
|
1225 |
<action |
|
1226 |
name="layer-create-default-form-definition" |
|
1227 |
label="Create default form definition" |
|
1228 |
tooltip="Create default form definition" |
|
1229 |
position="601400000" |
|
1230 |
action-command="layer-create-default-form-definition" |
|
1231 |
icon="layer-create-default-form-definition" |
|
1232 |
accelerator="" |
|
1233 |
/> |
|
1234 |
|
|
1235 |
<menu |
|
1236 |
name="layer-create-default-form-definition" |
|
1237 |
text="Layer/Create default form (experimental)" |
|
1238 |
/> |
|
1239 |
|
|
1240 |
</extension> |
|
1241 |
|
|
1221 | 1242 |
</extensions> |
1222 | 1243 |
|
1223 | 1244 |
</plugin-config> |
Also available in: Unified diff