You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2018/01/05 21:35:30 UTC

svn commit: r1820356 - in /pivot/trunk: core/src/org/apache/pivot/util/ImageUtils.java wtk/src/org/apache/pivot/wtk/FontUtilities.java

Author: rwhitcomb
Date: Fri Jan  5 21:35:30 2018
New Revision: 1820356

URL: http://svn.apache.org/viewvc?rev=1820356&view=rev
Log:
Create two new classes: "wtk.FontUtilities" and "util.ImageUtils" to
consolidate common code.

Added:
    pivot/trunk/core/src/org/apache/pivot/util/ImageUtils.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java

Added: pivot/trunk/core/src/org/apache/pivot/util/ImageUtils.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/ImageUtils.java?rev=1820356&view=auto
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/ImageUtils.java (added)
+++ pivot/trunk/core/src/org/apache/pivot/util/ImageUtils.java Fri Jan  5 21:35:30 2018
@@ -0,0 +1,46 @@
+/*
+ * 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.util;
+
+import java.net.URL;
+
+/**
+ * Utility class for dealing with images.
+ */
+public class ImageUtils {
+
+    /**
+     * Find an image in the application's resources given the name.
+     *
+     * @param imageName The name of the image resource to find (whose
+     * leading character is stripped off -- likely "@" or "/").
+     * @return The URL from which to load the image if it is found.
+     * @throws IllegalArgumentException if the image resource cannot be found,
+     * or if the {@code imageName} is null or empty.
+     */
+    public static URL findByName(String imageName) {
+        Utils.checkNullOrEmpty(imageName, "imageName");
+
+        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+        URL url = classLoader.getResource(imageName.substring(1));
+        if (url == null) {
+            throw new IllegalArgumentException("Cannot find image resource: " + imageName);
+        }
+        return url;
+    }
+
+}

Added: pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java?rev=1820356&view=auto
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java (added)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/FontUtilities.java Fri Jan  5 21:35:30 2018
@@ -0,0 +1,57 @@
+/*
+ * 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;
+
+import java.awt.Font;
+
+import org.apache.pivot.json.JSONSerializer;
+import org.apache.pivot.serialization.SerializationException;
+import org.apache.pivot.util.Utils;
+
+/**
+ * Utility class for dealing with fonts.
+ */
+public class FontUtilities {
+
+    /**
+     * Interpret a string as a font specification.
+     *
+     * @param value Either a JSON dictionary {@link Theme#deriveFont describing
+     * a font relative to the current theme}, or one of the
+     * {@link Font#decode(String) standard Java font specifications}.
+     * @return The font corresponding to the specification.
+     * @throws IllegalArgumentException if the given string is <tt>null</tt>
+     * or empty or the font specification cannot be decoded.
+     */
+    public static Font decodeFont(String value) {
+        Utils.checkNullOrEmpty(value, "font");
+
+        Font font;
+        if (value.startsWith("{")) {
+            try {
+                font = Theme.deriveFont(JSONSerializer.parseMap(value));
+            } catch (SerializationException exception) {
+                throw new IllegalArgumentException(exception);
+            }
+        } else {
+            font = Font.decode(value);
+        }
+
+        return font;
+    }
+
+}