[udig-devel] ODD side-effect. Feature update creating duplicate
Project
M.S.Bachler
M.S.Bachler at open.ac.uk
Thu May 4 08:30:06 PDT 2006
Hi!
Somehow I have got my code into a strange position where when I update a
point feature's data, it actually creates a duplicate project
(+map/layer) listing in the Project Explorer View.
The update code is:
EditManager manager = (EditManager)map.getEditManager();
manager.setEditFeature(feature, layer);
String sData = (String)feature.getAttribute(7);
// code to process sData and create the sFinalData
feature.setAttribute(7, sFinalData);
manager.commitTransaction();
This looks fine to me, and I am doing this type of thing in other places
without this ODD side-effect.
The only difference this time is the way I have obtained the map, layer
and feature references from searching the projects:
private void searchProjects(String sMapPath, String sLayer,
String sPoint) {
URI mapURI = URI.createFileURI(sMapPath);
List projects =
ProjectRegistryImpl.getProjectRegistry().getProjects();
Project project = null;
boolean bFound = false;
Resource resource = null;
List list = null;
int countk = 0;
int countj = projects.size();
for (int j= 0; j<countj; j++) {
bFound = false;
project = (Project)projects.get(j);
// SEE IF THIS PROJECT HAS A MAP WITH THE
CORRECT NAME
// IF IT HAS THE CORRECT LAYER, THEN FOUND +
TRUE
try {
resource =
project.eResource().getResourceSet().getResource(mapURI, true);
if (resource != null && resource
instanceof Map) {
if (processMap((Map)resource,
sLayer, sPoint)) {
bFound = true;
}
}
} catch (Exception io){} // getResource can
throw FileNotFoundException
// ELSE SEARCH ALL MAPS FOR THE CORRECT LAYER
if (!bFound) {
ResourceSet resources =
project.eResource().getResourceSet();
List elements =
resources.getResources();
countk=elements.size();
for (int k=0;k <countk; k++) {
resource
=(Resource)elements.get(k);
list = resource.getContents();
if (list != null && list.size()
> 0) {
System.out.println("mapResourceContents="+(resource.getContents().get(0)
).getClass().getName());
Object obj =
resource.getContents().get(0);
if (obj instanceof Map)
{
processMap((Map)obj, sLayer, sPoint);
}
}
}
}
}
}
private boolean processMap(Map map, String sLayer, String
sPoint) {
boolean bLayerFound = false;
Layer layer = findLayer(map, sLayer);
if (layer != null) {
Feature feature = findPoint(layer, sPoint);
bLayerFound = true;
}
return bLayerFound;
}
private Layer findLayer(Map map, String sLayer) {
Layer layer = null;
if (map != null ) {
List layers = map.getLayersInternal();
int count = layers.size();
for (int j= 0; j < count; j++) {
layer = (Layer)layers.get(j);
System.out.println("checking
layer="+layer.getID());
if
((layer.getID().toString()).equals(sLayer)) {
nMatchCount++;
vtMapMatches.insertElementAt(map, nMatchCount);
vtLayerMatches.insertElementAt(layer, nMatchCount);
vtFeatureMatches.insertElementAt("None", nMatchCount);
break;
} else {
layer = null;
}
}
}
return layer;
}
private Feature findPoint(Layer layer, String sPoint) {
Feature feature = null;
String sFeatureID = "";
FeatureIterator featurReader = null;
FeatureCollection results = null;
FeatureSource resource = null;
IProgressMonitor monitor = new
NullProgressMonitor();
try {
resource =
layer.getResource(FeatureSource.class, monitor);
if (resource != null) {
results = resource.getFeatures();
featurReader = results.features();
while (featurReader.hasNext())
{
feature=featurReader.next();
sFeatureID =
(String)feature.getAttribute(1);
if (sFeatureID.equals(sPoint)) {
vtFeatureMatches.removeElementAt(nMatchCount);
vtFeatureMatches.insertElementAt(feature, nMatchCount);
break;
}
}
featurReader.close();
}
} catch (IOException e) {}
monitor.done();
return feature;
}
Can anyone see why obtaining the map, layer and feature references this
way would mean that when I update the feature the Project Explorer View
lists the project again (shows it twice)?
Michelle
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.refractions.net/pipermail/udig-devel/attachments/20060504/11bcf3c2/attachment-0001.html
More information about the udig-devel
mailing list