]> git.gag.com Git - debian/openrocket/commitdiff
Add slightly better sorting to the manufacturer and partno columns. It attempts...
authorkruland2607 <kruland2607@180e2498-e6e9-4542-8430-84ac67f01cd8>
Mon, 7 May 2012 01:34:23 +0000 (01:34 +0000)
committerkruland2607 <kruland2607@180e2498-e6e9-4542-8430-84ac67f01cd8>
Mon, 7 May 2012 01:34:23 +0000 (01:34 +0000)
git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@651 180e2498-e6e9-4542-8430-84ac67f01cd8

core/src/net/sf/openrocket/gui/dialogs/preset/ComponentPresetTable.java
core/src/net/sf/openrocket/util/AlphanumComparator.java [new file with mode: 0644]

index 984147ffa9bf94d625b6fc243f45c3292678b16c..1c08f09be95e4e74490c40602f688e0edf643cd1 100644 (file)
@@ -10,14 +10,12 @@ import java.util.List;
 
 import javax.swing.JCheckBoxMenuItem;
 import javax.swing.JMenu;
-import javax.swing.JMenuItem;
 import javax.swing.JPopupMenu;
 import javax.swing.JTable;
 import javax.swing.ListSelectionModel;
 import javax.swing.RowFilter;
 import javax.swing.RowSorter.SortKey;
 import javax.swing.SortOrder;
-import javax.swing.event.ListSelectionEvent;
 import javax.swing.table.AbstractTableModel;
 import javax.swing.table.JTableHeader;
 import javax.swing.table.TableColumn;
@@ -30,6 +28,7 @@ import net.sf.openrocket.preset.TypedKey;
 import net.sf.openrocket.startup.Application;
 import net.sf.openrocket.unit.Unit;
 import net.sf.openrocket.unit.UnitGroup;
+import net.sf.openrocket.util.AlphanumComparator;
 
 public class ComponentPresetTable extends JTable {
 
@@ -46,31 +45,6 @@ public class ComponentPresetTable extends JTable {
                this.presets = presets;
                this.columns = new ComponentPresetTableColumn[ComponentPreset.orderedKeyList.size()+1];
 
-               tableColumnModel = new XTableColumnModel();
-
-               /*
-                * Set up the Column Table model
-                */
-               columns[0] = new ComponentPresetTableColumn.Favorite(0);
-               tableColumnModel.addColumn(columns[0]);
-
-               List<TableColumn> hiddenColumns = new ArrayList<TableColumn>();
-               {
-                       int index = 1;
-                       for (final TypedKey<?> key: ComponentPreset.orderedKeyList ) {
-                               if ( key.getType() == Double.class && key.getUnitGroup() != null ) {
-                                       columns[index] = new ComponentPresetTableColumn.DoubleWithUnit((TypedKey<Double>)key,index);
-                               } else {
-                                       columns[index] = new ComponentPresetTableColumn.Parameter(key,index);
-                               }
-                               tableColumnModel.addColumn(columns[index]);
-                               if ( visibleColumnKeys.indexOf(key) < 0 ) {
-                                       hiddenColumns.add(columns[index]);
-                               }
-                               index ++;
-                       }
-               }
-
 
                tableModel = new AbstractTableModel() {
                        final ComponentPresetTableColumn[] myColumns = columns;
@@ -111,18 +85,47 @@ public class ComponentPresetTable extends JTable {
 
                };
 
+
+               sorter = new TableRowSorter<TableModel>(tableModel);
+
+               tableColumnModel = new XTableColumnModel();
+
+               /*
+                * Set up the Column Table model, and customize the sorting.
+                */
+               columns[0] = new ComponentPresetTableColumn.Favorite(0);
+               tableColumnModel.addColumn(columns[0]);
+
+               List<TableColumn> hiddenColumns = new ArrayList<TableColumn>();
+               {
+                       int index = 1;
+                       for (final TypedKey<?> key: ComponentPreset.orderedKeyList ) {
+                               if ( key.getType() == Double.class && key.getUnitGroup() != null ) {
+                                       columns[index] = new ComponentPresetTableColumn.DoubleWithUnit((TypedKey<Double>)key,index);
+                               } else {
+                                       columns[index] = new ComponentPresetTableColumn.Parameter(key,index);
+                               }
+                               tableColumnModel.addColumn(columns[index]);
+                               if ( key == ComponentPreset.MANUFACTURER || key == ComponentPreset.PARTNO ) {
+                                       sorter.setComparator(index, new AlphanumComparator());
+                               }
+                               if ( visibleColumnKeys.indexOf(key) < 0 ) {
+                                       hiddenColumns.add(columns[index]);
+                               }
+                               index ++;
+                       }
+               }
+
                this.setAutoCreateColumnsFromModel(false);
                this.setColumnModel( tableColumnModel );
                this.setModel(tableModel);
                this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+               this.setRowSorter(sorter);
 
                for ( TableColumn hiddenColumn : hiddenColumns ) {
                        tableColumnModel.setColumnVisible(hiddenColumn, false);
                }
 
-               sorter = new TableRowSorter<TableModel>(tableModel);
-               this.setRowSorter(sorter);
-
                JTableHeader header = this.getTableHeader();
                
                header.setReorderingAllowed(true);
diff --git a/core/src/net/sf/openrocket/util/AlphanumComparator.java b/core/src/net/sf/openrocket/util/AlphanumComparator.java
new file mode 100644 (file)
index 0000000..c7a7b48
--- /dev/null
@@ -0,0 +1,139 @@
+/*\r
+ * The Alphanum Algorithm is an improved sorting algorithm for strings\r
+ * containing numbers.  Instead of sorting numbers in ASCII order like\r
+ * a standard sort, this algorithm sorts numbers in numeric order.\r
+ *\r
+ * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com\r
+ *\r
+ *\r
+ * This library is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU Lesser General Public\r
+ * License as published by the Free Software Foundation; either\r
+ * version 2.1 of the License, or any later version.\r
+ *\r
+ * This library is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+ * Lesser General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU Lesser General Public\r
+ * License along with this library; if not, write to the Free Software\r
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA\r
+ *\r
+ */\r
+\r
+/*\r
+ * Subsequently this code had been hacked up to make it genericized and support\r
+ * folding upper/lower case.\r
+ */\r
+package net.sf.openrocket.util;\r
+\r
+import java.text.Collator;\r
+import java.util.Comparator;\r
+\r
+/**\r
+ * This is an updated version with enhancements made by Daniel Migowski,\r
+ * Andre Bogus, and David Koelle\r
+ *\r
+ * To convert to use Templates (Java 1.5+):\r
+ *   - Change "implements Comparator" to "implements Comparator<String>"\r
+ *   - Change "compare(Object o1, Object o2)" to "compare(String s1, String s2)"\r
+ *   - Remove the type checking and casting in compare().\r
+ *\r
+ * To use this class:\r
+ *   Use the static "sort" method from the java.util.Collections class:\r
+ *   Collections.sort(your list, new AlphanumComparator());\r
+ */\r
+public class AlphanumComparator implements Comparator<String>\r
+{\r
+\r
+       private static final Collator sorter = Collator.getInstance();\r
+       static {\r
+               sorter.setStrength(Collator.TERTIARY);\r
+               sorter.setDecomposition(Collator.CANONICAL_DECOMPOSITION);\r
+       }\r
+\r
+       private final boolean isDigit(char ch)\r
+       {\r
+               return ch >= 48 && ch <= 57;\r
+       }\r
+\r
+       /** Length of string is passed in for improved efficiency (only need to calculate it once) **/\r
+       private final String getChunk(String s, int slength, int marker)\r
+       {\r
+               StringBuilder chunk = new StringBuilder();\r
+               char c = s.charAt(marker);\r
+               chunk.append(c);\r
+               marker++;\r
+               if (isDigit(c))\r
+               {\r
+                       while (marker < slength)\r
+                       {\r
+                               c = s.charAt(marker);\r
+                               if (!isDigit(c))\r
+                                       break;\r
+                               chunk.append(c);\r
+                               marker++;\r
+                       }\r
+               } else\r
+               {\r
+                       while (marker < slength)\r
+                       {\r
+                               c = s.charAt(marker);\r
+                               if (isDigit(c))\r
+                                       break;\r
+                               chunk.append(c);\r
+                               marker++;\r
+                       }\r
+               }\r
+               return chunk.toString();\r
+       }\r
+\r
+       @Override\r
+       public int compare(String s1, String s2)\r
+       {\r
+\r
+               int thisMarker = 0;\r
+               int thatMarker = 0;\r
+               int s1Length = s1.length();\r
+               int s2Length = s2.length();\r
+\r
+               while (thisMarker < s1Length && thatMarker < s2Length)\r
+               {\r
+                       String thisChunk = getChunk(s1, s1Length, thisMarker);\r
+                       thisMarker += thisChunk.length();\r
+\r
+                       String thatChunk = getChunk(s2, s2Length, thatMarker);\r
+                       thatMarker += thatChunk.length();\r
+\r
+                       // If both chunks contain numeric characters, sort them numerically\r
+                       int result = 0;\r
+                       if (isDigit(thisChunk.charAt(0)) && isDigit(thatChunk.charAt(0)))\r
+                       {\r
+                               // Simple chunk comparison by length.\r
+                               int thisChunkLength = thisChunk.length();\r
+                               result = thisChunkLength - thatChunk.length();\r
+                               // If equal, the first different number counts\r
+                               if (result == 0)\r
+                               {\r
+                                       for (int i = 0; i < thisChunkLength; i++)\r
+                                       {\r
+                                               result = thisChunk.charAt(i) - thatChunk.charAt(i);\r
+                                               if (result != 0)\r
+                                               {\r
+                                                       return result;\r
+                                               }\r
+                                       }\r
+                               }\r
+                       } else\r
+                       {\r
+                               result = sorter.compare(thisChunk, thatChunk);\r
+                       }\r
+\r
+                       if (result != 0)\r
+                               return result;\r
+               }\r
+\r
+               return s1Length - s2Length;\r
+       }\r
+}\r