You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/08/14 17:45:37 UTC

svn commit: r804267 - in /incubator/pivot/trunk: core/src/org/apache/pivot/text/ wtk/src/org/apache/pivot/wtk/content/ wtk/src/org/apache/pivot/wtk/skin/terra/

Author: gbrown
Date: Fri Aug 14 15:45:37 2009
New Revision: 804267

URL: http://svn.apache.org/viewvc?rev=804267&view=rev
Log:
Move file size formatting logic to a new org.apache.pivot.text.FileSizeFormat class; add a TableViewFileSizeCellRenderer class.

Added:
    incubator/pivot/trunk/core/src/org/apache/pivot/text/
    incubator/pivot/trunk/core/src/org/apache/pivot/text/FileSizeFormat.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/TableViewFileSizeCellRenderer.java
Modified:
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraFileBrowserSkin.java

Added: incubator/pivot/trunk/core/src/org/apache/pivot/text/FileSizeFormat.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/text/FileSizeFormat.java?rev=804267&view=auto
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/text/FileSizeFormat.java (added)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/text/FileSizeFormat.java Fri Aug 14 15:45:37 2009
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.text;
+
+import java.text.FieldPosition;
+import java.text.Format;
+import java.text.NumberFormat;
+import java.text.ParsePosition;
+
+/**
+ * Converts a file size into a human-readable representation using binary
+ * prefixes (1KB = 1024 bytes).
+ *
+ * @author gbrown
+ */
+public class FileSizeFormat extends Format {
+    private static final long serialVersionUID = 0;
+
+    public static final int KILOBYTE = 1024;
+    public static final String[] ABBREVIATIONS = {"K", "M", "G", "T", "P", "E", "Z", "Y"};
+
+    private static final FileSizeFormat FILE_SIZE_FORMAT = new FileSizeFormat();
+
+    private FileSizeFormat() {
+    }
+
+    /**
+     * Formats a file size.
+     *
+     * @param object
+     * A <tt>Number</tt> containing the length of the file, in bytes. May be
+     * negative to indicate an unknown file size.
+     *
+     * @param stringBuffer
+     * The string buffer to which the formatted output will be appended.
+     *
+     * @param fieldPosition
+     * Not used.
+     *
+     * @return
+     * The original string buffer, with the formatted value appended.
+     */
+    @Override
+    public StringBuffer format(Object object, StringBuffer stringBuffer,
+        FieldPosition fieldPosition) {
+        Number number = (Number)object;
+
+        long length = number.longValue();
+
+        if (length >= 0) {
+            double size = length;
+
+            int i = -1;
+            do {
+                size /= KILOBYTE;
+                i++;
+            } while (size > KILOBYTE);
+
+            NumberFormat numberFormat = NumberFormat.getNumberInstance();
+            if (i == 0
+                && size > 1) {
+                numberFormat.setMaximumFractionDigits(0);
+            } else {
+                numberFormat.setMaximumFractionDigits(1);
+            }
+
+            stringBuffer.append(numberFormat.format(size) + " " + ABBREVIATIONS[i] + "B");
+        }
+
+        return stringBuffer;
+    }
+
+    /**
+     * This method is not supported.
+     *
+     * @throws UnsupportedOperationException
+     */
+    @Override
+    public Object parseObject(String arg0, ParsePosition arg1) {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * Returns a shared file size format instance.
+     *
+     * @return
+     * A shared file format size instance.
+     */
+    public static FileSizeFormat getInstance() {
+        return FILE_SIZE_FORMAT;
+    }
+}

Added: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/TableViewFileSizeCellRenderer.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/TableViewFileSizeCellRenderer.java?rev=804267&view=auto
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/TableViewFileSizeCellRenderer.java (added)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/TableViewFileSizeCellRenderer.java Fri Aug 14 15:45:37 2009
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.wtk.content;
+
+import org.apache.pivot.beans.BeanDictionary;
+import org.apache.pivot.collections.Dictionary;
+import org.apache.pivot.text.FileSizeFormat;
+import org.apache.pivot.wtk.HorizontalAlignment;
+import org.apache.pivot.wtk.Insets;
+import org.apache.pivot.wtk.TableView;
+
+/**
+ * Default renderer for table view cells that contain file size data. Renders
+ * cell contents as a formatted file size.
+ *
+ * @author gbrown
+ */
+public class TableViewFileSizeCellRenderer extends TableViewCellRenderer {
+    public TableViewFileSizeCellRenderer() {
+        getStyles().put("horizontalAlignment", HorizontalAlignment.RIGHT);
+
+        // Apply more padding on the right so the right-aligned cells don't
+        // appear to run into left-aligned cells in the next column
+        getStyles().put("padding", new Insets(2, 2, 2, 6));
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public void render(Object value, TableView tableView, TableView.Column column,
+        boolean rowSelected, boolean rowHighlighted, boolean rowDisabled) {
+        renderStyles(tableView, rowSelected, rowDisabled);
+
+        if (value != null) {
+            String formattedFileSize = null;
+
+            // Get the row and cell data
+            String columnName = column.getName();
+            if (columnName != null) {
+                Dictionary<String, Object> rowData;
+                if (value instanceof Dictionary<?, ?>) {
+                    rowData = (Dictionary<String, Object>)value;
+                } else {
+                    rowData = new BeanDictionary(value);
+                }
+
+                Object cellData = rowData.get(columnName);
+
+                if (cellData instanceof Number) {
+                    formattedFileSize = FileSizeFormat.getInstance().format(cellData);
+                } else {
+                    System.err.println("Data for \"" + columnName + "\" is not an instance of "
+                        + Number.class.getName());
+                }
+            }
+
+            setText(formattedFileSize);
+        }
+    }
+}

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraFileBrowserSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraFileBrowserSkin.java?rev=804267&r1=804266&r2=804267&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraFileBrowserSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraFileBrowserSkin.java Fri Aug 14 15:45:37 2009
@@ -21,13 +21,13 @@
 import java.io.File;
 import java.io.IOException;
 import java.text.DateFormat;
-import java.text.NumberFormat;
 import java.util.Date;
 
 import org.apache.pivot.collections.ArrayList;
 import org.apache.pivot.collections.Sequence;
 import org.apache.pivot.io.Folder;
 import org.apache.pivot.serialization.SerializationException;
+import org.apache.pivot.text.FileSizeFormat;
 import org.apache.pivot.util.Filter;
 import org.apache.pivot.util.Resources;
 import org.apache.pivot.util.concurrent.TaskExecutionException;
@@ -77,9 +77,6 @@
         public static final Image HOME_FOLDER_IMAGE;
         public static final Image FILE_IMAGE;
 
-        public static final int KILOBYTE = 1024;
-        public static final String[] ABBREVIATIONS = {"K", "M", "G", "T", "P", "E", "Z", "Y"};
-
         public static final File HOME_DIRECTORY;
 
         static {
@@ -127,46 +124,6 @@
 
             return icon;
         }
-
-        /**
-         * Converts a file size into a human-readable representation using binary
-         * prefixes (1KB = 1024 bytes).
-         *
-         * @param length
-         * The length of the file, in bytes. May be <tt>-1</tt> to indicate an
-         * unknown file size.
-         *
-         * @return
-         * The formatted file size, or null if <tt>length</tt> is <tt>-1</tt>.
-         */
-        public static String formatSize(File file) {
-            String formattedSize;
-
-            long length = file.length();
-            if (length == -1) {
-                formattedSize = null;
-            } else {
-                double size = length;
-
-                int i = -1;
-                do {
-                    size /= KILOBYTE;
-                    i++;
-                } while (size > KILOBYTE);
-
-                NumberFormat numberFormat = NumberFormat.getNumberInstance();
-                if (i == 0
-                    && size > 1) {
-                    numberFormat.setMaximumFractionDigits(0);
-                } else {
-                    numberFormat.setMaximumFractionDigits(1);
-                }
-
-                formattedSize = numberFormat.format(size) + " " + ABBREVIATIONS[i] + "B";
-            }
-
-            return formattedSize;
-        }
     }
 
     /**
@@ -275,7 +232,8 @@
                     icon = getIcon(file);
                     getStyles().put("horizontalAlignment", HorizontalAlignment.LEFT);
                 } else if (columnName.equals(SIZE_KEY)) {
-                    text = formatSize(file);
+                    long length = file.length();
+                    text = FileSizeFormat.getInstance().format(length);
                     getStyles().put("horizontalAlignment", HorizontalAlignment.RIGHT);
                 } else if (columnName.equals(LAST_MODIFIED_KEY)) {
                     long lastModified = file.lastModified();