Revision 100
org.gvsig.attributeeditor/trunk/org.gvsig.attributeeditor/org.gvsig.attributeeditor.app/org.gvsig.attributeeditor.app.mainplugin/src/main/java/org/gvsig/app/mainplugin/extension/DefaultFeatureFormDefinitionsProvider.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.app.mainplugin.extension; |
|
3 |
|
|
4 |
import java.io.File; |
|
5 |
import java.io.FileInputStream; |
|
6 |
import java.io.FileNotFoundException; |
|
7 |
import java.io.IOException; |
|
8 |
import java.util.Map; |
|
9 |
import java.util.zip.CRC32; |
|
10 |
import org.apache.commons.io.FileUtils; |
|
11 |
import org.gvsig.andami.PluginServices; |
|
12 |
import org.gvsig.andami.PluginsLocator; |
|
13 |
import org.gvsig.andami.PluginsManager; |
|
14 |
import org.gvsig.featureform.swing.FeatureFormDefinitionsProvider; |
|
15 |
import org.gvsig.fmap.dal.exception.DataException; |
|
16 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
17 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
18 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
19 |
import org.gvsig.tools.ToolsLocator; |
|
20 |
import org.gvsig.tools.dynobject.DynClass; |
|
21 |
import org.gvsig.tools.dynobject.DynObjectManager; |
|
22 |
import org.slf4j.Logger; |
|
23 |
import org.slf4j.LoggerFactory; |
|
24 |
import org.xmlpull.v1.XmlPullParserException; |
|
25 |
|
|
26 |
|
|
27 |
public class DefaultFeatureFormDefinitionsProvider implements FeatureFormDefinitionsProvider { |
|
28 |
|
|
29 |
private static final Logger logger = LoggerFactory.getLogger(DefaultFeatureFormDefinitionsProvider.class); |
|
30 |
|
|
31 |
private File definitionsFolder = null; |
|
32 |
|
|
33 |
private File getDefinitionsFolder() throws IOException { |
|
34 |
if( this.definitionsFolder == null ) { |
|
35 |
PluginsManager pluginManager = PluginsLocator.getManager(); |
|
36 |
PluginServices plugin = pluginManager.getPlugin(AttributeEditorExtension.class); |
|
37 |
File homeFolder = plugin.getPluginHomeFolder(); |
|
38 |
File definitionsFolder = new File(homeFolder,"definitions"); |
|
39 |
if( !definitionsFolder.exists() ) { |
|
40 |
FileUtils.forceMkdir(definitionsFolder); |
|
41 |
} |
|
42 |
this.definitionsFolder = definitionsFolder; |
|
43 |
} |
|
44 |
return this.definitionsFolder; |
|
45 |
} |
|
46 |
|
|
47 |
private long getCRC(FeatureType type) { |
|
48 |
StringBuffer buffer = new StringBuffer(); |
|
49 |
for( int i=0; i< type.size(); i++) { |
|
50 |
FeatureAttributeDescriptor x = type.getAttributeDescriptor(i); |
|
51 |
buffer.append(x.getName()); |
|
52 |
buffer.append(x.getDataTypeName()); |
|
53 |
buffer.append(x.getSize()); |
|
54 |
} |
|
55 |
CRC32 crc = new CRC32(); |
|
56 |
byte[] data = buffer.toString().getBytes(); |
|
57 |
crc.update(data); |
|
58 |
return crc.getValue(); |
|
59 |
} |
|
60 |
|
|
61 |
private String getFilename(FeatureStore store) throws DataException { |
|
62 |
FeatureType type = store.getDefaultFeatureType(); |
|
63 |
|
|
64 |
return store.getName()+"-"+Long.toHexString(getCRC(type))+".xml"; |
|
65 |
} |
|
66 |
|
|
67 |
public void put(FeatureStore store) throws DataException, IOException { |
|
68 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
|
69 |
FeatureType type = store.getDefaultFeatureType(); |
|
70 |
if( !getDefinitionsFolder().exists() ) { |
|
71 |
FileUtils.forceMkdir(getDefinitionsFolder()); |
|
72 |
} |
|
73 |
File f = new File(getDefinitionsFolder(), getFilename(store)); |
|
74 |
dynObjectManager.exportSimpleDynClassDefinitions(f,type); |
|
75 |
} |
|
76 |
|
|
77 |
public DynClass get(FeatureStore store) throws DataException { |
|
78 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
|
79 |
FeatureType type = store.getDefaultFeatureType(); |
|
80 |
|
|
81 |
File f; |
|
82 |
try { |
|
83 |
f = new File(getDefinitionsFolder(), getFilename(store)); |
|
84 |
} catch (IOException ex) { |
|
85 |
return type; |
|
86 |
} |
|
87 |
if( !f.exists() ) { |
|
88 |
return type; |
|
89 |
} |
|
90 |
FileInputStream fis; |
|
91 |
try { |
|
92 |
fis = new FileInputStream(f); |
|
93 |
} catch (FileNotFoundException ex) { |
|
94 |
return type; |
|
95 |
} |
|
96 |
Map classes = null; |
|
97 |
try { |
|
98 |
classes = dynObjectManager.importDynClassDefinitions(fis, this.getClass().getClassLoader()); |
|
99 |
} catch (XmlPullParserException ex) { |
|
100 |
logger.warn("Can't load xml definition.", ex); |
|
101 |
return type; |
|
102 |
} catch (IOException ex) { |
|
103 |
return type; |
|
104 |
} |
|
105 |
DynClass dynClass = (DynClass) classes.get(type.getName()); |
|
106 |
if( dynClass == null ) { |
|
107 |
return type; |
|
108 |
} |
|
109 |
return dynClass; |
|
110 |
} |
|
111 |
|
|
112 |
} |
org.gvsig.attributeeditor/trunk/org.gvsig.attributeeditor/org.gvsig.attributeeditor.app/org.gvsig.attributeeditor.app.mainplugin/src/main/java/org/gvsig/app/mainplugin/extension/CreateDefaultDefinitionExtension.java | ||
---|---|---|
1 |
/* |
|
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package org.gvsig.app.mainplugin.extension; |
|
7 |
|
|
8 |
import org.slf4j.Logger; |
|
9 |
import org.slf4j.LoggerFactory; |
|
10 |
|
|
11 |
import org.gvsig.andami.plugins.Extension; |
|
12 |
import org.gvsig.app.ApplicationLocator; |
|
13 |
import org.gvsig.app.ApplicationManager; |
|
14 |
import org.gvsig.app.project.documents.view.ViewDocument; |
|
15 |
import org.gvsig.app.project.documents.view.ViewManager; |
|
16 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
|
17 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
|
18 |
import org.gvsig.fmap.mapcontext.MapContext; |
|
19 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
|
20 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
|
21 |
|
|
22 |
/** |
|
23 |
* |
|
24 |
* @author jjdelcerro |
|
25 |
*/ |
|
26 |
public class CreateDefaultDefinitionExtension extends Extension { |
|
27 |
|
|
28 |
private static final Logger logger = LoggerFactory.getLogger(CreateDefaultDefinitionExtension.class); |
|
29 |
|
|
30 |
public void initialize() { |
|
31 |
|
|
32 |
} |
|
33 |
|
|
34 |
public void execute(String actionCommand) { |
|
35 |
if( "tools-attributeditor-create-default-definition".equalsIgnoreCase(actionCommand) ) { |
|
36 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
37 |
ViewDocument viewDoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
|
38 |
if( viewDoc == null ) { |
|
39 |
return; |
|
40 |
} |
|
41 |
MapContext mapContext = viewDoc.getMapContext(); |
|
42 |
if( !mapContext.hasActiveVectorLayers() ) { |
|
43 |
return; |
|
44 |
} |
|
45 |
DataSwingManager featureFormManager = DALSwingLocator.getSwingManager(); |
|
46 |
FLayer[] layers = mapContext.getLayers().getActives(); |
|
47 |
for( int i=0; i<layers.length; i++ ) { |
|
48 |
if( layers[i] instanceof FLyrVect ) { |
|
49 |
FLyrVect layer = (FLyrVect) layers[i]; |
|
50 |
try { |
|
51 |
featureFormManager.getFeatureFormDefinitionsProvider().put(layer.getFeatureStore()); |
|
52 |
} catch (Exception ex) { |
|
53 |
logger.warn("Can't save form definition for layer '"+layer.getName()+"'.",ex); |
|
54 |
} |
|
55 |
|
|
56 |
} |
|
57 |
} |
|
58 |
} |
|
59 |
} |
|
60 |
|
|
61 |
public boolean isEnabled() { |
|
62 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
63 |
ViewDocument viewDoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
|
64 |
if( viewDoc == null ) { |
|
65 |
return false; |
|
66 |
} |
|
67 |
MapContext mapContext = viewDoc.getMapContext(); |
|
68 |
return mapContext.hasActiveVectorLayers(); |
|
69 |
} |
|
70 |
|
|
71 |
public boolean isVisible() { |
|
72 |
return true; |
|
73 |
} |
|
74 |
|
|
75 |
} |
org.gvsig.attributeeditor/trunk/org.gvsig.attributeeditor/org.gvsig.attributeeditor.app/org.gvsig.attributeeditor.app.mainplugin/src/main/java/org/gvsig/app/mainplugin/extension/AttributeEditorExtension.java | ||
---|---|---|
31 | 31 |
import org.gvsig.app.project.documents.view.ViewDocument; |
32 | 32 |
import org.gvsig.app.project.documents.view.ViewManager; |
33 | 33 |
import org.gvsig.app.project.documents.view.gui.IView; |
34 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
|
35 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
|
36 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
|
37 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
|
38 | 34 |
import org.gvsig.fmap.mapcontrol.MapControl; |
39 | 35 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.AttributeEditorBehavior; |
40 | 36 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.AttributeEditorPointListener; |
... | ... | |
45 | 41 |
*/ |
46 | 42 |
public class AttributeEditorExtension extends Extension { |
47 | 43 |
|
48 |
private static Logger logger = LoggerFactory.getLogger( |
|
49 |
AttributeEditorExtension.class); |
|
44 |
private static final Logger logger = LoggerFactory.getLogger(AttributeEditorExtension.class); |
|
50 | 45 |
|
46 |
@Override |
|
51 | 47 |
public void execute(String actionCommand) { |
52 |
|
|
53 | 48 |
if (actionCommand.compareToIgnoreCase("attribute-editor") != 0) { |
54 | 49 |
return; |
55 | 50 |
} |
56 |
|
|
57 |
IView vw = (IView) ApplicationLocator.getManager().getActiveComponent(ViewDocument.class); |
|
58 |
|
|
59 |
if (vw != null) { |
|
60 |
|
|
61 |
FLayer[] act_lyr = vw.getMapControl().getMapContext().getLayers().getActives(); |
|
62 |
if (act_lyr == null || act_lyr.length != 1 |
|
63 |
|| !(act_lyr[0] instanceof FLyrVect)) { |
|
64 |
//do nothing |
|
65 |
|
|
66 |
} else { |
|
67 |
|
|
68 |
FLyrVect lyr = (FLyrVect) act_lyr[0]; |
|
69 |
MapControl mapControl = vw.getMapControl(); |
|
51 |
IView view = (IView) ApplicationLocator.getManager().getActiveComponent(ViewDocument.class); |
|
52 |
if (view != null) { |
|
53 |
MapControl mapControl = view.getMapControl(); |
|
54 |
if( mapControl.getMapContext().hasActiveVectorLayers() ) { |
|
70 | 55 |
if (!mapControl.hasTool(AttributeEditorPointListener.ATTRIBUTE_EDITOR_TOOL_NAME)) { |
71 | 56 |
AttributeEditorBehavior attributeEditorBehavior = new AttributeEditorBehavior(mapControl); |
72 | 57 |
mapControl.addBehavior(AttributeEditorPointListener.ATTRIBUTE_EDITOR_TOOL_NAME, attributeEditorBehavior); |
73 | 58 |
} |
74 |
|
|
75 | 59 |
mapControl.setTool(AttributeEditorPointListener.ATTRIBUTE_EDITOR_TOOL_NAME); |
76 |
|
|
77 | 60 |
} |
78 | 61 |
} |
79 | 62 |
|
80 | 63 |
} |
81 | 64 |
|
65 |
@Override |
|
82 | 66 |
public void initialize() { |
83 |
|
|
84 | 67 |
IconThemeHelper.registerIcon("action", "attribute-editor", this); |
85 |
|
|
86 |
DataSwingManager featureFormManager = DALSwingLocator.getSwingManager(); |
|
87 |
featureFormManager.registerFeatureFormDefinitionsProvider(new DefaultFeatureFormDefinitionsProvider()); |
|
88 | 68 |
} |
89 | 69 |
|
70 |
@Override |
|
90 | 71 |
public boolean isEnabled() { |
91 |
|
|
92 | 72 |
/* |
93 | 73 |
* It's enabled if there is exactly one vector layer in the active view |
94 | 74 |
* and it has a selection |
95 | 75 |
*/ |
96 |
ViewDocument vw = actWin(); |
|
97 |
|
|
98 |
if (vw != null) { |
|
99 |
|
|
100 |
FLayer[] act_lyr = vw.getMapContext().getLayers().getActives(); |
|
101 |
if (act_lyr == null || act_lyr.length != 1 |
|
102 |
|| !(act_lyr[0] instanceof FLyrVect)) { |
|
103 |
return false; |
|
104 |
|
|
105 |
} else { |
|
106 |
FLyrVect vect = (FLyrVect) act_lyr[0]; |
|
107 |
if (!vect.isAvailable()) { |
|
108 |
/* |
|
109 |
* This can happen when opening a persisted project |
|
110 |
* and there is a "slow" layer (GeoDB) |
|
111 |
*/ |
|
112 |
return false; |
|
113 |
} |
|
114 |
return true; |
|
115 |
} |
|
116 |
|
|
117 |
} else { |
|
76 |
ViewDocument viewDoc = actWin(); |
|
77 |
if (viewDoc == null) { |
|
118 | 78 |
return false; |
119 | 79 |
} |
80 |
return viewDoc.getMapContext().hasActiveVectorLayers(); |
|
120 | 81 |
} |
121 | 82 |
|
83 |
@Override |
|
122 | 84 |
public boolean isVisible() { |
123 |
|
|
124 | 85 |
return actWin() != null; |
125 | 86 |
} |
126 | 87 |
|
org.gvsig.attributeeditor/trunk/org.gvsig.attributeeditor/org.gvsig.attributeeditor.app/org.gvsig.attributeeditor.app.mainplugin/pom.xml | ||
---|---|---|
30 | 30 |
<groupId>org.gvsig</groupId> |
31 | 31 |
<artifactId>org.gvsig.attributeeditor.mapcontrol</artifactId> |
32 | 32 |
</dependency> |
33 |
<dependency> |
|
34 |
<groupId>commons-collections</groupId> |
|
35 |
<artifactId>commons-collections</artifactId> |
|
36 |
<version>3.2.1</version> |
|
37 |
<type>jar</type> |
|
38 |
</dependency> |
|
33 | 39 |
</dependencies> |
34 | 40 |
</project> |
Also available in: Unified diff