Browse Source

Can now sort by non-string fields in a sane manner

master
Chris Smith 15 years ago
parent
commit
7ca6ad5d38

+ 2
- 1
src/uk/co/md87/evetool/ui/data/BasicShipInfoSurrogate.java View File

@@ -23,14 +23,15 @@
23 23
 package uk.co.md87.evetool.ui.data;
24 24
 
25 25
 import java.awt.Image;
26
-import java.io.IOException;
27 26
 import java.net.URL;
28 27
 import java.util.concurrent.ExecutionException;
29 28
 import java.util.logging.Level;
30 29
 import java.util.logging.Logger;
30
+
31 31
 import javax.imageio.ImageIO;
32 32
 import javax.swing.ImageIcon;
33 33
 import javax.swing.SwingWorker;
34
+
34 35
 import uk.co.md87.evetool.api.wrappers.CharacterSheet;
35 36
 import uk.co.md87.evetool.api.wrappers.data.BasicShipInfo;
36 37
 import uk.co.md87.evetool.ui.listable.ListableImpl;

+ 29
- 1
src/uk/co/md87/evetool/ui/listable/ListableComparator.java View File

@@ -23,6 +23,7 @@
23 23
 package uk.co.md87.evetool.ui.listable;
24 24
 
25 25
 import java.util.Comparator;
26
+import java.util.List;
26 27
 import uk.co.md87.evetool.ui.listable.ListableConfig.ConfigElement;
27 28
 
28 29
 /**
@@ -42,7 +43,10 @@ public class ListableComparator implements Comparator<Object> {
42 43
 
43 44
     public int compare(final Object o1, final Object o2) {
44 45
         for (ConfigElement element : order) {
45
-            final int res = element.getValue(o1, parser).compareTo(element.getValue(o2, parser));
46
+            final Object op1 = element.getUnformattedValue(o1, parser);
47
+            final Object op2 = element.getUnformattedValue(o2, parser);
48
+
49
+            int res = doCompare(op1, op2);
46 50
 
47 51
             if (res != 0) {
48 52
                 return res;
@@ -52,4 +56,28 @@ public class ListableComparator implements Comparator<Object> {
52 56
         return 0;
53 57
     }
54 58
 
59
+    @SuppressWarnings("unchecked")
60
+    protected int doCompare(Object op1, Object op2) {
61
+        int res = 0;
62
+
63
+        if (op1 instanceof Comparable) {
64
+            res = ((Comparable) op1).compareTo(op2);
65
+        } else if (op1 instanceof List) {
66
+            final List<?> objs1 = (List<?>) op1;
67
+            final List<?> objs2 = (List<?>) op2;
68
+
69
+            for (int i = 0; i < objs1.size(); i++) {
70
+                res = doCompare(objs1.get(i), objs2.get(i));
71
+
72
+                if (res != 0) {
73
+                    break;
74
+                }
75
+            }
76
+        } else {
77
+            res = String.valueOf(op1).compareTo(String.valueOf(op2));
78
+        }
79
+
80
+        return res;
81
+    }
82
+
55 83
 }

+ 22
- 0
src/uk/co/md87/evetool/ui/listable/ListableConfig.java View File

@@ -22,7 +22,9 @@
22 22
 
23 23
 package uk.co.md87.evetool.ui.listable;
24 24
 
25
+import java.util.ArrayList;
25 26
 import java.util.Arrays;
27
+import java.util.List;
26 28
 
27 29
 /**
28 30
  *
@@ -39,6 +41,8 @@ public class ListableConfig implements Cloneable {
39 41
 
40 42
         String getValue(final Object target, final ListableParser parser);
41 43
 
44
+        Object getUnformattedValue(final Object target, final ListableParser parser);
45
+
42 46
     }
43 47
 
44 48
     public static class BasicConfigElement implements ConfigElement {
@@ -53,6 +57,10 @@ public class ListableConfig implements Cloneable {
53 57
             return parser.getValue(target, name);
54 58
         }
55 59
 
60
+        public Object getUnformattedValue(final Object target, ListableParser parser) {
61
+            return parser.getUnformattedValue(target, name);
62
+        }
63
+
56 64
         public String getName() {
57 65
             return name;
58 66
         }
@@ -70,6 +78,10 @@ public class ListableConfig implements Cloneable {
70 78
             return text;
71 79
         }
72 80
 
81
+        public Object getUnformattedValue(Object target, ListableParser parser) {
82
+            return text;
83
+        }
84
+
73 85
         public String getText() {
74 86
             return text;
75 87
         }
@@ -93,6 +105,16 @@ public class ListableConfig implements Cloneable {
93 105
             return builder.toString();
94 106
         }
95 107
 
108
+        public List<Object> getUnformattedValue(Object target, ListableParser parser) {
109
+            final List<Object> res = new ArrayList<Object>();
110
+
111
+            for (ConfigElement element : elements) {
112
+                res.add(element.getUnformattedValue(target, parser));
113
+            }
114
+
115
+            return res;
116
+        }
117
+
96 118
         public ConfigElement[] getElements() {
97 119
             return elements;
98 120
         }

+ 23
- 9
src/uk/co/md87/evetool/ui/listable/ListableParser.java View File

@@ -102,7 +102,7 @@ public class ListableParser {
102 102
         return methods.keySet();
103 103
     }
104 104
 
105
-    public String getValue(final Object target, final String name) {
105
+    public Object getUnformattedValue(final Object target, final String name) {
106 106
         if (methods.containsKey(name)) {
107 107
             Object result = target;
108 108
 
@@ -111,24 +111,38 @@ public class ListableParser {
111 111
                     result = method.invoke(result);
112 112
                 }
113 113
 
114
-                final Class<?> formatter = methods.get(name)
115
-                        .get(methods.get(name).size() - 1)
116
-                        .getAnnotation(Retrievable.class).formatWith();
117
-
118
-                return (String) formatter.getMethod("getValue", Object.class)
119
-                        .invoke(null, result);
114
+                return result;
120 115
             } catch (IllegalAccessException ex) {
121 116
                 LOGGER.log(Level.SEVERE, "Unable to retrieve value", ex);
122 117
             } catch (IllegalArgumentException ex) {
123 118
                 LOGGER.log(Level.SEVERE, "Unable to retrieve value", ex);
124 119
             } catch (InvocationTargetException ex) {
125 120
                 LOGGER.log(Level.SEVERE, "Unable to retrieve value", ex);
126
-            } catch (NoSuchMethodException ex) {
127
-                LOGGER.log(Level.SEVERE, "Unable to retrieve value", ex);
128 121
             }
129 122
         }
130 123
 
131 124
         return "Unknown";
132 125
     }
133 126
 
127
+    public String getValue(final Object target, final String name) {
128
+        try {
129
+            final Class<?> formatter = methods.get(name)
130
+                    .get(methods.get(name).size() - 1)
131
+                    .getAnnotation(Retrievable.class).formatWith();
132
+
133
+            return (String) formatter.getMethod("getValue", Object.class)
134
+                    .invoke(null, getUnformattedValue(target, name));
135
+        } catch (IllegalAccessException ex) {
136
+            LOGGER.log(Level.SEVERE, "Unable to retrieve value", ex);
137
+        } catch (IllegalArgumentException ex) {
138
+            LOGGER.log(Level.SEVERE, "Unable to retrieve value", ex);
139
+        } catch (InvocationTargetException ex) {
140
+            LOGGER.log(Level.SEVERE, "Unable to retrieve value", ex);
141
+        } catch (NoSuchMethodException ex) {
142
+            LOGGER.log(Level.SEVERE, "Unable to retrieve value", ex);
143
+        }
144
+
145
+        return "Unknown";
146
+    }
147
+
134 148
 }

Loading…
Cancel
Save