Table Widget: add sorting table item routine
authorjihye424.kim <jihye424.kim@samsung.com>
Sun, 6 Sep 2015 05:13:46 +0000 (14:13 +0900)
committerjihye424.kim <jihye424.kim@samsung.com>
Tue, 8 Sep 2015 05:09:20 +0000 (14:09 +0900)
- modify default sort listener
- using sort direction and sort column when drawing column

Change-Id: Icd3791ca9cc9ee4c26c03d8828794e5c26c4010c
Signed-off-by: jihye424.kim <jihye424.kim@samsung.com>
resource/table/icon_column_down.png [moved from resource/table/up.png with 94% similarity]
resource/table/icon_column_up.png [moved from resource/table/down.png with 94% similarity]
src/org/tizen/emulator/manager/ui/table/ImageResources.java
src/org/tizen/emulator/manager/ui/table/Table.java
src/org/tizen/emulator/manager/ui/table/TableColumn.java
src/org/tizen/emulator/manager/ui/table/TableItemComparable.java [new file with mode: 0644]

similarity index 94%
rename from resource/table/up.png
rename to resource/table/icon_column_down.png
index ce162c3..4c0f744 100644 (file)
Binary files a/resource/table/up.png and b/resource/table/icon_column_down.png differ
similarity index 94%
rename from resource/table/down.png
rename to resource/table/icon_column_up.png
index 6da26c6..4dc77b5 100644 (file)
Binary files a/resource/table/down.png and b/resource/table/icon_column_up.png differ
index d69f244..ac4b6e4 100644 (file)
@@ -41,8 +41,8 @@ public class ImageResources {
        public static Image CHECKBOX_UNCHECKED_NML = getImage("btn_checkbox_unchecked_nml", "png");
        public static Image CHECKBOX_CHECKED_HOVER = getImage("btn_checkbox_checked_hover", "png");
        public static Image CHECKBOX_CHECKED_NML = getImage("btn_checkbox_checked_nml", "png");
-       public static Image SORT_INDICATOR_UP = getImage("up", "png");
-       public static Image SORT_INDICATOR_DOWN = getImage("down", "png");
+       public static Image SORT_INDICATOR_UP = getImage("icon_column_up", "png");
+       public static Image SORT_INDICATOR_DOWN = getImage("icon_column_down", "png");
 
        public static Image getImage(String name, String extension) {
                Image i = null;
index c7a0b5f..1b8469d 100644 (file)
@@ -31,6 +31,7 @@
 package org.tizen.emulator.manager.ui.table;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import org.eclipse.swt.SWT;
@@ -576,9 +577,8 @@ public class Table extends TableScrolledComposite {
                if (length != 0 && ((getStyle() & SWT.SINGLE) == 0
                                || length <= 1)) {
                        setFocusIndex(indices[0]);
+                       rearrangeItems(indices[0]);
                }
-
-               rearrangeItems(indices[0]);
        }
 
        public void setSelection(TableItem[] items) {}
@@ -996,7 +996,12 @@ public class Table extends TableScrolledComposite {
                if (column != null && column.isDisposed()) {
                        SWT.error(SWT.ERROR_INVALID_ARGUMENT);
                }
+               TableColumn backupSortColumn = sortColumn;
                sortColumn = column;
+
+               if (backupSortColumn != null) {
+                       backupSortColumn.redraw();
+               }
        }
 
        public int getSortDirection() {
@@ -1004,6 +1009,12 @@ public class Table extends TableScrolledComposite {
                return sortDirection;
        }
 
+       /**
+        * SWT.UP: ascending order
+        * SWT.DOWN: descending order
+        * SWT.NONE
+        * @param direction
+        */
        public void setSortDirection(int direction) {
                checkWidget();
                if ((direction & (SWT.UP | SWT.DOWN)) != 0
@@ -1326,71 +1337,83 @@ public class Table extends TableScrolledComposite {
                }
        };
 
+       static class ItemStringPair extends TableItemComparable {
+               public ItemStringPair(TableItem item, int index, int direction) {
+                       super(item, index, direction);
+               }
+
+               @Override
+               public int compareTo(Object o) {
+                       TableItem target = ((ItemStringPair)o).item;
+                       String targetStr = target.getText(index);
+                       String sourceStr = item.getText(index);
+
+                       if (direction == SWT.UP) {
+                               return sourceStr.compareTo(targetStr);
+                       } else {
+                               return targetStr.compareTo(sourceStr);
+                       }
+               }
+       }
+
        private Listener sortListener = new Listener() {
 
                @Override
                public void handleEvent(Event event) {
                        TableColumn column = (TableColumn)event.widget;
-                       int index = columns.indexOf(column);
-                       if (index == -1) {
-                               return;
-                       }
+                       settingSortColumn(column);
+                       defaultSortTableItem(column);
+               }
+       };
 
-                       int dir = getSortDirection();
+       public void settingSortColumn(TableColumn column) {
 
-                       if (getSortColumn() != column) {
-                               setSortColumn(column);
-                               dir = SWT.UP;
-                       }
+               int dir = getSortDirection();
+               if (dir == SWT.DOWN || dir == SWT.NONE) {
+                       dir = SWT.UP;
+               } else {
+                       dir = SWT.DOWN;
+               }
 
-                       TableItem[] newItems = new TableItem[itemCount];
-                       int[] newSelection = new int[selection.length];
-                       newItems[0] = items[0];
-                       for (int i = 1; i < items.length; i++) {
-                               if (items[i] == null) {
-                                       continue;
-                               }
-                               String value1 = items[i].getText(index);
-                               if (value1 == null) {
-                                       continue;
-                               }
-                               int j = i - 1;
-
-                               while (j >= 0) {
-                                       String value2 = newItems[j].getText(index);
-                                       int compare = value1.compareTo(value2);
-                                       if (compare < 0 && dir == SWT.UP) {
-                                               newItems[j + 1] = newItems[j];
-                                               j--;
-                                       } else if (compare > 0 && dir == SWT.DOWN){
-                                               newItems[j + 1] = newItems[j];
-                                               j--;
-                                       } else {
-                                               break;
-                                       }
-                               }
-                               newItems[j + 1] = items[i];
-                       }
-                       adjustItemIndices(0);
+               // set new sort column
+               if (getSortColumn() != column) {
+                       setSortColumn(column);
+                       dir = SWT.UP;
+               }
 
-                       for (int in = 0; in < selection.length; in++) {
-                               TableItem item = items[selection[in]];
-                               for (int i = 0; i < newItems.length; i++) {
-                                       if (newItems[i] == item) {
-                                               newSelection[in] = i;
-                                               break;
-                                       }
-                               }
+               setSortDirection(dir);
+       }
+
+       public void defaultSortTableItem(TableColumn column) {
+               int index = columns.indexOf(column);
+               if (index == -1) {
+                       return;
+               }
+
+               TableItem[] newItems = new TableItem[itemCount];
+               int dir = getSortDirection();
+
+               TableItemComparable[] pairs = column.getTableItemPairs();
+               if (pairs == null || pairs.length != itemCount) {
+                       pairs = new ItemStringPair[itemCount];
+                       for (int i = 0; i < itemCount; i++) {
+                               pairs[i] = new ItemStringPair(items[i], index, dir);
                        }
-                       items = newItems;
-                       dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
-                       setSortDirection(dir);
-                       setSelection(newSelection);
-                       showSelection();
                }
-       };
 
-       public Listener getDifaultSortListener() {
+               Arrays.sort(pairs);
+
+               for (int i = 0; i < itemCount; i++) {
+                       newItems[i] = pairs[i].getTableItem();
+               }
+
+               items = newItems;
+               adjustItemIndices(0);
+
+               showSelection();
+       }
+
+       public Listener getDefaultSortListener() {
                checkWidget();
                return sortListener;
        }
index d2d27d3..9da634a 100644 (file)
@@ -55,10 +55,12 @@ import org.eclipse.swt.widgets.TypedListener;
 
 public class TableColumn extends Canvas {
        private static final int SORT_INDICATOR_WIDTH = 10;
+       private static final int SORT_INDICATOR_SPACING = 2;
        private static final int SPACING = 5;
+       private static final int LINE_WIDTH = 1;
        // minimum size
        private static final int RESIZABLE_WIDTH = 10;
-       private static final int MIN_WIDTH = 20;
+       private static final int MIN_WIDTH = 30;
        private static final int HEADERPADDING_WIDTH = 30;
 
        private final Table parent;
@@ -96,7 +98,6 @@ public class TableColumn extends Canvas {
        private String drawingText;
        private boolean needRedrawingText = true;
 
-       //private SortButton sortButton;;
        private Image sortIndicator;
 
        public TableColumn(Table parent, int style) {
@@ -340,13 +341,13 @@ public class TableColumn extends Canvas {
 
                gc.setForeground(ColorResources.DEFAULT_STROKE);
                gc.setLineWidth(1);
-               gc.drawLine(rect.width-1, 0, rect.width-1, rect.height);
+               gc.drawLine(rect.width - LINE_WIDTH , 0, rect.width - LINE_WIDTH, rect.height);
 
                gc.setForeground(isHoverState ? selectedForeground : foreground);
 
                if (parent.getSortColumn() == this) {
                        // sort column
-                       int x = rect.x + (rect.width - SORT_INDICATOR_WIDTH - SPACING);
+                       int x = rect.x + (rect.width - SORT_INDICATOR_WIDTH - SORT_INDICATOR_SPACING);
                        if (parent.getSortDirection() == SWT.UP) {
                                sortIndicator = ImageResources.SORT_INDICATOR_UP;
                        } else if (parent.getSortDirection() == SWT.DOWN){
@@ -356,16 +357,20 @@ public class TableColumn extends Canvas {
                        }
                        if (sortIndicator != null) {
                                Rectangle bounds = sortIndicator.getBounds();
-                               //sortButton.setBounds(x, 0, SORT_INDICATOR_WIDTH, rect.height);
                                gc.drawImage(sortIndicator, x, rect.y + (rect.height- bounds.height) / 2);
                        }
                }
+
                if (image != null || !text.isEmpty()) {
                        //
                        int imageX = rect.x;
                        int textX  = rect.x;
                        int imageY = 0;
                        int textY  = 0;
+                       int width = rect.width;
+                       if (sortIndicator != null) {
+                               width -= SORT_INDICATOR_WIDTH - SORT_INDICATOR_SPACING;
+                       }
 
                        Point imageEX = new Point(0, 0);
                        if (image != null) {
@@ -376,8 +381,8 @@ public class TableColumn extends Canvas {
                        if (needRedrawingText) {
                                int textWidth = TextSizeUtil.textExtent(getFont(), text);
                                drawingText = text;
-                               if (textWidth > (rect.width - imageEX.x - 10)) {
-                                       while (textWidth > (rect.width - 10) - TextSizeUtil.OMITLEN && drawingText.length() > 1) {
+                               if (textWidth > (width - imageEX.x - 10)) {
+                                       while (textWidth > (width - 10) - TextSizeUtil.OMITLEN && drawingText.length() > 1) {
                                                drawingText = drawingText.substring(0, drawingText.length() - 1);
                                                textWidth = TextSizeUtil.textExtent(getFont(), drawingText);
                                        }
@@ -394,14 +399,14 @@ public class TableColumn extends Canvas {
                                break;
                        case SWT.RIGHT:
                                imageX += (image != null
-                               ? rect.width - imageEX.x - SPACING - textWidth - 10
-                                               : rect.width - textWidth - 10);
+                               ? width - imageEX.x - SPACING - textWidth - 10
+                                               : width - textWidth - 10);
                                textX += imageX + imageEX.x + SPACING;
                                break;
                        case SWT.CENTER:
                                imageX += (image != null
-                               ? rect.x + ((rect.width - image.getImageData().width - textWidth) / 2)
-                                               : rect.x + ((rect.width - textWidth) / 2));
+                               ? rect.x + ((width - image.getImageData().width - textWidth) / 2)
+                                               : rect.x + ((width - textWidth) / 2));
                                textX += imageX + imageEX.x;
                                break;
                        }
@@ -523,6 +528,7 @@ public class TableColumn extends Canvas {
                                Event ev = new Event();
                                ev.widget = e.widget;
                                notifyListeners(SWT.Selection, ev);
+                               redraw();
                        }
                }
 
@@ -610,4 +616,13 @@ public class TableColumn extends Canvas {
                        }
                }
        }
+
+       private TableItemComparable[] pairs;
+       public void setTableItemPairs(TableItemComparable[] pairs) {
+               this.pairs = pairs;
+       }
+
+       public TableItemComparable[] getTableItemPairs() {
+               return pairs;
+       }
 }
diff --git a/src/org/tizen/emulator/manager/ui/table/TableItemComparable.java b/src/org/tizen/emulator/manager/ui/table/TableItemComparable.java
new file mode 100644 (file)
index 0000000..3d9d4ad
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Emulator Manager
+ *
+ * Copyright (C) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * JiHye Kim <jihye424.kim@samsung.com>
+ * Minkee Lee <minkee.lee@samsung.com>
+ * SeokYeon Hwang <syeon.hwang@samsung.com>
+ * Sangho Park <sangho.p@samsung.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ */
+
+package org.tizen.emulator.manager.ui.table;
+
+public abstract class TableItemComparable implements Comparable<Object> {
+       protected TableItem item;
+       protected int index;
+       protected int direction;
+
+       public TableItemComparable(TableItem item, int index, int direction) {
+               this.item = item;
+               this.index = index;
+               this.direction = direction;
+       }
+
+       public TableItem getTableItem() {
+               return item;
+       }
+}