Table: change function to calcurate string size
authorjihye424.kim <jihye424.kim@samsung.com>
Wed, 4 Nov 2015 11:02:14 +0000 (20:02 +0900)
committerjihye424.kim <jihye424.kim@samsung.com>
Wed, 4 Nov 2015 11:02:14 +0000 (20:02 +0900)
- remove textExtend function
-- use only stringExtend function
- set max width to table column

Change-Id: I45c564e3003bd51e1f642a5bb4d477202c36db95
Signed-off-by: jihye424.kim <jihye424.kim@samsung.com>
src/org/tizen/emulator/manager/ui/table/TableColumn.java
src/org/tizen/emulator/manager/ui/table/TableItem.java
src/org/tizen/emulator/manager/ui/table/TableItemCell.java
src/org/tizen/emulator/manager/ui/table/TextSizeUtil.java

index 9da634a..9f133e3 100644 (file)
@@ -67,7 +67,8 @@ public class TableColumn extends Canvas {
        private int style = 0;
 
        private int width;
-       private String text;
+       private int maxWidth = 200;
+       private String text = "";
        private Image image;
 
        private boolean resizable = false;
@@ -186,6 +187,11 @@ public class TableColumn extends Canvas {
        public void pack () {
                checkWidget();
                int w = getPreferredWidth();
+               //
+               if (w > maxWidth) {
+                       w = maxWidth;
+               }
+
                if (w != width) {
                        setWidth(w);
                }
@@ -326,6 +332,14 @@ public class TableColumn extends Canvas {
                this.selectedFont = font;
        }
 
+       public int getMaxWidth() {
+               return maxWidth;
+       }
+
+       public void setMaxWidth(int width) {
+               this.maxWidth = width;
+       }
+
        void relesaeParent() {
                parent.destroyColumn(this);
        }
@@ -379,19 +393,19 @@ public class TableColumn extends Canvas {
                        }
 
                        if (needRedrawingText) {
-                               int textWidth = TextSizeUtil.textExtent(getFont(), text);
+                               int textWidth = TextSizeUtil.stringExtend(getFont(), text).x;
                                drawingText = text;
                                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);
+                                               textWidth = TextSizeUtil.stringExtend(getFont(), drawingText).x;
                                        }
                                        drawingText = drawingText + TextSizeUtil.OMIT;
                                }
                                needRedrawingText = false;
                        }
 
-                       int textWidth = TextSizeUtil.textExtent(getFont(), drawingText);
+                       Point textSize = TextSizeUtil.stringExtend(getFont(), drawingText);
                        switch (getAlignment()) {
                        case SWT.LEFT:
                                imageX += (image != null ? SPACING : 0);
@@ -399,14 +413,14 @@ public class TableColumn extends Canvas {
                                break;
                        case SWT.RIGHT:
                                imageX += (image != null
-                               ? width - imageEX.x - SPACING - textWidth - 10
-                                               : width - textWidth - 10);
+                               ? width - imageEX.x - SPACING - textSize.x - 10
+                                               : width - textSize.x - 10);
                                textX += imageX + imageEX.x + SPACING;
                                break;
                        case SWT.CENTER:
                                imageX += (image != null
-                               ? rect.x + ((width - image.getImageData().width - textWidth) / 2)
-                                               : rect.x + ((width - textWidth) / 2));
+                               ? rect.x + ((width - image.getImageData().width - textSize.x) / 2)
+                                               : rect.x + ((width - textSize.x) / 2));
                                textX += imageX + imageEX.x;
                                break;
                        }
@@ -416,8 +430,7 @@ public class TableColumn extends Canvas {
                                gc.drawImage(image, imageX, imageY);
                        }
 
-                       int fontHeight = gc.getFontMetrics().getHeight();
-                       textY = rect.y + (rect.height - fontHeight) / 2 - 1;
+                       textY = rect.y + (rect.height - textSize.y) / 2 - 1;
 
                        if (!text.isEmpty()) {
                                gc.setFont(isHoverState? selectedFont : font);
@@ -511,7 +524,8 @@ public class TableColumn extends Canvas {
                @Override
                public void mouseDoubleClick(MouseEvent e) {
                        if (isResizeableState) {
-                               setWidth(getPreferredWidth());
+                               //setWidth(getPreferredWidth());
+                               pack();
                        }
                }
 
@@ -578,11 +592,7 @@ public class TableColumn extends Canvas {
                int result = 0;
                Font font = getFont();
                if (text != null && text.length() >= 0) {
-                       if (text.indexOf('\n') != -1) {
-                               result = TextSizeUtil.stringExtend(font,  text);
-                       } else {
-                               result = TextSizeUtil.textExtent(font, text);
-                       }
+                       result = TextSizeUtil.stringExtend(font,  text).x;
 
                        Image image = getImage();
                        if (image != null) {
index 31e8251..20b6c4f 100644 (file)
@@ -504,7 +504,7 @@ public class TableItem extends Canvas {
                        ensureData(index, count);
                        if (!text.equals(dataList[index].text)) {
                                dataList[index].text = text;
-                               dataList[index].textWidth = TextSizeUtil.textExtent(font, text);
+                               dataList[index].textWidth = TextSizeUtil.stringExtend(font, text).x;
                                dataList[index].needRedrawingText = true;
                                if (parent.getColumnCount() == 0) {
                                        parent.updateScrollBars(true);
@@ -758,10 +758,10 @@ public class TableItem extends Canvas {
        private int getTextWidth(int index, Font font) {
                if (hasData(index)) {
                        TableItemData data = dataList[index];
-                       if (data.textWidth == -1) {
+                       //if (data.textWidth == -1) {
                                data.textWidth
-                               = TextSizeUtil.stringExtend(font, data.text);
-                       }
+                               = TextSizeUtil.stringExtend(font, data.text).x;
+                       //}
                        return data.textWidth;
                }
                return 0;
index 2fda325..9bb08c4 100644 (file)
@@ -120,36 +120,37 @@ class TableItemCell extends Composite {
                        String drawingText = data.drawingText;
                        if (data.needRedrawingText) {
                                drawingText = item.getText(index);
-                               int textWidth = TextSizeUtil.textExtent(getFont(), drawingText);
+                               int textWidth = TextSizeUtil.stringExtend(getFont(), drawingText).x;
                                if (textWidth > (rect.width - imageSize.x - 10)) {
                                        while((textWidth > (rect.width - 10) - TextSizeUtil.OMITLEN)
                                                        && drawingText.length() > 1) {
                                                drawingText = drawingText.substring(0, drawingText.length() - 1);
-                                               textWidth = TextSizeUtil.textExtent(getFont(), drawingText);
+                                               textWidth = TextSizeUtil.stringExtend(getFont(), drawingText).x;
                                        }
                                        drawingText = drawingText + TextSizeUtil.OMIT;
                                }
                                data.needRedrawingText = false;
                                data.drawingText = drawingText;
-                               data.drawingTextWidth = TextSizeUtil.textExtent(getFont(), drawingText);
+                               data.drawingTextWidth = TextSizeUtil.stringExtend(getFont(), drawingText).x;
                        }
 
+                       int spacing = image != null ? 5 : 0;
                        switch(item.getParentTable().getColumn(index).getAlignment()) {
                        case SWT.LEFT:
                                imageX += (image != null ? 5 : 0);
-                               textX += imageX + imageSize.x + 5;
+                               textX += imageX + imageSize.x + spacing;
                                break;
                        case SWT.RIGHT:
                                imageX += (image != null
                                                ? rect.width - imageSize.x - data.drawingTextWidth - 10
                                                : rect.width - data.drawingTextWidth - 10);
-                               textX += imageX + imageSize.x + 5;
+                               textX += imageX + imageSize.x + spacing;
                                break;
                        case SWT.CENTER:
                                imageX += (image != null
-                                               ? (rect.width - imageSize.x - data.drawingTextWidth) / 2 + 5
-                                               : (rect.width - data.drawingTextWidth) / 2) - 5;
-                               textX += imageX + imageSize.x + 5;
+                                               ? (rect.width - imageSize.x - data.drawingTextWidth) / 2
+                                               : (rect.width - data.drawingTextWidth) / 2);
+                               textX += imageX + imageSize.x + spacing;
                                break;
                        default:
                        }
@@ -161,7 +162,7 @@ class TableItemCell extends Composite {
 
                        if (data.text != null && !data.text.isEmpty()) {
                                gc.setFont(item.getFont(index));
-                               textY = rect.y + (rect.height - gc.getFontMetrics().getHeight())/2 - 1;
+                               textY = rect.y + (rect.height - gc.textExtent(drawingText).y)/2;
                                gc.setForeground(isSelected ? item.getSelectedForground(): item.getForeground());
                                gc.drawText(drawingText, textX, textY);
                        }
index 93091ae..3c514f6 100644 (file)
 
 package org.tizen.emulator.manager.ui.table;
 
-import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Font;
-import org.eclipse.swt.graphics.FontData;
-import org.eclipse.swt.graphics.FontMetrics;
 import org.eclipse.swt.graphics.GC;
 import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.widgets.Display;
@@ -46,32 +43,11 @@ public class TextSizeUtil {
        public static final int OMITLEN = 12;
        public static final String OMIT = "...";
 
-       public static float getCharHeight(Font font) {
-               float result;
-               FontData fontData = font.getFontData()[0];
-               result = fontData.getHeight() * 0.48f;
-
-               if ((fontData.getStyle() & SWT.BOLD)!= 0) {
-                       result *= 1.45;
-               }
-               return result;
-       }
-
-       public static int textExtent(Font font, String text) {
-
-               GC gc = new GC(Display.getCurrent());
-               gc.setFont(font);
-               FontMetrics metrics = gc.getFontMetrics();
-               int width = metrics.getAverageCharWidth();
-               gc.dispose();
-               return width * text.length();
-       }
-
-       public static int stringExtend(Font font, String text) {
+       public static Point stringExtend(Font font, String text) {
                GC gc = new GC(Display.getCurrent());
                gc.setFont(font);
-               Point p = gc.stringExtent(text);
+               Point p = gc.textExtent(text);
                gc.dispose();
-               return p.x;
+               return p;
        }
 }