[Thinlet-development] thinlet/src/java/thinlet/examples/demo Demo.java,1.3,1.4 demo.xml,1.3,1.4 demo
Brought to you by:
bajzat
From: Andrzej B. <ab...@us...> - 2004-06-07 23:10:35
|
Update of /cvsroot/thinlet/thinlet/src/java/thinlet/examples/demo In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6674 Modified Files: Demo.java demo.xml demodialog.xml Log Message: Show off new features: resizeable and sortable table columns, and a close action for dialogs. Index: demo.xml =================================================================== RCS file: /cvsroot/thinlet/thinlet/src/java/thinlet/examples/demo/demo.xml,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- demo.xml 19 Apr 2004 17:42:30 -0000 1.3 +++ demo.xml 7 Jun 2004 23:10:25 -0000 1.4 @@ -116,25 +116,25 @@ <separator/> <menuitem text="three"/> </popupmenu> - <header> + <header action="sortAction(this)"> <column text="Column" icon="/icons/bookmarks.gif" width="120" /> <column text="Column" alignment="center" /> <column text="Column" alignment="center" /> </header> <row> - <cell text="Cell" /> - <cell text="Cell" /> - <cell text="Cell" icon="/icons/bookmarks.gif" /> + <cell text="Cell A" /> + <cell text="Cell B" /> + <cell text="Cell C" icon="/icons/bookmarks.gif" /> </row> <row selected="true"> - <cell text="Cell" icon="/icons/bookmarks.gif" /> - <cell text="Cell" /> - <cell text="Cell" /> + <cell text="Cell B" icon="/icons/bookmarks.gif" /> + <cell text="Cell C" /> + <cell text="Cell A" /> </row> <row> - <cell text="Cell" /> - <cell text="Cell" icon="/icons/bookmarks.gif" enabled="false" /> - <cell text="Cell" /> + <cell text="Cell C" /> + <cell text="Cell A" icon="/icons/bookmarks.gif" enabled="false" /> + <cell text="Cell B" /> </row> </table> </splitpane> Index: Demo.java =================================================================== RCS file: /cvsroot/thinlet/thinlet/src/java/thinlet/examples/demo/Demo.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- Demo.java 19 Apr 2004 17:42:30 -0000 1.3 +++ Demo.java 7 Jun 2004 23:10:25 -0000 1.4 @@ -260,6 +260,62 @@ setBoolean(delete, "enabled", false); } + public void sortAction(Object header) { + int idx = getSelectedIndex(header); + Object column = getSelectedItem(header); + String sort = getChoice(column, "sort"); + if (sort == "none") return; + Object table = getParent(header); + Object[] rows = getItems(table); + removeAll(table); + sortArray(rows, new RowComparator(this, idx, sort == "ascent")); + for (int i = 0; i < rows.length; i++) add(table, rows[i]); + repaint(table); + } + + /** + * Basic sort for small arrays. + */ + private void sortArray(Object[] arr, Comparator comp) { + + boolean swapped; + + do { + swapped = false; + for (int i = 0; i < arr.length - 1; i++) { + if (comp.compare(arr[i], arr[i + 1]) > 0) { + Object temp = arr[i + 1]; + arr[i + 1] = arr[i]; + arr[i] = temp; + swapped = true; + } + } + } while (swapped); + } + + + static class RowComparator implements Comparator { + private boolean ascending = false; + private int column = 0; + private Thinlet thinlet = null; + + public RowComparator(Thinlet thinlet, int column, boolean ascending) { + this.thinlet = thinlet; + this.column = column; + this.ascending = ascending; + } + + public int compare(Object row1, Object row2) { + Object cell1 = thinlet.getItem(row1, column); + Object cell2 = thinlet.getItem(row2, column); + String s1 = thinlet.getString(cell1, "text"); + String s2 = thinlet.getString(cell2, "text"); + if (s1 == null) return ascending? -1 : 1; + if (s2 == null) return ascending? 1 : -1; + return (ascending ? 1 : -1 ) * s1.compareTo(s2); + } + } + /** * */ Index: demodialog.xml =================================================================== RCS file: /cvsroot/thinlet/thinlet/src/java/thinlet/examples/demo/demodialog.xml,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- demodialog.xml 26 Jun 2003 15:21:55 -0000 1.1 +++ demodialog.xml 7 Jun 2004 23:10:25 -0000 1.2 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="ISO-8859-1"?> -<dialog text="Find" icon="/icons/find.gif" modal="true" +<dialog text="Find" icon="/icons/find.gif" modal="true" close="remove(this)" columns="4" top="4" left="4" bottom="4" right="4" gap="4"> <label text="Find what:" /> <combobox name="ch_what" colspan="2" valign="center" /> |