Revision: 5145
          http://sourceforge.net/p/jump-pilot/code/5145
Author:   michaudm
Date:     2016-11-03 23:17:57 +0000 (Thu, 03 Nov 2016)
Log Message:
-----------
Random attribute values generators

Modified Paths:
--------------
    core/trunk/scripts/default-plugins.xml
    core/trunk/src/com/vividsolutions/jump/workbench/ui/AttributeTypeFilter.java
    
core/trunk/src/org/openjump/core/ui/plugin/tools/AutoAssignAttributePlugIn.java

Added Paths:
-----------
    
core/trunk/src/org/openjump/core/ui/plugin/tools/GenerateRandomNumberPlugIn.java
    
core/trunk/src/org/openjump/core/ui/plugin/tools/GenerateRandomStringPlugIn.java

Modified: core/trunk/scripts/default-plugins.xml
===================================================================
--- core/trunk/scripts/default-plugins.xml      2016-11-03 11:53:22 UTC (rev 
5144)
+++ core/trunk/scripts/default-plugins.xml      2016-11-03 23:17:57 UTC (rev 
5145)
@@ -803,6 +803,12 @@
                org.openjump.core.ui.plugin.tools.AutoAssignAttributePlugIn
        </plug-in>
        <plug-in>
+               org.openjump.core.ui.plugin.tools.GenerateRandomNumberPlugIn
+       </plug-in>
+       <plug-in>
+               org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn
+       </plug-in>
+       <plug-in>
                
de.fho.jump.pirol.plugins.EditAttributeByFormula.EditAttributeByFormulaPlugIn
        </plug-in>
        <plug-in>

Modified: 
core/trunk/src/com/vividsolutions/jump/workbench/ui/AttributeTypeFilter.java
===================================================================
--- 
core/trunk/src/com/vividsolutions/jump/workbench/ui/AttributeTypeFilter.java    
    2016-11-03 11:53:22 UTC (rev 5144)
+++ 
core/trunk/src/com/vividsolutions/jump/workbench/ui/AttributeTypeFilter.java    
    2016-11-03 23:17:57 UTC (rev 5145)
@@ -37,6 +37,7 @@
 import com.vividsolutions.jump.workbench.model.LayerManager;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 
 /**
@@ -121,6 +122,22 @@
         }
         return layerList;
     }
+
+    /**
+     * Filter layers from a LayerManager according to AttributeType of their
+     * attributes.
+     *
+     * @param layers candidate layers to filter
+     * @return a List of Layers
+     */
+    public List<Layer> filter(Collection<Layer> layers) {
+        List<Layer> layerList = new ArrayList<>();
+        for (Layer layer : layers) {
+            FeatureSchema schema = 
layer.getFeatureCollectionWrapper().getFeatureSchema();
+            if (filter(schema).size() > 0) layerList.add(layer);
+        }
+        return layerList;
+    }
     
     /**
      * Filter attributes of a FeatureSchema according to their AttributeType.

Modified: 
core/trunk/src/org/openjump/core/ui/plugin/tools/AutoAssignAttributePlugIn.java
===================================================================
--- 
core/trunk/src/org/openjump/core/ui/plugin/tools/AutoAssignAttributePlugIn.java 
    2016-11-03 11:53:22 UTC (rev 5144)
+++ 
core/trunk/src/org/openjump/core/ui/plugin/tools/AutoAssignAttributePlugIn.java 
    2016-11-03 23:17:57 UTC (rev 5145)
@@ -149,22 +149,24 @@
        dialog.setSideBarDescription(DESCRIPTION);
         
        // Source layer and target attribute
-       final JComboBox layerComboBox = dialog.addEditableLayerComboBox(
-                        LAYER_COMBO_BOX, 
-                        context.getLayerNamePanel().chooseEditableLayer(), 
-                        null, context.getLayerManager());
+        dialog.addLayerComboBox(LAYER_COMBO_BOX,
+                context.getLayerableNamePanel().chooseEditableLayer(),
+                null, context.getLayerManager().getEditableLayers());
+
         boolean selectionExists = context.getLayerViewPanel()
                                          .getSelectionManager()
                                          .getFeaturesWithSelectedItems()
                                          .size() > 0;
+
         if (!selectionExists) selectedFeaturesOnly = false;
-        final JCheckBox selectedFeaturesOnlyCheckBox = 
-            dialog.addCheckBox(SELECTED_CHECK_BOX, selectedFeaturesOnly);
+        dialog.addCheckBox(SELECTED_CHECK_BOX, selectedFeaturesOnly);
         dialog.setFieldVisible(SELECTED_CHECK_BOX, selectionExists);
+
         final JComboBox targetAttributeComboBox = 
             dialog.addAttributeComboBox(TARGET_ATTRIBUTE_COMBO_BOX, 
LAYER_COMBO_BOX,
                     new AttributeTypeFilter(AttributeTypeFilter.DATE | 
AttributeTypeFilter.STRING |
                     AttributeTypeFilter.DOUBLE | AttributeTypeFilter.INTEGER), 
"");
+
         for (int i = 0 ; i < targetAttributeComboBox.getModel().getSize() ; 
i++) {
             Object item = targetAttributeComboBox.getModel().getElementAt(i);
             if (item.equals(targetAttribute)) 
targetAttributeComboBox.setSelectedIndex(i);
@@ -173,8 +175,8 @@
         // Auto-increment options
         dialog.addSeparator();
         final JCheckBox autoIncCheckBox = 
dialog.addCheckBox(AUTOINC_CHECK_BOX, autoIncrement);
-        final JTextField autoIncPatternField = 
dialog.addTextField(AUTOINC_PATTERN_BOX, pattern, 4, null, 
AUTOINC_DESCRIPTION_2);
-        final JTextField incField = dialog.addIntegerField(INC_VALUE_EDIT_BOX, 
1, 4, "");
+        dialog.addTextField(AUTOINC_PATTERN_BOX, pattern, 4, null, 
AUTOINC_DESCRIPTION_2);
+        dialog.addIntegerField(INC_VALUE_EDIT_BOX, 1, 4, "");
         
         // From other attribute option
         dialog.addSeparator();
@@ -278,29 +280,12 @@
             }
         }));
     }
-    
-    private String attributeName(List attributeNames, int preferredIndex) {
-        return (String) attributeNames.get(
-            attributeNames.size() > preferredIndex ? preferredIndex : 0);
+
+    private interface Converter {
+        Object convert(String d);
     }
-    
-    private Layer candidateLayer(PlugInContext context) {
-        if (context.getActiveInternalFrame() instanceof LayerNamePanelProxy) {
-            Layer[] selectedLayers = context.getSelectedLayers();
-            for (int i = 0; i < selectedLayers.length; i++) {
-                if (selectedLayers[i].isEditable()) {
-                    return selectedLayers[i];
-                }
-            }
-        }
-        return (Layer) 
context.getLayerManager().getEditableLayers().iterator().next();
-    }
 
-    private static interface Converter {
-        public Object convert(String d);
-    }
-    
-    private Map typeToConverterMap = new HashMap() {
+    private Map<AttributeType,Converter> typeToConverterMap = new 
HashMap<AttributeType,Converter>() {
         {
             put(AttributeType.STRING, new Converter() {
                 public Object convert(String d) {
@@ -311,28 +296,40 @@
                 public Object convert(String d) {
                     if (d==null) return null;
                     try {
-                        return new Integer(d);
+                        return Integer.parseInt(d);
                     } catch(NumberFormatException nfe) {
                         return null;
                     }
-                       //String s = parseNumber(d);
-                       //if (s.length() == 0) 
-                       //      return new Integer(0);
-                    //return new Integer(s);
                 }
             });
+            put(AttributeType.LONG, new Converter() {
+                public Object convert(String d) {
+                    if (d==null) return null;
+                    try {
+                        return Long.parseLong(d);
+                    } catch(NumberFormatException nfe) {
+                        return null;
+                    }
+                }
+            });
+            put(AttributeType.BOOLEAN, new Converter() {
+                public Object convert(String d) {
+                    if (d==null) return null;
+                    try {
+                        return Boolean.parseBoolean(d);
+                    } catch(NumberFormatException nfe) {
+                        return null;
+                    }
+                }
+            });
             put(AttributeType.DOUBLE, new Converter() {
                 public Object convert(String d) {
                     if (d==null) return null;
                     try {
-                        return new Double(d);
+                        return Double.parseDouble(d);
                     } catch(NumberFormatException nfe) {
                         return null;
                     }
-                       //String s = parseNumber(d);
-                       //if (s.length() == 0) 
-                       //      return new Double(0);
-                    //return new Double(parseNumber(d));
                 }
             });
             put(AttributeType.DATE, new Converter() {
@@ -340,7 +337,6 @@
                 public Object convert(String d) {
                     if (d==null) return null;
                     try {
-                        //return new Double(d);
                         return parser.parse(d, true);
                     } catch(ParseException nfe) {
                         return null;
@@ -351,7 +347,7 @@
     };
  
     private String parseNumber(String text) {
-        int b=0; int e=0;
+        int b; int e;
        for (int i=0; i<text.length(); i++) {
                if (Character.isDigit(text.charAt(i))) {
                        b=i; e=i;
@@ -365,8 +361,8 @@
 
     private void assignValues(PlugInContext context) {
         //Iterator iterator;
-        final Collection newFeatures = new ArrayList<Feature>();
-        final Collection oldFeatures = new ArrayList<Feature>();
+        final Collection<Feature> newFeatures = new ArrayList<>();
+        final Collection<Feature> oldFeatures = new ArrayList<>();
         final Collection<Feature> features;
         if (selectedFeaturesOnly) {
             Collection layers = 
context.getLayerViewPanel().getSelectionManager()
@@ -380,8 +376,7 @@
             features = layer.getFeatureCollectionWrapper().getFeatures();
         }
         context.getLayerManager().getUndoableEditReceiver().startReceiving();
-        for (Iterator i = features.iterator(); i.hasNext(); ) {
-            Feature feature = (Feature) i.next();
+        for (Feature feature : features) {
             String s;
             if (autoIncrement) {
                 String value = "" + autoInc;
@@ -395,7 +390,7 @@
             } else {
                 s = textToAssign;
             }
-            Object object = ((Converter) 
typeToConverterMap.get(destinationAttributeType)).convert(s);
+            Object object = 
typeToConverterMap.get(destinationAttributeType).convert(s);
             oldFeatures.add(feature.clone(false));
             Feature newFeature = feature.clone(false);
             newFeature.setAttribute(targetAttribute, object);

Added: 
core/trunk/src/org/openjump/core/ui/plugin/tools/GenerateRandomNumberPlugIn.java
===================================================================
--- 
core/trunk/src/org/openjump/core/ui/plugin/tools/GenerateRandomNumberPlugIn.java
                            (rev 0)
+++ 
core/trunk/src/org/openjump/core/ui/plugin/tools/GenerateRandomNumberPlugIn.java
    2016-11-03 23:17:57 UTC (rev 5145)
@@ -0,0 +1,170 @@
+package org.openjump.core.ui.plugin.tools;
+
+import com.vividsolutions.jump.I18N;
+import com.vividsolutions.jump.feature.*;
+import com.vividsolutions.jump.workbench.WorkbenchContext;
+import com.vividsolutions.jump.workbench.model.FeatureEventType;
+import com.vividsolutions.jump.workbench.model.Layer;
+import com.vividsolutions.jump.workbench.model.UndoableCommand;
+import com.vividsolutions.jump.workbench.plugin.EnableCheck;
+import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
+import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
+import com.vividsolutions.jump.workbench.plugin.PlugInContext;
+import com.vividsolutions.jump.workbench.ui.*;
+import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller;
+import org.openjump.core.ui.plugin.AbstractUiPlugIn;
+
+import javax.swing.*;
+import java.awt.*;
+import java.util.*;
+
+/**
+ * Plugin to fill an attribute with randomly generated numbers
+ */
+public class GenerateRandomNumberPlugIn extends AbstractUiPlugIn {
+
+    private static String LAYER        = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomNumberPlugIn.layer");
+    private static String ATTRIBUTE    = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomNumberPlugIn.attribute");
+    private static String ATTRIBUTE_TT = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomNumberPlugIn.attribute-tooltip");
+    private static String RANDOM       = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomNumberPlugIn.random-generators");
+    private static String MIN          = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomNumberPlugIn.min-value");
+    private static String MIN_TT       = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomNumberPlugIn.min-value-tooltip");
+    private static String MAX          = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomNumberPlugIn.max-value");
+    private static String MAX_TT       = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomNumberPlugIn.max-value-tooltip");
+
+    private static String NO_CANDIDATE  = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.no-editable-layer-with-numeric-attribute");
+
+    private Layer layer;
+    private String attribute;
+    private double min, max;
+
+    public void initialize(PlugInContext context) throws Exception {
+
+        FeatureInstaller featureInstaller = new 
FeatureInstaller(context.getWorkbenchContext());
+        featureInstaller.addMainMenuPlugin(
+                this,
+                new String[] {MenuNames.TOOLS, 
MenuNames.TOOLS_EDIT_ATTRIBUTES, RANDOM},
+                getName() + "...", false, null,
+                createEnableCheck(context.getWorkbenchContext()));
+    }
+
+    public static MultiEnableCheck createEnableCheck(final WorkbenchContext 
workbenchContext) {
+        EnableCheckFactory checkFactory = new 
EnableCheckFactory(workbenchContext);
+        return new MultiEnableCheck()
+                .add(checkFactory.createAtLeastNLayersMustExistCheck(1))
+                .add(checkFactory.createAtLeastNLayersMustBeEditableCheck(1))
+                .add(new EnableCheck() {
+                    @Override
+                    public String check(JComponent component) {
+                        if (AttributeTypeFilter.NUMERIC_FILTER.filter(
+                                
workbenchContext.getLayerManager().getEditableLayers()).size() == 0) {
+                            return NO_CANDIDATE;
+                        }
+                        return null;
+                    }
+                });
+    }
+
+    public boolean execute(PlugInContext context) throws Exception{
+        this.reportNothingToUndoYet(context);
+
+        MultiInputDialog dialog = new MultiInputDialog(
+                context.getWorkbenchFrame(), getName(), true);
+        setDialogValues(dialog, context);
+        GUIUtil.centreOnWindow(dialog);
+        dialog.setVisible(true);
+        if (! dialog.wasOKPressed()) { return false; }
+        getDialogValues(dialog);
+        boolean empty = checkAttributeEmpty();
+        if (!empty) {
+            OKCancelDialog okCancelDialog = new OKCancelDialog(dialog, 
"Attribute column is not empty", true,
+                    new JLabel("Attribute column is not empty. Do you want to 
overwrite it ?"),
+                    new OKCancelDialog.Validator() {
+                        @Override
+                        public String validateInput(Component component) {
+                            return null;
+                        }
+                    });
+            okCancelDialog.setVisible(true);
+            if (!okCancelDialog.wasOKPressed()) {
+                return false;
+            }
+        }
+        computeRandomValues(context);
+        return true;
+    }
+
+    private boolean checkAttributeEmpty() {
+        for (Feature f : layer.getFeatureCollectionWrapper().getFeatures()) {
+            if (f.getAttribute(attribute) != null) return false;
+        }
+        return true;
+    }
+
+    private void computeRandomValues(PlugInContext context) {
+        Random rdm = new Random();
+        FeatureSchema schema = 
layer.getFeatureCollectionWrapper().getFeatureSchema();
+        AttributeType type = schema.getAttributeType(attribute);
+        final Collection<Feature> oldFeatures = new ArrayList<>();
+        final Collection<Feature> newFeatures = new ArrayList<>();
+        reportNothingToUndoYet(context);
+        for (Feature f : layer.getFeatureCollectionWrapper().getFeatures()) {
+            oldFeatures.add(f.clone(true, true));
+            f = f.clone(true, true);
+            if (type == AttributeType.DOUBLE) {
+                f.setAttribute(attribute, min + rdm.nextDouble()*(max-min));
+            } else if (type == AttributeType.INTEGER) {
+                f.setAttribute(attribute, (int)min + 
rdm.nextInt((int)(max-min)));
+            } else if (type == AttributeType.LONG) {
+                f.setAttribute(attribute, (long)min + 
(long)rdm.nextInt((int)(max-min)));
+            }
+            newFeatures.add(f);
+        }
+        context.getLayerManager().getUndoableEditReceiver().startReceiving();
+        try {
+            UndoableCommand command =
+                    new 
UndoableCommand(I18N.get(AutoAssignAttributePlugIn.class.getName())) {
+
+                        public void execute() {
+                            Iterator<Feature> newFeatIterator = 
newFeatures.iterator();
+                            for (Feature f : 
layer.getFeatureCollectionWrapper().getFeatures()) {
+                                f.setAttribute(attribute, 
newFeatIterator.next().getAttribute(attribute));
+                            }
+                            
layer.getLayerManager().fireFeaturesAttChanged(newFeatures,
+                                    FeatureEventType.ATTRIBUTES_MODIFIED, 
layer, oldFeatures);
+                        }
+
+                        public void unexecute() {
+                            Iterator<Feature> oldFeatIterator = 
oldFeatures.iterator();
+                            for (Feature f : 
layer.getFeatureCollectionWrapper().getFeatures()) {
+                                f.setAttribute(attribute, 
oldFeatIterator.next().getAttribute(attribute));
+                            }
+                            layer.getLayerManager().fireFeaturesAttChanged(
+                                    
layer.getFeatureCollectionWrapper().getFeatures(),
+                                    FeatureEventType.ATTRIBUTES_MODIFIED, 
layer, newFeatures);
+                        }
+                    };
+            command.execute();
+            
layer.getLayerManager().getUndoableEditReceiver().receive(command.toUndoableEdit());
+        } finally {
+            layer.getLayerManager().getUndoableEditReceiver().stopReceiving();
+        }
+    }
+
+    private void setDialogValues(MultiInputDialog dialog, PlugInContext 
context) {
+        layer = context.getLayerableNamePanel().chooseEditableLayer();
+        dialog.addLayerComboBox(LAYER, layer, context.getLayerManager());
+        dialog.addLayerComboBox(LAYER, layer, null,
+                
AttributeTypeFilter.NUMERIC_FILTER.filter(context.getLayerManager().getEditableLayers()));
+        dialog.addAttributeComboBox(ATTRIBUTE, LAYER, 
AttributeTypeFilter.NUMERIC_FILTER, ATTRIBUTE_TT);
+        dialog.addDoubleField(MIN, 0, 12, MIN_TT);
+        dialog.addDoubleField(MAX, 0, 12, MAX_TT);
+    }
+
+    private void getDialogValues(MultiInputDialog dialog) {
+        layer = dialog.getLayer(LAYER);
+        attribute = dialog.getText(ATTRIBUTE);
+        min = dialog.getDouble(MIN);
+        max = dialog.getDouble(MAX);
+    }
+}

Added: 
core/trunk/src/org/openjump/core/ui/plugin/tools/GenerateRandomStringPlugIn.java
===================================================================
--- 
core/trunk/src/org/openjump/core/ui/plugin/tools/GenerateRandomStringPlugIn.java
                            (rev 0)
+++ 
core/trunk/src/org/openjump/core/ui/plugin/tools/GenerateRandomStringPlugIn.java
    2016-11-03 23:17:57 UTC (rev 5145)
@@ -0,0 +1,208 @@
+package org.openjump.core.ui.plugin.tools;
+
+import com.vividsolutions.jump.I18N;
+import com.vividsolutions.jump.feature.*;
+import com.vividsolutions.jump.workbench.WorkbenchContext;
+import com.vividsolutions.jump.workbench.model.FeatureEventType;
+import com.vividsolutions.jump.workbench.model.Layer;
+import com.vividsolutions.jump.workbench.model.UndoableCommand;
+import com.vividsolutions.jump.workbench.plugin.EnableCheck;
+import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
+import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
+import com.vividsolutions.jump.workbench.plugin.PlugInContext;
+import com.vividsolutions.jump.workbench.ui.*;
+import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller;
+import org.openjump.core.ui.plugin.AbstractUiPlugIn;
+
+import javax.swing.*;
+import java.awt.*;
+import java.util.*;
+
+/**
+ * Plugin to fill an attribute with randomly generated numbers
+ */
+public class GenerateRandomStringPlugIn extends AbstractUiPlugIn {
+
+    private static String LAYER         = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.layer");
+    private static String ATTRIBUTE     = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.attribute");
+    private static String ATTRIBUTE_TT  = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.attribute-tooltip");
+    private static String RANDOM        = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.random-generators");
+    private static String MIN_LENGTH    = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.min-length");
+    private static String MIN_LENGTH_TT = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.min-length-tooltip");
+    private static String MAX_LENGTH    = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.max-length");
+    private static String MAX_LENGTH_TT = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.max-length-tooltip");
+
+    private static String LETTER_BASED  = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.letter-based");
+    private static String WORD_BASED    = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.word-based");
+    private static String DIGITS        = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.digits");
+    private static String HEXA          = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.hexa");
+    private static String ASCII         = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.ascii");
+    private static String CITIES        = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.cities");
+    private static String NAMES         = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.names");
+
+    private static String NO_CANDIDATE  = 
I18N.get("org.openjump.core.ui.plugin.tools.GenerateRandomStringPlugIn.no-editable-layer-with-string-attribute");
+
+    private Layer layer;
+    private String attribute;
+    private int min, max;
+    private boolean digits, hexa, ascii, cities, names;
+
+    public void initialize(PlugInContext context) throws Exception {
+
+        FeatureInstaller featureInstaller = new 
FeatureInstaller(context.getWorkbenchContext());
+        featureInstaller.addMainMenuPlugin(
+                this,
+                new String[] {MenuNames.TOOLS, 
MenuNames.TOOLS_EDIT_ATTRIBUTES, RANDOM},
+                getName() + "...", false, null,
+                createEnableCheck(context.getWorkbenchContext()));
+    }
+
+    public static MultiEnableCheck createEnableCheck(final WorkbenchContext 
workbenchContext) {
+        EnableCheckFactory checkFactory = new 
EnableCheckFactory(workbenchContext);
+        return new MultiEnableCheck()
+                .add(checkFactory.createAtLeastNLayersMustExistCheck(1))
+                .add(checkFactory.createAtLeastNLayersMustBeEditableCheck(1))
+                .add(new EnableCheck() {
+                    @Override
+                    public String check(JComponent component) {
+                        if (AttributeTypeFilter.STRING_FILTER.filter(
+                                
workbenchContext.getLayerManager().getEditableLayers()).size() == 0) {
+                            return NO_CANDIDATE;
+                        }
+                        return null;
+                    }
+                });
+    }
+
+    public boolean execute(PlugInContext context) throws Exception{
+        this.reportNothingToUndoYet(context);
+
+        MultiInputDialog dialog = new MultiInputDialog(
+                context.getWorkbenchFrame(), getName(), true);
+        setDialogValues(dialog, context);
+        GUIUtil.centreOnWindow(dialog);
+        dialog.setVisible(true);
+        if (! dialog.wasOKPressed()) { return false; }
+        getDialogValues(dialog);
+        boolean empty = checkAttributeEmpty();
+        if (!empty) {
+            OKCancelDialog okCancelDialog = new OKCancelDialog(dialog, 
"Attribute column is not empty", true,
+                    new JLabel("Attribute column is not empty. Do you want to 
overwrite it ?"),
+                    new OKCancelDialog.Validator() {
+                        @Override
+                        public String validateInput(Component component) {
+                            return null;
+                        }
+                    });
+            okCancelDialog.setVisible(true);
+            if (!okCancelDialog.wasOKPressed()) {
+                return false;
+            }
+        }
+        computeRandomValues(context);
+        return true;
+    }
+
+    private boolean checkAttributeEmpty() {
+        for (Feature f : layer.getFeatureCollectionWrapper().getFeatures()) {
+            if (f.getAttribute(attribute) != null) return false;
+        }
+        return true;
+    }
+
+    private void computeRandomValues(PlugInContext context) {
+        final Collection<Feature> oldFeatures = new ArrayList<>();
+        final Collection<Feature> newFeatures = new ArrayList<>();
+        reportNothingToUndoYet(context);
+        for (Feature f : layer.getFeatureCollectionWrapper().getFeatures()) {
+            oldFeatures.add(f.clone(true, true));
+            f = f.clone(true, true);
+            f.setAttribute(attribute, generate(min, max));
+            newFeatures.add(f);
+        }
+        context.getLayerManager().getUndoableEditReceiver().startReceiving();
+        try {
+            UndoableCommand command =
+                    new 
UndoableCommand(I18N.get(AutoAssignAttributePlugIn.class.getName())) {
+
+                        public void execute() {
+                            Iterator<Feature> newFeatIterator = 
newFeatures.iterator();
+                            for (Feature f : 
layer.getFeatureCollectionWrapper().getFeatures()) {
+                                f.setAttribute(attribute, 
newFeatIterator.next().getAttribute(attribute));
+                            }
+                            
layer.getLayerManager().fireFeaturesAttChanged(newFeatures,
+                                    FeatureEventType.ATTRIBUTES_MODIFIED, 
layer, oldFeatures);
+                        }
+
+                        public void unexecute() {
+                            Iterator<Feature> oldFeatIterator = 
oldFeatures.iterator();
+                            for (Feature f : 
layer.getFeatureCollectionWrapper().getFeatures()) {
+                                f.setAttribute(attribute, 
oldFeatIterator.next().getAttribute(attribute));
+                            }
+                            layer.getLayerManager().fireFeaturesAttChanged(
+                                    
layer.getFeatureCollectionWrapper().getFeatures(),
+                                    FeatureEventType.ATTRIBUTES_MODIFIED, 
layer, newFeatures);
+                        }
+                    };
+            command.execute();
+            
layer.getLayerManager().getUndoableEditReceiver().receive(command.toUndoableEdit());
+        } finally {
+            layer.getLayerManager().getUndoableEditReceiver().stopReceiving();
+        }
+    }
+
+    private Random rnd = new Random();
+    private char[] digitsArray = "0123456789".toCharArray();
+    private char[] hexaArray   = "0123456789ABCDEF".toCharArray();
+    private char[] asciiArray  = " 
!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~".toCharArray();
+    private String[] cityArray = new 
String[]{"Shanghai","Karachi","Beijing","Delhi","Lagos","Tianjin","Istanbul","Tokyo","Guangzhou","Mumbai","Moscow","S\xE3o
 Paulo","Shenzhen","Jakarta","Lahore","Seoul","Kinshasa","Cairo","Mexico 
City","Lima","London","New York City","Bengaluru","Bangkok","Ho Chi Minh 
City","Dongguan","Chongqing","5,473[d]","Nanjing","Tehran","Shenyang","Bogot\xE1","Ningbo","Hong
 Kong","Hanoi","Baghdad","Changsha","Dhaka","Wuhan","Hyderabad","Chennai","Rio 
de 
Janeiro","Faisalabad","Foshan","Zunyi","Santiago","Riyadh","Ahmedabad","Singapore","Shantou","Yangon","Saint
 Petersburg"};
+    private String[] surnames  = new 
String[]{"SMITH","JOHNSON","WILLIAMS","BROWN","JONES","MILLER","DAVIS","GARCIA","RODRIGUEZ","WILSON","MARTINEZ","ANDERSON","TAYLOR","THOMAS","HERNANDEZ","MOORE","MARTIN","JACKSON","THOMPSON","WHITE","LOPEZ","LEE","GONZALEZ","HARRIS","CLARK","LEWIS","ROBINSON","WALKER","PEREZ","HALL","YOUNG","ALLEN","SANCHEZ","WRIGHT","KING","SCOTT","GREEN","BAKER","ADAMS","NELSON","HILL","RAMIREZ","CAMPBELL","MITCHELL","ROBERTS","CARTER","PHILLIPS","EVANS","TURNER","TORRES","PARKER","COLLINS","EDWARDS","STEWART","FLORES","MORRIS","NGUYEN","MURPHY","RIVERA","COOK","ROGERS","MORGAN","PETERSON","COOPER","REED","BAILEY","BELL","GOMEZ","KELLY","HOWARD","WARD","COX","DIAZ","RICHARDSON","WOOD","WATSON","BROOKS","BENNETT","GRAY","JAMES","REYES","CRUZ","HUGHES","PRICE","MYERS","LONG","FOSTER","SANDERS","ROSS","MORALES","POWELL","SULLIVAN","RUSSELL","ORTIZ","JENKINS","GUTIERREZ","PERRY","BUTLER","BARNES","FISHER"};
+    private String[] firstNames = new 
String[]{"James","Mary","John","Patricia","Robert","Jennifer","Michael","Elizabeth","William","Linda","David","Barbara","Richard","Susan","Joseph","Jessica","Thomas","Margaret","Charles","Sarah","Christopher","Karen","Daniel","Nancy","Matthew","Betty","Anthony","Dorothy","Donald","Lisa","Mark","Sandra","Paul","Ashley","Steven","Kimberly","George","Donna","Kenneth","Carol","Andrew","Michelle","Joshua","Emily","Edward","Helen","Brian","Amanda","Kevin","Melissa","Ronald","Deborah","Timothy","Stephanie","Jason","Laura","Jeffrey","Rebecca","Ryan","Sharon","Gary","Cynthia","Jacob","Kathleen","Nicholas","Shirley","Eric","Amy","Stephen","Anna","Jonathan","Angela","Larry","Ruth","Scott","Brenda","Frank","Pamela","Justin","Virginia","Brandon","Katherine","Raymond","Nicole","Gregory","Catherine","Samuel","Christine","Benjamin","Samantha","Patrick","Debra","Jack","Janet","Alexander","Carolyn","Dennis","Rachel","Jerry","Heather"};
+
+    private String generate(int min, int max) {
+        int l = min + rnd.nextInt(1+Math.max(max-min, 0));
+        StringBuilder sb = new StringBuilder(l);
+        if (digits || hexa || ascii) {
+            char[] array = digits ? digitsArray : (hexa ? hexaArray : 
asciiArray);
+            for (int i = 0; i < l; i++) {
+                sb.append(array[rnd.nextInt(array.length)]);
+            }
+        } else if (cities) {
+            sb.append(cityArray[rnd.nextInt(cityArray.length)]);
+        } else if (names) {
+            
sb.append(firstNames[rnd.nextInt(cityArray.length)]).append("").append(surnames[rnd.nextInt(surnames.length)]);
+        }
+        return sb.toString();
+    }
+
+    private void setDialogValues(MultiInputDialog dialog, PlugInContext 
context) {
+        layer = context.getLayerableNamePanel().chooseEditableLayer();
+        dialog.addLayerComboBox(LAYER, layer, context.getLayerManager());
+        dialog.addLayerComboBox(LAYER, layer, null,
+                
AttributeTypeFilter.STRING_FILTER.filter(context.getLayerManager().getEditableLayers()));
+        dialog.addAttributeComboBox(ATTRIBUTE, LAYER, 
AttributeTypeFilter.STRING_FILTER, ATTRIBUTE_TT);
+        String GROUP = "group";
+        dialog.addSubTitle(LETTER_BASED);
+        dialog.addIntegerField(MIN_LENGTH, min, 12, MIN_LENGTH_TT);
+        dialog.addIntegerField(MAX_LENGTH, max, 12, MAX_LENGTH_TT);
+        dialog.addRadioButton(DIGITS, GROUP, digits, null);
+        dialog.addRadioButton(HEXA,   GROUP, hexa, null);
+        dialog.addRadioButton(ASCII,  GROUP, ascii, null);
+        dialog.addSubTitle(WORD_BASED);
+        dialog.addRadioButton(CITIES, GROUP, cities, null);
+        dialog.addRadioButton(NAMES,  GROUP, names, null);
+    }
+
+    private void getDialogValues(MultiInputDialog dialog) {
+        layer = dialog.getLayer(LAYER);
+        attribute = dialog.getText(ATTRIBUTE);
+        min = dialog.getInteger(MIN_LENGTH);
+        max = dialog.getInteger(MAX_LENGTH);
+        digits = dialog.getBoolean(DIGITS);
+        hexa   = dialog.getBoolean(HEXA);
+        ascii  = dialog.getBoolean(ASCII);
+        cities = dialog.getBoolean(CITIES);
+        names  = dialog.getBoolean(NAMES);
+    }
+}
+


------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Jump-pilot-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel

Reply via email to