From 15c2fb1ad77bf606090c2a9d70f3fba199fc691d Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Wed, 23 Jun 2010 13:49:09 +0000 Subject: [PATCH] checkstyle fix in GQuery class. Moved or method to utils class --- .../com/google/gwt/query/client/GQUtils.java | 14 +++ .../com/google/gwt/query/client/GQuery.java | 89 ++++++++++--------- .../google/gwt/query/client/LazyGQuery.java | 14 +-- .../gwt/query/client/SelectorEngine.java | 4 - .../gwt/query/client/GQueryCoreTest.java | 2 + 5 files changed, 68 insertions(+), 55 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQUtils.java index 6be21c5e..fd083057 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQUtils.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQUtils.java @@ -87,6 +87,20 @@ public class GQUtils { GQuery.$("#" + id).remove(); gp.append(gs.attr("src", url).attr("type", "text/javascript").attr("id", id)); } + + /** + * Return the element which is truth in the double scope. + */ + public static native double or(double s1, double s2) /*-{ + return s1 || s2; + }-*/; + + /** + * Return the element which is truth in the javascript scope. + */ + public static native T or(T s1, T s2) /*-{ + return s1 || s2; + }-*/; /** * Check if a number is true in the javascript scope. diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java index b19c1eae..6e7e505b 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java @@ -48,8 +48,6 @@ import com.google.gwt.user.client.Window; */ public class GQuery implements Lazy { - private static final String OLD_DATA_PREFIX = "old-"; - /** * A POJO used to store the top/left CSS positioning values of an element. */ @@ -71,6 +69,9 @@ public class GQuery implements Lazy { } } + /** + * A class to store data in an element + */ protected static final class DataCache extends JavaScriptObject { protected DataCache() { @@ -142,25 +143,29 @@ public class GQuery implements Lazy { this[id]=obj; }-*/; } - - public static final Document document = Document.get(); + public static final BodyElement body = Document.get().getBody(); + public static final Document document = Document.get(); public static boolean fxOff = false; - + public static Class GQUERY = GQuery.class; public static final Element window = window(); private static DataCache dataCache = null; + private static SelectorEngine engine; + private static final int FUNC_PREPEND = 0, FUNC_APPEND = 1, FUNC_AFTER = 2, FUNC_BEFORE = 3; + private static final String OLD_DATA_PREFIX = "old-"; + private static JsMap, Plugin> - plugins; + plugins;; - private static DocumentStyleImpl styleImpl = GWT.create(DocumentStyleImpl.class);; + private static DocumentStyleImpl styleImpl = GWT.create(DocumentStyleImpl.class); private static Element windowData = null; @@ -179,17 +184,17 @@ public class GQuery implements Lazy { } /** - * Wrap a GQuery around an existing node. + * Wrap a GQuery around an event's target element. */ - public static GQuery $(Node n) { - return new GQuery(JSArray.create(n)); + public static GQuery $(Event event) { + return $((Element) event.getCurrentEventTarget().cast()); } /** - * Wrap a GQuery around an event's target element. + * Wrap a GQuery around an existing node. */ - public static GQuery $(Event event) { - return $((Element)event.getCurrentEventTarget().cast()); + public static GQuery $(Node n) { + return new GQuery(JSArray.create(n)); } /** @@ -244,8 +249,7 @@ public class GQuery implements Lazy { Class plugin) { try { if (plugins != null) { - T gquery = (T) plugins.get(plugin). - init(new GQuery(select(selector, context))); + T gquery = (T) plugins.get(plugin).init(new GQuery(select(selector, context))); return gquery; } throw new RuntimeException("No plugin for class " + plugin); @@ -331,7 +335,7 @@ public class GQuery implements Lazy { n = n.getLastChild(); } // TODO: add fixes for IE TBODY issue - return $((NodeList)n.getChildNodes().cast()); + return $((NodeList) n.getChildNodes().cast()); } protected static Object data(Element item, String name, S value) { @@ -354,7 +358,7 @@ public class GQuery implements Lazy { } return name != null ? d.getObject(name) : id; } - + protected static String[] jsArrayToString(JsArrayString array) { if (GWT.isScript()) { return jsArrayToString0(array); @@ -366,7 +370,7 @@ public class GQuery implements Lazy { return result; } } - + private static JSArray copyNodeList(NodeList n) { JSArray res = JSArray.create(); for (int i = 0; i < n.getLength(); i++) { @@ -386,12 +390,10 @@ public class GQuery implements Lazy { private static native String[] jsArrayToString0(JsArrayString array) /*-{ return array; }-*/; - + private static native T[] reinterpretCast(NodeList nl) /*-{ return nl; }-*/; - - private static SelectorEngine engine; private static NodeList select(String selector, Node context) { if (engine == null) { @@ -1150,8 +1152,7 @@ public class GQuery implements Lazy { * function fires. */ public GQuery hover(Function fover, Function fout) { - return bind(Event.ONMOUSEOVER, null, fover). - bind(Event.ONMOUSEOUT, null, fout); + return bind(Event.ONMOUSEOVER, null, fover).bind(Event.ONMOUSEOUT, null, fout); } /** @@ -1461,11 +1462,11 @@ public class GQuery implements Lazy { * relative or absolute). This method only works with visible elements. */ public GQuery offsetParent() { - Element offParent = SelectorEngine. - or(elements.getItem(0).getOffsetParent(), body); - while (offParent != null && !"body".equalsIgnoreCase(offParent.getTagName()) - && !"html".equalsIgnoreCase(offParent.getTagName()) && "static". - equals(styleImpl.curCSS(offParent, "position", true))) { + Element offParent = GQUtils.or(elements.getItem(0).getOffsetParent(), body); + while (offParent != null + && !"body".equalsIgnoreCase(offParent.getTagName()) + && !"html".equalsIgnoreCase(offParent.getTagName()) + && "static".equals(styleImpl.curCSS(offParent, "position", true))) { offParent = offParent.getOffsetParent(); } return new GQuery(offParent); @@ -1777,15 +1778,6 @@ public class GQuery implements Lazy { } } - /** - * Force the current matched set of elements to become - * the specified array of elements. - */ - public GQuery setArray(NodeList nodes){ - this.elements = nodes; - return this; - } - /** * Bind a set of functions to the scroll event of each matched element. * Or trigger the event if no functions are provided. @@ -1860,6 +1852,15 @@ public class GQuery implements Lazy { return as(Events).triggerHtmlEvent("select"); } + /** + * Force the current matched set of elements to become + * the specified array of elements. + */ + public GQuery setArray(NodeList nodes) { + this.elements = nodes; + return this; + } + /** * Set CSS property on the first element. */ @@ -1898,7 +1899,8 @@ public class GQuery implements Lazy { */ public GQuery show() { for (Element e : elements()) { - styleImpl.setStyleProperty(e, "display", SelectorEngine.or((String)data(e, "oldDisplay", null), "")); + styleImpl.setStyleProperty(e, "display", + GQUtils.or((String) data(e, "oldDisplay", null), "")); // When the display=none is in the stylesheet. if (!styleImpl.isVisible(e)) { styleImpl.setStyleProperty(e, "display", "block"); @@ -1978,7 +1980,7 @@ public class GQuery implements Lazy { * Toggle visibility of elements. */ public GQuery toggle() { - for (Element e: elements()) { + for (Element e : elements()) { if ($(e).visible()) { $(e).hide(); } else { @@ -1989,7 +1991,6 @@ public class GQuery implements Lazy { return this; } - /** * Toggle among two or more function calls every other click. */ @@ -2005,7 +2006,7 @@ public class GQuery implements Lazy { /** * Adds or removes the specified classes to each matched element - * depending on the class's presence + * depending on the class's presence. */ public GQuery toggleClass(String... classes) { for (Element e : elements()) { @@ -2124,8 +2125,8 @@ public class GQuery implements Lazy { } else if ("input".equalsIgnoreCase(name)) { InputElement ie = InputElement.as(e); String type = ie.getType(); - if ("radio".equalsIgnoreCase((type)) || "checkbox". - equalsIgnoreCase(type)) { + if ("radio".equalsIgnoreCase((type)) + || "checkbox".equalsIgnoreCase(type)) { if ("checkbox".equalsIgnoreCase(type)) { for (String val : values) { if (ie.getValue().equals(val)) { @@ -2413,7 +2414,7 @@ public class GQuery implements Lazy { JSArray newNodes = JSArray.create(); for (int i = 0; i < elements().length; i++) { Element e = elements()[i]; - if (document.equals(e)){ + if (document.equals(e)) { e = body; } for (int j = 0; j < g.size(); j++) { diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java index fb4ce6ff..4a2f01fc 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java @@ -877,12 +877,6 @@ public interface LazyGQuery extends LazyBase{ */ void saveCssAttrs(String... cssProps); - /** - * Force the current matched set of elements to become - * the specified array of elements. - */ - LazyGQuery setArray(NodeList nodes); - /** * Bind a set of functions to the scroll event of each matched element. * Or trigger the event if no functions are provided. @@ -917,6 +911,12 @@ public interface LazyGQuery extends LazyBase{ LazyGQuery select(); + /** + * Force the current matched set of elements to become + * the specified array of elements. + */ + LazyGQuery setArray(NodeList nodes); + /** * Set CSS property on the first element. */ @@ -988,7 +988,7 @@ public interface LazyGQuery extends LazyBase{ /** * Adds or removes the specified classes to each matched element - * depending on the class's presence + * depending on the class's presence. */ LazyGQuery toggleClass(String... classes); diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/SelectorEngine.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/SelectorEngine.java index 1fd286e8..8f0c39af 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/SelectorEngine.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/SelectorEngine.java @@ -40,10 +40,6 @@ public class SelectorEngine { return n.previousSibling || null; }-*/; - public static native T or(T s1, T s2) /*-{ - return s1 || s2; - }-*/; - public static native NodeList querySelectorAll(String selector, Node ctx) /*-{ return ctx.querySelectorAll(selector); diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java index 9bbba14d..78dc7e0e 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java @@ -668,6 +668,8 @@ public class GQueryCoreTest extends GWTTestCase { assertFalse(GQUtils.eq(45, 42)); assertFalse(GQUtils.eq("", null)); assertFalse(GQUtils.eq(0.45, 0.451)); + +// assertEquals("a", GQUtils.or("a", "")); } public void testUtilsTruth() { -- 2.39.5