Skip to content

Commit

Permalink
add feature browser
Browse files Browse the repository at this point in the history
  • Loading branch information
moovida committed Feb 20, 2017
1 parent e307668 commit 28277cf
Show file tree
Hide file tree
Showing 5 changed files with 1,108 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package org.jgrasstools.gvsig.featurebrowser;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComponent;

import org.gvsig.fmap.dal.exception.DataException;
import org.gvsig.fmap.dal.feature.Feature;
import org.gvsig.fmap.dal.feature.FeatureSelection;
import org.gvsig.fmap.geom.Geometry;
import org.gvsig.fmap.geom.primitive.Envelope;
import org.gvsig.fmap.mapcontext.MapContext;
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
import org.gvsig.tools.swing.api.Component;
import org.gvsig.tools.swing.api.ToolsSwingLocator;
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
import org.jgrasstools.gvsig.base.FeatureUtilities;
import org.jgrasstools.gvsig.base.JGTUtilities;
import org.jgrasstools.gvsig.base.LayerUtilities;

public class FeatureBrowserController extends FeatureBrowserView implements Component {

private HashMap<String, FLyrVect> name2LayersMap = new HashMap<String, FLyrVect>();
private MapContext currentMapcontext;
private ThreadSafeDialogsManager dialogManager = ToolsSwingLocator.getThreadSafeDialogsManager();

private String selectedLayerName;

private int featureCount = 0;
private int featureSize = 0;
private List<Feature> features;
private String[] attributesNames;
private FLyrVect selectedLayer;

public FeatureBrowserController() {
setPreferredSize(new Dimension(600, 300));
init();
}

private void init() {

// TODO implement buffer part
_bufferLabel.setVisible(false);
_bufferText.setVisible(false);
// end TODO

setCombos();

_layerCombo.addActionListener(new ActionListener(){


public void actionPerformed( ActionEvent e ) {
selectedLayerName = (String) _layerCombo.getSelectedItem();

selectedLayer = name2LayersMap.get(selectedLayerName);
if (selectedLayer != null) {
try {
attributesNames = LayerUtilities.getAttributesNames(selectedLayer, false);

features = FeatureUtilities.getFeatures(selectedLayer.getFeatureStore(), null, null);
featureCount = 1;
featureSize = features.size();

updateForFeature();

} catch (Exception e1) {
e1.printStackTrace();
}
} else {
_infoTextArea.setText("");
}
}
});


_nextButton.addActionListener(new ActionListener(){

@Override
public void actionPerformed( ActionEvent e ) {
featureCount++;
if (featureCount > featureSize) {
featureCount = 1;
}
updateForFeature();
}
});
_previousButton.addActionListener(new ActionListener(){

@Override
public void actionPerformed( ActionEvent e ) {
featureCount--;
if (featureCount < 1) {
featureCount = featureSize;
}
updateForFeature();
}
});
}

private void updateForFeature() {
_featureCountLabel.setText("Feature N." + featureCount + " of " + featureSize);

Feature feature = features.get(featureCount - 1);

StringBuilder sb = new StringBuilder();
for( String attrName : attributesNames ) {
sb.append(attrName).append("=").append(feature.get(attrName).toString()).append("\n");
}
_infoTextArea.setText(sb.toString());

Geometry defaultGeometry = feature.getDefaultGeometry();
Envelope envelope = defaultGeometry.getEnvelope();

// String bufferStr = _bufferText.getText();
// double buffer = 0;
// try {
// buffer = Double.parseDouble(bufferStr);
// } catch (NumberFormatException e) {
// // ignore
// }
JGTUtilities.zoomTo(envelope);

try {
FeatureSelection featureSelection = selectedLayer.getFeatureStore().getFeatureSelection();
featureSelection.deselectAll();
featureSelection.select(feature);
} catch (DataException e) {
e.printStackTrace();
}
}

private void setCombos() {
try {
List<FLyrVect> vectorLayers = LayerUtilities.getVectorLayers(currentMapcontext);

if (vectorLayers.size() == name2LayersMap.size()) {
return;
}


name2LayersMap = new HashMap<String, FLyrVect>();
String[] names = new String[vectorLayers.size()];
int count = 0;
for( FLyrVect fLyrVect : vectorLayers ) {
String name = fLyrVect.getName();
name2LayersMap.put(name, fLyrVect);
names[count++] = name;
}

Object selectedLayer = _layerCombo.getSelectedItem();
_layerCombo.setModel(new DefaultComboBoxModel<String>(names));
if (selectedLayer != null) {
_layerCombo.setSelectedItem(selectedLayer);
}
} catch (Exception e) {
e.printStackTrace();
}
}

public JComponent asJComponent() {
return this;
}

public void isVisibleTriggered() {
// MapContext newMapcontext = ProjectUtilities.getCurrentMapcontext();
// if (newMapcontext == currentMapcontext) {
// return;
// }
setCombos();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* This file is part of JGrasstools (http://www.jgrasstools.org)
* (C) HydroloGIS - www.hydrologis.com
*
* JGrasstools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jgrasstools.gvsig.featurebrowser;

import org.gvsig.andami.IconThemeHelper;
import org.gvsig.andami.plugins.Extension;
import org.gvsig.tools.swing.api.ToolsSwingLocator;
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author Andrea Antonello (www.hydrologis.com)
*/
public class FeatureBrowserExtension extends Extension {

private static final String ACTION_FEATUREBROWSE = "feature-browser-tools";

private FeatureBrowserController featureBrowserController;

public void initialize() {
IconThemeHelper.registerIcon("action", "copy", this);
}

public void postInitialize() {
}

/**
* Execute the actions associated to this extension.
*/
public void execute( String actionCommand ) {
if (ACTION_FEATUREBROWSE.equalsIgnoreCase(actionCommand)) {
featureBrowserController = new FeatureBrowserController();
WindowManager windowManager = ToolsSwingLocator.getWindowManager();
windowManager.showWindow(featureBrowserController.asJComponent(), "Feature Browser", MODE.WINDOW);
}
}

/**
* Check if tools of this extension are enabled.
*/
public boolean isEnabled() {
return true;
}

/**
* Check if tools of this extension are visible.
*/
public boolean isVisible() {
if (featureBrowserController != null)
featureBrowserController.isVisibleTriggered();
return true;
}

}
Loading

0 comments on commit 28277cf

Please sign in to comment.