Omaha #4641 Volume browser sources sorting and null checks

Change-Id: Iabd25162a4ea115d7cf54145fb6c2496241a20e4

Former-commit-id: d1a484d56a9351c5b75c851435fc3d72d987ee5d
This commit is contained in:
Mark Peters 2015-07-10 15:09:38 -05:00
parent 0c8f6a7f9c
commit a7059ba80c
2 changed files with 94 additions and 12 deletions

View file

@ -22,6 +22,7 @@ import com.raytheon.viz.volumebrowser.vbui.VBMenuBarItemsMgr.ViewMenu;
* Dec 11, 2013 2602 bsteffen Remove ISerializableObject.
* Aug 14, 2014 3506 mapeters Added remove field and equals
* and hashCode functions.
* Jul 10, 2015 4641 mapeters Added toString().
*
* </pre>
*
@ -142,7 +143,6 @@ public class VbSource {
@Override
public boolean equals(Object that) {
if (that instanceof VbSource) {
if ((this.key.equals(((VbSource) that).getKey()) && (this
.getCategory().compareTo(((VbSource) that).getCategory()) == 0))) {
@ -156,6 +156,20 @@ public class VbSource {
public int hashCode() {
String newKey = key.concat(category);
return newKey.hashCode();
}
@Override
public String toString() {
StringBuilder sourceString = new StringBuilder("VbSource[");
sourceString.append("key=").append(key);
sourceString.append(", ");
sourceString.append("name=").append(name);
sourceString.append(", ");
sourceString.append("category=").append(category);
sourceString.append(", ");
sourceString.append("subCategory=").append(subCategory);
sourceString.append("]");
return sourceString.toString();
}
}

View file

@ -5,6 +5,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
@ -32,6 +33,9 @@ import com.raytheon.uf.common.menus.xml.CommonMenuContribution;
import com.raytheon.uf.common.menus.xml.CommonTitleImgContribution;
import com.raytheon.uf.common.menus.xml.CommonToolBarContribution;
import com.raytheon.uf.common.menus.xml.CommonToolbarSubmenuContribution;
import com.raytheon.uf.common.status.IUFStatusHandler;
import com.raytheon.uf.common.status.UFStatus;
import com.raytheon.uf.common.status.UFStatus.Priority;
import com.raytheon.viz.volumebrowser.vbui.VBMenuBarItemsMgr.ViewMenu;
/**
@ -52,6 +56,7 @@ import com.raytheon.viz.volumebrowser.vbui.VBMenuBarItemsMgr.ViewMenu;
* source files instead of one file, merge sources from
* different localization levels instead of overriding.
* Jul 07, 2015 4641 mapeters Fix/improve comparators for VbSource sorting.
* Jul 10, 2015 4641 mapeters Added check for sources with null key/category fields.
*
* </pre>
*
@ -62,8 +67,15 @@ import com.raytheon.viz.volumebrowser.vbui.VBMenuBarItemsMgr.ViewMenu;
@XmlRootElement
public class VbSourceList {
private static final transient IUFStatusHandler statusHandler = UFStatus
.getHandler(VbSourceList.class);
private final static IPathManager pm = PathManagerFactory.getPathManager();
/** The sources categories in order */
private static final String[] CATEGORIES = new String[] { "Volume",
"SfcGrid", "Local", "Point" };
/**
* Comparator for sorting sources (compares category, then subcategory, then
* name).
@ -83,10 +95,16 @@ public class VbSourceList {
int minParts = Math.min(cat1Parts.length, cat2Parts.length);
for (int i = 0; i < minParts; i++) {
if (!cat1Parts[i].equals(cat2Parts[i])) {
return comparatorString.compare(cat1Parts[i],
cat2Parts[i]);
/*
* Compare the drop down menu names differently to keep
* them in the order the NWS is used to.
*/
Comparator<String> comparator = (i == 0) ? categoryComparator
: stringComparator;
return comparator.compare(cat1Parts[i], cat2Parts[i]);
}
}
/*
* At this point, categories must match up to the end of the
* smaller of the two (e.g. SfcGrid and SfcGrid/RTOFS/forecast).
@ -94,10 +112,10 @@ public class VbSourceList {
* otherSource's next submenu level (RTOFS in the example).
*/
if (cat1Parts.length > minParts) {
return comparatorString.compare(cat1Parts[minParts],
return stringComparator.compare(cat1Parts[minParts],
source2.getName());
} else {
return comparatorString.compare(source1.getName(),
return stringComparator.compare(source1.getName(),
cat2Parts[minParts]);
}
}
@ -111,7 +129,7 @@ public class VbSourceList {
String subCat2 = source2.getSubCategory();
if (subCat1 != null && subCat2 != null) {
if (!subCat1.equals(subCat2)) {
return comparatorString.compare(subCat1, subCat2);
return stringComparator.compare(subCat1, subCat2);
}
} else if (subCat1 != null) {
return 1;
@ -120,7 +138,7 @@ public class VbSourceList {
}
// Compare names if categories and subcategories match.
return comparatorString.compare(source1.getName(),
return stringComparator.compare(source1.getName(),
source2.getName());
}
};
@ -130,7 +148,7 @@ public class VbSourceList {
* comparing numeric values (assumes there are no leading zeros in the
* numeric values).
*/
private static Comparator<String> comparatorString = new Comparator<String>() {
private static Comparator<String> stringComparator = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
int n1 = s1.length();
@ -202,6 +220,38 @@ public class VbSourceList {
}
};
/**
* Comparator for comparing the drop down menu names of the sources.
* Determines order based on {@link #CATEGORIES}.
*/
private static Comparator<String> categoryComparator = new Comparator<String>() {
@Override
public int compare(String cat1, String cat2) {
if (cat1.equals(cat2)) {
return 0;
}
for (String category : CATEGORIES) {
/*
* The categories aren't equal (checked for above), so whichever
* one appears first in the ordered categories list (CATEGORIES)
* should be returned as being smaller (making it also appear
* earlier in the sorted list of VbSources).
*/
if (cat1.equals(category)) {
return -1;
} else if (cat2.equals(category)) {
return 1;
}
}
/*
* If neither category is in the ordered list of expected
* categories, compare them alphabetically.
*/
return stringComparator.compare(cat1, cat2);
}
};
/**
* @deprecated This file path string exists only to support legacy overrides
* and should eventually be removed.
@ -311,6 +361,18 @@ public class VbSourceList {
List<VbSource> sources = JAXB.unmarshal(locFile.getFile(),
VbSourceList.class).getEntries();
if (sources != null) {
Iterator<VbSource> itr = sources.iterator();
while (itr.hasNext()) {
VbSource source = itr.next();
if (source.getCategory() == null
|| source.getKey() == null) {
statusHandler
.handle(Priority.WARN,
source
+ " was excluded from sources menu due to null key and/or category field.");
itr.remove();
}
}
allSources.addAll(sources);
}
}
@ -321,8 +383,11 @@ public class VbSourceList {
DatasetInfo info;
// Set containing sources to not be added to lists
Set<VbSource> removes = new HashSet<VbSource>();
for (int i = 0; i < allSources.size(); i++) {
VbSource source = allSources.get(i);
Iterator<VbSource> itr = allSources.iterator();
// The current index in allSources
int i = 0;
while (itr.hasNext()) {
VbSource source = itr.next();
// Set display names for sources
if (source.getName() == null) {
info = lookup.getInfo(source.getKey());
@ -331,11 +396,14 @@ public class VbSourceList {
if (source.getRemove()) {
// Add sources with remove tags to removal set and remove them.
removes.add(source);
allSources.remove(i--);
itr.remove();
} else if (removes.contains(source)
|| allSources.subList(0, i).contains(source)) {
// Remove sources in removal set and repeats
allSources.remove(i--);
itr.remove();
} else {
// Increment index in allSources if source wasn't removed
i++;
}
}
Collections.sort(allSources, comparator);