From: <jrb...@us...> - 2009-08-18 20:33:19
|
Revision: 927 http://cishell.svn.sourceforge.net/cishell/?rev=927&view=rev Author: jrbibers Date: 2009-08-18 20:33:00 +0000 (Tue, 18 Aug 2009) Log Message: ----------- Previous commit included what is probably a poor assumption: that the list would not include the option to default. Now defaulting never adds an option to the list; it only checks for the presence of the given default in the given options and, if found, swaps it the front of the structure (as the called code treats the first option in the structure as the default). Modified Paths: -------------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/mutateParameter/DropdownMutator.java Added Paths: ----------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/ArrayUtilities.java Added: trunk/core/org.cishell.utilities/src/org/cishell/utilities/ArrayUtilities.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/ArrayUtilities.java (rev 0) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/ArrayUtilities.java 2009-08-18 20:33:00 UTC (rev 927) @@ -0,0 +1,13 @@ +package org.cishell.utilities; + +public class ArrayUtilities { + public static int indexOf(Object[] array, Object target) { + for (int ii = 0; ii < array.length; ii++) { + if (target.equals(array[ii])) { + return ii; + } + } + + return -1; + } +} Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/mutateParameter/DropdownMutator.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/mutateParameter/DropdownMutator.java 2009-08-18 19:52:44 UTC (rev 926) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/mutateParameter/DropdownMutator.java 2009-08-18 20:33:00 UTC (rev 927) @@ -1,9 +1,9 @@ package org.cishell.utilities.mutateParameter; import java.util.ArrayList; -import java.util.Collection; import java.util.List; +import org.cishell.utilities.ArrayUtilities; import org.osgi.service.metatype.AttributeDefinition; import org.osgi.service.metatype.ObjectClassDefinition; @@ -25,70 +25,51 @@ return ObjectClassDefinitionTransformer.transform(ocd, transforms); } - public void add(String id, Collection options, String defaultOption) { - List defaultedOptions = new ArrayList(); - defaultedOptions.add(defaultOption); - defaultedOptions.addAll(options); - - add(id, defaultedOptions); + public void add(String id, List options, String defaultOption) { + add(id, swapToFront(options, defaultOption)); } - public void add(String id, Collection options) { + public void add(String id, List options) { add(id, options, options); } - public void add(String id, Collection optionLabels, String defaultOptionLabel, Collection optionValues, String defaultOptionValue) { - List defaultedOptionLabels = new ArrayList(); - defaultedOptionLabels.add(defaultOptionLabel); - defaultedOptionLabels.addAll(optionLabels); - - List defaultedOptionValues = new ArrayList(); - defaultedOptionValues.add(defaultOptionValue); - defaultedOptionValues.addAll(optionValues); - - add(id, defaultedOptionLabels, defaultedOptionValues); + public void add(String id, + List optionLabels, + String defaultOptionLabel, + List optionValues, + String defaultOptionValue) { + add(id, + swapToFront(optionLabels, defaultOptionLabel), + swapToFront(optionValues, defaultOptionValue)); } - public void add(String id, Collection optionLabels, Collection optionValues) { - add(id, (String[]) optionLabels.toArray(new String[0]), (String[]) optionValues.toArray(new String[0])); + public void add(String id, List optionLabels, List optionValues) { + add(id, + (String[]) optionLabels.toArray(new String[0]), + (String[]) optionValues.toArray(new String[0])); } public void add(String id, String[] options, String defaultOption) { - String[] defaultedOptions = new String[options.length + 1]; - defaultedOptions[0] = defaultOption; - for (int ii = 0; ii < options.length; ii++) { - defaultedOptions[ii+1] = options[ii]; - } - - System.out.println("options = "); - for (int ii = 0; ii < defaultedOptions.length; ii++) { - System.out.println(defaultedOptions[ii]); - } - - add(id, defaultedOptions); + add(id, swapToFront(options, defaultOption)); } public void add(String id, String[] options) { add(id, options, options); } - public void add(final String id, final String[] optionLabels, String defaultOptionLabel, final String[] optionValues, String defaultOptionValue) { - String[] defaultedOptionLabels = new String[optionLabels.length + 1]; - defaultedOptionLabels[0] = defaultOptionLabel; - for (int ii = 0; ii < optionLabels.length; ii++) { - defaultedOptionLabels[ii+1] = optionLabels[ii]; - } - - String[] defaultedOptionValues = new String[optionValues.length + 1]; - defaultedOptionValues[0] = defaultOptionValue; - for (int ii = 0; ii < optionValues.length; ii++) { - defaultedOptionValues[ii+1] = optionValues[ii]; - } - - add(id, defaultedOptionLabels, defaultedOptionValues); + public void add(final String id, + final String[] optionLabels, + String defaultOptionLabel, + final String[] optionValues, + String defaultOptionValue) { + add(id, + swapToFront(optionLabels, defaultOptionLabel), + swapToFront(optionValues, defaultOptionValue)); } - public void add(final String id, final String[] optionLabels, final String[] optionValues) { + public void add(final String id, + final String[] optionLabels, + final String[] optionValues) { transforms.add( new NullDropdownTransformer() { public boolean shouldTransform(AttributeDefinition ad) { @@ -104,4 +85,27 @@ } }); } + + private static List swapToFront(List list, String item) { + if (list.contains(item)) { + int index = list.indexOf(item); + String displacedItem = (String) list.get(0); + list.set(0, item); + list.set(index, displacedItem); + } + + return list; + } + + private static String[] swapToFront(String[] array, String item) { + int index = ArrayUtilities.indexOf(array, item); + + if (index != -1) { + String displacedItem = array[0]; + array[0] = item; + array[index] = displacedItem; + } + + return array; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |