diff options
Diffstat (limited to 'client')
198 files changed, 25446 insertions, 1763 deletions
diff --git a/client/build.xml b/client/build.xml index 19ec05b28a..1e65dc37c5 100644 --- a/client/build.xml +++ b/client/build.xml @@ -1,6 +1,6 @@ <?xml version="1.0"?> -<project name="vaadin-client" basedir="." default="publish-local" xmlns:ivy="antlib:org.apache.ivy.ant"> +<project name="vaadin-client" basedir="." default="publish-local"> <description> Compiles build helpers used when building other modules. @@ -18,25 +18,30 @@ <!-- Could possibly compile GWT files also here to verify that a) the same dependencies are used and b) all dependencies have been declared --> <fileset file="${gwt.user.jar}" /> + <fileset file="${gwt.elemental.jar}" /> </path> <path id="classpath.test.custom" /> <target name="jar"> - <property name="jar.file" location="${result.dir}/lib/${module.name}-${vaadin.version}.jar" /> + <property name="jar.file" + location="${result.dir}/lib/${module.name}-${vaadin.version}.jar" /> <antcall target="common.jar"> <reference refid="client.gwt.includes" torefid="extra.jar.includes" /> </antcall> <jar destfile="${jar.file}" update="true"> <manifest> - <attribute name="Vaadin-Package-Version" value="1" /> + <attribute name="Vaadin-Package-Version" + value="1" /> <attribute name="Vaadin-Widgetsets" value="com.vaadin.DefaultWidgetSet" /> </manifest> </jar> <!-- Hack to add validation dependency with source classifier --> - <property name="pom.xml" location="${result.dir}/lib/${module.name}-${vaadin.version}.pom" /> + <property name="pom.xml" + location="${result.dir}/lib/${module.name}-${vaadin.version}.pom" /> <copy file="${pom.xml}" tofile="${temp.pom}"> <filterchain> - <replacestring from=" </dependencies>" to=" <dependency> + <replacestring from=" </dependencies>" + to=" <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.0.0.GA</version> diff --git a/client/ivy.xml b/client/ivy.xml index 3abdcf9ba5..6b941af818 100644 --- a/client/ivy.xml +++ b/client/ivy.xml @@ -42,6 +42,9 @@ <dependency org="javax.validation" name="validation-api" rev="1.0.0.GA" conf="build->default,sources" /> + <!-- Testing dependencies --> + <dependency org="org.easymock" name="easymock" rev="3.0" + conf="test,ide-> default" /> </dependencies> diff --git a/client/src/com/vaadin/DefaultWidgetSet.gwt.xml b/client/src/com/vaadin/DefaultWidgetSet.gwt.xml index 8512d547e3..3047924ac7 100755 --- a/client/src/com/vaadin/DefaultWidgetSet.gwt.xml +++ b/client/src/com/vaadin/DefaultWidgetSet.gwt.xml @@ -8,8 +8,23 @@ <inherits name="com.vaadin.Vaadin" /> + <!-- Elemental is used for handling Json only --> + <inherits name="elemental.Json" /> + <entry-point class="com.vaadin.client.ApplicationConfiguration" /> + <generate-with + class="com.vaadin.server.widgetsetutils.AcceptCriteriaFactoryGenerator"> + <when-type-is class="com.vaadin.client.ui.dd.VAcceptCriterionFactory" /> + </generate-with> + + <generate-with + class="com.vaadin.server.widgetsetutils.ConnectorBundleLoaderFactory"> + <when-type-assignable + class="com.vaadin.client.metadata.ConnectorBundleLoader" /> + </generate-with> + + <!-- Since 7.2. Compile all permutations (browser support) into one Javascript file. Speeds up compilation and does not make the Javascript significantly larger. --> diff --git a/client/src/com/vaadin/Vaadin.gwt.xml b/client/src/com/vaadin/Vaadin.gwt.xml index 5e8f08fe22..1bbece6bd6 100644 --- a/client/src/com/vaadin/Vaadin.gwt.xml +++ b/client/src/com/vaadin/Vaadin.gwt.xml @@ -10,8 +10,6 @@ <inherits name="com.google.gwt.http.HTTP" /> - <inherits name="com.google.gwt.json.JSON" /> - <inherits name="com.google.gwt.logging.Logging" /> <set-property name="gwt.logging.enabled" value="TRUE" /> @@ -26,17 +24,6 @@ <when-type-is class="com.google.gwt.core.client.impl.SchedulerImpl" /> </replace-with> - <generate-with - class="com.vaadin.server.widgetsetutils.AcceptCriteriaFactoryGenerator"> - <when-type-is class="com.vaadin.client.ui.dd.VAcceptCriterionFactory" /> - </generate-with> - - <generate-with - class="com.vaadin.server.widgetsetutils.ConnectorBundleLoaderFactory"> - <when-type-assignable - class="com.vaadin.client.metadata.ConnectorBundleLoader" /> - </generate-with> - <replace-with class="com.vaadin.client.communication.AtmospherePushConnection"> <when-type-is class="com.vaadin.client.communication.PushConnection" /> @@ -92,4 +79,9 @@ <when-type-is class="com.vaadin.client.event.PointerEventSupportImpl" /> <when-property-is value="ie10" name="user.agent" /> </replace-with> + + <!-- Make Sass linking available --> + <define-linker name="sass" + class="com.vaadin.sass.linker.SassLinker" /> + </module> diff --git a/client/src/com/vaadin/client/ApplicationConfiguration.java b/client/src/com/vaadin/client/ApplicationConfiguration.java index 5eeeabe743..37d689c6d3 100644 --- a/client/src/com/vaadin/client/ApplicationConfiguration.java +++ b/client/src/com/vaadin/client/ApplicationConfiguration.java @@ -406,14 +406,14 @@ public class ApplicationConfiguration implements EntryPoint { * desired locations even if the base URL of the page changes later * (e.g. with pushState) */ - serviceUrl = Util.getAbsoluteUrl(serviceUrl); + serviceUrl = WidgetUtil.getAbsoluteUrl(serviceUrl); } // Ensure there's an ending slash (to make appending e.g. UIDL work) if (!useServiceUrlPathParam() && !serviceUrl.endsWith("/")) { serviceUrl += '/'; } - vaadinDirUrl = Util.getAbsoluteUrl(jsoConfiguration + vaadinDirUrl = WidgetUtil.getAbsoluteUrl(jsoConfiguration .getConfigString(ApplicationConstants.VAADIN_DIR_URL)); uiId = jsoConfiguration.getConfigInteger(UIConstants.UI_ID_PARAMETER) .intValue(); diff --git a/client/src/com/vaadin/client/ApplicationConnection.java b/client/src/com/vaadin/client/ApplicationConnection.java index 0ac2f4312d..140c1dbfef 100644 --- a/client/src/com/vaadin/client/ApplicationConnection.java +++ b/client/src/com/vaadin/client/ApplicationConnection.java @@ -51,10 +51,6 @@ import com.google.gwt.http.client.RequestCallback; import com.google.gwt.http.client.RequestException; import com.google.gwt.http.client.Response; import com.google.gwt.http.client.URL; -import com.google.gwt.json.client.JSONArray; -import com.google.gwt.json.client.JSONNumber; -import com.google.gwt.json.client.JSONObject; -import com.google.gwt.json.client.JSONString; import com.google.gwt.regexp.shared.MatchResult; import com.google.gwt.regexp.shared.RegExp; import com.google.gwt.user.client.Command; @@ -66,6 +62,7 @@ import com.google.gwt.user.client.Window.ClosingHandler; import com.google.gwt.user.client.ui.HasWidgets; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.ApplicationConfiguration.ErrorMessage; +import com.vaadin.client.ApplicationConnection.ApplicationStoppedEvent; import com.vaadin.client.ResourceLoader.ResourceLoadEvent; import com.vaadin.client.ResourceLoader.ResourceLoadListener; import com.vaadin.client.communication.HasJavaScriptConnectorHelper; @@ -84,6 +81,7 @@ import com.vaadin.client.metadata.NoDataException; import com.vaadin.client.metadata.Property; import com.vaadin.client.metadata.Type; import com.vaadin.client.metadata.TypeData; +import com.vaadin.client.metadata.TypeDataStore; import com.vaadin.client.ui.AbstractComponentConnector; import com.vaadin.client.ui.AbstractConnector; import com.vaadin.client.ui.FontIcon; @@ -108,6 +106,11 @@ import com.vaadin.shared.ui.ui.UIConstants; import com.vaadin.shared.ui.ui.UIState.PushConfigurationState; import com.vaadin.shared.util.SharedUtil; +import elemental.json.Json; +import elemental.json.JsonArray; +import elemental.json.JsonObject; +import elemental.json.JsonValue; + /** * This is the client side communication "engine", managing client-server * communication with its server side counterpart @@ -144,15 +147,20 @@ public class ApplicationConnection implements HasHandlers { private FastStringSet detachedConnectorIds = FastStringSet.create(); } - public static final String MODIFIED_CLASSNAME = "v-modified"; + @Deprecated + public static final String MODIFIED_CLASSNAME = StyleConstants.MODIFIED; - public static final String DISABLED_CLASSNAME = "v-disabled"; + @Deprecated + public static final String DISABLED_CLASSNAME = StyleConstants.DISABLED; - public static final String REQUIRED_CLASSNAME = "v-required"; + @Deprecated + public static final String REQUIRED_CLASSNAME = StyleConstants.REQUIRED; - public static final String REQUIRED_CLASSNAME_EXT = "-required"; + @Deprecated + public static final String REQUIRED_CLASSNAME_EXT = StyleConstants.REQUIRED_EXT; - public static final String ERROR_CLASSNAME_EXT = "-error"; + @Deprecated + public static final String ERROR_CLASSNAME_EXT = StyleConstants.ERROR_EXT; /** * A string that, if found in a non-JSON response to a UIDL request, will @@ -796,7 +804,7 @@ public class ApplicationConnection implements HasHandlers { } protected void repaintAll() { - makeUidlRequest(new JSONArray(), getRepaintAllParameters()); + makeUidlRequest(Json.createArray(), getRepaintAllParameters()); } /** @@ -835,21 +843,19 @@ public class ApplicationConnection implements HasHandlers { * no parameters should be added. Should not start with any * special character. */ - protected void makeUidlRequest(final JSONArray reqInvocations, + protected void makeUidlRequest(final JsonArray reqInvocations, final String extraParams) { startRequest(); - JSONObject payload = new JSONObject(); + JsonObject payload = Json.createObject(); if (!getCsrfToken().equals( ApplicationConstants.CSRF_TOKEN_DEFAULT_VALUE)) { - payload.put(ApplicationConstants.CSRF_TOKEN, new JSONString( - getCsrfToken())); + payload.put(ApplicationConstants.CSRF_TOKEN, getCsrfToken()); } payload.put(ApplicationConstants.RPC_INVOCATIONS, reqInvocations); - payload.put(ApplicationConstants.SERVER_SYNC_ID, new JSONNumber( - lastSeenServerSyncId)); + payload.put(ApplicationConstants.SERVER_SYNC_ID, lastSeenServerSyncId); - VConsole.log("Making UIDL Request with params: " + payload); + VConsole.log("Making UIDL Request with params: " + payload.toJson()); String uri = translateVaadinUri(ApplicationConstants.APP_PROTOCOL_PREFIX + ApplicationConstants.UIDL_PATH + '/'); @@ -872,7 +878,7 @@ public class ApplicationConnection implements HasHandlers { * @param payload * The contents of the request to send */ - protected void doUidlRequest(final String uri, final JSONObject payload) { + protected void doUidlRequest(final String uri, final JsonObject payload) { doUidlRequest(uri, payload, true); } @@ -888,7 +894,7 @@ public class ApplicationConnection implements HasHandlers { * true when a status code 0 should be retried * @since 7.3.7 */ - protected void doUidlRequest(final String uri, final JSONObject payload, + protected void doUidlRequest(final String uri, final JsonObject payload, final boolean retry) { RequestCallback requestCallback = new RequestCallback() { @Override @@ -1073,14 +1079,14 @@ public class ApplicationConnection implements HasHandlers { * @throws RequestException * if the request could not be sent */ - protected void doAjaxRequest(String uri, JSONObject payload, + protected void doAjaxRequest(String uri, JsonObject payload, RequestCallback requestCallback) throws RequestException { RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, uri); // TODO enable timeout // rb.setTimeoutMillis(timeoutMillis); // TODO this should be configurable rb.setHeader("Content-Type", JsonConstants.JSON_CONTENT_TYPE); - rb.setRequestData(payload.toString()); + rb.setRequestData(payload.toJson()); rb.setCallback(requestCallback); final Request request = rb.send(); @@ -1301,7 +1307,6 @@ public class ApplicationConnection implements HasHandlers { } hasActiveRequest = true; requestStartTime = new Date(); - loadingIndicator.trigger(); eventBus.fireEvent(new RequestStartingEvent(this)); } @@ -1326,7 +1331,8 @@ public class ApplicationConnection implements HasHandlers { Scheduler.get().scheduleDeferred(new Command() { @Override public void execute() { - if (!hasActiveRequest()) { + if (!isApplicationRunning() + || !(hasActiveRequest() || deferredSendPending)) { getLoadingIndicator().hide(); // If on Liferay and session expiration management is in @@ -1591,6 +1597,8 @@ public class ApplicationConnection implements HasHandlers { } Command c = new Command() { + private boolean onlyNoLayoutUpdates = true; + @Override public void execute() { assert syncId == -1 || syncId == lastSeenServerSyncId; @@ -1684,15 +1692,17 @@ public class ApplicationConnection implements HasHandlers { updatingState = false; - Profiler.enter("Layout processing"); - try { - LayoutManager layoutManager = getLayoutManager(); - layoutManager.setEverythingNeedsMeasure(); - layoutManager.layoutNow(); - } catch (final Throwable e) { - VConsole.error(e); + if (!onlyNoLayoutUpdates) { + Profiler.enter("Layout processing"); + try { + LayoutManager layoutManager = getLayoutManager(); + layoutManager.setEverythingNeedsMeasure(); + layoutManager.layoutNow(); + } catch (final Throwable e) { + VConsole.error(e); + } + Profiler.leave("Layout processing"); } - Profiler.leave("Layout processing"); if (ApplicationConfiguration.isDebugMode()) { Profiler.enter("Dumping state changes to the console"); @@ -1763,11 +1773,12 @@ public class ApplicationConnection implements HasHandlers { // Create fake server response that says that the uiConnector // has no children - JSONObject fakeHierarchy = new JSONObject(); - fakeHierarchy.put(uiConnectorId, new JSONArray()); - JSONObject fakeJson = new JSONObject(); + JsonObject fakeHierarchy = Json.createObject(); + fakeHierarchy.put(uiConnectorId, Json.createArray()); + JsonObject fakeJson = Json.createObject(); fakeJson.put("hierarchy", fakeHierarchy); - ValueMap fakeValueMap = fakeJson.getJavaScriptObject().cast(); + ValueMap fakeValueMap = ((JavaScriptObject) fakeJson.toNative()) + .cast(); // Update hierarchy based on the fake response ConnectorHierarchyUpdateResult connectorHierarchyUpdateResult = updateConnectorHierarchy(fakeValueMap); @@ -1896,7 +1907,7 @@ public class ApplicationConnection implements HasHandlers { } catch (NoDataException e) { throw new RuntimeException( "Missing data needed to invoke @DelegateToWidget for " - + Util.getSimpleName(component), e); + + component.getClass().getSimpleName(), e); } } @@ -2012,6 +2023,11 @@ public class ApplicationConnection implements HasHandlers { if (connector != null) { continue; } + + // Always do layouts if there's at least one new + // connector + onlyNoLayoutUpdates = false; + int connectorType = Integer.parseInt(types .getString(connectorId)); @@ -2053,6 +2069,11 @@ public class ApplicationConnection implements HasHandlers { JsArray<ValueMap> changes = json.getJSValueMapArray("changes"); int length = changes.length(); + // Must always do layout if there's even a single legacy update + if (length != 0) { + onlyNoLayoutUpdates = false; + } + VConsole.log(" * Passing UIDL to Vaadin 6 style connectors"); // update paintables for (int i = 0; i < length; i++) { @@ -2067,7 +2088,8 @@ public class ApplicationConnection implements HasHandlers { String key = null; if (Profiler.isEnabled()) { key = "updateFromUIDL for " - + Util.getSimpleName(legacyConnector); + + legacyConnector.getClass() + .getSimpleName(); Profiler.enter(key); } @@ -2167,30 +2189,44 @@ public class ApplicationConnection implements HasHandlers { Profiler.enter("updateConnectorState inner loop"); if (Profiler.isEnabled()) { Profiler.enter("Decode connector state " - + Util.getSimpleName(connector)); + + connector.getClass().getSimpleName()); } - JSONObject stateJson = new JSONObject( - states.getJavaScriptObject(connectorId)); + JavaScriptObject jso = states + .getJavaScriptObject(connectorId); + JsonObject stateJson = Util.jso2json(jso); if (connector instanceof HasJavaScriptConnectorHelper) { ((HasJavaScriptConnectorHelper) connector) .getJavascriptConnectorHelper() - .setNativeState( - stateJson.getJavaScriptObject()); + .setNativeState(jso); } SharedState state = connector.getState(); + Type stateType = new Type(state.getClass() + .getName(), null); + + if (onlyNoLayoutUpdates) { + Profiler.enter("updateConnectorState @NoLayout handling"); + for (String propertyName : stateJson.keys()) { + Property property = stateType + .getProperty(propertyName); + if (!property.isNoLayout()) { + onlyNoLayoutUpdates = false; + break; + } + } + Profiler.leave("updateConnectorState @NoLayout handling"); + } Profiler.enter("updateConnectorState decodeValue"); - JsonDecoder.decodeValue(new Type(state.getClass() - .getName(), null), stateJson, state, - ApplicationConnection.this); + JsonDecoder.decodeValue(stateType, stateJson, + state, ApplicationConnection.this); Profiler.leave("updateConnectorState decodeValue"); if (Profiler.isEnabled()) { Profiler.leave("Decode connector state " - + Util.getSimpleName(connector)); + + connector.getClass().getSimpleName()); } Profiler.enter("updateConnectorState create event"); @@ -2224,7 +2260,7 @@ public class ApplicationConnection implements HasHandlers { .getConnector(connectorId); StateChangeEvent event = new StateChangeEvent(connector, - new JSONObject(), true); + Json.createObject(), true); events.add(event); @@ -2403,6 +2439,10 @@ public class ApplicationConnection implements HasHandlers { Profiler.leave("updateConnectorHierarchy detach removed connectors"); + if (result.events.size() != 0) { + onlyNoLayoutUpdates = false; + } + Profiler.leave("updateConnectorHierarchy"); return result; @@ -2526,15 +2566,23 @@ public class ApplicationConnection implements HasHandlers { VConsole.log(" * Performing server to client RPC calls"); - JSONArray rpcCalls = new JSONArray( - json.getJavaScriptObject("rpc")); + JsonArray rpcCalls = Util.jso2json(json + .getJavaScriptObject("rpc")); - int rpcLength = rpcCalls.size(); + int rpcLength = rpcCalls.length(); for (int i = 0; i < rpcLength; i++) { try { - JSONArray rpcCall = (JSONArray) rpcCalls.get(i); - rpcManager.parseAndApplyInvocation(rpcCall, - ApplicationConnection.this); + JsonArray rpcCall = rpcCalls.getArray(i); + MethodInvocation invocation = rpcManager + .parseAndApplyInvocation(rpcCall, + ApplicationConnection.this); + + if (onlyNoLayoutUpdates + && !RpcManager.getMethod(invocation) + .isNoLayout()) { + onlyNoLayoutUpdates = false; + } + } catch (final Throwable e) { VConsole.error(e); } @@ -2708,8 +2756,8 @@ public class ApplicationConnection implements HasHandlers { * */ public void sendPendingVariableChanges() { - if (!deferedSendPending) { - deferedSendPending = true; + if (!deferredSendPending) { + deferredSendPending = true; Scheduler.get().scheduleFinally(sendPendingCommand); } } @@ -2717,11 +2765,11 @@ public class ApplicationConnection implements HasHandlers { private final ScheduledCommand sendPendingCommand = new ScheduledCommand() { @Override public void execute() { - deferedSendPending = false; + deferredSendPending = false; doSendPendingVariableChanges(); } }; - private boolean deferedSendPending = false; + private boolean deferredSendPending = false; private void doSendPendingVariableChanges() { if (isApplicationRunning()) { @@ -2756,22 +2804,19 @@ public class ApplicationConnection implements HasHandlers { */ private void buildAndSendVariableBurst( LinkedHashMap<String, MethodInvocation> pendingInvocations) { - - JSONArray reqJson = new JSONArray(); + boolean showLoadingIndicator = false; + JsonArray reqJson = Json.createArray(); if (!pendingInvocations.isEmpty()) { if (ApplicationConfiguration.isDebugMode()) { Util.logVariableBurst(this, pendingInvocations.values()); } for (MethodInvocation invocation : pendingInvocations.values()) { - JSONArray invocationJson = new JSONArray(); - invocationJson.set(0, - new JSONString(invocation.getConnectorId())); - invocationJson.set(1, - new JSONString(invocation.getInterfaceName())); - invocationJson.set(2, - new JSONString(invocation.getMethodName())); - JSONArray paramJson = new JSONArray(); + JsonArray invocationJson = Json.createArray(); + invocationJson.set(0, invocation.getConnectorId()); + invocationJson.set(1, invocation.getInterfaceName()); + invocationJson.set(2, invocation.getMethodName()); + JsonArray paramJson = Json.createArray(); Type[] parameterTypes = null; if (!isLegacyVariableChange(invocation) @@ -2782,10 +2827,16 @@ public class ApplicationConnection implements HasHandlers { Method method = type.getMethod(invocation .getMethodName()); parameterTypes = method.getParameterTypes(); + + showLoadingIndicator |= !TypeDataStore + .isNoLoadingIndicator(method); } catch (NoDataException e) { throw new RuntimeException("No type data for " + invocation.toString(), e); } + } else { + // Always show loading indicator for legacy requests + showLoadingIndicator = true; } for (int i = 0; i < invocation.getParameters().length; ++i) { @@ -2795,10 +2846,11 @@ public class ApplicationConnection implements HasHandlers { type = parameterTypes[i]; } Object value = invocation.getParameters()[i]; - paramJson.set(i, JsonEncoder.encode(value, type, this)); + JsonValue jsonValue = JsonEncoder.encode(value, type, this); + paramJson.set(i, jsonValue); } invocationJson.set(3, paramJson); - reqJson.set(reqJson.size(), invocationJson); + reqJson.set(reqJson.length(), invocationJson); } pendingInvocations.clear(); @@ -2816,6 +2868,9 @@ public class ApplicationConnection implements HasHandlers { getConfiguration().setWidgetsetVersionSent(); } + if (showLoadingIndicator) { + getLoadingIndicator().trigger(); + } makeUidlRequest(reqJson, extraParams); } @@ -3562,7 +3617,7 @@ public class ApplicationConnection implements HasHandlers { * @return Connector for focused element or null. */ private ComponentConnector getActiveConnector() { - Element focusedElement = Util.getFocusedElement(); + Element focusedElement = WidgetUtil.getFocusedElement(); if (focusedElement == null) { return null; } diff --git a/client/src/com/vaadin/client/BrowserInfo.java b/client/src/com/vaadin/client/BrowserInfo.java index e8b8d8309a..5ca79cb121 100644 --- a/client/src/com/vaadin/client/BrowserInfo.java +++ b/client/src/com/vaadin/client/BrowserInfo.java @@ -343,7 +343,7 @@ public class BrowserInfo { public boolean requiresOverflowAutoFix() { return (getWebkitVersion() > 0 || getOperaVersion() >= 11 || getIEVersion() >= 10 || isFirefox()) - && Util.getNativeScrollbarSize() > 0; + && WidgetUtil.getNativeScrollbarSize() > 0; } /** @@ -359,7 +359,8 @@ public class BrowserInfo { * otherwise <code>false</code> */ public boolean requiresPositionAbsoluteOverflowAutoFix() { - return (getWebkitVersion() > 0) && Util.getNativeScrollbarSize() > 0; + return (getWebkitVersion() > 0) + && WidgetUtil.getNativeScrollbarSize() > 0; } /** diff --git a/client/src/com/vaadin/client/JavaScriptConnectorHelper.java b/client/src/com/vaadin/client/JavaScriptConnectorHelper.java index c4b36d6453..1eb326115e 100644 --- a/client/src/com/vaadin/client/JavaScriptConnectorHelper.java +++ b/client/src/com/vaadin/client/JavaScriptConnectorHelper.java @@ -26,7 +26,6 @@ import java.util.Set; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArray; import com.google.gwt.dom.client.Element; -import com.google.gwt.json.client.JSONArray; import com.vaadin.client.communication.JavaScriptMethodInvocation; import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler; @@ -35,6 +34,8 @@ import com.vaadin.client.ui.layout.ElementResizeListener; import com.vaadin.shared.JavaScriptConnectorState; import com.vaadin.shared.communication.MethodInvocation; +import elemental.json.JsonArray; + public class JavaScriptConnectorHelper { private final ServerConnector connector; @@ -49,7 +50,7 @@ public class JavaScriptConnectorHelper { private JavaScriptObject connectorWrapper; private int tag; - private boolean inited = false; + private String initFunctionName; public JavaScriptConnectorHelper(ServerConnector connector) { this.connector = connector; @@ -58,51 +59,79 @@ public class JavaScriptConnectorHelper { rpcObjects.put("", JavaScriptObject.createObject()); } + /** + * The id of the previous response for which state changes have been + * processed. If this is the same as the + * {@link ApplicationConnection#getLastResponseId()}, it means that the + * state change has already been handled and should not be done again. + */ + private int processedResponseId = -1; + public void init() { connector.addStateChangeHandler(new StateChangeHandler() { @Override public void onStateChanged(StateChangeEvent stateChangeEvent) { - JavaScriptObject wrapper = getConnectorWrapper(); - JavaScriptConnectorState state = getConnectorState(); + processStateChanges(); + } + }); + } - for (String callback : state.getCallbackNames()) { - ensureCallback(JavaScriptConnectorHelper.this, wrapper, - callback); - } + /** + * Makes sure the javascript part of the connector has been initialized. The + * javascript is usually initalized the first time a state change event is + * received, but it might in some cases be necessary to make this happen + * earlier. + * + * @since 7.4.0 + */ + public void ensureJavascriptInited() { + if (initFunctionName == null) { + processStateChanges(); + } + } + + private void processStateChanges() { + int lastResponseId = connector.getConnection().getLastResponseId(); + if (processedResponseId == lastResponseId) { + return; + } + processedResponseId = lastResponseId; - for (Entry<String, Set<String>> entry : state - .getRpcInterfaces().entrySet()) { - String rpcName = entry.getKey(); - String jsName = getJsInterfaceName(rpcName); - if (!rpcObjects.containsKey(jsName)) { - Set<String> methods = entry.getValue(); - rpcObjects.put(jsName, - createRpcObject(rpcName, methods)); - - // Init all methods for wildcard rpc - for (String method : methods) { - JavaScriptObject wildcardRpcObject = rpcObjects - .get(""); - Set<String> interfaces = rpcMethods.get(method); - if (interfaces == null) { - interfaces = new HashSet<String>(); - rpcMethods.put(method, interfaces); - attachRpcMethod(wildcardRpcObject, null, method); - } - interfaces.add(rpcName); - } + JavaScriptObject wrapper = getConnectorWrapper(); + JavaScriptConnectorState state = getConnectorState(); + + for (String callback : state.getCallbackNames()) { + ensureCallback(JavaScriptConnectorHelper.this, wrapper, callback); + } + + for (Entry<String, Set<String>> entry : state.getRpcInterfaces() + .entrySet()) { + String rpcName = entry.getKey(); + String jsName = getJsInterfaceName(rpcName); + if (!rpcObjects.containsKey(jsName)) { + Set<String> methods = entry.getValue(); + rpcObjects.put(jsName, createRpcObject(rpcName, methods)); + + // Init all methods for wildcard rpc + for (String method : methods) { + JavaScriptObject wildcardRpcObject = rpcObjects.get(""); + Set<String> interfaces = rpcMethods.get(method); + if (interfaces == null) { + interfaces = new HashSet<String>(); + rpcMethods.put(method, interfaces); + attachRpcMethod(wildcardRpcObject, null, method); } + interfaces.add(rpcName); } + } + } - // Init after setting up callbacks & rpc - if (!inited) { - initJavaScript(); - inited = true; - } + // Init after setting up callbacks & rpc + if (initFunctionName == null) { + initJavaScript(); + } - invokeIfPresent(wrapper, "onStateChange"); - } - }); + invokeIfPresent(wrapper, "onStateChange"); } private static String getJsInterfaceName(String rpcName) { @@ -119,7 +148,7 @@ public class JavaScriptConnectorHelper { return object; } - private boolean initJavaScript() { + protected boolean initJavaScript() { ApplicationConfiguration conf = connector.getConnection() .getConfiguration(); ArrayList<String> attemptedNames = new ArrayList<String>(); @@ -131,6 +160,7 @@ public class JavaScriptConnectorHelper { if (tryInitJs(initFunctionName, getConnectorWrapper())) { VConsole.log("JavaScript connector initialized using " + initFunctionName); + this.initFunctionName = initFunctionName; return true; } else { VConsole.log("No JavaScript function " + initFunctionName @@ -159,7 +189,7 @@ public class JavaScriptConnectorHelper { } }-*/; - private JavaScriptObject getConnectorWrapper() { + public JavaScriptObject getConnectorWrapper() { if (connectorWrapper == null) { connectorWrapper = createConnectorWrapper(this, connector.getConnection(), nativeState, rpcMap, @@ -318,7 +348,7 @@ public class JavaScriptConnectorHelper { iface = findWildcardInterface(method); } - JSONArray argumentsArray = new JSONArray(arguments); + JsonArray argumentsArray = Util.jso2json(arguments); Object[] parameters = new Object[arguments.length()]; for (int i = 0; i < parameters.length; i++) { parameters[i] = argumentsArray.get(i); @@ -355,7 +385,7 @@ public class JavaScriptConnectorHelper { MethodInvocation invocation = new JavaScriptMethodInvocation( connector.getConnectorId(), "com.vaadin.ui.JavaScript$JavaScriptCallbackRpc", "call", - new Object[] { name, new JSONArray(arguments) }); + new Object[] { name, arguments }); connector.getConnection().addMethodInvocationToQueue(invocation, false, false); } @@ -381,8 +411,8 @@ public class JavaScriptConnectorHelper { } }-*/; - public Object[] decodeRpcParameters(JSONArray parametersJson) { - return new Object[] { parametersJson.getJavaScriptObject() }; + public Object[] decodeRpcParameters(JsonArray parametersJson) { + return new Object[] { Util.json2jso(parametersJson) }; } public void setTag(int tag) { @@ -390,18 +420,16 @@ public class JavaScriptConnectorHelper { } public void invokeJsRpc(MethodInvocation invocation, - JSONArray parametersJson) { + JsonArray parametersJson) { String iface = invocation.getInterfaceName(); String method = invocation.getMethodName(); if ("com.vaadin.ui.JavaScript$JavaScriptCallbackRpc".equals(iface) && "call".equals(method)) { - String callbackName = parametersJson.get(0).isString() - .stringValue(); - JavaScriptObject arguments = parametersJson.get(1).isArray() - .getJavaScriptObject(); + String callbackName = parametersJson.getString(0); + JavaScriptObject arguments = Util.json2jso(parametersJson.get(1)); invokeCallback(getConnectorWrapper(), callbackName, arguments); } else { - JavaScriptObject arguments = parametersJson.getJavaScriptObject(); + JavaScriptObject arguments = Util.json2jso(parametersJson); invokeJsRpc(rpcMap, iface, method, arguments); // Also invoke wildcard interface invokeJsRpc(rpcMap, "", method, arguments); @@ -466,4 +494,7 @@ public class JavaScriptConnectorHelper { } }-*/; + public String getInitFunctionName() { + return initFunctionName; + } } diff --git a/client/src/com/vaadin/client/LayoutManager.java b/client/src/com/vaadin/client/LayoutManager.java index 1fee13b16d..9775c29ab6 100644 --- a/client/src/com/vaadin/client/LayoutManager.java +++ b/client/src/com/vaadin/client/LayoutManager.java @@ -360,7 +360,8 @@ public class LayoutManager { if (Profiler.isEnabled()) { Profiler.enter("ElementResizeListener.onElementResize construct profiler key"); key = "ElementResizeListener.onElementResize for " - + Util.getSimpleName(listener); + + listener.getClass() + .getSimpleName(); Profiler.leave("ElementResizeListener.onElementResize construct profiler key"); Profiler.enter(key); } @@ -403,7 +404,7 @@ public class LayoutManager { String key = null; if (Profiler.isEnabled()) { key = "layoutHorizontally() for " - + Util.getSimpleName(cl); + + cl.getClass().getSimpleName(); Profiler.enter(key); } @@ -425,7 +426,8 @@ public class LayoutManager { try { String key = null; if (Profiler.isEnabled()) { - key = "layout() for " + Util.getSimpleName(rr); + key = "layout() for " + + rr.getClass().getSimpleName(); Profiler.enter(key); } @@ -458,7 +460,7 @@ public class LayoutManager { String key = null; if (Profiler.isEnabled()) { key = "layoutVertically() for " - + Util.getSimpleName(cl); + + cl.getClass().getSimpleName(); Profiler.enter(key); } @@ -480,7 +482,8 @@ public class LayoutManager { try { String key = null; if (Profiler.isEnabled()) { - key = "layout() for " + Util.getSimpleName(rr); + key = "layout() for " + + rr.getClass().getSimpleName(); Profiler.enter(key); } @@ -559,7 +562,7 @@ public class LayoutManager { String key = null; if (Profiler.isEnabled()) { key = "layout PostLayoutListener for " - + Util.getSimpleName(connector); + + connector.getClass().getSimpleName(); Profiler.enter(key); } diff --git a/client/src/com/vaadin/client/LayoutManagerIE8.java b/client/src/com/vaadin/client/LayoutManagerIE8.java index 941ac589b2..9fb6819e83 100644 --- a/client/src/com/vaadin/client/LayoutManagerIE8.java +++ b/client/src/com/vaadin/client/LayoutManagerIE8.java @@ -94,7 +94,7 @@ public class LayoutManagerIE8 extends LayoutManager { * the containing element. To force a reflow by modifying the magical * zoom property. */ - Util.forceIE8Redraw(RootPanel.get().getElement()); + WidgetUtil.forceIE8Redraw(RootPanel.get().getElement()); Profiler.leave("LayoutManagerIE8.performBrowserLayoutHacks"); } } diff --git a/client/src/com/vaadin/client/MeasuredSize.java b/client/src/com/vaadin/client/MeasuredSize.java index 2531ff9389..8520635a4d 100644 --- a/client/src/com/vaadin/client/MeasuredSize.java +++ b/client/src/com/vaadin/client/MeasuredSize.java @@ -236,7 +236,7 @@ public class MeasuredSize { Profiler.leave("Measure borders"); Profiler.enter("Measure height"); - int requiredHeight = Util.getRequiredHeight(element); + int requiredHeight = WidgetUtil.getRequiredHeight(element); int marginHeight = sumHeights(margins); int oldHeight = height; int oldWidth = width; @@ -247,7 +247,7 @@ public class MeasuredSize { Profiler.leave("Measure height"); Profiler.enter("Measure width"); - int requiredWidth = Util.getRequiredWidth(element); + int requiredWidth = WidgetUtil.getRequiredWidth(element); int marginWidth = sumWidths(margins); if (setOuterWidth(requiredWidth + marginWidth)) { debugSizeChange(element, "Width (outer)", oldWidth, width); diff --git a/client/src/com/vaadin/client/MouseEventDetailsBuilder.java b/client/src/com/vaadin/client/MouseEventDetailsBuilder.java index 313fe682fd..11ebe3925c 100644 --- a/client/src/com/vaadin/client/MouseEventDetailsBuilder.java +++ b/client/src/com/vaadin/client/MouseEventDetailsBuilder.java @@ -57,8 +57,8 @@ public class MouseEventDetailsBuilder { Element relativeToElement) { MouseEventDetails mouseEventDetails = new MouseEventDetails(); mouseEventDetails.setType(Event.getTypeInt(evt.getType())); - mouseEventDetails.setClientX(Util.getTouchOrMouseClientX(evt)); - mouseEventDetails.setClientY(Util.getTouchOrMouseClientY(evt)); + mouseEventDetails.setClientX(WidgetUtil.getTouchOrMouseClientX(evt)); + mouseEventDetails.setClientY(WidgetUtil.getTouchOrMouseClientY(evt)); if (evt.getButton() == NativeEvent.BUTTON_LEFT) { mouseEventDetails.setButton(MouseButton.LEFT); } else if (evt.getButton() == NativeEvent.BUTTON_RIGHT) { diff --git a/client/src/com/vaadin/client/Profiler.java b/client/src/com/vaadin/client/Profiler.java index 6c0967099f..4b35427575 100644 --- a/client/src/com/vaadin/client/Profiler.java +++ b/client/src/com/vaadin/client/Profiler.java @@ -17,11 +17,13 @@ package com.vaadin.client; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; +import java.util.List; import java.util.Map; import java.util.logging.Logger; @@ -29,8 +31,6 @@ import com.google.gwt.core.client.Duration; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArray; import com.google.gwt.core.shared.GWT; -import com.vaadin.client.debug.internal.ProfilerSection.Node; -import com.vaadin.client.debug.internal.ProfilerSection.ProfilerResultConsumer; /** * Lightweight profiling tool that can be used to collect profiling data with @@ -55,6 +55,236 @@ public class Profiler { } } + /** + * Interface for getting data from the {@link Profiler}. + * <p> + * <b>Warning!</b> This interface is most likely to change in the future + * + * @since 7.1 + * @author Vaadin Ltd + */ + public interface ProfilerResultConsumer { + public void addProfilerData(Node rootNode, List<Node> totals); + + public void addBootstrapData(LinkedHashMap<String, Double> timings); + } + + /** + * A hierarchical representation of the time spent running a named block of + * code. + * <p> + * <b>Warning!</b> This class is most likely to change in the future and is + * therefore defined in this class in an internal package instead of + * Profiler where it might seem more logical. + */ + public static class Node { + private final String name; + private final LinkedHashMap<String, Node> children = new LinkedHashMap<String, Node>(); + private double time = 0; + private int count = 0; + private double enterTime = 0; + private double minTime = 1000000000; + private double maxTime = 0; + + /** + * Create a new node with the given name. + * + * @param name + */ + public Node(String name) { + this.name = name; + } + + /** + * Gets the name of the node + * + * @return the name of the node + */ + public String getName() { + return name; + } + + /** + * Creates a new child node or retrieves and existing child and updates + * its total time and hit count. + * + * @param name + * the name of the child + * @param timestamp + * the timestamp for when the node is entered + * @return the child node object + */ + public Node enterChild(String name, double timestamp) { + Node child = children.get(name); + if (child == null) { + child = new Node(name); + children.put(name, child); + } + child.enterTime = timestamp; + child.count++; + return child; + } + + /** + * Gets the total time spent in this node, including time spent in sub + * nodes + * + * @return the total time spent, in milliseconds + */ + public double getTimeSpent() { + return time; + } + + /** + * Gets the minimum time spent for one invocation of this node, + * including time spent in sub nodes + * + * @return the time spent for the fastest invocation, in milliseconds + */ + public double getMinTimeSpent() { + return minTime; + } + + /** + * Gets the maximum time spent for one invocation of this node, + * including time spent in sub nodes + * + * @return the time spent for the slowest invocation, in milliseconds + */ + public double getMaxTimeSpent() { + return maxTime; + } + + /** + * Gets the number of times this node has been entered + * + * @return the number of times the node has been entered + */ + public int getCount() { + return count; + } + + /** + * Gets the total time spent in this node, excluding time spent in sub + * nodes + * + * @return the total time spent, in milliseconds + */ + public double getOwnTime() { + double time = getTimeSpent(); + for (Node node : children.values()) { + time -= node.getTimeSpent(); + } + return time; + } + + /** + * Gets the child nodes of this node + * + * @return a collection of child nodes + */ + public Collection<Node> getChildren() { + return Collections.unmodifiableCollection(children.values()); + } + + private void buildRecursiveString(StringBuilder builder, String prefix) { + if (getName() != null) { + String msg = getStringRepresentation(prefix); + builder.append(msg + '\n'); + } + String childPrefix = prefix + "*"; + for (Node node : children.values()) { + node.buildRecursiveString(builder, childPrefix); + } + } + + @Override + public String toString() { + return getStringRepresentation(""); + } + + public String getStringRepresentation(String prefix) { + if (getName() == null) { + return ""; + } + String msg = prefix + " " + getName() + " in " + getTimeSpent() + + " ms."; + if (getCount() > 1) { + msg += " Invoked " + + getCount() + + " times (" + + roundToSignificantFigures(getTimeSpent() / getCount()) + + " ms per time, min " + + roundToSignificantFigures(getMinTimeSpent()) + + " ms, max " + + roundToSignificantFigures(getMaxTimeSpent()) + + " ms)."; + } + if (!children.isEmpty()) { + double ownTime = getOwnTime(); + msg += " " + ownTime + " ms spent in own code"; + if (getCount() > 1) { + msg += " (" + + roundToSignificantFigures(ownTime / getCount()) + + " ms per time)"; + } + msg += '.'; + } + return msg; + } + + private static double roundToSignificantFigures(double num) { + // Number of significant digits + int n = 3; + if (num == 0) { + return 0; + } + + final double d = Math.ceil(Math.log10(num < 0 ? -num : num)); + final int power = n - (int) d; + + final double magnitude = Math.pow(10, power); + final long shifted = Math.round(num * magnitude); + return shifted / magnitude; + } + + public void sumUpTotals(Map<String, Node> totals) { + String name = getName(); + if (name != null) { + Node totalNode = totals.get(name); + if (totalNode == null) { + totalNode = new Node(name); + totals.put(name, totalNode); + } + + totalNode.time += getOwnTime(); + totalNode.count += getCount(); + totalNode.minTime = Math.min(totalNode.minTime, + getMinTimeSpent()); + totalNode.maxTime = Math.max(totalNode.maxTime, + getMaxTimeSpent()); + } + for (Node node : children.values()) { + node.sumUpTotals(totals); + } + } + + /** + * @param timestamp + */ + public void leave(double timestamp) { + double elapsed = (timestamp - enterTime); + time += elapsed; + enterTime = 0; + if (elapsed < minTime) { + minTime = elapsed; + } + if (elapsed > maxTime) { + maxTime = elapsed; + } + } + } + private static final String evtGroup = "VaadinProfiler"; private static final class GwtStatsEvent extends JavaScriptObject { diff --git a/client/src/com/vaadin/client/RenderSpace.java b/client/src/com/vaadin/client/RenderSpace.java index 5a7440b682..dff774aa6f 100644 --- a/client/src/com/vaadin/client/RenderSpace.java +++ b/client/src/com/vaadin/client/RenderSpace.java @@ -34,7 +34,7 @@ public class RenderSpace extends Size { public RenderSpace(int width, int height, boolean useNativeScrollbarSize) { super(width, height); if (useNativeScrollbarSize) { - scrollBarSize = Util.getNativeScrollbarSize(); + scrollBarSize = WidgetUtil.getNativeScrollbarSize(); } } diff --git a/client/src/com/vaadin/client/ResourceLoader.java b/client/src/com/vaadin/client/ResourceLoader.java index ceede263fc..9e9ce5ac49 100644 --- a/client/src/com/vaadin/client/ResourceLoader.java +++ b/client/src/com/vaadin/client/ResourceLoader.java @@ -225,7 +225,7 @@ public class ResourceLoader { */ public void loadScript(final String scriptUrl, final ResourceLoadListener resourceLoadListener, boolean async) { - final String url = Util.getAbsoluteUrl(scriptUrl); + final String url = WidgetUtil.getAbsoluteUrl(scriptUrl); ResourceLoadEvent event = new ResourceLoadEvent(this, url, false); if (loadedResources.contains(url)) { if (resourceLoadListener != null) { @@ -307,7 +307,7 @@ public class ResourceLoader { */ public void preloadResource(String url, ResourceLoadListener resourceLoadListener) { - url = Util.getAbsoluteUrl(url); + url = WidgetUtil.getAbsoluteUrl(url); ResourceLoadEvent event = new ResourceLoadEvent(this, url, true); if (loadedResources.contains(url) || preloadedResources.contains(url)) { // Already loaded or preloaded -> just fire listener @@ -424,7 +424,7 @@ public class ResourceLoader { */ public void loadStylesheet(final String stylesheetUrl, final ResourceLoadListener resourceLoadListener) { - final String url = Util.getAbsoluteUrl(stylesheetUrl); + final String url = WidgetUtil.getAbsoluteUrl(stylesheetUrl); final ResourceLoadEvent event = new ResourceLoadEvent(this, url, false); if (loadedResources.contains(url)) { if (resourceLoadListener != null) { diff --git a/client/src/com/vaadin/client/StyleConstants.java b/client/src/com/vaadin/client/StyleConstants.java index c4588587d4..fad88f1359 100644 --- a/client/src/com/vaadin/client/StyleConstants.java +++ b/client/src/com/vaadin/client/StyleConstants.java @@ -35,4 +35,13 @@ public class StyleConstants { * Added to all layouts to denote they are layouts */ public static final String UI_LAYOUT = "v-layout"; + + public static final String MODIFIED = "v-modified"; + public static final String DISABLED = "v-disabled"; + + public static final String REQUIRED = "v-required"; + + public static final String REQUIRED_EXT = "-required"; + + public static final String ERROR_EXT = "-error"; } diff --git a/client/src/com/vaadin/client/SuperDevMode.java b/client/src/com/vaadin/client/SuperDevMode.java index f1020b3d25..821af6075a 100644 --- a/client/src/com/vaadin/client/SuperDevMode.java +++ b/client/src/com/vaadin/client/SuperDevMode.java @@ -89,7 +89,7 @@ public class SuperDevMode { VConsole.error("JSONP compile call failed"); // Don't log exception as they are shown as // notifications - VConsole.error(Util.getSimpleName(caught) + ": " + VConsole.error(caught.getClass().getSimpleName() + ": " + caught.getMessage()); failed(); diff --git a/client/src/com/vaadin/client/Util.java b/client/src/com/vaadin/client/Util.java index 585045ddd5..778f7c3861 100644 --- a/client/src/com/vaadin/client/Util.java +++ b/client/src/com/vaadin/client/Util.java @@ -16,38 +16,20 @@ package com.vaadin.client; -import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.HashMap; import java.util.Iterator; import java.util.List; -import java.util.Map; -import com.google.gwt.core.client.Scheduler; -import com.google.gwt.core.client.Scheduler.ScheduledCommand; -import com.google.gwt.dom.client.AnchorElement; -import com.google.gwt.dom.client.DivElement; -import com.google.gwt.dom.client.Document; +import com.google.gwt.core.client.GWT; +import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.NativeEvent; -import com.google.gwt.dom.client.Node; -import com.google.gwt.dom.client.NodeList; -import com.google.gwt.dom.client.Style; -import com.google.gwt.dom.client.Style.Display; -import com.google.gwt.dom.client.Style.Unit; -import com.google.gwt.dom.client.Touch; import com.google.gwt.event.dom.client.KeyEvent; -import com.google.gwt.regexp.shared.MatchResult; -import com.google.gwt.regexp.shared.RegExp; -import com.google.gwt.user.client.Command; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; -import com.google.gwt.user.client.EventListener; -import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.HasWidgets; -import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.RenderInformation.FloatSize; import com.vaadin.client.ui.VOverlay; @@ -57,6 +39,9 @@ import com.vaadin.shared.communication.MethodInvocation; import com.vaadin.shared.ui.ComponentStateUtil; import com.vaadin.shared.util.SharedUtil; +import elemental.js.json.JsJsonValue; +import elemental.json.JsonValue; + public class Util { /** @@ -65,11 +50,10 @@ public class Util { * Stops execution on firefox browsers on a breakpoint. * */ - public static native void browserDebugger() - /*-{ - if($wnd.console) - debugger; - }-*/; + @Deprecated + public static void browserDebugger() { + WidgetUtil.browserDebugger(); + } /** * Helper method for a bug fix #14041. For mozilla getKeyCode return 0 for @@ -80,12 +64,9 @@ public class Util { * @return return key code * @since 7.2.4 */ + @Deprecated public static int getKeyCode(KeyEvent<?> event) { - int keyCode = event.getNativeEvent().getKeyCode(); - if (keyCode == 0) { - keyCode = event.getNativeEvent().getCharCode(); - } - return keyCode; + return WidgetUtil.getKeyCode(event); } /** @@ -99,17 +80,11 @@ public class Util { * @param y * @return the element at given coordinates */ - public static native com.google.gwt.user.client.Element getElementFromPoint( - int clientX, int clientY) - /*-{ - var el = $wnd.document.elementFromPoint(clientX, clientY); - // Call elementFromPoint two times to make sure IE8 also returns something sensible if the application is running in an iframe - el = $wnd.document.elementFromPoint(clientX, clientY); - if(el != null && el.nodeType == 3) { - el = el.parentNode; - } - return el; - }-*/; + @Deprecated + public static com.google.gwt.user.client.Element getElementFromPoint( + int clientX, int clientY) { + return DOM.asOld(WidgetUtil.getElementFromPoint(clientX, clientY)); + } /** * This helper method can be called if components size have been changed @@ -159,37 +134,20 @@ public class Util { return null; } + @Deprecated public static float parseRelativeSize(String size) { - if (size == null || !size.endsWith("%")) { - return -1; - } - - try { - return Float.parseFloat(size.substring(0, size.length() - 1)); - } catch (Exception e) { - VConsole.log("Unable to parse relative size"); - return -1; - } + return WidgetUtil.parseRelativeSize(size); } - private static final Element escapeHtmlHelper = DOM.createDiv(); - /** * Converts html entities to text. * * @param html * @return escaped string presentation of given html */ + @Deprecated public static String escapeHTML(String html) { - DOM.setInnerText(escapeHtmlHelper, html); - String escapedText = DOM.getInnerHTML(escapeHtmlHelper); - if (BrowserInfo.get().isIE8()) { - // #7478 IE8 "incorrectly" returns "<br>" for newlines set using - // setInnerText. The same for " " which is converted to " " - escapedText = escapedText.replaceAll("<(BR|br)>", "\n"); - escapedText = escapedText.replaceAll(" ", " "); - } - return escapedText; + return WidgetUtil.escapeHTML(html); } /** @@ -199,16 +157,9 @@ public class Util { * The string to escape * @return An escaped version of <literal>attribute</literal>. */ + @Deprecated public static String escapeAttribute(String attribute) { - if (attribute == null) { - return ""; - } - attribute = attribute.replace("\"", """); - attribute = attribute.replace("'", "'"); - attribute = attribute.replace(">", ">"); - attribute = attribute.replace("<", "<"); - attribute = attribute.replace("&", "&"); - return attribute; + return WidgetUtil.escapeAttribute(attribute); } /** @@ -221,222 +172,74 @@ public class Util { * clone child tree also * @return */ - public static native com.google.gwt.user.client.Element cloneNode( - Element element, boolean deep) - /*-{ - return element.cloneNode(deep); - }-*/; + @Deprecated + public static com.google.gwt.user.client.Element cloneNode(Element element, + boolean deep) { + return DOM.asOld(WidgetUtil.cloneNode(element, deep)); + } + @Deprecated public static int measureHorizontalPaddingAndBorder(Element element, int paddingGuess) { - String originalWidth = DOM.getStyleAttribute(element, "width"); - - int originalOffsetWidth = element.getOffsetWidth(); - int widthGuess = (originalOffsetWidth - paddingGuess); - if (widthGuess < 1) { - widthGuess = 1; - } - element.getStyle().setWidth(widthGuess, Unit.PX); - int padding = element.getOffsetWidth() - widthGuess; - - element.getStyle().setProperty("width", originalWidth); - - return padding; + return WidgetUtil.measureHorizontalPaddingAndBorder(element, + paddingGuess); } + @Deprecated public static int measureVerticalPaddingAndBorder(Element element, int paddingGuess) { - String originalHeight = DOM.getStyleAttribute(element, "height"); - int originalOffsetHeight = element.getOffsetHeight(); - int widthGuess = (originalOffsetHeight - paddingGuess); - if (widthGuess < 1) { - widthGuess = 1; - } - element.getStyle().setHeight(widthGuess, Unit.PX); - int padding = element.getOffsetHeight() - widthGuess; - - element.getStyle().setProperty("height", originalHeight); - return padding; + return WidgetUtil + .measureVerticalPaddingAndBorder(element, paddingGuess); } + @Deprecated public static int measureHorizontalBorder(Element element) { - int borders; - - if (BrowserInfo.get().isIE()) { - String width = element.getStyle().getProperty("width"); - String height = element.getStyle().getProperty("height"); - - int offsetWidth = element.getOffsetWidth(); - int offsetHeight = element.getOffsetHeight(); - if (offsetHeight < 1) { - offsetHeight = 1; - } - if (offsetWidth < 1) { - offsetWidth = 10; - } - element.getStyle().setPropertyPx("height", offsetHeight); - element.getStyle().setPropertyPx("width", offsetWidth); - - borders = element.getOffsetWidth() - element.getClientWidth(); - - element.getStyle().setProperty("width", width); - element.getStyle().setProperty("height", height); - } else { - borders = element.getOffsetWidth() - - element.getPropertyInt("clientWidth"); - } - assert borders >= 0; - - return borders; + return WidgetUtil.measureHorizontalBorder(element); } + @Deprecated public static int measureVerticalBorder(Element element) { - int borders; - if (BrowserInfo.get().isIE()) { - String width = element.getStyle().getProperty("width"); - String height = element.getStyle().getProperty("height"); - - int offsetWidth = element.getOffsetWidth(); - int offsetHeight = element.getOffsetHeight(); - if (offsetHeight < 1) { - offsetHeight = 1; - } - if (offsetWidth < 1) { - offsetWidth = 10; - } - element.getStyle().setPropertyPx("width", offsetWidth); - - element.getStyle().setPropertyPx("height", offsetHeight); - - borders = element.getOffsetHeight() - - element.getPropertyInt("clientHeight"); - - element.getStyle().setProperty("height", height); - element.getStyle().setProperty("width", width); - } else { - borders = element.getOffsetHeight() - - element.getPropertyInt("clientHeight"); - } - assert borders >= 0; - - return borders; + return WidgetUtil.measureVerticalBorder(element); } + @Deprecated public static int measureMarginLeft(Element element) { - return element.getAbsoluteLeft() - - element.getParentElement().getAbsoluteLeft(); + return WidgetUtil.measureMarginLeft(element); } + @Deprecated public static int setHeightExcludingPaddingAndBorder(Widget widget, String height, int paddingBorderGuess) { - if (height.equals("")) { - setHeight(widget, ""); - return paddingBorderGuess; - } else if (height.endsWith("px")) { - int pixelHeight = Integer.parseInt(height.substring(0, - height.length() - 2)); - return setHeightExcludingPaddingAndBorder(widget.getElement(), - pixelHeight, paddingBorderGuess, false); - } else { - // Set the height in unknown units - setHeight(widget, height); - // Use the offsetWidth - return setHeightExcludingPaddingAndBorder(widget.getElement(), - widget.getOffsetHeight(), paddingBorderGuess, true); - } - } - - private static void setWidth(Widget widget, String width) { - widget.getElement().getStyle().setProperty("width", width); - } - - private static void setHeight(Widget widget, String height) { - widget.getElement().getStyle().setProperty("height", height); + return WidgetUtil.setHeightExcludingPaddingAndBorder(widget, height, + paddingBorderGuess); } + @Deprecated public static int setWidthExcludingPaddingAndBorder(Widget widget, String width, int paddingBorderGuess) { - if (width.equals("")) { - setWidth(widget, ""); - return paddingBorderGuess; - } else if (width.endsWith("px")) { - int pixelWidth = Integer.parseInt(width.substring(0, - width.length() - 2)); - return setWidthExcludingPaddingAndBorder(widget.getElement(), - pixelWidth, paddingBorderGuess, false); - } else { - setWidth(widget, width); - return setWidthExcludingPaddingAndBorder(widget.getElement(), - widget.getOffsetWidth(), paddingBorderGuess, true); - } + return WidgetUtil.setWidthExcludingPaddingAndBorder(widget, width, + paddingBorderGuess); } + @Deprecated public static int setWidthExcludingPaddingAndBorder(Element element, int requestedWidth, int horizontalPaddingBorderGuess, boolean requestedWidthIncludesPaddingBorder) { - - int widthGuess = requestedWidth - horizontalPaddingBorderGuess; - if (widthGuess < 0) { - widthGuess = 0; - } - - element.getStyle().setWidth(widthGuess, Unit.PX); - int captionOffsetWidth = DOM.getElementPropertyInt(element, - "offsetWidth"); - - int actualPadding = captionOffsetWidth - widthGuess; - - if (requestedWidthIncludesPaddingBorder) { - actualPadding += actualPadding; - } - - if (actualPadding != horizontalPaddingBorderGuess) { - int w = requestedWidth - actualPadding; - if (w < 0) { - // Cannot set negative width even if we would want to - w = 0; - } - element.getStyle().setWidth(w, Unit.PX); - - } - - return actualPadding; - + return WidgetUtil.setWidthExcludingPaddingAndBorder(element, + requestedWidth, horizontalPaddingBorderGuess, + requestedWidthIncludesPaddingBorder); } + @Deprecated public static int setHeightExcludingPaddingAndBorder(Element element, int requestedHeight, int verticalPaddingBorderGuess, boolean requestedHeightIncludesPaddingBorder) { - - int heightGuess = requestedHeight - verticalPaddingBorderGuess; - if (heightGuess < 0) { - heightGuess = 0; - } - - element.getStyle().setHeight(heightGuess, Unit.PX); - int captionOffsetHeight = DOM.getElementPropertyInt(element, - "offsetHeight"); - - int actualPadding = captionOffsetHeight - heightGuess; - - if (requestedHeightIncludesPaddingBorder) { - actualPadding += actualPadding; - } - - if (actualPadding != verticalPaddingBorderGuess) { - int h = requestedHeight - actualPadding; - if (h < 0) { - // Cannot set negative height even if we would want to - h = 0; - } - element.getStyle().setHeight(h, Unit.PX); - - } - - return actualPadding; - + return WidgetUtil.setHeightExcludingPaddingAndBorder(element, + requestedHeight, verticalPaddingBorderGuess, + requestedHeightIncludesPaddingBorder); } + @Deprecated public static String getSimpleName(Object widget) { if (widget == null) { return "(null)"; @@ -446,31 +249,14 @@ public class Util { return name.substring(name.lastIndexOf('.') + 1); } + @Deprecated public static void setFloat(Element element, String value) { - if (BrowserInfo.get().isIE()) { - element.getStyle().setProperty("styleFloat", value); - } else { - element.getStyle().setProperty("cssFloat", value); - } + WidgetUtil.setFloat(element, value); } - private static int detectedScrollbarSize = -1; - + @Deprecated public static int getNativeScrollbarSize() { - if (detectedScrollbarSize < 0) { - Element scroller = DOM.createDiv(); - scroller.getStyle().setProperty("width", "50px"); - scroller.getStyle().setProperty("height", "50px"); - scroller.getStyle().setProperty("overflow", "scroll"); - scroller.getStyle().setProperty("position", "absolute"); - scroller.getStyle().setProperty("marginLeft", "-5000px"); - RootPanel.getBodyElement().appendChild(scroller); - detectedScrollbarSize = scroller.getOffsetWidth() - - scroller.getPropertyInt("clientWidth"); - - RootPanel.getBodyElement().removeChild(scroller); - } - return detectedScrollbarSize; + return WidgetUtil.getNativeScrollbarSize(); } /** @@ -480,15 +266,9 @@ public class Util { * @param elem * with overflow auto */ + @Deprecated public static void runWebkitOverflowAutoFixDeferred(final Element elem) { - Scheduler.get().scheduleDeferred(new Command() { - - @Override - public void execute() { - Util.runWebkitOverflowAutoFix(elem); - } - }); - + WidgetUtil.runWebkitOverflowAutoFixDeferred(elem); } /** @@ -499,66 +279,9 @@ public class Util { * @param elem * with overflow auto */ + @Deprecated public static void runWebkitOverflowAutoFix(final Element elem) { - // Add max version if fix lands sometime to Webkit - // Starting from Opera 11.00, also a problem in Opera - if (BrowserInfo.get().requiresOverflowAutoFix()) { - final String originalOverflow = elem.getStyle().getProperty( - "overflow"); - if ("hidden".equals(originalOverflow)) { - return; - } - - // check the scrolltop value before hiding the element - final int scrolltop = elem.getScrollTop(); - final int scrollleft = elem.getScrollLeft(); - elem.getStyle().setProperty("overflow", "hidden"); - - Scheduler.get().scheduleDeferred(new Command() { - @Override - public void execute() { - // Dough, Safari scroll auto means actually just a moped - elem.getStyle().setProperty("overflow", originalOverflow); - - if (scrolltop > 0 || elem.getScrollTop() > 0) { - int scrollvalue = scrolltop; - if (scrollvalue == 0) { - // mysterious are the ways of webkits scrollbar - // handling. In some cases webkit reports bad (0) - // scrolltop before hiding the element temporary, - // sometimes after. - scrollvalue = elem.getScrollTop(); - } - // fix another bug where scrollbar remains in wrong - // position - elem.setScrollTop(scrollvalue - 1); - elem.setScrollTop(scrollvalue); - } - - // fix for #6940 : Table horizontal scroll sometimes not - // updated when collapsing/expanding columns - // Also appeared in Safari 5.1 with webkit 534 (#7667) - if ((BrowserInfo.get().isChrome() || (BrowserInfo.get() - .isSafari() && BrowserInfo.get().getWebkitVersion() >= 534)) - && (scrollleft > 0 || elem.getScrollLeft() > 0)) { - int scrollvalue = scrollleft; - - if (scrollvalue == 0) { - // mysterious are the ways of webkits scrollbar - // handling. In some cases webkit may report a bad - // (0) scrollleft before hiding the element - // temporary, sometimes after. - scrollvalue = elem.getScrollLeft(); - } - // fix another bug where scrollbar remains in wrong - // position - elem.setScrollLeft(scrollvalue - 1); - elem.setScrollLeft(scrollvalue); - } - } - }); - } - + WidgetUtil.runWebkitOverflowAutoFix(elem); } /** @@ -576,8 +299,8 @@ public class Util { return null; } - float relativeWidth = Util.parseRelativeSize(state.width); - float relativeHeight = Util.parseRelativeSize(state.height); + float relativeWidth = WidgetUtil.parseRelativeSize(state.width); + float relativeHeight = WidgetUtil.parseRelativeSize(state.height); FloatSize relativeSize = new FloatSize(relativeWidth, relativeHeight); return relativeSize; @@ -589,10 +312,9 @@ public class Util { return uidl.getBooleanAttribute("cached"); } + @Deprecated public static void alert(String string) { - if (true) { - Window.alert(string); - } + WidgetUtil.alert(string); } /** @@ -625,21 +347,9 @@ public class Util { * The element to check * @return The border-box width for the element */ + @Deprecated public static int getRequiredWidth(com.google.gwt.dom.client.Element element) { - int reqWidth = getRequiredWidthBoundingClientRect(element); - if (BrowserInfo.get().isIE() && !BrowserInfo.get().isIE8()) { - int csSize = getRequiredWidthComputedStyle(element); - if (csSize == reqWidth + 1) { - // If computed style reports one pixel larger than requiredWidth - // we would be rounding in the wrong direction in IE9. Round up - // instead. - // We do not always use csSize as it e.g. for 100% wide Labels - // in GridLayouts produces senseless values (see e.g. - // ThemeTestUI with Runo). - return csSize; - } - } - return reqWidth; + return WidgetUtil.getRequiredWidth(element); } /** @@ -650,94 +360,44 @@ public class Util { * The element to check * @return The border-box height for the element */ + @Deprecated public static int getRequiredHeight( com.google.gwt.dom.client.Element element) { - int reqHeight = getRequiredHeightBoundingClientRect(element); - if (BrowserInfo.get().isIE() && !BrowserInfo.get().isIE8()) { - int csSize = getRequiredHeightComputedStyle(element); - if (csSize == reqHeight + 1) { - // If computed style reports one pixel larger than - // requiredHeight we would be rounding in the wrong direction in - // IE9. Round up instead. - // We do not always use csSize as it e.g. for 100% wide Labels - // in GridLayouts produces senseless values (see e.g. - // ThemeTestUI with Runo). - return csSize; - } - } - return reqHeight; + return WidgetUtil.getRequiredHeight(element); } - public static native int getRequiredWidthBoundingClientRect( - com.google.gwt.dom.client.Element element) - /*-{ - if (element.getBoundingClientRect) { - var rect = element.getBoundingClientRect(); - return Math.ceil(rect.right - rect.left); - } else { - return element.offsetWidth; - } - }-*/; + @Deprecated + public int getRequiredWidthBoundingClientRect( + com.google.gwt.dom.client.Element element) { + return WidgetUtil.getRequiredWidthBoundingClientRect(element); + } - public static native int getRequiredHeightComputedStyle( - com.google.gwt.dom.client.Element element) - /*-{ - var cs = element.ownerDocument.defaultView.getComputedStyle(element); - var heightPx = cs.height; - if(heightPx == 'auto'){ - // Fallback for when IE reports auto - heightPx = @com.vaadin.client.Util::getRequiredHeightBoundingClientRect(Lcom/google/gwt/dom/client/Element;)(element) + 'px'; - } - var borderTopPx = cs.borderTop; - var borderBottomPx = cs.borderBottom; - var paddingTopPx = cs.paddingTop; - var paddingBottomPx = cs.paddingBottom; - - var height = heightPx.substring(0,heightPx.length-2); - var border = borderTopPx.substring(0,borderTopPx.length-2)+borderBottomPx.substring(0,borderBottomPx.length-2); - var padding = paddingTopPx.substring(0,paddingTopPx.length-2)+paddingBottomPx.substring(0,paddingBottomPx.length-2); - return Math.ceil(height+border+padding); - }-*/; - - public static native int getRequiredWidthComputedStyle( - com.google.gwt.dom.client.Element element) - /*-{ - var cs = element.ownerDocument.defaultView.getComputedStyle(element); - var widthPx = cs.width; - if(widthPx == 'auto'){ - // Fallback for when IE reports auto - widthPx = @com.vaadin.client.Util::getRequiredWidthBoundingClientRect(Lcom/google/gwt/dom/client/Element;)(element) + 'px'; - } - var borderLeftPx = cs.borderLeft; - var borderRightPx = cs.borderRight; - var paddingLeftPx = cs.paddingLeft; - var paddingRightPx = cs.paddingRight; - - var width = widthPx.substring(0,widthPx.length-2); - var border = borderLeftPx.substring(0,borderLeftPx.length-2)+borderRightPx.substring(0,borderRightPx.length-2); - var padding = paddingLeftPx.substring(0,paddingLeftPx.length-2)+paddingRightPx.substring(0,paddingRightPx.length-2); - return Math.ceil(width+border+padding); - }-*/; - - public static native int getRequiredHeightBoundingClientRect( - com.google.gwt.dom.client.Element element) - /*-{ - var height; - if (element.getBoundingClientRect != null) { - var rect = element.getBoundingClientRect(); - height = Math.ceil(rect.bottom - rect.top); - } else { - height = element.offsetHeight; - } - return height; - }-*/; + @Deprecated + public static int getRequiredHeightComputedStyle( + com.google.gwt.dom.client.Element element) { + return WidgetUtil.getRequiredHeightComputedStyle(element); + } + + @Deprecated + public static int getRequiredWidthComputedStyle( + com.google.gwt.dom.client.Element element) { + return WidgetUtil.getRequiredWidthComputedStyle(element); + } + + @Deprecated + public static int getRequiredHeightBoundingClientRect( + com.google.gwt.dom.client.Element element) { + return WidgetUtil.getRequiredHeightBoundingClientRect(element); + } + @Deprecated public static int getRequiredWidth(Widget widget) { - return getRequiredWidth(widget.getElement()); + return WidgetUtil.getRequiredWidth(widget); } + @Deprecated public static int getRequiredHeight(Widget widget) { - return getRequiredHeight(widget.getElement()); + return WidgetUtil.getRequiredHeight(widget); } /** @@ -747,53 +407,12 @@ public class Util { * the element to detect * @return true if auto or scroll */ + @Deprecated public static boolean mayHaveScrollBars(com.google.gwt.dom.client.Element pe) { - String overflow = getComputedStyle(pe, "overflow"); - if (overflow != null) { - if (overflow.equals("auto") || overflow.equals("scroll")) { - return true; - } else { - return false; - } - } else { - return false; - } + return WidgetUtil.mayHaveScrollBars(pe); } /** - * A simple helper method to detect "computed style" (aka style sheets + - * element styles). Values returned differ a lot depending on browsers. - * Always be very careful when using this. - * - * @param el - * the element from which the style property is detected - * @param p - * the property to detect - * @return String value of style property - */ - private static native String getComputedStyle( - com.google.gwt.dom.client.Element el, String p) - /*-{ - try { - - if (el.currentStyle) { - // IE - return el.currentStyle[p]; - } else if (window.getComputedStyle) { - // Sa, FF, Opera - var view = el.ownerDocument.defaultView; - return view.getComputedStyle(el,null).getPropertyValue(p); - } else { - // fall back for non IE, Sa, FF, Opera - return ""; - } - } catch (e) { - return ""; - } - - }-*/; - - /** * Locates the nested child component of <literal>parent</literal> which * contains the element <literal>element</literal>. The child component is * also returned if "element" is part of its caption. If @@ -863,14 +482,10 @@ public class Util { * @param el * the element to focus */ - public static native void focus(Element el) - /*-{ - try { - el.focus(); - } catch (e) { - - } - }-*/; + @Deprecated + public static void focus(Element el) { + WidgetUtil.focus(el); + } /** * Helper method to find the nearest parent paintable instance by traversing @@ -899,33 +514,10 @@ public class Util { * @param class1 * the Widget type to seek for */ - @SuppressWarnings("unchecked") + @Deprecated public static <T> T findWidget(Element element, Class<? extends Widget> class1) { - if (element != null) { - /* First seek for the first EventListener (~Widget) from dom */ - EventListener eventListener = null; - while (eventListener == null && element != null) { - eventListener = Event.getEventListener(element); - if (eventListener == null) { - element = element.getParentElement(); - } - } - if (eventListener instanceof Widget) { - /* - * Then find the first widget of type class1 from widget - * hierarchy - */ - Widget w = (Widget) eventListener; - while (w != null) { - if (class1 == null || w.getClass() == class1) { - return (T) w; - } - w = w.getParent(); - } - } - } - return null; + return WidgetUtil.findWidget(element, class1); } /** @@ -934,14 +526,9 @@ public class Util { * @param element * The element that should be redrawn */ + @Deprecated public static void forceWebkitRedraw(Element element) { - Style style = element.getStyle(); - String s = style.getProperty("webkitTransform"); - if (s == null || s.length() == 0) { - style.setProperty("webkitTransform", "scale(1)"); - } else { - style.setProperty("webkitTransform", ""); - } + WidgetUtil.forceWebkitRedraw(element); } /** @@ -952,10 +539,9 @@ public class Util { * @param e * The element to perform the hack on */ + @Deprecated public static final void forceIE8Redraw(Element e) { - if (BrowserInfo.get().isIE8()) { - forceIERedraw(e); - } + WidgetUtil.forceIE8Redraw(e); } /** @@ -967,10 +553,9 @@ public class Util { * @param e * The element to perform the hack on */ + @Deprecated public static void forceIERedraw(Element e) { - if (BrowserInfo.get().isIE()) { - setStyleTemporarily(e, "zoom", "1"); - } + WidgetUtil.forceIERedraw(e); } /** @@ -982,33 +567,14 @@ public class Util { * @param element * The element to detach and re-attach */ + @Deprecated public static void detachAttach(Element element) { - if (element == null) { - return; - } - - Node nextSibling = element.getNextSibling(); - Node parent = element.getParentNode(); - if (parent == null) { - return; - } - - parent.removeChild(element); - if (nextSibling == null) { - parent.appendChild(element); - } else { - parent.insertBefore(element, nextSibling); - } - + WidgetUtil.detachAttach(element); } + @Deprecated public static void sinkOnloadForImages(Element element) { - NodeList<com.google.gwt.dom.client.Element> imgElements = element - .getElementsByTagName("img"); - for (int i = 0; i < imgElements.getLength(); i++) { - DOM.sinkEvents(imgElements.getItem(i), Event.ONLOAD); - } - + WidgetUtil.sinkOnloadForImages(element); } /** @@ -1017,14 +583,9 @@ public class Util { * @param subElement * @return */ + @Deprecated public static int getChildElementIndex(Element childElement) { - int idx = 0; - Node n = childElement; - while ((n = n.getPreviousSibling()) != null) { - idx++; - } - - return idx; + return WidgetUtil.getChildElementIndex(childElement); } private static void printConnectorInvocations( @@ -1097,15 +658,10 @@ public class Util { * @param tempValue * The temporary value */ + @Deprecated public static void setStyleTemporarily(Element element, final String styleProperty, String tempValue) { - final Style style = element.getStyle(); - final String currentValue = style.getProperty(styleProperty); - - style.setProperty(styleProperty, tempValue); - element.getOffsetWidth(); - style.setProperty(styleProperty, currentValue); - + WidgetUtil.setStyleTemporarily(element, styleProperty, tempValue); } /** @@ -1116,12 +672,9 @@ public class Util { * @param event * @return */ + @Deprecated public static int getTouchOrMouseClientX(Event event) { - if (isTouchEvent(event)) { - return event.getChangedTouches().get(0).getClientX(); - } else { - return event.getClientX(); - } + return WidgetUtil.getTouchOrMouseClientX(event); } /** @@ -1133,12 +686,10 @@ public class Util { * the mouse event to get coordinates from * @return the element at the coordinates of the event */ + @Deprecated public static com.google.gwt.user.client.Element getElementUnderMouse( NativeEvent event) { - int pageX = getTouchOrMouseClientX(event); - int pageY = getTouchOrMouseClientY(event); - - return getElementFromPoint(pageX, pageY); + return DOM.asOld(WidgetUtil.getElementUnderMouse(event)); } /** @@ -1149,12 +700,9 @@ public class Util { * @param event * @return */ + @Deprecated public static int getTouchOrMouseClientY(Event event) { - if (isTouchEvent(event)) { - return event.getChangedTouches().get(0).getClientY(); - } else { - return event.getClientY(); - } + return WidgetUtil.getTouchOrMouseClientY(event); } /** @@ -1163,8 +711,9 @@ public class Util { * @param currentGwtEvent * @return */ + @Deprecated public static int getTouchOrMouseClientY(NativeEvent currentGwtEvent) { - return getTouchOrMouseClientY(Event.as(currentGwtEvent)); + return WidgetUtil.getTouchOrMouseClientY(currentGwtEvent); } /** @@ -1173,67 +722,25 @@ public class Util { * @param event * @return */ + @Deprecated public static int getTouchOrMouseClientX(NativeEvent event) { - return getTouchOrMouseClientX(Event.as(event)); + return WidgetUtil.getTouchOrMouseClientX(event); } + @Deprecated public static boolean isTouchEvent(Event event) { - return event.getType().contains("touch"); + return WidgetUtil.isTouchEvent(event); } + @Deprecated public static boolean isTouchEvent(NativeEvent event) { - return isTouchEvent(Event.as(event)); + return WidgetUtil.isTouchEvent(event); } + @Deprecated public static void simulateClickFromTouchEvent(Event touchevent, Widget widget) { - Touch touch = touchevent.getChangedTouches().get(0); - final NativeEvent createMouseUpEvent = Document.get() - .createMouseUpEvent(0, touch.getScreenX(), touch.getScreenY(), - touch.getClientX(), touch.getClientY(), false, false, - false, false, NativeEvent.BUTTON_LEFT); - final NativeEvent createMouseDownEvent = Document.get() - .createMouseDownEvent(0, touch.getScreenX(), - touch.getScreenY(), touch.getClientX(), - touch.getClientY(), false, false, false, false, - NativeEvent.BUTTON_LEFT); - final NativeEvent createMouseClickEvent = Document.get() - .createClickEvent(0, touch.getScreenX(), touch.getScreenY(), - touch.getClientX(), touch.getClientY(), false, false, - false, false); - - /* - * Get target with element from point as we want the actual element, not - * the one that sunk the event. - */ - final Element target = getElementFromPoint(touch.getClientX(), - touch.getClientY()); - - /* - * Fixes infocusable form fields in Safari of iOS 5.x and some Android - * browsers. - */ - Widget targetWidget = findWidget(target, null); - if (targetWidget instanceof com.google.gwt.user.client.ui.Focusable) { - final com.google.gwt.user.client.ui.Focusable toBeFocusedWidget = (com.google.gwt.user.client.ui.Focusable) targetWidget; - toBeFocusedWidget.setFocus(true); - } else if (targetWidget instanceof Focusable) { - ((Focusable) targetWidget).focus(); - } - - Scheduler.get().scheduleDeferred(new ScheduledCommand() { - @Override - public void execute() { - try { - target.dispatchEvent(createMouseDownEvent); - target.dispatchEvent(createMouseUpEvent); - target.dispatchEvent(createMouseClickEvent); - } catch (Exception e) { - } - - } - }); - + WidgetUtil.simulateClickFromTouchEvent(touchevent, widget); } /** @@ -1241,14 +748,10 @@ public class Util { * * @return The active element or null if no active element could be found. */ - public native static com.google.gwt.user.client.Element getFocusedElement() - /*-{ - if ($wnd.document.activeElement) { - return $wnd.document.activeElement; - } - - return null; - }-*/; + @Deprecated + public static com.google.gwt.user.client.Element getFocusedElement() { + return DOM.asOld(WidgetUtil.getFocusedElement()); + } /** * Gets the currently focused element for Internet Explorer. @@ -1268,18 +771,9 @@ public class Util { * * @return true if focused element is editable */ + @Deprecated public static boolean isFocusedElementEditable() { - Element focusedElement = Util.getFocusedElement(); - if (focusedElement != null) { - String tagName = focusedElement.getTagName(); - String contenteditable = focusedElement - .getAttribute("contenteditable"); - - return "textarea".equalsIgnoreCase(tagName) - || "input".equalsIgnoreCase(tagName) - || "true".equalsIgnoreCase(contenteditable); - } - return false; + return WidgetUtil.isFocusedElementEditable(); } /** @@ -1291,30 +785,9 @@ public class Util { * @param widget * @return true if attached and displayed */ + @Deprecated public static boolean isAttachedAndDisplayed(Widget widget) { - if (widget.isAttached()) { - /* - * Failfast using offset size, then by iterating the widget tree - */ - boolean notZeroSized = widget.getOffsetHeight() > 0 - || widget.getOffsetWidth() > 0; - return notZeroSized || checkVisibilityRecursively(widget); - } else { - return false; - } - } - - private static boolean checkVisibilityRecursively(Widget widget) { - if (widget.isVisible()) { - Widget parent = widget.getParent(); - if (parent == null) { - return true; // root panel - } else { - return checkVisibilityRecursively(parent); - } - } else { - return false; - } + return WidgetUtil.isAttachedAndDisplayed(widget); } /** @@ -1324,33 +797,10 @@ public class Util { * @param elem * The element to scroll into view */ - public static native void scrollIntoViewVertically(Element elem) - /*-{ - var top = elem.offsetTop; - var height = elem.offsetHeight; - - if (elem.parentNode != elem.offsetParent) { - top -= elem.parentNode.offsetTop; - } - - var cur = elem.parentNode; - while (cur && (cur.nodeType == 1)) { - if (top < cur.scrollTop) { - cur.scrollTop = top; - } - if (top + height > cur.scrollTop + cur.clientHeight) { - cur.scrollTop = (top + height) - cur.clientHeight; - } - - var offsetTop = cur.offsetTop; - if (cur.parentNode != cur.offsetParent) { - offsetTop -= cur.parentNode.offsetTop; - } - - top += offsetTop - cur.scrollTop; - cur = cur.parentNode; - } - }-*/; + @Deprecated + public static void scrollIntoViewVertically(Element elem) { + WidgetUtil.scrollIntoViewVertically(elem); + } /** * Checks if the given event is either a touch event or caused by the left @@ -1360,9 +810,9 @@ public class Util { * @return true if the event is a touch event or caused by the left mouse * button, false otherwise */ + @Deprecated public static boolean isTouchEventOrLeftMouseButton(Event event) { - boolean touchEvent = Util.isTouchEvent(event); - return touchEvent || event.getButton() == Event.BUTTON_LEFT; + return WidgetUtil.isTouchEventOrLeftMouseButton(event); } /** @@ -1418,26 +868,9 @@ public class Util { * a string with the relative URL to resolve * @return the corresponding absolute URL as a string */ + @Deprecated public static String getAbsoluteUrl(String url) { - if (BrowserInfo.get().isIE8()) { - // The hard way - must use innerHTML and attach to DOM in IE8 - DivElement divElement = Document.get().createDivElement(); - divElement.getStyle().setDisplay(Display.NONE); - - RootPanel.getBodyElement().appendChild(divElement); - divElement.setInnerHTML("<a href='" + escapeAttribute(url) - + "' ></a>"); - - AnchorElement a = divElement.getChild(0).cast(); - String href = a.getHref(); - - RootPanel.getBodyElement().removeChild(divElement); - return href; - } else { - AnchorElement a = Document.get().createAnchorElement(); - a.setHref(url); - return a.getHref(); - } + return WidgetUtil.getAbsoluteUrl(url); } /** @@ -1461,169 +894,70 @@ public class Util { * * @since 7.3 */ - public native static void setSelectionRange(Element elem, int pos, - int length, String direction) - /*-{ - try { - elem.setSelectionRange(pos, pos + length, direction); - } catch (e) { - // Firefox throws exception if TextBox is not visible, even if attached - } - }-*/; + @Deprecated + public static void setSelectionRange(Element elem, int pos, int length, + String direction) { + WidgetUtil.setSelectionRange(elem, pos, length, direction); + } /** - * Wrap a css size value and its unit and translate back and forth to the - * string representation.<br/> - * Eg. 50%, 123px, ... - * - * @since 7.2.6 - * @author Vaadin Ltd + * Converts a native {@link JavaScriptObject} into a {@link JsonValue}. This + * is a no-op in GWT code compiled to javascript, but needs some special + * handling to work when run in JVM. + * + * @param jso + * the java script object to represent as json + * @return the json representation */ - @SuppressWarnings("serial") - public static class CssSize implements Serializable { - - /* - * Map the size units with their type. - */ - private static Map<String, Unit> type2Unit = new HashMap<String, Style.Unit>(); - static { - for (Unit unit : Unit.values()) { - type2Unit.put(unit.getType(), unit); - } - } - - /** - * Gets the unit value by its type. - * - * @param type - * the type of the unit as found in the style. - * @return the unit value. - */ - public static Unit unitByType(String type) { - return type2Unit.get(type); - } - - /* - * Regex to parse the size. - */ - private static final RegExp sizePattern = RegExp - .compile(SharedUtil.SIZE_PATTERN); - - /** - * Parse the size from string format to {@link CssSize}. - * - * @param s - * the size as string. - * @return a {@link CssSize} object. - */ - public static CssSize fromString(String s) { - if (s == null) { - return null; - } - - s = s.trim(); - if ("".equals(s)) { - return null; - } - - float size = 0; - Unit unit = null; - - MatchResult matcher = sizePattern.exec(s); - if (matcher.getGroupCount() > 1) { - - size = Float.parseFloat(matcher.getGroup(1)); - if (size < 0) { - size = -1; - unit = Unit.PX; - - } else { - String symbol = matcher.getGroup(2); - unit = unitByType(symbol); - } - } else { - throw new IllegalArgumentException("Invalid size argument: \"" - + s + "\" (should match " + sizePattern.getSource() - + ")"); - } - return new CssSize(size, unit); - } - - /** - * Creates a {@link CssSize} using a value and its measurement unit. - * - * @param value - * the value. - * @param unit - * the unit. - * @return the {@link CssSize} object. - */ - public static CssSize fromValueUnit(float value, Unit unit) { - return new CssSize(value, unit); - } - - /* - * The value. - */ - private final float value; - - /* - * The measure unit. - */ - private final Unit unit; - - private CssSize(float value, Unit unit) { - this.value = value; - this.unit = unit; - } - - /** - * Gets the value for this css size. - * - * @return the value. - */ - public float getValue() { - return value; - } - - /** - * Gets the measurement unit for this css size. - * - * @return the unit. - */ - public Unit getUnit() { - return unit; - } - - @Override - public String toString() { - return value + unit.getType(); + public static <T extends JsonValue> T jso2json(JavaScriptObject jso) { + if (GWT.isProdMode()) { + return (T) jso.<JsJsonValue> cast(); + } else { + return elemental.json.Json.instance().parse(stringify(jso)); } + } - @Override - public boolean equals(Object obj) { - if (obj instanceof CssSize) { - CssSize size = (CssSize) obj; - return size.value == value && size.unit == unit; - } - - return false; + /** + * Converts a {@link JsonValue} into a native {@link JavaScriptObject}. This + * is a no-op in GWT code compiled to javascript, but needs some special + * handling to work when run in JVM. + * + * @param jsonValue + * the json value + * @return a native javascript object representation of the json value + */ + public static JavaScriptObject json2jso(JsonValue jsonValue) { + if (GWT.isProdMode()) { + return ((JavaScriptObject) jsonValue.toNative()).cast(); + } else { + return parse(jsonValue.toJson()); } + } - /** - * Check whether the two sizes are equals. - * - * @param cssSize1 - * the first size to compare. - * @param cssSize2 - * the other size to compare with the first one. - * @return true if the two sizes are equals, otherwise false. - */ - public static boolean equals(String cssSize1, String cssSize2) { - return CssSize.fromString(cssSize1).equals( - CssSize.fromString(cssSize2)); - } + /** + * Convert a {@link JavaScriptObject} into a string representation. + * + * @param json + * a JavaScript object to be converted to a string + * @return JSON in string representation + */ + private native static String stringify(JavaScriptObject json) + /*-{ + return JSON.stringify(json); + }-*/; - } + /** + * Parse a string containing JSON into a {@link JavaScriptObject}. + * + * @param <T> + * the overlay type to expect from the parse + * @param jsonAsString + * @return a JavaScript object constructed from the parse + */ + public native static <T extends JavaScriptObject> T parse( + String jsonAsString) + /*-{ + return JSON.parse(jsonAsString); + }-*/; } diff --git a/client/src/com/vaadin/client/VCaption.java b/client/src/com/vaadin/client/VCaption.java index eb19dedf8b..050edae8be 100644 --- a/client/src/com/vaadin/client/VCaption.java +++ b/client/src/com/vaadin/client/VCaption.java @@ -148,7 +148,7 @@ public class VCaption extends HTML { } } if (!owner.isEnabled()) { - style += " " + ApplicationConnection.DISABLED_CLASSNAME; + style += " " + StyleConstants.DISABLED; } setStyleName(style); @@ -328,7 +328,7 @@ public class VCaption extends HTML { String style = VCaption.CLASSNAME; if (disabled) { - style += " " + ApplicationConnection.DISABLED_CLASSNAME; + style += " " + StyleConstants.DISABLED; } setStyleName(style); if (hasDescription) { @@ -510,17 +510,17 @@ public class VCaption extends HTML { int width = 0; if (icon != null) { - width += Util.getRequiredWidth(icon.getElement()); + width += WidgetUtil.getRequiredWidth(icon.getElement()); } if (captionText != null) { - width += Util.getRequiredWidth(captionText); + width += WidgetUtil.getRequiredWidth(captionText); } if (requiredFieldIndicator != null) { - width += Util.getRequiredWidth(requiredFieldIndicator); + width += WidgetUtil.getRequiredWidth(requiredFieldIndicator); } if (errorIndicatorElement != null) { - width += Util.getRequiredWidth(errorIndicatorElement); + width += WidgetUtil.getRequiredWidth(errorIndicatorElement); } return width; @@ -531,7 +531,7 @@ public class VCaption extends HTML { int width = 0; if (icon != null) { - width += Util.getRequiredWidth(icon.getElement()); + width += WidgetUtil.getRequiredWidth(icon.getElement()); } if (captionText != null) { int textWidth = captionText.getScrollWidth(); @@ -540,7 +540,7 @@ public class VCaption extends HTML { * In Firefox3 the caption might require more space than the * scrollWidth returns as scrollWidth is rounded down. */ - int requiredWidth = Util.getRequiredWidth(captionText); + int requiredWidth = WidgetUtil.getRequiredWidth(captionText); if (requiredWidth > textWidth) { textWidth = requiredWidth; } @@ -549,10 +549,10 @@ public class VCaption extends HTML { width += textWidth; } if (requiredFieldIndicator != null) { - width += Util.getRequiredWidth(requiredFieldIndicator); + width += WidgetUtil.getRequiredWidth(requiredFieldIndicator); } if (errorIndicatorElement != null) { - width += Util.getRequiredWidth(errorIndicatorElement); + width += WidgetUtil.getRequiredWidth(errorIndicatorElement); } return width; @@ -564,26 +564,26 @@ public class VCaption extends HTML { int h; if (icon != null) { - h = Util.getRequiredHeight(icon.getElement()); + h = WidgetUtil.getRequiredHeight(icon.getElement()); if (h > height) { height = h; } } if (captionText != null) { - h = Util.getRequiredHeight(captionText); + h = WidgetUtil.getRequiredHeight(captionText); if (h > height) { height = h; } } if (requiredFieldIndicator != null) { - h = Util.getRequiredHeight(requiredFieldIndicator); + h = WidgetUtil.getRequiredHeight(requiredFieldIndicator); if (h > height) { height = h; } } if (errorIndicatorElement != null) { - h = Util.getRequiredHeight(errorIndicatorElement); + h = WidgetUtil.getRequiredHeight(errorIndicatorElement); if (h > height) { height = h; } @@ -619,11 +619,13 @@ public class VCaption extends HTML { // DOM.setStyleAttribute(getElement(), "width", maxWidth + "px"); if (requiredFieldIndicator != null) { - availableWidth -= Util.getRequiredWidth(requiredFieldIndicator); + availableWidth -= WidgetUtil + .getRequiredWidth(requiredFieldIndicator); } if (errorIndicatorElement != null) { - availableWidth -= Util.getRequiredWidth(errorIndicatorElement); + availableWidth -= WidgetUtil + .getRequiredWidth(errorIndicatorElement); } if (availableWidth < 0) { @@ -631,8 +633,8 @@ public class VCaption extends HTML { } if (icon != null) { - int iconRequiredWidth = Util - .getRequiredWidth(icon.getElement()); + int iconRequiredWidth = WidgetUtil.getRequiredWidth(icon + .getElement()); if (availableWidth > iconRequiredWidth) { availableWidth -= iconRequiredWidth; } else { @@ -642,7 +644,7 @@ public class VCaption extends HTML { } } if (captionText != null) { - int captionWidth = Util.getRequiredWidth(captionText); + int captionWidth = WidgetUtil.getRequiredWidth(captionText); if (availableWidth > captionWidth) { availableWidth -= captionWidth; diff --git a/client/src/com/vaadin/client/VLoadingIndicator.java b/client/src/com/vaadin/client/VLoadingIndicator.java index e873005d3a..7c7edeb04f 100644 --- a/client/src/com/vaadin/client/VLoadingIndicator.java +++ b/client/src/com/vaadin/client/VLoadingIndicator.java @@ -154,6 +154,18 @@ public class VLoadingIndicator { } /** + * Triggers displaying of this loading indicator unless it's already visible + * or scheduled to be shown after a delay. + * + * @since 7.4 + */ + public void ensureTriggered() { + if (!isVisible() && !firstTimer.isRunning()) { + trigger(); + } + } + + /** * Shows the loading indicator in its standard state and triggers timers for * transitioning into the "second" and "third" states. */ diff --git a/client/src/com/vaadin/client/VUIDLBrowser.java b/client/src/com/vaadin/client/VUIDLBrowser.java index 4b4fd2f389..08f4c653a5 100644 --- a/client/src/com/vaadin/client/VUIDLBrowser.java +++ b/client/src/com/vaadin/client/VUIDLBrowser.java @@ -33,14 +33,16 @@ import com.google.gwt.dom.client.Style.Unit; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.MouseOutEvent; import com.google.gwt.event.dom.client.MouseOutHandler; -import com.google.gwt.json.client.JSONArray; -import com.google.gwt.json.client.JSONObject; -import com.google.gwt.json.client.JSONValue; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.ui.UnknownComponentConnector; import com.vaadin.client.ui.VWindow; +import elemental.json.JsonArray; +import elemental.json.JsonObject; +import elemental.json.JsonType; +import elemental.json.JsonValue; + /** * @author Vaadin Ltd * @@ -159,7 +161,7 @@ public class VUIDLBrowser extends SimpleTree { } else { setText("Unknown connector (" + connectorId + ")"); } - dir(new JSONObject(stateChanges), this); + dir((JsonObject) Util.jso2json(stateChanges), this); } @Override @@ -167,28 +169,28 @@ public class VUIDLBrowser extends SimpleTree { return connectorId; } - private void dir(String key, JSONValue value, SimpleTree tree) { - if (value.isObject() != null) { + private void dir(String key, JsonValue value, SimpleTree tree) { + if (value.getType() == JsonType.OBJECT) { SimpleTree subtree = new SimpleTree(key + "=object"); tree.add(subtree); - dir(value.isObject(), subtree); - } else if (value.isArray() != null) { + dir((JsonObject) value, subtree); + } else if (value.getType() == JsonType.ARRAY) { SimpleTree subtree = new SimpleTree(key + "=array"); - dir(value.isArray(), subtree); + dir((JsonArray) value, subtree); tree.add(subtree); } else { tree.addItem(key + "=" + value); } } - private void dir(JSONObject state, SimpleTree tree) { - for (String key : state.keySet()) { + private void dir(JsonObject state, SimpleTree tree) { + for (String key : state.keys()) { dir(key, state.get(key), tree); } } - private void dir(JSONArray array, SimpleTree tree) { - for (int i = 0; i < array.size(); ++i) { + private void dir(JsonArray array, SimpleTree tree) { + for (int i = 0; i < array.length(); ++i) { dir("" + i, array.get(i), tree); } } diff --git a/client/src/com/vaadin/client/WidgetUtil.java b/client/src/com/vaadin/client/WidgetUtil.java new file mode 100644 index 0000000000..96f161c4a8 --- /dev/null +++ b/client/src/com/vaadin/client/WidgetUtil.java @@ -0,0 +1,1405 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Logger; + +import com.google.gwt.core.client.Scheduler; +import com.google.gwt.core.client.Scheduler.ScheduledCommand; +import com.google.gwt.dom.client.AnchorElement; +import com.google.gwt.dom.client.DivElement; +import com.google.gwt.dom.client.Document; +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.NativeEvent; +import com.google.gwt.dom.client.Node; +import com.google.gwt.dom.client.NodeList; +import com.google.gwt.dom.client.Style; +import com.google.gwt.dom.client.Style.Display; +import com.google.gwt.dom.client.Style.Unit; +import com.google.gwt.dom.client.Touch; +import com.google.gwt.event.dom.client.KeyEvent; +import com.google.gwt.regexp.shared.MatchResult; +import com.google.gwt.regexp.shared.RegExp; +import com.google.gwt.user.client.Command; +import com.google.gwt.user.client.DOM; +import com.google.gwt.user.client.Event; +import com.google.gwt.user.client.EventListener; +import com.google.gwt.user.client.Window; +import com.google.gwt.user.client.ui.RootPanel; +import com.google.gwt.user.client.ui.Widget; +import com.vaadin.shared.util.SharedUtil; + +/** + * Utility methods which are related to client side code only + */ +public class WidgetUtil { + + /** + * Helper method for debugging purposes. + * + * Stops execution on firefox browsers on a breakpoint. + * + */ + public static native void browserDebugger() + /*-{ + if($wnd.console) + debugger; + }-*/; + + /** + * Helper method for a bug fix #14041. For mozilla getKeyCode return 0 for + * space bar (because space is considered as char). If return 0 use + * getCharCode. + * + * @param event + * @return return key code + * @since 7.2.4 + */ + public static int getKeyCode(KeyEvent<?> event) { + int keyCode = event.getNativeEvent().getKeyCode(); + if (keyCode == 0) { + keyCode = event.getNativeEvent().getCharCode(); + } + return keyCode; + } + + /** + * + * Returns the topmost element of from given coordinates. + * + * TODO fix crossplat issues clientX vs pageX. See quircksmode. Not critical + * for vaadin as we scroll div istead of page. + * + * @param x + * @param y + * @return the element at given coordinates + */ + public static native Element getElementFromPoint(int clientX, int clientY) + /*-{ + var el = $wnd.document.elementFromPoint(clientX, clientY); + // Call elementFromPoint two times to make sure IE8 also returns something sensible if the application is running in an iframe + el = $wnd.document.elementFromPoint(clientX, clientY); + if(el != null && el.nodeType == 3) { + el = el.parentNode; + } + return el; + }-*/; + + public static float parseRelativeSize(String size) { + if (size == null || !size.endsWith("%")) { + return -1; + } + + try { + return Float.parseFloat(size.substring(0, size.length() - 1)); + } catch (Exception e) { + getLogger().warning("Unable to parse relative size"); + return -1; + } + } + + private static final Element escapeHtmlHelper = DOM.createDiv(); + + /** + * Converts html entities to text. + * + * @param html + * @return escaped string presentation of given html + */ + public static String escapeHTML(String html) { + DOM.setInnerText(escapeHtmlHelper, html); + String escapedText = DOM.getInnerHTML(escapeHtmlHelper); + if (BrowserInfo.get().isIE8()) { + // #7478 IE8 "incorrectly" returns "<br>" for newlines set using + // setInnerText. The same for " " which is converted to " " + escapedText = escapedText.replaceAll("<(BR|br)>", "\n"); + escapedText = escapedText.replaceAll(" ", " "); + } + return escapedText; + } + + /** + * Escapes the string so it is safe to write inside an HTML attribute. + * + * @param attribute + * The string to escape + * @return An escaped version of <literal>attribute</literal>. + */ + public static String escapeAttribute(String attribute) { + if (attribute == null) { + return ""; + } + attribute = attribute.replace("\"", """); + attribute = attribute.replace("'", "'"); + attribute = attribute.replace(">", ">"); + attribute = attribute.replace("<", "<"); + attribute = attribute.replace("&", "&"); + return attribute; + } + + /** + * Clones given element as in JavaScript. + * + * Deprecate this if there appears similar method into GWT someday. + * + * @param element + * @param deep + * clone child tree also + * @return + */ + public static native Element cloneNode(Element element, boolean deep) + /*-{ + return element.cloneNode(deep); + }-*/; + + public static int measureHorizontalPaddingAndBorder(Element element, + int paddingGuess) { + String originalWidth = DOM.getStyleAttribute(element, "width"); + + int originalOffsetWidth = element.getOffsetWidth(); + int widthGuess = (originalOffsetWidth - paddingGuess); + if (widthGuess < 1) { + widthGuess = 1; + } + element.getStyle().setWidth(widthGuess, Unit.PX); + int padding = element.getOffsetWidth() - widthGuess; + + element.getStyle().setProperty("width", originalWidth); + + return padding; + } + + public static int measureVerticalPaddingAndBorder(Element element, + int paddingGuess) { + String originalHeight = DOM.getStyleAttribute(element, "height"); + int originalOffsetHeight = element.getOffsetHeight(); + int widthGuess = (originalOffsetHeight - paddingGuess); + if (widthGuess < 1) { + widthGuess = 1; + } + element.getStyle().setHeight(widthGuess, Unit.PX); + int padding = element.getOffsetHeight() - widthGuess; + + element.getStyle().setProperty("height", originalHeight); + return padding; + } + + public static int measureHorizontalBorder(Element element) { + int borders; + + if (BrowserInfo.get().isIE()) { + String width = element.getStyle().getProperty("width"); + String height = element.getStyle().getProperty("height"); + + int offsetWidth = element.getOffsetWidth(); + int offsetHeight = element.getOffsetHeight(); + if (offsetHeight < 1) { + offsetHeight = 1; + } + if (offsetWidth < 1) { + offsetWidth = 10; + } + element.getStyle().setPropertyPx("height", offsetHeight); + element.getStyle().setPropertyPx("width", offsetWidth); + + borders = element.getOffsetWidth() - element.getClientWidth(); + + element.getStyle().setProperty("width", width); + element.getStyle().setProperty("height", height); + } else { + borders = element.getOffsetWidth() + - element.getPropertyInt("clientWidth"); + } + assert borders >= 0; + + return borders; + } + + public static int measureVerticalBorder(Element element) { + int borders; + if (BrowserInfo.get().isIE()) { + String width = element.getStyle().getProperty("width"); + String height = element.getStyle().getProperty("height"); + + int offsetWidth = element.getOffsetWidth(); + int offsetHeight = element.getOffsetHeight(); + if (offsetHeight < 1) { + offsetHeight = 1; + } + if (offsetWidth < 1) { + offsetWidth = 10; + } + element.getStyle().setPropertyPx("width", offsetWidth); + + element.getStyle().setPropertyPx("height", offsetHeight); + + borders = element.getOffsetHeight() + - element.getPropertyInt("clientHeight"); + + element.getStyle().setProperty("height", height); + element.getStyle().setProperty("width", width); + } else { + borders = element.getOffsetHeight() + - element.getPropertyInt("clientHeight"); + } + assert borders >= 0; + + return borders; + } + + public static int measureMarginLeft(Element element) { + return element.getAbsoluteLeft() + - element.getParentElement().getAbsoluteLeft(); + } + + public static int setHeightExcludingPaddingAndBorder(Widget widget, + String height, int paddingBorderGuess) { + if (height.equals("")) { + setHeight(widget, ""); + return paddingBorderGuess; + } else if (height.endsWith("px")) { + int pixelHeight = Integer.parseInt(height.substring(0, + height.length() - 2)); + return setHeightExcludingPaddingAndBorder(widget.getElement(), + pixelHeight, paddingBorderGuess, false); + } else { + // Set the height in unknown units + setHeight(widget, height); + // Use the offsetWidth + return setHeightExcludingPaddingAndBorder(widget.getElement(), + widget.getOffsetHeight(), paddingBorderGuess, true); + } + } + + private static void setWidth(Widget widget, String width) { + widget.getElement().getStyle().setProperty("width", width); + } + + private static void setHeight(Widget widget, String height) { + widget.getElement().getStyle().setProperty("height", height); + } + + public static int setWidthExcludingPaddingAndBorder(Widget widget, + String width, int paddingBorderGuess) { + if (width.equals("")) { + setWidth(widget, ""); + return paddingBorderGuess; + } else if (width.endsWith("px")) { + int pixelWidth = Integer.parseInt(width.substring(0, + width.length() - 2)); + return setWidthExcludingPaddingAndBorder(widget.getElement(), + pixelWidth, paddingBorderGuess, false); + } else { + setWidth(widget, width); + return setWidthExcludingPaddingAndBorder(widget.getElement(), + widget.getOffsetWidth(), paddingBorderGuess, true); + } + } + + public static int setWidthExcludingPaddingAndBorder(Element element, + int requestedWidth, int horizontalPaddingBorderGuess, + boolean requestedWidthIncludesPaddingBorder) { + + int widthGuess = requestedWidth - horizontalPaddingBorderGuess; + if (widthGuess < 0) { + widthGuess = 0; + } + + element.getStyle().setWidth(widthGuess, Unit.PX); + int captionOffsetWidth = DOM.getElementPropertyInt(element, + "offsetWidth"); + + int actualPadding = captionOffsetWidth - widthGuess; + + if (requestedWidthIncludesPaddingBorder) { + actualPadding += actualPadding; + } + + if (actualPadding != horizontalPaddingBorderGuess) { + int w = requestedWidth - actualPadding; + if (w < 0) { + // Cannot set negative width even if we would want to + w = 0; + } + element.getStyle().setWidth(w, Unit.PX); + + } + + return actualPadding; + + } + + public static int setHeightExcludingPaddingAndBorder(Element element, + int requestedHeight, int verticalPaddingBorderGuess, + boolean requestedHeightIncludesPaddingBorder) { + + int heightGuess = requestedHeight - verticalPaddingBorderGuess; + if (heightGuess < 0) { + heightGuess = 0; + } + + element.getStyle().setHeight(heightGuess, Unit.PX); + int captionOffsetHeight = DOM.getElementPropertyInt(element, + "offsetHeight"); + + int actualPadding = captionOffsetHeight - heightGuess; + + if (requestedHeightIncludesPaddingBorder) { + actualPadding += actualPadding; + } + + if (actualPadding != verticalPaddingBorderGuess) { + int h = requestedHeight - actualPadding; + if (h < 0) { + // Cannot set negative height even if we would want to + h = 0; + } + element.getStyle().setHeight(h, Unit.PX); + + } + + return actualPadding; + + } + + public static void setFloat(Element element, String value) { + if (BrowserInfo.get().isIE()) { + element.getStyle().setProperty("styleFloat", value); + } else { + element.getStyle().setProperty("cssFloat", value); + } + } + + private static int detectedScrollbarSize = -1; + + public static int getNativeScrollbarSize() { + if (detectedScrollbarSize < 0) { + Element scroller = DOM.createDiv(); + scroller.getStyle().setProperty("width", "50px"); + scroller.getStyle().setProperty("height", "50px"); + scroller.getStyle().setProperty("overflow", "scroll"); + scroller.getStyle().setProperty("position", "absolute"); + scroller.getStyle().setProperty("marginLeft", "-5000px"); + RootPanel.getBodyElement().appendChild(scroller); + detectedScrollbarSize = scroller.getOffsetWidth() + - scroller.getPropertyInt("clientWidth"); + + RootPanel.getBodyElement().removeChild(scroller); + } + return detectedScrollbarSize; + } + + /** + * Defers the execution of {@link #runWebkitOverflowAutoFix(Element)} + * + * @since 7.2.6 + * @param elem + * with overflow auto + */ + public static void runWebkitOverflowAutoFixDeferred(final Element elem) { + Scheduler.get().scheduleDeferred(new Command() { + + @Override + public void execute() { + WidgetUtil.runWebkitOverflowAutoFix(elem); + } + }); + + } + + /** + * Run workaround for webkits overflow auto issue. + * + * See: our bug #2138 and https://bugs.webkit.org/show_bug.cgi?id=21462 + * + * @param elem + * with overflow auto + */ + public static void runWebkitOverflowAutoFix(final Element elem) { + // Add max version if fix lands sometime to Webkit + // Starting from Opera 11.00, also a problem in Opera + if (BrowserInfo.get().requiresOverflowAutoFix()) { + final String originalOverflow = elem.getStyle().getProperty( + "overflow"); + if ("hidden".equals(originalOverflow)) { + return; + } + + // check the scrolltop value before hiding the element + final int scrolltop = elem.getScrollTop(); + final int scrollleft = elem.getScrollLeft(); + elem.getStyle().setProperty("overflow", "hidden"); + + Scheduler.get().scheduleDeferred(new Command() { + @Override + public void execute() { + // Dough, Safari scroll auto means actually just a moped + elem.getStyle().setProperty("overflow", originalOverflow); + + if (scrolltop > 0 || elem.getScrollTop() > 0) { + int scrollvalue = scrolltop; + if (scrollvalue == 0) { + // mysterious are the ways of webkits scrollbar + // handling. In some cases webkit reports bad (0) + // scrolltop before hiding the element temporary, + // sometimes after. + scrollvalue = elem.getScrollTop(); + } + // fix another bug where scrollbar remains in wrong + // position + elem.setScrollTop(scrollvalue - 1); + elem.setScrollTop(scrollvalue); + } + + // fix for #6940 : Table horizontal scroll sometimes not + // updated when collapsing/expanding columns + // Also appeared in Safari 5.1 with webkit 534 (#7667) + if ((BrowserInfo.get().isChrome() || (BrowserInfo.get() + .isSafari() && BrowserInfo.get().getWebkitVersion() >= 534)) + && (scrollleft > 0 || elem.getScrollLeft() > 0)) { + int scrollvalue = scrollleft; + + if (scrollvalue == 0) { + // mysterious are the ways of webkits scrollbar + // handling. In some cases webkit may report a bad + // (0) scrollleft before hiding the element + // temporary, sometimes after. + scrollvalue = elem.getScrollLeft(); + } + // fix another bug where scrollbar remains in wrong + // position + elem.setScrollLeft(scrollvalue - 1); + elem.setScrollLeft(scrollvalue); + } + } + }); + } + + } + + public static void alert(String string) { + if (true) { + Window.alert(string); + } + } + + /** + * Gets the border-box width for the given element, i.e. element width + + * border + padding. Always rounds up to nearest integer. + * + * @param element + * The element to check + * @return The border-box width for the element + */ + public static int getRequiredWidth(com.google.gwt.dom.client.Element element) { + int reqWidth = getRequiredWidthBoundingClientRect(element); + if (BrowserInfo.get().isIE() && !BrowserInfo.get().isIE8()) { + int csSize = getRequiredWidthComputedStyle(element); + if (csSize == reqWidth + 1) { + // If computed style reports one pixel larger than requiredWidth + // we would be rounding in the wrong direction in IE9. Round up + // instead. + // We do not always use csSize as it e.g. for 100% wide Labels + // in GridLayouts produces senseless values (see e.g. + // ThemeTestUI with Runo). + return csSize; + } + } + return reqWidth; + } + + /** + * Gets the border-box height for the given element, i.e. element height + + * border + padding. Always rounds up to nearest integer. + * + * @param element + * The element to check + * @return The border-box height for the element + */ + public static int getRequiredHeight( + com.google.gwt.dom.client.Element element) { + int reqHeight = getRequiredHeightBoundingClientRect(element); + if (BrowserInfo.get().isIE() && !BrowserInfo.get().isIE8()) { + int csSize = getRequiredHeightComputedStyle(element); + if (csSize == reqHeight + 1) { + // If computed style reports one pixel larger than + // requiredHeight we would be rounding in the wrong direction in + // IE9. Round up instead. + // We do not always use csSize as it e.g. for 100% wide Labels + // in GridLayouts produces senseless values (see e.g. + // ThemeTestUI with Runo). + return csSize; + } + } + return reqHeight; + } + + /** + * Calculates the width of the element's bounding rectangle. + * <p> + * In case the browser doesn't support bounding rectangles, the returned + * value is the offset width. + * + * @param element + * the element of which to calculate the width + * @return the width of the element + */ + public static int getRequiredWidthBoundingClientRect( + com.google.gwt.dom.client.Element element) { + return (int) getRequiredWidthBoundingClientRectDouble(element); + } + + /** + * Calculates the width of the element's bounding rectangle to subpixel + * precision. + * <p> + * In case the browser doesn't support bounding rectangles, the returned + * value is the offset width. + * + * @param element + * the element of which to calculate the width + * @return the subpixel-accurate width of the element + * @since 7.4 + */ + public static native double getRequiredWidthBoundingClientRectDouble( + com.google.gwt.dom.client.Element element) + /*-{ + if (element.getBoundingClientRect) { + var rect = element.getBoundingClientRect(); + return Math.ceil(rect.right - rect.left); + } else { + return element.offsetWidth; + } + }-*/; + + public static native int getRequiredHeightComputedStyle( + com.google.gwt.dom.client.Element element) + /*-{ + var cs = element.ownerDocument.defaultView.getComputedStyle(element); + var heightPx = cs.height; + if(heightPx == 'auto'){ + // Fallback for when IE reports auto + heightPx = @com.vaadin.client.WidgetUtil::getRequiredHeightBoundingClientRect(Lcom/google/gwt/dom/client/Element;)(element) + 'px'; + } + var borderTopPx = cs.borderTop; + var borderBottomPx = cs.borderBottom; + var paddingTopPx = cs.paddingTop; + var paddingBottomPx = cs.paddingBottom; + + var height = heightPx.substring(0,heightPx.length-2); + var border = borderTopPx.substring(0,borderTopPx.length-2)+borderBottomPx.substring(0,borderBottomPx.length-2); + var padding = paddingTopPx.substring(0,paddingTopPx.length-2)+paddingBottomPx.substring(0,paddingBottomPx.length-2); + return Math.ceil(height+border+padding); + }-*/; + + public static native int getRequiredWidthComputedStyle( + com.google.gwt.dom.client.Element element) + /*-{ + var cs = element.ownerDocument.defaultView.getComputedStyle(element); + var widthPx = cs.width; + if(widthPx == 'auto'){ + // Fallback for when IE reports auto + widthPx = @com.vaadin.client.WidgetUtil::getRequiredWidthBoundingClientRect(Lcom/google/gwt/dom/client/Element;)(element) + 'px'; + } + var borderLeftPx = cs.borderLeft; + var borderRightPx = cs.borderRight; + var paddingLeftPx = cs.paddingLeft; + var paddingRightPx = cs.paddingRight; + + var width = widthPx.substring(0,widthPx.length-2); + var border = borderLeftPx.substring(0,borderLeftPx.length-2)+borderRightPx.substring(0,borderRightPx.length-2); + var padding = paddingLeftPx.substring(0,paddingLeftPx.length-2)+paddingRightPx.substring(0,paddingRightPx.length-2); + return Math.ceil(width+border+padding); + }-*/; + + /** + * Calculates the height of the element's bounding rectangle. + * <p> + * In case the browser doesn't support bounding rectangles, the returned + * value is the offset height. + * + * @param element + * the element of which to calculate the height + * @return the height of the element + */ + public static int getRequiredHeightBoundingClientRect( + com.google.gwt.dom.client.Element element) { + return (int) getRequiredHeightBoundingClientRectDouble(element); + } + + /** + * Calculates the height of the element's bounding rectangle to subpixel + * precision. + * <p> + * In case the browser doesn't support bounding rectangles, the returned + * value is the offset height. + * + * @param element + * the element of which to calculate the height + * @return the subpixel-accurate height of the element + * @since 7.4 + */ + public static native double getRequiredHeightBoundingClientRectDouble( + com.google.gwt.dom.client.Element element) + /*-{ + var height; + if (element.getBoundingClientRect != null) { + var rect = element.getBoundingClientRect(); + height = Math.ceil(rect.bottom - rect.top); + } else { + height = element.offsetHeight; + } + return height; + }-*/; + + public static int getRequiredWidth(Widget widget) { + return getRequiredWidth(widget.getElement()); + } + + public static int getRequiredHeight(Widget widget) { + return getRequiredHeight(widget.getElement()); + } + + /** + * Detects what is currently the overflow style attribute in given element. + * + * @param pe + * the element to detect + * @return true if auto or scroll + */ + public static boolean mayHaveScrollBars(com.google.gwt.dom.client.Element pe) { + String overflow = getComputedStyle(pe, "overflow"); + if (overflow != null) { + if (overflow.equals("auto") || overflow.equals("scroll")) { + return true; + } else { + return false; + } + } else { + return false; + } + } + + /** + * A simple helper method to detect "computed style" (aka style sheets + + * element styles). Values returned differ a lot depending on browsers. + * Always be very careful when using this. + * + * @param el + * the element from which the style property is detected + * @param p + * the property to detect + * @return String value of style property + */ + private static native String getComputedStyle( + com.google.gwt.dom.client.Element el, String p) + /*-{ + try { + + if (el.currentStyle) { + // IE + return el.currentStyle[p]; + } else if (window.getComputedStyle) { + // Sa, FF, Opera + var view = el.ownerDocument.defaultView; + return view.getComputedStyle(el,null).getPropertyValue(p); + } else { + // fall back for non IE, Sa, FF, Opera + return ""; + } + } catch (e) { + return ""; + } + + }-*/; + + /** + * Will (attempt) to focus the given DOM Element. + * + * @param el + * the element to focus + */ + public static native void focus(Element el) + /*-{ + try { + el.focus(); + } catch (e) { + + } + }-*/; + + /** + * Helper method to find first instance of given Widget type found by + * traversing DOM upwards from given element. + * <p> + * <strong>Note:</strong> If {@code element} is inside some widget {@code W} + * , <em>and</em> {@code W} in turn is wrapped in a {@link Composite} + * {@code C}, this method will not find {@code W}. It returns either + * {@code C} or null, depending on whether the class parameter matches. This + * may also be the case with other Composite-like classes that hijack the + * event handling of their child widget(s). + * + * @param element + * the element where to start seeking of Widget + * @param class1 + * the Widget type to seek for + */ + @SuppressWarnings("unchecked") + public static <T> T findWidget(Element element, + Class<? extends Widget> class1) { + if (element != null) { + /* First seek for the first EventListener (~Widget) from dom */ + EventListener eventListener = null; + while (eventListener == null && element != null) { + eventListener = Event.getEventListener(element); + if (eventListener == null) { + element = element.getParentElement(); + } + } + if (eventListener instanceof Widget) { + /* + * Then find the first widget of type class1 from widget + * hierarchy + */ + Widget w = (Widget) eventListener; + while (w != null) { + if (class1 == null || w.getClass() == class1) { + return (T) w; + } + w = w.getParent(); + } + } + } + return null; + } + + /** + * Force webkit to redraw an element + * + * @param element + * The element that should be redrawn + */ + public static void forceWebkitRedraw(Element element) { + Style style = element.getStyle(); + String s = style.getProperty("webkitTransform"); + if (s == null || s.length() == 0) { + style.setProperty("webkitTransform", "scale(1)"); + } else { + style.setProperty("webkitTransform", ""); + } + } + + /** + * Performs a hack to trigger a re-layout in the IE8. This is usually + * necessary in cases where IE8 "forgets" to update child elements when they + * resize. + * + * @param e + * The element to perform the hack on + */ + public static final void forceIE8Redraw(Element e) { + if (BrowserInfo.get().isIE8()) { + forceIERedraw(e); + } + } + + /** + * Performs a hack to trigger a re-layout in the IE browser. This is usually + * necessary in cases where IE "forgets" to update child elements when they + * resize. + * + * @since 7.3 + * @param e + * The element to perform the hack on + */ + public static void forceIERedraw(Element e) { + if (BrowserInfo.get().isIE()) { + setStyleTemporarily(e, "zoom", "1"); + } + } + + /** + * Detaches and re-attaches the element from its parent. The element is + * reattached at the same position in the DOM as it was before. + * + * Does nothing if the element is not attached to the DOM. + * + * @param element + * The element to detach and re-attach + */ + public static void detachAttach(Element element) { + if (element == null) { + return; + } + + Node nextSibling = element.getNextSibling(); + Node parent = element.getParentNode(); + if (parent == null) { + return; + } + + parent.removeChild(element); + if (nextSibling == null) { + parent.appendChild(element); + } else { + parent.insertBefore(element, nextSibling); + } + + } + + public static void sinkOnloadForImages(Element element) { + NodeList<com.google.gwt.dom.client.Element> imgElements = element + .getElementsByTagName("img"); + for (int i = 0; i < imgElements.getLength(); i++) { + DOM.sinkEvents(imgElements.getItem(i), Event.ONLOAD); + } + + } + + /** + * Returns the index of the childElement within its parent. + * + * @param subElement + * @return + */ + public static int getChildElementIndex(Element childElement) { + int idx = 0; + Node n = childElement; + while ((n = n.getPreviousSibling()) != null) { + idx++; + } + + return idx; + } + + /** + * Temporarily sets the {@code styleProperty} to {@code tempValue} and then + * resets it to its current value. Used mainly to work around rendering + * issues in IE (and possibly in other browsers) + * + * @param element + * The target element + * @param styleProperty + * The name of the property to set + * @param tempValue + * The temporary value + */ + public static void setStyleTemporarily(Element element, + final String styleProperty, String tempValue) { + final Style style = element.getStyle(); + final String currentValue = style.getProperty(styleProperty); + + style.setProperty(styleProperty, tempValue); + element.getOffsetWidth(); + style.setProperty(styleProperty, currentValue); + + } + + /** + * A helper method to return the client position from an event. Returns + * position from either first changed touch (if touch event) or from the + * event itself. + * + * @param event + * @return + */ + public static int getTouchOrMouseClientX(Event event) { + if (isTouchEvent(event)) { + return event.getChangedTouches().get(0).getClientX(); + } else { + return event.getClientX(); + } + } + + /** + * Find the element corresponding to the coordinates in the passed mouse + * event. Please note that this is not always the same as the target of the + * event e.g. if event capture is used. + * + * @param event + * the mouse event to get coordinates from + * @return the element at the coordinates of the event + */ + public static Element getElementUnderMouse(NativeEvent event) { + int pageX = getTouchOrMouseClientX(event); + int pageY = getTouchOrMouseClientY(event); + + return getElementFromPoint(pageX, pageY); + } + + /** + * A helper method to return the client position from an event. Returns + * position from either first changed touch (if touch event) or from the + * event itself. + * + * @param event + * @return + */ + public static int getTouchOrMouseClientY(Event event) { + if (isTouchEvent(event)) { + return event.getChangedTouches().get(0).getClientY(); + } else { + return event.getClientY(); + } + } + + /** + * + * @see #getTouchOrMouseClientY(Event) + * @param currentGwtEvent + * @return + */ + public static int getTouchOrMouseClientY(NativeEvent currentGwtEvent) { + return getTouchOrMouseClientY(Event.as(currentGwtEvent)); + } + + /** + * @see #getTouchOrMouseClientX(Event) + * + * @param event + * @return + */ + public static int getTouchOrMouseClientX(NativeEvent event) { + return getTouchOrMouseClientX(Event.as(event)); + } + + public static boolean isTouchEvent(Event event) { + return event.getType().contains("touch"); + } + + public static boolean isTouchEvent(NativeEvent event) { + return isTouchEvent(Event.as(event)); + } + + public static void simulateClickFromTouchEvent(Event touchevent, + Widget widget) { + Touch touch = touchevent.getChangedTouches().get(0); + final NativeEvent createMouseUpEvent = Document.get() + .createMouseUpEvent(0, touch.getScreenX(), touch.getScreenY(), + touch.getClientX(), touch.getClientY(), false, false, + false, false, NativeEvent.BUTTON_LEFT); + final NativeEvent createMouseDownEvent = Document.get() + .createMouseDownEvent(0, touch.getScreenX(), + touch.getScreenY(), touch.getClientX(), + touch.getClientY(), false, false, false, false, + NativeEvent.BUTTON_LEFT); + final NativeEvent createMouseClickEvent = Document.get() + .createClickEvent(0, touch.getScreenX(), touch.getScreenY(), + touch.getClientX(), touch.getClientY(), false, false, + false, false); + + /* + * Get target with element from point as we want the actual element, not + * the one that sunk the event. + */ + final Element target = getElementFromPoint(touch.getClientX(), + touch.getClientY()); + + /* + * Fixes infocusable form fields in Safari of iOS 5.x and some Android + * browsers. + */ + Widget targetWidget = findWidget(target, null); + if (targetWidget instanceof com.google.gwt.user.client.ui.Focusable) { + final com.google.gwt.user.client.ui.Focusable toBeFocusedWidget = (com.google.gwt.user.client.ui.Focusable) targetWidget; + toBeFocusedWidget.setFocus(true); + } else if (targetWidget instanceof Focusable) { + ((Focusable) targetWidget).focus(); + } + + Scheduler.get().scheduleDeferred(new ScheduledCommand() { + @Override + public void execute() { + try { + target.dispatchEvent(createMouseDownEvent); + target.dispatchEvent(createMouseUpEvent); + target.dispatchEvent(createMouseClickEvent); + } catch (Exception e) { + } + + } + }); + + } + + /** + * Gets the currently focused element. + * + * @return The active element or null if no active element could be found. + */ + public native static Element getFocusedElement() + /*-{ + if ($wnd.document.activeElement) { + return $wnd.document.activeElement; + } + + return null; + }-*/; + + /** + * Gets currently focused element and checks if it's editable + * + * @since 7.4 + * + * @return true if focused element is editable + */ + public static boolean isFocusedElementEditable() { + Element focusedElement = WidgetUtil.getFocusedElement(); + if (focusedElement != null) { + String tagName = focusedElement.getTagName(); + String contenteditable = focusedElement + .getAttribute("contenteditable"); + + return "textarea".equalsIgnoreCase(tagName) + || "input".equalsIgnoreCase(tagName) + || "true".equalsIgnoreCase(contenteditable); + } + return false; + } + + /** + * Kind of stronger version of isAttached(). In addition to std isAttached, + * this method checks that this widget nor any of its parents is hidden. Can + * be e.g used to check whether component should react to some events or + * not. + * + * @param widget + * @return true if attached and displayed + */ + public static boolean isAttachedAndDisplayed(Widget widget) { + if (widget.isAttached()) { + /* + * Failfast using offset size, then by iterating the widget tree + */ + boolean notZeroSized = widget.getOffsetHeight() > 0 + || widget.getOffsetWidth() > 0; + return notZeroSized || checkVisibilityRecursively(widget); + } else { + return false; + } + } + + private static boolean checkVisibilityRecursively(Widget widget) { + if (widget.isVisible()) { + Widget parent = widget.getParent(); + if (parent == null) { + return true; // root panel + } else { + return checkVisibilityRecursively(parent); + } + } else { + return false; + } + } + + /** + * Scrolls an element into view vertically only. Modified version of + * Element.scrollIntoView. + * + * @param elem + * The element to scroll into view + */ + public static native void scrollIntoViewVertically(Element elem) + /*-{ + var top = elem.offsetTop; + var height = elem.offsetHeight; + + if (elem.parentNode != elem.offsetParent) { + top -= elem.parentNode.offsetTop; + } + + var cur = elem.parentNode; + while (cur && (cur.nodeType == 1)) { + if (top < cur.scrollTop) { + cur.scrollTop = top; + } + if (top + height > cur.scrollTop + cur.clientHeight) { + cur.scrollTop = (top + height) - cur.clientHeight; + } + + var offsetTop = cur.offsetTop; + if (cur.parentNode != cur.offsetParent) { + offsetTop -= cur.parentNode.offsetTop; + } + + top += offsetTop - cur.scrollTop; + cur = cur.parentNode; + } + }-*/; + + /** + * Checks if the given event is either a touch event or caused by the left + * mouse button + * + * @param event + * @return true if the event is a touch event or caused by the left mouse + * button, false otherwise + */ + public static boolean isTouchEventOrLeftMouseButton(Event event) { + boolean touchEvent = WidgetUtil.isTouchEvent(event); + return touchEvent || event.getButton() == Event.BUTTON_LEFT; + } + + /** + * Resolve a relative URL to an absolute URL based on the current document's + * location. + * + * @param url + * a string with the relative URL to resolve + * @return the corresponding absolute URL as a string + */ + public static String getAbsoluteUrl(String url) { + if (BrowserInfo.get().isIE8()) { + // The hard way - must use innerHTML and attach to DOM in IE8 + DivElement divElement = Document.get().createDivElement(); + divElement.getStyle().setDisplay(Display.NONE); + + RootPanel.getBodyElement().appendChild(divElement); + divElement.setInnerHTML("<a href='" + escapeAttribute(url) + + "' ></a>"); + + AnchorElement a = divElement.getChild(0).cast(); + String href = a.getHref(); + + RootPanel.getBodyElement().removeChild(divElement); + return href; + } else { + AnchorElement a = Document.get().createAnchorElement(); + a.setHref(url); + return a.getHref(); + } + } + + /** + * Sets the selection range of an input element. + * + * We need this JSNI function to set selection range so that we can use the + * optional direction attribute to set the anchor to the end and the focus + * to the start. This makes Firefox work the same way as other browsers + * (#13477) + * + * @param elem + * the html input element. + * @param pos + * the index of the first selected character. + * @param length + * the selection length. + * @param direction + * a string indicating the direction in which the selection was + * performed. This may be "forward" or "backward", or "none" if + * the direction is unknown or irrelevant. + * + * @since 7.3 + */ + public native static void setSelectionRange(Element elem, int pos, + int length, String direction) + /*-{ + try { + elem.setSelectionRange(pos, pos + length, direction); + } catch (e) { + // Firefox throws exception if TextBox is not visible, even if attached + } + }-*/; + + /** + * The allowed value inaccuracy when comparing two double-typed pixel + * values. + * <p> + * Since we're comparing pixels on a screen, epsilon must be less than 1. + * 0.49 was deemed a perfectly fine and beautifully round number. + */ + public static final double PIXEL_EPSILON = 0.49d; + + /** + * Compares two double values with the error margin of + * {@link #PIXEL_EPSILON} (i.e. {@value #PIXEL_EPSILON}) + * + * @param num1 + * the first value for which to compare equality + * @param num2 + * the second value for which to compare equality + * @since 7.4 + * + * @return true if the values are considered equals; false otherwise + */ + public static boolean pixelValuesEqual(final double num1, final double num2) { + return Math.abs(num1 - num2) <= PIXEL_EPSILON; + } + + /** + * Wrap a css size value and its unit and translate back and forth to the + * string representation.<br/> + * Eg. 50%, 123px, ... + * + * @since 7.2.6 + * @author Vaadin Ltd + */ + @SuppressWarnings("serial") + public static class CssSize implements Serializable { + + /* + * Map the size units with their type. + */ + private static Map<String, Unit> type2Unit = new HashMap<String, Style.Unit>(); + static { + for (Unit unit : Unit.values()) { + type2Unit.put(unit.getType(), unit); + } + } + + /** + * Gets the unit value by its type. + * + * @param type + * the type of the unit as found in the style. + * @return the unit value. + */ + public static Unit unitByType(String type) { + return type2Unit.get(type); + } + + /* + * Regex to parse the size. + */ + private static final RegExp sizePattern = RegExp + .compile(SharedUtil.SIZE_PATTERN); + + /** + * Parse the size from string format to {@link CssSize}. + * + * @param s + * the size as string. + * @return a {@link CssSize} object. + */ + public static CssSize fromString(String s) { + if (s == null) { + return null; + } + + s = s.trim(); + if ("".equals(s)) { + return null; + } + + float size = 0; + Unit unit = null; + + MatchResult matcher = sizePattern.exec(s); + if (matcher.getGroupCount() > 1) { + + size = Float.parseFloat(matcher.getGroup(1)); + if (size < 0) { + size = -1; + unit = Unit.PX; + + } else { + String symbol = matcher.getGroup(2); + unit = unitByType(symbol); + } + } else { + throw new IllegalArgumentException("Invalid size argument: \"" + + s + "\" (should match " + sizePattern.getSource() + + ")"); + } + return new CssSize(size, unit); + } + + /** + * Creates a {@link CssSize} using a value and its measurement unit. + * + * @param value + * the value. + * @param unit + * the unit. + * @return the {@link CssSize} object. + */ + public static CssSize fromValueUnit(float value, Unit unit) { + return new CssSize(value, unit); + } + + /* + * The value. + */ + private final float value; + + /* + * The measure unit. + */ + private final Unit unit; + + private CssSize(float value, Unit unit) { + this.value = value; + this.unit = unit; + } + + /** + * Gets the value for this css size. + * + * @return the value. + */ + public float getValue() { + return value; + } + + /** + * Gets the measurement unit for this css size. + * + * @return the unit. + */ + public Unit getUnit() { + return unit; + } + + @Override + public String toString() { + return value + unit.getType(); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof CssSize) { + CssSize size = (CssSize) obj; + return size.value == value && size.unit == unit; + } + + return false; + } + + /** + * Check whether the two sizes are equals. + * + * @param cssSize1 + * the first size to compare. + * @param cssSize2 + * the other size to compare with the first one. + * @return true if the two sizes are equals, otherwise false. + */ + public static boolean equals(String cssSize1, String cssSize2) { + return CssSize.fromString(cssSize1).equals( + CssSize.fromString(cssSize2)); + } + + } + + private static Logger getLogger() { + return Logger.getLogger(WidgetUtil.class.getName()); + } + +} diff --git a/client/src/com/vaadin/client/communication/AtmospherePushConnection.java b/client/src/com/vaadin/client/communication/AtmospherePushConnection.java index a2346db186..da08928f36 100644 --- a/client/src/com/vaadin/client/communication/AtmospherePushConnection.java +++ b/client/src/com/vaadin/client/communication/AtmospherePushConnection.java @@ -20,7 +20,6 @@ import java.util.ArrayList; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.Scheduler; -import com.google.gwt.json.client.JSONObject; import com.google.gwt.user.client.Command; import com.google.gwt.user.client.Window.Location; import com.vaadin.client.ApplicationConfiguration; @@ -37,6 +36,8 @@ import com.vaadin.shared.ui.ui.UIConstants; import com.vaadin.shared.ui.ui.UIState.PushConfigurationState; import com.vaadin.shared.util.SharedUtil; +import elemental.json.JsonObject; + /** * The default {@link PushConnection} implementation that uses Atmosphere for * handling the communication channel. @@ -114,7 +115,7 @@ public class AtmospherePushConnection implements PushConnection { private JavaScriptObject socket; - private ArrayList<JSONObject> messageQueue = new ArrayList<JSONObject>(); + private ArrayList<JsonObject> messageQueue = new ArrayList<JsonObject>(); private State state = State.CONNECT_PENDING; @@ -204,25 +205,25 @@ public class AtmospherePushConnection implements PushConnection { } @Override - public void push(JSONObject message) { + public void push(JsonObject message) { switch (state) { case CONNECT_PENDING: assert isActive(); - VConsole.log("Queuing push message: " + message); + VConsole.log("Queuing push message: " + message.toJson()); messageQueue.add(message); break; case CONNECTED: assert isActive(); - VConsole.log("Sending push message: " + message); + VConsole.log("Sending push message: " + message.toJson()); if (transport.equals("websocket")) { FragmentedMessage fragmented = new FragmentedMessage( - message.toString()); + message.toJson()); while (fragmented.hasNextFragment()) { doPush(socket, fragmented.getNextFragment()); } } else { - doPush(socket, message.toString()); + doPush(socket, message.toJson()); } break; case DISCONNECT_PENDING: @@ -261,7 +262,7 @@ public class AtmospherePushConnection implements PushConnection { switch (state) { case CONNECT_PENDING: state = State.CONNECTED; - for (JSONObject message : messageQueue) { + for (JsonObject message : messageQueue) { push(message); } messageQueue.clear(); diff --git a/client/src/com/vaadin/client/communication/Date_Serializer.java b/client/src/com/vaadin/client/communication/Date_Serializer.java index 15ef3869aa..14eb6e4e3d 100644 --- a/client/src/com/vaadin/client/communication/Date_Serializer.java +++ b/client/src/com/vaadin/client/communication/Date_Serializer.java @@ -17,11 +17,12 @@ package com.vaadin.client.communication; import java.util.Date; -import com.google.gwt.json.client.JSONNumber; -import com.google.gwt.json.client.JSONValue; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.metadata.Type; +import elemental.json.Json; +import elemental.json.JsonValue; + /** * Client side serializer/deserializer for java.util.Date * @@ -31,14 +32,14 @@ import com.vaadin.client.metadata.Type; public class Date_Serializer implements JSONSerializer<Date> { @Override - public Date deserialize(Type type, JSONValue jsonValue, + public Date deserialize(Type type, JsonValue jsonValue, ApplicationConnection connection) { - return new Date((long) ((JSONNumber) jsonValue).doubleValue()); + return new Date((long) jsonValue.asNumber()); } @Override - public JSONValue serialize(Date value, ApplicationConnection connection) { - return new JSONNumber(value.getTime()); + public JsonValue serialize(Date value, ApplicationConnection connection) { + return Json.create(value.getTime()); } } diff --git a/client/src/com/vaadin/client/communication/DiffJSONSerializer.java b/client/src/com/vaadin/client/communication/DiffJSONSerializer.java index 59575604a1..d433a8964c 100644 --- a/client/src/com/vaadin/client/communication/DiffJSONSerializer.java +++ b/client/src/com/vaadin/client/communication/DiffJSONSerializer.java @@ -15,9 +15,9 @@ */ package com.vaadin.client.communication; -import com.google.gwt.json.client.JSONValue; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.metadata.Type; +import elemental.json.JsonValue; public interface DiffJSONSerializer<T> extends JSONSerializer<T> { /** @@ -27,6 +27,6 @@ public interface DiffJSONSerializer<T> extends JSONSerializer<T> { * @param jsonValue * @param connection */ - public void update(T target, Type type, JSONValue jsonValue, + public void update(T target, Type type, JsonValue jsonValue, ApplicationConnection connection); } diff --git a/client/src/com/vaadin/client/communication/JSONSerializer.java b/client/src/com/vaadin/client/communication/JSONSerializer.java index 3327baf842..59e0329ae1 100644 --- a/client/src/com/vaadin/client/communication/JSONSerializer.java +++ b/client/src/com/vaadin/client/communication/JSONSerializer.java @@ -16,16 +16,16 @@ package com.vaadin.client.communication; -import com.google.gwt.json.client.JSONValue; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.metadata.Type; +import elemental.json.JsonValue; /** * Implementors of this interface knows how to serialize an Object of a given * type to JSON and how to deserialize the JSON back into an object. * <p> * The {@link #serialize(Object, ApplicationConnection)} and - * {@link #deserialize(Type, JSONValue, ApplicationConnection)} methods must be + * {@link #deserialize(Type, JsonValue, ApplicationConnection)} methods must be * symmetric so they can be chained and produce the original result (or an equal * result). * <p> @@ -53,12 +53,12 @@ public interface JSONSerializer<T> { * * @return A deserialized object */ - T deserialize(Type type, JSONValue jsonValue, + T deserialize(Type type, JsonValue jsonValue, ApplicationConnection connection); /** * Serialize the given object into JSON. Must be compatible with - * {@link #deserialize(Type, JSONValue, ApplicationConnection)} and also + * {@link #deserialize(Type, JsonValue, ApplicationConnection)} and also * with the server side JsonCodec.decodeCustomType method. * * @param value @@ -67,6 +67,6 @@ public interface JSONSerializer<T> { * the application connection providing the context * @return A JSON serialized version of the object */ - JSONValue serialize(T value, ApplicationConnection connection); + JsonValue serialize(T value, ApplicationConnection connection); } diff --git a/client/src/com/vaadin/client/communication/JsonDecoder.java b/client/src/com/vaadin/client/communication/JsonDecoder.java index 37c113bb2f..0ce89c873e 100644 --- a/client/src/com/vaadin/client/communication/JsonDecoder.java +++ b/client/src/com/vaadin/client/communication/JsonDecoder.java @@ -24,10 +24,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -import com.google.gwt.json.client.JSONArray; -import com.google.gwt.json.client.JSONObject; -import com.google.gwt.json.client.JSONString; -import com.google.gwt.json.client.JSONValue; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.ConnectorMap; import com.vaadin.client.FastStringSet; @@ -38,15 +34,20 @@ import com.vaadin.client.metadata.Property; import com.vaadin.client.metadata.Type; import com.vaadin.shared.Connector; +import elemental.json.JsonArray; +import elemental.json.JsonObject; +import elemental.json.JsonType; +import elemental.json.JsonValue; + /** * Client side decoder for decodeing shared state and other values from JSON * received from the server. - * + * * Currently, basic data types as well as Map, String[] and Object[] are * supported, where maps and Object[] can contain other supported data types. - * + * * TODO extensible type support - * + * * @since 7.0 */ public class JsonDecoder { @@ -72,62 +73,60 @@ public class JsonDecoder { /** * Decode a JSON array with two elements (type and value) into a client-side * type, recursively if necessary. - * + * * @param jsonValue * JSON value with encoded data * @param connection * reference to the current ApplicationConnection * @return decoded value (does not contain JSON types) */ - public static Object decodeValue(Type type, JSONValue jsonValue, + public static Object decodeValue(Type type, JsonValue jsonValue, Object target, ApplicationConnection connection) { + String baseTypeName = type.getBaseTypeName(); + if (baseTypeName.startsWith("elemental.json.Json")) { + return jsonValue; + } - // Null is null, regardless of type - if (jsonValue.isNull() != null) { + // Null is null, regardless of type (except JSON) + if (jsonValue.getType() == JsonType.NULL) { return null; } - String baseTypeName = type.getBaseTypeName(); if (Map.class.getName().equals(baseTypeName) || HashMap.class.getName().equals(baseTypeName)) { return decodeMap(type, jsonValue, connection); } else if (List.class.getName().equals(baseTypeName) || ArrayList.class.getName().equals(baseTypeName)) { - return decodeList(type, (JSONArray) jsonValue, connection); + assert jsonValue.getType() == JsonType.ARRAY; + return decodeList(type, (JsonArray) jsonValue, connection); } else if (Set.class.getName().equals(baseTypeName)) { - return decodeSet(type, (JSONArray) jsonValue, connection); + assert jsonValue.getType() == JsonType.ARRAY; + return decodeSet(type, (JsonArray) jsonValue, connection); } else if (String.class.getName().equals(baseTypeName)) { - return ((JSONString) jsonValue).stringValue(); + return jsonValue.asString(); } else if (Integer.class.getName().equals(baseTypeName)) { - return Integer.valueOf(String.valueOf(jsonValue)); + return Integer.valueOf((int) jsonValue.asNumber()); } else if (Long.class.getName().equals(baseTypeName)) { - // TODO handle properly - return Long.valueOf(String.valueOf(jsonValue)); + return Long.valueOf((long) jsonValue.asNumber()); } else if (Float.class.getName().equals(baseTypeName)) { - // TODO handle properly - return Float.valueOf(String.valueOf(jsonValue)); + return Float.valueOf((float) jsonValue.asNumber()); } else if (Double.class.getName().equals(baseTypeName)) { - // TODO handle properly - return Double.valueOf(String.valueOf(jsonValue)); + return Double.valueOf(jsonValue.asNumber()); } else if (Boolean.class.getName().equals(baseTypeName)) { - // TODO handle properly - return Boolean.valueOf(String.valueOf(jsonValue)); + return Boolean.valueOf(jsonValue.asString()); } else if (Byte.class.getName().equals(baseTypeName)) { - // TODO handle properly - return Byte.valueOf(String.valueOf(jsonValue)); + return Byte.valueOf((byte) jsonValue.asNumber()); } else if (Character.class.getName().equals(baseTypeName)) { - // TODO handle properly - return Character.valueOf(((JSONString) jsonValue).stringValue() - .charAt(0)); + return Character.valueOf(jsonValue.asString().charAt(0)); } else if (Connector.class.getName().equals(baseTypeName)) { return ConnectorMap.get(connection).getConnector( - ((JSONString) jsonValue).stringValue()); + jsonValue.asString()); } else { return decodeObject(type, jsonValue, target, connection); } } - private static Object decodeObject(Type type, JSONValue jsonValue, + private static Object decodeObject(Type type, JsonValue jsonValue, Object target, ApplicationConnection connection) { Profiler.enter("JsonDecoder.decodeObject"); JSONSerializer<Object> serializer = (JSONSerializer<Object>) type @@ -152,14 +151,12 @@ public class JsonDecoder { if (target == null) { target = type.createInstance(); } - JSONObject jsonObject = jsonValue.isObject(); + JsonObject jsonObject = (JsonObject) jsonValue; int size = properties.size(); for (int i = 0; i < size; i++) { Property property = properties.get(i); - JSONValue encodedPropertyValue = jsonObject.get(property - .getName()); - if (encodedPropertyValue == null) { + if (!jsonObject.hasKey(property.getName())) { continue; } @@ -173,6 +170,8 @@ public class JsonDecoder { } Profiler.leave("JsonDecoder.decodeObject meta data processing"); + JsonValue encodedPropertyValue = jsonObject.get(property + .getName()); Object decodedValue = decodeValue(propertyType, encodedPropertyValue, propertyReference, connection); Profiler.enter("JsonDecoder.decodeObject meta data processing"); @@ -194,13 +193,13 @@ public class JsonDecoder { return !decodedWithoutReference.contains(type.getBaseTypeName()); } - private static Map<Object, Object> decodeMap(Type type, JSONValue jsonMap, + private static Map<Object, Object> decodeMap(Type type, JsonValue jsonMap, ApplicationConnection connection) { // Client -> server encodes empty map as an empty array because of // #8906. Do the same for server -> client to maintain symmetry. - if (jsonMap instanceof JSONArray) { - JSONArray array = (JSONArray) jsonMap; - if (array.size() == 0) { + if (jsonMap.getType() == JsonType.ARRAY) { + JsonArray array = (JsonArray) jsonMap; + if (array.length() == 0) { return new HashMap<Object, Object>(); } } @@ -209,26 +208,30 @@ public class JsonDecoder { Type valueType = type.getParameterTypes()[1]; if (keyType.getBaseTypeName().equals(String.class.getName())) { - return decodeStringMap(valueType, jsonMap, connection); + assert jsonMap.getType() == JsonType.OBJECT; + return decodeStringMap(valueType, (JsonObject) jsonMap, connection); } else if (keyType.getBaseTypeName().equals(Connector.class.getName())) { - return decodeConnectorMap(valueType, jsonMap, connection); + assert jsonMap.getType() == JsonType.OBJECT; + return decodeConnectorMap(valueType, (JsonObject) jsonMap, + connection); } else { - return decodeObjectMap(keyType, valueType, jsonMap, connection); + assert jsonMap.getType() == JsonType.ARRAY; + return decodeObjectMap(keyType, valueType, (JsonArray) jsonMap, + connection); } } private static Map<Object, Object> decodeObjectMap(Type keyType, - Type valueType, JSONValue jsonValue, + Type valueType, JsonArray jsonValue, ApplicationConnection connection) { Map<Object, Object> map = new HashMap<Object, Object>(); - JSONArray mapArray = (JSONArray) jsonValue; - JSONArray keys = (JSONArray) mapArray.get(0); - JSONArray values = (JSONArray) mapArray.get(1); + JsonArray keys = jsonValue.get(0); + JsonArray values = jsonValue.get(1); - assert (keys.size() == values.size()); + assert (keys.length() == values.length()); - for (int i = 0; i < keys.size(); i++) { + for (int i = 0; i < keys.length(); i++) { Object decodedKey = decodeValue(keyType, keys.get(i), null, connection); Object decodedValue = decodeValue(valueType, values.get(i), null, @@ -241,13 +244,12 @@ public class JsonDecoder { } private static Map<Object, Object> decodeConnectorMap(Type valueType, - JSONValue jsonValue, ApplicationConnection connection) { + JsonObject jsonMap, ApplicationConnection connection) { Map<Object, Object> map = new HashMap<Object, Object>(); - JSONObject jsonMap = (JSONObject) jsonValue; ConnectorMap connectorMap = ConnectorMap.get(connection); - for (String connectorId : jsonMap.keySet()) { + for (String connectorId : jsonMap.keys()) { Object value = decodeValue(valueType, jsonMap.get(connectorId), null, connection); map.put(connectorMap.getConnector(connectorId), value); @@ -257,12 +259,10 @@ public class JsonDecoder { } private static Map<Object, Object> decodeStringMap(Type valueType, - JSONValue jsonValue, ApplicationConnection connection) { + JsonObject jsonMap, ApplicationConnection connection) { Map<Object, Object> map = new HashMap<Object, Object>(); - JSONObject jsonMap = (JSONObject) jsonValue; - - for (String key : jsonMap.keySet()) { + for (String key : jsonMap.keys()) { Object value = decodeValue(valueType, jsonMap.get(key), null, connection); map.put(key, value); @@ -271,7 +271,7 @@ public class JsonDecoder { return map; } - private static List<Object> decodeList(Type type, JSONArray jsonArray, + private static List<Object> decodeList(Type type, JsonArray jsonArray, ApplicationConnection connection) { List<Object> tokens = new ArrayList<Object>(); decodeIntoCollection(type.getParameterTypes()[0], jsonArray, @@ -279,7 +279,7 @@ public class JsonDecoder { return tokens; } - private static Set<Object> decodeSet(Type type, JSONArray jsonArray, + private static Set<Object> decodeSet(Type type, JsonArray jsonArray, ApplicationConnection connection) { Set<Object> tokens = new HashSet<Object>(); decodeIntoCollection(type.getParameterTypes()[0], jsonArray, @@ -288,12 +288,22 @@ public class JsonDecoder { } private static void decodeIntoCollection(Type childType, - JSONArray jsonArray, ApplicationConnection connection, + JsonArray jsonArray, ApplicationConnection connection, Collection<Object> tokens) { - for (int i = 0; i < jsonArray.size(); ++i) { + for (int i = 0; i < jsonArray.length(); ++i) { // each entry always has two elements: type and value - JSONValue entryValue = jsonArray.get(i); + JsonValue entryValue = jsonArray.get(i); tokens.add(decodeValue(childType, entryValue, null, connection)); } } + + /** + * Called by generated deserialization code to treat a generic object as a + * JsonValue. This is needed because GWT refuses to directly cast String + * typed as Object into a JSO. + */ + public static native <T extends JsonValue> T obj2jso(Object object) + /*-{ + return object; + }-*/; } diff --git a/client/src/com/vaadin/client/communication/JsonEncoder.java b/client/src/com/vaadin/client/communication/JsonEncoder.java index 6783e802ec..fad4ad602a 100644 --- a/client/src/com/vaadin/client/communication/JsonEncoder.java +++ b/client/src/com/vaadin/client/communication/JsonEncoder.java @@ -22,13 +22,6 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import com.google.gwt.json.client.JSONArray; -import com.google.gwt.json.client.JSONBoolean; -import com.google.gwt.json.client.JSONNull; -import com.google.gwt.json.client.JSONNumber; -import com.google.gwt.json.client.JSONObject; -import com.google.gwt.json.client.JSONString; -import com.google.gwt.json.client.JSONValue; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.JsArrayObject; import com.vaadin.client.metadata.NoDataException; @@ -38,6 +31,11 @@ import com.vaadin.shared.Connector; import com.vaadin.shared.JsonConstants; import com.vaadin.shared.communication.UidlValue; +import elemental.json.Json; +import elemental.json.JsonArray; +import elemental.json.JsonObject; +import elemental.json.JsonValue; + /** * Encoder for converting RPC parameters and other values to JSON for transfer * between the client and the server. @@ -60,27 +58,27 @@ public class JsonEncoder { * @param connection * @return JSON representation of the value */ - public static JSONValue encode(Object value, Type type, + public static JsonValue encode(Object value, Type type, ApplicationConnection connection) { if (null == value) { - return JSONNull.getInstance(); - } else if (value instanceof JSONValue) { - return (JSONValue) value; + return Json.createNull(); + } else if (value instanceof JsonValue) { + return (JsonValue) value; } else if (value instanceof String[]) { String[] array = (String[]) value; - JSONArray jsonArray = new JSONArray(); + JsonArray jsonArray = Json.createArray(); for (int i = 0; i < array.length; ++i) { - jsonArray.set(i, new JSONString(array[i])); + jsonArray.set(i, array[i]); } return jsonArray; } else if (value instanceof String) { - return new JSONString((String) value); + return Json.create((String) value); } else if (value instanceof Boolean) { - return JSONBoolean.getInstance((Boolean) value); + return Json.create((Boolean) value); } else if (value instanceof Byte) { - return new JSONNumber((Byte) value); + return Json.create((Byte) value); } else if (value instanceof Character) { - return new JSONString(String.valueOf(value)); + return Json.create(String.valueOf(value)); } else if (value instanceof Object[] && type == null) { // Non-legacy arrays handed by generated serializer return encodeLegacyObjectArray((Object[]) value, connection); @@ -90,7 +88,7 @@ public class JsonEncoder { return encodeMap((Map) value, type, connection); } else if (value instanceof Connector) { Connector connector = (Connector) value; - return new JSONString(connector.getConnectorId()); + return Json.create(connector.getConnectorId()); } else if (value instanceof Collection) { return encodeCollection((Collection) value, type, connection); } else if (value instanceof UidlValue) { @@ -108,21 +106,21 @@ public class JsonEncoder { String transportType = getTransportType(value); if (transportType != null) { // Send the string value for remaining legacy types - return new JSONString(String.valueOf(value)); + return Json.create(String.valueOf(value)); } else if (type != null) { // And finally try using bean serialization logic try { JsArrayObject<Property> properties = type .getPropertiesAsArray(); - JSONObject jsonObject = new JSONObject(); + JsonObject jsonObject = Json.createObject(); int size = properties.size(); for (int i = 0; i < size; i++) { Property property = properties.get(i); Object propertyValue = property.getValue(value); Type propertyType = property.getType(); - JSONValue encodedPropertyValue = encode(propertyValue, + JsonValue encodedPropertyValue = encode(propertyValue, propertyType, connection); jsonObject .put(property.getName(), encodedPropertyValue); @@ -141,11 +139,11 @@ public class JsonEncoder { } } - private static JSONValue encodeVariableChange(UidlValue uidlValue, + private static JsonValue encodeVariableChange(UidlValue uidlValue, ApplicationConnection connection) { Object value = uidlValue.getValue(); - JSONArray jsonArray = new JSONArray(); + JsonArray jsonArray = Json.createArray(); String transportType = getTransportType(value); if (transportType == null) { /* @@ -159,13 +157,13 @@ public class JsonEncoder { throw new IllegalArgumentException("Cannot encode object of type " + valueType); } - jsonArray.set(0, new JSONString(transportType)); + jsonArray.set(0, Json.create(transportType)); jsonArray.set(1, encode(value, null, connection)); return jsonArray; } - private static JSONValue encodeMap(Map<Object, Object> map, Type type, + private static JsonValue encodeMap(Map<Object, Object> map, Type type, ApplicationConnection connection) { /* * As we have no info about declared types, we instead select encoding @@ -174,7 +172,7 @@ public class JsonEncoder { * server-side decoding must check for. (see #8906) */ if (map.isEmpty()) { - return new JSONArray(); + return Json.createArray(); } Object firstKey = map.keySet().iterator().next(); @@ -190,7 +188,7 @@ public class JsonEncoder { } } - private static JSONValue encodeChildValue(Object value, + private static JsonValue encodeChildValue(Object value, Type collectionType, int typeIndex, ApplicationConnection connection) { if (collectionType == null) { return encode(new UidlValue(value), null, connection); @@ -204,35 +202,35 @@ public class JsonEncoder { } } - private static JSONValue encodeObjectMap(Map<Object, Object> map, + private static JsonArray encodeObjectMap(Map<Object, Object> map, Type type, ApplicationConnection connection) { - JSONArray keys = new JSONArray(); - JSONArray values = new JSONArray(); + JsonArray keys = Json.createArray(); + JsonArray values = Json.createArray(); assert type != null : "Should only be used for non-legacy types"; for (Entry<?, ?> entry : map.entrySet()) { - keys.set(keys.size(), + keys.set(keys.length(), encodeChildValue(entry.getKey(), type, 0, connection)); - values.set(values.size(), + values.set(values.length(), encodeChildValue(entry.getValue(), type, 1, connection)); } - JSONArray keysAndValues = new JSONArray(); + JsonArray keysAndValues = Json.createArray(); keysAndValues.set(0, keys); keysAndValues.set(1, values); return keysAndValues; } - private static JSONValue encodeConnectorMap(Map<Object, Object> map, + private static JsonValue encodeConnectorMap(Map<Object, Object> map, Type type, ApplicationConnection connection) { - JSONObject jsonMap = new JSONObject(); + JsonObject jsonMap = Json.createObject(); for (Entry<?, ?> entry : map.entrySet()) { Connector connector = (Connector) entry.getKey(); - JSONValue encodedValue = encodeChildValue(entry.getValue(), type, + JsonValue encodedValue = encodeChildValue(entry.getValue(), type, 1, connection); jsonMap.put(connector.getConnectorId(), encodedValue); @@ -241,9 +239,9 @@ public class JsonEncoder { return jsonMap; } - private static JSONValue encodeStringMap(Map<Object, Object> map, + private static JsonValue encodeStringMap(Map<Object, Object> map, Type type, ApplicationConnection connection) { - JSONObject jsonMap = new JSONObject(); + JsonObject jsonMap = Json.createObject(); for (Entry<?, ?> entry : map.entrySet()) { String key = (String) entry.getKey(); @@ -255,14 +253,14 @@ public class JsonEncoder { return jsonMap; } - private static JSONValue encodeEnum(Enum<?> e, + private static JsonValue encodeEnum(Enum<?> e, ApplicationConnection connection) { - return new JSONString(e.toString()); + return Json.create(e.toString()); } - private static JSONValue encodeLegacyObjectArray(Object[] array, + private static JsonValue encodeLegacyObjectArray(Object[] array, ApplicationConnection connection) { - JSONArray jsonArray = new JSONArray(); + JsonArray jsonArray = Json.createArray(); for (int i = 0; i < array.length; ++i) { // TODO handle object graph loops? Object value = array[i]; @@ -271,12 +269,12 @@ public class JsonEncoder { return jsonArray; } - private static JSONValue encodeCollection(Collection collection, Type type, + private static JsonArray encodeCollection(Collection collection, Type type, ApplicationConnection connection) { - JSONArray jsonArray = new JSONArray(); + JsonArray jsonArray = Json.createArray(); int idx = 0; for (Object o : collection) { - JSONValue encodedObject = encodeChildValue(o, type, 0, connection); + JsonValue encodedObject = encodeChildValue(o, type, 0, connection); jsonArray.set(idx++, encodedObject); } if (collection instanceof Set) { diff --git a/client/src/com/vaadin/client/communication/PushConnection.java b/client/src/com/vaadin/client/communication/PushConnection.java index 3bdb18ff1b..8066746dc6 100644 --- a/client/src/com/vaadin/client/communication/PushConnection.java +++ b/client/src/com/vaadin/client/communication/PushConnection.java @@ -16,11 +16,11 @@ package com.vaadin.client.communication; -import com.google.gwt.json.client.JSONObject; import com.google.gwt.user.client.Command; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.ApplicationConnection.CommunicationErrorHandler; import com.vaadin.shared.ui.ui.UIState.PushConfigurationState; +import elemental.json.JsonObject; /** * Represents the client-side endpoint of a bidirectional ("push") communication @@ -61,7 +61,7 @@ public interface PushConnection { * * @see #isActive() */ - public void push(JSONObject payload); + public void push(JsonObject payload); /** * Checks whether this push connection is in a state where it can push diff --git a/client/src/com/vaadin/client/communication/RpcManager.java b/client/src/com/vaadin/client/communication/RpcManager.java index 7b706fca2d..f5c3ca9ffb 100644 --- a/client/src/com/vaadin/client/communication/RpcManager.java +++ b/client/src/com/vaadin/client/communication/RpcManager.java @@ -18,8 +18,6 @@ package com.vaadin.client.communication; import java.util.Collection; -import com.google.gwt.json.client.JSONArray; -import com.google.gwt.json.client.JSONString; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.ConnectorMap; import com.vaadin.client.ServerConnector; @@ -30,6 +28,8 @@ import com.vaadin.client.metadata.Type; import com.vaadin.shared.communication.ClientRpc; import com.vaadin.shared.communication.MethodInvocation; +import elemental.json.JsonArray; + /** * Client side RPC manager that can invoke methods based on RPC calls received * from the server. @@ -64,7 +64,18 @@ public class RpcManager { } } - private Method getMethod(MethodInvocation invocation) { + /** + * Gets the method that an invocation targets. + * + * @param invocation + * the method invocation to get the method for + * + * @since 7.4 + * @return the method targeted by this invocation + */ + public static Method getMethod(MethodInvocation invocation) { + // Implemented here instead of in MethodInovcation since it's in shared + // and can't use our Method class. Type type = new Type(invocation.getInterfaceName(), null); Method method = type.getMethod(invocation.getMethodName()); return method; @@ -86,14 +97,14 @@ public class RpcManager { } } - public void parseAndApplyInvocation(JSONArray rpcCall, + public MethodInvocation parseAndApplyInvocation(JsonArray rpcCall, ApplicationConnection connection) { ConnectorMap connectorMap = ConnectorMap.get(connection); - String connectorId = ((JSONString) rpcCall.get(0)).stringValue(); - String interfaceName = ((JSONString) rpcCall.get(1)).stringValue(); - String methodName = ((JSONString) rpcCall.get(2)).stringValue(); - JSONArray parametersJson = (JSONArray) rpcCall.get(3); + String connectorId = rpcCall.getString(0); + String interfaceName = rpcCall.getString(1); + String methodName = rpcCall.getString(2); + JsonArray parametersJson = rpcCall.getArray(3); ServerConnector connector = connectorMap.getConnector(connectorId); @@ -114,14 +125,16 @@ public class RpcManager { VConsole.log("Server to client RPC call: " + invocation); applyInvocation(invocation, connector); } + + return invocation; } private void parseMethodParameters(MethodInvocation methodInvocation, - JSONArray parametersJson, ApplicationConnection connection) { + JsonArray parametersJson, ApplicationConnection connection) { Type[] parameterTypes = getParameterTypes(methodInvocation); - Object[] parameters = new Object[parametersJson.size()]; - for (int j = 0; j < parametersJson.size(); ++j) { + Object[] parameters = new Object[parametersJson.length()]; + for (int j = 0; j < parametersJson.length(); ++j) { parameters[j] = JsonDecoder.decodeValue(parameterTypes[j], parametersJson.get(j), null, connection); } diff --git a/client/src/com/vaadin/client/communication/StateChangeEvent.java b/client/src/com/vaadin/client/communication/StateChangeEvent.java index 6bda41cef2..c2c1ceaea9 100644 --- a/client/src/com/vaadin/client/communication/StateChangeEvent.java +++ b/client/src/com/vaadin/client/communication/StateChangeEvent.java @@ -21,16 +21,18 @@ import java.util.Set; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.event.shared.EventHandler; -import com.google.gwt.json.client.JSONObject; import com.vaadin.client.FastStringSet; import com.vaadin.client.JsArrayObject; import com.vaadin.client.Profiler; import com.vaadin.client.ServerConnector; +import com.vaadin.client.Util; import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler; import com.vaadin.client.metadata.NoDataException; import com.vaadin.client.metadata.Property; import com.vaadin.client.ui.AbstractConnector; +import elemental.json.JsonObject; + public class StateChangeEvent extends AbstractServerConnectorEvent<StateChangeHandler> { /** @@ -54,7 +56,7 @@ public class StateChangeEvent extends private boolean initialStateChange = false; - private JSONObject stateJson; + private JsonObject stateJson; @Override public Type<StateChangeHandler> getAssociatedType() { @@ -69,7 +71,7 @@ public class StateChangeEvent extends * @param changedPropertiesSet * a set of names of the changed properties * @deprecated As of 7.0.1, use - * {@link #StateChangeEvent(ServerConnector, JSONObject, boolean)} + * {@link #StateChangeEvent(ServerConnector, JsonObject, boolean)} * instead for improved performance. */ @Deprecated @@ -93,7 +95,7 @@ public class StateChangeEvent extends * @param changedProperties * a set of names of the changed properties * @deprecated As of 7.0.2, use - * {@link #StateChangeEvent(ServerConnector, JSONObject, boolean)} + * {@link #StateChangeEvent(ServerConnector, JsonObject, boolean)} * instead for improved performance. */ @Deprecated @@ -114,7 +116,7 @@ public class StateChangeEvent extends * <code>true</code> if the state change is for a new connector, * otherwise <code>false</code> */ - public StateChangeEvent(ServerConnector connector, JSONObject stateJson, + public StateChangeEvent(ServerConnector connector, JsonObject stateJson, boolean initialStateChange) { setConnector(connector); this.stateJson = stateJson; @@ -203,7 +205,7 @@ public class StateChangeEvent extends return true; } else if (stateJson != null) { // Check whether it's in the json object - return isInJson(property, stateJson.getJavaScriptObject()); + return isInJson(property, Util.json2jso(stateJson)); } else { // Legacy cases if (changedProperties != null) { @@ -297,13 +299,13 @@ public class StateChangeEvent extends * the base name of the current object */ @Deprecated - private static void addJsonFields(JSONObject json, + private static void addJsonFields(JsonObject json, FastStringSet changedProperties, String context) { - for (String key : json.keySet()) { + for (String key : json.keys()) { String fieldName = context + key; changedProperties.add(fieldName); - JSONObject object = json.get(key).isObject(); + JsonObject object = json.get(key); if (object != null) { addJsonFields(object, changedProperties, fieldName + "."); } diff --git a/client/src/com/vaadin/client/communication/URLReference_Serializer.java b/client/src/com/vaadin/client/communication/URLReference_Serializer.java index 4ecdc606d2..71403c3fb3 100644 --- a/client/src/com/vaadin/client/communication/URLReference_Serializer.java +++ b/client/src/com/vaadin/client/communication/URLReference_Serializer.java @@ -16,26 +16,28 @@ package com.vaadin.client.communication; import com.google.gwt.core.client.GWT; -import com.google.gwt.json.client.JSONObject; -import com.google.gwt.json.client.JSONValue; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.metadata.Type; import com.vaadin.shared.communication.URLReference; +import elemental.json.Json; +import elemental.json.JsonObject; +import elemental.json.JsonValue; + public class URLReference_Serializer implements JSONSerializer<URLReference> { // setURL() -> uRL as first char becomes lower case... private static final String URL_FIELD = "uRL"; @Override - public URLReference deserialize(Type type, JSONValue jsonValue, + public URLReference deserialize(Type type, JsonValue jsonValue, ApplicationConnection connection) { TranslatedURLReference reference = GWT .create(TranslatedURLReference.class); reference.setConnection(connection); - JSONObject json = (JSONObject) jsonValue; - if (json.containsKey(URL_FIELD)) { - JSONValue jsonURL = json.get(URL_FIELD); + JsonObject json = (JsonObject) jsonValue; + if (json.hasKey(URL_FIELD)) { + JsonValue jsonURL = json.get(URL_FIELD); String URL = (String) JsonDecoder.decodeValue( new Type(String.class.getName(), null), jsonURL, null, connection); @@ -45,9 +47,9 @@ public class URLReference_Serializer implements JSONSerializer<URLReference> { } @Override - public JSONValue serialize(URLReference value, + public JsonValue serialize(URLReference value, ApplicationConnection connection) { - JSONObject json = new JSONObject(); + JsonObject json = Json.createObject(); // No type info required for encoding a String... json.put(URL_FIELD, JsonEncoder.encode(value.getURL(), null, connection)); diff --git a/client/src/com/vaadin/client/componentlocator/LegacyLocatorStrategy.java b/client/src/com/vaadin/client/componentlocator/LegacyLocatorStrategy.java index 5df9854038..517d979c8e 100644 --- a/client/src/com/vaadin/client/componentlocator/LegacyLocatorStrategy.java +++ b/client/src/com/vaadin/client/componentlocator/LegacyLocatorStrategy.java @@ -32,6 +32,7 @@ import com.vaadin.client.ConnectorMap; import com.vaadin.client.ServerConnector; import com.vaadin.client.Util; import com.vaadin.client.VCaption; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.SubPartAware; import com.vaadin.client.ui.VCssLayout; import com.vaadin.client.ui.VGridLayout; @@ -211,10 +212,10 @@ public class LegacyLocatorStrategy implements LocatorStrategy { // widget to which the path is relative. Otherwise, the current // implementation simply interprets the path as if baseElement was // null. - Widget baseWidget = Util.findWidget(baseElement, null); + Widget baseWidget = WidgetUtil.findWidget(baseElement, null); Widget w = getWidgetFromPath(widgetPath, baseWidget); - if (w == null || !Util.isAttachedAndDisplayed(w)) { + if (w == null || !WidgetUtil.isAttachedAndDisplayed(w)) { return null; } if (parts.length == 1) { @@ -333,7 +334,7 @@ public class LegacyLocatorStrategy implements LocatorStrategy { String childIndexString = part.substring("domChild[".length(), part.length() - 1); - if (Util.findWidget(baseElement, null) instanceof VAbstractOrderedLayout) { + if (WidgetUtil.findWidget(baseElement, null) instanceof VAbstractOrderedLayout) { if (element.hasChildNodes()) { Element e = element.getFirstChildElement().cast(); String cn = e.getClassName(); diff --git a/client/src/com/vaadin/client/connectors/AbstractRendererConnector.java b/client/src/com/vaadin/client/connectors/AbstractRendererConnector.java new file mode 100644 index 0000000000..f7e3c15ac8 --- /dev/null +++ b/client/src/com/vaadin/client/connectors/AbstractRendererConnector.java @@ -0,0 +1,182 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.connectors; + +import com.vaadin.client.ServerConnector; +import com.vaadin.client.communication.JsonDecoder; +import com.vaadin.client.extensions.AbstractExtensionConnector; +import com.vaadin.client.metadata.NoDataException; +import com.vaadin.client.metadata.Type; +import com.vaadin.client.metadata.TypeData; +import com.vaadin.client.metadata.TypeDataStore; +import com.vaadin.client.renderers.Renderer; +import com.vaadin.client.widgets.Grid.Column; + +import elemental.json.JsonObject; +import elemental.json.JsonValue; + +/** + * An abstract base class for renderer connectors. A renderer connector is used + * to link a client-side {@link Renderer} to a server-side + * {@link com.vaadin.ui.components.grid.Renderer Renderer}. As a connector, it + * can use the regular Vaadin RPC and shared state mechanism to pass additional + * state and information between the client and the server. This base class + * itself only uses the basic + * {@link com.vaadin.shared.communication.SharedState SharedState} and no RPC + * interfaces. + * + * @param <T> + * the presentation type of the renderer + * + * @since 7.4 + * @author Vaadin Ltd + */ +public abstract class AbstractRendererConnector<T> extends + AbstractExtensionConnector { + + private Renderer<T> renderer = null; + + private final Type presentationType = TypeDataStore + .getPresentationType(this.getClass()); + + protected AbstractRendererConnector() { + if (presentationType == null) { + throw new IllegalStateException( + "No presentation type found for " + + getClass().getSimpleName() + + ". This may be caused by some unspecified problem in widgetset compilation."); + } + } + + /** + * Returns the renderer associated with this renderer connector. + * <p> + * A subclass of AbstractRendererConnector should override this method as + * shown below. The framework uses + * {@link com.google.gwt.core.client.GWT#create(Class) GWT.create(Class)} to + * create a renderer based on the return type of the overridden method, but + * only if {@link #createRenderer()} is not overridden as well: + * + * <pre> + * public MyRenderer getRenderer() { + * return (MyRenderer) super.getRenderer(); + * } + * </pre> + * + * @return the renderer bound to this connector + */ + public Renderer<T> getRenderer() { + if (renderer == null) { + renderer = createRenderer(); + } + return renderer; + } + + /** + * Creates a new Renderer instance associated with this renderer connector. + * <p> + * You should typically not override this method since the framework by + * default generates an implementation that uses + * {@link com.google.gwt.core.client.GWT#create(Class)} to create a renderer + * of the same type as returned by the most specific override of + * {@link #getRenderer()}. If you do override the method, you can't call + * <code>super.createRenderer()</code> since the metadata needed for that + * implementation is not generated if there's an override of the method. + * + * @return a new renderer to be used with this connector + */ + protected Renderer<T> createRenderer() { + // TODO generate type data + Type type = TypeData.getType(getClass()); + try { + Type rendererType = type.getMethod("getRenderer").getReturnType(); + @SuppressWarnings("unchecked") + Renderer<T> instance = (Renderer<T>) rendererType.createInstance(); + return instance; + } catch (NoDataException e) { + throw new IllegalStateException( + "Default implementation of createRenderer() does not work for " + + getClass().getSimpleName() + + ". This might be caused by explicitely using " + + "super.createRenderer() or some unspecified " + + "problem with the widgetset compilation.", e); + } + } + + /** + * Decodes the given JSON value into a value of type T so it can be passed + * to the {@link #getRenderer() renderer}. + * + * @param value + * the value to decode + * @return the decoded value of {@code value} + */ + public T decode(JsonValue value) { + @SuppressWarnings("unchecked") + T decodedValue = (T) JsonDecoder.decodeValue(presentationType, value, + null, getConnection()); + return decodedValue; + } + + @Override + @Deprecated + protected void extend(ServerConnector target) { + // NOOP + } + + /** + * Gets the row key for a row object. + * <p> + * In case this renderer wants be able to identify a row in such a way that + * the server also understands it, the row key is used for that. Rows are + * identified by unified keys between the client and the server. + * + * @param row + * the row object + * @return the row key for the given row + */ + protected String getRowKey(JsonObject row) { + final ServerConnector parent = getParent(); + if (parent instanceof GridConnector) { + return ((GridConnector) parent).getRowKey(row); + } else { + throw new IllegalStateException("Renderers can only be used " + + "with a Grid."); + } + } + + /** + * Gets the column id for a column. + * <p> + * In case this renderer wants be able to identify a column in such a way + * that the server also understands it, the column id is used for that. + * Columns are identified by unified ids between the client and the server. + * + * @param column + * the column object + * @return the column id for the given column + */ + protected String getColumnId(Column<?, JsonObject> column) { + final ServerConnector parent = getParent(); + if (parent instanceof GridConnector) { + return ((GridConnector) parent).getColumnId(column); + } else { + throw new IllegalStateException("Renderers can only be used " + + "with a Grid."); + } + } + +} diff --git a/client/src/com/vaadin/client/connectors/ButtonRendererConnector.java b/client/src/com/vaadin/client/connectors/ButtonRendererConnector.java new file mode 100644 index 0000000000..44c34e3bf4 --- /dev/null +++ b/client/src/com/vaadin/client/connectors/ButtonRendererConnector.java @@ -0,0 +1,44 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.connectors; + +import com.google.web.bindery.event.shared.HandlerRegistration; +import com.vaadin.client.renderers.ButtonRenderer; +import com.vaadin.client.renderers.ClickableRenderer.RendererClickHandler; +import com.vaadin.shared.ui.Connect; + +import elemental.json.JsonObject; + +/** + * A connector for {@link ButtonRenderer}. + * + * @since 7.4 + * @author Vaadin Ltd + */ +@Connect(com.vaadin.ui.renderer.ButtonRenderer.class) +public class ButtonRendererConnector extends ClickableRendererConnector<String> { + + @Override + public ButtonRenderer getRenderer() { + return (ButtonRenderer) super.getRenderer(); + } + + @Override + protected HandlerRegistration addClickHandler( + RendererClickHandler<JsonObject> handler) { + return getRenderer().addClickHandler(handler); + } +} diff --git a/client/src/com/vaadin/client/connectors/ClickableRendererConnector.java b/client/src/com/vaadin/client/connectors/ClickableRendererConnector.java new file mode 100644 index 0000000000..87f88c5106 --- /dev/null +++ b/client/src/com/vaadin/client/connectors/ClickableRendererConnector.java @@ -0,0 +1,61 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.connectors; + +import com.google.web.bindery.event.shared.HandlerRegistration; +import com.vaadin.client.MouseEventDetailsBuilder; +import com.vaadin.client.renderers.ClickableRenderer.RendererClickEvent; +import com.vaadin.client.renderers.ClickableRenderer.RendererClickHandler; +import com.vaadin.shared.ui.grid.renderers.RendererClickRpc; + +import elemental.json.JsonObject; + +/** + * An abstract base class for {@link ClickableRenderer} connectors. + * + * @param <T> + * the presentation type of the renderer + * + * @since 7.4 + * @author Vaadin Ltd + */ +public abstract class ClickableRendererConnector<T> extends + AbstractRendererConnector<T> { + + HandlerRegistration clickRegistration; + + @Override + protected void init() { + clickRegistration = addClickHandler(new RendererClickHandler<JsonObject>() { + @Override + public void onClick(RendererClickEvent<JsonObject> event) { + getRpcProxy(RendererClickRpc.class).click( + getRowKey(event.getCell().getRow()), + getColumnId(event.getCell().getColumn()), + MouseEventDetailsBuilder.buildMouseEventDetails(event + .getNativeEvent())); + } + }); + } + + @Override + public void onUnregister() { + clickRegistration.removeHandler(); + } + + protected abstract HandlerRegistration addClickHandler( + RendererClickHandler<JsonObject> handler); +} diff --git a/client/src/com/vaadin/client/connectors/DateRendererConnector.java b/client/src/com/vaadin/client/connectors/DateRendererConnector.java new file mode 100644 index 0000000000..30d1db345d --- /dev/null +++ b/client/src/com/vaadin/client/connectors/DateRendererConnector.java @@ -0,0 +1,34 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.connectors; + +import com.vaadin.shared.ui.Connect; + +/** + * A connector for {@link com.vaadin.ui.components.grid.renderers.DateRenderer + * DateRenderer}. + * <p> + * The server-side Renderer operates on dates, but the data is serialized as a + * string, and displayed as-is on the client side. This is to be able to support + * the server's locale. + * + * @since 7.4 + * @author Vaadin Ltd + */ +@Connect(com.vaadin.ui.renderer.DateRenderer.class) +public class DateRendererConnector extends TextRendererConnector { + // No implementation needed +} diff --git a/client/src/com/vaadin/client/connectors/GridConnector.java b/client/src/com/vaadin/client/connectors/GridConnector.java new file mode 100644 index 0000000000..827e366de0 --- /dev/null +++ b/client/src/com/vaadin/client/connectors/GridConnector.java @@ -0,0 +1,964 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.connectors; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.logging.Logger; + +import com.google.gwt.core.client.Scheduler; +import com.google.gwt.core.client.Scheduler.ScheduledCommand; +import com.google.gwt.dom.client.NativeEvent; +import com.google.gwt.user.client.ui.Widget; +import com.vaadin.client.ComponentConnector; +import com.vaadin.client.ConnectorHierarchyChangeEvent; +import com.vaadin.client.MouseEventDetailsBuilder; +import com.vaadin.client.annotations.OnStateChange; +import com.vaadin.client.communication.StateChangeEvent; +import com.vaadin.client.connectors.RpcDataSourceConnector.RpcDataSource; +import com.vaadin.client.data.DataSource.RowHandle; +import com.vaadin.client.renderers.Renderer; +import com.vaadin.client.ui.AbstractFieldConnector; +import com.vaadin.client.ui.AbstractHasComponentsConnector; +import com.vaadin.client.ui.SimpleManagedLayout; +import com.vaadin.client.widget.grid.CellReference; +import com.vaadin.client.widget.grid.CellStyleGenerator; +import com.vaadin.client.widget.grid.EditorHandler; +import com.vaadin.client.widget.grid.RowReference; +import com.vaadin.client.widget.grid.RowStyleGenerator; +import com.vaadin.client.widget.grid.events.BodyClickHandler; +import com.vaadin.client.widget.grid.events.BodyDoubleClickHandler; +import com.vaadin.client.widget.grid.events.GridClickEvent; +import com.vaadin.client.widget.grid.events.GridDoubleClickEvent; +import com.vaadin.client.widget.grid.events.SelectAllEvent; +import com.vaadin.client.widget.grid.events.SelectAllHandler; +import com.vaadin.client.widget.grid.selection.AbstractRowHandleSelectionModel; +import com.vaadin.client.widget.grid.selection.SelectionEvent; +import com.vaadin.client.widget.grid.selection.SelectionHandler; +import com.vaadin.client.widget.grid.selection.SelectionModelMulti; +import com.vaadin.client.widget.grid.selection.SelectionModelNone; +import com.vaadin.client.widget.grid.selection.SelectionModelSingle; +import com.vaadin.client.widget.grid.sort.SortEvent; +import com.vaadin.client.widget.grid.sort.SortHandler; +import com.vaadin.client.widget.grid.sort.SortOrder; +import com.vaadin.client.widgets.Grid; +import com.vaadin.client.widgets.Grid.Column; +import com.vaadin.client.widgets.Grid.FooterCell; +import com.vaadin.client.widgets.Grid.FooterRow; +import com.vaadin.client.widgets.Grid.HeaderCell; +import com.vaadin.client.widgets.Grid.HeaderRow; +import com.vaadin.shared.data.sort.SortDirection; +import com.vaadin.shared.ui.Connect; +import com.vaadin.shared.ui.grid.EditorClientRpc; +import com.vaadin.shared.ui.grid.EditorServerRpc; +import com.vaadin.shared.ui.grid.GridClientRpc; +import com.vaadin.shared.ui.grid.GridColumnState; +import com.vaadin.shared.ui.grid.GridConstants; +import com.vaadin.shared.ui.grid.GridServerRpc; +import com.vaadin.shared.ui.grid.GridState; +import com.vaadin.shared.ui.grid.GridState.SharedSelectionMode; +import com.vaadin.shared.ui.grid.GridStaticSectionState; +import com.vaadin.shared.ui.grid.GridStaticSectionState.CellState; +import com.vaadin.shared.ui.grid.GridStaticSectionState.RowState; +import com.vaadin.shared.ui.grid.ScrollDestination; + +import elemental.json.JsonObject; +import elemental.json.JsonValue; + +/** + * Connects the client side {@link Grid} widget with the server side + * {@link com.vaadin.ui.components.grid.Grid} component. + * <p> + * The Grid is typed to JSONObject. The structure of the JSONObject is described + * at {@link com.vaadin.shared.data.DataProviderRpc#setRowData(int, List) + * DataProviderRpc.setRowData(int, List)}. + * + * @since 7.4 + * @author Vaadin Ltd + */ +@Connect(com.vaadin.ui.Grid.class) +public class GridConnector extends AbstractHasComponentsConnector implements + SimpleManagedLayout { + + private static final class CustomCellStyleGenerator implements + CellStyleGenerator<JsonObject> { + @Override + public String getStyle(CellReference<JsonObject> cellReference) { + JsonObject row = cellReference.getRow(); + if (!row.hasKey(GridState.JSONKEY_CELLSTYLES)) { + return null; + } + + Column<?, JsonObject> column = cellReference.getColumn(); + if (!(column instanceof CustomGridColumn)) { + // Selection checkbox column + return null; + } + CustomGridColumn c = (CustomGridColumn) column; + + JsonObject cellStylesObject = row + .getObject(GridState.JSONKEY_CELLSTYLES); + assert cellStylesObject != null; + + if (cellStylesObject.hasKey(c.id)) { + return cellStylesObject.getString(c.id); + } else { + return null; + } + } + + } + + private static final class CustomRowStyleGenerator implements + RowStyleGenerator<JsonObject> { + @Override + public String getStyle(RowReference<JsonObject> rowReference) { + JsonObject row = rowReference.getRow(); + if (row.hasKey(GridState.JSONKEY_ROWSTYLE)) { + return row.getString(GridState.JSONKEY_ROWSTYLE); + } else { + return null; + } + } + + } + + /** + * Custom implementation of the custom grid column using a JSONObject to + * represent the cell value and String as a column type. + */ + private class CustomGridColumn extends Grid.Column<Object, JsonObject> { + + private final String id; + + private AbstractRendererConnector<Object> rendererConnector; + + private AbstractFieldConnector editorConnector; + + public CustomGridColumn(String id, + AbstractRendererConnector<Object> rendererConnector) { + super(rendererConnector.getRenderer()); + this.rendererConnector = rendererConnector; + this.id = id; + } + + @Override + public Object getValue(final JsonObject obj) { + final JsonObject rowData = obj.getObject(GridState.JSONKEY_DATA); + + if (rowData.hasKey(id)) { + final JsonValue columnValue = rowData.get(id); + + return rendererConnector.decode(columnValue); + } + + return null; + } + + /* + * Only used to check that the renderer connector will not change during + * the column lifetime. + * + * TODO remove once support for changing renderers is implemented + */ + private AbstractRendererConnector<Object> getRendererConnector() { + return rendererConnector; + } + + private AbstractFieldConnector getEditorConnector() { + return editorConnector; + } + + private void setEditorConnector(AbstractFieldConnector editorConnector) { + this.editorConnector = editorConnector; + } + } + + /* + * An editor handler using Vaadin RPC to manage the editor state. + */ + private class CustomEditorHandler implements EditorHandler<JsonObject> { + + private EditorServerRpc rpc = getRpcProxy(EditorServerRpc.class); + + private EditorRequest<?> currentRequest = null; + private boolean serverInitiated = false; + + public CustomEditorHandler() { + registerRpc(EditorClientRpc.class, new EditorClientRpc() { + + @Override + public void bind(final int rowIndex) { + // call this finally to avoid issues with editing on init + Scheduler.get().scheduleFinally(new ScheduledCommand() { + @Override + public void execute() { + GridConnector.this.getWidget().editRow(rowIndex); + } + }); + } + + @Override + public void cancel(int rowIndex) { + serverInitiated = true; + GridConnector.this.getWidget().cancelEditor(); + } + + @Override + public void confirmBind(final boolean bindSucceeded) { + endRequest(bindSucceeded); + } + + @Override + public void confirmSave(boolean saveSucceeded) { + endRequest(saveSucceeded); + } + }); + } + + @Override + public void bind(EditorRequest<JsonObject> request) { + startRequest(request); + rpc.bind(request.getRowIndex()); + } + + @Override + public void save(EditorRequest<JsonObject> request) { + startRequest(request); + rpc.save(request.getRowIndex()); + } + + @Override + public void cancel(EditorRequest<JsonObject> request) { + if (!handleServerInitiated(request)) { + // No startRequest as we don't get (or need) + // a confirmation from the server + rpc.cancel(request.getRowIndex()); + } + } + + @Override + public Widget getWidget(Grid.Column<?, JsonObject> column) { + assert column != null; + + if (column instanceof CustomGridColumn) { + AbstractFieldConnector c = ((CustomGridColumn) column) + .getEditorConnector(); + return c != null ? c.getWidget() : null; + } else { + throw new IllegalStateException("Unexpected column type: " + + column.getClass().getName()); + } + } + + /** + * Used to handle the case where the editor calls us because it was + * invoked by the server via RPC and not by the client. In that case, + * the request can be simply synchronously completed. + * + * @param request + * the request object + * @return true if the request was originally triggered by the server, + * false otherwise + */ + private boolean handleServerInitiated(EditorRequest<?> request) { + assert request != null : "Cannot handle null request"; + assert currentRequest == null : "Earlier request not yet finished"; + + if (serverInitiated) { + serverInitiated = false; + request.success(); + return true; + } else { + return false; + } + } + + private void startRequest(EditorRequest<?> request) { + assert currentRequest == null : "Earlier request not yet finished"; + + currentRequest = request; + } + + private void endRequest(boolean succeeded) { + assert currentRequest != null : "Current request was null"; + /* + * Clear current request first to ensure the state is valid if + * another request is made in the callback. + */ + EditorRequest<?> request = currentRequest; + currentRequest = null; + if (succeeded) { + request.success(); + } else { + request.fail(); + } + } + } + + private class ItemClickHandler implements BodyClickHandler, + BodyDoubleClickHandler { + + @Override + public void onClick(GridClickEvent event) { + if (hasEventListener(GridConstants.ITEM_CLICK_EVENT_ID)) { + fireItemClick(event.getTargetCell(), event.getNativeEvent()); + } + } + + @Override + public void onDoubleClick(GridDoubleClickEvent event) { + if (hasEventListener(GridConstants.ITEM_CLICK_EVENT_ID)) { + fireItemClick(event.getTargetCell(), event.getNativeEvent()); + } + } + + private void fireItemClick(CellReference<?> cell, NativeEvent mouseEvent) { + String rowKey = getRowKey((JsonObject) cell.getRow()); + String columnId = getColumnId(cell.getColumn()); + getRpcProxy(GridServerRpc.class) + .itemClick( + rowKey, + columnId, + MouseEventDetailsBuilder + .buildMouseEventDetails(mouseEvent)); + } + } + + /** + * Maps a generated column id to a grid column instance + */ + private Map<String, CustomGridColumn> columnIdToColumn = new HashMap<String, CustomGridColumn>(); + + private AbstractRowHandleSelectionModel<JsonObject> selectionModel; + private Set<String> selectedKeys = new LinkedHashSet<String>(); + private List<String> columnOrder = new ArrayList<String>(); + + /** + * updateFromState is set to true when {@link #updateSelectionFromState()} + * makes changes to selection. This flag tells the + * {@code internalSelectionChangeHandler} to not send same data straight + * back to server. Said listener sets it back to false when handling that + * event. + */ + private boolean updatedFromState = false; + + private RpcDataSource dataSource; + + private SelectionHandler<JsonObject> internalSelectionChangeHandler = new SelectionHandler<JsonObject>() { + @Override + public void onSelect(SelectionEvent<JsonObject> event) { + if (event.isBatchedSelection()) { + return; + } + if (!updatedFromState) { + for (JsonObject row : event.getRemoved()) { + selectedKeys.remove(dataSource.getRowKey(row)); + } + + for (JsonObject row : event.getAdded()) { + selectedKeys.add(dataSource.getRowKey(row)); + } + + getRpcProxy(GridServerRpc.class).select( + new ArrayList<String>(selectedKeys)); + } else { + updatedFromState = false; + } + } + }; + + private ItemClickHandler itemClickHandler = new ItemClickHandler(); + + private String lastKnownTheme = null; + + @Override + @SuppressWarnings("unchecked") + public Grid<JsonObject> getWidget() { + return (Grid<JsonObject>) super.getWidget(); + } + + @Override + public GridState getState() { + return (GridState) super.getState(); + } + + @Override + protected void init() { + super.init(); + + // All scroll RPC calls are executed finally to avoid issues on init + registerRpc(GridClientRpc.class, new GridClientRpc() { + @Override + public void scrollToStart() { + Scheduler.get().scheduleFinally(new ScheduledCommand() { + @Override + public void execute() { + getWidget().scrollToStart(); + } + }); + } + + @Override + public void scrollToEnd() { + Scheduler.get().scheduleFinally(new ScheduledCommand() { + @Override + public void execute() { + getWidget().scrollToEnd(); + } + }); + } + + @Override + public void scrollToRow(final int row, + final ScrollDestination destination) { + Scheduler.get().scheduleFinally(new ScheduledCommand() { + @Override + public void execute() { + getWidget().scrollToRow(row, destination); + } + }); + } + }); + + getWidget().addSelectionHandler(internalSelectionChangeHandler); + + /* Item click events */ + getWidget().addBodyClickHandler(itemClickHandler); + getWidget().addBodyDoubleClickHandler(itemClickHandler); + + getWidget().addSortHandler(new SortHandler<JsonObject>() { + @Override + public void sort(SortEvent<JsonObject> event) { + List<SortOrder> order = event.getOrder(); + String[] columnIds = new String[order.size()]; + SortDirection[] directions = new SortDirection[order.size()]; + for (int i = 0; i < order.size(); i++) { + SortOrder sortOrder = order.get(i); + CustomGridColumn column = (CustomGridColumn) sortOrder + .getColumn(); + columnIds[i] = column.id; + + directions[i] = sortOrder.getDirection(); + } + + if (!Arrays.equals(columnIds, getState().sortColumns) + || !Arrays.equals(directions, getState().sortDirs)) { + // Report back to server if changed + getRpcProxy(GridServerRpc.class).sort(columnIds, + directions, event.isUserOriginated()); + } + } + }); + + getWidget().addSelectAllHandler(new SelectAllHandler<JsonObject>() { + + @Override + public void onSelectAll(SelectAllEvent<JsonObject> event) { + getRpcProxy(GridServerRpc.class).selectAll(); + } + + }); + + getWidget().setEditorHandler(new CustomEditorHandler()); + getLayoutManager().registerDependency(this, getWidget().getElement()); + layout(); + } + + @Override + public void onStateChanged(final StateChangeEvent stateChangeEvent) { + super.onStateChanged(stateChangeEvent); + + // Column updates + if (stateChangeEvent.hasPropertyChanged("columns")) { + + // Remove old columns + purgeRemovedColumns(); + + // Add new columns + for (GridColumnState state : getState().columns) { + if (!columnIdToColumn.containsKey(state.id)) { + addColumnFromStateChangeEvent(state); + } + updateColumnFromState(columnIdToColumn.get(state.id), state); + } + } + + if (stateChangeEvent.hasPropertyChanged("columnOrder")) { + if (orderNeedsUpdate(getState().columnOrder)) { + updateColumnOrderFromState(getState().columnOrder); + } + } + + // Header and footer + if (stateChangeEvent.hasPropertyChanged("header")) { + updateHeaderFromState(getState().header); + } + + if (stateChangeEvent.hasPropertyChanged("footer")) { + updateFooterFromState(getState().footer); + } + + // Selection + if (stateChangeEvent.hasPropertyChanged("selectionMode")) { + onSelectionModeChange(); + } + if (stateChangeEvent.hasPropertyChanged("selectedKeys")) { + updateSelectionFromState(); + } + + // Sorting + if (stateChangeEvent.hasPropertyChanged("sortColumns") + || stateChangeEvent.hasPropertyChanged("sortDirs")) { + onSortStateChange(); + } + + // Editor + if (stateChangeEvent.hasPropertyChanged("editorEnabled")) { + getWidget().setEditorEnabled(getState().editorEnabled); + } + + // Frozen columns + if (stateChangeEvent.hasPropertyChanged("frozenColumnCount")) { + getWidget().setFrozenColumnCount(getState().frozenColumnCount); + } + + // Theme features + String activeTheme = getConnection().getUIConnector().getActiveTheme(); + if (lastKnownTheme == null) { + lastKnownTheme = activeTheme; + } else if (!lastKnownTheme.equals(activeTheme)) { + getWidget().resetSizesFromDom(); + lastKnownTheme = activeTheme; + } + } + + private void updateColumnOrderFromState(List<String> stateColumnOrder) { + CustomGridColumn[] columns = new CustomGridColumn[stateColumnOrder + .size()]; + int i = 0; + for (String id : stateColumnOrder) { + columns[i] = columnIdToColumn.get(id); + i++; + } + getWidget().setColumnOrder(columns); + columnOrder = stateColumnOrder; + } + + private boolean orderNeedsUpdate(List<String> stateColumnOrder) { + if (stateColumnOrder.size() == columnOrder.size()) { + for (int i = 0; i < columnOrder.size(); ++i) { + if (!stateColumnOrder.get(i).equals(columnOrder.get(i))) { + return true; + } + } + return false; + } + return true; + } + + private void updateHeaderFromState(GridStaticSectionState state) { + getWidget().setHeaderVisible(state.visible); + + while (getWidget().getHeaderRowCount() > 0) { + getWidget().removeHeaderRow(0); + } + + for (RowState rowState : state.rows) { + HeaderRow row = getWidget().appendHeaderRow(); + + for (CellState cellState : rowState.cells) { + CustomGridColumn column = columnIdToColumn + .get(cellState.columnId); + updateHeaderCellFromState(row.getCell(column), cellState); + } + + for (Set<String> group : rowState.cellGroups.keySet()) { + Grid.Column<?, ?>[] columns = new Grid.Column<?, ?>[group + .size()]; + CellState cellState = rowState.cellGroups.get(group); + + int i = 0; + for (String columnId : group) { + columns[i] = columnIdToColumn.get(columnId); + i++; + } + + // Set state to be the same as first in group. + updateHeaderCellFromState(row.join(columns), cellState); + } + + if (rowState.defaultRow) { + getWidget().setDefaultHeaderRow(row); + } + + row.setStyleName(rowState.styleName); + } + } + + private void updateHeaderCellFromState(HeaderCell cell, CellState cellState) { + switch (cellState.type) { + case TEXT: + cell.setText(cellState.text); + break; + case HTML: + cell.setHtml(cellState.html); + break; + case WIDGET: + ComponentConnector connector = (ComponentConnector) cellState.connector; + cell.setWidget(connector.getWidget()); + break; + default: + throw new IllegalStateException("unexpected cell type: " + + cellState.type); + } + cell.setStyleName(cellState.styleName); + } + + private void updateFooterFromState(GridStaticSectionState state) { + getWidget().setFooterVisible(state.visible); + + while (getWidget().getFooterRowCount() > 0) { + getWidget().removeFooterRow(0); + } + + for (RowState rowState : state.rows) { + FooterRow row = getWidget().appendFooterRow(); + + for (CellState cellState : rowState.cells) { + CustomGridColumn column = columnIdToColumn + .get(cellState.columnId); + updateFooterCellFromState(row.getCell(column), cellState); + } + + for (Set<String> group : rowState.cellGroups.keySet()) { + Grid.Column<?, ?>[] columns = new Grid.Column<?, ?>[group + .size()]; + CellState cellState = rowState.cellGroups.get(group); + + int i = 0; + for (String columnId : group) { + columns[i] = columnIdToColumn.get(columnId); + i++; + } + + // Set state to be the same as first in group. + updateFooterCellFromState(row.join(columns), cellState); + } + + row.setStyleName(rowState.styleName); + } + } + + private void updateFooterCellFromState(FooterCell cell, CellState cellState) { + switch (cellState.type) { + case TEXT: + cell.setText(cellState.text); + break; + case HTML: + cell.setHtml(cellState.html); + break; + case WIDGET: + ComponentConnector connector = (ComponentConnector) cellState.connector; + cell.setWidget(connector.getWidget()); + break; + default: + throw new IllegalStateException("unexpected cell type: " + + cellState.type); + } + cell.setStyleName(cellState.styleName); + } + + /** + * Updates a column from a state change event. + * + * @param columnIndex + * The index of the column to update + */ + private void updateColumnFromStateChangeEvent(GridColumnState columnState) { + CustomGridColumn column = columnIdToColumn.get(columnState.id); + + updateColumnFromState(column, columnState); + + if (columnState.rendererConnector != column.getRendererConnector()) { + throw new UnsupportedOperationException( + "Changing column renderer after initialization is currently unsupported"); + } + } + + /** + * Adds a new column to the grid widget from a state change event + * + * @param columnIndex + * The index of the column, according to how it + */ + private void addColumnFromStateChangeEvent(GridColumnState state) { + @SuppressWarnings("unchecked") + CustomGridColumn column = new CustomGridColumn(state.id, + ((AbstractRendererConnector<Object>) state.rendererConnector)); + columnIdToColumn.put(state.id, column); + + /* + * Add column to grid. Reordering is handled as a separate problem. + */ + getWidget().addColumn(column); + columnOrder.add(state.id); + } + + /** + * If we have a selection column renderer, we need to offset the index by + * one when referring to the column index in the widget. + */ + private int getWidgetColumnIndex(final int columnIndex) { + Renderer<Boolean> selectionColumnRenderer = getWidget() + .getSelectionModel().getSelectionColumnRenderer(); + int widgetColumnIndex = columnIndex; + if (selectionColumnRenderer != null) { + widgetColumnIndex++; + } + return widgetColumnIndex; + } + + /** + * Updates the column values from a state + * + * @param column + * The column to update + * @param state + * The state to get the data from + */ + private static void updateColumnFromState(CustomGridColumn column, + GridColumnState state) { + column.setWidth(state.width); + column.setMinimumWidth(state.minWidth); + column.setMaximumWidth(state.maxWidth); + column.setExpandRatio(state.expandRatio); + + column.setSortable(state.sortable); + column.setEditorConnector((AbstractFieldConnector) state.editorConnector); + } + + /** + * Removes any orphan columns that has been removed from the state from the + * grid + */ + private void purgeRemovedColumns() { + + // Get columns still registered in the state + Set<String> columnsInState = new HashSet<String>(); + for (GridColumnState columnState : getState().columns) { + columnsInState.add(columnState.id); + } + + // Remove column no longer in state + Iterator<String> columnIdIterator = columnIdToColumn.keySet() + .iterator(); + while (columnIdIterator.hasNext()) { + String id = columnIdIterator.next(); + if (!columnsInState.contains(id)) { + CustomGridColumn column = columnIdToColumn.get(id); + columnIdIterator.remove(); + getWidget().removeColumn(column); + columnOrder.remove(id); + } + } + } + + public void setDataSource(RpcDataSource dataSource) { + this.dataSource = dataSource; + getWidget().setDataSource(this.dataSource); + } + + private void onSelectionModeChange() { + SharedSelectionMode mode = getState().selectionMode; + if (mode == null) { + getLogger().fine("ignored mode change"); + return; + } + + AbstractRowHandleSelectionModel<JsonObject> model = createSelectionModel(mode); + if (selectionModel == null + || !model.getClass().equals(selectionModel.getClass())) { + selectionModel = model; + getWidget().setSelectionModel(model); + selectedKeys.clear(); + } + } + + @OnStateChange("hasCellStyleGenerator") + private void onCellStyleGeneratorChange() { + if (getState().hasCellStyleGenerator) { + getWidget().setCellStyleGenerator(new CustomCellStyleGenerator()); + } else { + getWidget().setCellStyleGenerator(null); + } + } + + @OnStateChange("hasRowStyleGenerator") + private void onRowStyleGeneratorChange() { + if (getState().hasRowStyleGenerator) { + getWidget().setRowStyleGenerator(new CustomRowStyleGenerator()); + } else { + getWidget().setRowStyleGenerator(null); + } + } + + private void updateSelectionFromState() { + boolean changed = false; + + List<String> stateKeys = getState().selectedKeys; + + // find new deselections + for (String key : selectedKeys) { + if (!stateKeys.contains(key)) { + changed = true; + deselectByHandle(dataSource.getHandleByKey(key)); + } + } + + // find new selections + for (String key : stateKeys) { + if (!selectedKeys.contains(key)) { + changed = true; + selectByHandle(dataSource.getHandleByKey(key)); + } + } + + /* + * A defensive copy in case the collection in the state is mutated + * instead of re-assigned. + */ + selectedKeys = new LinkedHashSet<String>(stateKeys); + + /* + * We need to fire this event so that Grid is able to re-render the + * selection changes (if applicable). + */ + if (changed) { + // At least for now there's no way to send the selected and/or + // deselected row data. Some data is only stored as keys + updatedFromState = true; + getWidget().fireEvent( + new SelectionEvent<JsonObject>(getWidget(), + (List<JsonObject>) null, null, false)); + } + } + + private void onSortStateChange() { + List<SortOrder> sortOrder = new ArrayList<SortOrder>(); + + String[] sortColumns = getState().sortColumns; + SortDirection[] sortDirs = getState().sortDirs; + + for (int i = 0; i < sortColumns.length; i++) { + sortOrder.add(new SortOrder(columnIdToColumn.get(sortColumns[i]), + sortDirs[i])); + } + + getWidget().setSortOrder(sortOrder); + } + + private Logger getLogger() { + return Logger.getLogger(getClass().getName()); + } + + @SuppressWarnings("static-method") + private AbstractRowHandleSelectionModel<JsonObject> createSelectionModel( + SharedSelectionMode mode) { + switch (mode) { + case SINGLE: + return new SelectionModelSingle<JsonObject>(); + case MULTI: + return new SelectionModelMulti<JsonObject>(); + case NONE: + return new SelectionModelNone<JsonObject>(); + default: + throw new IllegalStateException("unexpected mode value: " + mode); + } + } + + /** + * A workaround method for accessing the protected method + * {@code AbstractRowHandleSelectionModel.selectByHandle} + */ + private native void selectByHandle(RowHandle<JsonObject> handle) + /*-{ + var model = this.@com.vaadin.client.connectors.GridConnector::selectionModel; + model.@com.vaadin.client.widget.grid.selection.AbstractRowHandleSelectionModel::selectByHandle(*)(handle); + }-*/; + + /** + * A workaround method for accessing the protected method + * {@code AbstractRowHandleSelectionModel.deselectByHandle} + */ + private native void deselectByHandle(RowHandle<JsonObject> handle) + /*-{ + var model = this.@com.vaadin.client.connectors.GridConnector::selectionModel; + model.@com.vaadin.client.widget.grid.selection.AbstractRowHandleSelectionModel::deselectByHandle(*)(handle); + }-*/; + + /** + * Gets the row key for a row object. + * + * @param row + * the row object + * @return the key for the given row + */ + public String getRowKey(JsonObject row) { + final Object key = dataSource.getRowKey(row); + assert key instanceof String : "Internal key was not a String but a " + + key.getClass().getSimpleName() + " (" + key + ")"; + return (String) key; + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.client.HasComponentsConnector#updateCaption(com.vaadin.client + * .ComponentConnector) + */ + @Override + public void updateCaption(ComponentConnector connector) { + // TODO Auto-generated method stub + + } + + @Override + public void onConnectorHierarchyChange( + ConnectorHierarchyChangeEvent connectorHierarchyChangeEvent) { + } + + public String getColumnId(Grid.Column<?, ?> column) { + if (column instanceof CustomGridColumn) { + return ((CustomGridColumn) column).id; + } + return null; + } + + @Override + public void layout() { + getWidget().onResize(); + } +} diff --git a/client/src/com/vaadin/client/connectors/ImageRendererConnector.java b/client/src/com/vaadin/client/connectors/ImageRendererConnector.java new file mode 100644 index 0000000000..9000ebb1c2 --- /dev/null +++ b/client/src/com/vaadin/client/connectors/ImageRendererConnector.java @@ -0,0 +1,55 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.connectors; + +import com.google.web.bindery.event.shared.HandlerRegistration; +import com.vaadin.client.communication.JsonDecoder; +import com.vaadin.client.metadata.TypeDataStore; +import com.vaadin.client.renderers.ClickableRenderer.RendererClickHandler; +import com.vaadin.client.renderers.ImageRenderer; +import com.vaadin.shared.communication.URLReference; +import com.vaadin.shared.ui.Connect; + +import elemental.json.JsonObject; +import elemental.json.JsonValue; + +/** + * A connector for {@link ImageRenderer}. + * + * @since 7.4 + * @author Vaadin Ltd + */ +@Connect(com.vaadin.ui.renderer.ImageRenderer.class) +public class ImageRendererConnector extends ClickableRendererConnector<String> { + + @Override + public ImageRenderer getRenderer() { + return (ImageRenderer) super.getRenderer(); + } + + @Override + public String decode(JsonValue value) { + return ((URLReference) JsonDecoder.decodeValue( + TypeDataStore.getType(URLReference.class), value, null, + getConnection())).getURL(); + } + + @Override + protected HandlerRegistration addClickHandler( + RendererClickHandler<JsonObject> handler) { + return getRenderer().addClickHandler(handler); + } +} diff --git a/client/src/com/vaadin/client/connectors/JavaScriptRendererConnector.java b/client/src/com/vaadin/client/connectors/JavaScriptRendererConnector.java new file mode 100644 index 0000000000..2670a3e184 --- /dev/null +++ b/client/src/com/vaadin/client/connectors/JavaScriptRendererConnector.java @@ -0,0 +1,280 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.connectors; + +import java.util.ArrayList; +import java.util.Collection; + +import com.google.gwt.core.client.JavaScriptObject; +import com.google.gwt.core.client.JsArrayString; +import com.google.gwt.dom.client.NativeEvent; +import com.vaadin.client.BrowserInfo; +import com.vaadin.client.JavaScriptConnectorHelper; +import com.vaadin.client.Util; +import com.vaadin.client.communication.HasJavaScriptConnectorHelper; +import com.vaadin.client.renderers.ComplexRenderer; +import com.vaadin.client.renderers.Renderer; +import com.vaadin.client.widget.grid.CellReference; +import com.vaadin.client.widget.grid.RendererCellReference; +import com.vaadin.shared.JavaScriptExtensionState; +import com.vaadin.shared.ui.Connect; +import com.vaadin.ui.renderer.AbstractJavaScriptRenderer; + +import elemental.json.JsonObject; +import elemental.json.JsonValue; + +/** + * Connector for server-side renderer implemented using JavaScript. + * + * @since 7.4 + * @author Vaadin Ltd + */ +@Connect(AbstractJavaScriptRenderer.class) +public class JavaScriptRendererConnector extends + AbstractRendererConnector<JsonValue> implements + HasJavaScriptConnectorHelper { + private final JavaScriptConnectorHelper helper = new JavaScriptConnectorHelper( + this); + + private final JavaScriptObject cellReferenceWrapper = createCellReferenceWrapper(BrowserInfo + .get().isIE8()); + + @Override + protected void init() { + super.init(); + helper.init(); + + addGetRowKey(helper.getConnectorWrapper()); + } + + private static native JavaScriptObject createCellReferenceWrapper( + boolean isIE8) + /*-{ + var reference = {}; + if (isIE8) { + // IE8 only supports defineProperty for DOM objects + reference = $doc.createElement('div'); + } + + var setProperty = function(name, getter, setter) { + var descriptor = { + get: getter + } + if (setter) { + descriptor.set = setter; + } + Object.defineProperty(reference, name, descriptor); + }; + + setProperty("element", function() { + return reference.target.@CellReference::getElement()(); + }, null); + + setProperty("rowIndex", function() { + return reference.target.@CellReference::getRowIndex()(); + }, null); + + setProperty("columnIndex", function() { + return reference.target.@CellReference::getColumnIndex()(); + }, null); + + setProperty("colSpan", function() { + return reference.target.@RendererCellReference::getColSpan()(); + }, function(colSpan) { + reference.target.@RendererCellReference::setColSpan(*)(colSpan); + }); + + return reference; + }-*/; + + @Override + public JavaScriptExtensionState getState() { + return (JavaScriptExtensionState) super.getState(); + } + + private native void addGetRowKey(JavaScriptObject wrapper) + /*-{ + var self = this; + wrapper.getRowKey = $entry(function(rowIndex) { + return @JavaScriptRendererConnector::findRowKey(*)(self, rowIndex); + }); + }-*/; + + private static String findRowKey(JavaScriptRendererConnector connector, + int rowIndex) { + GridConnector gc = (GridConnector) connector.getParent(); + JsonObject row = gc.getWidget().getDataSource().getRow(rowIndex); + return connector.getRowKey(row); + } + + private boolean hasFunction(String name) { + return hasFunction(helper.getConnectorWrapper(), name); + } + + private static native boolean hasFunction(JavaScriptObject wrapper, + String name) + /*-{ + return typeof wrapper[name] === 'function'; + }-*/; + + @Override + protected Renderer<JsonValue> createRenderer() { + helper.ensureJavascriptInited(); + + if (!hasFunction("render")) { + throw new RuntimeException("JavaScriptRenderer " + + helper.getInitFunctionName() + + " must have a function named 'render'"); + } + + final boolean hasInit = hasFunction("init"); + final boolean hasDestroy = hasFunction("destroy"); + final boolean hasOnActivate = hasFunction("onActivate"); + final boolean hasGetConsumedEvents = hasFunction("getConsumedEvents"); + final boolean hasOnBrowserEvent = hasFunction("onBrowserEvent"); + + return new ComplexRenderer<JsonValue>() { + @Override + public void render(RendererCellReference cell, JsonValue data) { + render(helper.getConnectorWrapper(), getJsCell(cell), + Util.json2jso(data)); + } + + private JavaScriptObject getJsCell(CellReference<?> cell) { + updateCellReference(cellReferenceWrapper, cell); + return cellReferenceWrapper; + } + + public native void render(JavaScriptObject wrapper, + JavaScriptObject cell, JavaScriptObject data) + /*-{ + wrapper.render(cell, data); + }-*/; + + @Override + public void init(RendererCellReference cell) { + if (hasInit) { + init(helper.getConnectorWrapper(), getJsCell(cell)); + } + } + + private native void init(JavaScriptObject wrapper, + JavaScriptObject cell) + /*-{ + wrapper.init(cell); + }-*/; + + private native void updateCellReference( + JavaScriptObject cellWrapper, CellReference<?> target) + /*-{ + cellWrapper.target = target; + }-*/; + + @Override + public void destroy(RendererCellReference cell) { + if (hasDestroy) { + destory(helper.getConnectorWrapper(), getJsCell(cell)); + } else { + super.destroy(cell); + } + } + + private native void destory(JavaScriptObject wrapper, + JavaScriptObject cell) + /*-{ + wrapper.destory(cell); + }-*/; + + @Override + public boolean onActivate(CellReference<?> cell) { + if (hasOnActivate) { + return onActivate(helper.getConnectorWrapper(), + getJsCell(cell)); + } else { + return super.onActivate(cell); + } + } + + private native boolean onActivate(JavaScriptObject wrapper, + JavaScriptObject cell) + /*-{ + return !!wrapper.onActivate(cell); + }-*/; + + @Override + public Collection<String> getConsumedEvents() { + if (hasGetConsumedEvents) { + JsArrayString events = getConsumedEvents(helper + .getConnectorWrapper()); + + ArrayList<String> list = new ArrayList<String>( + events.length()); + for (int i = 0; i < events.length(); i++) { + list.add(events.get(i)); + } + return list; + } else { + return super.getConsumedEvents(); + } + } + + private native JsArrayString getConsumedEvents( + JavaScriptObject wrapper) + /*-{ + var rawEvents = wrapper.getConsumedEvents(); + var events = []; + for(var i = 0; i < rawEvents.length; i++) { + events[i] = ""+rawEvents[i]; + } + return events; + }-*/; + + @Override + public boolean onBrowserEvent(CellReference<?> cell, + NativeEvent event) { + if (hasOnBrowserEvent) { + return onBrowserEvent(helper.getConnectorWrapper(), + getJsCell(cell), event); + } else { + return super.onBrowserEvent(cell, event); + } + } + + private native boolean onBrowserEvent(JavaScriptObject wrapper, + JavaScriptObject cell, NativeEvent event) + /*-{ + return !!wrapper.onBrowserEvent(cell, event); + }-*/; + }; + } + + @Override + public JsonValue decode(JsonValue value) { + // Let the js logic decode the raw json that the server sent + return value; + } + + @Override + public void onUnregister() { + super.onUnregister(); + helper.onUnregister(); + } + + @Override + public JavaScriptConnectorHelper getJavascriptConnectorHelper() { + return helper; + } +} diff --git a/client/src/com/vaadin/client/connectors/NumberRendererConnector.java b/client/src/com/vaadin/client/connectors/NumberRendererConnector.java new file mode 100644 index 0000000000..84b319710d --- /dev/null +++ b/client/src/com/vaadin/client/connectors/NumberRendererConnector.java @@ -0,0 +1,35 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.connectors; + +import com.vaadin.shared.ui.Connect; + +/** + * A connector for + * {@link com.vaadin.ui.components.grid.renderers.NumberRenderer NumberRenderer} + * . + * <p> + * The server-side Renderer operates on numbers, but the data is serialized as a + * string, and displayed as-is on the client side. This is to be able to support + * the server's locale. + * + * @since 7.4 + * @author Vaadin Ltd + */ +@Connect(com.vaadin.ui.renderer.NumberRenderer.class) +public class NumberRendererConnector extends TextRendererConnector { + // no implementation needed +} diff --git a/client/src/com/vaadin/client/connectors/ObjectRendererConnector.java b/client/src/com/vaadin/client/connectors/ObjectRendererConnector.java new file mode 100644 index 0000000000..877eaaa569 --- /dev/null +++ b/client/src/com/vaadin/client/connectors/ObjectRendererConnector.java @@ -0,0 +1,38 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.connectors; + +import com.vaadin.client.renderers.TextRenderer; +import com.vaadin.shared.ui.Connect; + +/** + * A connector for {@link com.vaadin.ui.renderer.ObjectRenderer the server side + * ObjectRenderer}. + * <p> + * This uses a {@link TextRenderer} to actually render the contents, as the + * object is already converted into a string server-side. + * + * @since + * @author Vaadin Ltd + */ +@Connect(com.vaadin.ui.renderer.ObjectRenderer.class) +public class ObjectRendererConnector extends AbstractRendererConnector<String> { + + @Override + public TextRenderer getRenderer() { + return (TextRenderer) super.getRenderer(); + } +} diff --git a/client/src/com/vaadin/client/connectors/ProgressBarRendererConnector.java b/client/src/com/vaadin/client/connectors/ProgressBarRendererConnector.java new file mode 100644 index 0000000000..fe410ccbe7 --- /dev/null +++ b/client/src/com/vaadin/client/connectors/ProgressBarRendererConnector.java @@ -0,0 +1,35 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.connectors; + +import com.vaadin.client.renderers.ProgressBarRenderer; +import com.vaadin.shared.ui.Connect; + +/** + * A connector for {@link ProgressBarRenderer}. + * + * @since 7.4 + * @author Vaadin Ltd + */ +@Connect(com.vaadin.ui.renderer.ProgressBarRenderer.class) +public class ProgressBarRendererConnector extends + AbstractRendererConnector<Double> { + + @Override + public ProgressBarRenderer getRenderer() { + return (ProgressBarRenderer) super.getRenderer(); + } +} diff --git a/client/src/com/vaadin/client/connectors/RpcDataSourceConnector.java b/client/src/com/vaadin/client/connectors/RpcDataSourceConnector.java new file mode 100644 index 0000000000..f8d6ebcb62 --- /dev/null +++ b/client/src/com/vaadin/client/connectors/RpcDataSourceConnector.java @@ -0,0 +1,183 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.connectors; + +import java.util.ArrayList; + +import com.vaadin.client.ServerConnector; +import com.vaadin.client.data.AbstractRemoteDataSource; +import com.vaadin.client.extensions.AbstractExtensionConnector; +import com.vaadin.shared.data.DataProviderRpc; +import com.vaadin.shared.data.DataRequestRpc; +import com.vaadin.shared.ui.Connect; +import com.vaadin.shared.ui.grid.GridState; +import com.vaadin.shared.ui.grid.Range; + +import elemental.json.Json; +import elemental.json.JsonArray; +import elemental.json.JsonObject; + +/** + * Connects a Vaadin server-side container data source to a Grid. This is + * currently implemented as an Extension hardcoded to support a specific + * connector type. This will be changed once framework support for something + * more flexible has been implemented. + * + * @since 7.4 + * @author Vaadin Ltd + */ +@Connect(com.vaadin.data.RpcDataProviderExtension.class) +public class RpcDataSourceConnector extends AbstractExtensionConnector { + + public class RpcDataSource extends AbstractRemoteDataSource<JsonObject> { + + protected RpcDataSource() { + registerRpc(DataProviderRpc.class, new DataProviderRpc() { + @Override + public void setRowData(int firstRow, JsonArray rowArray) { + ArrayList<JsonObject> rows = new ArrayList<JsonObject>( + rowArray.length()); + for (int i = 0; i < rowArray.length(); i++) { + JsonObject rowObject = rowArray.getObject(i); + rows.add(rowObject); + } + + dataSource.setRowData(firstRow, rows); + } + + @Override + public void removeRowData(int firstRow, int count) { + dataSource.removeRowData(firstRow, count); + } + + @Override + public void insertRowData(int firstRow, int count) { + dataSource.insertRowData(firstRow, count); + } + + @Override + public void resetDataAndSize(int size) { + dataSource.resetDataAndSize(size); + } + }); + } + + private DataRequestRpc rpcProxy = getRpcProxy(DataRequestRpc.class); + + @Override + protected void requestRows(int firstRowIndex, int numberOfRows, + RequestRowsCallback<JsonObject> callback) { + /* + * If you're looking at this code because you want to learn how to + * use AbstactRemoteDataSource, please look somewhere else instead. + * + * We're not doing things in the conventional way with the callback + * here since Vaadin doesn't directly support RPC with return + * values. We're instead asking the server to push us some data, and + * when we receive pushed data, we just push it along to the + * underlying cache in the same way no matter if it was a genuine + * push or just a result of us requesting rows. + */ + + Range cached = getCachedRange(); + + rpcProxy.requestRows(firstRowIndex, numberOfRows, + cached.getStart(), cached.length()); + + /* + * Show the progress indicator if there is a pending data request + * and some of the visible rows are being requested. The RPC in + * itself will not trigger the indicator since it might just fetch + * some rows in the background to fill the cache. + * + * The indicator will be hidden by the framework when the response + * is received (unless another request is already on its way at that + * point). + */ + if (getRequestedAvailability().intersects( + Range.withLength(firstRowIndex, numberOfRows))) { + getConnection().getLoadingIndicator().ensureTriggered(); + } + } + + @Override + public void ensureAvailability(int firstRowIndex, int numberOfRows) { + super.ensureAvailability(firstRowIndex, numberOfRows); + + /* + * We trigger the indicator already at this point since the actual + * RPC will not be sent right away when waiting for the response to + * a previous request. + * + * Only triggering here would not be enough since the check that + * sets isWaitingForData is deferred. We don't want to trigger the + * loading indicator here if we don't know that there is actually a + * request going on since some other bug might then cause the + * loading indicator to not be hidden. + */ + if (isWaitingForData() + && !Range.withLength(firstRowIndex, numberOfRows) + .isSubsetOf(getCachedRange())) { + getConnection().getLoadingIndicator().ensureTriggered(); + } + } + + @Override + public String getRowKey(JsonObject row) { + if (row.hasKey(GridState.JSONKEY_ROWKEY)) { + return row.getString(GridState.JSONKEY_ROWKEY); + } else { + return null; + } + } + + public RowHandle<JsonObject> getHandleByKey(Object key) { + JsonObject row = Json.createObject(); + row.put(GridState.JSONKEY_ROWKEY, (String) key); + return new RowHandleImpl(row, key); + } + + @Override + protected void pinHandle(RowHandleImpl handle) { + // Server only knows if something is pinned or not. No need to pin + // multiple times. + boolean pinnedBefore = handle.isPinned(); + super.pinHandle(handle); + if (!pinnedBefore) { + rpcProxy.setPinned(getRowKey(handle.getRow()), true); + } + } + + @Override + protected void unpinHandle(RowHandleImpl handle) { + // Row data is no longer available after it has been unpinned. + String key = getRowKey(handle.getRow()); + super.unpinHandle(handle); + if (!handle.isPinned()) { + rpcProxy.setPinned(key, false); + } + + } + } + + private final RpcDataSource dataSource = new RpcDataSource(); + + @Override + protected void extend(ServerConnector target) { + ((GridConnector) target).setDataSource(dataSource); + } +} diff --git a/client/src/com/vaadin/client/connectors/TextRendererConnector.java b/client/src/com/vaadin/client/connectors/TextRendererConnector.java new file mode 100644 index 0000000000..3059d3f8bb --- /dev/null +++ b/client/src/com/vaadin/client/connectors/TextRendererConnector.java @@ -0,0 +1,34 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.connectors; + +import com.vaadin.client.renderers.TextRenderer; +import com.vaadin.shared.ui.Connect; + +/** + * A connector for {@link TextRenderer}. + * + * @since 7.4 + * @author Vaadin Ltd + */ +@Connect(com.vaadin.ui.renderer.TextRenderer.class) +public class TextRendererConnector extends AbstractRendererConnector<String> { + + @Override + public TextRenderer getRenderer() { + return (TextRenderer) super.getRenderer(); + } +} diff --git a/client/src/com/vaadin/client/connectors/UnsafeHtmlRendererConnector.java b/client/src/com/vaadin/client/connectors/UnsafeHtmlRendererConnector.java new file mode 100644 index 0000000000..420f18427d --- /dev/null +++ b/client/src/com/vaadin/client/connectors/UnsafeHtmlRendererConnector.java @@ -0,0 +1,43 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.connectors; + +import com.vaadin.client.renderers.Renderer; +import com.vaadin.client.widget.grid.RendererCellReference; +import com.vaadin.shared.ui.Connect; + +/** + * A connector for {@link UnsafeHtmlRenderer} + * + * @since 7.4 + * @author Vaadin Ltd + */ +@Connect(com.vaadin.ui.renderer.HtmlRenderer.class) +public class UnsafeHtmlRendererConnector extends + AbstractRendererConnector<String> { + + public static class UnsafeHtmlRenderer implements Renderer<String> { + @Override + public void render(RendererCellReference cell, String data) { + cell.getElement().setInnerHTML(data); + } + } + + @Override + public UnsafeHtmlRenderer getRenderer() { + return (UnsafeHtmlRenderer) super.getRenderer(); + } +} diff --git a/client/src/com/vaadin/client/data/AbstractRemoteDataSource.java b/client/src/com/vaadin/client/data/AbstractRemoteDataSource.java new file mode 100644 index 0000000000..0ad1631e19 --- /dev/null +++ b/client/src/com/vaadin/client/data/AbstractRemoteDataSource.java @@ -0,0 +1,714 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.data; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.google.gwt.core.client.Duration; +import com.google.gwt.core.client.Scheduler; +import com.google.gwt.core.client.Scheduler.ScheduledCommand; +import com.vaadin.client.Profiler; +import com.vaadin.shared.ui.grid.Range; + +/** + * Base implementation for data sources that fetch data from a remote system. + * This class takes care of caching data and communicating with the data source + * user. An implementation of this class should override + * {@link #requestRows(int, int, RequestRowsCallback)} to trigger asynchronously + * loading of data and then pass the loaded data into the provided callback. + * + * @since 7.4 + * @author Vaadin Ltd + * @param <T> + * the row type + */ +public abstract class AbstractRemoteDataSource<T> implements DataSource<T> { + + /** + * Callback used by + * {@link AbstractRemoteDataSource#requestRows(int, int, RequestRowsCallback)} + * to pass data to the underlying implementation when data has been fetched. + */ + public static class RequestRowsCallback<T> { + private final Range requestedRange; + private final double requestStart; + private final AbstractRemoteDataSource<T> source; + + /** + * Creates a new callback + * + * @param source + * the data source for which the request is made + * @param requestedRange + * the requested row range + */ + protected RequestRowsCallback(AbstractRemoteDataSource<T> source, + Range requestedRange) { + this.source = source; + this.requestedRange = requestedRange; + + requestStart = Duration.currentTimeMillis(); + } + + /** + * Called by the + * {@link AbstractRemoteDataSource#requestRows(int, int, RequestRowsCallback)} + * implementation when data has been received. + * + * @param rowData + * a list of row objects starting at the requested offset + * @param totalSize + * the total number of rows available at the remote end + */ + public void onResponse(List<T> rowData, int totalSize) { + if (source.size != totalSize) { + source.resetDataAndSize(totalSize); + } + source.setRowData(requestedRange.getStart(), rowData); + } + + /** + * Gets the range of rows that was requested. + * + * @return the requsted row range + */ + public Range getRequestedRange() { + return requestedRange; + } + + } + + protected class RowHandleImpl extends RowHandle<T> { + private T row; + private final Object key; + + public RowHandleImpl(final T row, final Object key) { + this.row = row; + this.key = key; + } + + /** + * A method for the data source to update the row data. + * + * @param row + * the updated row object + */ + public void setRow(final T row) { + this.row = row; + assert getRowKey(row).equals(key) : "The old key does not " + + "equal the new key for the given row (old: " + key + + ", new :" + getRowKey(row) + ")"; + } + + @Override + public T getRow() throws IllegalStateException { + if (isPinned()) { + return row; + } else { + throw new IllegalStateException("The row handle for key " + key + + " was not pinned"); + } + } + + public boolean isPinned() { + return pinnedRows.containsKey(key); + } + + @Override + public void pin() { + pinHandle(this); + } + + @Override + public void unpin() throws IllegalStateException { + unpinHandle(this); + } + + @Override + protected boolean equalsExplicit(final Object obj) { + if (obj instanceof AbstractRemoteDataSource.RowHandleImpl) { + /* + * Java prefers AbstractRemoteDataSource<?>.RowHandleImpl. I + * like the @SuppressWarnings more (keeps the line length in + * check.) + */ + @SuppressWarnings("unchecked") + final RowHandleImpl rhi = (RowHandleImpl) obj; + return key.equals(rhi.key); + } else { + return false; + } + } + + @Override + protected int hashCodeExplicit() { + return key.hashCode(); + } + + @Override + public void updateRow() { + int index = indexOf(row); + if (index >= 0) { + dataChangeHandler.dataUpdated(index, 1); + } + } + } + + private RequestRowsCallback<T> currentRequestCallback; + + private boolean coverageCheckPending = false; + + private Range requestedAvailability = Range.between(0, 0); + + private Range cached = Range.between(0, 0); + + private final HashMap<Integer, T> indexToRowMap = new HashMap<Integer, T>(); + private final HashMap<Object, Integer> keyToIndexMap = new HashMap<Object, Integer>(); + + private DataChangeHandler dataChangeHandler; + + private CacheStrategy cacheStrategy = new CacheStrategy.DefaultCacheStrategy(); + + private final ScheduledCommand coverageChecker = new ScheduledCommand() { + @Override + public void execute() { + coverageCheckPending = false; + checkCacheCoverage(); + } + }; + + private Map<Object, Integer> pinnedCounts = new HashMap<Object, Integer>(); + private Map<Object, RowHandleImpl> pinnedRows = new HashMap<Object, RowHandleImpl>(); + protected Collection<T> temporarilyPinnedRows = Collections.emptySet(); + + // Size not yet known + private int size = -1; + + private void ensureCoverageCheck() { + if (!coverageCheckPending) { + coverageCheckPending = true; + Scheduler.get().scheduleDeferred(coverageChecker); + } + } + + /** + * Pins a row with given handle. This function can be overridden to do + * specific logic related to pinning rows. + * + * @param handle + * row handle to pin + */ + protected void pinHandle(RowHandleImpl handle) { + Object key = handle.key; + Integer count = pinnedCounts.get(key); + if (count == null) { + count = Integer.valueOf(0); + pinnedRows.put(key, handle); + } + pinnedCounts.put(key, Integer.valueOf(count.intValue() + 1)); + } + + /** + * Unpins a previously pinned row with given handle. This function can be + * overridden to do specific logic related to unpinning rows. + * + * @param handle + * row handle to unpin + * + * @throws IllegalStateException + * if given row handle has not been pinned before + */ + protected void unpinHandle(RowHandleImpl handle) + throws IllegalStateException { + Object key = handle.key; + final Integer count = pinnedCounts.get(key); + if (count == null) { + throw new IllegalStateException("Row " + handle.getRow() + + " with key " + key + " was not pinned to begin with"); + } else if (count.equals(Integer.valueOf(1))) { + pinnedRows.remove(key); + pinnedCounts.remove(key); + } else { + pinnedCounts.put(key, Integer.valueOf(count.intValue() - 1)); + } + } + + @Override + public void ensureAvailability(int firstRowIndex, int numberOfRows) { + requestedAvailability = Range.withLength(firstRowIndex, numberOfRows); + + /* + * Don't request any data right away since the data might be included in + * a message that has been received but not yet fully processed. + */ + ensureCoverageCheck(); + } + + /** + * Gets the row index range that was requested by the previous call to + * {@link #ensureAvailability(int, int)}. + * + * @return the requested availability range + */ + public Range getRequestedAvailability() { + return requestedAvailability; + } + + private void checkCacheCoverage() { + if (isWaitingForData()) { + // Anyone clearing the waiting status should run this method again + return; + } + + Profiler.enter("AbstractRemoteDataSource.checkCacheCoverage"); + + Range minCacheRange = getMinCacheRange(); + + if (!minCacheRange.intersects(cached) || cached.isEmpty()) { + /* + * Simple case: no overlap between cached data and needed data. + * Clear the cache and request new data + */ + indexToRowMap.clear(); + keyToIndexMap.clear(); + cached = Range.between(0, 0); + + handleMissingRows(getMaxCacheRange()); + } else { + discardStaleCacheEntries(); + + // Might need more rows -> request them + if (!minCacheRange.isSubsetOf(cached)) { + Range[] missingCachePartition = getMaxCacheRange() + .partitionWith(cached); + handleMissingRows(missingCachePartition[0]); + handleMissingRows(missingCachePartition[2]); + } else { + dataChangeHandler.dataAvailable(cached.getStart(), + cached.length()); + } + } + + Profiler.leave("AbstractRemoteDataSource.checkCacheCoverage"); + } + + /** + * Checks whether this data source is currently waiting for more rows to + * become available. + * + * @return <code>true</code> if waiting for data; otherwise + * <code>false</code> + */ + public boolean isWaitingForData() { + return currentRequestCallback != null; + } + + private void discardStaleCacheEntries() { + Range[] cacheParition = cached.partitionWith(getMaxCacheRange()); + dropFromCache(cacheParition[0]); + cached = cacheParition[1]; + dropFromCache(cacheParition[2]); + } + + private void dropFromCache(Range range) { + for (int i = range.getStart(); i < range.getEnd(); i++) { + T removed = indexToRowMap.remove(Integer.valueOf(i)); + keyToIndexMap.remove(getRowKey(removed)); + } + } + + private void handleMissingRows(Range range) { + if (range.isEmpty()) { + return; + } + currentRequestCallback = new RequestRowsCallback<T>(this, range); + requestRows(range.getStart(), range.length(), currentRequestCallback); + } + + /** + * Triggers fetching rows from the remote data source. The provided callback + * should be informed when the requested rows have been received. + * + * @param firstRowIndex + * the index of the first row to fetch + * @param numberOfRows + * the number of rows to fetch + * @param callback + * callback to inform when the requested rows are available + */ + protected abstract void requestRows(int firstRowIndex, int numberOfRows, + RequestRowsCallback<T> callback); + + @Override + public T getRow(int rowIndex) { + return indexToRowMap.get(Integer.valueOf(rowIndex)); + } + + @Override + public int indexOf(T row) { + Object key = getRowKey(row); + if (keyToIndexMap.containsKey(key)) { + return keyToIndexMap.get(key); + } + return -1; + } + + @Override + public void setDataChangeHandler(DataChangeHandler dataChangeHandler) { + this.dataChangeHandler = dataChangeHandler; + + if (dataChangeHandler != null && !cached.isEmpty()) { + // Push currently cached data to the implementation + dataChangeHandler.dataUpdated(cached.getStart(), cached.length()); + dataChangeHandler.dataAvailable(cached.getStart(), cached.length()); + } + } + + /** + * Informs this data source that updated data has been sent from the server. + * + * @param firstRowIndex + * the index of the first received row + * @param rowData + * a list of rows, starting from <code>firstRowIndex</code> + */ + protected void setRowData(int firstRowIndex, List<T> rowData) { + + assert firstRowIndex + rowData.size() <= size(); + + Profiler.enter("AbstractRemoteDataSource.setRowData"); + + Range received = Range.withLength(firstRowIndex, rowData.size()); + + if (isWaitingForData()) { + cacheStrategy.onDataArrive(Duration.currentTimeMillis() + - currentRequestCallback.requestStart, received.length()); + + currentRequestCallback = null; + } + + Range maxCacheRange = getMaxCacheRange(); + + Range[] partition = received.partitionWith(maxCacheRange); + + Range newUsefulData = partition[1]; + if (!newUsefulData.isEmpty()) { + // Update the parts that are actually inside + for (int i = newUsefulData.getStart(); i < newUsefulData.getEnd(); i++) { + final T row = rowData.get(i - firstRowIndex); + indexToRowMap.put(Integer.valueOf(i), row); + keyToIndexMap.put(getRowKey(row), Integer.valueOf(i)); + } + + if (dataChangeHandler != null) { + Profiler.enter("AbstractRemoteDataSource.setRowData notify dataChangeHandler"); + dataChangeHandler.dataUpdated(newUsefulData.getStart(), + newUsefulData.length()); + Profiler.leave("AbstractRemoteDataSource.setRowData notify dataChangeHandler"); + } + + // Potentially extend the range + if (cached.isEmpty()) { + cached = newUsefulData; + } else { + discardStaleCacheEntries(); + + /* + * everything might've become stale so we need to re-check for + * emptiness. + */ + if (!cached.isEmpty()) { + cached = cached.combineWith(newUsefulData); + } else { + cached = newUsefulData; + } + } + dataChangeHandler.dataAvailable(cached.getStart(), cached.length()); + + updatePinnedRows(rowData); + } + + if (!partition[0].isEmpty() || !partition[2].isEmpty()) { + /* + * FIXME + * + * Got data that we might need in a moment if the container is + * updated before the widget settings. Support for this will be + * implemented later on. + */ + } + + // Eventually check whether all needed rows are now available + ensureCoverageCheck(); + + Profiler.leave("AbstractRemoteDataSource.setRowData"); + } + + private void updatePinnedRows(final List<T> rowData) { + for (final T row : rowData) { + final Object key = getRowKey(row); + final RowHandleImpl handle = pinnedRows.get(key); + if (handle != null) { + handle.setRow(row); + } + } + } + + /** + * Informs this data source that the server has removed data. + * + * @param firstRowIndex + * the index of the first removed row + * @param count + * the number of removed rows, starting from + * <code>firstRowIndex</code> + */ + protected void removeRowData(int firstRowIndex, int count) { + Profiler.enter("AbstractRemoteDataSource.removeRowData"); + + size -= count; + + // shift indices to fill the cache correctly + int firstMoved = Math.max(firstRowIndex + count, cached.getStart()); + for (int i = firstMoved; i < cached.getEnd(); i++) { + moveRowFromIndexToIndex(i, i - count); + } + + Range removedRange = Range.withLength(firstRowIndex, count); + if (cached.isSubsetOf(removedRange)) { + // Whole cache is part of the removal. Empty cache + cached = Range.withLength(0, 0); + } else if (removedRange.intersects(cached)) { + // Removal and cache share some indices. fix accordingly. + Range[] partitions = cached.partitionWith(removedRange); + Range remainsBefore = partitions[0]; + Range transposedRemainsAfter = partitions[2].offsetBy(-removedRange + .length()); + cached = remainsBefore.combineWith(transposedRemainsAfter); + } else if (removedRange.getEnd() <= cached.getStart()) { + // Removal was before the cache. offset the cache. + cached = cached.offsetBy(-removedRange.length()); + } + + assertDataChangeHandlerIsInjected(); + dataChangeHandler.dataRemoved(firstRowIndex, count); + ensureCoverageCheck(); + + Profiler.leave("AbstractRemoteDataSource.removeRowData"); + } + + /** + * Informs this data source that new data has been inserted from the server. + * + * @param firstRowIndex + * the destination index of the new row data + * @param count + * the number of rows inserted + */ + protected void insertRowData(int firstRowIndex, int count) { + Profiler.enter("AbstractRemoteDataSource.insertRowData"); + + size += count; + + if (firstRowIndex <= cached.getStart()) { + Range oldCached = cached; + cached = cached.offsetBy(count); + + for (int i = 1; i <= indexToRowMap.size(); i++) { + int oldIndex = oldCached.getEnd() - i; + int newIndex = cached.getEnd() - i; + moveRowFromIndexToIndex(oldIndex, newIndex); + } + } else if (cached.contains(firstRowIndex)) { + int oldCacheEnd = cached.getEnd(); + /* + * We need to invalidate the cache from the inserted row onwards, + * since the cache wants to be a contiguous range. It doesn't + * support holes. + * + * If holes were supported, we could shift the higher part of + * "cached" and leave a hole the size of "count" in the middle. + */ + cached = cached.splitAt(firstRowIndex)[0]; + + for (int i = firstRowIndex; i < oldCacheEnd; i++) { + T row = indexToRowMap.remove(Integer.valueOf(i)); + keyToIndexMap.remove(getRowKey(row)); + } + } + assertDataChangeHandlerIsInjected(); + dataChangeHandler.dataAdded(firstRowIndex, count); + ensureCoverageCheck(); + + Profiler.leave("AbstractRemoteDataSource.insertRowData"); + } + + private void moveRowFromIndexToIndex(int oldIndex, int newIndex) { + T row = indexToRowMap.remove(oldIndex); + if (indexToRowMap.containsKey(newIndex)) { + // Old row is about to be overwritten. Remove it from keyCache. + T row2 = indexToRowMap.remove(newIndex); + if (row2 != null) { + keyToIndexMap.remove(getRowKey(row2)); + } + } + indexToRowMap.put(newIndex, row); + if (row != null) { + keyToIndexMap.put(getRowKey(row), newIndex); + } + } + + /** + * Gets the current range of cached rows + * + * @return the range of currently cached rows + */ + public Range getCachedRange() { + return cached; + } + + /** + * Sets the cache strategy that is used to determine how much data is + * fetched and cached. + * <p> + * The new strategy is immediately used to evaluate whether currently cached + * rows should be discarded or new rows should be fetched. + * + * @param cacheStrategy + * a cache strategy implementation, not <code>null</code> + */ + public void setCacheStrategy(CacheStrategy cacheStrategy) { + if (cacheStrategy == null) { + throw new IllegalArgumentException(); + } + + if (this.cacheStrategy != cacheStrategy) { + this.cacheStrategy = cacheStrategy; + + checkCacheCoverage(); + } + } + + private Range getMinCacheRange() { + Range availableDataRange = getAvailableRangeForCache(); + + Range minCacheRange = cacheStrategy.getMinCacheRange( + requestedAvailability, cached, availableDataRange); + + assert minCacheRange.isSubsetOf(availableDataRange); + + return minCacheRange; + } + + private Range getMaxCacheRange() { + Range availableDataRange = getAvailableRangeForCache(); + Range maxCacheRange = cacheStrategy.getMaxCacheRange( + requestedAvailability, cached, availableDataRange); + + assert maxCacheRange.isSubsetOf(availableDataRange); + + return maxCacheRange; + } + + private Range getAvailableRangeForCache() { + int upperBound = size(); + if (upperBound == -1) { + upperBound = requestedAvailability.length(); + } + return Range.withLength(0, upperBound); + } + + @Override + public RowHandle<T> getHandle(T row) throws IllegalStateException { + Object key = getRowKey(row); + + if (key == null) { + throw new NullPointerException("key may not be null (row: " + row + + ")"); + } + + if (pinnedRows.containsKey(key)) { + return pinnedRows.get(key); + } else if (keyToIndexMap.containsKey(key)) { + return new RowHandleImpl(row, key); + } else { + throw new IllegalStateException("The cache of this DataSource " + + "does not currently contain the row " + row); + } + } + + /** + * Gets a stable key for the row object. + * <p> + * This method is a workaround for the fact that there is no means to force + * proper implementations for {@link #hashCode()} and + * {@link #equals(Object)} methods. + * <p> + * Since the same row object will be created several times for the same + * logical data, the DataSource needs a mechanism to be able to compare two + * objects, and figure out whether or not they represent the same data. Even + * if all the fields of an entity would be changed, it still could represent + * the very same thing (say, a person changes all of her names.) + * <p> + * A very usual and simple example what this could be, is an unique ID for + * this object that would also be stored in a database. + * + * @param row + * the row object for which to get the key + * @return a non-null object that uniquely and consistently represents the + * row object + */ + abstract public Object getRowKey(T row); + + @Override + public int size() { + return size; + } + + /** + * Updates the size, discarding all cached data. This method is used when + * the size of the container is changed without any information about the + * structure of the change. In this case, all cached data is discarded to + * avoid cache offset issues. + * <p> + * If you have information about the structure of the change, use + * {@link #insertRowData(int, int)} or {@link #removeRowData(int, int)} to + * indicate where the inserted or removed rows are located. + * + * @param newSize + * the new size of the container + */ + protected void resetDataAndSize(int newSize) { + size = newSize; + dropFromCache(getCachedRange()); + cached = Range.withLength(0, 0); + assertDataChangeHandlerIsInjected(); + dataChangeHandler.resetDataAndSize(newSize); + } + + private void assertDataChangeHandlerIsInjected() { + assert dataChangeHandler != null : "The dataChangeHandler was " + + "called before it was injected. Maybe you tried " + + "to manipulate the data in the DataSource's " + + "constructor instead of in overriding onAttach() " + + "and doing it there?"; + } +} diff --git a/client/src/com/vaadin/client/data/CacheStrategy.java b/client/src/com/vaadin/client/data/CacheStrategy.java new file mode 100644 index 0000000000..79ce537314 --- /dev/null +++ b/client/src/com/vaadin/client/data/CacheStrategy.java @@ -0,0 +1,183 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.data; + +import com.vaadin.shared.ui.grid.Range; + +/** + * Determines what data an {@link AbstractRemoteDataSource} should fetch and + * keep cached. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface CacheStrategy { + /** + * A helper class for creating a simple symmetric cache strategy that uses + * the same logic for both rows before and after the currently cached range. + * <p> + * This simple approach rules out more advanced heuristics that would take + * the current scrolling direction or past scrolling behavior into account. + */ + public static abstract class AbstractBasicSymmetricalCacheStrategy + implements CacheStrategy { + + @Override + public void onDataArrive(double roundTripTime, int rowCount) { + // NOP + } + + @Override + public Range getMinCacheRange(Range displayedRange, Range cachedRange, + Range estimatedAvailableRange) { + int cacheSize = getMinimumCacheSize(displayedRange.length()); + + return displayedRange.expand(cacheSize, cacheSize).restrictTo( + estimatedAvailableRange); + } + + @Override + public Range getMaxCacheRange(Range displayedRange, Range cachedRange, + Range estimatedAvailableRange) { + int cacheSize = getMaximumCacheSize(displayedRange.length()); + + return displayedRange.expand(cacheSize, cacheSize).restrictTo( + estimatedAvailableRange); + } + + /** + * Gets the maximum number of extra items to cache in one direction. + * + * @param pageSize + * the current number of items used at once + * @return maximum of items to cache + */ + public abstract int getMaximumCacheSize(int pageSize); + + /** + * Gets the the minimum number of extra items to cache in one direction. + * + * @param pageSize + * the current number of items used at once + * @return minimum number of items to cache + */ + public abstract int getMinimumCacheSize(int pageSize); + } + + /** + * The default cache strategy used by {@link AbstractRemoteDataSource}, + * using multiples of the page size for determining the minimum and maximum + * number of items to keep in the cache. By default, at least three times + * the page size both before and after the currently used range are kept in + * the cache and items are discarded if there's yet another page size worth + * of items cached in either direction. + */ + public static class DefaultCacheStrategy extends + AbstractBasicSymmetricalCacheStrategy { + private final int minimumRatio; + private final int maximumRatio; + + /** + * Creates a DefaultCacheStrategy keeping between 3 and 4 pages worth of + * data cached both before and after the active range. + */ + public DefaultCacheStrategy() { + this(3, 4); + } + + /** + * Creates a DefaultCacheStrategy with custom ratios for how much data + * to cache. The ratios denote how many multiples of the currently used + * page size are kept in the cache in each direction. + * + * @param minimumRatio + * the minimum number of pages to keep in the cache in each + * direction + * @param maximumRatio + * the maximum number of pages to keep in the cache in each + * direction + */ + public DefaultCacheStrategy(int minimumRatio, int maximumRatio) { + this.minimumRatio = minimumRatio; + this.maximumRatio = maximumRatio; + } + + @Override + public int getMinimumCacheSize(int pageSize) { + return pageSize * minimumRatio; + } + + @Override + public int getMaximumCacheSize(int pageSize) { + return pageSize * maximumRatio; + } + } + + /** + * Called whenever data requested by the data source has arrived. This + * information can e.g. be used for measuring how long it takes to fetch + * different number of rows from the server. + * <p> + * A cache strategy implementation cannot use this information to keep track + * of which items are in the cache since the data source might discard items + * without notifying the cache strategy. + * + * @param roundTripTime + * the total number of milliseconds elapsed from requesting the + * data until the response was passed to the data source + * @param rowCount + * the number of received rows + */ + public void onDataArrive(double roundTripTime, int rowCount); + + /** + * Gets the minimum row range that should be cached. The data source will + * fetch new data if the currently cached range does not fill the entire + * minimum cache range. + * + * @param displayedRange + * the range of currently displayed rows + * @param cachedRange + * the range of currently cached rows + * @param estimatedAvailableRange + * the estimated range of rows available for the data source + * + * @return the minimum range of rows that should be cached, should at least + * include the displayed range and should not exceed the total + * estimated available range + */ + public Range getMinCacheRange(Range displayedRange, Range cachedRange, + Range estimatedAvailableRange); + + /** + * Gets the maximum row range that should be cached. The data source will + * discard cached rows that are outside the maximum range. + * + * @param displayedRange + * the range of currently displayed rows + * @param cachedRange + * the range of currently cached rows + * @param estimatedAvailableRange + * the estimated range of rows available for the data source + * + * @return the maximum range of rows that should be cached, should at least + * include the displayed range and should not exceed the total + * estimated available range + */ + public Range getMaxCacheRange(Range displayedRange, Range cachedRange, + Range estimatedAvailableRange); +} diff --git a/client/src/com/vaadin/client/data/DataChangeHandler.java b/client/src/com/vaadin/client/data/DataChangeHandler.java new file mode 100644 index 0000000000..35f1eafea9 --- /dev/null +++ b/client/src/com/vaadin/client/data/DataChangeHandler.java @@ -0,0 +1,82 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.data; + +/** + * Callback interface used by {@link DataSource} to inform its user about + * updates to the data. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface DataChangeHandler { + /** + * Called when the contents of the data source has changed. If the number of + * rows has changed or if rows have been moved around, + * {@link #dataAdded(int, int)} or {@link #dataRemoved(int, int)} should + * ideally be used instead. + * + * @param firstRowIndex + * the index of the first changed row + * @param numberOfRows + * the number of changed rows + */ + public void dataUpdated(int firstRowIndex, int numberOfRows); + + /** + * Called when rows have been removed from the data source. + * + * @param firstRowIndex + * the index that the first removed row had prior to removal + * @param numberOfRows + * the number of removed rows + */ + public void dataRemoved(int firstRowIndex, int numberOfRows); + + /** + * Called when the new rows have been added to the container. + * + * @param firstRowIndex + * the index of the first added row + * @param numberOfRows + * the number of added rows + */ + public void dataAdded(int firstRowIndex, int numberOfRows); + + /** + * Called when rows requested with + * {@link DataSource#ensureAvailability(int, int)} rows are available. + * + * @param firstRowIndex + * the index of the first available row + * @param numberOfRows + * the number of available rows + */ + public void dataAvailable(int firstRowIndex, int numberOfRows); + + /** + * Resets all data and defines a new size for the data. + * <p> + * This should be used in the cases where the data has changed in some + * unverifiable way. I.e. "something happened". This will lead to a + * re-rendering of the current Grid viewport + * + * @param estimatedNewDataSize + * the estimated size of the new data set + */ + public void resetDataAndSize(int estimatedNewDataSize); +} diff --git a/client/src/com/vaadin/client/data/DataSource.java b/client/src/com/vaadin/client/data/DataSource.java new file mode 100644 index 0000000000..076226bf5c --- /dev/null +++ b/client/src/com/vaadin/client/data/DataSource.java @@ -0,0 +1,209 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.data; + +/** + * Source of data for widgets showing lazily loaded data based on indexable + * items (e.g. rows) of a specified type. The data source is a lazy view into a + * larger data set. + * + * @since 7.4 + * @author Vaadin Ltd + * @param <T> + * the row type + */ +public interface DataSource<T> { + + /** + * A handle that contains information on whether a row should be + * {@link #pin() pinned} or {@link #unpin() unpinned}, and also always the + * most recent representation for that particular row. + * + * @param <T> + * the row type + */ + public abstract class RowHandle<T> { + /** + * Gets the most recent representation for the row this handle + * represents. + * + * @return the most recent representation for the row this handle + * represents + * @throws IllegalStateException + * if this row handle isn't currently pinned + * @see #pin() + */ + public abstract T getRow() throws IllegalStateException; + + /** + * Marks this row as pinned. + * <p> + * <em>Note:</em> Pinning a row multiple times requires an equal amount + * of unpins to free the row from the "pinned" status. + * <p> + * <em>Technical Note:</em> Pinning a row makes sure that the row object + * for a particular set of data is always kept as up to date as the data + * source is able to. Since the DataSource might create a new instance + * of an object, object references aren't necessarily kept up-to-date. + * This is a technical work-around for that. + * + * @see #unpin() + */ + public abstract void pin(); + + /** + * Marks this row as unpinned. + * <p> + * <em>Note:</em> Pinning a row multiple times requires an equal amount + * of unpins to free the row from the "pinned" status. + * <p> + * <em>Technical Note:</em> Pinning a row makes sure that the row object + * for a particular set of data is always kept as up to date as the data + * source is able to. Since the DataSource might create a new instance + * of an object, object references aren't necessarily kept up-to-date. + * This is a technical work-around for that. + * + * @throws IllegalStateException + * if this row handle has not been pinned before + * @see #pin() + */ + public abstract void unpin() throws IllegalStateException; + + /** + * Informs the DataSource that the row data represented by this + * RowHandle has been updated. DataChangeHandler for the DataSource + * should be informed that parts of data have been updated. + * + * @see DataChangeHandler#dataUpdated(int, int) + */ + public abstract void updateRow(); + + /** + * An explicit override for {@link Object#equals(Object)}. This method + * should be functionally equivalent to a properly implemented equals + * method. + * <p> + * Having a properly implemented equals method is imperative for + * RowHandle to function. Because Java has no mechanism to force an + * override of an existing method, we're defining a new method for that + * instead. + * + * @param rowHandle + * the reference object with which to compare + * @return {@code true} if this object is the same as the obj argument; + * {@code false} otherwise. + */ + protected abstract boolean equalsExplicit(Object obj); + + /** + * An explicit override for {@link Object#hashCode()}. This method + * should be functionally equivalent to a properly implemented hashCode + * method. + * <p> + * Having a properly implemented hashCode method is imperative for + * RowHandle to function. Because Java has no mechanism to force an + * override of an existing method, we're defining a new method for that + * instead. + * + * @return a hash code value for this object + */ + protected abstract int hashCodeExplicit(); + + @Override + public int hashCode() { + return hashCodeExplicit(); + } + + @Override + public boolean equals(Object obj) { + return equalsExplicit(obj); + } + } + + /** + * Informs the data source that data for the given range is needed. A data + * source only has one active region at a time, so calling this method + * discards the previously set range. + * <p> + * This method triggers lazy loading of data if necessary. The change + * handler registered using {@link #setDataChangeHandler(DataChangeHandler)} + * is informed when new data has been loaded. + * <p> + * After any possible lazy loading and updates are done, the change handler + * is informed that new data is available. + * + * @param firstRowIndex + * the index of the first needed row + * @param numberOfRows + * the number of needed rows + */ + public void ensureAvailability(int firstRowIndex, int numberOfRows); + + /** + * Retrieves the data for the row at the given index. If the row data is not + * available, returns <code>null</code>. + * <p> + * This method does not trigger loading of unavailable data. + * {@link #ensureAvailability(int, int)} should be used to signal what data + * will be needed. + * + * @param rowIndex + * the index of the row to retrieve data for + * @return data for the row; or <code>null</code> if no data is available + */ + public T getRow(int rowIndex); + + /** + * Returns the number of rows in the data source. + * + * @return the current size of the data source + */ + public int size(); + + /** + * Sets a data change handler to inform when data is updated, added or + * removed. + * + * @param dataChangeHandler + * the data change handler + */ + public void setDataChangeHandler(DataChangeHandler dataChangeHandler); + + /** + * Gets a {@link RowHandle} of a row object in the cache. + * + * @param row + * the row object for which to retrieve a row handle + * @return a non-<code>null</code> row handle of the given row object + * @throw IllegalStateException if this data source cannot be sure whether + * or not the given row exists. <em>In practice</em> this usually + * means that the row is not currently in this data source's cache. + */ + public RowHandle<T> getHandle(T row); + + /** + * Retrieves the index for given row object. + * <p> + * <em>Note:</em> This method does not verify that the given row object + * exists at all in this DataSource. + * + * @param row + * the row object + * @return index of the row; or <code>-1</code> if row is not available + */ + int indexOf(T row); +} diff --git a/client/src/com/vaadin/client/debug/internal/AnalyzeLayoutsPanel.java b/client/src/com/vaadin/client/debug/internal/AnalyzeLayoutsPanel.java index 1238d88345..fc9f3856b5 100644 --- a/client/src/com/vaadin/client/debug/internal/AnalyzeLayoutsPanel.java +++ b/client/src/com/vaadin/client/debug/internal/AnalyzeLayoutsPanel.java @@ -40,7 +40,6 @@ import com.vaadin.client.ComputedStyle; import com.vaadin.client.ConnectorMap; import com.vaadin.client.ServerConnector; import com.vaadin.client.SimpleTree; -import com.vaadin.client.Util; import com.vaadin.client.ValueMap; /** @@ -112,9 +111,12 @@ public class AnalyzeLayoutsPanel extends FlowPanel { final ServerConnector parent = connector.getParent(); final String parentId = parent.getConnectorId(); - final Label errorDetails = new Label(Util.getSimpleName(connector) - + "[" + connector.getConnectorId() + "]" + " inside " - + Util.getSimpleName(parent)); + final Label errorDetails = new Label(connector.getClass() + .getSimpleName() + + "[" + + connector.getConnectorId() + + "]" + + " inside " + parent.getClass().getSimpleName()); if (parent instanceof ComponentConnector) { final ComponentConnector parentConnector = (ComponentConnector) parent; @@ -171,8 +173,8 @@ public class AnalyzeLayoutsPanel extends FlowPanel { Highlight.show(connector); - final SimpleTree errorNode = new SimpleTree( - Util.getSimpleName(connector) + " id: " + pid); + final SimpleTree errorNode = new SimpleTree(connector.getClass() + .getSimpleName() + " id: " + pid); errorNode.addDomHandler(new MouseOverHandler() { @Override public void onMouseOver(MouseOverEvent event) { diff --git a/client/src/com/vaadin/client/debug/internal/ConnectorInfoPanel.java b/client/src/com/vaadin/client/debug/internal/ConnectorInfoPanel.java index 0b49fa7aaf..0856bb3575 100644 --- a/client/src/com/vaadin/client/debug/internal/ConnectorInfoPanel.java +++ b/client/src/com/vaadin/client/debug/internal/ConnectorInfoPanel.java @@ -24,8 +24,8 @@ import com.google.gwt.user.client.ui.HTML; import com.vaadin.client.ComponentConnector; import com.vaadin.client.JsArrayObject; import com.vaadin.client.ServerConnector; -import com.vaadin.client.Util; import com.vaadin.client.VConsole; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.metadata.NoDataException; import com.vaadin.client.metadata.Property; import com.vaadin.client.ui.AbstractConnector; @@ -51,7 +51,7 @@ public class ConnectorInfoPanel extends FlowPanel { ignoreProperties.add("id"); String html = getRowHTML("Id", connector.getConnectorId()); - html += getRowHTML("Connector", Util.getSimpleName(connector)); + html += getRowHTML("Connector", connector.getClass().getSimpleName()); if (connector instanceof ComponentConnector) { ComponentConnector component = (ComponentConnector) connector; @@ -61,8 +61,8 @@ public class ConnectorInfoPanel extends FlowPanel { AbstractComponentState componentState = component.getState(); - html += getRowHTML("Widget", - Util.getSimpleName(component.getWidget())); + html += getRowHTML("Widget", component.getWidget().getClass() + .getSimpleName()); html += getRowHTML("Caption", componentState.caption); html += getRowHTML("Description", componentState.description); html += getRowHTML("Width", componentState.width + " (actual: " @@ -95,7 +95,8 @@ public class ConnectorInfoPanel extends FlowPanel { return "<div class=\"" + VDebugWindow.STYLENAME + "-row\"><span class=\"caption\">" + caption + "</span><span class=\"value\">" - + Util.escapeHTML(String.valueOf(value)) + "</span></div>"; + + WidgetUtil.escapeHTML(String.valueOf(value)) + + "</span></div>"; } /** diff --git a/client/src/com/vaadin/client/debug/internal/HierarchySection.java b/client/src/com/vaadin/client/debug/internal/HierarchySection.java index 404ac430df..c772a9d267 100644 --- a/client/src/com/vaadin/client/debug/internal/HierarchySection.java +++ b/client/src/com/vaadin/client/debug/internal/HierarchySection.java @@ -35,6 +35,7 @@ import com.vaadin.client.ComponentConnector; import com.vaadin.client.ServerConnector; import com.vaadin.client.Util; import com.vaadin.client.ValueMap; +import com.vaadin.client.WidgetUtil; /** * Provides functionality for examining the UI component hierarchy. @@ -240,7 +241,7 @@ public class HierarchySection implements Section { } if (event.getTypeInt() == Event.ONMOUSEMOVE) { Highlight.hideAll(); - Element eventTarget = Util.getElementFromPoint(event + Element eventTarget = WidgetUtil.getElementFromPoint(event .getNativeEvent().getClientX(), event.getNativeEvent() .getClientY()); if (VDebugWindow.get().getElement().isOrHasChild(eventTarget)) { @@ -272,7 +273,7 @@ public class HierarchySection implements Section { event.consume(); event.getNativeEvent().stopPropagation(); stopFind(); - Element eventTarget = Util.getElementFromPoint(event + Element eventTarget = WidgetUtil.getElementFromPoint(event .getNativeEvent().getClientX(), event.getNativeEvent() .getClientY()); for (ApplicationConnection a : ApplicationConfiguration diff --git a/client/src/com/vaadin/client/debug/internal/ProfilerSection.java b/client/src/com/vaadin/client/debug/internal/ProfilerSection.java index c4fea5cf71..7fb0284f8e 100644 --- a/client/src/com/vaadin/client/debug/internal/ProfilerSection.java +++ b/client/src/com/vaadin/client/debug/internal/ProfilerSection.java @@ -16,10 +16,8 @@ package com.vaadin.client.debug.internal; import java.util.Collection; -import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import java.util.Map.Entry; import java.util.Set; @@ -29,6 +27,8 @@ import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.Profiler; +import com.vaadin.client.Profiler.Node; +import com.vaadin.client.Profiler.ProfilerResultConsumer; import com.vaadin.client.SimpleTree; import com.vaadin.client.ValueMap; @@ -42,237 +42,6 @@ import com.vaadin.client.ValueMap; * @see Profiler */ public class ProfilerSection implements Section { - /** - * Interface for getting data from the {@link Profiler}. - * <p> - * <b>Warning!</b> This interface is most likely to change in the future and - * is therefore defined in this class in an internal package instead of - * Profiler where it might seem more logical. - * - * @since 7.1 - * @author Vaadin Ltd - */ - public interface ProfilerResultConsumer { - public void addProfilerData(Node rootNode, List<Node> totals); - - public void addBootstrapData(LinkedHashMap<String, Double> timings); - } - - /** - * A hierarchical representation of the time spent running a named block of - * code. - * <p> - * <b>Warning!</b> This class is most likely to change in the future and is - * therefore defined in this class in an internal package instead of - * Profiler where it might seem more logical. - */ - public static class Node { - private final String name; - private final LinkedHashMap<String, Node> children = new LinkedHashMap<String, Node>(); - private double time = 0; - private int count = 0; - private double enterTime = 0; - private double minTime = 1000000000; - private double maxTime = 0; - - /** - * Create a new node with the given name. - * - * @param name - */ - public Node(String name) { - this.name = name; - } - - /** - * Gets the name of the node - * - * @return the name of the node - */ - public String getName() { - return name; - } - - /** - * Creates a new child node or retrieves and existing child and updates - * its total time and hit count. - * - * @param name - * the name of the child - * @param timestamp - * the timestamp for when the node is entered - * @return the child node object - */ - public Node enterChild(String name, double timestamp) { - Node child = children.get(name); - if (child == null) { - child = new Node(name); - children.put(name, child); - } - child.enterTime = timestamp; - child.count++; - return child; - } - - /** - * Gets the total time spent in this node, including time spent in sub - * nodes - * - * @return the total time spent, in milliseconds - */ - public double getTimeSpent() { - return time; - } - - /** - * Gets the minimum time spent for one invocation of this node, - * including time spent in sub nodes - * - * @return the time spent for the fastest invocation, in milliseconds - */ - public double getMinTimeSpent() { - return minTime; - } - - /** - * Gets the maximum time spent for one invocation of this node, - * including time spent in sub nodes - * - * @return the time spent for the slowest invocation, in milliseconds - */ - public double getMaxTimeSpent() { - return maxTime; - } - - /** - * Gets the number of times this node has been entered - * - * @return the number of times the node has been entered - */ - public int getCount() { - return count; - } - - /** - * Gets the total time spent in this node, excluding time spent in sub - * nodes - * - * @return the total time spent, in milliseconds - */ - public double getOwnTime() { - double time = getTimeSpent(); - for (Node node : children.values()) { - time -= node.getTimeSpent(); - } - return time; - } - - /** - * Gets the child nodes of this node - * - * @return a collection of child nodes - */ - public Collection<Node> getChildren() { - return Collections.unmodifiableCollection(children.values()); - } - - private void buildRecursiveString(StringBuilder builder, String prefix) { - if (getName() != null) { - String msg = getStringRepresentation(prefix); - builder.append(msg + '\n'); - } - String childPrefix = prefix + "*"; - for (Node node : children.values()) { - node.buildRecursiveString(builder, childPrefix); - } - } - - @Override - public String toString() { - return getStringRepresentation(""); - } - - public String getStringRepresentation(String prefix) { - if (getName() == null) { - return ""; - } - String msg = prefix + " " + getName() + " in " + getTimeSpent() - + " ms."; - if (getCount() > 1) { - msg += " Invoked " - + getCount() - + " times (" - + roundToSignificantFigures(getTimeSpent() / getCount()) - + " ms per time, min " - + roundToSignificantFigures(getMinTimeSpent()) - + " ms, max " - + roundToSignificantFigures(getMaxTimeSpent()) - + " ms)."; - } - if (!children.isEmpty()) { - double ownTime = getOwnTime(); - msg += " " + ownTime + " ms spent in own code"; - if (getCount() > 1) { - msg += " (" - + roundToSignificantFigures(ownTime / getCount()) - + " ms per time)"; - } - msg += '.'; - } - return msg; - } - - private static double roundToSignificantFigures(double num) { - // Number of significant digits - int n = 3; - if (num == 0) { - return 0; - } - - final double d = Math.ceil(Math.log10(num < 0 ? -num : num)); - final int power = n - (int) d; - - final double magnitude = Math.pow(10, power); - final long shifted = Math.round(num * magnitude); - return shifted / magnitude; - } - - public void sumUpTotals(Map<String, Node> totals) { - String name = getName(); - if (name != null) { - Node totalNode = totals.get(name); - if (totalNode == null) { - totalNode = new Node(name); - totals.put(name, totalNode); - } - - totalNode.time += getOwnTime(); - totalNode.count += getCount(); - totalNode.minTime = Math.min(totalNode.minTime, - getMinTimeSpent()); - totalNode.maxTime = Math.max(totalNode.maxTime, - getMaxTimeSpent()); - } - for (Node node : children.values()) { - node.sumUpTotals(totals); - } - } - - /** - * @param timestamp - */ - public void leave(double timestamp) { - double elapsed = (timestamp - enterTime); - time += elapsed; - enterTime = 0; - if (elapsed < minTime) { - minTime = elapsed; - } - if (elapsed > maxTime) { - maxTime = elapsed; - } - } - } private static final int MAX_ROWS = 10; diff --git a/client/src/com/vaadin/client/debug/internal/TestBenchSection.java b/client/src/com/vaadin/client/debug/internal/TestBenchSection.java index 355565f706..d0b6b10722 100644 --- a/client/src/com/vaadin/client/debug/internal/TestBenchSection.java +++ b/client/src/com/vaadin/client/debug/internal/TestBenchSection.java @@ -41,6 +41,7 @@ import com.vaadin.client.ComponentConnector; import com.vaadin.client.ServerConnector; import com.vaadin.client.Util; import com.vaadin.client.ValueMap; +import com.vaadin.client.WidgetUtil; /** * Provides functionality for picking selectors for Vaadin TestBench. @@ -62,7 +63,8 @@ public class TestBenchSection implements Section { String html = "<div class=\"" + VDebugWindow.STYLENAME + "-selector\"><span class=\"tb-selector\">" - + Util.escapeHTML(path.getElementQuery()) + "</span></div>"; + + WidgetUtil.escapeHTML(path.getElementQuery()) + + "</span></div>"; setHTML(html); addMouseOverHandler(this); @@ -216,7 +218,7 @@ public class TestBenchSection implements Section { } if (event.getTypeInt() == Event.ONMOUSEMOVE || event.getTypeInt() == Event.ONCLICK) { - Element eventTarget = Util.getElementFromPoint(event + Element eventTarget = WidgetUtil.getElementFromPoint(event .getNativeEvent().getClientX(), event.getNativeEvent() .getClientY()); if (VDebugWindow.get().getElement().isOrHasChild(eventTarget)) { @@ -230,8 +232,9 @@ public class TestBenchSection implements Section { // make sure that not finding the highlight element only Highlight.hideAll(); - eventTarget = Util.getElementFromPoint(event.getNativeEvent() - .getClientX(), event.getNativeEvent().getClientY()); + eventTarget = WidgetUtil.getElementFromPoint(event + .getNativeEvent().getClientX(), event.getNativeEvent() + .getClientY()); ComponentConnector connector = findConnector(eventTarget); if (event.getTypeInt() == Event.ONMOUSEMOVE) { diff --git a/client/src/com/vaadin/client/extensions/ResponsiveConnector.java b/client/src/com/vaadin/client/extensions/ResponsiveConnector.java index e88025531b..2e1e75f6cd 100644 --- a/client/src/com/vaadin/client/extensions/ResponsiveConnector.java +++ b/client/src/com/vaadin/client/extensions/ResponsiveConnector.java @@ -382,7 +382,7 @@ public class ResponsiveConnector extends AbstractExtensionConnector implements /** * Forces IE8 to reinterpret CSS rules. - * {@link com.vaadin.client.Util#forceIE8Redraw(com.google.gwt.dom.client.Element)} + * {@link com.vaadin.client.WidgetUtil#forceIE8Redraw(com.google.gwt.dom.client.Element)} * doesn't work in this case. * * @param element diff --git a/client/src/com/vaadin/client/extensions/javascriptmanager/JavaScriptManagerConnector.java b/client/src/com/vaadin/client/extensions/javascriptmanager/JavaScriptManagerConnector.java index f76f5058c5..d48571452e 100644 --- a/client/src/com/vaadin/client/extensions/javascriptmanager/JavaScriptManagerConnector.java +++ b/client/src/com/vaadin/client/extensions/javascriptmanager/JavaScriptManagerConnector.java @@ -21,8 +21,8 @@ import java.util.Set; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArray; -import com.google.gwt.json.client.JSONArray; import com.vaadin.client.ServerConnector; +import com.vaadin.client.Util; import com.vaadin.client.communication.JavaScriptMethodInvocation; import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.extensions.AbstractExtensionConnector; @@ -116,7 +116,7 @@ public class JavaScriptManagerConnector extends AbstractExtensionConnector { }-*/; public void sendRpc(String name, JsArray<JavaScriptObject> arguments) { - Object[] parameters = new Object[] { name, new JSONArray(arguments) }; + Object[] parameters = new Object[] { name, Util.jso2json(arguments) }; /* * Must invoke manually as the RPC interface can't be used in GWT diff --git a/client/src/com/vaadin/client/metadata/Method.java b/client/src/com/vaadin/client/metadata/Method.java index d6b474fabc..8757a9de20 100644 --- a/client/src/com/vaadin/client/metadata/Method.java +++ b/client/src/com/vaadin/client/metadata/Method.java @@ -15,6 +15,8 @@ */ package com.vaadin.client.metadata; +import com.vaadin.shared.annotations.NoLayout; + public class Method { private final Type type; @@ -100,4 +102,16 @@ public class Method { return TypeDataStore.isLastOnly(this); } + /** + * Checks whether this method is annotated with {@link NoLayout}. + * + * @since 7.4 + * + * @return <code>true</code> if this method has a NoLayout annotation; + * otherwise <code>false</code> + */ + public boolean isNoLayout() { + return TypeDataStore.isNoLayoutRpcMethod(this); + } + } diff --git a/client/src/com/vaadin/client/metadata/Property.java b/client/src/com/vaadin/client/metadata/Property.java index f421a5525b..90b29b32b7 100644 --- a/client/src/com/vaadin/client/metadata/Property.java +++ b/client/src/com/vaadin/client/metadata/Property.java @@ -16,6 +16,7 @@ package com.vaadin.client.metadata; import com.vaadin.shared.annotations.DelegateToWidget; +import com.vaadin.shared.annotations.NoLayout; public class Property { private final Type bean; @@ -127,4 +128,16 @@ public class Property { return b.toString(); } + /** + * Checks whether this property is annotated with {@link NoLayout}. + * + * @since 7.4 + * + * @return <code>true</code> if this property has a NoLayout annotation; + * otherwise <code>false</code> + */ + public boolean isNoLayout() { + return TypeDataStore.isNoLayoutProperty(this); + } + } diff --git a/client/src/com/vaadin/client/metadata/TypeDataStore.java b/client/src/com/vaadin/client/metadata/TypeDataStore.java index 7aa952d0f2..46f26f1b25 100644 --- a/client/src/com/vaadin/client/metadata/TypeDataStore.java +++ b/client/src/com/vaadin/client/metadata/TypeDataStore.java @@ -25,8 +25,13 @@ import com.vaadin.client.FastStringSet; import com.vaadin.client.JsArrayObject; import com.vaadin.client.annotations.OnStateChange; import com.vaadin.client.communication.JSONSerializer; +import com.vaadin.shared.annotations.NoLayout; public class TypeDataStore { + public static enum MethodAttribute { + DELAYED, LAST_ONLY, NO_LAYOUT, NO_LOADING_INDICATOR; + } + private static final String CONSTRUCTOR_NAME = "!new"; private final FastStringMap<Class<?>> identifiers = FastStringMap.create(); @@ -37,6 +42,8 @@ public class TypeDataStore { .create(); private final FastStringMap<JsArrayString> delegateToWidgetProperties = FastStringMap .create(); + private final FastStringMap<Type> presentationTypes = FastStringMap + .create(); /** * Maps connector class -> state property name -> hander method data @@ -44,8 +51,8 @@ public class TypeDataStore { private final FastStringMap<FastStringMap<JsArrayObject<OnStateChangeMethod>>> onStateChangeMethods = FastStringMap .create(); - private final FastStringSet delayedMethods = FastStringSet.create(); - private final FastStringSet lastOnlyMethods = FastStringSet.create(); + private final FastStringMap<FastStringSet> methodAttributes = FastStringMap + .create(); private final FastStringMap<Type> returnTypes = FastStringMap.create(); private final FastStringMap<Invoker> invokers = FastStringMap.create(); @@ -135,6 +142,10 @@ public class TypeDataStore { return get().delegateToWidgetProperties.get(type.getBaseTypeName()); } + public static Type getPresentationType(Class<?> type) { + return get().presentationTypes.get(getType(type).getBaseTypeName()); + } + public void setDelegateToWidget(Class<?> clazz, String propertyName, String delegateValue) { Type type = getType(clazz); @@ -150,6 +161,11 @@ public class TypeDataStore { typeProperties.push(propertyName); } + public void setPresentationType(Class<?> type, Class<?> presentationType) { + presentationTypes.put(getType(type).getBaseTypeName(), + getType(presentationType)); + } + public void setReturnType(Class<?> type, String methodName, Type returnType) { returnTypes.put(new Method(getType(type), methodName).getLookupKey(), returnType); @@ -200,20 +216,33 @@ public class TypeDataStore { } public static boolean isDelayed(Method method) { - return get().delayedMethods.contains(method.getLookupKey()); + return hasMethodAttribute(method, MethodAttribute.DELAYED); } - public void setDelayed(Class<?> type, String methodName) { - delayedMethods.add(getType(type).getMethod(methodName).getLookupKey()); + public static boolean isNoLoadingIndicator(Method method) { + return hasMethodAttribute(method, MethodAttribute.NO_LOADING_INDICATOR); } - public static boolean isLastOnly(Method method) { - return get().lastOnlyMethods.contains(method.getLookupKey()); + private static boolean hasMethodAttribute(Method method, + MethodAttribute attribute) { + FastStringSet attributes = get().methodAttributes.get(method + .getLookupKey()); + return attributes != null && attributes.contains(attribute.name()); + } + + public void setMethodAttribute(Class<?> type, String methodName, + MethodAttribute attribute) { + String key = getType(type).getMethod(methodName).getLookupKey(); + FastStringSet attributes = methodAttributes.get(key); + if (attributes == null) { + attributes = FastStringSet.create(); + methodAttributes.put(key, attributes); + } + attributes.add(attribute.name()); } - public void setLastOnly(Class<?> clazz, String methodName) { - lastOnlyMethods - .add(getType(clazz).getMethod(methodName).getLookupKey()); + public static boolean isLastOnly(Method method) { + return hasMethodAttribute(method, MethodAttribute.LAST_ONLY); } /** @@ -334,6 +363,12 @@ public class TypeDataStore { return typeData[beanName][propertyName].setter !== undefined; }-*/; + private static native boolean hasNoLayout(JavaScriptObject typeData, + String beanName, String propertyName) + /*-{ + return typeData[beanName][propertyName].noLayout !== undefined; + }-*/; + private static native Object getJsPropertyValue(JavaScriptObject typeData, String beanName, String propertyName, Object beanInstance) /*-{ @@ -418,4 +453,35 @@ public class TypeDataStore { propertyHandlers.add(method); } } + + /** + * Checks whether the provided method is annotated with {@link NoLayout}. + * + * @param method + * the rpc method to check + * + * @since 7.4 + * + * @return <code>true</code> if the method has a NoLayout annotation; + * otherwise <code>false</code> + */ + public static boolean isNoLayoutRpcMethod(Method method) { + return hasMethodAttribute(method, MethodAttribute.NO_LAYOUT); + } + + /** + * Checks whether the provided property is annotated with {@link NoLayout}. + * + * @param property + * the property to check + * + * @since 7.4 + * + * @return <code>true</code> if the property has a NoLayout annotation; + * otherwise <code>false</code> + */ + public static boolean isNoLayoutProperty(Property property) { + return hasNoLayout(get().jsTypeData, property.getBeanType() + .getSignature(), property.getName()); + } } diff --git a/client/src/com/vaadin/client/renderers/ButtonRenderer.java b/client/src/com/vaadin/client/renderers/ButtonRenderer.java new file mode 100644 index 0000000000..b173aef60a --- /dev/null +++ b/client/src/com/vaadin/client/renderers/ButtonRenderer.java @@ -0,0 +1,44 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.renderers; + +import com.google.gwt.core.shared.GWT; +import com.google.gwt.user.client.ui.Button; +import com.vaadin.client.widget.grid.RendererCellReference; + +/** + * A Renderer that displays buttons with textual captions. The values of the + * corresponding column are used as the captions. Click handlers can be added to + * the renderer, invoked when any of the rendered buttons is clicked. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class ButtonRenderer extends ClickableRenderer<String, Button> { + + @Override + public Button createWidget() { + Button b = GWT.create(Button.class); + b.addClickHandler(this); + b.setStylePrimaryName("v-nativebutton"); + return b; + } + + @Override + public void render(RendererCellReference cell, String text, Button button) { + button.setText(text); + } +} diff --git a/client/src/com/vaadin/client/renderers/ClickableRenderer.java b/client/src/com/vaadin/client/renderers/ClickableRenderer.java new file mode 100644 index 0000000000..f5368d31c9 --- /dev/null +++ b/client/src/com/vaadin/client/renderers/ClickableRenderer.java @@ -0,0 +1,229 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.renderers; + +import com.google.gwt.dom.client.BrowserEvents; +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.EventTarget; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.event.dom.client.DomEvent; +import com.google.gwt.event.dom.client.MouseEvent; +import com.google.gwt.event.shared.EventHandler; +import com.google.gwt.event.shared.HandlerManager; +import com.google.gwt.user.client.ui.Widget; +import com.google.web.bindery.event.shared.HandlerRegistration; +import com.vaadin.client.WidgetUtil; +import com.vaadin.client.widget.escalator.Cell; +import com.vaadin.client.widget.escalator.RowContainer; +import com.vaadin.client.widget.grid.CellReference; +import com.vaadin.client.widget.grid.EventCellReference; +import com.vaadin.client.widgets.Escalator; +import com.vaadin.client.widgets.Grid; + +/** + * An abstract superclass for renderers that render clickable widgets. Click + * handlers can be added to a renderer to listen to click events emitted by all + * widgets rendered by the renderer. + * + * @param <T> + * the presentation (column) type + * @param <W> + * the widget type + * + * @since 7.4 + * @author Vaadin Ltd + */ +public abstract class ClickableRenderer<T, W extends Widget> extends + WidgetRenderer<T, W> implements ClickHandler { + + /** + * A handler for {@link RendererClickEvent renderer click events}. + * + * @param <R> + * the row type of the containing Grid + * + * @see {@link ButtonRenderer#addClickHandler(RendererClickHandler)} + */ + public interface RendererClickHandler<R> extends EventHandler { + + /** + * Called when a rendered button is clicked. + * + * @param event + * the event representing the click + */ + void onClick(RendererClickEvent<R> event); + } + + /** + * An event fired when a widget rendered by a ClickableWidgetRenderer + * subclass is clicked. + * + * @param <R> + * the row type of the containing Grid + */ + @SuppressWarnings("rawtypes") + public static class RendererClickEvent<R> extends + MouseEvent<RendererClickHandler> { + + @SuppressWarnings("unchecked") + static final Type<RendererClickHandler> TYPE = new Type<RendererClickHandler>( + BrowserEvents.CLICK, new RendererClickEvent()); + + private CellReference<R> cell; + + private R row; + + private RendererClickEvent() { + } + + /** + * Returns the cell of the clicked button. + * + * @return the cell + */ + public CellReference<R> getCell() { + return cell; + } + + /** + * Returns the data object corresponding to the row of the clicked + * button. + * + * @return the row data object + */ + public R getRow() { + return row; + } + + @Override + public Type<RendererClickHandler> getAssociatedType() { + return TYPE; + } + + @Override + @SuppressWarnings("unchecked") + protected void dispatch(RendererClickHandler handler) { + + EventTarget target = getNativeEvent().getEventTarget(); + + if (!Element.is(target)) { + return; + } + + Element e = Element.as(target); + Grid<R> grid = (Grid<R>) findClosestParentGrid(e); + + cell = findCell(grid, e); + row = cell.getRow(); + + handler.onClick(this); + } + + /** + * Returns the cell the given element belongs to. + * + * @param grid + * the grid instance that is queried + * @param e + * a cell element or the descendant of one + * @return the cell or null if the element is not a grid cell or a + * descendant of one + */ + private static <T> CellReference<T> findCell(Grid<T> grid, Element e) { + RowContainer container = getEscalator(grid).findRowContainer(e); + if (container == null) { + return null; + } + Cell cell = container.getCell(e); + EventCellReference<T> cellReference = new EventCellReference<T>( + grid); + cellReference.set(cell); + return cellReference; + } + + private native static Escalator getEscalator(Grid<?> grid) + /*-{ + return grid.@com.vaadin.client.widgets.Grid::escalator; + }-*/; + + /** + * Returns the Grid instance containing the given element, if any. + * <p> + * <strong>Note:</strong> This method may not work reliably if the grid + * in question is wrapped in a {@link Composite} <em>unless</em> the + * element is inside another widget that is a child of the wrapped grid; + * please refer to the note in + * {@link WidgetUtil#findWidget(Element, Class) Util.findWidget} for + * details. + * + * @param e + * the element whose parent grid to find + * @return the parent grid or null if none found. + */ + private static Grid<?> findClosestParentGrid(Element e) { + Widget w = WidgetUtil.findWidget(e, null); + + while (w != null && !(w instanceof Grid)) { + w = w.getParent(); + } + return (Grid<?>) w; + } + } + + private HandlerManager handlerManager; + + /** + * {@inheritDoc} + * <p> + * <em>Implementation note:</em> It is the implementing method's + * responsibility to add {@code this} as a click handler of the returned + * widget, or a widget nested therein, in order to make click events + * propagate properly to handlers registered via + * {@link #addClickHandler(RendererClickHandler) addClickHandler}. + */ + @Override + public abstract W createWidget(); + + /** + * Adds a click handler to this button renderer. The handler is invoked + * every time one of the widgets rendered by this renderer is clicked. + * <p> + * Note that the row type of the click handler must match the row type of + * the containing Grid. + * + * @param handler + * the click handler to be added + */ + public HandlerRegistration addClickHandler(RendererClickHandler<?> handler) { + if (handlerManager == null) { + handlerManager = new HandlerManager(this); + } + return handlerManager.addHandler(RendererClickEvent.TYPE, handler); + } + + @Override + public void onClick(ClickEvent event) { + /* + * The handler manager is lazily instantiated so it's null iff + * addClickHandler is never called. + */ + if (handlerManager != null) { + DomEvent.fireNativeEvent(event.getNativeEvent(), handlerManager); + } + } +} diff --git a/client/src/com/vaadin/client/renderers/ComplexRenderer.java b/client/src/com/vaadin/client/renderers/ComplexRenderer.java new file mode 100644 index 0000000000..75ea523cdc --- /dev/null +++ b/client/src/com/vaadin/client/renderers/ComplexRenderer.java @@ -0,0 +1,157 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.renderers; + +import java.util.Collection; +import java.util.Collections; + +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.NativeEvent; +import com.google.gwt.dom.client.Node; +import com.google.gwt.dom.client.Style.Visibility; +import com.vaadin.client.widget.escalator.Cell; +import com.vaadin.client.widget.escalator.FlyweightCell; +import com.vaadin.client.widget.grid.CellReference; +import com.vaadin.client.widget.grid.RendererCellReference; + +/** + * Base class for renderers that needs initialization and destruction logic + * (override {@link #init(FlyweightCell) and #destroy(FlyweightCell) } and event + * handling (see {@link #onBrowserEvent(Cell, NativeEvent)}, + * {@link #getConsumedEvents()} and {@link #onActivate()}. + * + * <p> + * Also provides a helper method for hiding the cell contents by overriding + * {@link #setContentVisible(FlyweightCell, boolean)} + * + * @since 7.4.0 + * @author Vaadin Ltd + */ +public abstract class ComplexRenderer<T> implements Renderer<T> { + + /** + * Called at initialization stage. Perform any initialization here e.g. + * attach handlers, attach widgets etc. + * + * @param cell + * The cell. Note that the cell is not to be stored outside of + * the method as the cell install will change. See + * {@link FlyweightCell} + */ + public abstract void init(RendererCellReference cell); + + /** + * Called after the cell is deemed to be destroyed and no longer used by the + * Grid. Called after the cell element is detached from the DOM. + * <p> + * The row object in the cell reference will be <code>null</code> since the + * row might no longer be present in the data source. + * + * @param cell + * The cell. Note that the cell is not to be stored outside of + * the method as the cell install will change. See + * {@link FlyweightCell} + */ + public void destroy(RendererCellReference cell) { + // Implement if needed + } + + /** + * Returns the events that the renderer should consume. These are also the + * events that the Grid will pass to + * {@link #onBrowserEvent(Cell, NativeEvent)} when they occur. + * + * @return a list of consumed events + * + * @see com.google.gwt.dom.client.BrowserEvents + */ + public Collection<String> getConsumedEvents() { + return Collections.emptyList(); + } + + /** + * Called whenever a registered event is triggered in the column the + * renderer renders. + * <p> + * The events that triggers this needs to be returned by the + * {@link #getConsumedEvents()} method. + * <p> + * Returns boolean telling if the event has been completely handled and + * should not cause any other actions. + * + * @param cell + * Object containing information about the cell the event was + * triggered on. + * + * @param event + * The original DOM event + * @return true if event should not be handled by grid + */ + public boolean onBrowserEvent(CellReference<?> cell, NativeEvent event) { + return false; + } + + /** + * Used by Grid to toggle whether to show actual data or just an empty + * placeholder while data is loading. This method is invoked whenever a cell + * changes between data being available and data missing. + * <p> + * Default implementation hides content by setting visibility: hidden to all + * elements inside the cell. Text nodes are left as is - renderers that add + * such to the root element need to implement explicit support hiding them. + * + * @param cell + * The cell + * @param hasData + * Has the cell content been loaded from the data source + * + */ + public void setContentVisible(RendererCellReference cell, boolean hasData) { + Element cellElement = cell.getElement(); + for (int n = 0; n < cellElement.getChildCount(); n++) { + Node node = cellElement.getChild(n); + if (Element.is(node)) { + Element e = Element.as(node); + if (hasData) { + e.getStyle().clearVisibility(); + } else { + e.getStyle().setVisibility(Visibility.HIDDEN); + } + } + } + } + + /** + * Called when the cell is activated by pressing <code>enter</code>, double + * clicking or performing a double tap on the cell. + * + * @param cell + * the activated cell + * @return <code>true</code> if event was handled and should not be + * interpreted as a generic gesture by Grid. + */ + public boolean onActivate(CellReference<?> cell) { + return false; + } + + /** + * Called when the renderer is deemed to be destroyed and no longer used by + * the Grid. + */ + public void destroy() { + // Implement if needed + } +} diff --git a/client/src/com/vaadin/client/renderers/DateRenderer.java b/client/src/com/vaadin/client/renderers/DateRenderer.java new file mode 100644 index 0000000000..4d15fac724 --- /dev/null +++ b/client/src/com/vaadin/client/renderers/DateRenderer.java @@ -0,0 +1,108 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.renderers; + +import java.util.Date; + +import com.google.gwt.i18n.client.TimeZone; +import com.google.gwt.i18n.shared.DateTimeFormat; +import com.google.gwt.i18n.shared.DateTimeFormat.PredefinedFormat; +import com.vaadin.client.widget.grid.RendererCellReference; + +/** + * A renderer for rendering dates into cells + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class DateRenderer implements Renderer<Date> { + + private DateTimeFormat format; + + // Calendar is unavailable for GWT + @SuppressWarnings("deprecation") + private TimeZone timeZone = TimeZone.createTimeZone(new Date() + .getTimezoneOffset()); + + public DateRenderer() { + this(PredefinedFormat.DATE_TIME_SHORT); + } + + public DateRenderer(PredefinedFormat format) { + this(DateTimeFormat.getFormat(format)); + } + + public DateRenderer(DateTimeFormat format) { + setFormat(format); + } + + @Override + public void render(RendererCellReference cell, Date date) { + String dateStr = format.format(date, timeZone); + cell.getElement().setInnerText(dateStr); + } + + /** + * Gets the format of how the date is formatted. + * + * @return the format + * @see <a + * href="http://www.gwtproject.org/javadoc/latest/com/google/gwt/i18n/shared/DateTimeFormat.html">GWT + * documentation on DateTimeFormat</a> + */ + public DateTimeFormat getFormat() { + return format; + } + + /** + * Sets the format used for formatting the dates. + * + * @param format + * the format to set + * @see <a + * href="http://www.gwtproject.org/javadoc/latest/com/google/gwt/i18n/shared/DateTimeFormat.html">GWT + * documentation on DateTimeFormat</a> + */ + public void setFormat(DateTimeFormat format) { + if (format == null) { + throw new IllegalArgumentException("Format should not be null"); + } + this.format = format; + } + + /** + * Returns the time zone of the date. + * + * @return the time zone + */ + public TimeZone getTimeZone() { + return timeZone; + } + + /** + * Sets the time zone of the the date. By default uses the time zone of the + * browser. + * + * @param timeZone + * the timeZone to set + */ + public void setTimeZone(TimeZone timeZone) { + if (timeZone == null) { + throw new IllegalArgumentException("Timezone should not be null"); + } + this.timeZone = timeZone; + } +} diff --git a/client/src/com/vaadin/client/renderers/HtmlRenderer.java b/client/src/com/vaadin/client/renderers/HtmlRenderer.java new file mode 100644 index 0000000000..ec6dc761f6 --- /dev/null +++ b/client/src/com/vaadin/client/renderers/HtmlRenderer.java @@ -0,0 +1,41 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.renderers; + +import com.google.gwt.safehtml.shared.SafeHtml; +import com.google.gwt.safehtml.shared.SafeHtmlUtils; +import com.vaadin.client.widget.grid.RendererCellReference; + +/** + * Renders a string as HTML into a cell. + * <p> + * The html string is rendered as is without any escaping. It is up to the + * developer to ensure that the html string honors the {@link SafeHtml} + * contract. For more information see + * {@link SafeHtmlUtils#fromSafeConstant(String)}. + * + * @since 7.4 + * @author Vaadin Ltd + * @see SafeHtmlUtils#fromSafeConstant(String) + */ +public class HtmlRenderer implements Renderer<String> { + + @Override + public void render(RendererCellReference cell, String htmlString) { + cell.getElement().setInnerSafeHtml( + SafeHtmlUtils.fromSafeConstant(htmlString)); + } +} diff --git a/client/src/com/vaadin/client/renderers/ImageRenderer.java b/client/src/com/vaadin/client/renderers/ImageRenderer.java new file mode 100644 index 0000000000..b1e8ce5702 --- /dev/null +++ b/client/src/com/vaadin/client/renderers/ImageRenderer.java @@ -0,0 +1,43 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.renderers; + +import com.google.gwt.core.client.GWT; +import com.google.gwt.user.client.ui.Image; +import com.vaadin.client.widget.grid.RendererCellReference; + +/** + * A renderer that renders an image into a cell. Click handlers can be added to + * the renderer, invoked every time any of the images rendered by that rendered + * is clicked. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class ImageRenderer extends ClickableRenderer<String, Image> { + + @Override + public Image createWidget() { + Image image = GWT.create(Image.class); + image.addClickHandler(this); + return image; + } + + @Override + public void render(RendererCellReference cell, String url, Image image) { + image.setUrl(url); + } +} diff --git a/client/src/com/vaadin/client/renderers/NumberRenderer.java b/client/src/com/vaadin/client/renderers/NumberRenderer.java new file mode 100644 index 0000000000..a040e8d1ce --- /dev/null +++ b/client/src/com/vaadin/client/renderers/NumberRenderer.java @@ -0,0 +1,71 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.renderers; + +import com.google.gwt.i18n.client.NumberFormat; +import com.vaadin.client.widget.grid.RendererCellReference; + +/** + * Renders a number into a cell using a specific {@link NumberFormat}. By + * default uses the default number format returned by + * {@link NumberFormat#getDecimalFormat()}. + * + * @since 7.4 + * @author Vaadin Ltd + * @param <T> + * The number type to render. + */ +public class NumberRenderer implements Renderer<Number> { + + private NumberFormat format; + + public NumberRenderer() { + this(NumberFormat.getDecimalFormat()); + } + + public NumberRenderer(NumberFormat format) { + setFormat(format); + } + + /** + * Gets the number format that the number should be formatted in. + * + * @return the number format used to render the number + */ + public NumberFormat getFormat() { + return format; + } + + /** + * Sets the number format to use for formatting the number. + * + * @param format + * the format to use + * @throws IllegalArgumentException + * when the format is null + */ + public void setFormat(NumberFormat format) throws IllegalArgumentException { + if (format == null) { + throw new IllegalArgumentException("Format cannot be null"); + } + this.format = format; + } + + @Override + public void render(RendererCellReference cell, Number number) { + cell.getElement().setInnerText(format.format(number)); + } +} diff --git a/client/src/com/vaadin/client/renderers/ObjectRenderer.java b/client/src/com/vaadin/client/renderers/ObjectRenderer.java new file mode 100644 index 0000000000..a2c4e7bfc6 --- /dev/null +++ b/client/src/com/vaadin/client/renderers/ObjectRenderer.java @@ -0,0 +1,36 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.renderers; + +import com.vaadin.client.widget.grid.RendererCellReference; + +/** + * A renderer for displaying an object to a string using the + * {@link Object#toString()} method. + * <p> + * If the object is <code>null</code>, then it is rendered as an empty string + * instead. + * + * @since + * @author Vaadin Ltd + */ +public class ObjectRenderer implements Renderer<Object> { + @Override + public void render(RendererCellReference cell, Object data) { + String text = (data != null) ? data.toString() : ""; + cell.getElement().setInnerText(text); + } +} diff --git a/client/src/com/vaadin/client/renderers/ProgressBarRenderer.java b/client/src/com/vaadin/client/renderers/ProgressBarRenderer.java new file mode 100644 index 0000000000..5b2c70d274 --- /dev/null +++ b/client/src/com/vaadin/client/renderers/ProgressBarRenderer.java @@ -0,0 +1,47 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.renderers; + +import com.google.gwt.core.shared.GWT; +import com.vaadin.client.ui.VProgressBar; +import com.vaadin.client.widget.grid.RendererCellReference; + +/** + * A Renderer that represents a double value as a graphical progress bar. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class ProgressBarRenderer extends WidgetRenderer<Double, VProgressBar> { + + @Override + public VProgressBar createWidget() { + VProgressBar progressBar = GWT.create(VProgressBar.class); + progressBar.addStyleDependentName("static"); + return progressBar; + } + + @Override + public void render(RendererCellReference cell, Double data, + VProgressBar progressBar) { + if (data == null) { + progressBar.setEnabled(false); + } else { + progressBar.setEnabled(true); + progressBar.setState(data.floatValue()); + } + } +} diff --git a/client/src/com/vaadin/client/renderers/Renderer.java b/client/src/com/vaadin/client/renderers/Renderer.java new file mode 100644 index 0000000000..a3faa1e9df --- /dev/null +++ b/client/src/com/vaadin/client/renderers/Renderer.java @@ -0,0 +1,48 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.renderers; + +import com.vaadin.client.widget.escalator.Cell; +import com.vaadin.client.widget.grid.RendererCellReference; +import com.vaadin.client.widgets.Grid; + +/** + * Renderer for rending a value <T> into cell. + * <p> + * You can add a renderer to any column by overring the + * {@link GridColumn#getRenderer()} method and returning your own renderer. You + * can retrieve the cell element using {@link Cell#getElement()}. + * + * @param <T> + * The column type + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface Renderer<T> { + + /** + * Called whenever the {@link Grid} updates a cell + * + * @param cell + * The cell. Note that the cell is a flyweight and should not be + * stored outside of the method as it will change. + * + * @param data + * The column data object + */ + void render(RendererCellReference cell, T data); +} diff --git a/client/src/com/vaadin/client/renderers/TextRenderer.java b/client/src/com/vaadin/client/renderers/TextRenderer.java new file mode 100644 index 0000000000..3f704fd0b4 --- /dev/null +++ b/client/src/com/vaadin/client/renderers/TextRenderer.java @@ -0,0 +1,32 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.renderers; + +import com.vaadin.client.widget.grid.RendererCellReference; + +/** + * Renderer that renders text into a cell. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class TextRenderer implements Renderer<String> { + + @Override + public void render(RendererCellReference cell, String text) { + cell.getElement().setInnerText(text); + } +} diff --git a/client/src/com/vaadin/client/renderers/WidgetRenderer.java b/client/src/com/vaadin/client/renderers/WidgetRenderer.java new file mode 100644 index 0000000000..668ec7b59e --- /dev/null +++ b/client/src/com/vaadin/client/renderers/WidgetRenderer.java @@ -0,0 +1,104 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.renderers; + +import com.google.gwt.dom.client.TableCellElement; +import com.google.gwt.user.client.ui.Widget; +import com.vaadin.client.WidgetUtil; +import com.vaadin.client.widget.grid.RendererCellReference; + +/** + * A renderer for rendering widgets into cells. + * + * @since 7.4 + * @author Vaadin Ltd + * @param <T> + * the row data type + * @param <W> + * the Widget type + */ +public abstract class WidgetRenderer<T, W extends Widget> extends + ComplexRenderer<T> { + + @Override + public void init(RendererCellReference cell) { + // Implement if needed + } + + /** + * Creates a widget to attach to a cell. The widgets will be attached to the + * cell after the cell element has been attached to DOM. + * + * @return widget to attach to a cell. All returned instances should be new + * widget instances without a parent. + */ + public abstract W createWidget(); + + @Override + public void render(RendererCellReference cell, T data) { + W w = getWidget(cell.getElement()); + assert w != null : "Widget not found in cell (" + cell.getColumn() + + "," + cell.getRow() + ")"; + render(cell, data, w); + } + + /** + * Renders a cell with a widget. This provides a way to update any + * information in the widget that is cell specific. Do not detach the Widget + * here, it will be done automatically by the Grid when the widget is no + * longer needed. + * + * @param cell + * the cell to render + * @param data + * the data of the cell + * @param widget + * the widget embedded in the cell + */ + public abstract void render(RendererCellReference cell, T data, W widget); + + /** + * Returns the widget contained inside the given cell element. Cannot be + * called for cells that do not contain a widget. + * + * @param e + * the element inside which to find a widget + * @return the widget inside the element + */ + protected W getWidget(TableCellElement e) { + W w = getWidget(e, null); + assert w != null : "Widget not found inside cell"; + return w; + } + + /** + * Returns the widget contained inside the given cell element, or null if it + * is not an instance of the given class. Cannot be called for cells that do + * not contain a widget. + * + * @param e + * the element inside to find a widget + * @param klass + * the type of the widget to find + * @return the widget inside the element, or null if its type does not match + */ + protected static <W extends Widget> W getWidget(TableCellElement e, + Class<W> klass) { + W w = WidgetUtil.findWidget(e.getFirstChildElement(), klass); + assert w == null || w.getElement() == e.getFirstChildElement() : "Widget not found inside cell"; + return w; + } +} diff --git a/client/src/com/vaadin/client/ui/AbstractClickEventHandler.java b/client/src/com/vaadin/client/ui/AbstractClickEventHandler.java index c08656c4d9..a2c54ec7ca 100644 --- a/client/src/com/vaadin/client/ui/AbstractClickEventHandler.java +++ b/client/src/com/vaadin/client/ui/AbstractClickEventHandler.java @@ -32,7 +32,7 @@ import com.google.gwt.user.client.Event; import com.google.gwt.user.client.Event.NativePreviewEvent; import com.google.gwt.user.client.Event.NativePreviewHandler; import com.vaadin.client.ComponentConnector; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.VConsole; public abstract class AbstractClickEventHandler implements MouseDownHandler, @@ -72,8 +72,8 @@ public abstract class AbstractClickEventHandler implements MouseDownHandler, // Event's reported target not always correct if event // capture is in use - Element elementUnderMouse = Util.getElementUnderMouse(event - .getNativeEvent()); + Element elementUnderMouse = WidgetUtil + .getElementUnderMouse(event.getNativeEvent()); if (lastMouseDownTarget != null && elementUnderMouse == lastMouseDownTarget) { mouseUpPreviewMatched = true; @@ -171,7 +171,8 @@ public abstract class AbstractClickEventHandler implements MouseDownHandler, * When getting a mousedown event, we must detect where the * corresponding mouseup event if it's on a different part of the page. */ - lastMouseDownTarget = Util.getElementUnderMouse(event.getNativeEvent()); + lastMouseDownTarget = WidgetUtil.getElementUnderMouse(event + .getNativeEvent()); mouseUpPreviewMatched = false; mouseUpEventPreviewRegistration = Event .addNativePreviewHandler(mouseUpPreviewHandler); @@ -188,7 +189,7 @@ public abstract class AbstractClickEventHandler implements MouseDownHandler, if (hasEventListener() && mouseUpPreviewMatched && lastMouseDownTarget != null - && Util.getElementUnderMouse(event.getNativeEvent()) == lastMouseDownTarget + && WidgetUtil.getElementUnderMouse(event.getNativeEvent()) == lastMouseDownTarget && shouldFireEvent(event)) { // "Click" with left, right or middle button fireClick(event.getNativeEvent()); diff --git a/client/src/com/vaadin/client/ui/AbstractComponentConnector.java b/client/src/com/vaadin/client/ui/AbstractComponentConnector.java index c3f14be40c..46ad289488 100644 --- a/client/src/com/vaadin/client/ui/AbstractComponentConnector.java +++ b/client/src/com/vaadin/client/ui/AbstractComponentConnector.java @@ -21,7 +21,6 @@ import com.google.gwt.dom.client.Element; import com.google.gwt.user.client.ui.Focusable; import com.google.gwt.user.client.ui.HasEnabled; import com.google.gwt.user.client.ui.Widget; -import com.vaadin.client.ApplicationConnection; import com.vaadin.client.ComponentConnector; import com.vaadin.client.HasComponentsConnector; import com.vaadin.client.LayoutManager; @@ -89,7 +88,7 @@ public abstract class AbstractComponentConnector extends AbstractConnector } catch (NoDataException e) { throw new IllegalStateException( "Default implementation of createWidget() does not work for " - + Util.getSimpleName(this) + + getClass().getSimpleName() + ". This might be caused by explicitely using " + "super.createWidget() or some unspecified " + "problem with the widgetset compilation.", e); @@ -106,10 +105,10 @@ public abstract class AbstractComponentConnector extends AbstractConnector public Widget getWidget() { if (widget == null) { Profiler.enter("AbstractComponentConnector.createWidget for " - + Util.getSimpleName(this)); + + getClass().getSimpleName()); widget = createWidget(); Profiler.leave("AbstractComponentConnector.createWidget for " - + Util.getSimpleName(this)); + + getClass().getSimpleName()); } return widget; @@ -195,8 +194,7 @@ public abstract class AbstractComponentConnector extends AbstractConnector @Override public void setWidgetEnabled(boolean widgetEnabled) { // add or remove v-disabled style name from the widget - setWidgetStyleName(ApplicationConnection.DISABLED_CLASSNAME, - !widgetEnabled); + setWidgetStyleName(StyleConstants.DISABLED, !widgetEnabled); if (getWidget() instanceof HasEnabled) { // set widget specific enabled state @@ -343,8 +341,7 @@ public abstract class AbstractComponentConnector extends AbstractConnector // add / remove error style name setWidgetStyleNameWithPrefix(primaryStyleName, - ApplicationConnection.ERROR_CLASSNAME_EXT, - null != state.errorMessage); + StyleConstants.ERROR_EXT, null != state.errorMessage); // add additional user defined style names as class names, prefixed with // component default class name. remove nonexistent style names. diff --git a/client/src/com/vaadin/client/ui/AbstractConnector.java b/client/src/com/vaadin/client/ui/AbstractConnector.java index e93ea0f507..a20c3463c2 100644 --- a/client/src/com/vaadin/client/ui/AbstractConnector.java +++ b/client/src/com/vaadin/client/ui/AbstractConnector.java @@ -120,11 +120,13 @@ public abstract class AbstractConnector implements ServerConnector, addStateChangeHandler(this); if (Profiler.isEnabled()) { - Profiler.enter("AbstractConnector.init " + Util.getSimpleName(this)); + Profiler.enter("AbstractConnector.init " + + getClass().getSimpleName()); } init(); if (Profiler.isEnabled()) { - Profiler.leave("AbstractConnector.init " + Util.getSimpleName(this)); + Profiler.leave("AbstractConnector.init " + + getClass().getSimpleName()); } Profiler.leave("AbstractConnector.doInit"); } @@ -214,8 +216,8 @@ public abstract class AbstractConnector implements ServerConnector, public void fireEvent(GwtEvent<?> event) { String profilerKey = null; if (Profiler.isEnabled()) { - profilerKey = "Fire " + Util.getSimpleName(event) + " for " - + Util.getSimpleName(this); + profilerKey = "Fire " + event.getClass().getSimpleName() + " for " + + getClass().getSimpleName(); Profiler.enter(profilerKey); } if (handlerManager != null) { @@ -377,7 +379,7 @@ public abstract class AbstractConnector implements ServerConnector, } catch (NoDataException e) { throw new IllegalStateException( "There is no information about the state for " - + Util.getSimpleName(this) + + getClass().getSimpleName() + ". Did you remember to compile the right widgetset?", e); } @@ -391,7 +393,7 @@ public abstract class AbstractConnector implements ServerConnector, } catch (NoDataException e) { throw new IllegalStateException( "There is no information about the state for " - + Util.getSimpleName(connector) + + connector.getClass().getSimpleName() + ". Did you remember to compile the right widgetset?", e); } diff --git a/client/src/com/vaadin/client/ui/AbstractFieldConnector.java b/client/src/com/vaadin/client/ui/AbstractFieldConnector.java index 965e79b6fd..8d8df81bd8 100644 --- a/client/src/com/vaadin/client/ui/AbstractFieldConnector.java +++ b/client/src/com/vaadin/client/ui/AbstractFieldConnector.java @@ -15,7 +15,7 @@ */ package com.vaadin.client.ui; -import com.vaadin.client.ApplicationConnection; +import com.vaadin.client.StyleConstants; import com.vaadin.shared.AbstractFieldState; public abstract class AbstractFieldConnector extends AbstractComponentConnector { @@ -51,14 +51,12 @@ public abstract class AbstractFieldConnector extends AbstractComponentConnector super.updateWidgetStyleNames(); // add / remove modified style name to Fields - setWidgetStyleName(ApplicationConnection.MODIFIED_CLASSNAME, - isModified()); + setWidgetStyleName(StyleConstants.MODIFIED, isModified()); // add / remove error style name to Fields setWidgetStyleNameWithPrefix(getWidget().getStylePrimaryName(), - ApplicationConnection.REQUIRED_CLASSNAME_EXT, isRequired()); + StyleConstants.REQUIRED_EXT, isRequired()); - getWidget().setStyleName(ApplicationConnection.REQUIRED_CLASSNAME, - isRequired()); + getWidget().setStyleName(StyleConstants.REQUIRED, isRequired()); } } diff --git a/client/src/com/vaadin/client/ui/MediaBaseConnector.java b/client/src/com/vaadin/client/ui/MediaBaseConnector.java index cebb2e3836..fdd9610517 100644 --- a/client/src/com/vaadin/client/ui/MediaBaseConnector.java +++ b/client/src/com/vaadin/client/ui/MediaBaseConnector.java @@ -15,7 +15,7 @@ */ package com.vaadin.client.ui; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.shared.communication.URLReference; import com.vaadin.shared.ui.AbstractMediaState; @@ -78,7 +78,7 @@ public abstract class MediaBaseConnector extends AbstractComponentConnector { if (altText == null || "".equals(altText)) { altText = getDefaultAltHtml(); } else if (!getState().htmlContentAllowed) { - altText = Util.escapeHTML(altText); + altText = WidgetUtil.escapeHTML(altText); } getWidget().setAltText(altText); } diff --git a/client/src/com/vaadin/client/ui/SubPartAware.java b/client/src/com/vaadin/client/ui/SubPartAware.java index a064b8a8a8..7a40eea20e 100644 --- a/client/src/com/vaadin/client/ui/SubPartAware.java +++ b/client/src/com/vaadin/client/ui/SubPartAware.java @@ -16,11 +16,10 @@ package com.vaadin.client.ui; import com.google.gwt.user.client.ui.Widget; -import com.vaadin.client.componentlocator.ComponentLocator; /** * Interface implemented by {@link Widget}s which can provide identifiers for at - * least one element inside the component. Used by {@link ComponentLocator}. + * least one element inside the component. * */ public interface SubPartAware { diff --git a/client/src/com/vaadin/client/ui/VAbstractSplitPanel.java b/client/src/com/vaadin/client/ui/VAbstractSplitPanel.java index 9d32355b70..b52663b161 100644 --- a/client/src/com/vaadin/client/ui/VAbstractSplitPanel.java +++ b/client/src/com/vaadin/client/ui/VAbstractSplitPanel.java @@ -44,7 +44,7 @@ import com.vaadin.client.ComponentConnector; import com.vaadin.client.ConnectorMap; import com.vaadin.client.LayoutManager; import com.vaadin.client.StyleConstants; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.VConsole; import com.vaadin.client.ui.TouchScrollDelegate.TouchScrollHandler; import com.vaadin.client.ui.VAbstractSplitPanel.SplitterMoveHandler.SplitterMoveEvent; @@ -577,7 +577,7 @@ public class VAbstractSplitPanel extends ComplexPanel { break; } // Only fire click event listeners if the splitter isn't moved - if (Util.isTouchEvent(event) || !resized) { + if (WidgetUtil.isTouchEvent(event) || !resized) { super.onBrowserEvent(event); } else if (DOM.eventGetType(event) == Event.ONMOUSEUP) { // Reset the resized flag after a mouseup has occured so the next @@ -596,8 +596,8 @@ public class VAbstractSplitPanel extends ComplexPanel { DOM.setCapture(getElement()); origX = DOM.getElementPropertyInt(splitter, "offsetLeft"); origY = DOM.getElementPropertyInt(splitter, "offsetTop"); - origMouseX = Util.getTouchOrMouseClientX(event); - origMouseY = Util.getTouchOrMouseClientY(event); + origMouseX = WidgetUtil.getTouchOrMouseClientX(event); + origMouseY = WidgetUtil.getTouchOrMouseClientY(event); event.stopPropagation(); event.preventDefault(); } @@ -606,12 +606,12 @@ public class VAbstractSplitPanel extends ComplexPanel { public void onMouseMove(Event event) { switch (orientation) { case HORIZONTAL: - final int x = Util.getTouchOrMouseClientX(event); + final int x = WidgetUtil.getTouchOrMouseClientX(event); onHorizontalMouseMove(x); break; case VERTICAL: default: - final int y = Util.getTouchOrMouseClientY(event); + final int y = WidgetUtil.getTouchOrMouseClientY(event); onVerticalMouseMove(y); break; } @@ -688,7 +688,7 @@ public class VAbstractSplitPanel extends ComplexPanel { DOM.releaseCapture(getElement()); hideDraggingCurtain(); resizing = false; - if (!Util.isTouchEvent(event)) { + if (!WidgetUtil.isTouchEvent(event)) { onMouseMove(event); } fireEvent(new SplitterMoveEvent(this)); diff --git a/client/src/com/vaadin/client/ui/VAccordion.java b/client/src/com/vaadin/client/ui/VAccordion.java index 422f195af9..06eaecaf70 100644 --- a/client/src/com/vaadin/client/ui/VAccordion.java +++ b/client/src/com/vaadin/client/ui/VAccordion.java @@ -29,7 +29,7 @@ import com.google.gwt.user.client.Event; import com.google.gwt.user.client.ui.ComplexPanel; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.ComponentConnector; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.VCaption; import com.vaadin.client.ui.TouchScrollDelegate.TouchScrollHandler; import com.vaadin.shared.ComponentConstants; @@ -203,7 +203,7 @@ public class VAccordion extends VTabsheetBase { } int captionWidth = caption.getRequiredWidth(); - int padding = Util.measureHorizontalPaddingAndBorder( + int padding = WidgetUtil.measureHorizontalPaddingAndBorder( caption.getElement(), 18); return captionWidth + padding; } diff --git a/client/src/com/vaadin/client/ui/VButton.java b/client/src/com/vaadin/client/ui/VButton.java index dcc364c1da..bf321f7f00 100644 --- a/client/src/com/vaadin/client/ui/VButton.java +++ b/client/src/com/vaadin/client/ui/VButton.java @@ -30,6 +30,7 @@ import com.google.gwt.user.client.ui.FocusWidget; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.BrowserInfo; import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; public class VButton extends FocusWidget implements ClickHandler { @@ -373,10 +374,10 @@ public class VButton extends FocusWidget implements ClickHandler { // Set (x,y) client coordinates to the middle of the button int x = getElement().getAbsoluteLeft() - getElement().getScrollLeft() - getElement().getOwnerDocument().getScrollLeft() - + Util.getRequiredWidth(getElement()) / 2; + + WidgetUtil.getRequiredWidth(getElement()) / 2; int y = getElement().getAbsoluteTop() - getElement().getScrollTop() - getElement().getOwnerDocument().getScrollTop() - + Util.getRequiredHeight(getElement()) / 2; + + WidgetUtil.getRequiredHeight(getElement()) / 2; NativeEvent evt = Document.get().createClickEvent(1, 0, 0, x, y, false, false, false, false); getElement().dispatchEvent(evt); diff --git a/client/src/com/vaadin/client/ui/VCalendarPanel.java b/client/src/com/vaadin/client/ui/VCalendarPanel.java index 6fc06bb153..e1b906b6e4 100644 --- a/client/src/com/vaadin/client/ui/VCalendarPanel.java +++ b/client/src/com/vaadin/client/ui/VCalendarPanel.java @@ -54,7 +54,7 @@ import com.google.gwt.user.client.ui.ListBox; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.BrowserInfo; import com.vaadin.client.DateTimeService; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.VConsole; import com.vaadin.shared.ui.datefield.Resolution; import com.vaadin.shared.util.SharedUtil; @@ -2065,7 +2065,7 @@ public class VCalendarPanel extends FocusableFlexTable implements return SUBPART_PREV_YEAR; } else if (contains(days, subElement)) { // Day, find out which dayOfMonth and use that as the identifier - Day day = Util.findWidget(subElement, Day.class); + Day day = WidgetUtil.findWidget(subElement, Day.class); if (day != null) { Date date = day.getDate(); int id = date.getDate(); diff --git a/client/src/com/vaadin/client/ui/VContextMenu.java b/client/src/com/vaadin/client/ui/VContextMenu.java index fa6d67fc0c..6028eea52c 100644 --- a/client/src/com/vaadin/client/ui/VContextMenu.java +++ b/client/src/com/vaadin/client/ui/VContextMenu.java @@ -51,7 +51,7 @@ import com.google.gwt.user.client.ui.PopupPanel; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.impl.FocusImpl; import com.vaadin.client.Focusable; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; public class VContextMenu extends VOverlay implements SubPartAware { @@ -89,7 +89,7 @@ public class VContextMenu extends VOverlay implements SubPartAware { addCloseHandler(new CloseHandler<PopupPanel>() { @Override public void onClose(CloseEvent<PopupPanel> event) { - Element currentFocus = Util.getFocusedElement(); + Element currentFocus = WidgetUtil.getFocusedElement(); if (focusedElement != null && (currentFocus == null || menu.getElement().isOrHasChild(currentFocus) || RootPanel @@ -137,11 +137,11 @@ public class VContextMenu extends VOverlay implements SubPartAware { } // Attach onload listeners to all images - Util.sinkOnloadForImages(menu.getElement()); + WidgetUtil.sinkOnloadForImages(menu.getElement()); // Store the currently focused element, which will be re-focused when // context menu is closed - focusedElement = Util.getFocusedElement(); + focusedElement = WidgetUtil.getFocusedElement(); // reset height (if it has been previously set explicitly) setHeight(""); diff --git a/client/src/com/vaadin/client/ui/VCustomLayout.java b/client/src/com/vaadin/client/ui/VCustomLayout.java index f5d572007a..5f8a8197d0 100644 --- a/client/src/com/vaadin/client/ui/VCustomLayout.java +++ b/client/src/com/vaadin/client/ui/VCustomLayout.java @@ -37,6 +37,7 @@ import com.vaadin.client.StyleConstants; import com.vaadin.client.Util; import com.vaadin.client.VCaption; import com.vaadin.client.VCaptionWrapper; +import com.vaadin.client.WidgetUtil; /** * Custom Layout implements complex layout defined with HTML template. @@ -158,7 +159,8 @@ public class VCustomLayout extends ComplexPanel { // TODO prefix img src:s here with a regeps, cannot work further with IE - String relImgPrefix = Util.escapeAttribute(themeUri + "/layouts/"); + String relImgPrefix = WidgetUtil + .escapeAttribute(themeUri + "/layouts/"); // prefix all relative image elements to point to theme dir with a // regexp search diff --git a/client/src/com/vaadin/client/ui/VEmbedded.java b/client/src/com/vaadin/client/ui/VEmbedded.java index acf814471a..f3970f9ac7 100644 --- a/client/src/com/vaadin/client/ui/VEmbedded.java +++ b/client/src/com/vaadin/client/ui/VEmbedded.java @@ -31,6 +31,7 @@ import com.vaadin.client.ConnectorMap; import com.vaadin.client.UIDL; import com.vaadin.client.Util; import com.vaadin.client.VConsole; +import com.vaadin.client.WidgetUtil; import com.vaadin.shared.ui.embedded.EmbeddedConstants; public class VEmbedded extends HTML { @@ -83,8 +84,8 @@ public class VEmbedded extends HTML { */ if (uidl.hasAttribute("classid")) { html.append("classid=\"" - + Util.escapeAttribute(uidl.getStringAttribute("classid")) - + "\" "); + + WidgetUtil.escapeAttribute(uidl + .getStringAttribute("classid")) + "\" "); } else { html.append("classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" "); } @@ -99,8 +100,8 @@ public class VEmbedded extends HTML { */ if (uidl.hasAttribute("codebase")) { html.append("codebase=\"" - + Util.escapeAttribute(uidl.getStringAttribute("codebase")) - + "\" "); + + WidgetUtil.escapeAttribute(uidl + .getStringAttribute("codebase")) + "\" "); } else { html.append("codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" "); } @@ -111,29 +112,29 @@ public class VEmbedded extends HTML { String width = paintable.getState().width; // Add width and height - html.append("width=\"" + Util.escapeAttribute(width) + "\" "); - html.append("height=\"" + Util.escapeAttribute(height) + "\" "); + html.append("width=\"" + WidgetUtil.escapeAttribute(width) + "\" "); + html.append("height=\"" + WidgetUtil.escapeAttribute(height) + "\" "); html.append("type=\"application/x-shockwave-flash\" "); // Codetype if (uidl.hasAttribute("codetype")) { html.append("codetype=\"" - + Util.escapeAttribute(uidl.getStringAttribute("codetype")) - + "\" "); + + WidgetUtil.escapeAttribute(uidl + .getStringAttribute("codetype")) + "\" "); } // Standby if (uidl.hasAttribute("standby")) { html.append("standby=\"" - + Util.escapeAttribute(uidl.getStringAttribute("standby")) - + "\" "); + + WidgetUtil.escapeAttribute(uidl + .getStringAttribute("standby")) + "\" "); } // Archive if (uidl.hasAttribute("archive")) { html.append("archive=\"" - + Util.escapeAttribute(uidl.getStringAttribute("archive")) - + "\" "); + + WidgetUtil.escapeAttribute(uidl + .getStringAttribute("archive")) + "\" "); } // End object tag @@ -148,25 +149,25 @@ public class VEmbedded extends HTML { // Add parameters to OBJECT for (String name : parameters.keySet()) { html.append("<param "); - html.append("name=\"" + Util.escapeAttribute(name) + "\" "); - html.append("value=\"" + Util.escapeAttribute(parameters.get(name)) - + "\" "); + html.append("name=\"" + WidgetUtil.escapeAttribute(name) + "\" "); + html.append("value=\"" + + WidgetUtil.escapeAttribute(parameters.get(name)) + "\" "); html.append("/>"); } // Build inner EMBED tag html.append("<embed "); - html.append("src=\"" + Util.escapeAttribute(getSrc(uidl, client)) + html.append("src=\"" + WidgetUtil.escapeAttribute(getSrc(uidl, client)) + "\" "); - html.append("width=\"" + Util.escapeAttribute(width) + "\" "); - html.append("height=\"" + Util.escapeAttribute(height) + "\" "); + html.append("width=\"" + WidgetUtil.escapeAttribute(width) + "\" "); + html.append("height=\"" + WidgetUtil.escapeAttribute(height) + "\" "); html.append("type=\"application/x-shockwave-flash\" "); // Add the parameters to the Embed for (String name : parameters.keySet()) { - html.append(Util.escapeAttribute(name)); + html.append(WidgetUtil.escapeAttribute(name)); html.append("="); - html.append("\"" + Util.escapeAttribute(parameters.get(name)) + html.append("\"" + WidgetUtil.escapeAttribute(parameters.get(name)) + "\""); } diff --git a/client/src/com/vaadin/client/ui/VFilterSelect.java b/client/src/com/vaadin/client/ui/VFilterSelect.java index bb217f2de2..c0575b1ea5 100644 --- a/client/src/com/vaadin/client/ui/VFilterSelect.java +++ b/client/src/com/vaadin/client/ui/VFilterSelect.java @@ -67,8 +67,8 @@ import com.vaadin.client.ComputedStyle; import com.vaadin.client.ConnectorMap; import com.vaadin.client.Focusable; import com.vaadin.client.UIDL; -import com.vaadin.client.Util; import com.vaadin.client.VConsole; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.aria.AriaHelper; import com.vaadin.client.ui.aria.HandlesAriaCaption; import com.vaadin.client.ui.aria.HandlesAriaInvalid; @@ -134,7 +134,7 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, // options and are not collapsed (#7506) content = " "; } else { - content = Util.escapeHTML(caption); + content = WidgetUtil.escapeHTML(caption); } sb.append("<span>" + content + "</span>"); return sb.toString(); @@ -599,8 +599,8 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, final int naturalMenuWidth = menuFirstChild.getOffsetWidth(); if (popupOuterPadding == -1) { - popupOuterPadding = Util.measureHorizontalPaddingAndBorder( - getElement(), 2); + popupOuterPadding = WidgetUtil + .measureHorizontalPaddingAndBorder(getElement(), 2); } if (naturalMenuWidth < desiredWidth) { @@ -657,7 +657,7 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, menu.setHeight(menuHeight + "px"); final int naturalMenuWidthPlusScrollBar = naturalMenuWidth - + Util.getNativeScrollbarSize(); + + WidgetUtil.getNativeScrollbarSize(); if (offsetWidth < naturalMenuWidthPlusScrollBar) { menu.setWidth(naturalMenuWidthPlusScrollBar + "px"); } @@ -818,7 +818,7 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, final MenuItem mi = new MenuItem(s.getDisplayString(), true, s); Roles.getListitemRole().set(mi.getElement()); - Util.sinkOnloadForImages(mi.getElement()); + WidgetUtil.sinkOnloadForImages(mi.getElement()); this.addItem(mi); if (s == currentSuggestion) { @@ -1069,7 +1069,8 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, * the end and the focus to the start. This makes Firefox work * the same way as other browsers (#13477) */ - Util.setSelectionRange(getElement(), pos, length, "backward"); + WidgetUtil.setSelectionRange(getElement(), pos, length, + "backward"); } else { /* @@ -1609,7 +1610,7 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, } private void forceReflow() { - Util.setStyleTemporarily(tb.getElement(), "zoom", "1"); + WidgetUtil.setStyleTemporarily(tb.getElement(), "zoom", "1"); } /** @@ -1621,7 +1622,7 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, int availableHeight = 0; availableHeight = getOffsetHeight(); - int iconHeight = Util.getRequiredHeight(selectedItemIcon); + int iconHeight = WidgetUtil.getRequiredHeight(selectedItemIcon); int marginTop = (availableHeight - iconHeight) / 2; selectedItemIcon.getElement().getStyle() .setMarginTop(marginTop, Unit.PX); @@ -1936,7 +1937,7 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, */ public void updateSuggestionPopupMinWidth() { // used only to calculate minimum width - String captions = Util.escapeHTML(inputPrompt); + String captions = WidgetUtil.escapeHTML(inputPrompt); for (FilterSelectSuggestion suggestion : currentSuggestions) { // Collect captions so we can calculate minimum width for @@ -1944,7 +1945,8 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, if (captions.length() > 0) { captions += "|"; } - captions += Util.escapeHTML(suggestion.getReplacementString()); + captions += WidgetUtil + .escapeHTML(suggestion.getReplacementString()); } // Calculate minimum textarea width @@ -2051,7 +2053,7 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, */ preventNextBlurEventInIE = false; - Element focusedElement = Util.getIEFocusedElement(); + Element focusedElement = WidgetUtil.getFocusedElement(); if (getElement().isOrHasChild(focusedElement) || suggestionPopup.getElement() .isOrHasChild(focusedElement)) { @@ -2129,7 +2131,7 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, * when the popup is used to view longer items than the text box is * wide. */ - int w = Util.getRequiredWidth(this); + int w = WidgetUtil.getRequiredWidth(this); if ((!initDone || currentPage + 1 < 0) && suggestionPopupMinWidth > w) { @@ -2150,9 +2152,9 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, // Use util.getRequiredWidth instead of getOffsetWidth here - int iconWidth = selectedItemIcon == null ? 0 : Util + int iconWidth = selectedItemIcon == null ? 0 : WidgetUtil .getRequiredWidth(selectedItemIcon); - int buttonWidth = popupOpener == null ? 0 : Util + int buttonWidth = popupOpener == null ? 0 : WidgetUtil .getRequiredWidth(popupOpener); /* diff --git a/client/src/com/vaadin/client/ui/VFlash.java b/client/src/com/vaadin/client/ui/VFlash.java index cf15f89cb4..eaf53836ee 100644 --- a/client/src/com/vaadin/client/ui/VFlash.java +++ b/client/src/com/vaadin/client/ui/VFlash.java @@ -19,7 +19,7 @@ import java.util.HashMap; import java.util.Map; import com.google.gwt.user.client.ui.HTML; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; public class VFlash extends HTML { @@ -156,7 +156,8 @@ public class VFlash extends HTML { * this by setting his own classid. */ if (classId != null) { - html.append("classid=\"" + Util.escapeAttribute(classId) + "\" "); + html.append("classid=\"" + WidgetUtil.escapeAttribute(classId) + + "\" "); } else { html.append("classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" "); } @@ -170,29 +171,33 @@ public class VFlash extends HTML { * codebase */ if (codebase != null) { - html.append("codebase=\"" + Util.escapeAttribute(codebase) + "\" "); + html.append("codebase=\"" + WidgetUtil.escapeAttribute(codebase) + + "\" "); } else { html.append("codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" "); } // Add width and height - html.append("width=\"" + Util.escapeAttribute(width) + "\" "); - html.append("height=\"" + Util.escapeAttribute(height) + "\" "); + html.append("width=\"" + WidgetUtil.escapeAttribute(width) + "\" "); + html.append("height=\"" + WidgetUtil.escapeAttribute(height) + "\" "); html.append("type=\"application/x-shockwave-flash\" "); // Codetype if (codetype != null) { - html.append("codetype=\"" + Util.escapeAttribute(codetype) + "\" "); + html.append("codetype=\"" + WidgetUtil.escapeAttribute(codetype) + + "\" "); } // Standby if (standby != null) { - html.append("standby=\"" + Util.escapeAttribute(standby) + "\" "); + html.append("standby=\"" + WidgetUtil.escapeAttribute(standby) + + "\" "); } // Archive if (archive != null) { - html.append("archive=\"" + Util.escapeAttribute(archive) + "\" "); + html.append("archive=\"" + WidgetUtil.escapeAttribute(archive) + + "\" "); } // End object tag @@ -206,25 +211,25 @@ public class VFlash extends HTML { // Add parameters to OBJECT for (String name : embedParams.keySet()) { html.append("<param "); - html.append("name=\"" + Util.escapeAttribute(name) + "\" "); + html.append("name=\"" + WidgetUtil.escapeAttribute(name) + "\" "); html.append("value=\"" - + Util.escapeAttribute(embedParams.get(name)) + "\" "); + + WidgetUtil.escapeAttribute(embedParams.get(name)) + "\" "); html.append("/>"); } // Build inner EMBED tag html.append("<embed "); - html.append("src=\"" + Util.escapeAttribute(source) + "\" "); - html.append("width=\"" + Util.escapeAttribute(width) + "\" "); - html.append("height=\"" + Util.escapeAttribute(height) + "\" "); + html.append("src=\"" + WidgetUtil.escapeAttribute(source) + "\" "); + html.append("width=\"" + WidgetUtil.escapeAttribute(width) + "\" "); + html.append("height=\"" + WidgetUtil.escapeAttribute(height) + "\" "); html.append("type=\"application/x-shockwave-flash\" "); // Add the parameters to the Embed for (String name : embedParams.keySet()) { - html.append(Util.escapeAttribute(name)); + html.append(WidgetUtil.escapeAttribute(name)); html.append("="); - html.append("\"" + Util.escapeAttribute(embedParams.get(name)) - + "\""); + html.append("\"" + + WidgetUtil.escapeAttribute(embedParams.get(name)) + "\""); } // End embed tag diff --git a/client/src/com/vaadin/client/ui/VFormLayout.java b/client/src/com/vaadin/client/ui/VFormLayout.java index 64a7c5e579..a2ea77d31c 100644 --- a/client/src/com/vaadin/client/ui/VFormLayout.java +++ b/client/src/com/vaadin/client/ui/VFormLayout.java @@ -30,7 +30,6 @@ import com.google.gwt.user.client.ui.FlexTable; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.SimplePanel; import com.google.gwt.user.client.ui.Widget; -import com.vaadin.client.ApplicationConnection; import com.vaadin.client.BrowserInfo; import com.vaadin.client.ComponentConnector; import com.vaadin.client.Focusable; @@ -78,7 +77,7 @@ public class VFormLayout extends SimplePanel { } if (!enabled) { - styles.add(ApplicationConnection.DISABLED_CLASSNAME); + styles.add(StyleConstants.DISABLED); } return styles.toArray(new String[styles.size()]); @@ -242,7 +241,7 @@ public class VFormLayout extends SimplePanel { if (styles != null) { for (String style : styles) { - if (ApplicationConnection.DISABLED_CLASSNAME.equals(style)) { + if (StyleConstants.DISABLED.equals(style)) { // Add v-disabled also without classname prefix so // generic v-disabled CSS rules work styleName += " " + style; diff --git a/client/src/com/vaadin/client/ui/VLabel.java b/client/src/com/vaadin/client/ui/VLabel.java index 0f996fa6b9..a3572759c4 100644 --- a/client/src/com/vaadin/client/ui/VLabel.java +++ b/client/src/com/vaadin/client/ui/VLabel.java @@ -21,6 +21,7 @@ import com.google.gwt.user.client.ui.HTML; import com.vaadin.client.BrowserInfo; import com.vaadin.client.Util; import com.vaadin.client.VTooltip; +import com.vaadin.client.WidgetUtil; public class VLabel extends HTML { @@ -63,7 +64,7 @@ public class VLabel extends HTML { if (BrowserInfo.get().isIE8()) { // #3983 - IE8 incorrectly replaces \n with <br> so we do the // escaping manually and set as HTML - super.setHTML(Util.escapeHTML(text)); + super.setHTML(WidgetUtil.escapeHTML(text)); } else { super.setText(text); } diff --git a/client/src/com/vaadin/client/ui/VMenuBar.java b/client/src/com/vaadin/client/ui/VMenuBar.java index b5dac3f7ef..08f70f4dde 100644 --- a/client/src/com/vaadin/client/ui/VMenuBar.java +++ b/client/src/com/vaadin/client/ui/VMenuBar.java @@ -50,6 +50,7 @@ import com.vaadin.client.LayoutManager; import com.vaadin.client.TooltipInfo; import com.vaadin.client.UIDL; import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.shared.ui.menubar.MenuBarConstants; public class VMenuBar extends SimpleFocusablePanel implements @@ -234,7 +235,7 @@ public class VMenuBar extends SimpleFocusablePanel implements } String itemText = item.getStringAttribute("text"); if (!htmlContentAllowed) { - itemText = Util.escapeHTML(itemText); + itemText = WidgetUtil.escapeHTML(itemText); } itemHTML.append(itemText); itemHTML.append("</span>"); @@ -658,7 +659,8 @@ public class VMenuBar extends SimpleFocusablePanel implements // Make room for the scroll bar by adjusting the width of the // popup - style.setWidth(contentWidth + Util.getNativeScrollbarSize(), + style.setWidth( + contentWidth + WidgetUtil.getNativeScrollbarSize(), Unit.PX); popup.positionOrSizeUpdated(); } @@ -983,7 +985,7 @@ public class VMenuBar extends SimpleFocusablePanel implements // Sink the onload event for any icons. The onload // events are handled by the parent VMenuBar. - Util.sinkOnloadForImages(getElement()); + WidgetUtil.sinkOnloadForImages(getElement()); } @Override @@ -993,7 +995,7 @@ public class VMenuBar extends SimpleFocusablePanel implements @Override public void setText(String text) { - setHTML(Util.escapeHTML(text)); + setHTML(WidgetUtil.escapeHTML(text)); } public void setEnabled(boolean enabled) { diff --git a/client/src/com/vaadin/client/ui/VNativeButton.java b/client/src/com/vaadin/client/ui/VNativeButton.java index 8e0dd2bce1..77b2515f45 100644 --- a/client/src/com/vaadin/client/ui/VNativeButton.java +++ b/client/src/com/vaadin/client/ui/VNativeButton.java @@ -25,6 +25,7 @@ import com.google.gwt.user.client.ui.Button; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.BrowserInfo; import com.vaadin.client.MouseEventDetailsBuilder; +import com.vaadin.client.StyleConstants; import com.vaadin.client.Util; import com.vaadin.shared.MouseEventDetails; import com.vaadin.shared.ui.button.ButtonServerRpc; @@ -146,7 +147,7 @@ public class VNativeButton extends Button implements ClickHandler { setEnabled(false); // FIXME: This should be moved to NativeButtonConnector along with // buttonRpcProxy - addStyleName(ApplicationConnection.DISABLED_CLASSNAME); + addStyleName(StyleConstants.DISABLED); buttonRpcProxy.disableOnClick(); } diff --git a/client/src/com/vaadin/client/ui/VNotification.java b/client/src/com/vaadin/client/ui/VNotification.java index 5e1df67e18..d7639b0022 100644 --- a/client/src/com/vaadin/client/ui/VNotification.java +++ b/client/src/com/vaadin/client/ui/VNotification.java @@ -38,7 +38,7 @@ import com.vaadin.client.AnimationUtil.AnimationEndListener; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.BrowserInfo; import com.vaadin.client.UIDL; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.aria.AriaHelper; import com.vaadin.shared.Position; import com.vaadin.shared.ui.ui.NotificationRole; @@ -259,7 +259,7 @@ public class VNotification extends VOverlay { * nudge (#8551) */ if (BrowserInfo.get().isAndroid()) { - Util.setStyleTemporarily(getElement(), "display", "none"); + WidgetUtil.setStyleTemporarily(getElement(), "display", "none"); } } @@ -491,7 +491,7 @@ public class VNotification extends VOverlay { String caption = notification .getStringAttribute(UIConstants.ATTRIBUTE_NOTIFICATION_CAPTION); if (onlyPlainText) { - caption = Util.escapeHTML(caption); + caption = WidgetUtil.escapeHTML(caption); caption = caption.replaceAll("\\n", "<br />"); } html += "<h1>" + caption + "</h1>"; @@ -501,7 +501,7 @@ public class VNotification extends VOverlay { String message = notification .getStringAttribute(UIConstants.ATTRIBUTE_NOTIFICATION_MESSAGE); if (onlyPlainText) { - message = Util.escapeHTML(message); + message = WidgetUtil.escapeHTML(message); message = message.replaceAll("\\n", "<br />"); } html += "<p>" + message + "</p>"; diff --git a/client/src/com/vaadin/client/ui/VOptionGroup.java b/client/src/com/vaadin/client/ui/VOptionGroup.java index 34227831b9..d429752069 100644 --- a/client/src/com/vaadin/client/ui/VOptionGroup.java +++ b/client/src/com/vaadin/client/ui/VOptionGroup.java @@ -40,10 +40,11 @@ import com.google.gwt.user.client.ui.HasEnabled; import com.google.gwt.user.client.ui.Panel; import com.google.gwt.user.client.ui.RadioButton; import com.google.gwt.user.client.ui.Widget; -import com.vaadin.client.ApplicationConnection; import com.vaadin.client.BrowserInfo; +import com.vaadin.client.StyleConstants; import com.vaadin.client.UIDL; import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.shared.EventId; import com.vaadin.shared.ui.optiongroup.OptionGroupConstants; @@ -136,7 +137,7 @@ public class VOptionGroup extends VOptionGroupBase implements FocusHandler, String itemHtml = opUidl.getStringAttribute("caption"); if (!htmlContentAllowed) { - itemHtml = Util.escapeHTML(itemHtml); + itemHtml = WidgetUtil.escapeHTML(itemHtml); } String iconUrl = opUidl.getStringAttribute("icon"); @@ -160,7 +161,7 @@ public class VOptionGroup extends VOptionGroupBase implements FocusHandler, op.setStyleName("v-radiobutton"); } if (iconUrl != null && iconUrl.length() != 0) { - Util.sinkOnloadForImages(op.getElement()); + WidgetUtil.sinkOnloadForImages(op.getElement()); op.addHandler(iconLoadHandler, LoadEvent.getType()); } @@ -178,8 +179,7 @@ public class VOptionGroup extends VOptionGroupBase implements FocusHandler, op.setEnabled(enabled); optionsEnabled.put(op, optionEnabled); - setStyleName(op.getElement(), - ApplicationConnection.DISABLED_CLASSNAME, + setStyleName(op.getElement(), StyleConstants.DISABLED, !(optionEnabled && isEnabled())); newwidgets.add(op); @@ -248,14 +248,12 @@ public class VOptionGroup extends VOptionGroupBase implements FocusHandler, Boolean isOptionEnabled = optionsEnabled.get(w); if (isOptionEnabled == null) { hasEnabled.setEnabled(optionGroupEnabled); - setStyleName(w.getElement(), - ApplicationConnection.DISABLED_CLASSNAME, + setStyleName(w.getElement(), StyleConstants.DISABLED, !isEnabled()); } else { hasEnabled .setEnabled(isOptionEnabled && optionGroupEnabled); - setStyleName(w.getElement(), - ApplicationConnection.DISABLED_CLASSNAME, + setStyleName(w.getElement(), StyleConstants.DISABLED, !(isOptionEnabled && isEnabled())); } } diff --git a/client/src/com/vaadin/client/ui/VOverlay.java b/client/src/com/vaadin/client/ui/VOverlay.java index 9845e89dab..3649afc74f 100644 --- a/client/src/com/vaadin/client/ui/VOverlay.java +++ b/client/src/com/vaadin/client/ui/VOverlay.java @@ -44,6 +44,7 @@ import com.vaadin.client.BrowserInfo; import com.vaadin.client.ComponentConnector; import com.vaadin.client.ComputedStyle; import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; /** * <p> @@ -672,7 +673,7 @@ public class VOverlay extends PopupPanel implements CloseHandler<PopupPanel> { // IE9 and IE10 have a bug, when resize an a element with box-shadow. // IE9 and IE10 need explicit update to remove extra box-shadows if (BrowserInfo.get().isIE9() || BrowserInfo.get().isIE10()) { - Util.forceIERedraw(getElement()); + WidgetUtil.forceIERedraw(getElement()); } } diff --git a/client/src/com/vaadin/client/ui/VPopupView.java b/client/src/com/vaadin/client/ui/VPopupView.java index 1923fc55e6..0f4e68acab 100644 --- a/client/src/com/vaadin/client/ui/VPopupView.java +++ b/client/src/com/vaadin/client/ui/VPopupView.java @@ -33,7 +33,14 @@ import com.google.gwt.event.logical.shared.CloseHandler; import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; -import com.google.gwt.user.client.ui.*; +import com.google.gwt.user.client.ui.Focusable; +import com.google.gwt.user.client.ui.HTML; +import com.google.gwt.user.client.ui.HasEnabled; +import com.google.gwt.user.client.ui.HasWidgets; +import com.google.gwt.user.client.ui.Label; +import com.google.gwt.user.client.ui.PopupPanel; +import com.google.gwt.user.client.ui.RootPanel; +import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.ComponentConnector; import com.vaadin.client.DeferredWorker; diff --git a/client/src/com/vaadin/client/ui/VProgressBar.java b/client/src/com/vaadin/client/ui/VProgressBar.java index 8d23d0c36d..348791728f 100644 --- a/client/src/com/vaadin/client/ui/VProgressBar.java +++ b/client/src/com/vaadin/client/ui/VProgressBar.java @@ -21,8 +21,7 @@ import com.google.gwt.dom.client.Style.Unit; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.ui.HasEnabled; import com.google.gwt.user.client.ui.Widget; -import com.vaadin.client.ApplicationConnection; -import com.vaadin.shared.ui.progressindicator.ProgressBarState; +import com.vaadin.client.StyleConstants; /** * Widget for showing the current progress of a long running task. @@ -37,6 +36,8 @@ import com.vaadin.shared.ui.progressindicator.ProgressBarState; */ public class VProgressBar extends Widget implements HasEnabled { + public static final String PRIMARY_STYLE_NAME = "v-progressbar"; + Element wrapper = DOM.createDiv(); Element indicator = DOM.createDiv(); @@ -49,7 +50,7 @@ public class VProgressBar extends Widget implements HasEnabled { getElement().appendChild(wrapper); wrapper.appendChild(indicator); - setStylePrimaryName(ProgressBarState.PRIMARY_STYLE_NAME); + setStylePrimaryName(PRIMARY_STYLE_NAME); } /* @@ -92,8 +93,9 @@ public class VProgressBar extends Widget implements HasEnabled { @Override public void setEnabled(boolean enabled) { - this.enabled = enabled; - setStyleName(ApplicationConnection.DISABLED_CLASSNAME, !enabled); + if (this.enabled != enabled) { + this.enabled = enabled; + setStyleName(StyleConstants.DISABLED, !enabled); + } } - } diff --git a/client/src/com/vaadin/client/ui/VScrollTable.java b/client/src/com/vaadin/client/ui/VScrollTable.java index 9a70653a15..927e2c31db 100644 --- a/client/src/com/vaadin/client/ui/VScrollTable.java +++ b/client/src/com/vaadin/client/ui/VScrollTable.java @@ -91,6 +91,7 @@ import com.vaadin.client.UIDL; import com.vaadin.client.Util; import com.vaadin.client.VConsole; import com.vaadin.client.VTooltip; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.VScrollTable.VScrollTableBody.VScrollTableRow; import com.vaadin.client.ui.dd.DDUtil; import com.vaadin.client.ui.dd.VAbstractDropHandler; @@ -510,8 +511,8 @@ public class VScrollTable extends FlowPanel implements HasWidgets, @Override public void showContextMenu(Event event) { - int left = Util.getTouchOrMouseClientX(event); - int top = Util.getTouchOrMouseClientY(event); + int left = WidgetUtil.getTouchOrMouseClientX(event); + int top = WidgetUtil.getTouchOrMouseClientY(event); boolean menuShown = handleBodyContextMenu(left, top); if (menuShown) { event.stopPropagation(); @@ -796,8 +797,8 @@ public class VScrollTable extends FlowPanel implements HasWidgets, // Event's reported target not always correct if event // capture is in use - Element elementUnderMouse = Util.getElementUnderMouse(event - .getNativeEvent()); + Element elementUnderMouse = WidgetUtil + .getElementUnderMouse(event.getNativeEvent()); if (lastMouseDownTarget != null && lastMouseDownTarget.isOrHasChild(elementUnderMouse)) { mouseUpPreviewMatched = true; @@ -2253,7 +2254,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, int w = total; w += scrollBody.getCellExtraWidth() * visibleColOrder.length; if (willHaveScrollbarz) { - w += Util.getNativeScrollbarSize(); + w += WidgetUtil.getNativeScrollbarSize(); } setContentWidth(w); } @@ -2266,7 +2267,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, availW -= scrollBody.getCellExtraWidth() * visibleColOrder.length; if (willHaveScrollbarz) { - availW -= Util.getNativeScrollbarSize(); + availW -= WidgetUtil.getNativeScrollbarSize(); } // TODO refactor this code to be the same as in resize timer @@ -2438,10 +2439,10 @@ public class VScrollTable extends FlowPanel implements HasWidgets, } boolean needsSpaceForHorizontalSrollbar = (total > availW); if (needsSpaceForHorizontalSrollbar) { - bodyHeight += Util.getNativeScrollbarSize(); + bodyHeight += WidgetUtil.getNativeScrollbarSize(); } scrollBodyPanel.setHeight(bodyHeight + "px"); - Util.runWebkitOverflowAutoFix(scrollBodyPanel.getElement()); + WidgetUtil.runWebkitOverflowAutoFix(scrollBodyPanel.getElement()); } isNewBody = false; @@ -2472,7 +2473,8 @@ public class VScrollTable extends FlowPanel implements HasWidgets, * Ensures the column alignments are correct at initial loading. <br/> * (child components widths are correct) */ - Util.runWebkitOverflowAutoFixDeferred(scrollBodyPanel.getElement()); + WidgetUtil.runWebkitOverflowAutoFixDeferred(scrollBodyPanel + .getElement()); hadScrollBars = willHaveScrollbarz; } @@ -3131,7 +3133,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, case Event.ONTOUCHSTART: case Event.ONMOUSEDOWN: if (columnReordering - && Util.isTouchEventOrLeftMouseButton(event)) { + && WidgetUtil.isTouchEventOrLeftMouseButton(event)) { if (event.getTypeInt() == Event.ONTOUCHSTART) { /* * prevent using this event in e.g. scrolling @@ -3151,11 +3153,11 @@ public class VScrollTable extends FlowPanel implements HasWidgets, case Event.ONTOUCHEND: case Event.ONTOUCHCANCEL: if (columnReordering - && Util.isTouchEventOrLeftMouseButton(event)) { + && WidgetUtil.isTouchEventOrLeftMouseButton(event)) { dragging = false; DOM.releaseCapture(getElement()); - if (Util.isTouchEvent(event)) { + if (WidgetUtil.isTouchEvent(event)) { /* * Prevent using in e.g. scrolling and prevent generated * events. @@ -3181,7 +3183,8 @@ public class VScrollTable extends FlowPanel implements HasWidgets, if (!moved) { // mouse event was a click to header -> sort column - if (sortable && Util.isTouchEventOrLeftMouseButton(event)) { + if (sortable + && WidgetUtil.isTouchEventOrLeftMouseButton(event)) { if (sortColumn.equals(cid)) { // just toggle order client.updateVariable(paintableId, "sortascending", @@ -3203,7 +3206,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, rowRequestHandler.run(); // run immediately } fireHeaderClickedEvent(event); - if (Util.isTouchEvent(event)) { + if (WidgetUtil.isTouchEvent(event)) { /* * Prevent using in e.g. scrolling and prevent generated * events. @@ -3219,7 +3222,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, break; case Event.ONTOUCHMOVE: case Event.ONMOUSEMOVE: - if (dragging && Util.isTouchEventOrLeftMouseButton(event)) { + if (dragging && WidgetUtil.isTouchEventOrLeftMouseButton(event)) { if (event.getTypeInt() == Event.ONTOUCHMOVE) { /* * prevent using this event in e.g. scrolling @@ -3231,7 +3234,8 @@ public class VScrollTable extends FlowPanel implements HasWidgets, moved = true; } - final int clientX = Util.getTouchOrMouseClientX(event); + final int clientX = WidgetUtil + .getTouchOrMouseClientX(event); final int x = clientX + tHead.hTableWrapper.getScrollLeft(); int slotX = headerX; closestSlot = colIndex; @@ -3269,7 +3273,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, private void onResizeEvent(Event event) { switch (DOM.eventGetType(event)) { case Event.ONMOUSEDOWN: - if (!Util.isTouchEventOrLeftMouseButton(event)) { + if (!WidgetUtil.isTouchEventOrLeftMouseButton(event)) { return; } isResizing = true; @@ -3280,7 +3284,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, DOM.eventPreventDefault(event); break; case Event.ONMOUSEUP: - if (!Util.isTouchEventOrLeftMouseButton(event)) { + if (!WidgetUtil.isTouchEventOrLeftMouseButton(event)) { return; } isResizing = false; @@ -3297,7 +3301,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, fireColumnResizeEvent(cid, originalWidth, getColWidth(cid)); break; case Event.ONMOUSEMOVE: - if (!Util.isTouchEventOrLeftMouseButton(event)) { + if (!WidgetUtil.isTouchEventOrLeftMouseButton(event)) { return; } if (isResizing) { @@ -4739,7 +4743,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, */ public int getRequiredHeight() { return preSpacer.getOffsetHeight() + postSpacer.getOffsetHeight() - + Util.getRequiredHeight(table); + + WidgetUtil.getRequiredHeight(table); } private void constructDOM() { @@ -5953,8 +5957,8 @@ public class VScrollTable extends FlowPanel implements HasWidgets, if (!BrowserInfo.get().isAndroid()) { event.preventDefault(); event.stopPropagation(); - Util.simulateClickFromTouchEvent(touchStart, - this); + WidgetUtil.simulateClickFromTouchEvent( + touchStart, this); } touchStart = null; } @@ -6013,7 +6017,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, */ if (mouseUpPreviewMatched && lastMouseDownTarget != null - && lastMouseDownTarget == getElementTdOrTr(Util + && lastMouseDownTarget == getElementTdOrTr(WidgetUtil .getElementUnderMouse(event))) { // "Click" with left, right or middle button @@ -6140,7 +6144,8 @@ public class VScrollTable extends FlowPanel implements HasWidgets, * Touch has not been handled as neither context or * drag start, handle it as a click. */ - Util.simulateClickFromTouchEvent(touchStart, this); + WidgetUtil.simulateClickFromTouchEvent(touchStart, + this); touchStart = null; } touchContextProvider.cancel(); @@ -6242,7 +6247,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, * the corresponding mouseup event if it's on a * different part of the page. */ - lastMouseDownTarget = getElementTdOrTr(Util + lastMouseDownTarget = getElementTdOrTr(WidgetUtil .getElementUnderMouse(event)); mouseUpPreviewMatched = false; mouseUpEventPreviewRegistration = Event @@ -6387,7 +6392,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, private Element getElementTdOrTr(Element element) { - Widget widget = Util.findWidget(element, null); + Widget widget = WidgetUtil.findWidget(element, null); if (widget != this) { /* @@ -6414,9 +6419,9 @@ public class VScrollTable extends FlowPanel implements HasWidgets, public void showContextMenu(Event event) { if (enabled && actionKeys != null) { // Show context menu if there are registered action handlers - int left = Util.getTouchOrMouseClientX(event) + int left = WidgetUtil.getTouchOrMouseClientX(event) + Window.getScrollLeft(); - int top = Util.getTouchOrMouseClientY(event) + int top = WidgetUtil.getTouchOrMouseClientY(event) + Window.getScrollTop(); showContextMenu(left, top); } @@ -6695,8 +6700,9 @@ public class VScrollTable extends FlowPanel implements HasWidgets, .getVisibleCellCount(); ix++) { spanWidth += tHead.getHeaderCell(ix).getOffsetWidth(); } - Util.setWidthExcludingPaddingAndBorder((Element) getElement() - .getChild(cellIx), spanWidth, 13, false); + WidgetUtil.setWidthExcludingPaddingAndBorder( + (Element) getElement().getChild(cellIx), spanWidth, 13, + false); } } @@ -6894,7 +6900,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, int totalExtraWidth = scrollBody.getCellExtraWidth() * visibleCellCount; if (willHaveScrollbars()) { - totalExtraWidth += Util.getNativeScrollbarSize(); + totalExtraWidth += WidgetUtil.getNativeScrollbarSize(); // if there will be vertical scrollbar, let's enable it scrollBodyPanel.getElement().getStyle().clearOverflowY(); } else { @@ -7045,7 +7051,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, int bodyHeight = scrollBody.getRequiredHeight(); boolean needsSpaceForHorizontalScrollbar = (availW < usedMinimumWidth); if (needsSpaceForHorizontalScrollbar) { - bodyHeight += Util.getNativeScrollbarSize(); + bodyHeight += WidgetUtil.getNativeScrollbarSize(); } int heightBefore = getOffsetHeight(); scrollBodyPanel.setHeight(bodyHeight + "px"); @@ -7090,7 +7096,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, */ private int getBorderWidth() { if (borderWidth < 0) { - borderWidth = Util.measureHorizontalPaddingAndBorder( + borderWidth = WidgetUtil.measureHorizontalPaddingAndBorder( scrollBodyPanel.getElement(), 2); if (borderWidth < 0) { borderWidth = 0; @@ -7433,7 +7439,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, Class<? extends Widget> clazz = getRowClass(); VScrollTableRow row = null; if (clazz != null) { - row = Util.findWidget(elementOver, clazz); + row = WidgetUtil.findWidget(elementOver, clazz); } if (row != null) { dropDetails.overkey = row.rowKey; @@ -7625,7 +7631,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, * FIXME The next line doesn't always do what expected, because if the * row is not in the DOM it won't scroll to it. */ - Util.scrollIntoViewVertically(row.getElement()); + WidgetUtil.scrollIntoViewVertically(row.getElement()); } /** @@ -7926,7 +7932,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, * ...and sometimes it sends blur events even though the focus * handler is still active. (#10464) */ - Element focusedElement = Util.getIEFocusedElement(); + Element focusedElement = WidgetUtil.getFocusedElement(); if (Util.getConnectorForElement(client, getParent(), focusedElement) == this && focusedElement != null && focusedElement != scrollBodyPanel.getFocusElement()) { @@ -8235,7 +8241,7 @@ public class VScrollTable extends FlowPanel implements HasWidgets, @Override public String getSubPartName(com.google.gwt.user.client.Element subElement) { - Widget widget = Util.findWidget(subElement, null); + Widget widget = WidgetUtil.findWidget(subElement, null); if (widget instanceof HeaderCell) { return SUBPART_HEADER + "[" + tHead.visibleCells.indexOf(widget) + "]"; diff --git a/client/src/com/vaadin/client/ui/VSlider.java b/client/src/com/vaadin/client/ui/VSlider.java index 27c8522f37..f5769ddf74 100644 --- a/client/src/com/vaadin/client/ui/VSlider.java +++ b/client/src/com/vaadin/client/ui/VSlider.java @@ -34,7 +34,7 @@ import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.HasValue; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.BrowserInfo; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.shared.ui.slider.SliderOrientation; public class VSlider extends SimpleFocusablePanel implements Field, @@ -299,7 +299,7 @@ public class VSlider extends SimpleFocusablePanel implements Field, } else if (DOM.eventGetType(event) == Event.ONMOUSEDOWN) { feedbackPopup.show(); } - if (Util.isTouchEvent(event)) { + if (WidgetUtil.isTouchEvent(event)) { event.preventDefault(); // avoid simulated events event.stopPropagation(); } @@ -423,9 +423,9 @@ public class VSlider extends SimpleFocusablePanel implements Field, */ protected int getEventPosition(Event event) { if (isVertical()) { - return Util.getTouchOrMouseClientY(event); + return WidgetUtil.getTouchOrMouseClientY(event); } else { - return Util.getTouchOrMouseClientX(event); + return WidgetUtil.getTouchOrMouseClientX(event); } } diff --git a/client/src/com/vaadin/client/ui/VTabsheet.java b/client/src/com/vaadin/client/ui/VTabsheet.java index 96af09bb32..c8984ece51 100644 --- a/client/src/com/vaadin/client/ui/VTabsheet.java +++ b/client/src/com/vaadin/client/ui/VTabsheet.java @@ -63,7 +63,7 @@ import com.vaadin.client.BrowserInfo; import com.vaadin.client.ComponentConnector; import com.vaadin.client.Focusable; import com.vaadin.client.TooltipInfo; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.VCaption; import com.vaadin.client.VTooltip; import com.vaadin.client.ui.aria.AriaHelper; @@ -415,7 +415,7 @@ public class VTabsheet extends VTabsheetBase implements Focusable, SubPartAware public int getRequiredWidth() { int width = super.getRequiredWidth(); if (closeButton != null) { - width += Util.getRequiredWidth(closeButton); + width += WidgetUtil.getRequiredWidth(closeButton); } return width; } @@ -1330,7 +1330,7 @@ public class VTabsheet extends VTabsheetBase implements Focusable, SubPartAware /** For internal use only. May be removed or replaced in the future. */ public int getContentAreaBorderWidth() { - return Util.measureHorizontalBorder(contentNode); + return WidgetUtil.measureHorizontalBorder(contentNode); } @Override diff --git a/client/src/com/vaadin/client/ui/VTextArea.java b/client/src/com/vaadin/client/ui/VTextArea.java index bb48b29e61..50930f2fee 100644 --- a/client/src/com/vaadin/client/ui/VTextArea.java +++ b/client/src/com/vaadin/client/ui/VTextArea.java @@ -32,7 +32,7 @@ import com.google.gwt.user.client.Command; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; import com.vaadin.client.BrowserInfo; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.dd.DragImageModifier; /** @@ -310,7 +310,7 @@ public class VTextArea extends VTextField implements DragImageModifier { // and reattach the whole TextArea. // Webkit fails to properly reflow the text when enabling wrapping, // same workaround - Util.detachAttach(getElement()); + WidgetUtil.detachAttach(getElement()); } this.wordwrap = wordwrap; } diff --git a/client/src/com/vaadin/client/ui/VTextField.java b/client/src/com/vaadin/client/ui/VTextField.java index b402ced218..1554bd1a22 100644 --- a/client/src/com/vaadin/client/ui/VTextField.java +++ b/client/src/com/vaadin/client/ui/VTextField.java @@ -34,7 +34,7 @@ import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.ui.TextBoxBase; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.BrowserInfo; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.shared.EventId; import com.vaadin.shared.ui.textfield.TextFieldConstants; @@ -422,7 +422,7 @@ public class VTextField extends TextBoxBase implements Field, ChangeHandler, * @return true iff the value was updated */ protected boolean updateCursorPosition() { - if (Util.isAttachedAndDisplayed(this)) { + if (WidgetUtil.isAttachedAndDisplayed(this)) { int cursorPos = getCursorPos(); if (lastCursorPos != cursorPos) { client.updateVariable(paintableId, diff --git a/client/src/com/vaadin/client/ui/VTree.java b/client/src/com/vaadin/client/ui/VTree.java index 82ffaced1f..6539eb49a9 100644 --- a/client/src/com/vaadin/client/ui/VTree.java +++ b/client/src/com/vaadin/client/ui/VTree.java @@ -60,6 +60,7 @@ import com.vaadin.client.ConnectorMap; import com.vaadin.client.MouseEventDetailsBuilder; import com.vaadin.client.UIDL; import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.aria.AriaHelper; import com.vaadin.client.ui.aria.HandlesAriaCaption; import com.vaadin.client.ui.dd.DDUtil; @@ -346,7 +347,7 @@ public class VTree extends FocusElementPanel implements VHasDropHandler, } private String findCurrentMouseOverKey(Element elementOver) { - TreeNode treeNode = Util.findWidget(elementOver, TreeNode.class); + TreeNode treeNode = WidgetUtil.findWidget(elementOver, TreeNode.class); return treeNode == null ? null : treeNode.key; } @@ -1132,7 +1133,7 @@ public class VTree extends FocusElementPanel implements VHasDropHandler, * Scrolls the caption into view */ public void scrollIntoView() { - Util.scrollIntoViewVertically(nodeCaptionDiv); + WidgetUtil.scrollIntoViewVertically(nodeCaptionDiv); } public void setIcon(String iconUrl, String altText) { @@ -2143,7 +2144,7 @@ public class VTree extends FocusElementPanel implements VHasDropHandler, return "fe"; } - TreeNode treeNode = Util.findWidget(subElement, TreeNode.class); + TreeNode treeNode = WidgetUtil.findWidget(subElement, TreeNode.class); if (treeNode == null) { // Did not click on a node, let somebody else take care of the // locator string diff --git a/client/src/com/vaadin/client/ui/VTreeTable.java b/client/src/com/vaadin/client/ui/VTreeTable.java index 9e5940a2f2..0ba84af4bb 100644 --- a/client/src/com/vaadin/client/ui/VTreeTable.java +++ b/client/src/com/vaadin/client/ui/VTreeTable.java @@ -37,7 +37,7 @@ import com.google.gwt.user.client.Event; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.ComputedStyle; import com.vaadin.client.UIDL; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.VTreeTable.VTreeTableScrollBody.VTreeTableRow; public class VTreeTable extends VScrollTable { @@ -418,8 +418,9 @@ public class VTreeTable extends VScrollTable { .getVisibleCellCount(); ix++) { spanWidth += tHead.getHeaderCell(ix).getOffsetWidth(); } - Util.setWidthExcludingPaddingAndBorder((Element) getElement() - .getChild(cellIx), spanWidth, 13, false); + WidgetUtil.setWidthExcludingPaddingAndBorder( + (Element) getElement().getChild(cellIx), spanWidth, 13, + false); } } diff --git a/client/src/com/vaadin/client/ui/VTwinColSelect.java b/client/src/com/vaadin/client/ui/VTwinColSelect.java index 3987460989..853bd8d456 100644 --- a/client/src/com/vaadin/client/ui/VTwinColSelect.java +++ b/client/src/com/vaadin/client/ui/VTwinColSelect.java @@ -37,9 +37,9 @@ import com.google.gwt.user.client.ui.FlowPanel; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.ListBox; import com.google.gwt.user.client.ui.Panel; -import com.vaadin.client.ApplicationConnection; +import com.vaadin.client.StyleConstants; import com.vaadin.client.UIDL; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.shared.ui.twincolselect.TwinColSelectConstants; public class VTwinColSelect extends VOptionGroupBase implements KeyDownHandler, @@ -352,7 +352,7 @@ public class VTwinColSelect extends VOptionGroupBase implements KeyDownHandler, /** For internal use only. May be removed or replaced in the future. */ public void setInternalHeights() { - int captionHeight = Util.getRequiredHeight(captionWrapper); + int captionHeight = WidgetUtil.getRequiredHeight(captionWrapper); int totalHeight = getOffsetHeight(); String selectHeight = (totalHeight - captionHeight) + "px"; @@ -394,10 +394,10 @@ public class VTwinColSelect extends VOptionGroupBase implements KeyDownHandler, /** For internal use only. May be removed or replaced in the future. */ public void setInternalWidths() { getElement().getStyle().setPosition(Position.RELATIVE); - int bordersAndPaddings = Util.measureHorizontalPaddingAndBorder( + int bordersAndPaddings = WidgetUtil.measureHorizontalPaddingAndBorder( buttons.getElement(), 0); - int buttonWidth = Util.getRequiredWidth(buttons); + int buttonWidth = WidgetUtil.getRequiredWidth(buttons); int totalWidth = getOffsetWidth(); int spaceForSelect = (totalWidth - buttonWidth - bordersAndPaddings) / 2; @@ -429,8 +429,8 @@ public class VTwinColSelect extends VOptionGroupBase implements KeyDownHandler, selections.setEnabled(enabled); add.setEnabled(enabled); remove.setEnabled(enabled); - add.setStyleName(ApplicationConnection.DISABLED_CLASSNAME, !enabled); - remove.setStyleName(ApplicationConnection.DISABLED_CLASSNAME, !enabled); + add.setStyleName(StyleConstants.DISABLED, !enabled); + remove.setStyleName(StyleConstants.DISABLED, !enabled); } @Override @@ -609,14 +609,14 @@ public class VTwinColSelect extends VOptionGroupBase implements KeyDownHandler, if (options.getElement() == subElement) { return SUBPART_OPTION_SELECT; } else { - int idx = Util.getChildElementIndex(subElement); + int idx = WidgetUtil.getChildElementIndex(subElement); return SUBPART_OPTION_SELECT_ITEM + idx; } } else if (selections.getElement().isOrHasChild(subElement)) { if (selections.getElement() == subElement) { return SUBPART_SELECTION_SELECT; } else { - int idx = Util.getChildElementIndex(subElement); + int idx = WidgetUtil.getChildElementIndex(subElement); return SUBPART_SELECTION_SELECT_ITEM + idx; } } else if (add.getElement().isOrHasChild(subElement)) { diff --git a/client/src/com/vaadin/client/ui/VUI.java b/client/src/com/vaadin/client/ui/VUI.java index eae4f6319d..0c1b83ab0f 100644 --- a/client/src/com/vaadin/client/ui/VUI.java +++ b/client/src/com/vaadin/client/ui/VUI.java @@ -44,7 +44,7 @@ import com.vaadin.client.ConnectorMap; import com.vaadin.client.Focusable; import com.vaadin.client.LayoutManager; import com.vaadin.client.Profiler; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.VConsole; import com.vaadin.client.ui.ShortcutActionHandler.ShortcutActionHandlerOwner; import com.vaadin.client.ui.TouchScrollDelegate.TouchScrollHandler; @@ -501,7 +501,7 @@ public class VUI extends SimplePanel implements ResizeHandler, * @param focusedElement */ public void storeFocus() { - storedFocus = Util.getFocusedElement(); + storedFocus = WidgetUtil.getFocusedElement(); } /** diff --git a/client/src/com/vaadin/client/ui/VUpload.java b/client/src/com/vaadin/client/ui/VUpload.java index 42fb08fb3c..dff45a6951 100644 --- a/client/src/com/vaadin/client/ui/VUpload.java +++ b/client/src/com/vaadin/client/ui/VUpload.java @@ -36,6 +36,7 @@ import com.google.gwt.user.client.ui.Panel; import com.google.gwt.user.client.ui.SimplePanel; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.BrowserInfo; +import com.vaadin.client.StyleConstants; import com.vaadin.client.VConsole; import com.vaadin.client.ui.upload.UploadIFrameOnloadStrategy; @@ -211,8 +212,7 @@ public class VUpload extends SimplePanel { private void setEnabledForSubmitButton(boolean enabled) { submitButton.setEnabled(enabled); - submitButton.setStyleName(ApplicationConnection.DISABLED_CLASSNAME, - !enabled); + submitButton.setStyleName(StyleConstants.DISABLED, !enabled); } /** diff --git a/client/src/com/vaadin/client/ui/VWindow.java b/client/src/com/vaadin/client/ui/VWindow.java index 786ecf1267..6977cf9e7f 100644 --- a/client/src/com/vaadin/client/ui/VWindow.java +++ b/client/src/com/vaadin/client/ui/VWindow.java @@ -16,7 +16,7 @@ package com.vaadin.client.ui; -import static com.vaadin.client.Util.isFocusedElementEditable; +import static com.vaadin.client.WidgetUtil.isFocusedElementEditable; import java.util.ArrayList; import java.util.Arrays; @@ -62,7 +62,7 @@ import com.vaadin.client.ComponentConnector; import com.vaadin.client.ConnectorMap; import com.vaadin.client.Focusable; import com.vaadin.client.LayoutManager; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.debug.internal.VDebugWindow; import com.vaadin.client.ui.ShortcutActionHandler.ShortcutActionHandlerOwner; import com.vaadin.client.ui.aria.AriaHelper; @@ -581,7 +581,8 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, * ticket #11994 which was changing the size to 110% was replaced * with this due to ticket #12943 */ - Util.runWebkitOverflowAutoFix(contents.getFirstChildElement()); + WidgetUtil + .runWebkitOverflowAutoFix(contents.getFirstChildElement()); } } @@ -884,7 +885,7 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, if (asHtml) { html = c == null ? "" : c; } else { - html = Util.escapeHTML(c); + html = WidgetUtil.escapeHTML(c); } // Provide information to assistive device users that a sub window was // opened @@ -1043,7 +1044,7 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, } private void onResizeEvent(Event event) { - if (resizable && Util.isTouchEventOrLeftMouseButton(event)) { + if (resizable && WidgetUtil.isTouchEventOrLeftMouseButton(event)) { switch (event.getTypeInt()) { case Event.ONMOUSEDOWN: case Event.ONTOUCHSTART: @@ -1055,8 +1056,8 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, resizeBox.getStyle().setVisibility(Visibility.HIDDEN); } resizing = true; - startX = Util.getTouchOrMouseClientX(event); - startY = Util.getTouchOrMouseClientY(event); + startX = WidgetUtil.getTouchOrMouseClientX(event); + startY = WidgetUtil.getTouchOrMouseClientY(event); origW = getElement().getOffsetWidth(); origH = getElement().getOffsetHeight(); DOM.setCapture(getElement()); @@ -1122,8 +1123,8 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, return; } - int w = Util.getTouchOrMouseClientX(event) - startX + origW; - int h = Util.getTouchOrMouseClientY(event) - startY + origH; + int w = WidgetUtil.getTouchOrMouseClientX(event) - startX + origW; + int h = WidgetUtil.getTouchOrMouseClientY(event) - startY + origH; w = Math.max(w, getMinWidth()); h = Math.max(h, getMinHeight()); @@ -1186,7 +1187,7 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, } private void onDragEvent(Event event) { - if (!Util.isTouchEventOrLeftMouseButton(event)) { + if (!WidgetUtil.isTouchEventOrLeftMouseButton(event)) { return; } @@ -1221,9 +1222,9 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, centered = false; if (cursorInsideBrowserContentArea(event)) { // Only drag while cursor is inside the browser client area - final int x = Util.getTouchOrMouseClientX(event) - startX + final int x = WidgetUtil.getTouchOrMouseClientX(event) - startX + origX; - final int y = Util.getTouchOrMouseClientY(event) - startY + final int y = WidgetUtil.getTouchOrMouseClientY(event) - startY + origY; setPopupPosition(x, y); } @@ -1235,8 +1236,8 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, if (draggable) { showDraggingCurtain(); dragging = true; - startX = Util.getTouchOrMouseClientX(event); - startY = Util.getTouchOrMouseClientY(event); + startX = WidgetUtil.getTouchOrMouseClientX(event); + startY = WidgetUtil.getTouchOrMouseClientY(event); origX = DOM.getAbsoluteLeft(getElement()); origY = DOM.getAbsoluteTop(getElement()); DOM.setCapture(getElement()); @@ -1284,7 +1285,7 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner, if (!DOM.isOrHasChild(getTopmostWindow().getElement(), target)) { // not within the modal window, but let's see if it's in the // debug window - Widget w = Util.findWidget(target, null); + Widget w = WidgetUtil.findWidget(target, null); while (w != null) { if (w instanceof VDebugWindow) { return true; // allow debug-window clicks diff --git a/client/src/com/vaadin/client/ui/accordion/AccordionConnector.java b/client/src/com/vaadin/client/ui/accordion/AccordionConnector.java index 72aa2dbdfd..949e19071c 100644 --- a/client/src/com/vaadin/client/ui/accordion/AccordionConnector.java +++ b/client/src/com/vaadin/client/ui/accordion/AccordionConnector.java @@ -17,7 +17,7 @@ package com.vaadin.client.ui.accordion; import com.vaadin.client.ComponentConnector; import com.vaadin.client.ConnectorHierarchyChangeEvent; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.ui.SimpleManagedLayout; import com.vaadin.client.ui.VAccordion; @@ -106,7 +106,8 @@ public class AccordionConnector extends TabsheetBaseConnector implements usedPixels += item.getCaptionHeight(); } else { // This includes the captionNode borders - usedPixels += Util.getRequiredHeight(item.getElement()); + usedPixels += WidgetUtil.getRequiredHeight(item + .getElement()); } } int rootElementInnerHeight = getLayoutManager().getInnerHeight( diff --git a/client/src/com/vaadin/client/ui/calendar/CalendarConnector.java b/client/src/com/vaadin/client/ui/calendar/CalendarConnector.java index 8c92ef1233..e9bbf2015c 100644 --- a/client/src/com/vaadin/client/ui/calendar/CalendarConnector.java +++ b/client/src/com/vaadin/client/ui/calendar/CalendarConnector.java @@ -35,7 +35,7 @@ import com.vaadin.client.ApplicationConnection; import com.vaadin.client.Paintable; import com.vaadin.client.TooltipInfo; import com.vaadin.client.UIDL; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.VConsole; import com.vaadin.client.communication.RpcProxy; import com.vaadin.client.communication.StateChangeEvent; @@ -422,7 +422,7 @@ public class CalendarConnector extends AbstractComponentConnector implements @Override public TooltipInfo getTooltipInfo(com.google.gwt.dom.client.Element element) { TooltipInfo tooltipInfo = null; - Widget w = Util.findWidget(element, null); + Widget w = WidgetUtil.findWidget(element, null); if (w instanceof HasTooltipKey) { tooltipInfo = GWT.create(TooltipInfo.class); String title = tooltips.get(((HasTooltipKey) w).getTooltipKey()); diff --git a/client/src/com/vaadin/client/ui/calendar/schedule/DateCell.java b/client/src/com/vaadin/client/ui/calendar/schedule/DateCell.java index 448083ba26..39d516b694 100644 --- a/client/src/com/vaadin/client/ui/calendar/schedule/DateCell.java +++ b/client/src/com/vaadin/client/ui/calendar/schedule/DateCell.java @@ -43,7 +43,7 @@ import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.ui.Widget; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; public class DateCell extends FocusableComplexPanel implements MouseDownHandler, MouseMoveHandler, MouseUpHandler, KeyDownHandler, @@ -201,7 +201,7 @@ public class DateCell extends FocusableComplexPanel implements addStyleDependentName("Hsized"); width = getOffsetWidth() - - Util.measureHorizontalBorder(getElement()); + - WidgetUtil.measureHorizontalBorder(getElement()); // Update moveWidth for any DateCellDayEvent child updateEventCellsWidth(); recalculateEventWidths(); @@ -338,7 +338,7 @@ public class DateCell extends FocusableComplexPanel implements } public int getSlotBorder() { - return Util.measureVerticalBorder(slotElements[0]); + return WidgetUtil.measureVerticalBorder(slotElements[0]); } private void drawDayEvents(List<DateCellGroup> groups) { diff --git a/client/src/com/vaadin/client/ui/calendar/schedule/DateCellContainer.java b/client/src/com/vaadin/client/ui/calendar/schedule/DateCellContainer.java index 82af89c794..26f5951987 100644 --- a/client/src/com/vaadin/client/ui/calendar/schedule/DateCellContainer.java +++ b/client/src/com/vaadin/client/ui/calendar/schedule/DateCellContainer.java @@ -23,7 +23,7 @@ import com.google.gwt.event.dom.client.MouseUpEvent; import com.google.gwt.event.dom.client.MouseUpHandler; import com.google.gwt.user.client.ui.FlowPanel; import com.google.gwt.user.client.ui.Widget; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.VCalendar; /** @@ -48,7 +48,7 @@ public class DateCellContainer extends FlowPanel implements MouseDownHandler, public static int measureBorderWidth(DateCellContainer dc) { if (borderWidth == -1) { - borderWidth = Util.measureHorizontalBorder(dc.getElement()); + borderWidth = WidgetUtil.measureHorizontalBorder(dc.getElement()); } return borderWidth; } diff --git a/client/src/com/vaadin/client/ui/calendar/schedule/DateCellDayEvent.java b/client/src/com/vaadin/client/ui/calendar/schedule/DateCellDayEvent.java index 8b08e9bc7a..1a54fe0454 100644 --- a/client/src/com/vaadin/client/ui/calendar/schedule/DateCellDayEvent.java +++ b/client/src/com/vaadin/client/ui/calendar/schedule/DateCellDayEvent.java @@ -41,7 +41,7 @@ import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.ui.HorizontalPanel; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.shared.ui.calendar.DateConstants; /** @@ -190,7 +190,7 @@ public class DateCellDayEvent extends FocusableHTML implements if (dateCell.weekgrid.getCalendar().isEventCaptionAsHtml()) { htmlOrText = calendarEvent.getCaption(); } else { - htmlOrText = Util.escapeHTML(calendarEvent.getCaption()); + htmlOrText = WidgetUtil.escapeHTML(calendarEvent.getCaption()); } if (bigMode) { diff --git a/client/src/com/vaadin/client/ui/calendar/schedule/WeekGrid.java b/client/src/com/vaadin/client/ui/calendar/schedule/WeekGrid.java index 545ddadc52..aecaff1931 100644 --- a/client/src/com/vaadin/client/ui/calendar/schedule/WeekGrid.java +++ b/client/src/com/vaadin/client/ui/calendar/schedule/WeekGrid.java @@ -31,7 +31,7 @@ import com.google.gwt.user.client.ui.ScrollPanel; import com.google.gwt.user.client.ui.SimplePanel; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.DateTimeService; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.VCalendar; import com.vaadin.shared.ui.calendar.DateConstants; @@ -160,7 +160,7 @@ public class WeekGrid extends SimplePanel { // Otherwise the scroll wrapper is somehow too narrow = horizontal // scroll wrapper.setWidth(content.getOffsetWidth() - + Util.getNativeScrollbarSize() + "px"); + + WidgetUtil.getNativeScrollbarSize() + "px"); this.width = content.getOffsetWidth() - timebar.getOffsetWidth(); @@ -169,7 +169,7 @@ public class WeekGrid extends SimplePanel { - timebar.getOffsetWidth(); if (isVerticalScrollable() && width != -1) { - this.width = this.width - Util.getNativeScrollbarSize(); + this.width = this.width - WidgetUtil.getNativeScrollbarSize(); } updateCellWidths(); } diff --git a/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarMonthDropHandler.java b/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarMonthDropHandler.java index 9cab421200..39e08e9d70 100644 --- a/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarMonthDropHandler.java +++ b/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarMonthDropHandler.java @@ -17,7 +17,7 @@ package com.vaadin.client.ui.calendar.schedule.dd; import com.google.gwt.dom.client.Element; import com.google.gwt.user.client.DOM; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.calendar.CalendarConnector; import com.vaadin.client.ui.calendar.schedule.SimpleDayCell; import com.vaadin.client.ui.dd.VAcceptCallback; @@ -51,7 +51,7 @@ public class CalendarMonthDropHandler extends CalendarDropHandler { protected void dragAccepted(VDragEvent drag) { deEmphasis(); currentTargetElement = drag.getElementOver(); - currentTargetDay = Util.findWidget(currentTargetElement, + currentTargetDay = WidgetUtil.findWidget(currentTargetElement, SimpleDayCell.class); emphasis(); } diff --git a/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarWeekDropHandler.java b/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarWeekDropHandler.java index 853e4b724e..e0edf21e89 100644 --- a/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarWeekDropHandler.java +++ b/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarWeekDropHandler.java @@ -17,7 +17,7 @@ package com.vaadin.client.ui.calendar.schedule.dd; import com.google.gwt.dom.client.Element; import com.google.gwt.user.client.DOM; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.calendar.CalendarConnector; import com.vaadin.client.ui.calendar.schedule.DateCell; import com.vaadin.client.ui.calendar.schedule.DateCellDayEvent; @@ -52,8 +52,8 @@ public class CalendarWeekDropHandler extends CalendarDropHandler { protected void dragAccepted(VDragEvent drag) { deEmphasis(); currentTargetElement = drag.getElementOver(); - currentTargetDay = Util - .findWidget(currentTargetElement, DateCell.class); + currentTargetDay = WidgetUtil.findWidget(currentTargetElement, + DateCell.class); emphasis(); } @@ -121,7 +121,7 @@ public class CalendarWeekDropHandler extends CalendarDropHandler { return DOM.isOrHasChild(weekGridElement, elementOver) && !DOM.isOrHasChild(timeBarElement, elementOver) && todayBarElement != elementOver - && (Util.findWidget(elementOver, DateCellDayEvent.class) == null); + && (WidgetUtil.findWidget(elementOver, DateCellDayEvent.class) == null); } /* diff --git a/client/src/com/vaadin/client/ui/dd/DDUtil.java b/client/src/com/vaadin/client/ui/dd/DDUtil.java index 77de1f9b1a..fdccd61767 100644 --- a/client/src/com/vaadin/client/ui/dd/DDUtil.java +++ b/client/src/com/vaadin/client/ui/dd/DDUtil.java @@ -18,7 +18,7 @@ package com.vaadin.client.ui.dd; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.NativeEvent; import com.google.gwt.user.client.Window; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.shared.ui.dd.HorizontalDropLocation; import com.vaadin.shared.ui.dd.VerticalDropLocation; @@ -33,7 +33,7 @@ public class DDUtil { public static VerticalDropLocation getVerticalDropLocation(Element element, int offsetHeight, NativeEvent event, double topBottomRatio) { - int clientY = Util.getTouchOrMouseClientY(event); + int clientY = WidgetUtil.getTouchOrMouseClientY(event); return getVerticalDropLocation(element, offsetHeight, clientY, topBottomRatio); } @@ -59,7 +59,7 @@ public class DDUtil { public static HorizontalDropLocation getHorizontalDropLocation( Element element, NativeEvent event, double leftRightRatio) { - int clientX = Util.getTouchOrMouseClientX(event); + int clientX = WidgetUtil.getTouchOrMouseClientX(event); // Event coordinates are relative to the viewport, element absolute // position is relative to the document. Make element position relative diff --git a/client/src/com/vaadin/client/ui/dd/VDragAndDropManager.java b/client/src/com/vaadin/client/ui/dd/VDragAndDropManager.java index 4ee19328d6..844f4c1b9c 100644 --- a/client/src/com/vaadin/client/ui/dd/VDragAndDropManager.java +++ b/client/src/com/vaadin/client/ui/dd/VDragAndDropManager.java @@ -38,7 +38,7 @@ import com.vaadin.client.ComponentConnector; import com.vaadin.client.MouseEventDetailsBuilder; import com.vaadin.client.Profiler; import com.vaadin.client.UIDL; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.VConsole; import com.vaadin.client.ValueMap; import com.vaadin.client.ui.VOverlay; @@ -92,16 +92,16 @@ public class VDragAndDropManager { targetElement = targetNode.getParentElement(); } - if (Util.isTouchEvent(nativeEvent) || dragElement != null) { + if (WidgetUtil.isTouchEvent(nativeEvent) || dragElement != null) { // to detect the "real" target, hide dragelement temporary and // use elementFromPoint String display = dragElement.getStyle().getDisplay(); dragElement.getStyle().setDisplay(Display.NONE); try { - int x = Util.getTouchOrMouseClientX(nativeEvent); - int y = Util.getTouchOrMouseClientY(nativeEvent); + int x = WidgetUtil.getTouchOrMouseClientX(nativeEvent); + int y = WidgetUtil.getTouchOrMouseClientY(nativeEvent); // Util.browserDebugger(); - targetElement = Util.getElementFromPoint(x, y); + targetElement = WidgetUtil.getElementFromPoint(x, y); if (targetElement == null) { // ApplicationConnection.getConsole().log( // "Event on dragImage, ignored"); @@ -361,10 +361,10 @@ public class VDragAndDropManager { deferredStartRegistration = Event .addNativePreviewHandler(new NativePreviewHandler() { - private int startX = Util + private int startX = WidgetUtil .getTouchOrMouseClientX(currentDrag .getCurrentGwtEvent()); - private int startY = Util + private int startY = WidgetUtil .getTouchOrMouseClientY(currentDrag .getCurrentGwtEvent()); @@ -419,10 +419,10 @@ public class VDragAndDropManager { } case Event.ONMOUSEMOVE: case Event.ONTOUCHMOVE: - int currentX = Util + int currentX = WidgetUtil .getTouchOrMouseClientX(event .getNativeEvent()); - int currentY = Util + int currentY = WidgetUtil .getTouchOrMouseClientY(event .getNativeEvent()); if (Math.abs(startX - currentX) > 3 @@ -462,9 +462,9 @@ public class VDragAndDropManager { private void updateDragImagePosition() { if (currentDrag.getCurrentGwtEvent() != null && dragElement != null) { Style style = dragElement.getStyle(); - int clientY = Util.getTouchOrMouseClientY(currentDrag + int clientY = WidgetUtil.getTouchOrMouseClientY(currentDrag .getCurrentGwtEvent()); - int clientX = Util.getTouchOrMouseClientX(currentDrag + int clientX = WidgetUtil.getTouchOrMouseClientX(currentDrag .getCurrentGwtEvent()); style.setTop(clientY, Unit.PX); style.setLeft(clientX, Unit.PX); @@ -480,7 +480,7 @@ public class VDragAndDropManager { */ private VDropHandler findDragTarget(Element element) { try { - Widget w = Util.findWidget(element, null); + Widget w = WidgetUtil.findWidget(element, null); if (w == null) { return null; } diff --git a/client/src/com/vaadin/client/ui/dd/VDragEvent.java b/client/src/com/vaadin/client/ui/dd/VDragEvent.java index 45f89bdb87..c889dbf34e 100644 --- a/client/src/com/vaadin/client/ui/dd/VDragEvent.java +++ b/client/src/com/vaadin/client/ui/dd/VDragEvent.java @@ -30,7 +30,7 @@ import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.EventListener; import com.vaadin.client.BrowserInfo; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; /** * DragEvent used by Vaadin client side engine. Supports components, items, @@ -262,8 +262,8 @@ public class VDragEvent { if (alignImageToEvent) { int absoluteTop = element.getAbsoluteTop(); int absoluteLeft = element.getAbsoluteLeft(); - int clientX = Util.getTouchOrMouseClientX(startEvent); - int clientY = Util.getTouchOrMouseClientY(startEvent); + int clientX = WidgetUtil.getTouchOrMouseClientX(startEvent); + int clientY = WidgetUtil.getTouchOrMouseClientY(startEvent); int offsetX = absoluteLeft - clientX; int offsetY = absoluteTop - clientY; setDragImage(cloneNode, offsetX, offsetY); diff --git a/client/src/com/vaadin/client/ui/formlayout/FormLayoutConnector.java b/client/src/com/vaadin/client/ui/formlayout/FormLayoutConnector.java index 494a1a87ff..9517619cf3 100644 --- a/client/src/com/vaadin/client/ui/formlayout/FormLayoutConnector.java +++ b/client/src/com/vaadin/client/ui/formlayout/FormLayoutConnector.java @@ -20,7 +20,7 @@ import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.ComponentConnector; import com.vaadin.client.ConnectorHierarchyChangeEvent; import com.vaadin.client.TooltipInfo; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.ui.AbstractFieldConnector; import com.vaadin.client.ui.AbstractLayoutConnector; @@ -114,14 +114,16 @@ public class FormLayoutConnector extends AbstractLayoutConnector { TooltipInfo info = null; if (element != getWidget().getElement()) { - Object node = Util.findWidget(element, VFormLayout.Caption.class); + Object node = WidgetUtil.findWidget(element, + VFormLayout.Caption.class); if (node != null) { VFormLayout.Caption caption = (VFormLayout.Caption) node; info = caption.getOwner().getTooltipInfo(element); } else { - node = Util.findWidget(element, VFormLayout.ErrorFlag.class); + node = WidgetUtil.findWidget(element, + VFormLayout.ErrorFlag.class); if (node != null) { VFormLayout.ErrorFlag flag = (VFormLayout.ErrorFlag) node; diff --git a/client/src/com/vaadin/client/ui/label/LabelConnector.java b/client/src/com/vaadin/client/ui/label/LabelConnector.java index 07defcc64d..fc94f27cf0 100644 --- a/client/src/com/vaadin/client/ui/label/LabelConnector.java +++ b/client/src/com/vaadin/client/ui/label/LabelConnector.java @@ -18,7 +18,7 @@ package com.vaadin.client.ui.label; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.PreElement; import com.vaadin.client.Profiler; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.ui.AbstractComponentConnector; import com.vaadin.client.ui.VLabel; @@ -69,7 +69,7 @@ public class LabelConnector extends AbstractComponentConnector { if (sinkOnloads) { Profiler.enter("LabelConnector.onStateChanged sinkOnloads"); - Util.sinkOnloadForImages(getWidget().getElement()); + WidgetUtil.sinkOnloadForImages(getWidget().getElement()); Profiler.leave("LabelConnector.onStateChanged sinkOnloads"); } } diff --git a/client/src/com/vaadin/client/ui/layout/LayoutDependencyTree.java b/client/src/com/vaadin/client/ui/layout/LayoutDependencyTree.java index ae866e3354..da3aed4bbc 100644 --- a/client/src/com/vaadin/client/ui/layout/LayoutDependencyTree.java +++ b/client/src/com/vaadin/client/ui/layout/LayoutDependencyTree.java @@ -586,7 +586,7 @@ public class LayoutDependencyTree { } private static String getCompactConnectorString(ServerConnector connector) { - return Util.getSimpleName(connector) + " (" + return connector.getClass().getSimpleName() + " (" + connector.getConnectorId() + ")"; } diff --git a/client/src/com/vaadin/client/ui/menubar/MenuBarConnector.java b/client/src/com/vaadin/client/ui/menubar/MenuBarConnector.java index 20cabf9a36..03eeb85165 100644 --- a/client/src/com/vaadin/client/ui/menubar/MenuBarConnector.java +++ b/client/src/com/vaadin/client/ui/menubar/MenuBarConnector.java @@ -25,7 +25,7 @@ import com.vaadin.client.ApplicationConnection; import com.vaadin.client.Paintable; import com.vaadin.client.TooltipInfo; import com.vaadin.client.UIDL; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.AbstractComponentConnector; import com.vaadin.client.ui.ImageIcon; import com.vaadin.client.ui.SimpleManagedLayout; @@ -78,7 +78,7 @@ public class MenuBarConnector extends AbstractComponentConnector implements if (moreItemUIDL.hasAttribute("icon")) { itemHTML.append("<img src=\"" - + Util.escapeAttribute(client + + WidgetUtil.escapeAttribute(client .translateVaadinUri(moreItemUIDL .getStringAttribute("icon"))) + "\" class=\"" + ImageIcon.CLASSNAME diff --git a/client/src/com/vaadin/client/ui/orderedlayout/AbstractOrderedLayoutConnector.java b/client/src/com/vaadin/client/ui/orderedlayout/AbstractOrderedLayoutConnector.java index c2157650a5..8fa885c2b9 100644 --- a/client/src/com/vaadin/client/ui/orderedlayout/AbstractOrderedLayoutConnector.java +++ b/client/src/com/vaadin/client/ui/orderedlayout/AbstractOrderedLayoutConnector.java @@ -30,6 +30,7 @@ import com.vaadin.client.Profiler; import com.vaadin.client.ServerConnector; import com.vaadin.client.TooltipInfo; import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler; import com.vaadin.client.ui.AbstractFieldConnector; @@ -387,7 +388,7 @@ public abstract class AbstractOrderedLayoutConnector extends @Override public TooltipInfo getTooltipInfo(com.google.gwt.dom.client.Element element) { if (element != getWidget().getElement()) { - Slot slot = Util.findWidget(element, Slot.class); + Slot slot = WidgetUtil.findWidget(element, Slot.class); if (slot != null && slot.getCaptionElement() != null && slot.getParent() == getWidget() && slot.getCaptionElement().isOrHasChild(element)) { diff --git a/client/src/com/vaadin/client/ui/orderedlayout/Slot.java b/client/src/com/vaadin/client/ui/orderedlayout/Slot.java index 4b60f11ab4..b97cf73989 100644 --- a/client/src/com/vaadin/client/ui/orderedlayout/Slot.java +++ b/client/src/com/vaadin/client/ui/orderedlayout/Slot.java @@ -30,7 +30,7 @@ import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.BrowserInfo; import com.vaadin.client.LayoutManager; import com.vaadin.client.StyleConstants; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.FontIcon; import com.vaadin.client.ui.Icon; import com.vaadin.client.ui.ImageIcon; @@ -74,7 +74,7 @@ public final class Slot extends SimplePanel { public void onElementResize(ElementResizeEvent e) { Element caption = getCaptionElement(); if (caption != null) { - Util.forceIE8Redraw(caption); + WidgetUtil.forceIE8Redraw(caption); } } }; @@ -493,7 +493,7 @@ public final class Slot extends SimplePanel { // Caption wrappers Widget widget = getWidget(); - final Element focusedElement = Util.getFocusedElement(); + final Element focusedElement = WidgetUtil.getFocusedElement(); // By default focus will not be lost boolean focusLost = false; if (captionText != null || icon != null || error != null || required) { @@ -613,7 +613,7 @@ public final class Slot extends SimplePanel { if (focusLost) { // Find out what element is currently focused. - Element currentFocus = Util.getFocusedElement(); + Element currentFocus = WidgetUtil.getFocusedElement(); if (currentFocus != null && currentFocus.equals(Document.get().getBody())) { // Focus has moved to BodyElement and should be moved back to @@ -627,12 +627,12 @@ public final class Slot extends SimplePanel { @Override public void run() { - if (Util.getFocusedElement() == null) { + if (WidgetUtil.getFocusedElement() == null) { // This should never become an infinite loop and // even if it does it will be stopped once something // is done with the browser. schedule(25); - } else if (Util.getFocusedElement().equals( + } else if (WidgetUtil.getFocusedElement().equals( Document.get().getBody())) { // Focus found it's way to BodyElement. Now it can // be restored diff --git a/client/src/com/vaadin/client/ui/orderedlayout/VAbstractOrderedLayout.java b/client/src/com/vaadin/client/ui/orderedlayout/VAbstractOrderedLayout.java index 4c74358753..2e6d4cf5c8 100644 --- a/client/src/com/vaadin/client/ui/orderedlayout/VAbstractOrderedLayout.java +++ b/client/src/com/vaadin/client/ui/orderedlayout/VAbstractOrderedLayout.java @@ -33,6 +33,7 @@ import com.vaadin.client.BrowserInfo; import com.vaadin.client.LayoutManager; import com.vaadin.client.Profiler; import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.shared.ui.MarginInfo; /** @@ -674,7 +675,7 @@ public class VAbstractOrderedLayout extends FlowPanel { } } } - Util.forceIE8Redraw(getElement()); + WidgetUtil.forceIE8Redraw(getElement()); } /** diff --git a/client/src/com/vaadin/client/ui/table/TableConnector.java b/client/src/com/vaadin/client/ui/table/TableConnector.java index 9f1f99bae3..0d34d2d4d9 100644 --- a/client/src/com/vaadin/client/ui/table/TableConnector.java +++ b/client/src/com/vaadin/client/ui/table/TableConnector.java @@ -31,7 +31,7 @@ import com.vaadin.client.Paintable; import com.vaadin.client.ServerConnector; import com.vaadin.client.TooltipInfo; import com.vaadin.client.UIDL; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.AbstractHasComponentsConnector; import com.vaadin.client.ui.PostLayoutListener; import com.vaadin.client.ui.VScrollTable; @@ -342,7 +342,7 @@ public class TableConnector extends AbstractHasComponentsConnector implements @Override public void execute() { // IE8 needs some hacks to measure sizes correctly - Util.forceIE8Redraw(getWidget().getElement()); + WidgetUtil.forceIE8Redraw(getWidget().getElement()); getLayoutManager().setNeedsMeasure(TableConnector.this); ServerConnector parent = getParent(); @@ -394,7 +394,7 @@ public class TableConnector extends AbstractHasComponentsConnector implements TooltipInfo info = null; if (element != getWidget().getElement()) { - Object node = Util.findWidget(element, VScrollTableRow.class); + Object node = WidgetUtil.findWidget(element, VScrollTableRow.class); if (node != null) { VScrollTableRow row = (VScrollTableRow) node; diff --git a/client/src/com/vaadin/client/ui/tabsheet/TabsheetConnector.java b/client/src/com/vaadin/client/ui/tabsheet/TabsheetConnector.java index d49581eaad..469fc6ba95 100644 --- a/client/src/com/vaadin/client/ui/tabsheet/TabsheetConnector.java +++ b/client/src/com/vaadin/client/ui/tabsheet/TabsheetConnector.java @@ -20,7 +20,7 @@ import com.google.gwt.dom.client.Style.Overflow; import com.vaadin.client.ComponentConnector; import com.vaadin.client.ConnectorHierarchyChangeEvent; import com.vaadin.client.TooltipInfo; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.ui.SimpleManagedLayout; import com.vaadin.client.ui.VTabsheet; @@ -139,7 +139,8 @@ public class TabsheetConnector extends TabsheetBaseConnector implements // Find a tooltip for the tab, if the element is a tab if (element != getWidget().getElement()) { - Object node = Util.findWidget(element, VTabsheet.TabCaption.class); + Object node = WidgetUtil.findWidget(element, + VTabsheet.TabCaption.class); if (node != null) { VTabsheet.TabCaption caption = (VTabsheet.TabCaption) node; diff --git a/client/src/com/vaadin/client/ui/textarea/TextAreaConnector.java b/client/src/com/vaadin/client/ui/textarea/TextAreaConnector.java index e9dc3e1dd7..3bc0a86df4 100644 --- a/client/src/com/vaadin/client/ui/textarea/TextAreaConnector.java +++ b/client/src/com/vaadin/client/ui/textarea/TextAreaConnector.java @@ -19,7 +19,7 @@ package com.vaadin.client.ui.textarea; import com.google.gwt.dom.client.Element; import com.google.gwt.event.dom.client.MouseUpEvent; import com.google.gwt.event.dom.client.MouseUpHandler; -import com.vaadin.client.Util.CssSize; +import com.vaadin.client.WidgetUtil.CssSize; import com.vaadin.client.ui.VTextArea; import com.vaadin.client.ui.textfield.TextFieldConnector; import com.vaadin.shared.ui.Connect; diff --git a/client/src/com/vaadin/client/ui/tree/TreeConnector.java b/client/src/com/vaadin/client/ui/tree/TreeConnector.java index 55224b455f..fc3e6ca0fc 100644 --- a/client/src/com/vaadin/client/ui/tree/TreeConnector.java +++ b/client/src/com/vaadin/client/ui/tree/TreeConnector.java @@ -27,7 +27,7 @@ import com.vaadin.client.BrowserInfo; import com.vaadin.client.Paintable; import com.vaadin.client.TooltipInfo; import com.vaadin.client.UIDL; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.VConsole; import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.ui.AbstractComponentConnector; @@ -172,7 +172,7 @@ public class TreeConnector extends AbstractComponentConnector implements } // IE8 needs a hack to measure the tree again after update - Util.forceIE8Redraw(getWidget().getElement()); + WidgetUtil.forceIE8Redraw(getWidget().getElement()); getWidget().rendering = false; @@ -333,7 +333,7 @@ public class TreeConnector extends AbstractComponentConnector implements // Try to find a tooltip for a node if (element != getWidget().getElement()) { - Object node = Util.findWidget(element, TreeNode.class); + Object node = WidgetUtil.findWidget(element, TreeNode.class); if (node != null) { TreeNode tnode = (TreeNode) node; diff --git a/client/src/com/vaadin/client/ui/treetable/TreeTableConnector.java b/client/src/com/vaadin/client/ui/treetable/TreeTableConnector.java index 5a42484b28..4aab248e29 100644 --- a/client/src/com/vaadin/client/ui/treetable/TreeTableConnector.java +++ b/client/src/com/vaadin/client/ui/treetable/TreeTableConnector.java @@ -19,7 +19,7 @@ import com.google.gwt.dom.client.Element; import com.vaadin.client.ApplicationConnection; import com.vaadin.client.TooltipInfo; import com.vaadin.client.UIDL; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.FocusableScrollPanel; import com.vaadin.client.ui.VScrollTable.VScrollTableBody.VScrollTableRow; import com.vaadin.client.ui.VTreeTable; @@ -129,7 +129,7 @@ public class TreeTableConnector extends TableConnector { TooltipInfo info = null; if (element != getWidget().getElement()) { - Object node = Util.findWidget(element, VTreeTableRow.class); + Object node = WidgetUtil.findWidget(element, VTreeTableRow.class); if (node != null) { VTreeTableRow row = (VTreeTableRow) node; diff --git a/client/src/com/vaadin/client/ui/window/WindowConnector.java b/client/src/com/vaadin/client/ui/window/WindowConnector.java index 33cabd563a..5214c33eec 100644 --- a/client/src/com/vaadin/client/ui/window/WindowConnector.java +++ b/client/src/com/vaadin/client/ui/window/WindowConnector.java @@ -38,7 +38,7 @@ import com.vaadin.client.ConnectorHierarchyChangeEvent; import com.vaadin.client.LayoutManager; import com.vaadin.client.Paintable; import com.vaadin.client.UIDL; -import com.vaadin.client.Util; +import com.vaadin.client.WidgetUtil; import com.vaadin.client.communication.RpcProxy; import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.ui.AbstractSingleComponentContainerConnector; @@ -247,7 +247,7 @@ public class WindowConnector extends AbstractSingleComponentContainerConnector Style childStyle = layoutElement.getStyle(); // IE8 needs some hackery to measure its content correctly - Util.forceIE8Redraw(layoutElement); + WidgetUtil.forceIE8Redraw(layoutElement); if (content.isRelativeHeight() && !BrowserInfo.get().isIE9()) { childStyle.setPosition(Position.ABSOLUTE); diff --git a/client/src/com/vaadin/client/widget/escalator/Cell.java b/client/src/com/vaadin/client/widget/escalator/Cell.java new file mode 100644 index 0000000000..08dbcf6955 --- /dev/null +++ b/client/src/com/vaadin/client/widget/escalator/Cell.java @@ -0,0 +1,85 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.escalator; + +import com.google.gwt.dom.client.TableCellElement; + +/** + * Describes a cell + * <p> + * It's a representation of the element in a grid cell, and its row and column + * indices. + * <p> + * Unlike the {@link FlyweightRow}, an instance of {@link Cell} can be stored in + * a field. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class Cell { + + private final int row; + + private final int column; + + private final TableCellElement element; + + /** + * Constructs a new {@link Cell}. + * + * @param row + * The index of the row + * @param column + * The index of the column + * @param element + * The cell element + */ + public Cell(int row, int column, TableCellElement element) { + super(); + this.row = row; + this.column = column; + this.element = element; + } + + /** + * Returns the index of the row the cell resides in. + * + * @return the row index + * + */ + public int getRow() { + return row; + } + + /** + * Returns the index of the column the cell resides in. + * + * @return the column index + */ + public int getColumn() { + return column; + } + + /** + * Returns the element of the cell. + * + * @return the cell element + */ + public TableCellElement getElement() { + return element; + } + +} diff --git a/client/src/com/vaadin/client/widget/escalator/ColumnConfiguration.java b/client/src/com/vaadin/client/widget/escalator/ColumnConfiguration.java new file mode 100644 index 0000000000..af49dcd64f --- /dev/null +++ b/client/src/com/vaadin/client/widget/escalator/ColumnConfiguration.java @@ -0,0 +1,179 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.escalator; + +import com.vaadin.client.widgets.Escalator; + +/** + * A representation of the columns in an instance of {@link Escalator}. + * + * @since 7.4 + * @author Vaadin Ltd + * @see Escalator#getColumnConfiguration() + */ +public interface ColumnConfiguration { + + /** + * Removes columns at certain indices. + * <p> + * If any of the removed columns were frozen, the number of frozen columns + * will be reduced by the number of the removed columns that were frozen. + * <p> + * <em>Note:</em> This method simply removes the given columns, and does not + * do much of anything else. Especially if you have column spans, you + * probably need to run {@link #refreshColumns(int, int)} or + * {@link RowContainer#refreshRows(int, int)} + * + * @param index + * the index of the first column to be removed + * @param numberOfColumns + * the number of rows to remove, starting from {@code index} + * @throws IndexOutOfBoundsException + * if the entire range of removed columns is not currently + * present in the escalator + * @throws IllegalArgumentException + * if <code>numberOfColumns</code> is less than 1. + */ + public void removeColumns(int index, int numberOfColumns) + throws IndexOutOfBoundsException, IllegalArgumentException; + + /** + * Adds columns at a certain index. + * <p> + * The new columns will be inserted between the column at the index, and the + * column before (an index of 0 means that the columns are inserted at the + * beginning). Therefore, the columns at the index and afterwards will be + * moved to the right. + * <p> + * The contents of the inserted columns will be queried from the respective + * cell renderers in the header, body and footer. + * <p> + * If there are frozen columns and the first added column is to the left of + * the last frozen column, the number of frozen columns will be increased by + * the number of inserted columns. + * <p> + * <em>Note:</em> Only the contents of the inserted columns will be + * rendered. If inserting new columns affects the contents of existing + * columns (e.g. you have column spans), + * {@link RowContainer#refreshRows(int, int)} or + * {@link #refreshColumns(int, int)} needs to be called as appropriate. + * + * @param index + * the index of the column before which new columns are inserted, + * or {@link #getColumnCount()} to add new columns at the end + * @param numberOfColumns + * the number of columns to insert after the <code>index</code> + * @throws IndexOutOfBoundsException + * if <code>index</code> is not an integer in the range + * <code>[0..{@link #getColumnCount()}]</code> + * @throws IllegalArgumentException + * if {@code numberOfColumns} is less than 1. + */ + public void insertColumns(int index, int numberOfColumns) + throws IndexOutOfBoundsException, IllegalArgumentException; + + /** + * Returns the number of columns in the escalator. + * + * @return the number of columns in the escalator + */ + public int getColumnCount(); + + /** + * Sets the number of leftmost columns that are not affected by horizontal + * scrolling. + * + * @param count + * the number of columns to freeze + * + * @throws IllegalArgumentException + * if the column count is < 0 or > the number of columns + * + */ + public void setFrozenColumnCount(int count) throws IllegalArgumentException; + + /** + * Get the number of leftmost columns that are not affected by horizontal + * scrolling. + * + * @return the number of frozen columns + */ + public int getFrozenColumnCount(); + + /** + * Sets (or unsets) an explicit width for a column. + * + * @param index + * the index of the column for which to set a width + * @param px + * the number of pixels the indicated column should be, or a + * negative number to let the escalator decide + * @throws IllegalArgumentException + * if <code>index</code> is not a valid column index + */ + public void setColumnWidth(int index, double px) + throws IllegalArgumentException; + + /** + * Returns the user-defined width of a column. + * + * @param index + * the index of the column for which to retrieve the width + * @return the column's width in pixels, or a negative number if the width + * is implicitly decided by the escalator + * @throws IllegalArgumentException + * if <code>index</code> is not a valid column index + */ + public double getColumnWidth(int index) throws IllegalArgumentException; + + /** + * Returns the actual width of a column. + * + * @param index + * the index of the column for which to retrieve the width + * @return the column's actual width in pixels + * @throws IllegalArgumentException + * if <code>index</code> is not a valid column index + */ + public double getColumnWidthActual(int index) + throws IllegalArgumentException; + + /** + * Refreshes a range of rows in the current row containers in each Escalator + * section. + * <p> + * The data for the refreshed columns is queried from the current cell + * renderer. + * + * @param index + * the index of the first row that will be updated + * @param numberOfRows + * the number of rows to update, starting from the index + * @throws IndexOutOfBoundsException + * if any integer number in the range + * <code>[index..(index+numberOfColumns)]</code> is not an + * existing column index. + * @throws IllegalArgumentException + * if {@code numberOfColumns} is less than 1. + * @see RowContainer#setEscalatorUpdater(EscalatorUpdater) + * @see Escalator#getHeader() + * @see Escalator#getBody() + * @see Escalator#getFooter() + */ + public void refreshColumns(int index, int numberOfColumns) + throws IndexOutOfBoundsException, IllegalArgumentException; +} diff --git a/client/src/com/vaadin/client/widget/escalator/EscalatorUpdater.java b/client/src/com/vaadin/client/widget/escalator/EscalatorUpdater.java new file mode 100644 index 0000000000..6109c5e51d --- /dev/null +++ b/client/src/com/vaadin/client/widget/escalator/EscalatorUpdater.java @@ -0,0 +1,157 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.escalator; + +import com.vaadin.client.widgets.Escalator; + +/** + * An interface that allows client code to define how a certain row in Escalator + * will be displayed. The contents of an escalator's header, body and footer are + * rendered by their respective updaters. + * <p> + * The updater is responsible for internally handling all remote communication, + * should the displayed data need to be fetched remotely. + * <p> + * This has a similar function to {@link Grid Grid's} {@link Renderer Renderers} + * , although they operate on different abstraction levels. + * + * @since 7.4 + * @author Vaadin Ltd + * @see RowContainer#setEscalatorUpdater(EscalatorUpdater) + * @see Escalator#getHeader() + * @see Escalator#getBody() + * @see Escalator#getFooter() + * @see Renderer + */ +public interface EscalatorUpdater { + + /** + * An {@link EscalatorUpdater} that doesn't render anything. + */ + public static final EscalatorUpdater NULL = new EscalatorUpdater() { + @Override + public void update(final Row row, + final Iterable<FlyweightCell> cellsToUpdate) { + // NOOP + } + + @Override + public void preAttach(final Row row, + final Iterable<FlyweightCell> cellsToAttach) { + // NOOP + + } + + @Override + public void postAttach(final Row row, + final Iterable<FlyweightCell> attachedCells) { + // NOOP + } + + @Override + public void preDetach(final Row row, + final Iterable<FlyweightCell> cellsToDetach) { + // NOOP + } + + @Override + public void postDetach(final Row row, + final Iterable<FlyweightCell> detachedCells) { + // NOOP + } + }; + + /** + * Renders a row contained in a row container. + * <p> + * <em>Note:</em> If rendering of cells is deferred (e.g. because + * asynchronous data retrieval), this method is responsible for explicitly + * displaying some placeholder data (empty content is valid). Because the + * cells (and rows) in an escalator are recycled, failing to reset a cell's + * presentation will lead to wrong data being displayed in the escalator. + * <p> + * For performance reasons, the escalator will never autonomously clear any + * data in a cell. + * + * @param row + * Information about the row that is being updated. + * <em>Note:</em> You should not store nor reuse this reference. + * @param cellsToUpdate + * A collection of cells that need to be updated. <em>Note:</em> + * You should neither store nor reuse the reference to the + * iterable, nor to the individual cells. + */ + public void update(Row row, Iterable<FlyweightCell> cellsToUpdate); + + /** + * Called before attaching new cells to the escalator. + * + * @param row + * Information about the row to which the cells will be added. + * <em>Note:</em> You should not store nor reuse this reference. + * @param cellsToAttach + * A collection of cells that are about to be attached. + * <em>Note:</em> You should neither store nor reuse the + * reference to the iterable, nor to the individual cells. + * + */ + public void preAttach(Row row, Iterable<FlyweightCell> cellsToAttach); + + /** + * Called after attaching new cells to the escalator. + * + * @param row + * Information about the row to which the cells were added. + * <em>Note:</em> You should not store nor reuse this reference. + * @param attachedCells + * A collection of cells that were attached. <em>Note:</em> You + * should neither store nor reuse the reference to the iterable, + * nor to the individual cells. + * + */ + public void postAttach(Row row, Iterable<FlyweightCell> attachedCells); + + /** + * Called before detaching cells from the escalator. + * + * @param row + * Information about the row from which the cells will be + * removed. <em>Note:</em> You should not store nor reuse this + * reference. + * @param cellsToAttach + * A collection of cells that are about to be detached. + * <em>Note:</em> You should neither store nor reuse the + * reference to the iterable, nor to the individual cells. + * + */ + public void preDetach(Row row, Iterable<FlyweightCell> cellsToDetach); + + /** + * Called after detaching cells from the escalator. + * + * @param row + * Information about the row from which the cells were removed. + * <em>Note:</em> You should not store nor reuse this reference. + * @param attachedCells + * A collection of cells that were detached. <em>Note:</em> You + * should neither store nor reuse the reference to the iterable, + * nor to the individual cells. + * + */ + public void postDetach(Row row, Iterable<FlyweightCell> detachedCells); + +} diff --git a/client/src/com/vaadin/client/widget/escalator/FlyweightCell.java b/client/src/com/vaadin/client/widget/escalator/FlyweightCell.java new file mode 100644 index 0000000000..b77b752327 --- /dev/null +++ b/client/src/com/vaadin/client/widget/escalator/FlyweightCell.java @@ -0,0 +1,201 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.escalator; + +import java.util.List; + +import com.google.gwt.dom.client.Style.Display; +import com.google.gwt.dom.client.Style.Unit; +import com.google.gwt.dom.client.TableCellElement; +import com.vaadin.client.widget.escalator.FlyweightRow.CellIterator; +import com.vaadin.client.widgets.Escalator; + +/** + * A {@link FlyweightCell} represents a cell in the {@link Grid} or + * {@link Escalator} at a certain point in time. + * + * <p> + * Since the {@link FlyweightCell} follows the <code>Flyweight</code>-pattern + * any instance of this object is subject to change without the user knowing it + * and so should not be stored anywhere outside of the method providing these + * instances. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class FlyweightCell { + public static final String COLSPAN_ATTR = "colSpan"; + + private final int column; + private final FlyweightRow row; + + private TableCellElement element = null; + private CellIterator currentIterator = null; + + public FlyweightCell(final FlyweightRow row, final int column) { + this.row = row; + this.column = column; + } + + /** + * Returns the row index of the cell + */ + public int getRow() { + assertSetup(); + return row.getRow(); + } + + /** + * Returns the column index of the cell + */ + public int getColumn() { + assertSetup(); + return column; + } + + /** + * Returns the element of the cell. Can be either a <code>TD</code> element + * or a <code>TH</code> element. + */ + public TableCellElement getElement() { + assertSetup(); + return element; + } + + /** + * Return the colspan attribute of the element of the cell. + */ + public int getColSpan() { + assertSetup(); + return element.getPropertyInt(COLSPAN_ATTR); + } + + /** + * Sets the DOM element for this FlyweightCell, either a <code>TD</code> or + * a <code>TH</code>. It is the caller's responsibility to actually insert + * the given element to the document when needed. + * + * @param element + * the element corresponding to this cell, cannot be null + */ + public void setElement(TableCellElement element) { + assert element != null; + assertSetup(); + this.element = element; + } + + void setup(final CellIterator iterator) { + currentIterator = iterator; + + if (iterator.areCellsAttached()) { + final TableCellElement e = row.getElement().getCells() + .getItem(column); + + assert e != null : "Cell " + column + " for logical row " + + row.getRow() + " doesn't exist in the DOM!"; + + e.setPropertyInt(COLSPAN_ATTR, 1); + if (row.getColumnWidth(column) >= 0) { + e.getStyle().setWidth(row.getColumnWidth(column), Unit.PX); + } + e.getStyle().clearDisplay(); + setElement(e); + } + } + + /** + * Tear down the state of the Cell. + * <p> + * This is an internal check method, to prevent retrieving uninitialized + * data by calling {@link #getRow()}, {@link #getColumn()} or + * {@link #getElement()} at an improper time. + * <p> + * This should only be used with asserts (" + * <code>assert flyweightCell.teardown()</code> ") so that the code is never + * run when asserts aren't enabled. + * + * @return always <code>true</code> + * @see FlyweightRow#teardown() + */ + boolean teardown() { + currentIterator = null; + element = null; + return true; + } + + /** + * Asserts that the flyweight cell has properly been set up before trying to + * access any of its data. + */ + private void assertSetup() { + assert currentIterator != null : "FlyweightCell was not properly " + + "initialized. This is either a bug in Grid/Escalator " + + "or a Cell reference has been stored and reused " + + "inappropriately."; + } + + public void setColSpan(final int numberOfCells) { + if (numberOfCells < 1) { + throw new IllegalArgumentException( + "Number of cells should be more than 0"); + } + + /*- + * This will default to 1 if unset, as per DOM specifications: + * http://www.w3.org/TR/html5/tabular-data.html#attributes-common-to-td-and-th-elements + */ + final int prevColSpan = getElement().getPropertyInt(COLSPAN_ATTR); + if (numberOfCells == 1 && prevColSpan == 1) { + return; + } + + getElement().setPropertyInt(COLSPAN_ATTR, numberOfCells); + adjustCellWidthForSpan(numberOfCells); + hideOrRevealAdjacentCellElements(numberOfCells, prevColSpan); + currentIterator.setSkipNext(numberOfCells - 1); + } + + private void adjustCellWidthForSpan(final int numberOfCells) { + final int cellsToTheRight = currentIterator.rawPeekNext( + numberOfCells - 1).size(); + + final double selfWidth = row.getColumnWidth(column); + double widthsOfColumnsToTheRight = 0; + for (int i = 0; i < cellsToTheRight; i++) { + widthsOfColumnsToTheRight += row.getColumnWidth(column + i + 1); + } + getElement().getStyle().setWidth(selfWidth + widthsOfColumnsToTheRight, + Unit.PX); + } + + private void hideOrRevealAdjacentCellElements(final int numberOfCells, + final int prevColSpan) { + final int affectedCellsNumber = Math.max(prevColSpan, numberOfCells); + final List<FlyweightCell> affectedCells = currentIterator + .rawPeekNext(affectedCellsNumber - 1); + if (prevColSpan < numberOfCells) { + for (int i = 0; i < affectedCells.size(); i++) { + affectedCells.get(prevColSpan + i - 1).getElement().getStyle() + .setDisplay(Display.NONE); + } + } else if (prevColSpan > numberOfCells) { + for (int i = 0; i < affectedCells.size(); i++) { + affectedCells.get(numberOfCells + i - 1).getElement() + .getStyle().clearDisplay(); + } + } + } +} diff --git a/client/src/com/vaadin/client/widget/escalator/FlyweightRow.java b/client/src/com/vaadin/client/widget/escalator/FlyweightRow.java new file mode 100644 index 0000000000..6e25e82235 --- /dev/null +++ b/client/src/com/vaadin/client/widget/escalator/FlyweightRow.java @@ -0,0 +1,295 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.escalator; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import com.google.gwt.dom.client.TableRowElement; +import com.vaadin.client.widgets.Escalator; + +/** + * An internal implementation of the {@link Row} interface. + * <p> + * There is only one instance per Escalator. This is designed to be re-used when + * rendering rows. + * + * @since 7.4 + * @author Vaadin Ltd + * @see Escalator.AbstractRowContainer#refreshRow(Node, int) + */ +public class FlyweightRow implements Row { + + static class CellIterator implements Iterator<FlyweightCell> { + /** A defensive copy of the cells in the current row. */ + private final ArrayList<FlyweightCell> cells; + private final boolean cellsAttached; + private int cursor = 0; + private int skipNext = 0; + + /** + * Creates a new iterator of attached flyweight cells. A cell is + * attached if it has a corresponding {@link FlyweightCell#getElement() + * DOM element} attached to the row element. + * + * @param cells + * the collection of cells to iterate + */ + public static CellIterator attached( + final Collection<FlyweightCell> cells) { + return new CellIterator(cells, true); + } + + /** + * Creates a new iterator of unattached flyweight cells. A cell is + * unattached if it does not have a corresponding + * {@link FlyweightCell#getElement() DOM element} attached to the row + * element. + * + * @param cells + * the collection of cells to iterate + */ + public static CellIterator unattached( + final Collection<FlyweightCell> cells) { + return new CellIterator(cells, false); + } + + private CellIterator(final Collection<FlyweightCell> cells, + final boolean attached) { + this.cells = new ArrayList<FlyweightCell>(cells); + cellsAttached = attached; + } + + @Override + public boolean hasNext() { + return cursor + skipNext < cells.size(); + } + + @Override + public FlyweightCell next() { + // if we needed to skip some cells since the last invocation. + for (int i = 0; i < skipNext; i++) { + cells.remove(cursor); + } + skipNext = 0; + + final FlyweightCell cell = cells.get(cursor++); + cell.setup(this); + return cell; + } + + @Override + public void remove() { + throw new UnsupportedOperationException( + "Cannot remove cells via iterator"); + } + + /** + * Sets the number of cells to skip when {@link #next()} is called the + * next time. Cell hiding is also handled eagerly in this method. + * + * @param colspan + * the number of cells to skip on next invocation of + * {@link #next()} + */ + public void setSkipNext(final int colspan) { + assert colspan > 0 : "Number of cells didn't make sense: " + + colspan; + skipNext = colspan; + } + + /** + * Gets the next <code>n</code> cells in the iterator, ignoring any + * possibly spanned cells. + * + * @param n + * the number of next cells to retrieve + * @return A list of next <code>n</code> cells, or less if there aren't + * enough cells to retrieve + */ + public List<FlyweightCell> rawPeekNext(final int n) { + final int from = Math.min(cursor, cells.size()); + final int to = Math.min(cursor + n, cells.size()); + List<FlyweightCell> nextCells = cells.subList(from, to); + for (FlyweightCell cell : nextCells) { + cell.setup(this); + } + return nextCells; + } + + public boolean areCellsAttached() { + return cellsAttached; + } + } + + private static final int BLANK = Integer.MIN_VALUE; + + private int row; + private TableRowElement element; + private double[] columnWidths = null; + private final List<FlyweightCell> cells = new ArrayList<FlyweightCell>(); + + public void setup(final TableRowElement e, final int row, + double[] columnWidths) { + element = e; + this.row = row; + this.columnWidths = columnWidths; + } + + /** + * Tear down the state of the Row. + * <p> + * This is an internal check method, to prevent retrieving uninitialized + * data by calling {@link #getRow()}, {@link #getElement()} or + * {@link #getCells()} at an improper time. + * <p> + * This should only be used with asserts (" + * <code>assert flyweightRow.teardown()</code> ") so that the code is never + * run when asserts aren't enabled. + * + * @return always <code>true</code> + */ + public boolean teardown() { + element = null; + row = BLANK; + columnWidths = null; + for (final FlyweightCell cell : cells) { + assert cell.teardown(); + } + return true; + } + + @Override + public int getRow() { + assertSetup(); + return row; + } + + @Override + public TableRowElement getElement() { + assertSetup(); + return element; + } + + public void addCells(final int index, final int numberOfColumns) { + for (int i = 0; i < numberOfColumns; i++) { + final int col = index + i; + cells.add(col, new FlyweightCell(this, col)); + } + updateRestOfCells(index + numberOfColumns); + } + + public void removeCells(final int index, final int numberOfColumns) { + cells.subList(index, index + numberOfColumns).clear(); + updateRestOfCells(index); + } + + private void updateRestOfCells(final int startPos) { + // update the column number for the cells to the right + for (int col = startPos; col < cells.size(); col++) { + cells.set(col, new FlyweightCell(this, col)); + } + } + + /** + * Returns flyweight cells for the client code to render. The cells get + * their associated {@link FlyweightCell#getElement() elements} from the row + * element. + * <p> + * Precondition: each cell has a corresponding element in the row + * + * @return an iterable of flyweight cells + * + * @see #setup(Element, int, int[]) + * @see #teardown() + */ + public Iterable<FlyweightCell> getCells() { + return getCells(0, cells.size()); + } + + /** + * Returns a subrange of flyweight cells for the client code to render. The + * cells get their associated {@link FlyweightCell#getElement() elements} + * from the row element. + * <p> + * Precondition: each cell has a corresponding element in the row + * + * @param offset + * the index of the first cell to return + * @param numberOfCells + * the number of cells to return + * @return an iterable of flyweight cells + */ + public Iterable<FlyweightCell> getCells(final int offset, + final int numberOfCells) { + assertSetup(); + assert offset >= 0 && offset + numberOfCells <= cells.size() : "Invalid range of cells"; + return new Iterable<FlyweightCell>() { + @Override + public Iterator<FlyweightCell> iterator() { + return CellIterator.attached(cells.subList(offset, offset + + numberOfCells)); + } + }; + } + + /** + * Returns a subrange of unattached flyweight cells. Unattached cells do not + * have {@link FlyweightCell#getElement() elements} associated. Note that + * FlyweightRow does not keep track of whether cells in actuality have + * corresponding DOM elements or not; it is the caller's responsibility to + * invoke this method with correct parameters. + * <p> + * Precondition: the range [offset, offset + numberOfCells) must be valid + * + * @param offset + * the index of the first cell to return + * @param numberOfCells + * the number of cells to return + * @return an iterable of flyweight cells + */ + public Iterable<FlyweightCell> getUnattachedCells(final int offset, + final int numberOfCells) { + assertSetup(); + assert offset >= 0 && offset + numberOfCells <= cells.size() : "Invalid range of cells"; + return new Iterable<FlyweightCell>() { + @Override + public Iterator<FlyweightCell> iterator() { + return CellIterator.unattached(cells.subList(offset, offset + + numberOfCells)); + } + }; + } + + /** + * Asserts that the flyweight row has properly been set up before trying to + * access any of its data. + */ + private void assertSetup() { + assert element != null && row != BLANK && columnWidths != null : "Flyweight row was not " + + "properly initialized. Make sure the setup-method is " + + "called before retrieving data. This is either a bug " + + "in Escalator, or the instance of the flyweight row " + + "has been stored and accessed."; + } + + double getColumnWidth(int column) { + assertSetup(); + return columnWidths[column]; + } +} diff --git a/client/src/com/vaadin/client/widget/escalator/PositionFunction.java b/client/src/com/vaadin/client/widget/escalator/PositionFunction.java new file mode 100644 index 0000000000..929f27df37 --- /dev/null +++ b/client/src/com/vaadin/client/widget/escalator/PositionFunction.java @@ -0,0 +1,118 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.escalator; + +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.Style.Unit; + +/** + * A functional interface that can be used for positioning elements in the DOM. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface PositionFunction { + /** + * A position function using "transform: translate3d(x,y,z)" to position + * elements in the DOM. + */ + public static class Translate3DPosition implements PositionFunction { + @Override + public void set(Element e, double x, double y) { + e.getStyle().setProperty("transform", + "translate3d(" + x + "px, " + y + "px, 0)"); + } + + @Override + public void reset(Element e) { + e.getStyle().clearProperty("transform"); + } + } + + /** + * A position function using "transform: translate(x,y)" to position + * elements in the DOM. + */ + public static class TranslatePosition implements PositionFunction { + @Override + public void set(Element e, double x, double y) { + e.getStyle().setProperty("transform", + "translate(" + x + "px," + y + "px)"); + } + + @Override + public void reset(Element e) { + e.getStyle().clearProperty("transform"); + } + } + + /** + * A position function using "-webkit-transform: translate3d(x,y,z)" to + * position elements in the DOM. + */ + public static class WebkitTranslate3DPosition implements PositionFunction { + @Override + public void set(Element e, double x, double y) { + e.getStyle().setProperty("webkitTransform", + "translate3d(" + x + "px," + y + "px,0)"); + } + + @Override + public void reset(Element e) { + e.getStyle().clearProperty("webkitTransform"); + } + } + + /** + * A position function using "left: x" and "top: y" to position elements in + * the DOM. + */ + public static class AbsolutePosition implements PositionFunction { + @Override + public void set(Element e, double x, double y) { + e.getStyle().setLeft(x, Unit.PX); + e.getStyle().setTop(y, Unit.PX); + } + + @Override + public void reset(Element e) { + e.getStyle().clearLeft(); + e.getStyle().clearTop(); + } + } + + /** + * Position an element in an (x,y) coordinate system in the DOM. + * + * @param e + * the element to position. Never <code>null</code>. + * @param x + * the x coordinate, in pixels + * @param y + * the y coordinate, in pixels + */ + void set(Element e, double x, double y); + + /** + * Resets any previously applied positioning, clearing the used style + * attributes. + * + * @param e + * the element for which to reset the positioning + */ + void reset(Element e); +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/escalator/Row.java b/client/src/com/vaadin/client/widget/escalator/Row.java new file mode 100644 index 0000000000..bcb3e163e4 --- /dev/null +++ b/client/src/com/vaadin/client/widget/escalator/Row.java @@ -0,0 +1,49 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.escalator; + +import com.google.gwt.dom.client.TableRowElement; +import com.vaadin.client.widgets.Escalator; + +/** + * A representation of a row in an {@link Escalator}. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface Row { + /** + * Gets the row index. + * + * @return the row index + */ + public int getRow(); + + /** + * Gets the root element for this row. + * <p> + * The {@link EscalatorUpdater} may update the class names of the element + * and add inline styles, but may not modify the contained DOM structure. + * <p> + * If you wish to modify the cells within this row element, access them via + * the <code>List<{@link Cell}></code> objects passed in to + * {@code EscalatorUpdater.updateCells(Row, List)} + * + * @return the root element of the row + */ + public TableRowElement getElement(); +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/escalator/RowContainer.java b/client/src/com/vaadin/client/widget/escalator/RowContainer.java new file mode 100644 index 0000000000..2fe2070b0d --- /dev/null +++ b/client/src/com/vaadin/client/widget/escalator/RowContainer.java @@ -0,0 +1,196 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.escalator; + +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.TableRowElement; +import com.vaadin.client.widgets.Escalator; + +/** + * A representation of the rows in each of the sections (header, body and + * footer) in an {@link Escalator}. + * + * @since 7.4 + * @author Vaadin Ltd + * @see Escalator#getHeader() + * @see Escalator#getBody() + * @see Escalator#getFooter() + */ +public interface RowContainer { + + /** + * An arbitrary pixel height of a row, before any autodetection for the row + * height has been made. + * */ + public static final double INITIAL_DEFAULT_ROW_HEIGHT = 20; + + /** + * Returns the current {@link EscalatorUpdater} used to render cells. + * + * @return the current escalator updater + */ + public EscalatorUpdater getEscalatorUpdater(); + + /** + * Sets the {@link EscalatorUpdater} to use when displaying data in the + * escalator. + * + * @param escalatorUpdater + * the escalator updater to use to render cells. May not be + * <code>null</code> + * @throws IllegalArgumentException + * if {@code cellRenderer} is <code>null</code> + * @see EscalatorUpdater#NULL + */ + public void setEscalatorUpdater(EscalatorUpdater escalatorUpdater) + throws IllegalArgumentException; + + /** + * Removes rows at a certain index in the current row container. + * + * @param index + * the index of the first row to be removed + * @param numberOfRows + * the number of rows to remove, starting from the index + * @throws IndexOutOfBoundsException + * if any integer number in the range + * <code>[index..(index+numberOfRows)]</code> is not an existing + * row index + * @throws IllegalArgumentException + * if {@code numberOfRows} is less than 1. + */ + public void removeRows(int index, int numberOfRows) + throws IndexOutOfBoundsException, IllegalArgumentException; + + /** + * Adds rows at a certain index in this row container. + * <p> + * The new rows will be inserted between the row at the index, and the row + * before (an index of 0 means that the rows are inserted at the beginning). + * Therefore, the rows currently at the index and afterwards will be moved + * downwards. + * <p> + * The contents of the inserted rows will subsequently be queried from the + * escalator updater. + * <p> + * <em>Note:</em> Only the contents of the inserted rows will be rendered. + * If inserting new rows affects the contents of existing rows, + * {@link #refreshRows(int, int)} needs to be called for those rows + * separately. + * + * @param index + * the index of the row before which new rows are inserted, or + * {@link #getRowCount()} to add rows at the end + * @param numberOfRows + * the number of rows to insert after the <code>index</code> + * @see #setEscalatorUpdater(EscalatorUpdater) + * @throws IndexOutOfBoundsException + * if <code>index</code> is not an integer in the range + * <code>[0..{@link #getRowCount()}]</code> + * @throws IllegalArgumentException + * if {@code numberOfRows} is less than 1. + */ + public void insertRows(int index, int numberOfRows) + throws IndexOutOfBoundsException, IllegalArgumentException; + + /** + * Refreshes a range of rows in the current row container. + * <p> + * The data for the refreshed rows is queried from the current cell + * renderer. + * + * @param index + * the index of the first row that will be updated + * @param numberOfRows + * the number of rows to update, starting from the index + * @see #setEscalatorUpdater(EscalatorUpdater) + * @throws IndexOutOfBoundsException + * if any integer number in the range + * <code>[index..(index+numberOfColumns)]</code> is not an + * existing column index. + * @throws IllegalArgumentException + * if {@code numberOfRows} is less than 1. + */ + public void refreshRows(int index, int numberOfRows) + throws IndexOutOfBoundsException, IllegalArgumentException; + + /** + * Gets the number of rows in the current row container. + * + * @return the number of rows in the current row container + */ + public int getRowCount(); + + /** + * The default height of the rows in this RowContainer. + * + * @param px + * the default height in pixels of the rows in this RowContainer + * @throws IllegalArgumentException + * if <code>px < 1</code> + * @see #getDefaultRowHeight() + */ + public void setDefaultRowHeight(double px) throws IllegalArgumentException; + + /** + * Returns the default height of the rows in this RowContainer. + * <p> + * This value will be equal to {@link #INITIAL_DEFAULT_ROW_HEIGHT} if the + * {@link Escalator} has not yet had a chance to autodetect the row height, + * or no explicit value has yet given via {@link #setDefaultRowHeight(int)} + * + * @return the default height of the rows in this RowContainer, in pixels + * @see #setDefaultRowHeight(int) + */ + public double getDefaultRowHeight(); + + /** + * Returns the cell object which contains information about the cell the + * element is in. + * + * @param element + * The element to get the cell for. If element is not present in + * row container then <code>null</code> is returned. + * + * @return the cell of the element, or <code>null</code> if element is not + * present in the {@link RowContainer}. + */ + public Cell getCell(Element element); + + /** + * Gets the row element with given logical index. For lazy loaded containers + * such as Escalators BodyRowContainer visibility should be checked before + * calling this function. See {@link Escalator#getVisibleRowRange()}. + * + * @param index + * the logical index of the element to retrieve + * @return the element at position {@code index} + * @throws IndexOutOfBoundsException + * if {@code index} is not valid within container + * @throws IllegalStateException + * if {@code index} is currently not available in the DOM + */ + public TableRowElement getRowElement(int index) + throws IndexOutOfBoundsException, IllegalStateException; + + /** + * Returns the root element of RowContainer + * + * @return RowContainer root element + */ + public Element getElement(); +} diff --git a/client/src/com/vaadin/client/widget/escalator/RowVisibilityChangeEvent.java b/client/src/com/vaadin/client/widget/escalator/RowVisibilityChangeEvent.java new file mode 100644 index 0000000000..968013b401 --- /dev/null +++ b/client/src/com/vaadin/client/widget/escalator/RowVisibilityChangeEvent.java @@ -0,0 +1,90 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.escalator; + +import com.google.gwt.event.shared.GwtEvent; + +/** + * Event fired when the range of visible rows changes e.g. because of scrolling. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class RowVisibilityChangeEvent extends + GwtEvent<RowVisibilityChangeHandler> { + /** + * The type of this event. + */ + public static final Type<RowVisibilityChangeHandler> TYPE = new Type<RowVisibilityChangeHandler>(); + + private final int firstVisibleRow; + private final int visibleRowCount; + + /** + * Creates a new row visibility change event + * + * @param firstVisibleRow + * the index of the first visible row + * @param visibleRowCount + * the number of visible rows + */ + public RowVisibilityChangeEvent(int firstVisibleRow, int visibleRowCount) { + this.firstVisibleRow = firstVisibleRow; + this.visibleRowCount = visibleRowCount; + } + + /** + * Gets the index of the first row that is at least partially visible. + * + * @return the index of the first visible row + */ + public int getFirstVisibleRow() { + return firstVisibleRow; + } + + /** + * Gets the number of at least partially visible rows. + * + * @return the number of visible rows + */ + public int getVisibleRowCount() { + return visibleRowCount; + } + + /* + * (non-Javadoc) + * + * @see com.google.gwt.event.shared.GwtEvent#getAssociatedType() + */ + @Override + public Type<RowVisibilityChangeHandler> getAssociatedType() { + return TYPE; + } + + /* + * (non-Javadoc) + * + * @see + * com.google.gwt.event.shared.GwtEvent#dispatch(com.google.gwt.event.shared + * .EventHandler) + */ + @Override + protected void dispatch(RowVisibilityChangeHandler handler) { + handler.onRowVisibilityChange(this); + } + +} diff --git a/client/src/com/vaadin/client/widget/escalator/RowVisibilityChangeHandler.java b/client/src/com/vaadin/client/widget/escalator/RowVisibilityChangeHandler.java new file mode 100644 index 0000000000..80a30184c0 --- /dev/null +++ b/client/src/com/vaadin/client/widget/escalator/RowVisibilityChangeHandler.java @@ -0,0 +1,38 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.escalator; + +import com.google.gwt.event.shared.EventHandler; + +/** + * Event handler that gets notified when the range of visible rows changes e.g. + * because of scrolling. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface RowVisibilityChangeHandler extends EventHandler { + + /** + * Called when the range of visible rows changes e.g. because of scrolling. + * + * @param event + * the row visibility change event describing the change + */ + void onRowVisibilityChange(RowVisibilityChangeEvent event); + +} diff --git a/client/src/com/vaadin/client/widget/escalator/ScrollbarBundle.java b/client/src/com/vaadin/client/widget/escalator/ScrollbarBundle.java new file mode 100644 index 0000000000..d7122329b7 --- /dev/null +++ b/client/src/com/vaadin/client/widget/escalator/ScrollbarBundle.java @@ -0,0 +1,854 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.escalator; + +import com.google.gwt.core.client.Scheduler; +import com.google.gwt.core.client.Scheduler.ScheduledCommand; +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.Style.Display; +import com.google.gwt.dom.client.Style.Overflow; +import com.google.gwt.dom.client.Style.Unit; +import com.google.gwt.event.shared.EventHandler; +import com.google.gwt.event.shared.GwtEvent; +import com.google.gwt.event.shared.HandlerManager; +import com.google.gwt.event.shared.HandlerRegistration; +import com.google.gwt.user.client.DOM; +import com.google.gwt.user.client.Event; +import com.google.gwt.user.client.EventListener; +import com.google.gwt.user.client.Timer; +import com.vaadin.client.DeferredWorker; +import com.vaadin.client.WidgetUtil; +import com.vaadin.client.widget.grid.events.ScrollEvent; +import com.vaadin.client.widget.grid.events.ScrollHandler; + +/** + * An element-like bundle representing a configurable and visual scrollbar in + * one axis. + * + * @since 7.4 + * @author Vaadin Ltd + * @see VerticalScrollbarBundle + * @see HorizontalScrollbarBundle + */ +public abstract class ScrollbarBundle implements DeferredWorker { + + private class ScrollEventFirer { + private final ScheduledCommand fireEventCommand = new ScheduledCommand() { + @Override + public void execute() { + + /* + * Some kind of native-scroll-event related asynchronous problem + * occurs here (at least on desktops) where the internal + * bookkeeping isn't up to date with the real scroll position. + * The weird thing is, that happens only once, and if you drag + * scrollbar fast enough. After it has failed once, it never + * fails again. + * + * Theory: the user drags the scrollbar, and this command is + * executed before the browser has a chance to fire a scroll + * event (which normally would correct this situation). This + * would explain why slow scrolling doesn't trigger the problem, + * while fast scrolling does. + * + * To make absolutely sure that we have the latest scroll + * position, let's update the internal value. + * + * This might lead to a slight performance hit (on my computer + * it was never more than 3ms on either of Chrome 38 or Firefox + * 31). It also _slightly_ counteracts the purpose of the + * internal bookkeeping. But since getScrollPos is called 3 + * times (on one direction) per scroll loop, it's still better + * to have take this small penalty than removing it altogether. + */ + updateScrollPosFromDom(); + + getHandlerManager().fireEvent(new ScrollEvent()); + isBeingFired = false; + } + }; + + private boolean isBeingFired; + + public void scheduleEvent() { + if (!isBeingFired) { + /* + * We'll gather all the scroll events, and only fire once, once + * everything has calmed down. + */ + Scheduler.get().scheduleDeferred(fireEventCommand); + isBeingFired = true; + } + } + } + + /** + * The orientation of the scrollbar. + */ + public enum Direction { + VERTICAL, HORIZONTAL; + } + + private class TemporaryResizer extends Object { + private static final int TEMPORARY_RESIZE_DELAY = 1000; + + private final Timer timer = new Timer() { + @Override + public void run() { + internalSetScrollbarThickness(1); + } + }; + + public void show() { + internalSetScrollbarThickness(OSX_INVISIBLE_SCROLLBAR_FAKE_SIZE_PX); + timer.schedule(TEMPORARY_RESIZE_DELAY); + } + } + + /** + * A means to listen to when the scrollbar handle in a + * {@link ScrollbarBundle} either appears or is removed. + */ + public interface VisibilityHandler extends EventHandler { + /** + * This method is called whenever the scrollbar handle's visibility is + * changed in a {@link ScrollbarBundle}. + * + * @param event + * the {@link VisibilityChangeEvent} + */ + void visibilityChanged(VisibilityChangeEvent event); + } + + public static class VisibilityChangeEvent extends + GwtEvent<VisibilityHandler> { + public static final Type<VisibilityHandler> TYPE = new Type<ScrollbarBundle.VisibilityHandler>() { + @Override + public String toString() { + return "VisibilityChangeEvent"; + } + }; + + private final boolean isScrollerVisible; + + private VisibilityChangeEvent(boolean isScrollerVisible) { + this.isScrollerVisible = isScrollerVisible; + } + + /** + * Checks whether the scroll handle is currently visible or not + * + * @return <code>true</code> if the scroll handle is currently visible. + * <code>false</code> if not. + */ + public boolean isScrollerVisible() { + return isScrollerVisible; + } + + @Override + public Type<VisibilityHandler> getAssociatedType() { + return TYPE; + } + + @Override + protected void dispatch(VisibilityHandler handler) { + handler.visibilityChanged(this); + } + } + + /** + * The pixel size for OSX's invisible scrollbars. + * <p> + * Touch devices don't show a scrollbar at all, so the scrollbar size is + * irrelevant in their case. There doesn't seem to be any other popular + * platforms that has scrollbars similar to OSX. Thus, this behavior is + * tailored for OSX only, until additional platforms start behaving this + * way. + */ + private static final int OSX_INVISIBLE_SCROLLBAR_FAKE_SIZE_PX = 13; + + /** + * A representation of a single vertical scrollbar. + * + * @see VerticalScrollbarBundle#getElement() + */ + public final static class VerticalScrollbarBundle extends ScrollbarBundle { + + @Override + public void setStylePrimaryName(String primaryStyleName) { + super.setStylePrimaryName(primaryStyleName); + root.addClassName(primaryStyleName + "-scroller-vertical"); + } + + @Override + protected void internalSetScrollPos(int px) { + root.setScrollTop(px); + } + + @Override + protected int internalGetScrollPos() { + return root.getScrollTop(); + } + + @Override + protected void internalSetScrollSize(double px) { + scrollSizeElement.getStyle().setHeight(px, Unit.PX); + } + + @Override + protected String internalGetScrollSize() { + return scrollSizeElement.getStyle().getHeight(); + } + + @Override + protected void internalSetOffsetSize(double px) { + root.getStyle().setHeight(px, Unit.PX); + } + + @Override + public String internalGetOffsetSize() { + return root.getStyle().getHeight(); + } + + @Override + protected void internalSetScrollbarThickness(double px) { + root.getStyle().setWidth(px, Unit.PX); + scrollSizeElement.getStyle().setWidth(px, Unit.PX); + } + + @Override + protected String internalGetScrollbarThickness() { + return root.getStyle().getWidth(); + } + + @Override + protected void internalForceScrollbar(boolean enable) { + if (enable) { + root.getStyle().setOverflowY(Overflow.SCROLL); + } else { + root.getStyle().clearOverflowY(); + } + } + + @Override + public Direction getDirection() { + return Direction.VERTICAL; + } + } + + /** + * A representation of a single horizontal scrollbar. + * + * @see HorizontalScrollbarBundle#getElement() + */ + public final static class HorizontalScrollbarBundle extends ScrollbarBundle { + + @Override + public void setStylePrimaryName(String primaryStyleName) { + super.setStylePrimaryName(primaryStyleName); + root.addClassName(primaryStyleName + "-scroller-horizontal"); + } + + @Override + protected void internalSetScrollPos(int px) { + root.setScrollLeft(px); + } + + @Override + protected int internalGetScrollPos() { + return root.getScrollLeft(); + } + + @Override + protected void internalSetScrollSize(double px) { + scrollSizeElement.getStyle().setWidth(px, Unit.PX); + } + + @Override + protected String internalGetScrollSize() { + return scrollSizeElement.getStyle().getWidth(); + } + + @Override + protected void internalSetOffsetSize(double px) { + root.getStyle().setWidth(px, Unit.PX); + } + + @Override + public String internalGetOffsetSize() { + return root.getStyle().getWidth(); + } + + @Override + protected void internalSetScrollbarThickness(double px) { + root.getStyle().setHeight(px, Unit.PX); + scrollSizeElement.getStyle().setHeight(px, Unit.PX); + } + + @Override + protected String internalGetScrollbarThickness() { + return root.getStyle().getHeight(); + } + + @Override + protected void internalForceScrollbar(boolean enable) { + if (enable) { + root.getStyle().setOverflowX(Overflow.SCROLL); + } else { + root.getStyle().clearOverflowX(); + } + } + + @Override + public Direction getDirection() { + return Direction.HORIZONTAL; + } + } + + protected final Element root = DOM.createDiv(); + protected final Element scrollSizeElement = DOM.createDiv(); + protected boolean isInvisibleScrollbar = false; + + private double scrollPos = 0; + private double maxScrollPos = 0; + + private boolean scrollHandleIsVisible = false; + + private boolean isLocked = false; + + /** @deprecarted access via {@link #getHandlerManager()} instead. */ + @Deprecated + private HandlerManager handlerManager; + + private TemporaryResizer invisibleScrollbarTemporaryResizer = new TemporaryResizer(); + + private final ScrollEventFirer scrollEventFirer = new ScrollEventFirer(); + + private HandlerRegistration scrollSizeTemporaryScrollHandler; + private HandlerRegistration offsetSizeTemporaryScrollHandler; + + private ScrollbarBundle() { + root.appendChild(scrollSizeElement); + root.getStyle().setDisplay(Display.NONE); + root.setTabIndex(-1); + } + + protected abstract String internalGetScrollSize(); + + /** + * Sets the primary style name + * + * @param primaryStyleName + * The primary style name to use + */ + public void setStylePrimaryName(String primaryStyleName) { + root.setClassName(primaryStyleName + "-scroller"); + } + + /** + * Gets the root element of this scrollbar-composition. + * + * @return the root element + */ + public final Element getElement() { + return root; + } + + /** + * Modifies the scroll position of this scrollbar by a number of pixels. + * <p> + * <em>Note:</em> Even though {@code double} values are used, they are + * currently only used as integers as large {@code int} (or small but fast + * {@code long}). This means, all values are truncated to zero decimal + * places. + * + * @param delta + * the delta in pixels to change the scroll position by + */ + public final void setScrollPosByDelta(double delta) { + if (delta != 0) { + setScrollPos(getScrollPos() + delta); + } + } + + /** + * Modifies {@link #root root's} dimensions in the axis the scrollbar is + * representing. + * + * @param px + * the new size of {@link #root} in the dimension this scrollbar + * is representing + */ + protected abstract void internalSetOffsetSize(double px); + + /** + * Sets the length of the scrollbar. + * <p> + * <em>Note:</em> Even though {@code double} values are used, they are + * currently only used as integers as large {@code int} (or small but fast + * {@code long}). This means, all values are truncated to zero decimal + * places. + * + * @param px + * the length of the scrollbar in pixels + */ + public final void setOffsetSize(final double px) { + + /* + * This needs to be made step-by-step because IE8 flat-out refuses to + * fire a scroll event when the scroll size becomes smaller than the + * offset size. All other browser need to suffer alongside. + */ + + boolean newOffsetSizeIsGreaterThanScrollSize = px > getScrollSize(); + boolean offsetSizeBecomesGreaterThanScrollSize = showsScrollHandle() + && newOffsetSizeIsGreaterThanScrollSize; + if (offsetSizeBecomesGreaterThanScrollSize && getScrollPos() != 0) { + // must be a field because Java insists. + offsetSizeTemporaryScrollHandler = addScrollHandler(new ScrollHandler() { + @Override + public void onScroll(ScrollEvent event) { + setOffsetSizeNow(px); + offsetSizeTemporaryScrollHandler.removeHandler(); + offsetSizeTemporaryScrollHandler = null; + } + }); + setScrollPos(0); + } else { + setOffsetSizeNow(px); + } + } + + private void setOffsetSizeNow(double px) { + internalSetOffsetSize(Math.max(0, truncate(px))); + recalculateMaxScrollPos(); + forceScrollbar(showsScrollHandle()); + fireVisibilityChangeIfNeeded(); + } + + /** + * Force the scrollbar to be visible with CSS. In practice, this means to + * set either <code>overflow-x</code> or <code>overflow-y</code> to " + * <code>scroll</code>" in the scrollbar's direction. + * <p> + * This is an IE8 workaround, since it doesn't always show scrollbars with + * <code>overflow: auto</code> enabled. + */ + protected void forceScrollbar(boolean enable) { + if (enable) { + root.getStyle().clearDisplay(); + } else { + root.getStyle().setDisplay(Display.NONE); + } + internalForceScrollbar(enable); + } + + protected abstract void internalForceScrollbar(boolean enable); + + /** + * Gets the length of the scrollbar + * + * @return the length of the scrollbar in pixels + */ + public double getOffsetSize() { + return parseCssDimensionToPixels(internalGetOffsetSize()); + } + + public abstract String internalGetOffsetSize(); + + /** + * Sets the scroll position of the scrollbar in the axis the scrollbar is + * representing. + * <p> + * <em>Note:</em> Even though {@code double} values are used, they are + * currently only used as integers as large {@code int} (or small but fast + * {@code long}). This means, all values are truncated to zero decimal + * places. + * + * @param px + * the new scroll position in pixels + */ + public final void setScrollPos(double px) { + if (isLocked()) { + return; + } + + double oldScrollPos = scrollPos; + scrollPos = Math.max(0, Math.min(maxScrollPos, truncate(px))); + + if (!WidgetUtil.pixelValuesEqual(oldScrollPos, scrollPos)) { + if (isInvisibleScrollbar) { + invisibleScrollbarTemporaryResizer.show(); + } + + /* + * This is where the value needs to be converted into an integer no + * matter how we flip it, since GWT expects an integer value. + * There's no point making a JSNI method that accepts doubles as the + * scroll position, since the browsers themselves don't support such + * large numbers (as of today, 25.3.2014). This double-ranged is + * only facilitating future virtual scrollbars. + */ + internalSetScrollPos(toInt32(scrollPos)); + } + } + + /** + * Truncates a double such that no decimal places are retained. + * <p> + * E.g. {@code trunc(2.3d) == 2.0d} and {@code trunc(-2.3d) == -2.0d}. + * + * @param num + * the double value to be truncated + * @return the {@code num} value without any decimal digits + */ + private static double truncate(double num) { + if (num > 0) { + return Math.floor(num); + } else { + return Math.ceil(num); + } + } + + /** + * Modifies the element's scroll position (scrollTop or scrollLeft). + * <p> + * <em>Note:</em> The parameter here is a type of integer (instead of a + * double) by design. The browsers internally convert all double values into + * an integer value. To make this fact explicit, this API has chosen to + * force integers already at this level. + * + * @param px + * integer pixel value to scroll to + */ + protected abstract void internalSetScrollPos(int px); + + /** + * Gets the scroll position of the scrollbar in the axis the scrollbar is + * representing. + * + * @return the new scroll position in pixels + */ + public final double getScrollPos() { + assert internalGetScrollPos() == toInt32(scrollPos) : "calculated scroll position (" + + toInt32(scrollPos) + + ") did not match the DOM element scroll position (" + + internalGetScrollPos() + ")"; + return scrollPos; + } + + /** + * Retrieves the element's scroll position (scrollTop or scrollLeft). + * <p> + * <em>Note:</em> The parameter here is a type of integer (instead of a + * double) by design. The browsers internally convert all double values into + * an integer value. To make this fact explicit, this API has chosen to + * force integers already at this level. + * + * @return integer pixel value of the scroll position + */ + protected abstract int internalGetScrollPos(); + + /** + * Modifies {@link #scrollSizeElement scrollSizeElement's} dimensions in + * such a way that the scrollbar is able to scroll a certain number of + * pixels in the axis it is representing. + * + * @param px + * the new size of {@link #scrollSizeElement} in the dimension + * this scrollbar is representing + */ + protected abstract void internalSetScrollSize(double px); + + /** + * Sets the amount of pixels the scrollbar needs to be able to scroll + * through. + * <p> + * <em>Note:</em> Even though {@code double} values are used, they are + * currently only used as integers as large {@code int} (or small but fast + * {@code long}). This means, all values are truncated to zero decimal + * places. + * + * @param px + * the number of pixels the scrollbar should be able to scroll + * through + */ + public final void setScrollSize(final double px) { + + /* + * This needs to be made step-by-step because IE8 flat-out refuses to + * fire a scroll event when the scroll size becomes smaller than the + * offset size. All other browser need to suffer alongside. + */ + + boolean newScrollSizeIsSmallerThanOffsetSize = px <= getOffsetSize(); + boolean scrollSizeBecomesSmallerThanOffsetSize = showsScrollHandle() + && newScrollSizeIsSmallerThanOffsetSize; + if (scrollSizeBecomesSmallerThanOffsetSize && getScrollPos() != 0) { + // must be a field because Java insists. + scrollSizeTemporaryScrollHandler = addScrollHandler(new ScrollHandler() { + @Override + public void onScroll(ScrollEvent event) { + setScrollSizeNow(px); + scrollSizeTemporaryScrollHandler.removeHandler(); + scrollSizeTemporaryScrollHandler = null; + } + }); + setScrollPos(0); + } else { + setScrollSizeNow(px); + } + } + + private void setScrollSizeNow(double px) { + internalSetScrollSize(Math.max(0, px)); + recalculateMaxScrollPos(); + forceScrollbar(showsScrollHandle()); + fireVisibilityChangeIfNeeded(); + } + + /** + * Gets the amount of pixels the scrollbar needs to be able to scroll + * through. + * + * @return the number of pixels the scrollbar should be able to scroll + * through + */ + public double getScrollSize() { + return parseCssDimensionToPixels(internalGetScrollSize()); + } + + /** + * Modifies {@link #scrollSizeElement scrollSizeElement's} dimensions in the + * opposite axis to what the scrollbar is representing. + * + * @param px + * the dimension that {@link #scrollSizeElement} should take in + * the opposite axis to what the scrollbar is representing + */ + protected abstract void internalSetScrollbarThickness(double px); + + /** + * Sets the scrollbar's thickness. + * <p> + * If the thickness is set to 0, the scrollbar will be treated as an + * "invisible" scrollbar. This means, the DOM structure will be given a + * non-zero size, but {@link #getScrollbarThickness()} will still return the + * value 0. + * + * @param px + * the scrollbar's thickness in pixels + */ + public final void setScrollbarThickness(double px) { + isInvisibleScrollbar = (px == 0); + + if (isInvisibleScrollbar) { + Event.sinkEvents(root, Event.ONSCROLL); + Event.setEventListener(root, new EventListener() { + @Override + public void onBrowserEvent(Event event) { + invisibleScrollbarTemporaryResizer.show(); + } + }); + } else { + Event.sinkEvents(root, 0); + Event.setEventListener(root, null); + } + + internalSetScrollbarThickness(Math.max(1d, px)); + } + + /** + * Gets the scrollbar's thickness as defined in the DOM. + * + * @return the scrollbar's thickness as defined in the DOM, in pixels + */ + protected abstract String internalGetScrollbarThickness(); + + /** + * Gets the scrollbar's thickness. + * <p> + * This value will differ from the value in the DOM, if the thickness was + * set to 0 with {@link #setScrollbarThickness(double)}, as the scrollbar is + * then treated as "invisible." + * + * @return the scrollbar's thickness in pixels + */ + public final double getScrollbarThickness() { + if (!isInvisibleScrollbar) { + return parseCssDimensionToPixels(internalGetScrollbarThickness()); + } else { + return 0; + } + } + + /** + * Checks whether the scrollbar's handle is visible. + * <p> + * In other words, this method checks whether the contents is larger than + * can visually fit in the element. + * + * @return <code>true</code> iff the scrollbar's handle is visible + */ + public boolean showsScrollHandle() { + return getOffsetSize() < getScrollSize(); + } + + public void recalculateMaxScrollPos() { + double scrollSize = getScrollSize(); + double offsetSize = getOffsetSize(); + maxScrollPos = Math.max(0, scrollSize - offsetSize); + + // make sure that the correct max scroll position is maintained. + setScrollPos(scrollPos); + } + + /** + * This is a method that JSNI can call to synchronize the object state from + * the DOM. + */ + private final void updateScrollPosFromDom() { + + /* + * TODO: this method probably shouldn't be called from Escalator's JSNI, + * but probably could be handled internally by this listening to its own + * element. Would clean up the code quite a bit. Needs further + * investigation. + */ + + int newScrollPos = internalGetScrollPos(); + if (!isLocked()) { + scrollPos = newScrollPos; + scrollEventFirer.scheduleEvent(); + } else if (scrollPos != newScrollPos) { + // we need to actually undo the setting of the scroll. + internalSetScrollPos(toInt32(scrollPos)); + } + } + + protected HandlerManager getHandlerManager() { + if (handlerManager == null) { + handlerManager = new HandlerManager(this); + } + return handlerManager; + } + + /** + * Adds handler for the scrollbar handle visibility. + * + * @param handler + * the {@link VisibilityHandler} to add + * @return {@link HandlerRegistration} used to remove the handler + */ + public HandlerRegistration addVisibilityHandler( + final VisibilityHandler handler) { + return getHandlerManager().addHandler(VisibilityChangeEvent.TYPE, + handler); + } + + private void fireVisibilityChangeIfNeeded() { + final boolean oldHandleIsVisible = scrollHandleIsVisible; + scrollHandleIsVisible = showsScrollHandle(); + if (oldHandleIsVisible != scrollHandleIsVisible) { + final VisibilityChangeEvent event = new VisibilityChangeEvent( + scrollHandleIsVisible); + getHandlerManager().fireEvent(event); + } + } + + /** + * Converts a double into an integer by JavaScript's terms. + * <p> + * Implementation copied from {@link Element#toInt32(double)}. + * + * @param val + * the double value to convert into an integer + * @return the double value converted to an integer + */ + private static native int toInt32(double val) + /*-{ + return val | 0; + }-*/; + + /** + * Locks or unlocks the scrollbar bundle. + * <p> + * A locked scrollbar bundle will refuse to scroll, both programmatically + * and via user-triggered events. + * + * @param isLocked + * <code>true</code> to lock, <code>false</code> to unlock + */ + public void setLocked(boolean isLocked) { + this.isLocked = isLocked; + } + + /** + * Checks whether the scrollbar bundle is locked or not. + * + * @return <code>true</code> iff the scrollbar bundle is locked + */ + public boolean isLocked() { + return isLocked; + } + + /** + * Returns the scroll direction of this scrollbar bundle. + * + * @return the scroll direction of this scrollbar bundle + */ + public abstract Direction getDirection(); + + /** + * Adds a scroll handler to the scrollbar bundle. + * + * @param handler + * the handler to add + * @return the registration object for the handler registration + */ + public HandlerRegistration addScrollHandler(final ScrollHandler handler) { + return getHandlerManager().addHandler(ScrollEvent.TYPE, handler); + } + + private static double parseCssDimensionToPixels(String size) { + + /* + * Sizes of elements are calculated from CSS rather than + * element.getOffset*() because those values are 0 whenever display: + * none. Because we know that all elements have populated + * CSS-dimensions, it's better to do it that way. + * + * Another solution would be to make the elements visible while + * measuring and then re-hide them, but that would cause unnecessary + * reflows that would probably kill the performance dead. + */ + + if (size.isEmpty()) { + return 0; + } else { + assert size.endsWith("px") : "Can't parse CSS dimension \"" + size + + "\""; + return Double.parseDouble(size.substring(0, size.length() - 2)); + } + } + + @Override + public boolean isWorkPending() { + return scrollSizeTemporaryScrollHandler != null + || offsetSizeTemporaryScrollHandler != null; + } +} diff --git a/client/src/com/vaadin/client/widget/grid/CellReference.java b/client/src/com/vaadin/client/widget/grid/CellReference.java new file mode 100644 index 0000000000..a2e841de43 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/CellReference.java @@ -0,0 +1,128 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid; + +import com.google.gwt.dom.client.TableCellElement; +import com.vaadin.client.widgets.Grid; + +/** + * A data class which contains information which identifies a cell in a + * {@link Grid}. + * <p> + * Since this class follows the <code>Flyweight</code>-pattern any instance of + * this object is subject to change without the user knowing it and so should + * not be stored anywhere outside of the method providing these instances. + * + * @author Vaadin Ltd + * @param <T> + * the type of the row object containing this cell + * @since 7.4 + */ +public class CellReference<T> { + private int columnIndex; + private Grid.Column<?, T> column; + private final RowReference<T> rowReference; + + public CellReference(RowReference<T> rowReference) { + this.rowReference = rowReference; + } + + /** + * Sets the identifying information for this cell. + * + * @param columnIndex + * the index of the column + * @param column + * the column object + */ + public void set(int columnIndex, Grid.Column<?, T> column) { + this.columnIndex = columnIndex; + this.column = column; + } + + /** + * Gets the grid that contains the referenced cell. + * + * @return the grid that contains referenced cell + */ + public Grid<T> getGrid() { + return rowReference.getGrid(); + } + + /** + * Gets the row index of the row. + * + * @return the index of the row + */ + public int getRowIndex() { + return rowReference.getRowIndex(); + } + + /** + * Gets the row data object. + * + * @return the row object + */ + public T getRow() { + return rowReference.getRow(); + } + + /** + * Gets the index of the column. + * + * @return the index of the column + */ + public int getColumnIndex() { + return columnIndex; + } + + /** + * Gets the column objects. + * + * @return the column object + */ + public Grid.Column<?, T> getColumn() { + return column; + } + + /** + * Gets the value of the cell. + * + * @return the value of the cell + */ + public Object getValue() { + return getColumn().getValue(getRow()); + } + + /** + * Get the element of the cell. + * + * @return the element of the cell + */ + public TableCellElement getElement() { + return rowReference.getElement().getCells().getItem(columnIndex); + } + + /** + * Gets the RowReference for this CellReference. + * + * @return the row reference + */ + protected RowReference<T> getRowReference() { + return rowReference; + } + +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/CellStyleGenerator.java b/client/src/com/vaadin/client/widget/grid/CellStyleGenerator.java new file mode 100644 index 0000000000..bbc540de64 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/CellStyleGenerator.java @@ -0,0 +1,40 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid; + +import com.vaadin.client.widgets.Grid; + +/** + * Callback interface for generating custom style names for cells + * + * @author Vaadin Ltd + * @param <T> + * the row type of the target grid + * @see Grid#setCellStyleGenerator(CellStyleGenerator) + * @since 7.4 + */ +public interface CellStyleGenerator<T> { + + /** + * Called by Grid to generate a style name for a column element. + * + * @param cellReference + * The cell to generate a style for + * @return the style name to add to this cell, or {@code null} to not set + * any style + */ + public abstract String getStyle(CellReference<T> cellReference); +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/DataAvailableEvent.java b/client/src/com/vaadin/client/widget/grid/DataAvailableEvent.java new file mode 100644 index 0000000000..d88fce4e11 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/DataAvailableEvent.java @@ -0,0 +1,55 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid; + +import com.google.gwt.event.shared.GwtEvent; +import com.vaadin.shared.ui.grid.Range; + +/** + * Event object describing a change of row availability in DataSource of a Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class DataAvailableEvent extends GwtEvent<DataAvailableHandler> { + + private Range rowsAvailable; + public static final Type<DataAvailableHandler> TYPE = new Type<DataAvailableHandler>(); + + public DataAvailableEvent(Range rowsAvailable) { + this.rowsAvailable = rowsAvailable; + } + + /** + * Returns the range of available rows in {@link DataSource} for this event. + * + * @return range of available rows + */ + public Range getAvailableRows() { + return rowsAvailable; + } + + @Override + public Type<DataAvailableHandler> getAssociatedType() { + return TYPE; + } + + @Override + protected void dispatch(DataAvailableHandler handler) { + handler.onDataAvailable(this); + } + +} diff --git a/client/src/com/vaadin/client/widget/grid/DataAvailableHandler.java b/client/src/com/vaadin/client/widget/grid/DataAvailableHandler.java new file mode 100644 index 0000000000..5e0650bc41 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/DataAvailableHandler.java @@ -0,0 +1,37 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid; + +import com.google.gwt.event.shared.EventHandler; + +/** + * Handler for {@link DataAvailableEvent}s. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface DataAvailableHandler extends EventHandler { + + /** + * Called when DataSource has data available. Supplied with row range. + * + * @param availableRows + * Range of rows available in the DataSource + * @return true if the command was successfully completed, false to call + * again the next time new data is available + */ + public void onDataAvailable(DataAvailableEvent event); +} diff --git a/client/src/com/vaadin/client/widget/grid/EditorHandler.java b/client/src/com/vaadin/client/widget/grid/EditorHandler.java new file mode 100644 index 0000000000..07ec1b231c --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/EditorHandler.java @@ -0,0 +1,243 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid; + +import com.google.gwt.user.client.ui.Widget; +import com.vaadin.client.widgets.Grid; + +/** + * An interface for binding widgets and data to the grid row editor. Used by the + * editor to support different row types, data sources and custom data binding + * mechanisms. + * + * @param <T> + * the row data type + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface EditorHandler<T> { + + /** + * A request class passed as a parameter to the editor handler methods. The + * request is callback-based to facilitate usage with remote or otherwise + * asynchronous data sources. + * <p> + * An implementation must call either {@link #success()} or {@link #fail()}, + * according to whether the operation was a success or failed during + * execution, respectively. + * + * @param <T> + * the row data type + */ + public static class EditorRequest<T> { + + /** + * A callback interface used to notify the invoker of the editor handler + * of completed editor requests. + * + * @param <T> + * the row data type + */ + public interface RequestCallback<T> { + /** + * The method that must be called when the request has been + * processed correctly. + * + * @param request + * the original request object + */ + public void onSuccess(EditorRequest<T> request); + + /** + * The method that must be called when processing the request has + * produced an aborting error. + * + * @param request + * the original request object + */ + public void onError(EditorRequest<T> request); + } + + private Grid<T> grid; + private int rowIndex; + private RequestCallback<T> callback; + private boolean completed = false; + + /** + * Creates a new editor request. + * + * @param rowIndex + * the index of the edited row + * @param callback + * the callback invoked when the request is ready, or null if + * no need to call back + */ + public EditorRequest(Grid<T> grid, int rowIndex, + RequestCallback<T> callback) { + this.grid = grid; + this.rowIndex = rowIndex; + this.callback = callback; + } + + /** + * Returns the index of the row being requested. + * + * @return the row index + */ + public int getRowIndex() { + return rowIndex; + } + + /** + * Returns the row data related to the row being requested. + * + * @return the row data + */ + public T getRow() { + return grid.getDataSource().getRow(rowIndex); + } + + /** + * Returns the grid instance related to this editor request. + * + * @return the grid instance + */ + public Grid<T> getGrid() { + return grid; + } + + /** + * Returns the editor widget used to edit the values of the given + * column. + * + * @param column + * the column whose widget to get + * @return the widget related to the column + */ + public Widget getWidget(Grid.Column<?, T> column) { + Widget w = grid.getEditorWidget(column); + assert w != null; + return w; + } + + /** + * Completes this request. The request can only be completed once. This + * method should only be called by an EditorHandler implementer if the + * request handling is asynchronous in nature and {@link #startAsync()} + * is previously invoked for this request. Synchronous requests are + * completed automatically by the editor. + * + * @throws IllegalStateException + * if the request is already completed + */ + private void complete() { + if (completed) { + throw new IllegalStateException( + "An EditorRequest must be completed exactly once"); + } + completed = true; + } + + /** + * Informs Grid that the editor request was a success. + */ + public void success() { + complete(); + if (callback != null) { + callback.onSuccess(this); + } + } + + /** + * Informs Grid that an error occurred while trying to process the + * request. + */ + public void fail() { + complete(); + if (callback != null) { + callback.onError(this); + } + } + + /** + * Checks whether the request is completed or not. + * + * @return <code>true</code> iff the request is completed + */ + public boolean isCompleted() { + return completed; + } + } + + /** + * Binds row data to the editor widgets. Called by the editor when it is + * opened for editing. + * <p> + * The implementation <em>must</em> call either + * {@link EditorRequest#success()} or {@link EditorRequest#fail()} to signal + * a successful or a failed (respectively) bind action. + * + * @param request + * the data binding request + * + * @see Grid#editRow(int) + */ + public void bind(EditorRequest<T> request); + + /** + * Called by the editor when editing is cancelled. This method may have an + * empty implementation in case no special processing is required. + * <p> + * In contrast to {@link #bind(EditorRequest)} and + * {@link #save(EditorRequest)}, any calls to + * {@link EditorRequest#success()} or {@link EditorRequest#fail()} have no + * effect on the outcome of the cancel action. The editor is already closed + * when this method is called. + * + * @param request + * the cancel request + * + * @see Grid#cancelEditor() + */ + public void cancel(EditorRequest<T> request); + + /** + * Commits changes in the currently active edit to the data source. Called + * by the editor when changes are saved. + * <p> + * The implementation <em>must</em> call either + * {@link EditorRequest#success()} or {@link EditorRequest#fail()} to signal + * a successful or a failed (respectively) save action. + * + * @param request + * the save request + * + * @see Grid#saveEditor() + */ + public void save(EditorRequest<T> request); + + /** + * Returns a widget instance that is used to edit the values in the given + * column. A null return value means the column is not editable. + * + * @param column + * the column whose values should be edited + * @return the editor widget for the column or null if the column is not + * editable + */ + public Widget getWidget(Grid.Column<?, T> column); +} diff --git a/client/src/com/vaadin/client/widget/grid/EventCellReference.java b/client/src/com/vaadin/client/widget/grid/EventCellReference.java new file mode 100644 index 0000000000..cf13798e11 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/EventCellReference.java @@ -0,0 +1,64 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid; + +import com.google.gwt.dom.client.TableCellElement; +import com.vaadin.client.widget.escalator.Cell; +import com.vaadin.client.widgets.Grid; + +/** + * A data class which contains information which identifies a cell being the + * target of an event from {@link Grid}. + * <p> + * Since this class follows the <code>Flyweight</code>-pattern any instance of + * this object is subject to change without the user knowing it and so should + * not be stored anywhere outside of the method providing these instances. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class EventCellReference<T> extends CellReference<T> { + + private Grid<T> grid; + private TableCellElement element; + + public EventCellReference(Grid<T> grid) { + super(new RowReference<T>(grid)); + this.grid = grid; + } + + /** + * Sets the RowReference and CellReference to point to given Cell. + * + * @param targetCell + * cell to point to + */ + public void set(Cell targetCell) { + int row = targetCell.getRow(); + int column = targetCell.getColumn(); + // At least for now we don't need to have the actual TableRowElement + // available. + getRowReference().set(row, grid.getDataSource().getRow(row), null); + set(column, grid.getColumn(column)); + + this.element = targetCell.getElement(); + } + + @Override + public TableCellElement getElement() { + return element; + } +} diff --git a/client/src/com/vaadin/client/widget/grid/RendererCellReference.java b/client/src/com/vaadin/client/widget/grid/RendererCellReference.java new file mode 100644 index 0000000000..533eafded6 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/RendererCellReference.java @@ -0,0 +1,89 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid; + +import com.google.gwt.dom.client.TableCellElement; +import com.vaadin.client.widget.escalator.FlyweightCell; +import com.vaadin.client.widgets.Grid; + +/** + * A data class which contains information which identifies a cell being + * rendered in a {@link Grid}. + * <p> + * Since this class follows the <code>Flyweight</code>-pattern any instance of + * this object is subject to change without the user knowing it and so should + * not be stored anywhere outside of the method providing these instances. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class RendererCellReference extends CellReference<Object> { + + /** + * Creates a new renderer cell reference bound to a row reference. + * + * @param rowReference + * the row reference to bind to + */ + public RendererCellReference(RowReference<Object> rowReference) { + super(rowReference); + } + + private FlyweightCell cell; + + /** + * Sets the identifying information for this cell. + * + * @param cell + * the flyweight cell to reference + * @param column + * the column to reference + */ + public void set(FlyweightCell cell, Grid.Column<?, ?> column) { + this.cell = cell; + super.set(cell.getColumn(), (Grid.Column<?, Object>) column); + } + + /** + * Returns the element of the cell. Can be either a <code>TD</code> element + * or a <code>TH</code> element. + * + * @return the element of the cell + */ + @Override + public TableCellElement getElement() { + return cell.getElement(); + } + + /** + * Sets the colspan attribute of the element of this cell. + * + * @param numberOfCells + * the number of columns that the cell should span + */ + public void setColSpan(int numberOfCells) { + cell.setColSpan(numberOfCells); + } + + /** + * Gets the colspan attribute of the element of this cell. + * + * @return the number of columns that the cell should span + */ + public int getColSpan() { + return cell.getColSpan(); + } +} diff --git a/client/src/com/vaadin/client/widget/grid/RowReference.java b/client/src/com/vaadin/client/widget/grid/RowReference.java new file mode 100644 index 0000000000..8874fcc5cc --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/RowReference.java @@ -0,0 +1,104 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid; + +import com.google.gwt.dom.client.TableRowElement; +import com.vaadin.client.widgets.Grid; + +/** + * A data class which contains information which identifies a row in a + * {@link Grid}. + * <p> + * Since this class follows the <code>Flyweight</code>-pattern any instance of + * this object is subject to change without the user knowing it and so should + * not be stored anywhere outside of the method providing these instances. + * + * @author Vaadin Ltd + * @param <T> + * the row object type + * @since 7.4 + */ +public class RowReference<T> { + private final Grid<T> grid; + + private int rowIndex; + private T row; + + private TableRowElement element; + + /** + * Creates a new row reference for the given grid. + * + * @param grid + * the grid that the row belongs to + */ + public RowReference(Grid<T> grid) { + this.grid = grid; + } + + /** + * Sets the identifying information for this row. + * + * @param rowIndex + * the index of the row + * @param row + * the row object + * @param elemenet + * the element of the row + */ + public void set(int rowIndex, T row, TableRowElement element) { + this.rowIndex = rowIndex; + this.row = row; + this.element = element; + } + + /** + * Gets the grid that contains the referenced row. + * + * @return the grid that contains referenced row + */ + public Grid<T> getGrid() { + return grid; + } + + /** + * Gets the row index of the row. + * + * @return the index of the row + */ + public int getRowIndex() { + return rowIndex; + } + + /** + * Gets the row data object. + * + * @return the row object + */ + public T getRow() { + return row; + } + + /** + * Gets the table row element of the row. + * + * @return the element of the row + */ + public TableRowElement getElement() { + return element; + } + +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/RowStyleGenerator.java b/client/src/com/vaadin/client/widget/grid/RowStyleGenerator.java new file mode 100644 index 0000000000..a12a9ff47d --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/RowStyleGenerator.java @@ -0,0 +1,40 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid; + +import java.io.Serializable; + +/** + * Callback interface for generating custom style names for data rows + * + * @author Vaadin Ltd + * @param <T> + * the row type of the target grid + * @see Grid#setRowStyleGenerator(RowStyleGenerator) + * @since 7.4 + */ +public interface RowStyleGenerator<T> extends Serializable { + + /** + * Called by Grid to generate a style name for a row. + * + * @param rowReference + * The row to generate a style for + * @return the style name to add to this row, or {@code null} to not set any + * style + */ + public abstract String getStyle(RowReference<T> rowReference); +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/datasources/ListDataSource.java b/client/src/com/vaadin/client/widget/grid/datasources/ListDataSource.java new file mode 100644 index 0000000000..56e1db5c36 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/datasources/ListDataSource.java @@ -0,0 +1,464 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.datasources; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +import com.vaadin.client.data.DataChangeHandler; +import com.vaadin.client.data.DataSource; +import com.vaadin.client.widget.grid.events.SelectAllEvent; +import com.vaadin.client.widget.grid.events.SelectAllHandler; +import com.vaadin.shared.util.SharedUtil; + +/** + * A simple list based on an in-memory data source for simply adding a list of + * row pojos to the grid. Based on a wrapped list instance which supports adding + * and removing of items. + * + * <p> + * Usage: + * + * <pre> + * ListDataSource<Integer> ds = new ListDataSource<Integer>(1, 2, 3, 4); + * + * // Add item to the data source + * ds.asList().add(5); + * + * // Remove item from the data source + * ds.asList().remove(3); + * + * // Add multiple items + * ds.asList().addAll(Arrays.asList(5, 6, 7)); + * </pre> + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class ListDataSource<T> implements DataSource<T> { + + private class RowHandleImpl extends RowHandle<T> { + + private final T row; + + public RowHandleImpl(T row) { + this.row = row; + } + + @Override + public T getRow() { + /* + * We'll cheat here and don't throw an IllegalStateException even if + * this isn't pinned, because we know that the reference never gets + * stale. + */ + return row; + } + + @Override + public void pin() { + // NOOP, really + } + + @Override + public void unpin() throws IllegalStateException { + /* + * Just to make things easier for everyone, we won't throw the + * exception, even in illegal situations. + */ + } + + @Override + protected boolean equalsExplicit(Object obj) { + if (obj instanceof ListDataSource.RowHandleImpl) { + /* + * Java prefers AbstractRemoteDataSource<?>.RowHandleImpl. I + * like the @SuppressWarnings more (keeps the line length in + * check.) + */ + @SuppressWarnings("unchecked") + RowHandleImpl rhi = (RowHandleImpl) obj; + return SharedUtil.equals(row, rhi.row); + } else { + return false; + } + } + + @Override + protected int hashCodeExplicit() { + return row.hashCode(); + } + + @Override + public void updateRow() { + changeHandler.dataUpdated(ds.indexOf(getRow()), 1); + } + } + + /** + * Wraps the datasource list and notifies the change handler of changing to + * the list + */ + private class ListWrapper implements List<T> { + + @Override + public int size() { + return ds.size(); + } + + @Override + public boolean isEmpty() { + return ds.isEmpty(); + } + + @Override + public boolean contains(Object o) { + return contains(o); + } + + @Override + public Iterator<T> iterator() { + return new ListWrapperIterator(ds.iterator()); + } + + @Override + public Object[] toArray() { + return ds.toArray(); + } + + @Override + @SuppressWarnings("hiding") + public <T> T[] toArray(T[] a) { + return toArray(a); + } + + @Override + public boolean add(T e) { + if (ds.add(e)) { + if (changeHandler != null) { + changeHandler.dataAdded(ds.size() - 1, 1); + } + return true; + } + return false; + } + + @Override + public boolean remove(Object o) { + int index = ds.indexOf(o); + if (ds.remove(o)) { + if (changeHandler != null) { + changeHandler.dataRemoved(index, 1); + } + return true; + } + return false; + } + + @Override + public boolean containsAll(Collection<?> c) { + return ds.containsAll(c); + } + + @Override + public boolean addAll(Collection<? extends T> c) { + int idx = ds.size(); + if (ds.addAll(c)) { + if (changeHandler != null) { + changeHandler.dataAdded(idx, c.size()); + } + return true; + } + return false; + } + + @Override + public boolean addAll(int index, Collection<? extends T> c) { + if (ds.addAll(index, c)) { + if (changeHandler != null) { + changeHandler.dataAdded(index, c.size()); + } + return true; + } + return false; + } + + @Override + public boolean removeAll(Collection<?> c) { + if (ds.removeAll(c)) { + if (changeHandler != null) { + // Have to update the whole list as the removal does not + // have to be a continuous range + changeHandler.dataUpdated(0, ds.size()); + changeHandler.dataAvailable(0, ds.size()); + } + return true; + } + return false; + } + + @Override + public boolean retainAll(Collection<?> c) { + if (ds.retainAll(c)) { + if (changeHandler != null) { + // Have to update the whole list as the retain does not + // have to be a continuous range + changeHandler.dataUpdated(0, ds.size()); + changeHandler.dataAvailable(0, ds.size()); + } + return true; + } + return false; + } + + @Override + public void clear() { + int size = ds.size(); + ds.clear(); + if (changeHandler != null) { + changeHandler.dataRemoved(0, size); + } + } + + @Override + public T get(int index) { + return ds.get(index); + } + + @Override + public T set(int index, T element) { + T prev = ds.set(index, element); + if (changeHandler != null) { + changeHandler.dataUpdated(index, 1); + } + return prev; + } + + @Override + public void add(int index, T element) { + ds.add(index, element); + if (changeHandler != null) { + changeHandler.dataAdded(index, 1); + } + } + + @Override + public T remove(int index) { + T removed = ds.remove(index); + if (changeHandler != null) { + changeHandler.dataRemoved(index, 1); + } + return removed; + } + + @Override + public int indexOf(Object o) { + return ds.indexOf(o); + } + + @Override + public int lastIndexOf(Object o) { + return ds.lastIndexOf(o); + } + + @Override + public ListIterator<T> listIterator() { + // TODO could be implemented by a custom iterator. + throw new UnsupportedOperationException( + "List iterators not supported at this time."); + } + + @Override + public ListIterator<T> listIterator(int index) { + // TODO could be implemented by a custom iterator. + throw new UnsupportedOperationException( + "List iterators not supported at this time."); + } + + @Override + public List<T> subList(int fromIndex, int toIndex) { + throw new UnsupportedOperationException("Sub lists not supported."); + } + } + + /** + * Iterator returned by {@link ListWrapper} + */ + private class ListWrapperIterator implements Iterator<T> { + + private final Iterator<T> iterator; + + /** + * Constructs a new iterator + */ + public ListWrapperIterator(Iterator<T> iterator) { + this.iterator = iterator; + } + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + public T next() { + return iterator.next(); + } + + @Override + public void remove() { + throw new UnsupportedOperationException( + "Iterator.remove() is not supported by this iterator."); + } + } + + /** + * Datasource for providing row pojo's + */ + private final List<T> ds; + + /** + * Wrapper that wraps the data source + */ + private final ListWrapper wrapper; + + /** + * Handler for listening to changes in the underlying list. + */ + private DataChangeHandler changeHandler; + + /** + * Constructs a new list data source. + * <p> + * Note: Modifications to the original list will not be reflected in the + * data source after the data source has been constructed. To add or remove + * items to the data source after it has been constructed use + * {@link ListDataSource#asList()}. + * + * + * @param datasource + * The list to use for providing the data to the grid + */ + public ListDataSource(List<T> datasource) { + if (datasource == null) { + throw new IllegalArgumentException("datasource cannot be null"); + } + ds = new ArrayList<T>(datasource); + wrapper = new ListWrapper(); + } + + /** + * Constructs a data source with a set of rows. You can dynamically add and + * remove rows from the data source via the list you get from + * {@link ListDataSource#asList()} + * + * @param rows + * The rows to initially add to the data source + */ + public ListDataSource(T... rows) { + if (rows == null) { + ds = new ArrayList<T>(); + } else { + ds = new ArrayList<T>(Arrays.asList(rows)); + } + wrapper = new ListWrapper(); + } + + @Override + public void ensureAvailability(int firstRowIndex, int numberOfRows) { + if (firstRowIndex >= ds.size()) { + throw new IllegalStateException( + "Trying to fetch rows outside of array"); + } + changeHandler.dataAvailable(firstRowIndex, numberOfRows); + } + + @Override + public T getRow(int rowIndex) { + return ds.get(rowIndex); + } + + @Override + public int size() { + return ds.size(); + } + + @Override + public void setDataChangeHandler(DataChangeHandler dataChangeHandler) { + this.changeHandler = dataChangeHandler; + } + + /** + * Gets the list that backs this datasource. Any changes made to this list + * will be reflected in the datasource. + * <p> + * Note: The list is not the same list as passed into the data source via + * the constructor. + * + * @return Returns a list implementation that wraps the real list that backs + * the data source and provides events for the data source + * listeners. + */ + public List<T> asList() { + return wrapper; + } + + @Override + public RowHandle<T> getHandle(T row) throws IllegalStateException { + assert ds.contains(row) : "This data source doesn't contain the row " + + row; + return new RowHandleImpl(row); + } + + /** + * Sort entire container according to a {@link Comparator}. + * + * @param comparator + * a comparator object, which compares two data source entries + * (beans/pojos) + */ + public void sort(Comparator<T> comparator) { + Collections.sort(ds, comparator); + if (changeHandler != null) { + changeHandler.dataUpdated(0, ds.size()); + } + } + + @Override + public int indexOf(T row) { + return ds.indexOf(row); + } + + /** + * Returns a {@link SelectAllHandler} for this ListDataSource. + * + * @return select all handler + */ + public SelectAllHandler<T> getSelectAllHandler() { + return new SelectAllHandler<T>() { + @Override + public void onSelectAll(SelectAllEvent<T> event) { + event.getSelectionModel().select(asList()); + } + }; + } +} diff --git a/client/src/com/vaadin/client/widget/grid/datasources/ListSorter.java b/client/src/com/vaadin/client/widget/grid/datasources/ListSorter.java new file mode 100644 index 0000000000..69bea629b0 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/datasources/ListSorter.java @@ -0,0 +1,175 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.datasources; + +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.google.gwt.event.shared.HandlerRegistration; +import com.vaadin.client.data.DataSource; +import com.vaadin.client.widget.grid.sort.SortEvent; +import com.vaadin.client.widget.grid.sort.SortHandler; +import com.vaadin.client.widget.grid.sort.SortOrder; +import com.vaadin.client.widgets.Grid; +import com.vaadin.shared.data.sort.SortDirection; + +/** + * Provides sorting facility from Grid for the {@link ListDataSource} in-memory + * data source. + * + * @author Vaadin Ltd + * @param <T> + * Grid row data type + * @since 7.4 + */ +public class ListSorter<T> { + + private Grid<T> grid; + private Map<Grid.Column<?, T>, Comparator<?>> comparators; + private HandlerRegistration sortHandlerRegistration; + + public ListSorter(Grid<T> grid) { + + if (grid == null) { + throw new IllegalArgumentException("Grid can not be null"); + } + + this.grid = grid; + comparators = new HashMap<Grid.Column<?, T>, Comparator<?>>(); + + sortHandlerRegistration = grid.addSortHandler(new SortHandler<T>() { + @Override + public void sort(SortEvent<T> event) { + ListSorter.this.sort(event.getOrder()); + } + }); + } + + /** + * Detach this Sorter from the Grid. This unregisters the sort event handler + * which was used to apply sorting to the ListDataSource. + */ + public void removeFromGrid() { + sortHandlerRegistration.removeHandler(); + } + + /** + * Assign or remove a comparator for a column. This comparator method, if + * defined, is always used in favour of 'natural' comparison of objects + * (i.e. the compareTo of objects implementing the Comparable interface, + * which includes all standard data classes like String, Number derivatives + * and Dates). Any existing comparator can be removed by passing in a + * non-null GridColumn and a null Comparator. + * + * @param column + * a grid column. May not be null. + * @param comparator + * comparator method for the values returned by the grid column. + * If null, any existing comparator is removed. + */ + public <C> void setComparator(Grid.Column<C, T> column, + Comparator<C> comparator) { + if (column == null) { + throw new IllegalArgumentException( + "Column reference can not be null"); + } + if (comparator == null) { + comparators.remove(column); + } else { + comparators.put(column, comparator); + } + } + + /** + * Retrieve the comparator assigned for a specific grid column. + * + * @param column + * a grid column. May not be null. + * @return a comparator, or null if no comparator for the specified grid + * column has been set. + */ + @SuppressWarnings("unchecked") + public <C> Comparator<C> getComparator(Grid.Column<C, T> column) { + if (column == null) { + throw new IllegalArgumentException( + "Column reference can not be null"); + } + return (Comparator<C>) comparators.get(column); + } + + /** + * Remove all comparator mappings. Useful if the data source has changed but + * this Sorter is being re-used. + */ + public void clearComparators() { + comparators.clear(); + } + + /** + * Apply sorting to the current ListDataSource. + * + * @param order + * the sort order list provided by the grid sort event + */ + private void sort(final List<SortOrder> order) { + DataSource<T> ds = grid.getDataSource(); + if (!(ds instanceof ListDataSource)) { + throw new IllegalStateException("Grid " + grid + + " data source is not a ListDataSource!"); + } + + ((ListDataSource<T>) ds).sort(new Comparator<T>() { + + @Override + @SuppressWarnings({ "rawtypes", "unchecked" }) + public int compare(T a, T b) { + + for (SortOrder o : order) { + + Grid.Column column = o.getColumn(); + Comparator cmp = ListSorter.this.comparators.get(column); + int result = 0; + Object value_a = column.getValue(a); + Object value_b = column.getValue(b); + if (cmp != null) { + result = cmp.compare(value_a, value_b); + } else { + if (!(value_a instanceof Comparable)) { + throw new IllegalStateException("Column " + column + + " has no assigned comparator and value " + + value_a + " isn't naturally comparable"); + } + result = ((Comparable) value_a).compareTo(value_b); + } + + if (result != 0) { + return o.getDirection() == SortDirection.ASCENDING ? result + : -result; + } + } + + if (order.size() > 0) { + return order.get(0).getDirection() == SortDirection.ASCENDING ? a + .hashCode() - b.hashCode() + : b.hashCode() - a.hashCode(); + } + return a.hashCode() - b.hashCode(); + } + }); + } +} diff --git a/client/src/com/vaadin/client/widget/grid/events/AbstractGridKeyEventHandler.java b/client/src/com/vaadin/client/widget/grid/events/AbstractGridKeyEventHandler.java new file mode 100644 index 0000000000..120c32d380 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/AbstractGridKeyEventHandler.java @@ -0,0 +1,44 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.google.gwt.event.shared.EventHandler; +import com.vaadin.client.widgets.Grid.AbstractGridKeyEvent; + +/** + * Base interface of all handlers for {@link AbstractGridKeyEvent}s. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public abstract interface AbstractGridKeyEventHandler extends EventHandler { + + public abstract interface GridKeyDownHandler extends + AbstractGridKeyEventHandler { + public void onKeyDown(GridKeyDownEvent event); + } + + public abstract interface GridKeyUpHandler extends + AbstractGridKeyEventHandler { + public void onKeyUp(GridKeyUpEvent event); + } + + public abstract interface GridKeyPressHandler extends + AbstractGridKeyEventHandler { + public void onKeyPress(GridKeyPressEvent event); + } + +} diff --git a/client/src/com/vaadin/client/widget/grid/events/AbstractGridMouseEventHandler.java b/client/src/com/vaadin/client/widget/grid/events/AbstractGridMouseEventHandler.java new file mode 100644 index 0000000000..15e22a6d57 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/AbstractGridMouseEventHandler.java @@ -0,0 +1,39 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.google.gwt.event.shared.EventHandler; +import com.vaadin.client.widgets.Grid.AbstractGridMouseEvent; + +/** + * Base interface of all handlers for {@link AbstractGridMouseEvent}s. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public abstract interface AbstractGridMouseEventHandler extends EventHandler { + + public abstract interface GridClickHandler extends + AbstractGridMouseEventHandler { + public void onClick(GridClickEvent event); + } + + public abstract interface GridDoubleClickHandler extends + AbstractGridMouseEventHandler { + public void onDoubleClick(GridDoubleClickEvent event); + } + +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/events/BodyClickHandler.java b/client/src/com/vaadin/client/widget/grid/events/BodyClickHandler.java new file mode 100644 index 0000000000..a66e170524 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/BodyClickHandler.java @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.vaadin.client.widget.grid.events.AbstractGridMouseEventHandler.GridClickHandler; + +/** + * Handler for {@link GridClickEvent}s that happen in the body of the Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface BodyClickHandler extends GridClickHandler { + +} diff --git a/client/src/com/vaadin/client/widget/grid/events/BodyDoubleClickHandler.java b/client/src/com/vaadin/client/widget/grid/events/BodyDoubleClickHandler.java new file mode 100644 index 0000000000..a7be5bad24 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/BodyDoubleClickHandler.java @@ -0,0 +1,29 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.vaadin.client.widget.grid.events.AbstractGridMouseEventHandler.GridDoubleClickHandler; + +/** + * Handler for {@link GridDoubleClickEvent}s that happen in the body of the + * Grid. + * + * @since + * @author Vaadin Ltd + */ +public interface BodyDoubleClickHandler extends GridDoubleClickHandler { + +} diff --git a/client/src/com/vaadin/client/widget/grid/events/BodyKeyDownHandler.java b/client/src/com/vaadin/client/widget/grid/events/BodyKeyDownHandler.java new file mode 100644 index 0000000000..ff1ae82d2e --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/BodyKeyDownHandler.java @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyDownHandler; + +/** + * Handler for {@link GridKeyDownEvent}s that happen when the focused cell is in + * the body of the Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface BodyKeyDownHandler extends GridKeyDownHandler { +} diff --git a/client/src/com/vaadin/client/widget/grid/events/BodyKeyPressHandler.java b/client/src/com/vaadin/client/widget/grid/events/BodyKeyPressHandler.java new file mode 100644 index 0000000000..245250d4c0 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/BodyKeyPressHandler.java @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyPressHandler; + +/** + * Handler for {@link GridKeyPressEvent}s that happen when the focused cell is + * in the body of the Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface BodyKeyPressHandler extends GridKeyPressHandler { +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/events/BodyKeyUpHandler.java b/client/src/com/vaadin/client/widget/grid/events/BodyKeyUpHandler.java new file mode 100644 index 0000000000..2c0951ea40 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/BodyKeyUpHandler.java @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyUpHandler; + +/** + * Handler for {@link GridKeyUpEvent}s that happen when the focused cell is in + * the body of the Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface BodyKeyUpHandler extends GridKeyUpHandler { +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/events/FooterClickHandler.java b/client/src/com/vaadin/client/widget/grid/events/FooterClickHandler.java new file mode 100644 index 0000000000..51fa38c948 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/FooterClickHandler.java @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.vaadin.client.widget.grid.events.AbstractGridMouseEventHandler.GridClickHandler; + +/** + * Handler for {@link GridClickEvent}s that happen in the footer of the Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface FooterClickHandler extends GridClickHandler { + +} diff --git a/client/src/com/vaadin/client/widget/grid/events/FooterDoubleClickHandler.java b/client/src/com/vaadin/client/widget/grid/events/FooterDoubleClickHandler.java new file mode 100644 index 0000000000..3bb9c9ee72 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/FooterDoubleClickHandler.java @@ -0,0 +1,29 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.vaadin.client.widget.grid.events.AbstractGridMouseEventHandler.GridDoubleClickHandler; + +/** + * Handler for {@link GridDoubleClickEvent}s that happen in the footer of the + * Grid. + * + * @since + * @author Vaadin Ltd + */ +public interface FooterDoubleClickHandler extends GridDoubleClickHandler { + +} diff --git a/client/src/com/vaadin/client/widget/grid/events/FooterKeyDownHandler.java b/client/src/com/vaadin/client/widget/grid/events/FooterKeyDownHandler.java new file mode 100644 index 0000000000..85f83970f2 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/FooterKeyDownHandler.java @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyDownHandler; + +/** + * Handler for {@link GridKeyDownEvent}s that happen when the focused cell is in + * the footer of the Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface FooterKeyDownHandler extends GridKeyDownHandler { +} diff --git a/client/src/com/vaadin/client/widget/grid/events/FooterKeyPressHandler.java b/client/src/com/vaadin/client/widget/grid/events/FooterKeyPressHandler.java new file mode 100644 index 0000000000..09778f6873 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/FooterKeyPressHandler.java @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyPressHandler; + +/** + * Handler for {@link GridKeyPressEvent}s that happen when the focused cell is + * in the footer of the Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface FooterKeyPressHandler extends GridKeyPressHandler { +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/events/FooterKeyUpHandler.java b/client/src/com/vaadin/client/widget/grid/events/FooterKeyUpHandler.java new file mode 100644 index 0000000000..688f89880f --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/FooterKeyUpHandler.java @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyUpHandler; + +/** + * Handler for {@link GridKeyUpEvent}s that happen when the focused cell is in + * the footer of the Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface FooterKeyUpHandler extends GridKeyUpHandler { +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/events/GridClickEvent.java b/client/src/com/vaadin/client/widget/grid/events/GridClickEvent.java new file mode 100644 index 0000000000..ade878abc6 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/GridClickEvent.java @@ -0,0 +1,50 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.google.gwt.dom.client.BrowserEvents; +import com.vaadin.client.widget.grid.CellReference; +import com.vaadin.client.widget.grid.events.AbstractGridMouseEventHandler.GridClickHandler; +import com.vaadin.client.widgets.Grid; +import com.vaadin.client.widgets.Grid.AbstractGridMouseEvent; +import com.vaadin.client.widgets.Grid.Section; + +/** + * Represents native mouse click event in Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class GridClickEvent extends AbstractGridMouseEvent<GridClickHandler> { + + public GridClickEvent(Grid<?> grid, CellReference<?> targetCell) { + super(grid, targetCell); + } + + @Override + protected String getBrowserEventType() { + return BrowserEvents.CLICK; + } + + @Override + protected void doDispatch(GridClickHandler handler, Section section) { + if ((section == Section.BODY && handler instanceof BodyClickHandler) + || (section == Section.HEADER && handler instanceof HeaderClickHandler) + || (section == Section.FOOTER && handler instanceof FooterClickHandler)) { + handler.onClick(this); + } + } +} diff --git a/client/src/com/vaadin/client/widget/grid/events/GridDoubleClickEvent.java b/client/src/com/vaadin/client/widget/grid/events/GridDoubleClickEvent.java new file mode 100644 index 0000000000..20e432aa85 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/GridDoubleClickEvent.java @@ -0,0 +1,52 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.google.gwt.dom.client.BrowserEvents; +import com.vaadin.client.widget.grid.CellReference; +import com.vaadin.client.widget.grid.events.AbstractGridMouseEventHandler.GridDoubleClickHandler; +import com.vaadin.client.widgets.Grid; +import com.vaadin.client.widgets.Grid.AbstractGridMouseEvent; +import com.vaadin.client.widgets.Grid.Section; + +/** + * Represents native mouse double click event in Grid. + * + * @since + * @author Vaadin Ltd + */ +public class GridDoubleClickEvent extends + AbstractGridMouseEvent<GridDoubleClickHandler> { + + public GridDoubleClickEvent(Grid<?> grid, CellReference<?> targetCell) { + super(grid, targetCell); + } + + @Override + protected String getBrowserEventType() { + return BrowserEvents.DBLCLICK; + } + + @Override + protected void doDispatch(GridDoubleClickHandler handler, Section section) { + if ((section == Section.BODY && handler instanceof BodyDoubleClickHandler) + || (section == Section.HEADER && handler instanceof HeaderDoubleClickHandler) + || (section == Section.FOOTER && handler instanceof FooterDoubleClickHandler)) { + handler.onDoubleClick(this); + } + } + +} diff --git a/client/src/com/vaadin/client/widget/grid/events/GridKeyDownEvent.java b/client/src/com/vaadin/client/widget/grid/events/GridKeyDownEvent.java new file mode 100644 index 0000000000..2ca7448849 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/GridKeyDownEvent.java @@ -0,0 +1,121 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.google.gwt.dom.client.BrowserEvents; +import com.google.gwt.event.dom.client.KeyCodes; +import com.vaadin.client.widget.grid.CellReference; +import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyDownHandler; +import com.vaadin.client.widgets.Grid; +import com.vaadin.client.widgets.Grid.AbstractGridKeyEvent; +import com.vaadin.client.widgets.Grid.Section; + +/** + * Represents native key down event in Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class GridKeyDownEvent extends AbstractGridKeyEvent<GridKeyDownHandler> { + + public GridKeyDownEvent(Grid<?> grid, CellReference<?> targetCell) { + super(grid, targetCell); + } + + @Override + protected void doDispatch(GridKeyDownHandler handler, Section section) { + if ((section == Section.BODY && handler instanceof BodyKeyDownHandler) + || (section == Section.HEADER && handler instanceof HeaderKeyDownHandler) + || (section == Section.FOOTER && handler instanceof FooterKeyDownHandler)) { + handler.onKeyDown(this); + } + } + + @Override + protected String getBrowserEventType() { + return BrowserEvents.KEYDOWN; + } + + /** + * Does the key code represent an arrow key? + * + * @param keyCode + * the key code + * @return if it is an arrow key code + */ + public static boolean isArrow(int keyCode) { + switch (keyCode) { + case KeyCodes.KEY_DOWN: + case KeyCodes.KEY_RIGHT: + case KeyCodes.KEY_UP: + case KeyCodes.KEY_LEFT: + return true; + default: + return false; + } + } + + /** + * Gets the native key code. These key codes are enumerated in the + * {@link KeyCodes} class. + * + * @return the key code + */ + public int getNativeKeyCode() { + return getNativeEvent().getKeyCode(); + } + + /** + * Is this a key down arrow? + * + * @return whether this is a down arrow key event + */ + public boolean isDownArrow() { + return getNativeKeyCode() == KeyCodes.KEY_DOWN; + } + + /** + * Is this a left arrow? + * + * @return whether this is a left arrow key event + */ + public boolean isLeftArrow() { + return getNativeKeyCode() == KeyCodes.KEY_LEFT; + } + + /** + * Is this a right arrow? + * + * @return whether this is a right arrow key event + */ + public boolean isRightArrow() { + return getNativeKeyCode() == KeyCodes.KEY_RIGHT; + } + + /** + * Is this a up arrow? + * + * @return whether this is a right arrow key event + */ + public boolean isUpArrow() { + return getNativeKeyCode() == KeyCodes.KEY_UP; + } + + @Override + public String toDebugString() { + return super.toDebugString() + "[" + getNativeKeyCode() + "]"; + } +} diff --git a/client/src/com/vaadin/client/widget/grid/events/GridKeyPressEvent.java b/client/src/com/vaadin/client/widget/grid/events/GridKeyPressEvent.java new file mode 100644 index 0000000000..7171814262 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/GridKeyPressEvent.java @@ -0,0 +1,74 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.google.gwt.dom.client.BrowserEvents; +import com.vaadin.client.widget.grid.CellReference; +import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyPressHandler; +import com.vaadin.client.widgets.Grid; +import com.vaadin.client.widgets.Grid.AbstractGridKeyEvent; +import com.vaadin.client.widgets.Grid.Section; + +/** + * Represents native key press event in Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class GridKeyPressEvent extends + AbstractGridKeyEvent<GridKeyPressHandler> { + + public GridKeyPressEvent(Grid<?> grid, CellReference<?> targetCell) { + super(grid, targetCell); + } + + @Override + protected void doDispatch(GridKeyPressHandler handler, Section section) { + if ((section == Section.BODY && handler instanceof BodyKeyPressHandler) + || (section == Section.HEADER && handler instanceof HeaderKeyPressHandler) + || (section == Section.FOOTER && handler instanceof FooterKeyPressHandler)) { + handler.onKeyPress(this); + } + } + + @Override + protected String getBrowserEventType() { + return BrowserEvents.KEYPRESS; + } + + /** + * Gets the char code for this event. + * + * @return the char code + */ + public char getCharCode() { + return (char) getUnicodeCharCode(); + } + + /** + * Gets the Unicode char code (code point) for this event. + * + * @return the Unicode char code + */ + public int getUnicodeCharCode() { + return getNativeEvent().getCharCode(); + } + + @Override + public String toDebugString() { + return super.toDebugString() + "[" + getCharCode() + "]"; + } +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/events/GridKeyUpEvent.java b/client/src/com/vaadin/client/widget/grid/events/GridKeyUpEvent.java new file mode 100644 index 0000000000..2b761a7039 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/GridKeyUpEvent.java @@ -0,0 +1,121 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.google.gwt.dom.client.BrowserEvents; +import com.google.gwt.event.dom.client.KeyCodes; +import com.vaadin.client.widget.grid.CellReference; +import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyUpHandler; +import com.vaadin.client.widgets.Grid; +import com.vaadin.client.widgets.Grid.AbstractGridKeyEvent; +import com.vaadin.client.widgets.Grid.Section; + +/** + * Represents native key up event in Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class GridKeyUpEvent extends AbstractGridKeyEvent<GridKeyUpHandler> { + + public GridKeyUpEvent(Grid<?> grid, CellReference<?> targetCell) { + super(grid, targetCell); + } + + @Override + protected void doDispatch(GridKeyUpHandler handler, Section section) { + if ((section == Section.BODY && handler instanceof BodyKeyUpHandler) + || (section == Section.HEADER && handler instanceof HeaderKeyUpHandler) + || (section == Section.FOOTER && handler instanceof FooterKeyUpHandler)) { + handler.onKeyUp(this); + } + } + + @Override + protected String getBrowserEventType() { + return BrowserEvents.KEYUP; + } + + /** + * Does the key code represent an arrow key? + * + * @param keyCode + * the key code + * @return if it is an arrow key code + */ + public static boolean isArrow(int keyCode) { + switch (keyCode) { + case KeyCodes.KEY_DOWN: + case KeyCodes.KEY_RIGHT: + case KeyCodes.KEY_UP: + case KeyCodes.KEY_LEFT: + return true; + default: + return false; + } + } + + /** + * Gets the native key code. These key codes are enumerated in the + * {@link KeyCodes} class. + * + * @return the key code + */ + public int getNativeKeyCode() { + return getNativeEvent().getKeyCode(); + } + + /** + * Is this a key down arrow? + * + * @return whether this is a down arrow key event + */ + public boolean isDownArrow() { + return getNativeKeyCode() == KeyCodes.KEY_DOWN; + } + + /** + * Is this a left arrow? + * + * @return whether this is a left arrow key event + */ + public boolean isLeftArrow() { + return getNativeKeyCode() == KeyCodes.KEY_LEFT; + } + + /** + * Is this a right arrow? + * + * @return whether this is a right arrow key event + */ + public boolean isRightArrow() { + return getNativeKeyCode() == KeyCodes.KEY_RIGHT; + } + + /** + * Is this a up arrow? + * + * @return whether this is a right arrow key event + */ + public boolean isUpArrow() { + return getNativeKeyCode() == KeyCodes.KEY_UP; + } + + @Override + public String toDebugString() { + return super.toDebugString() + "[" + getNativeKeyCode() + "]"; + } +} diff --git a/client/src/com/vaadin/client/widget/grid/events/HeaderClickHandler.java b/client/src/com/vaadin/client/widget/grid/events/HeaderClickHandler.java new file mode 100644 index 0000000000..da20e80905 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/HeaderClickHandler.java @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.vaadin.client.widget.grid.events.AbstractGridMouseEventHandler.GridClickHandler; + +/** + * Handler for {@link GridClickEvent}s that happen in the header of the Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface HeaderClickHandler extends GridClickHandler { + +} diff --git a/client/src/com/vaadin/client/widget/grid/events/HeaderDoubleClickHandler.java b/client/src/com/vaadin/client/widget/grid/events/HeaderDoubleClickHandler.java new file mode 100644 index 0000000000..7ebb0c17f8 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/HeaderDoubleClickHandler.java @@ -0,0 +1,29 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.vaadin.client.widget.grid.events.AbstractGridMouseEventHandler.GridDoubleClickHandler; + +/** + * Handler for {@link GridDoubleClickEvent}s that happen in the header of the + * Grid. + * + * @since + * @author Vaadin Ltd + */ +public interface HeaderDoubleClickHandler extends GridDoubleClickHandler { + +} diff --git a/client/src/com/vaadin/client/widget/grid/events/HeaderKeyDownHandler.java b/client/src/com/vaadin/client/widget/grid/events/HeaderKeyDownHandler.java new file mode 100644 index 0000000000..555eb936af --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/HeaderKeyDownHandler.java @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyDownHandler; + +/** + * Handler for {@link GridKeyDownEvent}s that happen when the focused cell is in + * the header of the Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface HeaderKeyDownHandler extends GridKeyDownHandler { +} diff --git a/client/src/com/vaadin/client/widget/grid/events/HeaderKeyPressHandler.java b/client/src/com/vaadin/client/widget/grid/events/HeaderKeyPressHandler.java new file mode 100644 index 0000000000..c4dd312f93 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/HeaderKeyPressHandler.java @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyPressHandler; + +/** + * Handler for {@link GridKeyPressEvent}s that happen when the focused cell is + * in the header of the Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface HeaderKeyPressHandler extends GridKeyPressHandler { +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/events/HeaderKeyUpHandler.java b/client/src/com/vaadin/client/widget/grid/events/HeaderKeyUpHandler.java new file mode 100644 index 0000000000..4dbe1c681e --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/HeaderKeyUpHandler.java @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyUpHandler; + +/** + * Handler for {@link GridKeyUpEvent}s that happen when the focused cell is in + * the header of the Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface HeaderKeyUpHandler extends GridKeyUpHandler { +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/events/ScrollEvent.java b/client/src/com/vaadin/client/widget/grid/events/ScrollEvent.java new file mode 100644 index 0000000000..08e1e07eab --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/ScrollEvent.java @@ -0,0 +1,40 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.google.gwt.event.shared.GwtEvent; + +/** + * An event that signifies that a scrollbar bundle has been scrolled + * + * @author Vaadin Ltd + * @since 7.4 + */ +public class ScrollEvent extends GwtEvent<ScrollHandler> { + + /** The type of this event */ + public static final Type<ScrollHandler> TYPE = new Type<ScrollHandler>(); + + @Override + public Type<ScrollHandler> getAssociatedType() { + return TYPE; + } + + @Override + protected void dispatch(final ScrollHandler handler) { + handler.onScroll(this); + } +} diff --git a/client/src/com/vaadin/client/widget/grid/events/ScrollHandler.java b/client/src/com/vaadin/client/widget/grid/events/ScrollHandler.java new file mode 100644 index 0000000000..1ce901e707 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/ScrollHandler.java @@ -0,0 +1,35 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.google.gwt.event.shared.EventHandler; + +/** + * A handler that gets called whenever a scrollbar bundle is scrolled + * + * @author Vaadin Ltd + * @since 7.4 + */ +public interface ScrollHandler extends EventHandler { + /** + * A callback method that is called once a scrollbar bundle has been + * scrolled. + * + * @param event + * the scroll event + */ + public void onScroll(ScrollEvent event); +} diff --git a/client/src/com/vaadin/client/widget/grid/events/SelectAllEvent.java b/client/src/com/vaadin/client/widget/grid/events/SelectAllEvent.java new file mode 100644 index 0000000000..43c2055e95 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/SelectAllEvent.java @@ -0,0 +1,59 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.google.gwt.event.shared.GwtEvent; +import com.vaadin.client.widget.grid.selection.SelectionModel; + +/** + * A select all event, fired by the Grid when it needs all rows in data source + * to be selected. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class SelectAllEvent<T> extends GwtEvent<SelectAllHandler<T>> { + + /** + * Handler type. + */ + private final static Type<SelectAllHandler<?>> TYPE = new Type<SelectAllHandler<?>>();; + + private SelectionModel.Multi<T> selectionModel; + + public SelectAllEvent(SelectionModel.Multi<T> selectionModel) { + this.selectionModel = selectionModel; + } + + public static final Type<SelectAllHandler<?>> getType() { + return TYPE; + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Override + public Type<SelectAllHandler<T>> getAssociatedType() { + return (Type) TYPE; + } + + @Override + protected void dispatch(SelectAllHandler<T> handler) { + handler.onSelectAll(this); + } + + public SelectionModel.Multi<T> getSelectionModel() { + return selectionModel; + } +} diff --git a/client/src/com/vaadin/client/widget/grid/events/SelectAllHandler.java b/client/src/com/vaadin/client/widget/grid/events/SelectAllHandler.java new file mode 100644 index 0000000000..2cdee8d1b3 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/events/SelectAllHandler.java @@ -0,0 +1,37 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.events; + +import com.google.gwt.event.shared.EventHandler; + +/** + * Handler for a Grid select all event, called when the Grid needs all rows in + * data source to be selected. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface SelectAllHandler<T> extends EventHandler { + + /** + * Called when select all value in SelectionColumn header changes value. + * + * @param event + * select all event telling that all rows should be selected + */ + public void onSelectAll(SelectAllEvent<T> event); + +} diff --git a/client/src/com/vaadin/client/widget/grid/selection/AbstractRowHandleSelectionModel.java b/client/src/com/vaadin/client/widget/grid/selection/AbstractRowHandleSelectionModel.java new file mode 100644 index 0000000000..6b7bbb6294 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/selection/AbstractRowHandleSelectionModel.java @@ -0,0 +1,66 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.selection; + +import com.vaadin.client.data.DataSource.RowHandle; + +/** + * An abstract class that adds a consistent API for common methods that's needed + * by Vaadin's server-based selection models to work. + * <p> + * <em>Note:</em> This should be an interface instead of an abstract class, if + * only we could define protected methods in an interface. + * + * @author Vaadin Ltd + * @param <T> + * The grid's row type + * @since 7.4 + */ +public abstract class AbstractRowHandleSelectionModel<T> implements + SelectionModel<T> { + /** + * Select a row, based on its + * {@link com.vaadin.client.data.DataSource.RowHandle RowHandle}. + * <p> + * <em>Note:</em> this method may not fire selection change events. + * + * @param handle + * the handle to select by + * @return <code>true</code> iff the selection state was changed by this + * call + * @throws UnsupportedOperationException + * if the selection model does not support either handles or + * selection + */ + protected abstract boolean selectByHandle(RowHandle<T> handle); + + /** + * Deselect a row, based on its + * {@link com.vaadin.client.data.DataSource.RowHandle RowHandle}. + * <p> + * <em>Note:</em> this method may not fire selection change events. + * + * @param handle + * the handle to deselect by + * @return <code>true</code> iff the selection state was changed by this + * call + * @throws UnsupportedOperationException + * if the selection model does not support either handles or + * deselection + */ + protected abstract boolean deselectByHandle(RowHandle<T> handle) + throws UnsupportedOperationException; +} diff --git a/client/src/com/vaadin/client/widget/grid/selection/ClickSelectHandler.java b/client/src/com/vaadin/client/widget/grid/selection/ClickSelectHandler.java new file mode 100644 index 0000000000..0a1154e787 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/selection/ClickSelectHandler.java @@ -0,0 +1,63 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.selection; + +import com.google.gwt.event.shared.HandlerRegistration; +import com.vaadin.client.widget.grid.events.BodyClickHandler; +import com.vaadin.client.widget.grid.events.GridClickEvent; +import com.vaadin.client.widgets.Grid; + +/** + * Generic class to perform selections when clicking on cells in body of Grid. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class ClickSelectHandler<T> { + + private Grid<T> grid; + private HandlerRegistration clickHandler; + + private class RowClickHandler implements BodyClickHandler { + + @Override + public void onClick(GridClickEvent event) { + T row = (T) event.getTargetCell().getRow(); + if (!grid.isSelected(row)) { + grid.select(row); + } + } + } + + /** + * Constructor for ClickSelectHandler. This constructor will add all + * necessary handlers for selecting rows by clicking cells. + * + * @param grid + * grid to attach to + */ + public ClickSelectHandler(Grid<T> grid) { + this.grid = grid; + clickHandler = grid.addBodyClickHandler(new RowClickHandler()); + } + + /** + * Clean up function for removing all now obsolete handlers. + */ + public void removeHandler() { + clickHandler.removeHandler(); + } +} diff --git a/client/src/com/vaadin/client/widget/grid/selection/HasSelectionHandlers.java b/client/src/com/vaadin/client/widget/grid/selection/HasSelectionHandlers.java new file mode 100644 index 0000000000..ffcad4c903 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/selection/HasSelectionHandlers.java @@ -0,0 +1,42 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.selection; + +import com.google.gwt.event.shared.HandlerRegistration; + +/** + * Marker interface for widgets that fires selection events. + * + * @author Vaadin Ltd + * @since 7.4 + */ +public interface HasSelectionHandlers<T> { + + /** + * Register a selection change handler. + * <p> + * This handler is called whenever a + * {@link com.vaadin.ui.components.grid.selection.SelectionModel + * SelectionModel} detects a change in selection state. + * + * @param handler + * a {@link SelectionHandler} + * @return a handler registration object, which can be used to remove the + * handler. + */ + public HandlerRegistration addSelectionHandler(SelectionHandler<T> handler); + +} diff --git a/client/src/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java b/client/src/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java new file mode 100644 index 0000000000..5024c8bffa --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java @@ -0,0 +1,719 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.selection; + +import java.util.Collection; +import java.util.HashSet; + +import com.google.gwt.animation.client.AnimationScheduler; +import com.google.gwt.animation.client.AnimationScheduler.AnimationCallback; +import com.google.gwt.animation.client.AnimationScheduler.AnimationHandle; +import com.google.gwt.dom.client.BrowserEvents; +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.InputElement; +import com.google.gwt.dom.client.NativeEvent; +import com.google.gwt.dom.client.TableElement; +import com.google.gwt.dom.client.TableSectionElement; +import com.google.gwt.event.shared.HandlerRegistration; +import com.google.gwt.user.client.DOM; +import com.google.gwt.user.client.Event; +import com.google.gwt.user.client.Event.NativePreviewEvent; +import com.google.gwt.user.client.Event.NativePreviewHandler; +import com.vaadin.client.WidgetUtil; +import com.vaadin.client.renderers.ComplexRenderer; +import com.vaadin.client.widget.grid.CellReference; +import com.vaadin.client.widget.grid.RendererCellReference; +import com.vaadin.client.widget.grid.selection.SelectionModel.Multi.Batched; +import com.vaadin.client.widgets.Grid; + +/** + * Renderer showing multi selection check boxes. + * + * @author Vaadin Ltd + * @param <T> + * the type of the associated grid + * @since 7.4 + */ +public class MultiSelectionRenderer<T> extends ComplexRenderer<Boolean> { + + /** The size of the autoscroll area, both top and bottom. */ + private static final int SCROLL_AREA_GRADIENT_PX = 100; + + /** The maximum number of pixels per second to autoscroll. */ + private static final int SCROLL_TOP_SPEED_PX_SEC = 500; + + /** + * The minimum area where the grid doesn't scroll while the pointer is + * pressed. + */ + private static final int MIN_NO_AUTOSCROLL_AREA_PX = 50; + + /** + * This class's main objective is to listen when to stop autoscrolling, and + * make sure everything stops accordingly. + */ + private class TouchEventHandler implements NativePreviewHandler { + @Override + public void onPreviewNativeEvent(final NativePreviewEvent event) { + switch (event.getTypeInt()) { + case Event.ONTOUCHSTART: { + if (event.getNativeEvent().getTouches().length() == 1) { + /* + * Something has dropped a touchend/touchcancel and the + * scroller is most probably running amok. Let's cancel it + * and pretend that everything's going as expected + * + * Because this is a preview, this code is run before the + * event handler in MultiSelectionRenderer.onBrowserEvent. + * Therefore, we can simply kill everything and let that + * method restart things as they should. + */ + autoScrollHandler.stop(); + + /* + * Related TODO: investigate why iOS seems to ignore a + * touchend/touchcancel when frames are dropped, and/or if + * something can be done about that. + */ + } + break; + } + + case Event.ONTOUCHMOVE: + event.cancel(); + break; + + case Event.ONTOUCHEND: + case Event.ONTOUCHCANCEL: + /* + * Remember: targetElement is always where touchstart started, + * not where the finger is pointing currently. + */ + final Element targetElement = Element.as(event.getNativeEvent() + .getEventTarget()); + if (isInFirstColumn(targetElement)) { + removeNativeHandler(); + event.cancel(); + } + break; + } + } + + private boolean isInFirstColumn(final Element element) { + if (element == null) { + return false; + } + final Element tbody = getTbodyElement(); + + if (tbody == null || !tbody.isOrHasChild(element)) { + return false; + } + + /* + * The null-parent in the while clause is in the case where element + * is an immediate tr child in the tbody. Should never happen in + * internal code, but hey... + */ + Element cursor = element; + while (cursor.getParentElement() != null + && cursor.getParentElement().getParentElement() != tbody) { + cursor = cursor.getParentElement(); + } + + final Element tr = cursor.getParentElement(); + return tr.getFirstChildElement().equals(cursor); + } + } + + /** + * This class's responsibility is to + * <ul> + * <li>scroll the table while a pointer is kept in a scrolling zone and + * <li>select rows whenever a pointer is "activated" on a selection cell + * </ul> + * <p> + * <em>Techical note:</em> This class is an AnimationCallback because we + * need a timer: when the finger is kept in place while the grid scrolls, we + * still need to be able to make new selections. So, instead of relying on + * events (which won't be fired, since the pointer isn't necessarily + * moving), we do this check on each frame while the pointer is "active" + * (mouse is pressed, finger is on screen). + */ + private class AutoScrollerAndSelector implements AnimationCallback { + + /** + * If the acceleration gradient area is smaller than this, autoscrolling + * will be disabled (it becomes too quick to accelerate to be usable). + */ + private static final int GRADIENT_MIN_THRESHOLD_PX = 10; + + /** + * The speed at which the gradient area recovers, once scrolling in that + * direction has started. + */ + private static final int SCROLL_AREA_REBOUND_PX_PER_SEC = 1; + private static final double SCROLL_AREA_REBOUND_PX_PER_MS = SCROLL_AREA_REBOUND_PX_PER_SEC / 1000.0d; + + /** + * The lowest y-coordinate on the {@link Event#getClientY() client} from + * where we need to start scrolling towards the top. + */ + private int topBound = -1; + + /** + * The highest y-coordinate on the {@link Event#getClientY() client} + * from where we need to scrolling towards the bottom. + */ + private int bottomBound = -1; + + /** + * <code>true</code> if the pointer is selecting, <code>false</code> if + * the pointer is deselecting. + */ + private final boolean selectionPaint; + + /** + * The area where the selection acceleration takes place. If < + * {@link #GRADIENT_MIN_THRESHOLD_PX}, autoscrolling is disabled + */ + private final int gradientArea; + + /** + * The number of pixels per seconds we currently are scrolling (negative + * is towards the top, positive is towards the bottom). + */ + private double scrollSpeed = 0; + + private double prevTimestamp = 0; + + /** + * This field stores fractions of pixels to scroll, to make sure that + * we're able to scroll less than one px per frame. + */ + private double pixelsToScroll = 0.0d; + + /** Should this animator be running. */ + private boolean running = false; + + /** The handle in which this instance is running. */ + private AnimationHandle handle; + + /** The pointer's pageX coordinate of the first click. */ + private int initialPageX = -1; + + /** The pointer's pageY coordinate. */ + private int pageY; + + /** The logical index of the row that was most recently modified. */ + private int lastModifiedLogicalRow = -1; + + /** @see #doScrollAreaChecks(int) */ + private int finalTopBound; + + /** @see #doScrollAreaChecks(int) */ + private int finalBottomBound; + + private boolean scrollAreaShouldRebound = false; + + private final int bodyAbsoluteTop; + private final int bodyAbsoluteBottom; + + public AutoScrollerAndSelector(final int topBound, + final int bottomBound, final int gradientArea, + final boolean selectionPaint) { + finalTopBound = topBound; + finalBottomBound = bottomBound; + this.gradientArea = gradientArea; + this.selectionPaint = selectionPaint; + + bodyAbsoluteTop = getBodyClientTop(); + bodyAbsoluteBottom = getBodyClientBottom(); + } + + @Override + public void execute(final double timestamp) { + final double timeDiff = timestamp - prevTimestamp; + prevTimestamp = timestamp; + + reboundScrollArea(timeDiff); + + pixelsToScroll += scrollSpeed * (timeDiff / 1000.0d); + final int intPixelsToScroll = (int) pixelsToScroll; + pixelsToScroll -= intPixelsToScroll; + + if (intPixelsToScroll != 0) { + grid.setScrollTop(grid.getScrollTop() + intPixelsToScroll); + } + + int constrainedPageY = Math.max(bodyAbsoluteTop, + Math.min(bodyAbsoluteBottom, pageY)); + int logicalRow = getLogicalRowIndex(WidgetUtil.getElementFromPoint( + initialPageX, constrainedPageY)); + + int incrementOrDecrement = (logicalRow > lastModifiedLogicalRow) ? 1 + : -1; + + /* + * Both pageY and initialPageX have their initialized (and + * unupdated) values while the cursor hasn't moved since the first + * invocation. This will lead to logicalRow being -1, until the + * pointer has been moved. + */ + while (logicalRow != -1 && lastModifiedLogicalRow != logicalRow) { + lastModifiedLogicalRow += incrementOrDecrement; + setSelected(lastModifiedLogicalRow, selectionPaint); + } + + reschedule(); + } + + /** + * If the scroll are has been offset by the pointer starting out there, + * move it back a bit + */ + private void reboundScrollArea(double timeDiff) { + if (!scrollAreaShouldRebound) { + return; + } + + int reboundPx = (int) Math.ceil(SCROLL_AREA_REBOUND_PX_PER_MS + * timeDiff); + if (topBound < finalTopBound) { + topBound += reboundPx; + topBound = Math.min(topBound, finalTopBound); + updateScrollSpeed(pageY); + } else if (bottomBound > finalBottomBound) { + bottomBound -= reboundPx; + bottomBound = Math.max(bottomBound, finalBottomBound); + updateScrollSpeed(pageY); + } + } + + private void updateScrollSpeed(final int pointerPageY) { + + final double ratio; + if (pointerPageY < topBound) { + final double distance = pointerPageY - topBound; + ratio = Math.max(-1, distance / gradientArea); + } + + else if (pointerPageY > bottomBound) { + final double distance = pointerPageY - bottomBound; + ratio = Math.min(1, distance / gradientArea); + } + + else { + ratio = 0; + } + + scrollSpeed = ratio * SCROLL_TOP_SPEED_PX_SEC; + } + + public void start(int logicalRowIndex) { + running = true; + setSelected(logicalRowIndex, selectionPaint); + lastModifiedLogicalRow = logicalRowIndex; + reschedule(); + } + + public void stop() { + running = false; + + if (handle != null) { + handle.cancel(); + handle = null; + } + } + + private void reschedule() { + if (running && gradientArea >= GRADIENT_MIN_THRESHOLD_PX) { + handle = AnimationScheduler.get().requestAnimationFrame(this, + grid.getElement()); + } + } + + public void updatePointerCoords(int pageX, int pageY) { + doScrollAreaChecks(pageY); + updateScrollSpeed(pageY); + this.pageY = pageY; + + if (initialPageX == -1) { + initialPageX = pageX; + } + } + + /** + * This method checks whether the first pointer event started in an area + * that would start scrolling immediately, and does some actions + * accordingly. + * <p> + * If it is, that scroll area will be offset "beyond" the pointer (above + * if pointer is towards the top, otherwise below). + * <p> + * <span style="font-size:smaller">*) This behavior will change in + * future patches (henrik paul 2.7.2014)</span> + */ + private void doScrollAreaChecks(int pageY) { + /* + * The first run makes sure that neither scroll position is + * underneath the finger, but offset to either direction from + * underneath the pointer. + */ + if (topBound == -1) { + topBound = Math.min(finalTopBound, pageY); + bottomBound = Math.max(finalBottomBound, pageY); + } + + /* + * Subsequent runs make sure that the scroll area grows (but doesn't + * shrink) with the finger, but no further than the final bound. + */ + else { + int oldTopBound = topBound; + if (topBound < finalTopBound) { + topBound = Math.max(topBound, + Math.min(finalTopBound, pageY)); + } + + int oldBottomBound = bottomBound; + if (bottomBound > finalBottomBound) { + bottomBound = Math.min(bottomBound, + Math.max(finalBottomBound, pageY)); + } + + final boolean topDidNotMove = oldTopBound == topBound; + final boolean bottomDidNotMove = oldBottomBound == bottomBound; + final boolean wasVerticalMovement = pageY != this.pageY; + scrollAreaShouldRebound = (topDidNotMove && bottomDidNotMove && wasVerticalMovement); + } + } + } + + /** + * This class makes sure that pointer movemenets are registered and + * delegated to the autoscroller so that it can: + * <ul> + * <li>modify the speed in which we autoscroll. + * <li>"paint" a new row with the selection. + * </ul> + * Essentially, when a pointer is pressed on the selection column, a native + * preview handler is registered (so that selection gestures can happen + * outside of the selection column). The handler itself makes sure that it's + * detached when the pointer is "lifted". + */ + private class AutoScrollHandler { + private AutoScrollerAndSelector autoScroller; + + /** The registration info for {@link #scrollPreviewHandler} */ + private HandlerRegistration handlerRegistration; + + private final NativePreviewHandler scrollPreviewHandler = new NativePreviewHandler() { + @Override + public void onPreviewNativeEvent(final NativePreviewEvent event) { + if (autoScroller == null) { + stop(); + return; + } + + final NativeEvent nativeEvent = event.getNativeEvent(); + int pageY = 0; + int pageX = 0; + switch (event.getTypeInt()) { + case Event.ONMOUSEMOVE: + case Event.ONTOUCHMOVE: + pageY = WidgetUtil.getTouchOrMouseClientY(nativeEvent); + pageX = WidgetUtil.getTouchOrMouseClientX(nativeEvent); + autoScroller.updatePointerCoords(pageX, pageY); + break; + case Event.ONMOUSEUP: + case Event.ONTOUCHEND: + case Event.ONTOUCHCANCEL: + stop(); + break; + } + } + }; + + /** + * The top bound, as calculated from the {@link Event#getClientY() + * client} coordinates. + */ + private int topBound = -1; + + /** + * The bottom bound, as calculated from the {@link Event#getClientY() + * client} coordinates. + */ + private int bottomBound = -1; + + /** The size of the autoscroll acceleration area. */ + private int gradientArea; + + public void start(int logicalRowIndex) { + + SelectionModel<T> model = grid.getSelectionModel(); + if (model instanceof Batched) { + Batched<?> batchedModel = (Batched<?>) model; + batchedModel.startBatchSelect(); + } + + /* + * bounds are updated whenever the autoscroll cycle starts, to make + * sure that the widget hasn't changed in size, moved around, or + * whatnot. + */ + updateScrollBounds(); + + assert handlerRegistration == null : "handlerRegistration was not null"; + assert autoScroller == null : "autoScroller was not null"; + handlerRegistration = Event + .addNativePreviewHandler(scrollPreviewHandler); + + autoScroller = new AutoScrollerAndSelector(topBound, bottomBound, + gradientArea, !isSelected(logicalRowIndex)); + autoScroller.start(logicalRowIndex); + } + + private void updateScrollBounds() { + final int topBorder = getBodyClientTop(); + final int bottomBorder = getBodyClientBottom(); + + final int scrollCompensation = getScrollCompensation(); + topBound = scrollCompensation + topBorder + SCROLL_AREA_GRADIENT_PX; + bottomBound = scrollCompensation + bottomBorder + - SCROLL_AREA_GRADIENT_PX; + gradientArea = SCROLL_AREA_GRADIENT_PX; + + // modify bounds if they're too tightly packed + if (bottomBound - topBound < MIN_NO_AUTOSCROLL_AREA_PX) { + int adjustment = MIN_NO_AUTOSCROLL_AREA_PX + - (bottomBound - topBound); + topBound -= adjustment / 2; + bottomBound += adjustment / 2; + gradientArea -= adjustment / 2; + } + } + + private int getScrollCompensation() { + Element cursor = grid.getElement(); + int scroll = 0; + while (cursor != null) { + scroll -= cursor.getScrollTop(); + cursor = cursor.getParentElement(); + } + + return scroll; + } + + public void stop() { + if (handlerRegistration != null) { + handlerRegistration.removeHandler(); + handlerRegistration = null; + } + + if (autoScroller != null) { + autoScroller.stop(); + autoScroller = null; + } + + SelectionModel<T> model = grid.getSelectionModel(); + if (model instanceof Batched) { + Batched<?> batchedModel = (Batched<?>) model; + batchedModel.commitBatchSelect(); + } + + removeNativeHandler(); + } + } + + private static final String LOGICAL_ROW_PROPERTY_INT = "vEscalatorLogicalRow"; + + private final Grid<T> grid; + private HandlerRegistration nativePreviewHandlerRegistration; + + private final AutoScrollHandler autoScrollHandler = new AutoScrollHandler(); + + public MultiSelectionRenderer(final Grid<T> grid) { + this.grid = grid; + } + + @Override + public void destroy() { + if (nativePreviewHandlerRegistration != null) { + removeNativeHandler(); + } + } + + @Override + public void init(RendererCellReference cell) { + final InputElement checkbox = InputElement.as(DOM.createInputCheck()); + cell.getElement().removeAllChildren(); + cell.getElement().appendChild(checkbox); + } + + @Override + public void render(final RendererCellReference cell, final Boolean data) { + InputElement checkbox = InputElement.as(cell.getElement() + .getFirstChildElement()); + checkbox.setChecked(data.booleanValue()); + checkbox.setPropertyInt(LOGICAL_ROW_PROPERTY_INT, cell.getRowIndex()); + } + + @Override + public Collection<String> getConsumedEvents() { + final HashSet<String> events = new HashSet<String>(); + + /* + * this column's first interest is only to attach a NativePreventHandler + * that does all the magic. These events are the beginning of that + * cycle. + */ + events.add(BrowserEvents.MOUSEDOWN); + events.add(BrowserEvents.TOUCHSTART); + + return events; + } + + @Override + public boolean onBrowserEvent(final CellReference<?> cell, + final NativeEvent event) { + if (BrowserEvents.TOUCHSTART.equals(event.getType()) + || (BrowserEvents.MOUSEDOWN.equals(event.getType()) && event + .getButton() == NativeEvent.BUTTON_LEFT)) { + injectNativeHandler(); + int logicalRowIndex = getLogicalRowIndex(Element.as(event + .getEventTarget())); + autoScrollHandler.start(logicalRowIndex); + event.preventDefault(); + event.stopPropagation(); + return true; + } else { + throw new IllegalStateException("received unexpected event: " + + event.getType()); + } + } + + private void injectNativeHandler() { + removeNativeHandler(); + nativePreviewHandlerRegistration = Event + .addNativePreviewHandler(new TouchEventHandler()); + } + + private void removeNativeHandler() { + if (nativePreviewHandlerRegistration != null) { + nativePreviewHandlerRegistration.removeHandler(); + nativePreviewHandlerRegistration = null; + } + } + + private int getLogicalRowIndex(final Element target) { + if (target == null) { + return -1; + } + + /* + * We can't simply go backwards until we find a <tr> first element, + * because of the table-in-table scenario. We need to, unfortunately, go + * up from our known root. + */ + final Element tbody = getTbodyElement(); + Element tr = tbody.getFirstChildElement(); + while (tr != null) { + if (tr.isOrHasChild(target)) { + final Element td = tr.getFirstChildElement(); + assert td != null : "Cell has disappeared"; + + final Element checkbox = td.getFirstChildElement(); + assert checkbox != null : "Checkbox has disappeared"; + + return checkbox.getPropertyInt(LOGICAL_ROW_PROPERTY_INT); + } + tr = tr.getNextSiblingElement(); + } + return -1; + } + + private TableElement getTableElement() { + final Element root = grid.getElement(); + final Element tablewrapper = Element.as(root.getChild(2)); + if (tablewrapper != null) { + return TableElement.as(tablewrapper.getFirstChildElement()); + } else { + return null; + } + } + + private TableSectionElement getTbodyElement() { + TableElement table = getTableElement(); + if (table != null) { + return table.getTBodies().getItem(0); + } else { + return null; + } + } + + private TableSectionElement getTheadElement() { + TableElement table = getTableElement(); + if (table != null) { + return table.getTHead(); + } else { + return null; + } + } + + private TableSectionElement getTfootElement() { + TableElement table = getTableElement(); + if (table != null) { + return table.getTFoot(); + } else { + return null; + } + } + + /** Get the "top" of an element in relation to "client" coordinates. */ + @SuppressWarnings("static-method") + private int getClientTop(final Element e) { + Element cursor = e; + int top = 0; + while (cursor != null) { + top += cursor.getOffsetTop(); + cursor = cursor.getOffsetParent(); + } + return top; + } + + private int getBodyClientBottom() { + return getClientTop(getTfootElement()) - 1; + } + + private int getBodyClientTop() { + return getClientTop(grid.getElement()) + + getTheadElement().getOffsetHeight(); + } + + protected boolean isSelected(final int logicalRow) { + return grid.isSelected(grid.getDataSource().getRow(logicalRow)); + } + + protected void setSelected(final int logicalRow, final boolean select) { + T row = grid.getDataSource().getRow(logicalRow); + if (select) { + grid.select(row); + } else { + grid.deselect(row); + } + } +} diff --git a/client/src/com/vaadin/client/widget/grid/selection/SelectionEvent.java b/client/src/com/vaadin/client/widget/grid/selection/SelectionEvent.java new file mode 100644 index 0000000000..528beb5809 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/selection/SelectionEvent.java @@ -0,0 +1,178 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.selection; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import com.google.gwt.event.shared.GwtEvent; +import com.vaadin.client.widgets.Grid; + +/** + * Event object describing a change in Grid row selection state. + * + * @since 7.4 + * @author Vaadin Ltd + */ +@SuppressWarnings("rawtypes") +public class SelectionEvent<T> extends GwtEvent<SelectionHandler> { + + private static final Type<SelectionHandler> eventType = new Type<SelectionHandler>(); + + private final Grid<T> grid; + private final List<T> added; + private final List<T> removed; + private final boolean batched; + + /** + * Creates an event with a single added or removed row. + * + * @param grid + * grid reference, used for getSource + * @param added + * the added row, or <code>null</code> if a row was not added + * @param removed + * the removed row, or <code>null</code> if a row was not removed + * @param batched + * whether or not this selection change event is triggered during + * a batched selection/deselection action + * @see SelectionModel.Multi.Batched + */ + public SelectionEvent(Grid<T> grid, T added, T removed, boolean batched) { + this.grid = grid; + this.batched = batched; + + if (added != null) { + this.added = Collections.singletonList(added); + } else { + this.added = Collections.emptyList(); + } + + if (removed != null) { + this.removed = Collections.singletonList(removed); + } else { + this.removed = Collections.emptyList(); + } + } + + /** + * Creates an event where several rows have been added or removed. + * + * @param grid + * Grid reference, used for getSource + * @param added + * a collection of added rows, or <code>null</code> if no rows + * were added + * @param removed + * a collection of removed rows, or <code>null</code> if no rows + * were removed + * @param batched + * whether or not this selection change event is triggered during + * a batched selection/deselection action + * @see SelectionModel.Multi.Batched + */ + public SelectionEvent(Grid<T> grid, Collection<T> added, + Collection<T> removed, boolean batched) { + this.grid = grid; + this.batched = batched; + + if (added != null) { + this.added = new ArrayList<T>(added); + } else { + this.added = Collections.emptyList(); + } + + if (removed != null) { + this.removed = new ArrayList<T>(removed); + } else { + this.removed = Collections.emptyList(); + } + } + + /** + * Gets a reference to the Grid object that fired this event. + * + * @return a grid reference + */ + @Override + public Grid<T> getSource() { + return grid; + } + + /** + * Gets all rows added to the selection since the last + * {@link SelectionEvent} . + * + * @return a collection of added rows. Empty collection if no rows were + * added. + */ + public Collection<T> getAdded() { + return Collections.unmodifiableCollection(added); + } + + /** + * Gets all rows removed from the selection since the last + * {@link SelectionEvent}. + * + * @return a collection of removed rows. Empty collection if no rows were + * removed. + */ + public Collection<T> getRemoved() { + return Collections.unmodifiableCollection(removed); + } + + /** + * Gets currently selected rows. + * + * @return a non-null collection containing all currently selected rows. + */ + public Collection<T> getSelected() { + return grid.getSelectedRows(); + } + + /** + * Gets a type identifier for this event. + * + * @return a {@link Type} identifier. + */ + public static Type<SelectionHandler> getType() { + return eventType; + } + + @Override + public Type<SelectionHandler> getAssociatedType() { + return eventType; + } + + @Override + @SuppressWarnings("unchecked") + protected void dispatch(SelectionHandler handler) { + handler.onSelect(this); + } + + /** + * Checks if this selection change event is fired during a batched + * selection/deselection operation. + * + * @return <code>true</code> iff this event is fired during a batched + * selection/deselection operation + */ + public boolean isBatchedSelection() { + return batched; + } +} diff --git a/client/src/com/vaadin/client/widget/grid/selection/SelectionHandler.java b/client/src/com/vaadin/client/widget/grid/selection/SelectionHandler.java new file mode 100644 index 0000000000..4f939fa798 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/selection/SelectionHandler.java @@ -0,0 +1,39 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.selection; + +import com.google.gwt.event.shared.EventHandler; + +/** + * Handler for {@link SelectionEvent}s. + * + * @author Vaadin Ltd + * @param <T> + * The row data type + * @since 7.4 + */ +public interface SelectionHandler<T> extends EventHandler { + + /** + * Called when a selection model's selection state is changed. + * + * @param event + * a selection event, containing info about rows that have been + * added to or removed from the selection. + */ + public void onSelect(SelectionEvent<T> event); + +} diff --git a/client/src/com/vaadin/client/widget/grid/selection/SelectionModel.java b/client/src/com/vaadin/client/widget/grid/selection/SelectionModel.java new file mode 100644 index 0000000000..37f6fb48c3 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/selection/SelectionModel.java @@ -0,0 +1,238 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.selection; + +import java.util.Collection; + +import com.vaadin.client.renderers.Renderer; +import com.vaadin.client.widgets.Grid; + +/** + * Common interface for all selection models. + * <p> + * Selection models perform tracking of selected rows in the Grid, as well as + * dispatching events when the selection state changes. + * + * @author Vaadin Ltd + * @param <T> + * Grid's row type + * @since 7.4 + */ +public interface SelectionModel<T> { + + /** + * Return true if the provided row is considered selected under the + * implementing selection model. + * + * @param row + * row object instance + * @return <code>true</code>, if the row given as argument is considered + * selected. + */ + public boolean isSelected(T row); + + /** + * Return the {@link Renderer} responsible for rendering the selection + * column. + * + * @return a renderer instance. If null is returned, a selection column will + * not be drawn. + */ + public Renderer<Boolean> getSelectionColumnRenderer(); + + /** + * Tells this SelectionModel which Grid it belongs to. + * <p> + * Implementations are free to have this be a no-op. This method is called + * internally by Grid. + * + * @param grid + * a {@link Grid} instance; <code>null</code> when removing from + * Grid + */ + public void setGrid(Grid<T> grid); + + /** + * Resets the SelectionModel to the initial state. + * <p> + * This method can be called internally, for example, when the attached + * Grid's data source changes. + */ + public void reset(); + + /** + * Returns a Collection containing all selected rows. + * + * @return a non-null collection. + */ + public Collection<T> getSelectedRows(); + + /** + * Selection model that allows a maximum of one row to be selected at any + * one time. + * + * @param <T> + * type parameter corresponding with Grid row type + */ + public interface Single<T> extends SelectionModel<T> { + + /** + * Selects a row. + * + * @param row + * a {@link Grid} row object + * @return true, if this row as not previously selected. + */ + public boolean select(T row); + + /** + * Deselects a row. + * <p> + * This is a no-op unless {@link row} is the currently selected row. + * + * @param row + * a {@link Grid} row object + * @return true, if the currently selected row was deselected. + */ + public boolean deselect(T row); + + /** + * Returns the currently selected row. + * + * @return a {@link Grid} row object or null, if nothing is selected. + */ + public T getSelectedRow(); + + } + + /** + * Selection model that allows for several rows to be selected at once. + * + * @param <T> + * type parameter corresponding with Grid row type + */ + public interface Multi<T> extends SelectionModel<T> { + + /** + * A multi selection model that can send selections and deselections in + * a batch, instead of committing them one-by-one. + * + * @param <T> + * type parameter corresponding with Grid row type + */ + public interface Batched<T> extends Multi<T> { + /** + * Starts a batch selection. + * <p> + * Any commands to any select or deselect method will be batched + * into one, and a final selection event will be fired when + * {@link #commitBatchSelect()} is called. + * <p> + * <em>Note:</em> {@link SelectionEvent SelectionChangeEvents} will + * still be fired for each selection/deselection. You should check + * whether the event is a part of a batch or not with + * {@link SelectionEvent#isBatchedSelection()}. + */ + public void startBatchSelect(); + + /** + * Commits and ends a batch selection. + * <p> + * Any and all selections and deselections since the last invocation + * of {@link #startBatchSelect()} will be fired at once as one + * collated {@link SelectionEvent}. + */ + public void commitBatchSelect(); + + /** + * Checks whether or not a batch has been started. + * + * @return <code>true</code> iff a batch has been started + */ + public boolean isBeingBatchSelected(); + + /** + * Gets all the rows that would become selected in this batch. + * + * @return a collection of the rows that would become selected + */ + public Collection<T> getSelectedRowsBatch(); + + /** + * Gets all the rows that would become deselected in this batch. + * + * @return a collection of the rows that would become deselected + */ + public Collection<T> getDeselectedRowsBatch(); + } + + /** + * Selects one or more rows. + * + * @param rows + * {@link Grid} row objects + * @return true, if the set of selected rows was changed. + */ + public boolean select(T... rows); + + /** + * Deselects one or more rows. + * + * @param rows + * Grid row objects + * @return true, if the set of selected rows was changed. + */ + public boolean deselect(T... rows); + + /** + * De-selects all rows. + * + * @return true, if any row was previously selected. + */ + public boolean deselectAll(); + + /** + * Select all rows in a {@link Collection}. + * + * @param rows + * a collection of Grid row objects + * @return true, if the set of selected rows was changed. + */ + public boolean select(Collection<T> rows); + + /** + * Deselect all rows in a {@link Collection}. + * + * @param rows + * a collection of Grid row objects + * @return true, if the set of selected rows was changed. + */ + public boolean deselect(Collection<T> rows); + + } + + /** + * Interface for a selection model that does not allow anything to be + * selected. + * + * @param <T> + * type parameter corresponding with Grid row type + */ + public interface None<T> extends SelectionModel<T> { + + } + +} diff --git a/client/src/com/vaadin/client/widget/grid/selection/SelectionModelMulti.java b/client/src/com/vaadin/client/widget/grid/selection/SelectionModelMulti.java new file mode 100644 index 0000000000..d654a28b7d --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/selection/SelectionModelMulti.java @@ -0,0 +1,273 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.selection; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Set; + +import com.vaadin.client.data.DataSource.RowHandle; +import com.vaadin.client.renderers.Renderer; +import com.vaadin.client.widgets.Grid; + +/** + * Multi-row selection model. + * + * @author Vaadin Ltd + * @since 7.4 + */ +public class SelectionModelMulti<T> extends AbstractRowHandleSelectionModel<T> + implements SelectionModel.Multi.Batched<T> { + + private final LinkedHashSet<RowHandle<T>> selectedRows; + private Renderer<Boolean> renderer; + private Grid<T> grid; + + private boolean batchStarted = false; + private final LinkedHashSet<RowHandle<T>> selectionBatch = new LinkedHashSet<RowHandle<T>>(); + private final LinkedHashSet<RowHandle<T>> deselectionBatch = new LinkedHashSet<RowHandle<T>>(); + + /* Event handling for selection with space key */ + private SpaceSelectHandler<T> spaceSelectHandler; + + public SelectionModelMulti() { + grid = null; + renderer = null; + selectedRows = new LinkedHashSet<RowHandle<T>>(); + } + + @Override + public boolean isSelected(T row) { + return isSelectedByHandle(grid.getDataSource().getHandle(row)); + } + + @Override + public Renderer<Boolean> getSelectionColumnRenderer() { + return renderer; + } + + @Override + public void setGrid(Grid<T> grid) { + if (this.grid != null && grid != null) { + // Trying to replace grid + throw new IllegalStateException( + "Selection model is already attached to a grid. " + + "Remove the selection model first from " + + "the grid and then add it."); + } + + this.grid = grid; + if (this.grid != null) { + spaceSelectHandler = new SpaceSelectHandler<T>(grid); + this.renderer = new MultiSelectionRenderer<T>(grid); + } else { + spaceSelectHandler.removeHandler(); + spaceSelectHandler = null; + this.renderer = null; + } + + } + + @Override + public boolean select(T... rows) { + if (rows == null) { + throw new IllegalArgumentException("Rows cannot be null"); + } + return select(Arrays.asList(rows)); + } + + @Override + public boolean deselect(T... rows) { + if (rows == null) { + throw new IllegalArgumentException("Rows cannot be null"); + } + return deselect(Arrays.asList(rows)); + } + + @Override + public boolean deselectAll() { + if (selectedRows.size() > 0) { + + @SuppressWarnings("unchecked") + final LinkedHashSet<RowHandle<T>> selectedRowsClone = (LinkedHashSet<RowHandle<T>>) selectedRows + .clone(); + SelectionEvent<T> event = new SelectionEvent<T>(grid, null, + getSelectedRows(), isBeingBatchSelected()); + selectedRows.clear(); + + if (isBeingBatchSelected()) { + selectionBatch.clear(); + deselectionBatch.clear(); + deselectionBatch.addAll(selectedRowsClone); + } + + grid.fireEvent(event); + return true; + } + return false; + } + + @Override + public boolean select(Collection<T> rows) { + if (rows == null) { + throw new IllegalArgumentException("Rows cannot be null"); + } + + Set<T> added = new LinkedHashSet<T>(); + + for (T row : rows) { + RowHandle<T> handle = grid.getDataSource().getHandle(row); + if (selectByHandle(handle)) { + added.add(row); + } + } + + if (added.size() > 0) { + grid.fireEvent(new SelectionEvent<T>(grid, added, null, + isBeingBatchSelected())); + + return true; + } + return false; + } + + @Override + public boolean deselect(Collection<T> rows) { + if (rows == null) { + throw new IllegalArgumentException("Rows cannot be null"); + } + + Set<T> removed = new LinkedHashSet<T>(); + + for (T row : rows) { + RowHandle<T> handle = grid.getDataSource().getHandle(row); + if (deselectByHandle(handle)) { + removed.add(row); + } + } + + if (removed.size() > 0) { + grid.fireEvent(new SelectionEvent<T>(grid, null, removed, + isBeingBatchSelected())); + return true; + } + return false; + } + + protected boolean isSelectedByHandle(RowHandle<T> handle) { + return selectedRows.contains(handle); + } + + @Override + protected boolean selectByHandle(RowHandle<T> handle) { + if (selectedRows.add(handle)) { + handle.pin(); + + if (isBeingBatchSelected()) { + deselectionBatch.remove(handle); + selectionBatch.add(handle); + } + + return true; + } + return false; + } + + @Override + protected boolean deselectByHandle(RowHandle<T> handle) { + if (selectedRows.remove(handle)) { + + if (!isBeingBatchSelected()) { + handle.unpin(); + } else { + selectionBatch.remove(handle); + deselectionBatch.add(handle); + } + return true; + } + return false; + } + + @Override + public Collection<T> getSelectedRows() { + Set<T> selected = new LinkedHashSet<T>(); + for (RowHandle<T> handle : selectedRows) { + selected.add(handle.getRow()); + } + return Collections.unmodifiableSet(selected); + } + + @Override + public void reset() { + deselectAll(); + } + + @Override + public void startBatchSelect() { + assert !isBeingBatchSelected() : "Batch has already been started"; + batchStarted = true; + } + + @Override + public void commitBatchSelect() { + assert isBeingBatchSelected() : "Batch was never started"; + if (!isBeingBatchSelected()) { + return; + } + + batchStarted = false; + + final Collection<T> added = getSelectedRowsBatch(); + selectionBatch.clear(); + + final Collection<T> removed = getDeselectedRowsBatch(); + + // unpin deselected rows + for (RowHandle<T> handle : deselectionBatch) { + handle.unpin(); + } + deselectionBatch.clear(); + + grid.fireEvent(new SelectionEvent<T>(grid, added, removed, + isBeingBatchSelected())); + } + + @Override + public boolean isBeingBatchSelected() { + return batchStarted; + } + + @Override + public Collection<T> getSelectedRowsBatch() { + return rowHandlesToRows(selectionBatch); + } + + @Override + public Collection<T> getDeselectedRowsBatch() { + return rowHandlesToRows(deselectionBatch); + } + + private ArrayList<T> rowHandlesToRows(Collection<RowHandle<T>> rowHandles) { + ArrayList<T> rows = new ArrayList<T>(rowHandles.size()); + for (RowHandle<T> handle : rowHandles) { + rows.add(handle.getRow()); + } + return rows; + } +} diff --git a/client/src/com/vaadin/client/widget/grid/selection/SelectionModelNone.java b/client/src/com/vaadin/client/widget/grid/selection/SelectionModelNone.java new file mode 100644 index 0000000000..4a8b203a94 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/selection/SelectionModelNone.java @@ -0,0 +1,73 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.selection; + +import java.util.Collection; +import java.util.Collections; + +import com.vaadin.client.data.DataSource.RowHandle; +import com.vaadin.client.renderers.Renderer; +import com.vaadin.client.widgets.Grid; + +/** + * No-row selection model. + * + * @author Vaadin Ltd + * @since 7.4 + */ +public class SelectionModelNone<T> extends AbstractRowHandleSelectionModel<T> + implements SelectionModel.None<T> { + + @Override + public boolean isSelected(T row) { + return false; + } + + @Override + public Renderer<Boolean> getSelectionColumnRenderer() { + return null; + } + + @Override + public void setGrid(Grid<T> grid) { + // noop + } + + @Override + public void reset() { + // noop + } + + @Override + public Collection<T> getSelectedRows() { + return Collections.emptySet(); + } + + @Override + protected boolean selectByHandle(RowHandle<T> handle) + throws UnsupportedOperationException { + throw new UnsupportedOperationException("This selection model " + + "does not support selection"); + } + + @Override + protected boolean deselectByHandle(RowHandle<T> handle) + throws UnsupportedOperationException { + throw new UnsupportedOperationException("This selection model " + + "does not support deselection"); + } + +} diff --git a/client/src/com/vaadin/client/widget/grid/selection/SelectionModelSingle.java b/client/src/com/vaadin/client/widget/grid/selection/SelectionModelSingle.java new file mode 100644 index 0000000000..20eb3c1e63 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/selection/SelectionModelSingle.java @@ -0,0 +1,151 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.selection; + +import java.util.Collection; +import java.util.Collections; + +import com.vaadin.client.data.DataSource.RowHandle; +import com.vaadin.client.renderers.Renderer; +import com.vaadin.client.widgets.Grid; + +/** + * Single-row selection model. + * + * @author Vaadin Ltd + * @since 7.4 + */ +public class SelectionModelSingle<T> extends AbstractRowHandleSelectionModel<T> + implements SelectionModel.Single<T> { + + private Grid<T> grid; + private RowHandle<T> selectedRow; + + /** Event handling for selection with space key */ + private SpaceSelectHandler<T> spaceSelectHandler; + + /** Event handling for selection by clicking cells */ + private ClickSelectHandler<T> clickSelectHandler; + + @Override + public boolean isSelected(T row) { + return selectedRow != null + && selectedRow.equals(grid.getDataSource().getHandle(row)); + } + + @Override + public Renderer<Boolean> getSelectionColumnRenderer() { + // No Selection column renderer for single selection + return null; + } + + @Override + public void setGrid(Grid<T> grid) { + if (this.grid != null && grid != null) { + // Trying to replace grid + throw new IllegalStateException( + "Selection model is already attached to a grid. " + + "Remove the selection model first from " + + "the grid and then add it."); + } + + this.grid = grid; + if (this.grid != null) { + spaceSelectHandler = new SpaceSelectHandler<T>(grid); + clickSelectHandler = new ClickSelectHandler<T>(grid); + } else { + spaceSelectHandler.removeHandler(); + clickSelectHandler.removeHandler(); + spaceSelectHandler = null; + clickSelectHandler = null; + } + } + + @Override + public boolean select(T row) { + + if (row == null) { + throw new IllegalArgumentException("Row cannot be null"); + } + + T removed = getSelectedRow(); + if (selectByHandle(grid.getDataSource().getHandle(row))) { + grid.fireEvent(new SelectionEvent<T>(grid, row, removed, false)); + + return true; + } + return false; + } + + @Override + public boolean deselect(T row) { + + if (row == null) { + throw new IllegalArgumentException("Row cannot be null"); + } + + if (isSelected(row)) { + deselectByHandle(selectedRow); + grid.fireEvent(new SelectionEvent<T>(grid, null, row, false)); + return true; + } + + return false; + } + + @Override + public T getSelectedRow() { + return (selectedRow != null ? selectedRow.getRow() : null); + } + + @Override + public void reset() { + if (selectedRow != null) { + deselect(getSelectedRow()); + } + } + + @Override + public Collection<T> getSelectedRows() { + if (getSelectedRow() != null) { + return Collections.singleton(getSelectedRow()); + } + return Collections.emptySet(); + } + + @Override + protected boolean selectByHandle(RowHandle<T> handle) { + if (handle != null && !handle.equals(selectedRow)) { + deselectByHandle(selectedRow); + selectedRow = handle; + selectedRow.pin(); + return true; + } else { + return false; + } + } + + @Override + protected boolean deselectByHandle(RowHandle<T> handle) { + if (handle != null && handle.equals(selectedRow)) { + selectedRow.unpin(); + selectedRow = null; + return true; + } else { + return false; + } + } +} diff --git a/client/src/com/vaadin/client/widget/grid/selection/SpaceSelectHandler.java b/client/src/com/vaadin/client/widget/grid/selection/SpaceSelectHandler.java new file mode 100644 index 0000000000..7a1bf2dc06 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/selection/SpaceSelectHandler.java @@ -0,0 +1,124 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.selection; + +import com.google.gwt.event.dom.client.KeyCodes; +import com.google.gwt.event.shared.HandlerRegistration; +import com.vaadin.client.widget.grid.DataAvailableEvent; +import com.vaadin.client.widget.grid.DataAvailableHandler; +import com.vaadin.client.widget.grid.events.BodyKeyDownHandler; +import com.vaadin.client.widget.grid.events.BodyKeyUpHandler; +import com.vaadin.client.widget.grid.events.GridKeyDownEvent; +import com.vaadin.client.widget.grid.events.GridKeyUpEvent; +import com.vaadin.client.widgets.Grid; +import com.vaadin.shared.ui.grid.ScrollDestination; + +/** + * Generic class to perform selections when pressing space key. + * + * @author Vaadin Ltd + * @param <T> + * row data type + * @since 7.4 + */ +public class SpaceSelectHandler<T> { + + /** + * Handler for space key down events in Grid Body + */ + private class SpaceKeyDownHandler implements BodyKeyDownHandler { + private HandlerRegistration scrollHandler = null; + + @Override + public void onKeyDown(GridKeyDownEvent event) { + if (event.getNativeKeyCode() != KeyCodes.KEY_SPACE || spaceDown) { + return; + } + + // Prevent space page scrolling + event.getNativeEvent().preventDefault(); + + spaceDown = true; + final int rowIndex = event.getFocusedCell().getRowIndex(); + + if (scrollHandler != null) { + scrollHandler.removeHandler(); + scrollHandler = null; + } + + scrollHandler = grid + .addDataAvailableHandler(new DataAvailableHandler() { + + @Override + public void onDataAvailable( + DataAvailableEvent dataAvailableEvent) { + if (dataAvailableEvent.getAvailableRows().contains( + rowIndex)) { + setSelected(grid, rowIndex); + scrollHandler.removeHandler(); + scrollHandler = null; + } + } + }); + grid.scrollToRow(rowIndex, ScrollDestination.ANY); + } + + protected void setSelected(Grid<T> grid, int rowIndex) { + T row = grid.getDataSource().getRow(rowIndex); + + if (grid.isSelected(row)) { + grid.deselect(row); + } else { + grid.select(row); + } + } + } + + private boolean spaceDown = false; + private Grid<T> grid; + private HandlerRegistration spaceUpHandler; + private HandlerRegistration spaceDownHandler; + + /** + * Constructor for SpaceSelectHandler. This constructor will add all + * necessary handlers for selecting rows with space. + * + * @param grid + * grid to attach to + */ + public SpaceSelectHandler(Grid<T> grid) { + this.grid = grid; + spaceDownHandler = grid + .addBodyKeyDownHandler(new SpaceKeyDownHandler()); + spaceUpHandler = grid.addBodyKeyUpHandler(new BodyKeyUpHandler() { + + @Override + public void onKeyUp(GridKeyUpEvent event) { + if (event.getNativeKeyCode() == KeyCodes.KEY_SPACE) { + spaceDown = false; + } + } + }); + } + + /** + * Clean up function for removing all now obsolete handlers. + */ + public void removeHandler() { + spaceDownHandler.removeHandler(); + spaceUpHandler.removeHandler(); + } +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/sort/Sort.java b/client/src/com/vaadin/client/widget/grid/sort/Sort.java new file mode 100644 index 0000000000..b1f3c6e39a --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/sort/Sort.java @@ -0,0 +1,154 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.sort; + +import java.util.ArrayList; +import java.util.List; + +import com.vaadin.client.widgets.Grid; +import com.vaadin.shared.data.sort.SortDirection; + +/** + * Fluid Sort descriptor object. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class Sort { + + private final Sort previous; + private final SortOrder order; + private final int count; + + /** + * Basic constructor, used by the {@link #by(GridColumn)} and + * {@link #by(GridColumn, SortDirection)} methods. + * + * @param column + * a grid column + * @param direction + * a sort direction + */ + private Sort(Grid.Column<?, ?> column, SortDirection direction) { + previous = null; + count = 1; + order = new SortOrder(column, direction); + } + + /** + * Extension constructor. Performs object equality checks on all previous + * Sort objects in the chain to make sure that the column being passed in + * isn't already used earlier (which would indicate a bug). If the column + * has been used before, this constructor throws an + * {@link IllegalStateException}. + * + * @param previous + * the sort instance that the new sort instance is to extend + * @param column + * a (previously unused) grid column reference + * @param direction + * a sort direction + */ + private Sort(Sort previous, Grid.Column<?, ?> column, + SortDirection direction) { + this.previous = previous; + count = previous.count + 1; + order = new SortOrder(column, direction); + + Sort s = previous; + while (s != null) { + if (s.order.getColumn() == column) { + throw new IllegalStateException( + "Can not sort along the same column twice"); + } + s = s.previous; + } + } + + /** + * Start building a Sort order by sorting a provided column in ascending + * order. + * + * @param column + * a grid column object reference + * @return a sort instance, typed to the grid data type + */ + public static Sort by(Grid.Column<?, ?> column) { + return by(column, SortDirection.ASCENDING); + } + + /** + * Start building a Sort order by sorting a provided column. + * + * @param column + * a grid column object reference + * @param direction + * indicator of sort direction - either ascending or descending + * @return a sort instance, typed to the grid data type + */ + public static Sort by(Grid.Column<?, ?> column, SortDirection direction) { + return new Sort(column, direction); + } + + /** + * Continue building a Sort order. The provided column is sorted in + * ascending order if the previously added columns have been evaluated as + * equals. + * + * @param column + * a grid column object reference + * @return a sort instance, typed to the grid data type + */ + public Sort then(Grid.Column<?, ?> column) { + return then(column, SortDirection.ASCENDING); + } + + /** + * Continue building a Sort order. The provided column is sorted in + * specified order if the previously added columns have been evaluated as + * equals. + * + * @param column + * a grid column object reference + * @param direction + * indicator of sort direction - either ascending or descending + * @return a sort instance, typed to the grid data type + */ + public Sort then(Grid.Column<?, ?> column, SortDirection direction) { + return new Sort(this, column, direction); + } + + /** + * Build a sort order list. This method is called internally by Grid when + * calling {@link com.vaadin.client.ui.grid.Grid#sort(Sort)}, but can also + * be called manually to create a SortOrder list, which can also be provided + * directly to Grid. + * + * @return a sort order list. + */ + public List<SortOrder> build() { + + List<SortOrder> order = new ArrayList<SortOrder>(count); + + Sort s = this; + for (int i = count - 1; i >= 0; --i) { + order.add(0, s.order); + s = s.previous; + } + + return order; + } +} diff --git a/client/src/com/vaadin/client/widget/grid/sort/SortEvent.java b/client/src/com/vaadin/client/widget/grid/sort/SortEvent.java new file mode 100644 index 0000000000..2aad6e4f95 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/sort/SortEvent.java @@ -0,0 +1,113 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.sort; + +import java.util.List; + +import com.google.gwt.event.shared.GwtEvent; +import com.vaadin.client.widgets.Grid; + +/** + * A sort event, fired by the Grid when it needs its data source to provide data + * sorted in a specific manner. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class SortEvent<T> extends GwtEvent<SortHandler<?>> { + + private static final Type<SortHandler<?>> TYPE = new Type<SortHandler<?>>(); + + private final Grid<T> grid; + private final List<SortOrder> order; + private final boolean userOriginated; + + /** + * Creates a new Sort Event. All provided parameters are final, and passed + * on as-is. + * + * @param grid + * a grid reference + * @param order + * an array dictating the desired sort order of the data source + * @param originator + * a value indicating where this event originated from + */ + public SortEvent(Grid<T> grid, List<SortOrder> order, boolean userOriginated) { + this.grid = grid; + this.order = order; + this.userOriginated = userOriginated; + } + + @Override + public Type<SortHandler<?>> getAssociatedType() { + return TYPE; + } + + /** + * Static access to the GWT event type identifier associated with this Event + * class + * + * @return a type object, uniquely describing this event type. + */ + public static Type<SortHandler<?>> getType() { + return TYPE; + } + + /** + * Get access to the Grid that fired this event + * + * @return the grid instance + */ + @Override + public Grid<T> getSource() { + return grid; + } + + /** + * Get access to the Grid that fired this event + * + * @return the grid instance + */ + public Grid<T> getGrid() { + return grid; + } + + /** + * Get the sort ordering that is to be applied to the Grid + * + * @return a list of sort order objects + */ + public List<SortOrder> getOrder() { + return order; + } + + /** + * Returns whether this event originated from actions done by the user. + * + * @return true if sort event originated from user interaction + */ + public boolean isUserOriginated() { + return userOriginated; + } + + @SuppressWarnings("unchecked") + @Override + protected void dispatch(SortHandler<?> handler) { + ((SortHandler<T>) handler).sort(this); + } + +} diff --git a/client/src/com/vaadin/client/widget/grid/sort/SortHandler.java b/client/src/com/vaadin/client/widget/grid/sort/SortHandler.java new file mode 100644 index 0000000000..330cbe9d58 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/sort/SortHandler.java @@ -0,0 +1,38 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.sort; + +import com.google.gwt.event.shared.EventHandler; + +/** + * Handler for a Grid sort event, called when the Grid needs its data source to + * provide data sorted in a specific manner. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface SortHandler<T> extends EventHandler { + + /** + * Handle sorting of the Grid. This method is called when a re-sorting of + * the Grid's data is requested. + * + * @param event + * the sort event + */ + public void sort(SortEvent<T> event); + +} diff --git a/client/src/com/vaadin/client/widget/grid/sort/SortOrder.java b/client/src/com/vaadin/client/widget/grid/sort/SortOrder.java new file mode 100644 index 0000000000..8166f1e6ed --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/sort/SortOrder.java @@ -0,0 +1,90 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widget.grid.sort; + +import com.vaadin.client.widgets.Grid; +import com.vaadin.shared.data.sort.SortDirection; + +/** + * Sort order descriptor. Contains column and direction references. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class SortOrder { + + private final Grid.Column<?, ?> column; + private final SortDirection direction; + + /** + * Create a sort order descriptor with a default sorting direction value of + * {@link SortDirection#ASCENDING}. + * + * @param column + * a grid column descriptor object + */ + public SortOrder(Grid.Column<?, ?> column) { + this(column, SortDirection.ASCENDING); + } + + /** + * Create a sort order descriptor. + * + * @param column + * a grid column descriptor object + * @param direction + * a sorting direction value (ascending or descending) + */ + public SortOrder(Grid.Column<?, ?> column, SortDirection direction) { + if (column == null) { + throw new IllegalArgumentException( + "Grid column reference can not be null!"); + } + if (direction == null) { + throw new IllegalArgumentException( + "Direction value can not be null!"); + } + this.column = column; + this.direction = direction; + } + + /** + * Returns the {@link GridColumn} reference given in the constructor. + * + * @return a grid column reference + */ + public Grid.Column<?, ?> getColumn() { + return column; + } + + /** + * Returns the {@link SortDirection} value given in the constructor. + * + * @return a sort direction value + */ + public SortDirection getDirection() { + return direction; + } + + /** + * Returns a new SortOrder object with the sort direction reversed. + * + * @return a new sort order object + */ + public SortOrder getOpposite() { + return new SortOrder(column, direction.getOpposite()); + } +} diff --git a/client/src/com/vaadin/client/widgets/Escalator.java b/client/src/com/vaadin/client/widgets/Escalator.java new file mode 100644 index 0000000000..4f853a928f --- /dev/null +++ b/client/src/com/vaadin/client/widgets/Escalator.java @@ -0,0 +1,5197 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widgets; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.google.gwt.animation.client.AnimationScheduler; +import com.google.gwt.animation.client.AnimationScheduler.AnimationCallback; +import com.google.gwt.animation.client.AnimationScheduler.AnimationHandle; +import com.google.gwt.core.client.Duration; +import com.google.gwt.core.client.JavaScriptObject; +import com.google.gwt.core.client.Scheduler; +import com.google.gwt.core.client.Scheduler.ScheduledCommand; +import com.google.gwt.dom.client.DivElement; +import com.google.gwt.dom.client.Document; +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.NativeEvent; +import com.google.gwt.dom.client.Node; +import com.google.gwt.dom.client.NodeList; +import com.google.gwt.dom.client.Style; +import com.google.gwt.dom.client.Style.Display; +import com.google.gwt.dom.client.Style.Unit; +import com.google.gwt.dom.client.TableCellElement; +import com.google.gwt.dom.client.TableRowElement; +import com.google.gwt.dom.client.TableSectionElement; +import com.google.gwt.event.shared.HandlerRegistration; +import com.google.gwt.logging.client.LogConfiguration; +import com.google.gwt.user.client.DOM; +import com.google.gwt.user.client.Window; +import com.google.gwt.user.client.ui.RequiresResize; +import com.google.gwt.user.client.ui.UIObject; +import com.google.gwt.user.client.ui.Widget; +import com.vaadin.client.BrowserInfo; +import com.vaadin.client.DeferredWorker; +import com.vaadin.client.Profiler; +import com.vaadin.client.WidgetUtil; +import com.vaadin.client.widget.escalator.Cell; +import com.vaadin.client.widget.escalator.ColumnConfiguration; +import com.vaadin.client.widget.escalator.EscalatorUpdater; +import com.vaadin.client.widget.escalator.FlyweightCell; +import com.vaadin.client.widget.escalator.FlyweightRow; +import com.vaadin.client.widget.escalator.PositionFunction; +import com.vaadin.client.widget.escalator.PositionFunction.AbsolutePosition; +import com.vaadin.client.widget.escalator.PositionFunction.Translate3DPosition; +import com.vaadin.client.widget.escalator.PositionFunction.TranslatePosition; +import com.vaadin.client.widget.escalator.PositionFunction.WebkitTranslate3DPosition; +import com.vaadin.client.widget.escalator.RowContainer; +import com.vaadin.client.widget.escalator.RowVisibilityChangeEvent; +import com.vaadin.client.widget.escalator.RowVisibilityChangeHandler; +import com.vaadin.client.widget.escalator.ScrollbarBundle; +import com.vaadin.client.widget.escalator.ScrollbarBundle.HorizontalScrollbarBundle; +import com.vaadin.client.widget.escalator.ScrollbarBundle.VerticalScrollbarBundle; +import com.vaadin.client.widget.grid.events.ScrollEvent; +import com.vaadin.client.widget.grid.events.ScrollHandler; +import com.vaadin.client.widgets.Escalator.JsniUtil.TouchHandlerBundle; +import com.vaadin.shared.ui.grid.HeightMode; +import com.vaadin.shared.ui.grid.Range; +import com.vaadin.shared.ui.grid.ScrollDestination; +import com.vaadin.shared.util.SharedUtil; + +/*- + + Maintenance Notes! Reading these might save your day. + (note for editors: line width is 80 chars, including the + one-space indentation) + + + == Row Container Structure + + AbstractRowContainer + |-- AbstractStaticRowContainer + | |-- HeaderRowContainer + | `-- FooterContainer + `---- BodyRowContainer + + AbstractRowContainer is intended to contain all common logic + between RowContainers. It manages the bookkeeping of row + count, makes sure that all individual cells are rendered + the same way, and so on. + + AbstractStaticRowContainer has some special logic that is + required by all RowContainers that don't scroll (hence the + word "static"). HeaderRowContainer and FooterRowContainer + are pretty thin special cases of a StaticRowContainer + (mostly relating to positioning of the root element). + + BodyRowContainer could also be split into an additional + "AbstractScrollingRowContainer", but I felt that no more + inner classes were needed. So it contains both logic + required for making things scroll about, and equivalent + special cases for layouting, as are found in + Header/FooterRowContainers. + + + == The Three Indices + + Each RowContainer can be thought to have three levels of + indices for any given displayed row (but the distinction + matters primarily for the BodyRowContainer, because of the + way it scrolls through data): + + - Logical index + - Physical (or DOM) index + - Visual index + + LOGICAL INDEX is the index that is linked to the data + source. If you want your data source to represent a SQL + database with 10 000 rows, the 7 000:th row in the SQL has a + logical index of 6 999, since the index is 0-based (unless + that data source does some funky logic). + + PHYSICAL INDEX is the index for a row that you see in a + browser's DOM inspector. If your row is the second <tr> + element within a <tbody> tag, it has a physical index of 1 + (because of 0-based indices). In Header and + FooterRowContainers, you are safe to assume that the logical + index is the same as the physical index. But because the + BodyRowContainer never displays large data sources entirely + in the DOM, a physical index usually has no apparent direct + relationship with its logical index. + + VISUAL INDEX is the index relating to the order that you + see a row in, in the browser, as it is rendered. The + topmost row is 0, the second is 1, and so on. The visual + index is similar to the physical index in the sense that + Header and FooterRowContainers can assume a 1:1 + relationship between visual index and logical index. And + again, BodyRowContainer has no such relationship. The + body's visual index has additionally no apparent + relationship with its physical index. Because the <tr> tags + are reused in the body and visually repositioned with CSS + as the user scrolls, the relationship between physical + index and visual index is quickly broken. You can get an + element's visual index via the field + BodyRowContainer.visualRowOrder. + + Currently, the physical and visual indices are kept in sync + _most of the time_ by a deferred rearrangement of rows. + They become desynced when scrolling. This is to help screen + readers to read the contents from the DOM in a natural + order. See BodyRowContainer.DeferredDomSorter for more + about that. + + */ + +/** + * A workaround-class for GWT and JSNI. + * <p> + * GWT is unable to handle some method calls to Java methods in inner-classes + * from within JSNI blocks. Having that inner class extend a non-inner-class (or + * implement such an interface), makes it possible for JSNI to indirectly refer + * to the inner class, by invoking methods and fields in the non-inner-class + * API. + * + * @see Escalator.Scroller + */ +abstract class JsniWorkaround { + /** + * A JavaScript function that handles the scroll DOM event, and passes it on + * to Java code. + * + * @see #createScrollListenerFunction(Escalator) + * @see Escalator#onScroll() + * @see Escalator.Scroller#onScroll() + */ + protected final JavaScriptObject scrollListenerFunction; + + /** + * A JavaScript function that handles the mousewheel DOM event, and passes + * it on to Java code. + * + * @see #createMousewheelListenerFunction(Escalator) + * @see Escalator#onScroll() + * @see Escalator.Scroller#onScroll() + */ + protected final JavaScriptObject mousewheelListenerFunction; + + /** + * A JavaScript function that handles the touch start DOM event, and passes + * it on to Java code. + * + * @see TouchHandlerBundle#touchStart(Escalator.JsniUtil.TouchHandlerBundle.CustomTouchEvent) + */ + protected JavaScriptObject touchStartFunction; + + /** + * A JavaScript function that handles the touch move DOM event, and passes + * it on to Java code. + * + * @see TouchHandlerBundle#touchMove(Escalator.JsniUtil.TouchHandlerBundle.CustomTouchEvent) + */ + protected JavaScriptObject touchMoveFunction; + + /** + * A JavaScript function that handles the touch end and cancel DOM events, + * and passes them on to Java code. + * + * @see TouchHandlerBundle#touchEnd(Escalator.JsniUtil.TouchHandlerBundle.CustomTouchEvent) + */ + protected JavaScriptObject touchEndFunction; + + protected TouchHandlerBundle touchHandlerBundle; + + protected JsniWorkaround(final Escalator escalator) { + scrollListenerFunction = createScrollListenerFunction(escalator); + mousewheelListenerFunction = createMousewheelListenerFunction(escalator); + + touchHandlerBundle = new TouchHandlerBundle(escalator); + touchStartFunction = touchHandlerBundle.getTouchStartHandler(); + touchMoveFunction = touchHandlerBundle.getTouchMoveHandler(); + touchEndFunction = touchHandlerBundle.getTouchEndHandler(); + } + + /** + * A method that constructs the JavaScript function that will be stored into + * {@link #scrollListenerFunction}. + * + * @param esc + * a reference to the current instance of {@link Escalator} + * @see Escalator#onScroll() + */ + protected abstract JavaScriptObject createScrollListenerFunction( + Escalator esc); + + /** + * A method that constructs the JavaScript function that will be stored into + * {@link #mousewheelListenerFunction}. + * + * @param esc + * a reference to the current instance of {@link Escalator} + * @see Escalator#onScroll() + */ + protected abstract JavaScriptObject createMousewheelListenerFunction( + Escalator esc); +} + +/** + * A low-level table-like widget that features a scrolling virtual viewport and + * lazily generated rows. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class Escalator extends Widget implements RequiresResize, DeferredWorker { + + // todo comments legend + /* + * [[optimize]]: There's an opportunity to rewrite the code in such a way + * that it _might_ perform better (rememeber to measure, implement, + * re-measure) + */ + /* + * [[rowheight]]: This code will require alterations that are relevant for + * being able to support variable row heights. NOTE: these bits can most + * often also be identified by searching for code reading the ROW_HEIGHT_PX + * constant. + */ + /* + * [[mpixscroll]]: This code will require alterations that are relevant for + * supporting the scrolling through more pixels than some browsers normally + * would support. (i.e. when we support more than "a million" pixels in the + * escalator DOM). NOTE: these bits can most often also be identified by + * searching for code that call scrollElem.getScrollTop();. + */ + + /** + * A utility class that contains utility methods that are usually called + * from JSNI. + * <p> + * The methods are moved in this class to minimize the amount of JSNI code + * as much as feasible. + */ + static class JsniUtil { + public static class TouchHandlerBundle { + + private static final double FLICK_POLL_FREQUENCY = 100d; + + /** + * A <a href= + * "http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsOverlay.html" + * >JavaScriptObject overlay</a> for the <a + * href="http://www.w3.org/TR/touch-events/">JavaScript + * TouchEvent</a> object. + * <p> + * This needs to be used in the touch event handlers, since GWT's + * {@link com.google.gwt.event.dom.client.TouchEvent TouchEvent} + * can't be cast from the JSNI call, and the + * {@link com.google.gwt.dom.client.NativeEvent NativeEvent} isn't + * properly populated with the correct values. + */ + private final static class CustomTouchEvent extends + JavaScriptObject { + protected CustomTouchEvent() { + } + + public native NativeEvent getNativeEvent() + /*-{ + return this; + }-*/; + + public native int getPageX() + /*-{ + return this.targetTouches[0].pageX; + }-*/; + + public native int getPageY() + /*-{ + return this.targetTouches[0].pageY; + }-*/; + } + + private double touches = 0; + private int lastX = 0; + private int lastY = 0; + private double lastTime = 0; + private boolean snappedScrollEnabled = true; + private double deltaX = 0; + private double deltaY = 0; + + private final Escalator escalator; + + private CustomTouchEvent latestTouchMoveEvent; + + /** The timestamp of {@link #flickPageX1} and {@link #flickPageY1} */ + private double flickTimestamp = Double.MIN_VALUE; + + /** The most recent flick touch reference Y */ + private double flickPageY1 = -1; + /** The most recent flick touch reference X */ + private double flickPageX1 = -1; + + /** The previous flick touch reference Y, before {@link #flickPageY1} */ + private double flickPageY2 = -1; + /** The previous flick touch reference X, before {@link #flickPageX1} */ + private double flickPageX2 = -1; + + /** + * This animation callback guarantees the fact that we don't scroll + * the grid more than once per visible frame. + * + * It seems that there will never be more touch events than there + * are rendered frames, but there's no guarantee for that. If it was + * guaranteed, we probably could do all of this immediately in + * {@link #touchMove(CustomTouchEvent)}, instead of deferring it + * over here. + */ + private AnimationCallback mover = new AnimationCallback() { + @Override + public void execute(double timestamp) { + if (touches != 1) { + return; + } + + final int x = latestTouchMoveEvent.getPageX(); + final int y = latestTouchMoveEvent.getPageY(); + + /* + * Check if we need a new flick coordinate sample (more than + * FLICK_POLL_FREQUENCY ms have passed since the last + * sample) + */ + if (timestamp - flickTimestamp > FLICK_POLL_FREQUENCY) { + flickTimestamp = timestamp; + flickPageY2 = flickPageY1; + flickPageY1 = y; + + flickPageX2 = flickPageX1; + flickPageX1 = x; + } + + deltaX = x - lastX; + deltaY = y - lastY; + lastX = x; + lastY = y; + + /* + * Instead of using the provided arbitrary timestamp, let's + * use a known-format and reproducible timestamp. + */ + lastTime = Duration.currentTimeMillis(); + + // snap the scroll to the major axes, at first. + if (snappedScrollEnabled) { + final double oldDeltaX = deltaX; + final double oldDeltaY = deltaY; + + /* + * Scrolling snaps to 40 degrees vs. flick scroll's 30 + * degrees, since slow movements have poor resolution - + * it's easy to interpret a slight angle as a steep + * angle, since the sample rate is "unnecessarily" high. + * 40 simply felt better than 30. + */ + final double[] snapped = Escalator.snapDeltas(deltaX, + deltaY, RATIO_OF_40_DEGREES); + deltaX = snapped[0]; + deltaY = snapped[1]; + + /* + * if the snap failed once, let's follow the pointer + * from now on. + */ + if (oldDeltaX != 0 && deltaX == oldDeltaX + && oldDeltaY != 0 && deltaY == oldDeltaY) { + snappedScrollEnabled = false; + } + } + + moveScrollFromEvent(escalator, -deltaX, -deltaY, + latestTouchMoveEvent.getNativeEvent()); + } + }; + private AnimationHandle animationHandle; + + public TouchHandlerBundle(final Escalator escalator) { + this.escalator = escalator; + } + + public native JavaScriptObject getTouchStartHandler() + /*-{ + // we need to store "this", since it won't be preserved on call. + var self = this; + return $entry(function (e) { + self.@com.vaadin.client.widgets.Escalator.JsniUtil.TouchHandlerBundle::touchStart(*)(e); + }); + }-*/; + + public native JavaScriptObject getTouchMoveHandler() + /*-{ + // we need to store "this", since it won't be preserved on call. + var self = this; + return $entry(function (e) { + self.@com.vaadin.client.widgets.Escalator.JsniUtil.TouchHandlerBundle::touchMove(*)(e); + }); + }-*/; + + public native JavaScriptObject getTouchEndHandler() + /*-{ + // we need to store "this", since it won't be preserved on call. + var self = this; + return $entry(function (e) { + self.@com.vaadin.client.widgets.Escalator.JsniUtil.TouchHandlerBundle::touchEnd(*)(e); + }); + }-*/; + + public void touchStart(final CustomTouchEvent event) { + touches = event.getNativeEvent().getTouches().length(); + if (touches != 1) { + return; + } + + escalator.scroller.cancelFlickScroll(); + + lastX = event.getPageX(); + lastY = event.getPageY(); + + snappedScrollEnabled = true; + } + + public void touchMove(final CustomTouchEvent event) { + /* + * since we only use the getPageX/Y, and calculate the diff + * within the handler, we don't need to calculate any + * intermediate deltas. + */ + latestTouchMoveEvent = event; + + if (animationHandle != null) { + animationHandle.cancel(); + } + animationHandle = AnimationScheduler.get() + .requestAnimationFrame(mover, escalator.bodyElem); + event.getNativeEvent().preventDefault(); + } + + public void touchEnd(final CustomTouchEvent event) { + touches = event.getNativeEvent().getTouches().length(); + + if (touches == 0) { + + /* + * We want to smooth the flick calculations here. We have + * taken a frame of reference every FLICK_POLL_FREQUENCY. + * But if the sample is too fresh, we might introduce noise + * in our sampling, so we use the older sample instead. it + * might be less accurate, but it's smoother. + * + * flickPage?1 is the most recent one, while flickPage?2 is + * the previous one. + */ + + final double finalPageY; + final double finalPageX; + double deltaT = lastTime - flickTimestamp; + boolean onlyOneSample = flickPageX2 < 0 || flickPageY2 < 0; + if (onlyOneSample || deltaT > FLICK_POLL_FREQUENCY / 3) { + finalPageY = flickPageY1; + finalPageX = flickPageX1; + } else { + deltaT += FLICK_POLL_FREQUENCY; + finalPageY = flickPageY2; + finalPageX = flickPageX2; + } + + flickPageY1 = -1; + flickPageY2 = -1; + flickTimestamp = Double.MIN_VALUE; + + double deltaX = latestTouchMoveEvent.getPageX() + - finalPageX; + double deltaY = latestTouchMoveEvent.getPageY() + - finalPageY; + + escalator.scroller + .handleFlickScroll(deltaX, deltaY, deltaT); + escalator.body.domSorter.reschedule(); + } + } + } + + public static void moveScrollFromEvent(final Escalator escalator, + final double deltaX, final double deltaY, + final NativeEvent event) { + + if (!Double.isNaN(deltaX)) { + escalator.horizontalScrollbar.setScrollPosByDelta(deltaX); + } + + if (!Double.isNaN(deltaY)) { + escalator.verticalScrollbar.setScrollPosByDelta(deltaY); + } + + /* + * TODO: only prevent if not scrolled to end/bottom. Or no? UX team + * needs to decide. + */ + final boolean warrantedYScroll = deltaY != 0 + && escalator.verticalScrollbar.showsScrollHandle(); + final boolean warrantedXScroll = deltaX != 0 + && escalator.horizontalScrollbar.showsScrollHandle(); + if (warrantedYScroll || warrantedXScroll) { + event.preventDefault(); + } + } + } + + /** + * The animation callback that handles the animation of a touch-scrolling + * flick with inertia. + */ + private class FlickScrollAnimator implements AnimationCallback { + private static final double MIN_MAGNITUDE = 0.005; + private static final double MAX_SPEED = 7; + + private double velX; + private double velY; + private double prevTime = 0; + private int millisLeft; + private double xFric; + private double yFric; + + private boolean cancelled = false; + private double lastLeft; + private double lastTop; + + /** + * Creates a new animation callback to handle touch-scrolling flick with + * inertia. + * + * @param deltaX + * the last scrolling delta in the x-axis in a touchmove + * @param deltaY + * the last scrolling delta in the y-axis in a touchmove + * @param lastTime + * the timestamp of the last touchmove + */ + public FlickScrollAnimator(final double deltaX, final double deltaY, + final double deltaT) { + velX = Math.max(Math.min(deltaX / deltaT, MAX_SPEED), -MAX_SPEED); + velY = Math.max(Math.min(deltaY / deltaT, MAX_SPEED), -MAX_SPEED); + + lastLeft = horizontalScrollbar.getScrollPos(); + lastTop = verticalScrollbar.getScrollPos(); + + /* + * If we're scrolling mainly in one of the four major directions, + * and only a teeny bit to any other side, snap the scroll to that + * major direction instead. + */ + final double[] snapDeltas = Escalator.snapDeltas(velX, velY, + RATIO_OF_30_DEGREES); + velX = snapDeltas[0]; + velY = snapDeltas[1]; + + if (velX * velX + velY * velY > MIN_MAGNITUDE) { + millisLeft = 1500; + xFric = velX / millisLeft; + yFric = velY / millisLeft; + } else { + millisLeft = 0; + } + + } + + @Override + public void execute(final double doNotUseThisTimestamp) { + /* + * We cannot use the timestamp provided to this method since it is + * of a format that cannot be determined at will. Therefore, we need + * a timestamp format that we can handle, so our calculations are + * correct. + */ + + if (millisLeft <= 0 || cancelled) { + scroller.currentFlickScroller = null; + return; + } + + final double timestamp = Duration.currentTimeMillis(); + if (prevTime == 0) { + prevTime = timestamp; + AnimationScheduler.get().requestAnimationFrame(this); + return; + } + + double currentLeft = horizontalScrollbar.getScrollPos(); + double currentTop = verticalScrollbar.getScrollPos(); + + final double timeDiff = timestamp - prevTime; + double left = currentLeft - velX * timeDiff; + setScrollLeft(left); + velX -= xFric * timeDiff; + + double top = currentTop - velY * timeDiff; + setScrollTop(top); + velY -= yFric * timeDiff; + + cancelBecauseOfEdgeOrCornerMaybe(); + + prevTime = timestamp; + millisLeft -= timeDiff; + lastLeft = currentLeft; + lastTop = currentTop; + AnimationScheduler.get().requestAnimationFrame(this); + } + + private void cancelBecauseOfEdgeOrCornerMaybe() { + if (lastLeft == horizontalScrollbar.getScrollPos() + && lastTop == verticalScrollbar.getScrollPos()) { + cancel(); + } + } + + public void cancel() { + cancelled = true; + } + } + + /** + * ScrollDestination case-specific handling logic. + */ + private static double getScrollPos(final ScrollDestination destination, + final double targetStartPx, final double targetEndPx, + final double viewportStartPx, final double viewportEndPx, + final double padding) { + + final double viewportLength = viewportEndPx - viewportStartPx; + + switch (destination) { + + /* + * Scroll as little as possible to show the target element. If the + * element fits into view, this works as START or END depending on the + * current scroll position. If the element does not fit into view, this + * works as START. + */ + case ANY: { + final double startScrollPos = targetStartPx - padding; + final double endScrollPos = targetEndPx + padding - viewportLength; + + if (startScrollPos < viewportStartPx) { + return startScrollPos; + } else if (targetEndPx + padding > viewportEndPx) { + return endScrollPos; + } else { + // NOOP, it's already visible + return viewportStartPx; + } + } + + /* + * Scrolls so that the element is shown at the end of the viewport. The + * viewport will, however, not scroll before its first element. + */ + case END: { + return targetEndPx + padding - viewportLength; + } + + /* + * Scrolls so that the element is shown in the middle of the viewport. + * The viewport will, however, not scroll beyond its contents, given + * more elements than what the viewport is able to show at once. Under + * no circumstances will the viewport scroll before its first element. + */ + case MIDDLE: { + final double targetMiddle = targetStartPx + + (targetEndPx - targetStartPx) / 2; + return targetMiddle - viewportLength / 2; + } + + /* + * Scrolls so that the element is shown at the start of the viewport. + * The viewport will, however, not scroll beyond its contents. + */ + case START: { + return targetStartPx - padding; + } + + /* + * Throw an error if we're here. This can only mean that + * ScrollDestination has been carelessly amended.. + */ + default: { + throw new IllegalArgumentException( + "Internal: ScrollDestination has been modified, " + + "but Escalator.getScrollPos has not been updated " + + "to match new values."); + } + } + + } + + /** An inner class that handles all logic related to scrolling. */ + private class Scroller extends JsniWorkaround { + private double lastScrollTop = 0; + private double lastScrollLeft = 0; + /** + * The current flick scroll animator. This is <code>null</code> if the + * view isn't animating a flick scroll at the moment. + */ + private FlickScrollAnimator currentFlickScroller; + + public Scroller() { + super(Escalator.this); + } + + @Override + protected native JavaScriptObject createScrollListenerFunction( + Escalator esc) + /*-{ + var vScroll = esc.@com.vaadin.client.widgets.Escalator::verticalScrollbar; + var vScrollElem = vScroll.@com.vaadin.client.widget.escalator.ScrollbarBundle::getElement()(); + + var hScroll = esc.@com.vaadin.client.widgets.Escalator::horizontalScrollbar; + var hScrollElem = hScroll.@com.vaadin.client.widget.escalator.ScrollbarBundle::getElement()(); + + return $entry(function(e) { + var target = e.target || e.srcElement; // IE8 uses e.scrElement + + // in case the scroll event was native (i.e. scrollbars were dragged, or + // the scrollTop/Left was manually modified), the bundles have old cache + // values. We need to make sure that the caches are kept up to date. + if (target === vScrollElem) { + vScroll.@com.vaadin.client.widget.escalator.ScrollbarBundle::updateScrollPosFromDom()(); + } else if (target === hScrollElem) { + hScroll.@com.vaadin.client.widget.escalator.ScrollbarBundle::updateScrollPosFromDom()(); + } else { + $wnd.console.error("unexpected scroll target: "+target); + } + }); + }-*/; + + @Override + protected native JavaScriptObject createMousewheelListenerFunction( + Escalator esc) + /*-{ + return $entry(function(e) { + var deltaX = e.deltaX ? e.deltaX : -0.5*e.wheelDeltaX; + var deltaY = e.deltaY ? e.deltaY : -0.5*e.wheelDeltaY; + + // IE8 has only delta y + if (isNaN(deltaY)) { + deltaY = -0.5*e.wheelDelta; + } + + @com.vaadin.client.widgets.Escalator.JsniUtil::moveScrollFromEvent(*)(esc, deltaX, deltaY, e); + }); + }-*/; + + /** + * Recalculates the virtual viewport represented by the scrollbars, so + * that the sizes of the scroll handles appear correct in the browser + */ + public void recalculateScrollbarsForVirtualViewport() { + double scrollContentHeight = body.calculateTotalRowHeight(); + double scrollContentWidth = columnConfiguration.calculateRowWidth(); + + double tableWrapperHeight = heightOfEscalator; + double tableWrapperWidth = widthOfEscalator; + + boolean verticalScrollNeeded = scrollContentHeight > tableWrapperHeight + - header.heightOfSection - footer.heightOfSection; + boolean horizontalScrollNeeded = scrollContentWidth > tableWrapperWidth; + + // One dimension got scrollbars, but not the other. Recheck time! + if (verticalScrollNeeded != horizontalScrollNeeded) { + if (!verticalScrollNeeded && horizontalScrollNeeded) { + verticalScrollNeeded = scrollContentHeight > tableWrapperHeight + - header.heightOfSection + - footer.heightOfSection + - horizontalScrollbar.getScrollbarThickness(); + } else { + horizontalScrollNeeded = scrollContentWidth > tableWrapperWidth + - verticalScrollbar.getScrollbarThickness(); + } + } + + // let's fix the table wrapper size, since it's now stable. + if (verticalScrollNeeded) { + tableWrapperWidth -= verticalScrollbar.getScrollbarThickness(); + tableWrapperWidth = Math.max(0, tableWrapperWidth); + } + if (horizontalScrollNeeded) { + tableWrapperHeight -= horizontalScrollbar + .getScrollbarThickness(); + tableWrapperHeight = Math.max(0, tableWrapperHeight); + } + tableWrapper.getStyle().setHeight(tableWrapperHeight, Unit.PX); + tableWrapper.getStyle().setWidth(tableWrapperWidth, Unit.PX); + + double vScrollbarHeight = Math.max(0, tableWrapperHeight + - footer.heightOfSection - header.heightOfSection); + verticalScrollbar.setOffsetSize(vScrollbarHeight); + verticalScrollbar.setScrollSize(scrollContentHeight); + + /* + * If decreasing the amount of frozen columns, and scrolled to the + * right, the scroll position might reset. So we need to remember + * the scroll position, and re-apply it once the scrollbar size has + * been adjusted. + */ + double prevScrollPos = horizontalScrollbar.getScrollPos(); + + double unfrozenPixels = columnConfiguration + .getCalculatedColumnsWidth(Range.between( + columnConfiguration.getFrozenColumnCount(), + columnConfiguration.getColumnCount())); + double frozenPixels = scrollContentWidth - unfrozenPixels; + double hScrollOffsetWidth = tableWrapperWidth - frozenPixels; + horizontalScrollbar.setOffsetSize(hScrollOffsetWidth); + horizontalScrollbar.setScrollSize(unfrozenPixels); + horizontalScrollbar.getElement().getStyle() + .setLeft(frozenPixels, Unit.PX); + horizontalScrollbar.setScrollPos(prevScrollPos); + + /* + * only show the scrollbar wrapper if the scrollbar itself is + * visible. + */ + if (horizontalScrollbar.showsScrollHandle()) { + horizontalScrollbarDeco.getStyle().clearDisplay(); + } else { + horizontalScrollbarDeco.getStyle().setDisplay(Display.NONE); + } + + /* + * only show corner background divs if the vertical scrollbar is + * visible. + */ + Style hCornerStyle = headerDeco.getStyle(); + Style fCornerStyle = footerDeco.getStyle(); + if (verticalScrollbar.showsScrollHandle()) { + hCornerStyle.clearDisplay(); + fCornerStyle.clearDisplay(); + + if (horizontalScrollbar.showsScrollHandle()) { + double offset = horizontalScrollbar.getScrollbarThickness(); + fCornerStyle.setBottom(offset, Unit.PX); + } else { + fCornerStyle.clearBottom(); + } + } else { + hCornerStyle.setDisplay(Display.NONE); + fCornerStyle.setDisplay(Display.NONE); + } + } + + /** + * Logical scrolling event handler for the entire widget. + */ + public void onScroll() { + + final double scrollTop = verticalScrollbar.getScrollPos(); + final double scrollLeft = horizontalScrollbar.getScrollPos(); + if (lastScrollLeft != scrollLeft) { + for (int i = 0; i < columnConfiguration.frozenColumns; i++) { + header.updateFreezePosition(i, scrollLeft); + body.updateFreezePosition(i, scrollLeft); + footer.updateFreezePosition(i, scrollLeft); + } + + position.set(headElem, -scrollLeft, 0); + + /* + * TODO [[optimize]]: cache this value in case the instanceof + * check has undesirable overhead. This could also be a + * candidate for some deferred binding magic so that e.g. + * AbsolutePosition is not even considered in permutations that + * we know support something better. That would let the compiler + * completely remove the entire condition since it knows that + * the if will never be true. + */ + if (position instanceof AbsolutePosition) { + /* + * we don't want to put "top: 0" on the footer, since it'll + * render wrong, as we already have + * "bottom: $footer-height". + */ + footElem.getStyle().setLeft(-scrollLeft, Unit.PX); + } else { + position.set(footElem, -scrollLeft, 0); + } + + lastScrollLeft = scrollLeft; + } + + body.setBodyScrollPosition(scrollLeft, scrollTop); + + lastScrollTop = scrollTop; + body.updateEscalatorRowsOnScroll(); + /* + * TODO [[optimize]]: Might avoid a reflow by first calculating new + * scrolltop and scrolleft, then doing the escalator magic based on + * those numbers and only updating the positions after that. + */ + } + + public native void attachScrollListener(Element element) + /* + * Attaching events with JSNI instead of the GWT event mechanism because + * GWT didn't provide enough details in events, or triggering the event + * handlers with GWT bindings was unsuccessful. Maybe, with more time + * and skill, it could be done with better success. JavaScript overlay + * types might work. This might also get rid of the JsniWorkaround + * class. + */ + /*-{ + if (element.addEventListener) { + element.addEventListener("scroll", this.@com.vaadin.client.widgets.JsniWorkaround::scrollListenerFunction); + } else { + element.attachEvent("onscroll", this.@com.vaadin.client.widgets.JsniWorkaround::scrollListenerFunction); + } + }-*/; + + public native void detachScrollListener(Element element) + /* + * Attaching events with JSNI instead of the GWT event mechanism because + * GWT didn't provide enough details in events, or triggering the event + * handlers with GWT bindings was unsuccessful. Maybe, with more time + * and skill, it could be done with better success. JavaScript overlay + * types might work. This might also get rid of the JsniWorkaround + * class. + */ + /*-{ + if (element.addEventListener) { + element.removeEventListener("scroll", this.@com.vaadin.client.widgets.JsniWorkaround::scrollListenerFunction); + } else { + element.detachEvent("onscroll", this.@com.vaadin.client.widgets.JsniWorkaround::scrollListenerFunction); + } + }-*/; + + public native void attachMousewheelListener(Element element) + /* + * Attaching events with JSNI instead of the GWT event mechanism because + * GWT didn't provide enough details in events, or triggering the event + * handlers with GWT bindings was unsuccessful. Maybe, with more time + * and skill, it could be done with better success. JavaScript overlay + * types might work. This might also get rid of the JsniWorkaround + * class. + */ + /*-{ + if (element.addEventListener) { + // firefox likes "wheel", while others use "mousewheel" + var eventName = element.onwheel===undefined?"mousewheel":"wheel"; + element.addEventListener(eventName, this.@com.vaadin.client.widgets.JsniWorkaround::mousewheelListenerFunction); + } else { + // IE8 + element.attachEvent("onmousewheel", this.@com.vaadin.client.widgets.JsniWorkaround::mousewheelListenerFunction); + } + }-*/; + + public native void detachMousewheelListener(Element element) + /* + * Detaching events with JSNI instead of the GWT event mechanism because + * GWT didn't provide enough details in events, or triggering the event + * handlers with GWT bindings was unsuccessful. Maybe, with more time + * and skill, it could be done with better success. JavaScript overlay + * types might work. This might also get rid of the JsniWorkaround + * class. + */ + /*-{ + if (element.addEventListener) { + // firefox likes "wheel", while others use "mousewheel" + var eventName = element.onwheel===undefined?"mousewheel":"wheel"; + element.removeEventListener(eventName, this.@com.vaadin.client.widgets.JsniWorkaround::mousewheelListenerFunction); + } else { + // IE8 + element.detachEvent("onmousewheel", this.@com.vaadin.client.widgets.JsniWorkaround::mousewheelListenerFunction); + } + }-*/; + + public native void attachTouchListeners(Element element) + /* + * Detaching events with JSNI instead of the GWT event mechanism because + * GWT didn't provide enough details in events, or triggering the event + * handlers with GWT bindings was unsuccessful. Maybe, with more time + * and skill, it could be done with better success. JavaScript overlay + * types might work. This might also get rid of the JsniWorkaround + * class. + */ + /*-{ + if (element.addEventListener) { + element.addEventListener("touchstart", this.@com.vaadin.client.widgets.JsniWorkaround::touchStartFunction); + element.addEventListener("touchmove", this.@com.vaadin.client.widgets.JsniWorkaround::touchMoveFunction); + element.addEventListener("touchend", this.@com.vaadin.client.widgets.JsniWorkaround::touchEndFunction); + element.addEventListener("touchcancel", this.@com.vaadin.client.widgets.JsniWorkaround::touchEndFunction); + } else { + // this would be IE8, but we don't support it with touch + } + }-*/; + + public native void detachTouchListeners(Element element) + /* + * Detaching events with JSNI instead of the GWT event mechanism because + * GWT didn't provide enough details in events, or triggering the event + * handlers with GWT bindings was unsuccessful. Maybe, with more time + * and skill, it could be done with better success. JavaScript overlay + * types might work. This might also get rid of the JsniWorkaround + * class. + */ + /*-{ + if (element.removeEventListener) { + element.removeEventListener("touchstart", this.@com.vaadin.client.widgets.JsniWorkaround::touchStartFunction); + element.removeEventListener("touchmove", this.@com.vaadin.client.widgets.JsniWorkaround::touchMoveFunction); + element.removeEventListener("touchend", this.@com.vaadin.client.widgets.JsniWorkaround::touchEndFunction); + element.removeEventListener("touchcancel", this.@com.vaadin.client.widgets.JsniWorkaround::touchEndFunction); + } else { + // this would be IE8, but we don't support it with touch + } + }-*/; + + private void cancelFlickScroll() { + if (currentFlickScroller != null) { + currentFlickScroller.cancel(); + } + } + + /** + * Handles a touch-based flick scroll. + * + * @param deltaX + * the last scrolling delta in the x-axis in a touchmove + * @param deltaY + * the last scrolling delta in the y-axis in a touchmove + * @param lastTime + * the timestamp of the last touchmove + */ + public void handleFlickScroll(double deltaX, double deltaY, + double deltaT) { + currentFlickScroller = new FlickScrollAnimator(deltaX, deltaY, + deltaT); + AnimationScheduler.get() + .requestAnimationFrame(currentFlickScroller); + } + + public void scrollToColumn(final int columnIndex, + final ScrollDestination destination, final int padding) { + assert columnIndex >= columnConfiguration.frozenColumns : "Can't scroll to a frozen column"; + + /* + * To cope with frozen columns, we just pretend those columns are + * not there at all when calculating the position of the target + * column and the boundaries of the viewport. The resulting + * scrollLeft will be correct without compensation since the DOM + * structure effectively means that scrollLeft also ignores the + * frozen columns. + */ + final double frozenPixels = columnConfiguration + .getCalculatedColumnsWidth(Range.withLength(0, + columnConfiguration.frozenColumns)); + + final double targetStartPx = columnConfiguration + .getCalculatedColumnsWidth(Range.withLength(0, columnIndex)) + - frozenPixels; + final double targetEndPx = targetStartPx + + columnConfiguration.getColumnWidthActual(columnIndex); + + final double viewportStartPx = getScrollLeft(); + double viewportEndPx = viewportStartPx + + WidgetUtil + .getRequiredWidthBoundingClientRectDouble(getElement()) + - frozenPixels; + if (verticalScrollbar.showsScrollHandle()) { + viewportEndPx -= WidgetUtil.getNativeScrollbarSize(); + } + + final double scrollLeft = getScrollPos(destination, targetStartPx, + targetEndPx, viewportStartPx, viewportEndPx, padding); + + /* + * note that it doesn't matter if the scroll would go beyond the + * content, since the browser will adjust for that, and everything + * fall into line accordingly. + */ + setScrollLeft(scrollLeft); + } + + public void scrollToRow(final int rowIndex, + final ScrollDestination destination, final double padding) { + /* + * FIXME [[rowheight]]: coded to work only with default row heights + * - will not work with variable row heights + */ + final double targetStartPx = body.getDefaultRowHeight() * rowIndex; + final double targetEndPx = targetStartPx + + body.getDefaultRowHeight(); + + final double viewportStartPx = getScrollTop(); + final double viewportEndPx = viewportStartPx + + body.calculateHeight(); + + final double scrollTop = getScrollPos(destination, targetStartPx, + targetEndPx, viewportStartPx, viewportEndPx, padding); + + /* + * note that it doesn't matter if the scroll would go beyond the + * content, since the browser will adjust for that, and everything + * falls into line accordingly. + */ + setScrollTop(scrollTop); + } + } + + private class ColumnAutoWidthAssignScheduler { + private boolean isScheduled = false; + private final ScheduledCommand widthCommand = new ScheduledCommand() { + @Override + public void execute() { + if (!isScheduled) { + return; + } + + isScheduled = false; + + ColumnConfigurationImpl cc = columnConfiguration; + for (int col = 0; col < cc.getColumnCount(); col++) { + ColumnConfigurationImpl.Column column = cc.columns.get(col); + if (!column.isWidthFinalized()) { + cc.setColumnWidth(col, -1); + column.widthIsFinalized(); + } + } + } + }; + + /** + * Calculates the widths of all uncalculated cells once the javascript + * execution is done. + * <p> + * This method makes sure that any duplicate requests in the same cycle + * are ignored. + */ + public void reschedule() { + if (!isScheduled) { + isScheduled = true; + Scheduler.get().scheduleFinally(widthCommand); + } + } + + public void cancel() { + isScheduled = false; + } + } + + protected abstract class AbstractRowContainer implements RowContainer { + private EscalatorUpdater updater = EscalatorUpdater.NULL; + + private int rows; + + /** + * The table section element ({@code <thead>}, {@code <tbody>} or + * {@code <tfoot>}) the rows (i.e. {@code <tr>} tags) are contained in. + */ + protected final TableSectionElement root; + + /** The height of the combined rows in the DOM. Never negative. */ + protected double heightOfSection = 0; + + /** + * The primary style name of the escalator. Most commonly provided by + * Escalator as "v-escalator". + */ + private String primaryStyleName = null; + + /** + * A map containing cached values of an element's current top position. + * <p> + * Don't use this field directly, because it will not take proper care + * of all the bookkeeping required. + * + * @deprecated Use {@link #setRowPosition(Element, int, int)}, + * {@link #getRowTop(Element)} and + * {@link #removeRowPosition(Element)} instead. + */ + @Deprecated + private final Map<TableRowElement, Double> rowTopPositionMap = new HashMap<TableRowElement, Double>(); + + private boolean defaultRowHeightShouldBeAutodetected = true; + + private double defaultRowHeight = INITIAL_DEFAULT_ROW_HEIGHT; + + public AbstractRowContainer( + final TableSectionElement rowContainerElement) { + root = rowContainerElement; + } + + @Override + public Element getElement() { + return root; + } + + /** + * Gets the tag name of an element to represent a cell in a row. + * <p> + * Usually {@code "th"} or {@code "td"}. + * <p> + * <em>Note:</em> To actually <em>create</em> such an element, use + * {@link #createCellElement(int, int)} instead. + * + * @return the tag name for the element to represent cells as + * @see #createCellElement(int, int) + */ + protected abstract String getCellElementTagName(); + + @Override + public EscalatorUpdater getEscalatorUpdater() { + return updater; + } + + /** + * {@inheritDoc} + * <p> + * <em>Implementation detail:</em> This method does no DOM modifications + * (i.e. is very cheap to call) if there is no data for rows or columns + * when this method is called. + * + * @see #hasColumnAndRowData() + */ + @Override + public void setEscalatorUpdater(final EscalatorUpdater escalatorUpdater) { + if (escalatorUpdater == null) { + throw new IllegalArgumentException( + "escalator updater cannot be null"); + } + + updater = escalatorUpdater; + + if (hasColumnAndRowData() && getRowCount() > 0) { + refreshRows(0, getRowCount()); + } + } + + /** + * {@inheritDoc} + * <p> + * <em>Implementation detail:</em> This method does no DOM modifications + * (i.e. is very cheap to call) if there are no rows in the DOM when + * this method is called. + * + * @see #hasSomethingInDom() + */ + @Override + public void removeRows(final int index, final int numberOfRows) { + assertArgumentsAreValidAndWithinRange(index, numberOfRows); + + rows -= numberOfRows; + + if (!isAttached()) { + return; + } + + if (hasSomethingInDom()) { + paintRemoveRows(index, numberOfRows); + } + } + + /** + * Removes those row elements from the DOM that correspond to the given + * range of logical indices. This may be fewer than {@code numberOfRows} + * , even zero, if not all the removed rows are actually visible. + * <p> + * The implementation must call {@link #paintRemoveRow(Element, int)} + * for each row that is removed from the DOM. + * + * @param index + * the logical index of the first removed row + * @param numberOfRows + * number of logical rows to remove + */ + protected abstract void paintRemoveRows(final int index, + final int numberOfRows); + + /** + * Removes a row element from the DOM, invoking + * {@link #getEscalatorUpdater()} + * {@link EscalatorUpdater#preDetach(Row, Iterable) preDetach} and + * {@link EscalatorUpdater#postDetach(Row, Iterable) postDetach} before + * and after removing the row, respectively. + * <p> + * This method must be called for each removed DOM row by any + * {@link #paintRemoveRows(int, int)} implementation. + * + * @param tr + * the row element to remove. + */ + protected void paintRemoveRow(final TableRowElement tr, + final int logicalRowIndex) { + + flyweightRow.setup(tr, logicalRowIndex, + columnConfiguration.getCalculatedColumnWidths()); + + getEscalatorUpdater().preDetach(flyweightRow, + flyweightRow.getCells()); + + tr.removeFromParent(); + + getEscalatorUpdater().postDetach(flyweightRow, + flyweightRow.getCells()); + + /* + * the "assert" guarantees that this code is run only during + * development/debugging. + */ + assert flyweightRow.teardown(); + + } + + protected void assertArgumentsAreValidAndWithinRange(final int index, + final int numberOfRows) throws IllegalArgumentException, + IndexOutOfBoundsException { + if (numberOfRows < 1) { + throw new IllegalArgumentException( + "Number of rows must be 1 or greater (was " + + numberOfRows + ")"); + } + + if (index < 0 || index + numberOfRows > getRowCount()) { + throw new IndexOutOfBoundsException("The given " + + "row range (" + index + ".." + (index + numberOfRows) + + ") was outside of the current number of rows (" + + getRowCount() + ")"); + } + } + + @Override + public int getRowCount() { + return rows; + } + + /** + * {@inheritDoc} + * <p> + * <em>Implementation detail:</em> This method does no DOM modifications + * (i.e. is very cheap to call) if there is no data for columns when + * this method is called. + * + * @see #hasColumnAndRowData() + */ + @Override + public void insertRows(final int index, final int numberOfRows) { + if (index < 0 || index > getRowCount()) { + throw new IndexOutOfBoundsException("The given index (" + index + + ") was outside of the current number of rows (0.." + + getRowCount() + ")"); + } + + if (numberOfRows < 1) { + throw new IllegalArgumentException( + "Number of rows must be 1 or greater (was " + + numberOfRows + ")"); + } + + rows += numberOfRows; + + /* + * only add items in the DOM if the widget itself is attached to the + * DOM. We can't calculate sizes otherwise. + */ + if (isAttached()) { + paintInsertRows(index, numberOfRows); + + if (rows == numberOfRows) { + /* + * We are inserting the first rows in this container. We + * potentially need to autocalculate the widths for the + * cells for the first time. + * + * To make sure that can take the entire dataset into + * account, we'll do this deferredly, so that each container + * section gets populated before we start calculating. + */ + columnAutoWidthAssignScheduler.reschedule(); + } + } + } + + /** + * Actually add rows into the DOM, now that everything can be + * calculated. + * + * @param visualIndex + * the DOM index to add rows into + * @param numberOfRows + * the number of rows to insert + * @return a list of the added row elements + */ + protected abstract void paintInsertRows(final int visualIndex, + final int numberOfRows); + + protected List<TableRowElement> paintInsertStaticRows( + final int visualIndex, final int numberOfRows) { + assert isAttached() : "Can't paint rows if Escalator is not attached"; + + final List<TableRowElement> addedRows = new ArrayList<TableRowElement>(); + + if (numberOfRows < 1) { + return addedRows; + } + + Node referenceRow; + if (root.getChildCount() != 0 && visualIndex != 0) { + // get the row node we're inserting stuff after + referenceRow = root.getChild(visualIndex - 1); + } else { + // index is 0, so just prepend. + referenceRow = null; + } + + for (int row = visualIndex; row < visualIndex + numberOfRows; row++) { + final TableRowElement tr = TableRowElement.as(DOM.createTR()); + addedRows.add(tr); + tr.addClassName(getStylePrimaryName() + "-row"); + + for (int col = 0; col < columnConfiguration.getColumnCount(); col++) { + final double colWidth = columnConfiguration + .getColumnWidthActual(col); + final TableCellElement cellElem = createCellElement(colWidth); + tr.appendChild(cellElem); + + // Set stylename and position if new cell is frozen + if (col < columnConfiguration.frozenColumns) { + cellElem.addClassName("frozen"); + position.set(cellElem, scroller.lastScrollLeft, 0); + } + } + + referenceRow = paintInsertRow(referenceRow, tr, row); + } + reapplyRowWidths(); + + recalculateSectionHeight(); + + return addedRows; + } + + /** + * Inserts a single row into the DOM, invoking + * {@link #getEscalatorUpdater()} + * {@link EscalatorUpdater#preAttach(Row, Iterable) preAttach} and + * {@link EscalatorUpdater#postAttach(Row, Iterable) postAttach} before + * and after inserting the row, respectively. The row should have its + * cells already inserted. + * + * @param referenceRow + * the row after which to insert or null if insert as first + * @param tr + * the row to be inserted + * @param logicalRowIndex + * the logical index of the inserted row + * @return the inserted row to be used as the new reference + */ + protected Node paintInsertRow(Node referenceRow, + final TableRowElement tr, int logicalRowIndex) { + flyweightRow.setup(tr, logicalRowIndex, + columnConfiguration.getCalculatedColumnWidths()); + + getEscalatorUpdater().preAttach(flyweightRow, + flyweightRow.getCells()); + + referenceRow = insertAfterReferenceAndUpdateIt(root, tr, + referenceRow); + + getEscalatorUpdater().postAttach(flyweightRow, + flyweightRow.getCells()); + updater.update(flyweightRow, flyweightRow.getCells()); + + /* + * the "assert" guarantees that this code is run only during + * development/debugging. + */ + assert flyweightRow.teardown(); + return referenceRow; + } + + private Node insertAfterReferenceAndUpdateIt(final Element parent, + final Element elem, final Node referenceNode) { + if (referenceNode != null) { + parent.insertAfter(elem, referenceNode); + } else { + /* + * referencenode being null means we have offset 0, i.e. make it + * the first row + */ + /* + * TODO [[optimize]]: Is insertFirst or append faster for an + * empty root? + */ + parent.insertFirst(elem); + } + return elem; + } + + abstract protected void recalculateSectionHeight(); + + /** + * Returns the height of all rows in the row container. + */ + protected double calculateTotalRowHeight() { + return getDefaultRowHeight() * getRowCount(); + } + + /** + * {@inheritDoc} + * <p> + * <em>Implementation detail:</em> This method does no DOM modifications + * (i.e. is very cheap to call) if there is no data for columns when + * this method is called. + * + * @see #hasColumnAndRowData() + */ + @Override + // overridden because of JavaDoc + public void refreshRows(final int index, final int numberOfRows) { + Range rowRange = Range.withLength(index, numberOfRows); + Range colRange = Range.withLength(0, getColumnConfiguration() + .getColumnCount()); + refreshCells(rowRange, colRange); + } + + protected abstract void refreshCells(Range logicalRowRange, + Range colRange); + + void refreshRow(TableRowElement tr, int logicalRowIndex) { + refreshRow(tr, logicalRowIndex, Range.withLength(0, + getColumnConfiguration().getColumnCount())); + } + + void refreshRow(final TableRowElement tr, final int logicalRowIndex, + Range colRange) { + flyweightRow.setup(tr, logicalRowIndex, + columnConfiguration.getCalculatedColumnWidths()); + Iterable<FlyweightCell> cellsToUpdate = flyweightRow.getCells( + colRange.getStart(), colRange.length()); + updater.update(flyweightRow, cellsToUpdate); + + /* + * the "assert" guarantees that this code is run only during + * development/debugging. + */ + assert flyweightRow.teardown(); + } + + /** + * Create and setup an empty cell element. + * + * @param width + * the width of the cell, in pixels + * + * @return a set-up empty cell element + */ + public TableCellElement createCellElement(final double width) { + final TableCellElement cellElem = TableCellElement.as(DOM + .createElement(getCellElementTagName())); + + final double height = getDefaultRowHeight(); + assert height >= 0 : "defaultRowHeight was negative. There's a setter leak somewhere."; + cellElem.getStyle().setHeight(height, Unit.PX); + + if (width >= 0) { + cellElem.getStyle().setWidth(width, Unit.PX); + } + cellElem.addClassName(getStylePrimaryName() + "-cell"); + return cellElem; + } + + @Override + public TableRowElement getRowElement(int index) { + return getTrByVisualIndex(index); + } + + /** + * Gets the child element that is visually at a certain index + * + * @param index + * the index of the element to retrieve + * @return the element at position {@code index} + * @throws IndexOutOfBoundsException + * if {@code index} is not valid within {@link #root} + */ + protected abstract TableRowElement getTrByVisualIndex(int index) + throws IndexOutOfBoundsException; + + protected void paintRemoveColumns(final int offset, + final int numberOfColumns) { + for (int i = 0; i < root.getChildCount(); i++) { + TableRowElement row = getTrByVisualIndex(i); + flyweightRow.setup(row, i, + columnConfiguration.getCalculatedColumnWidths()); + + Iterable<FlyweightCell> attachedCells = flyweightRow.getCells( + offset, numberOfColumns); + getEscalatorUpdater().preDetach(flyweightRow, attachedCells); + + for (int j = 0; j < numberOfColumns; j++) { + row.getCells().getItem(offset).removeFromParent(); + } + + Iterable<FlyweightCell> detachedCells = flyweightRow + .getUnattachedCells(offset, numberOfColumns); + getEscalatorUpdater().postDetach(flyweightRow, detachedCells); + + assert flyweightRow.teardown(); + } + } + + protected void paintInsertColumns(final int offset, + final int numberOfColumns, boolean frozen) { + + for (int row = 0; row < root.getChildCount(); row++) { + final TableRowElement tr = getTrByVisualIndex(row); + paintInsertCells(tr, row, offset, numberOfColumns); + } + reapplyRowWidths(); + + if (frozen) { + for (int col = offset; col < offset + numberOfColumns; col++) { + setColumnFrozen(col, true); + } + } + } + + /** + * Inserts new cell elements into a single row element, invoking + * {@link #getEscalatorUpdater()} + * {@link EscalatorUpdater#preAttach(Row, Iterable) preAttach} and + * {@link EscalatorUpdater#postAttach(Row, Iterable) postAttach} before + * and after inserting the cells, respectively. + * <p> + * Precondition: The row must be already attached to the DOM and the + * FlyweightCell instances corresponding to the new columns added to + * {@code flyweightRow}. + * + * @param tr + * the row in which to insert the cells + * @param logicalRowIndex + * the index of the row + * @param offset + * the index of the first cell + * @param numberOfCells + * the number of cells to insert + */ + private void paintInsertCells(final TableRowElement tr, + int logicalRowIndex, final int offset, final int numberOfCells) { + + assert root.isOrHasChild(tr) : "The row must be attached to the document"; + + flyweightRow.setup(tr, logicalRowIndex, + columnConfiguration.getCalculatedColumnWidths()); + + Iterable<FlyweightCell> cells = flyweightRow.getUnattachedCells( + offset, numberOfCells); + + for (FlyweightCell cell : cells) { + final double colWidth = columnConfiguration + .getColumnWidthActual(cell.getColumn()); + final TableCellElement cellElem = createCellElement(colWidth); + cell.setElement(cellElem); + } + + getEscalatorUpdater().preAttach(flyweightRow, cells); + + Node referenceCell; + if (offset != 0) { + referenceCell = tr.getChild(offset - 1); + } else { + referenceCell = null; + } + + for (FlyweightCell cell : cells) { + referenceCell = insertAfterReferenceAndUpdateIt(tr, + cell.getElement(), referenceCell); + } + + getEscalatorUpdater().postAttach(flyweightRow, cells); + getEscalatorUpdater().update(flyweightRow, cells); + + assert flyweightRow.teardown(); + } + + public void setColumnFrozen(int column, boolean frozen) { + final NodeList<TableRowElement> childRows = root.getRows(); + + for (int row = 0; row < childRows.getLength(); row++) { + final TableRowElement tr = childRows.getItem(row); + + TableCellElement cell = tr.getCells().getItem(column); + if (frozen) { + cell.addClassName("frozen"); + } else { + cell.removeClassName("frozen"); + position.reset(cell); + } + } + + if (frozen) { + updateFreezePosition(column, scroller.lastScrollLeft); + } + } + + public void updateFreezePosition(int column, double scrollLeft) { + final NodeList<TableRowElement> childRows = root.getRows(); + + for (int row = 0; row < childRows.getLength(); row++) { + final TableRowElement tr = childRows.getItem(row); + + TableCellElement cell = tr.getCells().getItem(column); + position.set(cell, scrollLeft, 0); + } + } + + /** + * Iterates through all the cells in a column and returns the width of + * the widest element in this RowContainer. + * + * @param index + * the index of the column to inspect + * @return the pixel width of the widest element in the indicated column + */ + public double calculateMaxColWidth(int index) { + TableRowElement row = TableRowElement.as(root + .getFirstChildElement()); + double maxWidth = 0; + while (row != null) { + final TableCellElement cell = row.getCells().getItem(index); + final boolean isVisible = !cell.getStyle().getDisplay() + .equals(Display.NONE.getCssName()); + if (isVisible) { + maxWidth = Math.max(maxWidth, WidgetUtil + .getRequiredWidthBoundingClientRectDouble(cell)); + } + row = TableRowElement.as(row.getNextSiblingElement()); + } + return maxWidth; + } + + /** + * Reapplies all the cells' widths according to the calculated widths in + * the column configuration. + */ + public void reapplyColumnWidths() { + Element row = root.getFirstChildElement(); + while (row != null) { + Element cell = row.getFirstChildElement(); + int columnIndex = 0; + while (cell != null) { + final double width = getCalculatedColumnWidthWithColspan( + cell, columnIndex); + + /* + * TODO Should Escalator implement ProvidesResize at some + * point, this is where we need to do that. + */ + cell.getStyle().setWidth(width, Unit.PX); + + cell = cell.getNextSiblingElement(); + columnIndex++; + } + row = row.getNextSiblingElement(); + } + + reapplyRowWidths(); + } + + private double getCalculatedColumnWidthWithColspan(final Element cell, + final int columnIndex) { + final int colspan = cell.getPropertyInt(FlyweightCell.COLSPAN_ATTR); + Range spannedColumns = Range.withLength(columnIndex, colspan); + + /* + * Since browsers don't explode with overflowing colspans, escalator + * shouldn't either. + */ + if (spannedColumns.getEnd() > columnConfiguration.getColumnCount()) { + spannedColumns = Range.between(columnIndex, + columnConfiguration.getColumnCount()); + } + return columnConfiguration + .getCalculatedColumnsWidth(spannedColumns); + } + + /** + * Applies the total length of the columns to each row element. + * <p> + * <em>Note:</em> In contrast to {@link #reapplyColumnWidths()}, this + * method only modifies the width of the {@code <tr>} element, not the + * cells within. + */ + protected void reapplyRowWidths() { + double rowWidth = columnConfiguration.calculateRowWidth(); + + com.google.gwt.dom.client.Element row = root.getFirstChildElement(); + while (row != null) { + if (rowWidth >= 0) { + row.getStyle().setWidth(rowWidth, Unit.PX); + } + row = row.getNextSiblingElement(); + } + } + + /** + * The primary style name for the container. + * + * @param primaryStyleName + * the style name to use as prefix for all row and cell style + * names. + */ + protected void setStylePrimaryName(String primaryStyleName) { + String oldStyle = getStylePrimaryName(); + if (SharedUtil.equals(oldStyle, primaryStyleName)) { + return; + } + + this.primaryStyleName = primaryStyleName; + + // Update already rendered rows and cells + Element row = root.getRows().getItem(0); + while (row != null) { + UIObject.setStylePrimaryName(row, primaryStyleName + "-row"); + Element cell = TableRowElement.as(row).getCells().getItem(0); + while (cell != null) { + assert TableCellElement.is(cell); + UIObject.setStylePrimaryName(cell, primaryStyleName + + "-cell"); + cell = cell.getNextSiblingElement(); + } + row = row.getNextSiblingElement(); + } + } + + /** + * Returns the primary style name of the container. + * + * @return The primary style name or <code>null</code> if not set. + */ + protected String getStylePrimaryName() { + return primaryStyleName; + } + + @Override + public void setDefaultRowHeight(double px) + throws IllegalArgumentException { + if (px < 1) { + throw new IllegalArgumentException("Height must be positive. " + + px + " was given."); + } + + defaultRowHeightShouldBeAutodetected = false; + defaultRowHeight = px; + reapplyDefaultRowHeights(); + } + + @Override + public double getDefaultRowHeight() { + return defaultRowHeight; + } + + /** + * The default height of rows has (most probably) changed. + * <p> + * Make sure that the displayed rows with a default height are updated + * in height and top position. + * <p> + * <em>Note:</em>This implementation should not call + * {@link Escalator#recalculateElementSizes()} - it is done by the + * discretion of the caller of this method. + */ + protected abstract void reapplyDefaultRowHeights(); + + protected void reapplyRowHeight(final TableRowElement tr, + final double heightPx) { + assert heightPx >= 0 : "Height must not be negative"; + + Element cellElem = tr.getFirstChildElement(); + while (cellElem != null) { + cellElem.getStyle().setHeight(heightPx, Unit.PX); + cellElem = cellElem.getNextSiblingElement(); + } + + /* + * no need to apply height to tr-element, it'll be resized + * implicitly. + */ + } + + @SuppressWarnings("boxing") + protected void setRowPosition(final TableRowElement tr, final int x, + final double y) { + position.set(tr, x, y); + rowTopPositionMap.put(tr, y); + } + + @SuppressWarnings("boxing") + protected double getRowTop(final TableRowElement tr) { + return rowTopPositionMap.get(tr); + } + + protected void removeRowPosition(TableRowElement tr) { + rowTopPositionMap.remove(tr); + } + + public void autodetectRowHeightLater() { + Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() { + @Override + public void execute() { + if (defaultRowHeightShouldBeAutodetected && isAttached()) { + autodetectRowHeightNow(); + defaultRowHeightShouldBeAutodetected = false; + } + } + }); + } + + public void autodetectRowHeightNow() { + if (!isAttached()) { + // Run again when attached + defaultRowHeightShouldBeAutodetected = true; + return; + } + + final Element detectionTr = DOM.createTR(); + detectionTr.setClassName(getStylePrimaryName() + "-row"); + + final Element cellElem = DOM.createElement(getCellElementTagName()); + cellElem.setClassName(getStylePrimaryName() + "-cell"); + cellElem.setInnerText("Ij"); + + detectionTr.appendChild(cellElem); + root.appendChild(detectionTr); + double boundingHeight = WidgetUtil + .getRequiredHeightBoundingClientRectDouble(cellElem); + defaultRowHeight = Math.max(1.0d, boundingHeight); + root.removeChild(detectionTr); + + if (root.hasChildNodes()) { + reapplyDefaultRowHeights(); + applyHeightByRows(); + } + } + + @Override + public Cell getCell(final Element element) { + if (element == null) { + throw new IllegalArgumentException("Element cannot be null"); + } + + /* + * Ensure that element is not root nor the direct descendant of root + * (a row) and ensure the element is inside the dom hierarchy of the + * root element. If not, return. + */ + if (root == element || element.getParentElement() == root + || !root.isOrHasChild(element)) { + return null; + } + + /* + * Ensure element is the cell element by iterating up the DOM + * hierarchy until reaching cell element. + */ + Element cellElementCandidate = element; + while (cellElementCandidate.getParentElement().getParentElement() != root) { + cellElementCandidate = cellElementCandidate.getParentElement(); + } + final TableCellElement cellElement = TableCellElement + .as(cellElementCandidate); + + // Find dom column + int domColumnIndex = -1; + for (Element e = cellElement; e != null; e = e + .getPreviousSiblingElement()) { + domColumnIndex++; + } + + // Find dom row + int domRowIndex = -1; + for (Element e = cellElement.getParentElement(); e != null; e = e + .getPreviousSiblingElement()) { + domRowIndex++; + } + + return new Cell(domRowIndex, domColumnIndex, cellElement); + } + + double getMaxCellWidth(int colIndex) throws IllegalArgumentException { + double maxCellWidth = -1; + + assert isAttached() : "Can't measure max width of cell, since Escalator is not attached to the DOM."; + + NodeList<TableRowElement> rows = root.getRows(); + for (int row = 0; row < rows.getLength(); row++) { + TableRowElement rowElement = rows.getItem(row); + TableCellElement cellOriginal = rowElement.getCells().getItem( + colIndex); + + if (cellIsPartOfSpan(cellOriginal)) { + continue; + } + + /* + * To get the actual width of the contents, we need to get the + * cell content without any hardcoded height or width. + * + * But we don't want to modify the existing column, because that + * might trigger some unnecessary listeners and whatnot. So, + * instead, we make a deep clone of that cell, but without any + * explicit dimensions, and measure that instead. + */ + + TableCellElement cellClone = TableCellElement + .as((Element) cellOriginal.cloneNode(true)); + cellClone.getStyle().clearHeight(); + cellClone.getStyle().clearWidth(); + + rowElement.insertBefore(cellClone, cellOriginal); + double requiredWidth = WidgetUtil + .getRequiredWidthBoundingClientRectDouble(cellClone); + + if (BrowserInfo.get().isIE9()) { + /* + * IE9 does not support subpixels. Usually it is rounded + * down which leads to content not shown. Increase the + * counted required size by one just to be on the safe side. + */ + requiredWidth += 1; + } + + maxCellWidth = Math.max(requiredWidth, maxCellWidth); + cellClone.removeFromParent(); + } + + return maxCellWidth; + } + + private boolean cellIsPartOfSpan(TableCellElement cell) { + boolean cellHasColspan = cell.getColSpan() > 1; + boolean cellIsHidden = Display.NONE.getCssName().equals( + cell.getStyle().getDisplay()); + return cellHasColspan || cellIsHidden; + } + + void refreshColumns(int index, int numberOfColumns) { + if (getRowCount() > 0) { + Range rowRange = Range.withLength(0, getRowCount()); + Range colRange = Range.withLength(index, numberOfColumns); + refreshCells(rowRange, colRange); + } + } + } + + private abstract class AbstractStaticRowContainer extends + AbstractRowContainer { + public AbstractStaticRowContainer(final TableSectionElement headElement) { + super(headElement); + } + + @Override + protected void paintRemoveRows(final int index, final int numberOfRows) { + for (int i = index; i < index + numberOfRows; i++) { + final TableRowElement tr = root.getRows().getItem(index); + paintRemoveRow(tr, index); + } + recalculateSectionHeight(); + } + + @Override + protected TableRowElement getTrByVisualIndex(final int index) + throws IndexOutOfBoundsException { + if (index >= 0 && index < root.getChildCount()) { + return root.getRows().getItem(index); + } else { + throw new IndexOutOfBoundsException("No such visual index: " + + index); + } + } + + @Override + public void insertRows(int index, int numberOfRows) { + super.insertRows(index, numberOfRows); + recalculateElementSizes(); + applyHeightByRows(); + } + + @Override + public void removeRows(int index, int numberOfRows) { + + /* + * While the rows in a static section are removed, the scrollbar is + * temporarily shrunk and then re-expanded. This leads to the fact + * that the scroll position is scooted up a bit. This means that we + * need to reset the position here. + * + * If Escalator, at some point, gets a JIT evaluation functionality, + * this re-setting is a strong candidate for removal. + */ + double oldScrollPos = verticalScrollbar.getScrollPos(); + + super.removeRows(index, numberOfRows); + recalculateElementSizes(); + applyHeightByRows(); + + verticalScrollbar.setScrollPos(oldScrollPos); + } + + @Override + protected void reapplyDefaultRowHeights() { + if (root.getChildCount() == 0) { + return; + } + + Profiler.enter("Escalator.AbstractStaticRowContainer.reapplyDefaultRowHeights"); + + Element tr = root.getRows().getItem(0); + while (tr != null) { + reapplyRowHeight(TableRowElement.as(tr), getDefaultRowHeight()); + tr = tr.getNextSiblingElement(); + } + + /* + * Because all rows are immediately displayed in the static row + * containers, the section's overall height has most probably + * changed. + */ + recalculateSectionHeight(); + + Profiler.leave("Escalator.AbstractStaticRowContainer.reapplyDefaultRowHeights"); + } + + @Override + protected void recalculateSectionHeight() { + Profiler.enter("Escalator.AbstractStaticRowContainer.recalculateSectionHeight"); + + double newHeight = calculateTotalRowHeight(); + if (newHeight != heightOfSection) { + heightOfSection = newHeight; + sectionHeightCalculated(); + + /* + * We need to update the scrollbar dimension at this point. If + * we are scrolled too far down and the static section shrinks, + * the body will try to render rows that don't exist during + * body.verifyEscalatorCount. This is because the logical row + * indices are calculated from the scrollbar position. + */ + verticalScrollbar.setOffsetSize(heightOfEscalator + - header.heightOfSection - footer.heightOfSection); + + body.verifyEscalatorCount(); + } + + Profiler.leave("Escalator.AbstractStaticRowContainer.recalculateSectionHeight"); + } + + /** + * Informs the row container that the height of its respective table + * section has changed. + * <p> + * These calculations might affect some layouting logic, such as the + * body is being offset by the footer, the footer needs to be readjusted + * according to its height, and so on. + * <p> + * A table section is either header, body or footer. + */ + protected abstract void sectionHeightCalculated(); + + @Override + protected void refreshCells(Range logicalRowRange, Range colRange) { + Profiler.enter("Escalator.AbstractStaticRowContainer.refreshRows"); + + assertArgumentsAreValidAndWithinRange(logicalRowRange.getStart(), + logicalRowRange.length()); + + if (!isAttached()) { + return; + } + + /* + * TODO [[rowheight]]: even if no rows are evaluated in the current + * viewport, the heights of some unrendered rows might change in a + * refresh. This would cause the scrollbar to be adjusted (in + * scrollHeight and/or scrollTop). Do we want to take this into + * account? + */ + if (hasColumnAndRowData()) { + /* + * TODO [[rowheight]]: nudge rows down with + * refreshRowPositions() as needed + */ + for (int row = logicalRowRange.getStart(); row < logicalRowRange + .getEnd(); row++) { + final TableRowElement tr = getTrByVisualIndex(row); + refreshRow(tr, row, colRange); + } + } + + Profiler.leave("Escalator.AbstractStaticRowContainer.refreshRows"); + } + + @Override + protected void paintInsertRows(int visualIndex, int numberOfRows) { + paintInsertStaticRows(visualIndex, numberOfRows); + } + } + + private class HeaderRowContainer extends AbstractStaticRowContainer { + public HeaderRowContainer(final TableSectionElement headElement) { + super(headElement); + } + + @Override + protected void sectionHeightCalculated() { + bodyElem.getStyle().setMarginTop(heightOfSection, Unit.PX); + verticalScrollbar.getElement().getStyle() + .setTop(heightOfSection, Unit.PX); + headerDeco.getStyle().setHeight(heightOfSection, Unit.PX); + } + + @Override + protected String getCellElementTagName() { + return "th"; + } + + @Override + public void setStylePrimaryName(String primaryStyleName) { + super.setStylePrimaryName(primaryStyleName); + UIObject.setStylePrimaryName(root, primaryStyleName + "-header"); + } + } + + private class FooterRowContainer extends AbstractStaticRowContainer { + public FooterRowContainer(final TableSectionElement footElement) { + super(footElement); + } + + @Override + public void setStylePrimaryName(String primaryStyleName) { + super.setStylePrimaryName(primaryStyleName); + UIObject.setStylePrimaryName(root, primaryStyleName + "-footer"); + } + + @Override + protected String getCellElementTagName() { + return "td"; + } + + @Override + protected void sectionHeightCalculated() { + int vscrollHeight = (int) Math.floor(heightOfEscalator + - header.heightOfSection - footer.heightOfSection); + + final boolean horizontalScrollbarNeeded = columnConfiguration + .calculateRowWidth() > widthOfEscalator; + if (horizontalScrollbarNeeded) { + vscrollHeight -= horizontalScrollbar.getScrollbarThickness(); + } + + footerDeco.getStyle().setHeight(footer.heightOfSection, Unit.PX); + + verticalScrollbar.setOffsetSize(vscrollHeight); + } + } + + private class BodyRowContainer extends AbstractRowContainer { + /* + * TODO [[optimize]]: check whether a native JsArray might be faster + * than LinkedList + */ + /** + * The order in which row elements are rendered visually in the browser, + * with the help of CSS tricks. Usually has nothing to do with the DOM + * order. + * + * @see #sortDomElements() + */ + private final LinkedList<TableRowElement> visualRowOrder = new LinkedList<TableRowElement>(); + + /** + * The logical index of the topmost row. + * + * @deprecated Use the accessors {@link #setTopRowLogicalIndex(int)}, + * {@link #updateTopRowLogicalIndex(int)} and + * {@link #getTopRowLogicalIndex()} instead + */ + @Deprecated + private int topRowLogicalIndex = 0; + + private void setTopRowLogicalIndex(int topRowLogicalIndex) { + if (LogConfiguration.loggingIsEnabled(Level.INFO)) { + Logger.getLogger("Escalator.BodyRowContainer").fine( + "topRowLogicalIndex: " + this.topRowLogicalIndex + + " -> " + topRowLogicalIndex); + } + assert topRowLogicalIndex >= 0 : "topRowLogicalIndex became negative (top left cell contents: " + + visualRowOrder.getFirst().getCells().getItem(0) + .getInnerText() + ") "; + /* + * if there's a smart way of evaluating and asserting the max index, + * this would be a nice place to put it. I haven't found out an + * effective and generic solution. + */ + + this.topRowLogicalIndex = topRowLogicalIndex; + } + + private int getTopRowLogicalIndex() { + return topRowLogicalIndex; + } + + private void updateTopRowLogicalIndex(int diff) { + setTopRowLogicalIndex(topRowLogicalIndex + diff); + } + + private class DeferredDomSorter { + private static final int SORT_DELAY_MILLIS = 50; + + // as it happens, 3 frames = 50ms @ 60fps. + private static final int REQUIRED_FRAMES_PASSED = 3; + + private final AnimationCallback frameCounter = new AnimationCallback() { + @Override + public void execute(double timestamp) { + framesPassed++; + boolean domWasSorted = sortIfConditionsMet(); + if (!domWasSorted) { + animationHandle = AnimationScheduler.get() + .requestAnimationFrame(this); + } else { + waiting = false; + } + } + }; + + private int framesPassed; + private double startTime; + private AnimationHandle animationHandle; + + /** <code>true</code> if a sort is scheduled */ + public boolean waiting = false; + + public void reschedule() { + waiting = true; + resetConditions(); + animationHandle = AnimationScheduler.get() + .requestAnimationFrame(frameCounter); + } + + private boolean sortIfConditionsMet() { + boolean enoughFramesHavePassed = framesPassed >= REQUIRED_FRAMES_PASSED; + boolean enoughTimeHasPassed = (Duration.currentTimeMillis() - startTime) >= SORT_DELAY_MILLIS; + boolean notAnimatingFlick = (scroller.currentFlickScroller == null); + boolean conditionsMet = enoughFramesHavePassed + && enoughTimeHasPassed && notAnimatingFlick; + + if (conditionsMet) { + resetConditions(); + sortDomElements(); + } + + return conditionsMet; + } + + private void resetConditions() { + if (animationHandle != null) { + animationHandle.cancel(); + animationHandle = null; + } + startTime = Duration.currentTimeMillis(); + framesPassed = 0; + } + } + + private DeferredDomSorter domSorter = new DeferredDomSorter(); + + public BodyRowContainer(final TableSectionElement bodyElement) { + super(bodyElement); + } + + @Override + public void setStylePrimaryName(String primaryStyleName) { + super.setStylePrimaryName(primaryStyleName); + UIObject.setStylePrimaryName(root, primaryStyleName + "-body"); + } + + public void updateEscalatorRowsOnScroll() { + if (visualRowOrder.isEmpty()) { + return; + } + + boolean rowsWereMoved = false; + + final double topRowPos = getRowTop(visualRowOrder.getFirst()); + // TODO [[mpixscroll]] + final double scrollTop = tBodyScrollTop; + final double viewportOffset = topRowPos - scrollTop; + + /* + * TODO [[optimize]] this if-else can most probably be refactored + * into a neater block of code + */ + + if (viewportOffset > 0) { + // there's empty room on top + + /* + * FIXME [[rowheight]]: coded to work only with default row + * heights - will not work with variable row heights + */ + int originalRowsToMove = (int) Math.ceil(viewportOffset + / getDefaultRowHeight()); + int rowsToMove = Math.min(originalRowsToMove, + root.getChildCount()); + + final int end = root.getChildCount(); + final int start = end - rowsToMove; + /* + * FIXME [[rowheight]]: coded to work only with default row + * heights - will not work with variable row heights + */ + final int logicalRowIndex = (int) (scrollTop / getDefaultRowHeight()); + moveAndUpdateEscalatorRows(Range.between(start, end), 0, + logicalRowIndex); + + setTopRowLogicalIndex(logicalRowIndex); + + rowsWereMoved = true; + } + + else if (viewportOffset + getDefaultRowHeight() <= 0) { + /* + * FIXME [[rowheight]]: coded to work only with default row + * heights - will not work with variable row heights + */ + + /* + * the viewport has been scrolled more than the topmost visual + * row. + */ + + int originalRowsToMove = (int) Math.abs(viewportOffset + / getDefaultRowHeight()); + int rowsToMove = Math.min(originalRowsToMove, + root.getChildCount()); + + int logicalRowIndex; + if (rowsToMove < root.getChildCount()) { + /* + * We scroll so little that we can just keep adding the rows + * below the current escalator + */ + logicalRowIndex = getLogicalRowIndex(visualRowOrder + .getLast()) + 1; + } else { + /* + * FIXME [[rowheight]]: coded to work only with default row + * heights - will not work with variable row heights + */ + /* + * Since we're moving all escalator rows, we need to + * calculate the first logical row index from the scroll + * position. + */ + logicalRowIndex = (int) (scrollTop / getDefaultRowHeight()); + } + + /* + * Since we're moving the viewport downwards, the visual index + * is always at the bottom. Note: Due to how + * moveAndUpdateEscalatorRows works, this will work out even if + * we move all the rows, and try to place them "at the end". + */ + final int targetVisualIndex = root.getChildCount(); + + // make sure that we don't move rows over the data boundary + boolean aRowWasLeftBehind = false; + if (logicalRowIndex + rowsToMove > getRowCount()) { + /* + * TODO [[rowheight]]: with constant row heights, there's + * always exactly one row that will be moved beyond the data + * source, when viewport is scrolled to the end. This, + * however, isn't guaranteed anymore once row heights start + * varying. + */ + rowsToMove--; + aRowWasLeftBehind = true; + } + + moveAndUpdateEscalatorRows(Range.between(0, rowsToMove), + targetVisualIndex, logicalRowIndex); + + if (aRowWasLeftBehind) { + /* + * To keep visualRowOrder as a spatially contiguous block of + * rows, let's make sure that the one row we didn't move + * visually still stays with the pack. + */ + final Range strayRow = Range.withOnly(0); + + /* + * We cannot trust getLogicalRowIndex, because it hasn't yet + * been updated. But since we're leaving rows behind, it + * means we've scrolled to the bottom. So, instead, we + * simply count backwards from the end. + */ + final int topLogicalIndex = getRowCount() + - visualRowOrder.size(); + moveAndUpdateEscalatorRows(strayRow, 0, topLogicalIndex); + } + + final int naiveNewLogicalIndex = getTopRowLogicalIndex() + + originalRowsToMove; + final int maxLogicalIndex = getRowCount() + - visualRowOrder.size(); + setTopRowLogicalIndex(Math.min(naiveNewLogicalIndex, + maxLogicalIndex)); + + rowsWereMoved = true; + } + + if (rowsWereMoved) { + fireRowVisibilityChangeEvent(); + + if (scroller.touchHandlerBundle.touches == 0) { + /* + * this will never be called on touch scrolling. That is + * handled separately and explicitly by + * TouchHandlerBundle.touchEnd(); + */ + domSorter.reschedule(); + } + } + } + + @Override + protected void paintInsertRows(final int index, final int numberOfRows) { + if (numberOfRows == 0) { + return; + } + + /* + * TODO: this method should probably only add physical rows, and not + * populate them - let everything be populated as appropriate by the + * logic that follows. + * + * This also would lead to the fact that paintInsertRows wouldn't + * need to return anything. + */ + final List<TableRowElement> addedRows = fillAndPopulateEscalatorRowsIfNeeded( + index, numberOfRows); + + /* + * insertRows will always change the number of rows - update the + * scrollbar sizes. + */ + scroller.recalculateScrollbarsForVirtualViewport(); + + /* + * FIXME [[rowheight]]: coded to work only with default row heights + * - will not work with variable row heights + */ + final boolean addedRowsAboveCurrentViewport = index + * getDefaultRowHeight() < getScrollTop(); + final boolean addedRowsBelowCurrentViewport = index + * getDefaultRowHeight() > getScrollTop() + + calculateHeight(); + + if (addedRowsAboveCurrentViewport) { + /* + * We need to tweak the virtual viewport (scroll handle + * positions, table "scroll position" and row locations), but + * without re-evaluating any rows. + */ + + /* + * FIXME [[rowheight]]: coded to work only with default row + * heights - will not work with variable row heights + */ + final double yDelta = numberOfRows * getDefaultRowHeight(); + adjustScrollPosIgnoreEvents(yDelta); + updateTopRowLogicalIndex(numberOfRows); + } + + else if (addedRowsBelowCurrentViewport) { + // NOOP, we already recalculated scrollbars. + } + + else { // some rows were added inside the current viewport + + final int unupdatedLogicalStart = index + addedRows.size(); + final int visualOffset = getLogicalRowIndex(visualRowOrder + .getFirst()); + + /* + * At this point, we have added new escalator rows, if so + * needed. + * + * If more rows were added than the new escalator rows can + * account for, we need to start to spin the escalator to update + * the remaining rows aswell. + */ + final int rowsStillNeeded = numberOfRows - addedRows.size(); + final Range unupdatedVisual = convertToVisual(Range.withLength( + unupdatedLogicalStart, rowsStillNeeded)); + final int end = root.getChildCount(); + final int start = end - unupdatedVisual.length(); + final int visualTargetIndex = unupdatedLogicalStart + - visualOffset; + moveAndUpdateEscalatorRows(Range.between(start, end), + visualTargetIndex, unupdatedLogicalStart); + + /* + * FIXME [[rowheight]]: coded to work only with default row + * heights - will not work with variable row heights + */ + // move the surrounding rows to their correct places. + double rowTop = (unupdatedLogicalStart + (end - start)) + * getDefaultRowHeight(); + final ListIterator<TableRowElement> i = visualRowOrder + .listIterator(visualTargetIndex + (end - start)); + while (i.hasNext()) { + final TableRowElement tr = i.next(); + setRowPosition(tr, 0, rowTop); + /* + * FIXME [[rowheight]]: coded to work only with default row + * heights - will not work with variable row heights + */ + rowTop += getDefaultRowHeight(); + } + + fireRowVisibilityChangeEvent(); + sortDomElements(); + } + } + + /** + * Move escalator rows around, and make sure everything gets + * appropriately repositioned and repainted. + * + * @param visualSourceRange + * the range of rows to move to a new place + * @param visualTargetIndex + * the visual index where the rows will be placed to + * @param logicalTargetIndex + * the logical index to be assigned to the first moved row + * @throws IllegalArgumentException + * if any of <code>visualSourceRange.getStart()</code>, + * <code>visualTargetIndex</code> or + * <code>logicalTargetIndex</code> is a negative number; or + * if <code>visualTargetInfo</code> is greater than the + * number of escalator rows. + */ + private void moveAndUpdateEscalatorRows(final Range visualSourceRange, + final int visualTargetIndex, final int logicalTargetIndex) + throws IllegalArgumentException { + + if (visualSourceRange.isEmpty()) { + return; + } + + if (visualSourceRange.getStart() < 0) { + throw new IllegalArgumentException( + "Logical source start must be 0 or greater (was " + + visualSourceRange.getStart() + ")"); + } else if (logicalTargetIndex < 0) { + throw new IllegalArgumentException( + "Logical target must be 0 or greater"); + } else if (visualTargetIndex < 0) { + throw new IllegalArgumentException( + "Visual target must be 0 or greater"); + } else if (visualTargetIndex > root.getChildCount()) { + throw new IllegalArgumentException( + "Visual target must not be greater than the number of escalator rows"); + } else if (logicalTargetIndex + visualSourceRange.length() > getRowCount()) { + Range logicalTargetRange = Range.withLength(logicalTargetIndex, + visualSourceRange.length()); + Range availableRange = Range.withLength(0, getRowCount()); + throw new IllegalArgumentException("Logical target leads " + + "to rows outside of the data range (" + + logicalTargetRange + " goes beyond " + availableRange + + ")"); + } + + /* + * Since we move a range into another range, the indices might move + * about. Having 10 rows, if we move 0..1 to index 10 (to the end of + * the collection), the target range will end up being 8..9, instead + * of 10..11. + * + * This applies only if we move elements forward in the collection, + * not backward. + */ + final int adjustedVisualTargetIndex; + if (visualSourceRange.getStart() < visualTargetIndex) { + adjustedVisualTargetIndex = visualTargetIndex + - visualSourceRange.length(); + } else { + adjustedVisualTargetIndex = visualTargetIndex; + } + + if (visualSourceRange.getStart() != adjustedVisualTargetIndex) { + + /* + * Reorder the rows to their correct places within + * visualRowOrder (unless rows are moved back to their original + * places) + */ + + /* + * TODO [[optimize]]: move whichever set is smaller: the ones + * explicitly moved, or the others. So, with 10 escalator rows, + * if we are asked to move idx[0..8] to the end of the list, + * it's faster to just move idx[9] to the beginning. + */ + + final List<TableRowElement> removedRows = new ArrayList<TableRowElement>( + visualSourceRange.length()); + for (int i = 0; i < visualSourceRange.length(); i++) { + final TableRowElement tr = visualRowOrder + .remove(visualSourceRange.getStart()); + removedRows.add(tr); + } + visualRowOrder.addAll(adjustedVisualTargetIndex, removedRows); + } + + { // Refresh the contents of the affected rows + final ListIterator<TableRowElement> iter = visualRowOrder + .listIterator(adjustedVisualTargetIndex); + for (int logicalIndex = logicalTargetIndex; logicalIndex < logicalTargetIndex + + visualSourceRange.length(); logicalIndex++) { + final TableRowElement tr = iter.next(); + refreshRow(tr, logicalIndex); + } + } + + { // Reposition the rows that were moved + /* + * FIXME [[rowheight]]: coded to work only with default row + * heights - will not work with variable row heights + */ + double newRowTop = logicalTargetIndex * getDefaultRowHeight(); + + final ListIterator<TableRowElement> iter = visualRowOrder + .listIterator(adjustedVisualTargetIndex); + for (int i = 0; i < visualSourceRange.length(); i++) { + final TableRowElement tr = iter.next(); + setRowPosition(tr, 0, newRowTop); + /* + * FIXME [[rowheight]]: coded to work only with default row + * heights - will not work with variable row heights + */ + newRowTop += getDefaultRowHeight(); + } + } + } + + /** + * Adjust the scroll position without having the scroll handler have any + * side-effects. + * <p> + * <em>Note:</em> {@link Scroller#onScroll()} <em>will</em> be + * triggered, but it will not do anything, with the help of + * {@link Escalator#internalScrollEventCalls}. + * + * @param yDelta + * the delta of pixels to scrolls. A positive value moves the + * viewport downwards, while a negative value moves the + * viewport upwards + */ + public void adjustScrollPosIgnoreEvents(final double yDelta) { + if (yDelta == 0) { + return; + } + + verticalScrollbar.setScrollPosByDelta(yDelta); + + /* + * FIXME [[rowheight]]: coded to work only with default row heights + * - will not work with variable row heights + */ + final double rowTopPos = yDelta - (yDelta % getDefaultRowHeight()); + for (final TableRowElement tr : visualRowOrder) { + setRowPosition(tr, 0, getRowTop(tr) + rowTopPos); + } + setBodyScrollPosition(tBodyScrollLeft, tBodyScrollTop + yDelta); + } + + /** + * Adds new physical escalator rows to the DOM at the given index if + * there's still a need for more escalator rows. + * <p> + * If Escalator already is at (or beyond) max capacity, this method does + * nothing to the DOM. + * + * @param index + * the index at which to add new escalator rows. + * <em>Note:</em>It is assumed that the index is both the + * visual index and the logical index. + * @param numberOfRows + * the number of rows to add at <code>index</code> + * @return a list of the added rows + */ + private List<TableRowElement> fillAndPopulateEscalatorRowsIfNeeded( + final int index, final int numberOfRows) { + + final int escalatorRowsStillFit = getMaxEscalatorRowCapacity() + - root.getChildCount(); + final int escalatorRowsNeeded = Math.min(numberOfRows, + escalatorRowsStillFit); + + if (escalatorRowsNeeded > 0) { + + final List<TableRowElement> addedRows = paintInsertStaticRows( + index, escalatorRowsNeeded); + visualRowOrder.addAll(index, addedRows); + + /* + * We need to figure out the top positions for the rows we just + * added. + */ + for (int i = 0; i < addedRows.size(); i++) { + /* + * FIXME [[rowheight]]: coded to work only with default row + * heights - will not work with variable row heights + */ + setRowPosition(addedRows.get(i), 0, (index + i) + * getDefaultRowHeight()); + } + + /* Move the other rows away from above the added escalator rows */ + for (int i = index + addedRows.size(); i < visualRowOrder + .size(); i++) { + final TableRowElement tr = visualRowOrder.get(i); + /* + * FIXME [[rowheight]]: coded to work only with default row + * heights - will not work with variable row heights + */ + setRowPosition(tr, 0, i * getDefaultRowHeight()); + } + + return addedRows; + } else { + return new ArrayList<TableRowElement>(); + } + } + + private int getMaxEscalatorRowCapacity() { + /* + * FIXME [[rowheight]]: coded to work only with default row heights + * - will not work with variable row heights + */ + final int maxEscalatorRowCapacity = (int) Math + .ceil(calculateHeight() / getDefaultRowHeight()) + 1; + + /* + * maxEscalatorRowCapacity can become negative if the headers and + * footers start to overlap. This is a crazy situation, but Vaadin + * blinks the components a lot, so it's feasible. + */ + return Math.max(0, maxEscalatorRowCapacity); + } + + @Override + protected void paintRemoveRows(final int index, final int numberOfRows) { + if (numberOfRows == 0) { + return; + } + + final Range viewportRange = getVisibleRowRange(); + final Range removedRowsRange = Range + .withLength(index, numberOfRows); + + final Range[] partitions = removedRowsRange + .partitionWith(viewportRange); + final Range removedAbove = partitions[0]; + final Range removedLogicalInside = partitions[1]; + final Range removedVisualInside = convertToVisual(removedLogicalInside); + + /* + * TODO: extract the following if-block to a separate method. I'll + * leave this be inlined for now, to make linediff-based code + * reviewing easier. Probably will be moved in the following patch + * set. + */ + + /* + * Adjust scroll position in one of two scenarios: + * + * 1) Rows were removed above. Then we just need to adjust the + * scrollbar by the height of the removed rows. + * + * 2) There are no logical rows above, and at least the first (if + * not more) visual row is removed. Then we need to snap the scroll + * position to the first visible row (i.e. reset scroll position to + * absolute 0) + * + * The logic is optimized in such a way that the + * adjustScrollPosIgnoreEvents is called only once, to avoid extra + * reflows, and thus the code might seem a bit obscure. + */ + final boolean firstVisualRowIsRemoved = !removedVisualInside + .isEmpty() && removedVisualInside.getStart() == 0; + + if (!removedAbove.isEmpty() || firstVisualRowIsRemoved) { + /* + * FIXME [[rowheight]]: coded to work only with default row + * heights - will not work with variable row heights + */ + final double yDelta = removedAbove.length() + * getDefaultRowHeight(); + final double firstLogicalRowHeight = getDefaultRowHeight(); + final boolean removalScrollsToShowFirstLogicalRow = verticalScrollbar + .getScrollPos() - yDelta < firstLogicalRowHeight; + + if (removedVisualInside.isEmpty() + && (!removalScrollsToShowFirstLogicalRow || !firstVisualRowIsRemoved)) { + /* + * rows were removed from above the viewport, so all we need + * to do is to adjust the scroll position to account for the + * removed rows + */ + adjustScrollPosIgnoreEvents(-yDelta); + } else if (removalScrollsToShowFirstLogicalRow) { + /* + * It seems like we've removed all rows from above, and also + * into the current viewport. This means we'll need to even + * out the scroll position to exactly 0 (i.e. adjust by the + * current negative scrolltop, presto!), so that it isn't + * aligned funnily + */ + adjustScrollPosIgnoreEvents(-verticalScrollbar + .getScrollPos()); + } + } + + // ranges evaluated, let's do things. + if (!removedVisualInside.isEmpty()) { + int escalatorRowCount = bodyElem.getChildCount(); + + /* + * remember: the rows have already been subtracted from the row + * count at this point + */ + int rowsLeft = getRowCount(); + if (rowsLeft < escalatorRowCount) { + int escalatorRowsToRemove = escalatorRowCount - rowsLeft; + for (int i = 0; i < escalatorRowsToRemove; i++) { + final TableRowElement tr = visualRowOrder + .remove(removedVisualInside.getStart()); + + paintRemoveRow(tr, index); + removeRowPosition(tr); + } + escalatorRowCount -= escalatorRowsToRemove; + + /* + * Because we're removing escalator rows, we don't have + * anything to scroll by. Let's make sure the viewport is + * scrolled to top, to render any rows possibly left above. + */ + body.setBodyScrollPosition(tBodyScrollLeft, 0); + + /* + * We might have removed some rows from the middle, so let's + * make sure we're not left with any holes. Also remember: + * visualIndex == logicalIndex applies now. + */ + final int dirtyRowsStart = removedLogicalInside.getStart(); + for (int i = dirtyRowsStart; i < escalatorRowCount; i++) { + final TableRowElement tr = visualRowOrder.get(i); + /* + * FIXME [[rowheight]]: coded to work only with default + * row heights - will not work with variable row heights + */ + setRowPosition(tr, 0, i * getDefaultRowHeight()); + } + + /* + * this is how many rows appeared into the viewport from + * below + */ + final int rowsToUpdateDataOn = numberOfRows + - escalatorRowsToRemove; + final int start = Math.max(0, escalatorRowCount + - rowsToUpdateDataOn); + final int end = escalatorRowCount; + for (int i = start; i < end; i++) { + final TableRowElement tr = visualRowOrder.get(i); + refreshRow(tr, i); + } + } + + else { + // No escalator rows need to be removed. + + /* + * Two things (or a combination thereof) can happen: + * + * 1) We're scrolled to the bottom, the last rows are + * removed. SOLUTION: moveAndUpdateEscalatorRows the + * bottommost rows, and place them at the top to be + * refreshed. + * + * 2) We're scrolled somewhere in the middle, arbitrary rows + * are removed. SOLUTION: moveAndUpdateEscalatorRows the + * removed rows, and place them at the bottom to be + * refreshed. + * + * Since a combination can also happen, we need to handle + * this in a smart way, all while avoiding + * double-refreshing. + */ + + /* + * FIXME [[rowheight]]: coded to work only with default row + * heights - will not work with variable row heights + */ + final double contentBottom = getRowCount() + * getDefaultRowHeight(); + final double viewportBottom = tBodyScrollTop + + calculateHeight(); + if (viewportBottom <= contentBottom) { + /* + * We're in the middle of the row container, everything + * is added to the bottom + */ + paintRemoveRowsAtMiddle(removedLogicalInside, + removedVisualInside, 0); + } + + else if (removedVisualInside.contains(0) + && numberOfRows >= visualRowOrder.size()) { + /* + * We're removing so many rows that the viewport is + * pushed up more than a screenful. This means we can + * simply scroll up and everything will work without a + * sweat. + */ + + double left = horizontalScrollbar.getScrollPos(); + double top = contentBottom - visualRowOrder.size() + * getDefaultRowHeight(); + setBodyScrollPosition(left, top); + + Range allEscalatorRows = Range.withLength(0, + visualRowOrder.size()); + int logicalTargetIndex = getRowCount() + - allEscalatorRows.length(); + moveAndUpdateEscalatorRows(allEscalatorRows, 0, + logicalTargetIndex); + + /* + * Scrolling the body to the correct location will be + * fixed automatically. Because the amount of rows is + * decreased, the viewport is pushed up as the scrollbar + * shrinks. So no need to do anything there. + * + * TODO [[optimize]]: This might lead to a double body + * refresh. Needs investigation. + */ + } + + else if (contentBottom + + (numberOfRows * getDefaultRowHeight()) + - viewportBottom < getDefaultRowHeight()) { + /* + * We're at the end of the row container, everything is + * added to the top. + */ + + /* + * FIXME [[rowheight]]: above if-clause is coded to only + * work with default row heights - will not work with + * variable row heights + */ + + paintRemoveRowsAtBottom(removedLogicalInside, + removedVisualInside); + updateTopRowLogicalIndex(-removedLogicalInside.length()); + } + + else { + /* + * We're in a combination, where we need to both scroll + * up AND show new rows at the bottom. + * + * Example: Scrolled down to show the second to last + * row. Remove two. Viewport scrolls up, revealing the + * row above row. The last element collapses up and into + * view. + * + * Reminder: this use case handles only the case when + * there are enough escalator rows to still render a + * full view. I.e. all escalator rows will _always_ be + * populated + */ + /*- + * 1 1 |1| <- newly rendered + * |2| |2| |2| + * |3| ==> |*| ==> |5| <- newly rendered + * |4| |*| + * 5 5 + * + * 1 1 |1| <- newly rendered + * |2| |*| |4| + * |3| ==> |*| ==> |5| <- newly rendered + * |4| |4| + * 5 5 + */ + + /* + * STEP 1: + * + * reorganize deprecated escalator rows to bottom, but + * don't re-render anything yet + */ + /*- + * 1 1 1 + * |2| |*| |4| + * |3| ==> |*| ==> |*| + * |4| |4| |*| + * 5 5 5 + */ + double newTop = getRowTop(visualRowOrder + .get(removedVisualInside.getStart())); + for (int i = 0; i < removedVisualInside.length(); i++) { + final TableRowElement tr = visualRowOrder + .remove(removedVisualInside.getStart()); + visualRowOrder.addLast(tr); + } + + for (int i = removedVisualInside.getStart(); i < escalatorRowCount; i++) { + final TableRowElement tr = visualRowOrder.get(i); + setRowPosition(tr, 0, (int) newTop); + + /* + * FIXME [[rowheight]]: coded to work only with + * default row heights - will not work with variable + * row heights + */ + newTop += getDefaultRowHeight(); + } + + /* + * STEP 2: + * + * manually scroll + */ + /*- + * 1 |1| <-- newly rendered (by scrolling) + * |4| |4| + * |*| ==> |*| + * |*| + * 5 5 + */ + final double newScrollTop = contentBottom + - calculateHeight(); + setScrollTop(newScrollTop); + /* + * Manually call the scroll handler, so we get immediate + * effects in the escalator. + */ + scroller.onScroll(); + + /* + * Move the bottommost (n+1:th) escalator row to top, + * because scrolling up doesn't handle that for us + * automatically + */ + moveAndUpdateEscalatorRows( + Range.withOnly(escalatorRowCount - 1), + 0, + getLogicalRowIndex(visualRowOrder.getFirst()) - 1); + updateTopRowLogicalIndex(-1); + + /* + * STEP 3: + * + * update remaining escalator rows + */ + /*- + * |1| |1| + * |4| ==> |4| + * |*| |5| <-- newly rendered + * + * 5 + */ + + /* + * FIXME [[rowheight]]: coded to work only with default + * row heights - will not work with variable row heights + */ + final int rowsScrolled = (int) (Math + .ceil((viewportBottom - contentBottom) + / getDefaultRowHeight())); + final int start = escalatorRowCount + - (removedVisualInside.length() - rowsScrolled); + final Range visualRefreshRange = Range.between(start, + escalatorRowCount); + final int logicalTargetIndex = getLogicalRowIndex(visualRowOrder + .getFirst()) + start; + // in-place move simply re-renders the rows. + moveAndUpdateEscalatorRows(visualRefreshRange, start, + logicalTargetIndex); + } + } + + fireRowVisibilityChangeEvent(); + sortDomElements(); + } + + updateTopRowLogicalIndex(-removedAbove.length()); + + /* + * this needs to be done after the escalator has been shrunk down, + * or it won't work correctly (due to setScrollTop invocation) + */ + scroller.recalculateScrollbarsForVirtualViewport(); + } + + private void paintRemoveRowsAtMiddle(final Range removedLogicalInside, + final Range removedVisualInside, final int logicalOffset) { + /*- + * : : : + * |2| |2| |2| + * |3| ==> |*| ==> |4| + * |4| |4| |6| <- newly rendered + * : : : + */ + + final int escalatorRowCount = visualRowOrder.size(); + + final int logicalTargetIndex = getLogicalRowIndex(visualRowOrder + .getLast()) + - (removedVisualInside.length() - 1) + + logicalOffset; + moveAndUpdateEscalatorRows(removedVisualInside, escalatorRowCount, + logicalTargetIndex); + + // move the surrounding rows to their correct places. + final ListIterator<TableRowElement> iterator = visualRowOrder + .listIterator(removedVisualInside.getStart()); + + /* + * FIXME [[rowheight]]: coded to work only with default row heights + * - will not work with variable row heights + */ + double rowTop = (removedLogicalInside.getStart() + logicalOffset) + * getDefaultRowHeight(); + for (int i = removedVisualInside.getStart(); i < escalatorRowCount + - removedVisualInside.length(); i++) { + final TableRowElement tr = iterator.next(); + setRowPosition(tr, 0, rowTop); + /* + * FIXME [[rowheight]]: coded to work only with default row + * heights - will not work with variable row heights + */ + rowTop += getDefaultRowHeight(); + } + } + + private void paintRemoveRowsAtBottom(final Range removedLogicalInside, + final Range removedVisualInside) { + /*- + * : + * : : |4| <- newly rendered + * |5| |5| |5| + * |6| ==> |*| ==> |7| + * |7| |7| + */ + + final int logicalTargetIndex = getLogicalRowIndex(visualRowOrder + .getFirst()) - removedVisualInside.length(); + moveAndUpdateEscalatorRows(removedVisualInside, 0, + logicalTargetIndex); + + // move the surrounding rows to their correct places. + final ListIterator<TableRowElement> iterator = visualRowOrder + .listIterator(removedVisualInside.getEnd()); + /* + * FIXME [[rowheight]]: coded to work only with default row heights + * - will not work with variable row heights + */ + double rowTop = removedLogicalInside.getStart() + * getDefaultRowHeight(); + while (iterator.hasNext()) { + final TableRowElement tr = iterator.next(); + setRowPosition(tr, 0, rowTop); + /* + * FIXME [[rowheight]]: coded to work only with default row + * heights - will not work with variable row heights + */ + rowTop += getDefaultRowHeight(); + } + } + + private int getLogicalRowIndex(final Element tr) { + assert tr.getParentNode() == root : "The given element isn't a row element in the body"; + int internalIndex = visualRowOrder.indexOf(tr); + return getTopRowLogicalIndex() + internalIndex; + } + + @Override + protected void recalculateSectionHeight() { + // NOOP for body, since it doesn't make any sense. + } + + /** + * Adjusts the row index and number to be relevant for the current + * virtual viewport. + * <p> + * It converts a logical range of rows index to the matching visual + * range, truncating the resulting range with the viewport. + * <p> + * <ul> + * <li>Escalator contains logical rows 0..100 + * <li>Current viewport showing logical rows 20..29 + * <li>convertToVisual([20..29]) → [0..9] + * <li>convertToVisual([15..24]) → [0..4] + * <li>convertToVisual([25..29]) → [5..9] + * <li>convertToVisual([26..39]) → [6..9] + * <li>convertToVisual([0..5]) → [0..-1] <em>(empty)</em> + * <li>convertToVisual([35..1]) → [0..-1] <em>(empty)</em> + * <li>convertToVisual([0..100]) → [0..9] + * </ul> + * + * @return a logical range converted to a visual range, truncated to the + * current viewport. The first visual row has the index 0. + */ + private Range convertToVisual(final Range logicalRange) { + if (logicalRange.isEmpty()) { + return logicalRange; + } else if (visualRowOrder.isEmpty()) { + // empty range + return Range.withLength(0, 0); + } + + /* + * TODO [[rowheight]]: these assumptions will be totally broken with + * variable row heights. + */ + final int maxEscalatorRows = getMaxEscalatorRowCapacity(); + final int currentTopRowIndex = getLogicalRowIndex(visualRowOrder + .getFirst()); + + final Range[] partitions = logicalRange.partitionWith(Range + .withLength(currentTopRowIndex, maxEscalatorRows)); + final Range insideRange = partitions[1]; + return insideRange.offsetBy(-currentTopRowIndex); + } + + @Override + protected String getCellElementTagName() { + return "td"; + } + + /** + * Calculates the height of the {@code <tbody>} as it should be rendered + * in the DOM. + */ + private double calculateHeight() { + final int tableHeight = tableWrapper.getOffsetHeight(); + final double footerHeight = footer.heightOfSection; + final double headerHeight = header.heightOfSection; + return tableHeight - footerHeight - headerHeight; + } + + @Override + protected void refreshCells(Range logicalRowRange, Range colRange) { + Profiler.enter("Escalator.BodyRowContainer.refreshRows"); + + final Range visualRange = convertToVisual(logicalRowRange); + + if (!visualRange.isEmpty()) { + final int firstLogicalRowIndex = getLogicalRowIndex(visualRowOrder + .getFirst()); + for (int rowNumber = visualRange.getStart(); rowNumber < visualRange + .getEnd(); rowNumber++) { + refreshRow(visualRowOrder.get(rowNumber), + firstLogicalRowIndex + rowNumber, colRange); + } + } + + Profiler.leave("Escalator.BodyRowContainer.refreshRows"); + } + + @Override + protected TableRowElement getTrByVisualIndex(final int index) + throws IndexOutOfBoundsException { + if (index >= 0 && index < visualRowOrder.size()) { + return visualRowOrder.get(index); + } else { + throw new IndexOutOfBoundsException("No such visual index: " + + index); + } + } + + @Override + public TableRowElement getRowElement(int index) { + if (index < 0 || index >= getRowCount()) { + throw new IndexOutOfBoundsException("No such logical index: " + + index); + } + int visualIndex = index + - getLogicalRowIndex(visualRowOrder.getFirst()); + if (visualIndex >= 0 && visualIndex < visualRowOrder.size()) { + return super.getRowElement(visualIndex); + } else { + throw new IllegalStateException("Row with logical index " + + index + " is currently not available in the DOM"); + } + } + + private void setBodyScrollPosition(final double scrollLeft, + final double scrollTop) { + tBodyScrollLeft = scrollLeft; + tBodyScrollTop = scrollTop; + position.set(bodyElem, -tBodyScrollLeft, -tBodyScrollTop); + } + + /** + * Make sure that there is a correct amount of escalator rows: Add more + * if needed, or remove any superfluous ones. + * <p> + * This method should be called when e.g. the height of the Escalator + * changes. + * <p> + * <em>Note:</em> This method will make sure that the escalator rows are + * placed in the proper places. By default new rows are added below, but + * if the content is scrolled down, the rows are populated on top + * instead. + */ + public void verifyEscalatorCount() { + /* + * This method indeed has a smell very similar to paintRemoveRows + * and paintInsertRows. + * + * Unfortunately, those the code can't trivially be shared, since + * there are some slight differences in the respective + * responsibilities. The "paint" methods fake the addition and + * removal of rows, and make sure to either push existing data out + * of view, or draw new data into view. Only in some special cases + * will the DOM element count change. + * + * This method, however, has the explicit responsibility to verify + * that when "something" happens, we still have the correct amount + * of escalator rows in the DOM, and if not, we make sure to modify + * that count. Only in some special cases do we need to take into + * account other things than simply modifying the DOM element count. + */ + + Profiler.enter("Escalator.BodyRowContainer.verifyEscalatorCount"); + + if (!isAttached()) { + return; + } + + final int maxEscalatorRows = getMaxEscalatorRowCapacity(); + final int neededEscalatorRows = Math.min(maxEscalatorRows, + body.getRowCount()); + final int neededEscalatorRowsDiff = neededEscalatorRows + - visualRowOrder.size(); + + if (neededEscalatorRowsDiff > 0) { + // needs more + + /* + * This is a workaround for the issue where we might be scrolled + * to the bottom, and the widget expands beyond the content + * range + */ + + final int index = visualRowOrder.size(); + final int nextLastLogicalIndex; + if (!visualRowOrder.isEmpty()) { + nextLastLogicalIndex = getLogicalRowIndex(visualRowOrder + .getLast()) + 1; + } else { + nextLastLogicalIndex = 0; + } + + final boolean contentWillFit = nextLastLogicalIndex < getRowCount() + - neededEscalatorRowsDiff; + if (contentWillFit) { + final List<TableRowElement> addedRows = fillAndPopulateEscalatorRowsIfNeeded( + index, neededEscalatorRowsDiff); + + /* + * Since fillAndPopulateEscalatorRowsIfNeeded operates on + * the assumption that index == visual index == logical + * index, we thank for the added escalator rows, but since + * they're painted in the wrong CSS position, we need to + * move them to their actual locations. + * + * Note: this is the second (see body.paintInsertRows) + * occasion where fillAndPopulateEscalatorRowsIfNeeded would + * behave "more correctly" if it only would add escalator + * rows to the DOM and appropriate bookkeping, and not + * actually populate them :/ + */ + moveAndUpdateEscalatorRows( + Range.withLength(index, addedRows.size()), index, + nextLastLogicalIndex); + } else { + /* + * TODO [[optimize]] + * + * We're scrolled so far down that all rows can't be simply + * appended at the end, since we might start displaying + * escalator rows that don't exist. To avoid the mess that + * is body.paintRemoveRows, this is a dirty hack that dumbs + * the problem down to a more basic and already-solved + * problem: + * + * 1) scroll all the way up 2) add the missing escalator + * rows 3) scroll back to the original position. + * + * Letting the browser scroll back to our original position + * will automatically solve any possible overflow problems, + * since the browser will not allow us to scroll beyond the + * actual content. + */ + + final double oldScrollTop = getScrollTop(); + setScrollTop(0); + scroller.onScroll(); + fillAndPopulateEscalatorRowsIfNeeded(index, + neededEscalatorRowsDiff); + setScrollTop(oldScrollTop); + scroller.onScroll(); + } + } + + else if (neededEscalatorRowsDiff < 0) { + // needs less + + final ListIterator<TableRowElement> iter = visualRowOrder + .listIterator(visualRowOrder.size()); + for (int i = 0; i < -neededEscalatorRowsDiff; i++) { + final Element last = iter.previous(); + last.removeFromParent(); + iter.remove(); + } + + /* + * If we were scrolled to the bottom so that we didn't have an + * extra escalator row at the bottom, we'll probably end up with + * blank space at the bottom of the escalator, and one extra row + * above the header. + * + * Experimentation idea #1: calculate "scrollbottom" vs content + * bottom and remove one row from top, rest from bottom. This + * FAILED, since setHeight has already happened, thus we never + * will detect ourselves having been scrolled all the way to the + * bottom. + */ + + if (!visualRowOrder.isEmpty()) { + final double firstRowTop = getRowTop(visualRowOrder + .getFirst()); + /* + * FIXME [[rowheight]]: coded to work only with default row + * heights - will not work with variable row heights + */ + final double firstRowMinTop = tBodyScrollTop + - getDefaultRowHeight(); + if (firstRowTop < firstRowMinTop) { + final int newLogicalIndex = getLogicalRowIndex(visualRowOrder + .getLast()) + 1; + moveAndUpdateEscalatorRows(Range.withOnly(0), + visualRowOrder.size(), newLogicalIndex); + } + } + } + + if (neededEscalatorRowsDiff != 0) { + fireRowVisibilityChangeEvent(); + } + + Profiler.leave("Escalator.BodyRowContainer.verifyEscalatorCount"); + } + + @Override + protected void reapplyDefaultRowHeights() { + if (visualRowOrder.isEmpty()) { + return; + } + + /* + * As an intermediate step between hard-coded row heights to crazily + * varying row heights, Escalator will support the modification of + * the default row height (which is applied to all rows). + * + * This allows us to do some assumptions and simplifications for + * now. This code is intended to be quite short-lived, but gives + * insight into what needs to be done when row heights change in the + * body, in a general sense. + * + * TODO [[rowheight]] remove this comment once row heights may + * genuinely vary. + */ + + Profiler.enter("Escalator.BodyRowContainer.reapplyDefaultRowHeights"); + + /* step 1: resize and reposition rows */ + for (int i = 0; i < visualRowOrder.size(); i++) { + TableRowElement tr = visualRowOrder.get(i); + reapplyRowHeight(tr, getDefaultRowHeight()); + + final int logicalIndex = getTopRowLogicalIndex() + i; + setRowPosition(tr, 0, logicalIndex * getDefaultRowHeight()); + } + + /* + * step 2: move scrollbar so that it corresponds to its previous + * place + */ + + /* + * This ratio needs to be calculated with the scrollsize (not max + * scroll position) in order to align the top row with the new + * scroll position. + */ + double scrollRatio = verticalScrollbar.getScrollPos() + / verticalScrollbar.getScrollSize(); + scroller.recalculateScrollbarsForVirtualViewport(); + verticalScrollbar.setScrollPos((int) (getDefaultRowHeight() + * getRowCount() * scrollRatio)); + setBodyScrollPosition(horizontalScrollbar.getScrollPos(), + verticalScrollbar.getScrollPos()); + scroller.onScroll(); + + /* step 3: make sure we have the correct amount of escalator rows. */ + verifyEscalatorCount(); + + /* + * TODO [[rowheight]] This simply doesn't work with variable rows + * heights. + */ + int logicalLogical = (int) (getRowTop(visualRowOrder.getFirst()) / getDefaultRowHeight()); + setTopRowLogicalIndex(logicalLogical); + + Profiler.leave("Escalator.BodyRowContainer.reapplyDefaultRowHeights"); + } + + /** + * Sorts the rows in the DOM to correspond to the visual order. + * + * @see #visualRowOrder + */ + private void sortDomElements() { + final String profilingName = "Escalator.BodyRowContainer.sortDomElements"; + Profiler.enter(profilingName); + + /* + * Focus is lost from an element if that DOM element is (or any of + * its parents are) removed from the document. Therefore, we sort + * everything around that row instead. + */ + final TableRowElement focusedRow = getEscalatorRowWithFocus(); + + if (focusedRow != null) { + assert focusedRow.getParentElement() == root : "Trying to sort around a row that doesn't exist in body"; + assert visualRowOrder.contains(focusedRow) : "Trying to sort around a row that doesn't exist in visualRowOrder."; + } + + /* + * Two cases handled simultaneously: + * + * 1) No focus on rows. We iterate visualRowOrder backwards, and + * take the respective element in the DOM, and place it as the first + * child in the body element. Then we take the next-to-last from + * visualRowOrder, and put that first, pushing the previous row as + * the second child. And so on... + * + * 2) Focus on some row within Escalator body. Again, we iterate + * visualRowOrder backwards. This time, we use the focused row as a + * pivot: Instead of placing rows from the bottom of visualRowOrder + * and placing it first, we place it underneath the focused row. + * Once we hit the focused row, we don't move it (to not reset + * focus) but change sorting mode. After that, we place all rows as + * the first child. + */ + + /* + * If we have a focused row, start in the mode where we put + * everything underneath that row. Otherwise, all rows are placed as + * first child. + */ + boolean insertFirst = (focusedRow == null); + + final ListIterator<TableRowElement> i = visualRowOrder + .listIterator(visualRowOrder.size()); + while (i.hasPrevious()) { + TableRowElement tr = i.previous(); + + if (tr == focusedRow) { + insertFirst = true; + } else if (insertFirst) { + root.insertFirst(tr); + } else { + root.insertAfter(tr, focusedRow); + } + } + + Profiler.leave(profilingName); + } + + /** + * Get the escalator row that has focus. + * + * @return The escalator row that contains a focused DOM element, or + * <code>null</code> if focus is outside of a body row. + */ + private TableRowElement getEscalatorRowWithFocus() { + TableRowElement rowContainingFocus = null; + + final Element focusedElement = WidgetUtil.getFocusedElement(); + + if (focusedElement != null && root.isOrHasChild(focusedElement)) { + Element e = focusedElement; + + while (e != null && e != root) { + /* + * You never know if there's several tables embedded in a + * cell... We'll take the deepest one. + */ + if (TableRowElement.is(e)) { + rowContainingFocus = TableRowElement.as(e); + } + e = e.getParentElement(); + } + } + + return rowContainingFocus; + } + + @Override + public Cell getCell(Element element) { + Cell cell = super.getCell(element); + if (cell == null) { + return null; + } + + // Convert DOM coordinates to logical coordinates for rows + Element rowElement = cell.getElement().getParentElement(); + return new Cell(getLogicalRowIndex(rowElement), cell.getColumn(), + cell.getElement()); + } + } + + private class ColumnConfigurationImpl implements ColumnConfiguration { + public class Column { + private static final int DEFAULT_COLUMN_WIDTH_PX = 100; + + private double definedWidth = -1; + private double calculatedWidth = DEFAULT_COLUMN_WIDTH_PX; + private boolean measuringRequested = false; + + /** + * If a column has been created (either via insertRow or + * insertColumn), it will be given an arbitrary width, and only then + * a width will be defined. + */ + private boolean widthHasBeenFinalized = false; + + public void setWidth(double px) { + definedWidth = px; + + if (px < 0) { + if (isAttached()) { + calculateWidth(); + } else { + /* + * the column's width is calculated at Escalator.onLoad + * via measureIfNeeded! + */ + measuringRequested = true; + } + } else { + calculatedWidth = px; + } + } + + public double getDefinedWidth() { + return definedWidth; + } + + /** + * Returns the actual width in the DOM. + * + * @return the width in pixels in the DOM. Returns -1 if the column + * needs measuring, but has not been yet measured + */ + public double getCalculatedWidth() { + /* + * This might return an untrue value (e.g. during init/onload), + * since we haven't had a proper chance to actually calculate + * widths yet. + * + * This is fixed during Escalator.onLoad, by the call to + * "measureIfNeeded", which fixes "everything". + */ + if (!measuringRequested) { + return calculatedWidth; + } else { + return -1; + } + } + + /** + * Checks if the column needs measuring, and then measures it. + * <p> + * Called by {@link Escalator#onLoad()}. + */ + public boolean measureAndSetWidthIfNeeded() { + assert isAttached() : "Column.measureIfNeeded() was called even though Escalator was not attached!"; + + if (measuringRequested) { + measuringRequested = false; + setWidth(definedWidth); + return true; + } + return false; + } + + private void calculateWidth() { + calculatedWidth = getMaxCellWidth(columns.indexOf(this)); + } + + public void widthIsFinalized() { + columnAutoWidthAssignScheduler.cancel(); + widthHasBeenFinalized = true; + } + + public boolean isWidthFinalized() { + return widthHasBeenFinalized; + } + } + + private final List<Column> columns = new ArrayList<Column>(); + private int frozenColumns = 0; + + /** + * A cached array of all the calculated column widths. + * + * @see #getCalculatedColumnWidths() + */ + private double[] widthsArray = null; + + /** + * {@inheritDoc} + * <p> + * <em>Implementation detail:</em> This method does no DOM modifications + * (i.e. is very cheap to call) if there are no rows in the DOM when + * this method is called. + * + * @see #hasSomethingInDom() + */ + @Override + public void removeColumns(final int index, final int numberOfColumns) { + // Validate + assertArgumentsAreValidAndWithinRange(index, numberOfColumns); + + // Move the horizontal scrollbar to the left, if removed columns are + // to the left of the viewport + removeColumnsAdjustScrollbar(index, numberOfColumns); + + // Remove from DOM + header.paintRemoveColumns(index, numberOfColumns); + body.paintRemoveColumns(index, numberOfColumns); + footer.paintRemoveColumns(index, numberOfColumns); + + // Remove from bookkeeping + flyweightRow.removeCells(index, numberOfColumns); + columns.subList(index, index + numberOfColumns).clear(); + + // Adjust frozen columns + if (index < getFrozenColumnCount()) { + if (index + numberOfColumns < frozenColumns) { + /* + * Last removed column was frozen, meaning that all removed + * columns were frozen. Just decrement the number of frozen + * columns accordingly. + */ + frozenColumns -= numberOfColumns; + } else { + /* + * If last removed column was not frozen, we have removed + * columns beyond the frozen range, so all remaining frozen + * columns are to the left of the removed columns. + */ + frozenColumns = index; + } + } + + scroller.recalculateScrollbarsForVirtualViewport(); + body.verifyEscalatorCount(); + + if (getColumnConfiguration().getColumnCount() > 0) { + reapplyRowWidths(header); + reapplyRowWidths(body); + reapplyRowWidths(footer); + } + + /* + * Colspans make any kind of automatic clever content re-rendering + * impossible: As soon as anything has colspans, removing one might + * reveal further colspans, modifying the DOM structure once again, + * ending in a cascade of updates. Because we don't know how the + * data is updated. + * + * So, instead, we don't do anything. The client code is responsible + * for re-rendering the content (if so desired). Everything Just + * Works (TM) if colspans aren't used. + */ + } + + private void reapplyRowWidths(AbstractRowContainer container) { + if (container.getRowCount() > 0) { + container.reapplyRowWidths(); + } + } + + private void removeColumnsAdjustScrollbar(int index, int numberOfColumns) { + if (horizontalScrollbar.getOffsetSize() >= horizontalScrollbar + .getScrollSize()) { + return; + } + + double leftPosOfFirstColumnToRemove = getCalculatedColumnsWidth(Range + .between(0, index)); + double widthOfColumnsToRemove = getCalculatedColumnsWidth(Range + .withLength(index, numberOfColumns)); + + double scrollLeft = horizontalScrollbar.getScrollPos(); + + if (scrollLeft <= leftPosOfFirstColumnToRemove) { + /* + * viewport is scrolled to the left of the first removed column, + * so there's no need to adjust anything + */ + return; + } + + double adjustedScrollLeft = Math.max(leftPosOfFirstColumnToRemove, + scrollLeft - widthOfColumnsToRemove); + horizontalScrollbar.setScrollPos(adjustedScrollLeft); + } + + /** + * Calculate the width of a row, as the sum of columns' widths. + * + * @return the width of a row, in pixels + */ + public double calculateRowWidth() { + return getCalculatedColumnsWidth(Range.between(0, getColumnCount())); + } + + private void assertArgumentsAreValidAndWithinRange(final int index, + final int numberOfColumns) { + if (numberOfColumns < 1) { + throw new IllegalArgumentException( + "Number of columns can't be less than 1 (was " + + numberOfColumns + ")"); + } + + if (index < 0 || index + numberOfColumns > getColumnCount()) { + throw new IndexOutOfBoundsException("The given " + + "column range (" + index + ".." + + (index + numberOfColumns) + + ") was outside of the current " + + "number of columns (" + getColumnCount() + ")"); + } + } + + /** + * {@inheritDoc} + * <p> + * <em>Implementation detail:</em> This method does no DOM modifications + * (i.e. is very cheap to call) if there is no data for rows when this + * method is called. + * + * @see #hasColumnAndRowData() + */ + @Override + public void insertColumns(final int index, final int numberOfColumns) { + // Validate + if (index < 0 || index > getColumnCount()) { + throw new IndexOutOfBoundsException("The given index(" + index + + ") was outside of the current number of columns (0.." + + getColumnCount() + ")"); + } + + if (numberOfColumns < 1) { + throw new IllegalArgumentException( + "Number of columns must be 1 or greater (was " + + numberOfColumns); + } + + // Add to bookkeeping + flyweightRow.addCells(index, numberOfColumns); + for (int i = 0; i < numberOfColumns; i++) { + columns.add(index, new Column()); + } + + // Adjust frozen columns + boolean frozen = index < frozenColumns; + if (frozen) { + frozenColumns += numberOfColumns; + } + + // this needs to be before the scrollbar adjustment. + boolean scrollbarWasNeeded = horizontalScrollbar.getOffsetSize() < horizontalScrollbar + .getScrollSize(); + scroller.recalculateScrollbarsForVirtualViewport(); + boolean scrollbarIsNowNeeded = horizontalScrollbar.getOffsetSize() < horizontalScrollbar + .getScrollSize(); + if (!scrollbarWasNeeded && scrollbarIsNowNeeded) { + body.verifyEscalatorCount(); + } + + // Add to DOM + header.paintInsertColumns(index, numberOfColumns, frozen); + body.paintInsertColumns(index, numberOfColumns, frozen); + footer.paintInsertColumns(index, numberOfColumns, frozen); + + // fix autowidth + if (header.getRowCount() > 0 || body.getRowCount() > 0 + || footer.getRowCount() > 0) { + for (int col = index; col < index + numberOfColumns; col++) { + getColumnConfiguration().setColumnWidth(col, -1); + columnConfiguration.columns.get(col).widthIsFinalized(); + } + } + + // Adjust scrollbar + double pixelsToInsertedColumn = columnConfiguration + .getCalculatedColumnsWidth(Range.withLength(0, index)); + final boolean columnsWereAddedToTheLeftOfViewport = scroller.lastScrollLeft > pixelsToInsertedColumn; + + if (columnsWereAddedToTheLeftOfViewport) { + double insertedColumnsWidth = columnConfiguration + .getCalculatedColumnsWidth(Range.withLength(index, + numberOfColumns)); + horizontalScrollbar.setScrollPos(scroller.lastScrollLeft + + insertedColumnsWidth); + } + + /* + * Colspans make any kind of automatic clever content re-rendering + * impossible: As soon as anything has colspans, adding one might + * affect surrounding colspans, modifying the DOM structure once + * again, ending in a cascade of updates. Because we don't know how + * the data is updated. + * + * So, instead, we don't do anything. The client code is responsible + * for re-rendering the content (if so desired). Everything Just + * Works (TM) if colspans aren't used. + */ + } + + @Override + public int getColumnCount() { + return columns.size(); + } + + @Override + public void setFrozenColumnCount(int count) + throws IllegalArgumentException { + if (count < 0 || count > getColumnCount()) { + throw new IllegalArgumentException( + "count must be between 0 and the current number of columns (" + + getColumnCount() + ")"); + } + int oldCount = frozenColumns; + if (count == oldCount) { + return; + } + + frozenColumns = count; + + if (hasSomethingInDom()) { + // Are we freezing or unfreezing? + boolean frozen = count > oldCount; + + int firstAffectedCol; + int firstUnaffectedCol; + + if (frozen) { + firstAffectedCol = oldCount; + firstUnaffectedCol = count; + } else { + firstAffectedCol = count; + firstUnaffectedCol = oldCount; + } + + for (int col = firstAffectedCol; col < firstUnaffectedCol; col++) { + header.setColumnFrozen(col, frozen); + body.setColumnFrozen(col, frozen); + footer.setColumnFrozen(col, frozen); + } + } + + scroller.recalculateScrollbarsForVirtualViewport(); + } + + @Override + public int getFrozenColumnCount() { + return frozenColumns; + } + + @Override + public void setColumnWidth(int index, double px) + throws IllegalArgumentException { + checkValidColumnIndex(index); + + columns.get(index).setWidth(px); + columns.get(index).widthIsFinalized(); + widthsArray = null; + + /* + * TODO [[optimize]]: only modify the elements that are actually + * modified. + */ + header.reapplyColumnWidths(); + body.reapplyColumnWidths(); + footer.reapplyColumnWidths(); + recalculateElementSizes(); + } + + private void checkValidColumnIndex(int index) + throws IllegalArgumentException { + if (!Range.withLength(0, getColumnCount()).contains(index)) { + throw new IllegalArgumentException("The given column index (" + + index + ") does not exist"); + } + } + + @Override + public double getColumnWidth(int index) throws IllegalArgumentException { + checkValidColumnIndex(index); + return columns.get(index).getDefinedWidth(); + } + + @Override + public double getColumnWidthActual(int index) { + return columns.get(index).getCalculatedWidth(); + } + + private double getMaxCellWidth(int colIndex) + throws IllegalArgumentException { + double headerWidth = header.getMaxCellWidth(colIndex); + double bodyWidth = body.getMaxCellWidth(colIndex); + double footerWidth = footer.getMaxCellWidth(colIndex); + + double maxWidth = Math.max(headerWidth, + Math.max(bodyWidth, footerWidth)); + assert maxWidth >= 0 : "Got a negative max width for a column, which should be impossible."; + return maxWidth; + } + + /** + * Calculates the width of the columns in a given range. + * + * @param columns + * the columns to calculate + * @return the total width of the columns in the given + * <code>columns</code> + */ + double getCalculatedColumnsWidth(final Range columns) { + /* + * This is an assert instead of an exception, since this is an + * internal method. + */ + assert columns.isSubsetOf(Range.between(0, getColumnCount())) : "Range " + + "was outside of current column range (i.e.: " + + Range.between(0, getColumnCount()) + + ", but was given :" + + columns; + + double sum = 0; + for (int i = columns.getStart(); i < columns.getEnd(); i++) { + double columnWidthActual = getColumnWidthActual(i); + sum += columnWidthActual; + } + return sum; + } + + double[] getCalculatedColumnWidths() { + if (widthsArray == null || widthsArray.length != getColumnCount()) { + widthsArray = new double[getColumnCount()]; + for (int i = 0; i < columns.size(); i++) { + widthsArray[i] = columns.get(i).getCalculatedWidth(); + } + } + return widthsArray; + } + + @Override + public void refreshColumns(int index, int numberOfColumns) + throws IndexOutOfBoundsException, IllegalArgumentException { + if (numberOfColumns < 1) { + throw new IllegalArgumentException( + "Number of columns must be 1 or greater (was " + + numberOfColumns + ")"); + } + + if (index < 0 || index + numberOfColumns > getColumnCount()) { + throw new IndexOutOfBoundsException("The given " + + "column range (" + index + ".." + + (index + numberOfColumns) + + ") was outside of the current number of columns (" + + getColumnCount() + ")"); + } + + header.refreshColumns(index, numberOfColumns); + body.refreshColumns(index, numberOfColumns); + footer.refreshColumns(index, numberOfColumns); + } + } + + // abs(atan(y/x))*(180/PI) = n deg, x = 1, solve y + /** + * The solution to + * <code>|tan<sup>-1</sup>(<i>x</i>)|×(180/π) = 30</code> + * . + * <p> + * This constant is placed in the Escalator class, instead of an inner + * class, since even mathematical expressions aren't allowed in non-static + * inner classes for constants. + */ + private static final double RATIO_OF_30_DEGREES = 1 / Math.sqrt(3); + /** + * The solution to + * <code>|tan<sup>-1</sup>(<i>x</i>)|×(180/π) = 40</code> + * . + * <p> + * This constant is placed in the Escalator class, instead of an inner + * class, since even mathematical expressions aren't allowed in non-static + * inner classes for constants. + */ + private static final double RATIO_OF_40_DEGREES = Math.tan(2 * Math.PI / 9); + + private static final String DEFAULT_WIDTH = "500.0px"; + private static final String DEFAULT_HEIGHT = "400.0px"; + + private FlyweightRow flyweightRow = new FlyweightRow(); + + /** The {@code <thead/>} tag. */ + private final TableSectionElement headElem = TableSectionElement.as(DOM + .createTHead()); + /** The {@code <tbody/>} tag. */ + private final TableSectionElement bodyElem = TableSectionElement.as(DOM + .createTBody()); + /** The {@code <tfoot/>} tag. */ + private final TableSectionElement footElem = TableSectionElement.as(DOM + .createTFoot()); + + /** + * TODO: investigate whether this field is now unnecessary, as + * {@link ScrollbarBundle} now caches its values. + * + * @deprecated maybe... + */ + @Deprecated + private double tBodyScrollTop = 0; + + /** + * TODO: investigate whether this field is now unnecessary, as + * {@link ScrollbarBundle} now caches its values. + * + * @deprecated maybe... + */ + @Deprecated + private double tBodyScrollLeft = 0; + + private final VerticalScrollbarBundle verticalScrollbar = new VerticalScrollbarBundle(); + private final HorizontalScrollbarBundle horizontalScrollbar = new HorizontalScrollbarBundle(); + + private final HeaderRowContainer header = new HeaderRowContainer(headElem); + private final BodyRowContainer body = new BodyRowContainer(bodyElem); + private final FooterRowContainer footer = new FooterRowContainer(footElem); + + private final Scroller scroller = new Scroller(); + + private final ColumnConfigurationImpl columnConfiguration = new ColumnConfigurationImpl(); + private final DivElement tableWrapper; + + private final DivElement horizontalScrollbarDeco = DivElement.as(DOM + .createDiv()); + private final DivElement headerDeco = DivElement.as(DOM.createDiv()); + private final DivElement footerDeco = DivElement.as(DOM.createDiv()); + + private PositionFunction position; + + /** The cached width of the escalator, in pixels. */ + private double widthOfEscalator = 0; + /** The cached height of the escalator, in pixels. */ + private double heightOfEscalator = 0; + + /** The height of Escalator in terms of body rows. */ + private double heightByRows = 10.0d; + + /** The height of Escalator, as defined by {@link #setHeight(String)} */ + private String heightByCss = ""; + + private HeightMode heightMode = HeightMode.CSS; + + private boolean layoutIsScheduled = false; + private ScheduledCommand layoutCommand = new ScheduledCommand() { + @Override + public void execute() { + recalculateElementSizes(); + layoutIsScheduled = false; + } + }; + + private final ColumnAutoWidthAssignScheduler columnAutoWidthAssignScheduler = new ColumnAutoWidthAssignScheduler(); + + /** + * Creates a new Escalator widget instance. + */ + public Escalator() { + + detectAndApplyPositionFunction(); + getLogger().info( + "Using " + position.getClass().getSimpleName() + + " for position"); + + final Element root = DOM.createDiv(); + setElement(root); + + ScrollHandler scrollHandler = new ScrollHandler() { + @Override + public void onScroll(ScrollEvent event) { + scroller.onScroll(); + fireEvent(new ScrollEvent()); + } + }; + + root.appendChild(verticalScrollbar.getElement()); + verticalScrollbar.addScrollHandler(scrollHandler); + verticalScrollbar.setScrollbarThickness(WidgetUtil + .getNativeScrollbarSize()); + + root.appendChild(horizontalScrollbar.getElement()); + horizontalScrollbar.addScrollHandler(scrollHandler); + horizontalScrollbar.setScrollbarThickness(WidgetUtil + .getNativeScrollbarSize()); + horizontalScrollbar + .addVisibilityHandler(new ScrollbarBundle.VisibilityHandler() { + @Override + public void visibilityChanged( + ScrollbarBundle.VisibilityChangeEvent event) { + /* + * We either lost or gained a scrollbar. In any case, we + * need to change the height, if it's defined by rows. + */ + applyHeightByRows(); + } + }); + + tableWrapper = DivElement.as(DOM.createDiv()); + + root.appendChild(tableWrapper); + + final Element table = DOM.createTable(); + tableWrapper.appendChild(table); + + table.appendChild(headElem); + table.appendChild(bodyElem); + table.appendChild(footElem); + + Style hCornerStyle = headerDeco.getStyle(); + hCornerStyle.setWidth(WidgetUtil.getNativeScrollbarSize(), Unit.PX); + hCornerStyle.setDisplay(Display.NONE); + root.appendChild(headerDeco); + + Style fCornerStyle = footerDeco.getStyle(); + fCornerStyle.setWidth(WidgetUtil.getNativeScrollbarSize(), Unit.PX); + fCornerStyle.setDisplay(Display.NONE); + root.appendChild(footerDeco); + + Style hWrapperStyle = horizontalScrollbarDeco.getStyle(); + hWrapperStyle.setDisplay(Display.NONE); + hWrapperStyle.setHeight(WidgetUtil.getNativeScrollbarSize(), Unit.PX); + root.appendChild(horizontalScrollbarDeco); + + setStylePrimaryName("v-escalator"); + + // init default dimensions + setHeight(null); + setWidth(null); + } + + @Override + protected void onLoad() { + super.onLoad(); + + header.autodetectRowHeightLater(); + body.autodetectRowHeightLater(); + footer.autodetectRowHeightLater(); + + header.paintInsertRows(0, header.getRowCount()); + footer.paintInsertRows(0, footer.getRowCount()); + recalculateElementSizes(); + /* + * Note: There's no need to explicitly insert rows into the body. + * + * recalculateElementSizes will recalculate the height of the body. This + * has the side-effect that as the body's size grows bigger (i.e. from 0 + * to its actual height), more escalator rows are populated. Those + * escalator rows are then immediately rendered. This, in effect, is the + * same thing as inserting those rows. + * + * In fact, having an extra paintInsertRows here would lead to duplicate + * rows. + */ + + boolean columnsChanged = false; + for (ColumnConfigurationImpl.Column column : columnConfiguration.columns) { + boolean columnChanged = column.measureAndSetWidthIfNeeded(); + if (columnChanged) { + columnsChanged = true; + } + } + if (columnsChanged) { + header.reapplyColumnWidths(); + body.reapplyColumnWidths(); + footer.reapplyColumnWidths(); + } + + scroller.attachScrollListener(verticalScrollbar.getElement()); + scroller.attachScrollListener(horizontalScrollbar.getElement()); + scroller.attachMousewheelListener(getElement()); + scroller.attachTouchListeners(getElement()); + } + + @Override + protected void onUnload() { + + scroller.detachScrollListener(verticalScrollbar.getElement()); + scroller.detachScrollListener(horizontalScrollbar.getElement()); + scroller.detachMousewheelListener(getElement()); + scroller.detachTouchListeners(getElement()); + + /* + * We can call paintRemoveRows here, because static ranges are simple to + * remove. + */ + header.paintRemoveRows(0, header.getRowCount()); + footer.paintRemoveRows(0, footer.getRowCount()); + + /* + * We can't call body.paintRemoveRows since it relies on rowCount to be + * updated correctly. Since it isn't, we'll simply and brutally rip out + * the DOM elements (in an elegant way, of course). + */ + int rowsToRemove = bodyElem.getChildCount(); + for (int i = 0; i < rowsToRemove; i++) { + int index = rowsToRemove - i - 1; + TableRowElement tr = bodyElem.getRows().getItem(index); + body.paintRemoveRow(tr, index); + body.removeRowPosition(tr); + } + body.visualRowOrder.clear(); + body.setTopRowLogicalIndex(0); + + super.onUnload(); + } + + private void detectAndApplyPositionFunction() { + /* + * firefox has a bug in its translate operation, showing white space + * when adjusting the scrollbar in BodyRowContainer.paintInsertRows + */ + if (Window.Navigator.getUserAgent().contains("Firefox")) { + position = new AbsolutePosition(); + return; + } + + final Style docStyle = Document.get().getBody().getStyle(); + if (hasProperty(docStyle, "transform")) { + if (hasProperty(docStyle, "transformStyle")) { + position = new Translate3DPosition(); + } else { + position = new TranslatePosition(); + } + } else if (hasProperty(docStyle, "webkitTransform")) { + position = new WebkitTranslate3DPosition(); + } else { + position = new AbsolutePosition(); + } + } + + private Logger getLogger() { + return Logger.getLogger(getClass().getName()); + } + + private static native boolean hasProperty(Style style, String name) + /*-{ + return style[name] !== undefined; + }-*/; + + /** + * Check whether there are both columns and any row data (for either + * headers, body or footer). + * + * @return <code>true</code> iff header, body or footer has rows && there + * are columns + */ + private boolean hasColumnAndRowData() { + return (header.getRowCount() > 0 || body.getRowCount() > 0 || footer + .getRowCount() > 0) && columnConfiguration.getColumnCount() > 0; + } + + /** + * Check whether there are any cells in the DOM. + * + * @return <code>true</code> iff header, body or footer has any child + * elements + */ + private boolean hasSomethingInDom() { + return headElem.hasChildNodes() || bodyElem.hasChildNodes() + || footElem.hasChildNodes(); + } + + /** + * Returns the row container for the header in this Escalator. + * + * @return the header. Never <code>null</code> + */ + public RowContainer getHeader() { + return header; + } + + /** + * Returns the row container for the body in this Escalator. + * + * @return the body. Never <code>null</code> + */ + public RowContainer getBody() { + return body; + } + + /** + * Returns the row container for the footer in this Escalator. + * + * @return the footer. Never <code>null</code> + */ + public RowContainer getFooter() { + return footer; + } + + /** + * Returns the configuration object for the columns in this Escalator. + * + * @return the configuration object for the columns in this Escalator. Never + * <code>null</code> + */ + public ColumnConfiguration getColumnConfiguration() { + return columnConfiguration; + } + + @Override + public void setWidth(final String width) { + if (width != null && !width.isEmpty()) { + super.setWidth(width); + } else { + super.setWidth(DEFAULT_WIDTH); + } + + recalculateElementSizes(); + } + + /** + * {@inheritDoc} + * <p> + * If Escalator is currently not in {@link HeightMode#CSS}, the given value + * is remembered, and applied once the mode is applied. + * + * @see #setHeightMode(HeightMode) + */ + @Override + public void setHeight(String height) { + /* + * TODO remove method once RequiresResize and the Vaadin layoutmanager + * listening mechanisms are implemented + */ + + if (height != null && !height.isEmpty()) { + heightByCss = height; + } else { + heightByCss = DEFAULT_HEIGHT; + } + + if (getHeightMode() == HeightMode.CSS) { + setHeightInternal(height); + } + } + + private void setHeightInternal(final String height) { + final int escalatorRowsBefore = body.visualRowOrder.size(); + + if (height != null && !height.isEmpty()) { + super.setHeight(height); + } else { + super.setHeight(DEFAULT_HEIGHT); + } + + recalculateElementSizes(); + + if (escalatorRowsBefore != body.visualRowOrder.size()) { + fireRowVisibilityChangeEvent(); + } + } + + /** + * Returns the vertical scroll offset. Note that this is not necessarily the + * same as the {@code scrollTop} attribute in the DOM. + * + * @return the logical vertical scroll offset + */ + public double getScrollTop() { + return verticalScrollbar.getScrollPos(); + } + + /** + * Sets the vertical scroll offset. Note that this will not necessarily + * become the same as the {@code scrollTop} attribute in the DOM. + * + * @param scrollTop + * the number of pixels to scroll vertically + */ + public void setScrollTop(final double scrollTop) { + verticalScrollbar.setScrollPos(scrollTop); + } + + /** + * Returns the logical horizontal scroll offset. Note that this is not + * necessarily the same as the {@code scrollLeft} attribute in the DOM. + * + * @return the logical horizontal scroll offset + */ + public double getScrollLeft() { + return horizontalScrollbar.getScrollPos(); + } + + /** + * Sets the logical horizontal scroll offset. Note that will not necessarily + * become the same as the {@code scrollLeft} attribute in the DOM. + * + * @param scrollLeft + * the number of pixels to scroll horizontally + */ + public void setScrollLeft(final double scrollLeft) { + horizontalScrollbar.setScrollPos(scrollLeft); + } + + /** + * Scrolls the body horizontally so that the column at the given index is + * visible and there is at least {@code padding} pixels in the direction of + * the given scroll destination. + * + * @param columnIndex + * the index of the column to scroll to + * @param destination + * where the column should be aligned visually after scrolling + * @param padding + * the number pixels to place between the scrolled-to column and + * the viewport edge. + * @throws IndexOutOfBoundsException + * if {@code columnIndex} is not a valid index for an existing + * column + * @throws IllegalArgumentException + * if {@code destination} is {@link ScrollDestination#MIDDLE} + * and padding is nonzero, or if the indicated column is frozen + */ + public void scrollToColumn(final int columnIndex, + final ScrollDestination destination, final int padding) + throws IndexOutOfBoundsException, IllegalArgumentException { + if (destination == ScrollDestination.MIDDLE && padding != 0) { + throw new IllegalArgumentException( + "You cannot have a padding with a MIDDLE destination"); + } + verifyValidColumnIndex(columnIndex); + + if (columnIndex < columnConfiguration.frozenColumns) { + throw new IllegalArgumentException("The given column index " + + columnIndex + " is frozen."); + } + + scroller.scrollToColumn(columnIndex, destination, padding); + } + + private void verifyValidColumnIndex(final int columnIndex) + throws IndexOutOfBoundsException { + if (columnIndex < 0 + || columnIndex >= columnConfiguration.getColumnCount()) { + throw new IndexOutOfBoundsException("The given column index " + + columnIndex + " does not exist."); + } + } + + /** + * Scrolls the body vertically so that the row at the given index is visible + * and there is at least {@literal padding} pixels to the given scroll + * destination. + * + * @param rowIndex + * the index of the logical row to scroll to + * @param destination + * where the row should be aligned visually after scrolling + * @param padding + * the number pixels to place between the scrolled-to row and the + * viewport edge. + * @throws IndexOutOfBoundsException + * if {@code rowIndex} is not a valid index for an existing row + * @throws IllegalArgumentException + * if {@code destination} is {@link ScrollDestination#MIDDLE} + * and padding is nonzero + */ + public void scrollToRow(final int rowIndex, + final ScrollDestination destination, final int padding) + throws IndexOutOfBoundsException, IllegalArgumentException { + if (destination == ScrollDestination.MIDDLE && padding != 0) { + throw new IllegalArgumentException( + "You cannot have a padding with a MIDDLE destination"); + } + verifyValidRowIndex(rowIndex); + + scroller.scrollToRow(rowIndex, destination, padding); + } + + private void verifyValidRowIndex(final int rowIndex) { + if (rowIndex < 0 || rowIndex >= body.getRowCount()) { + throw new IndexOutOfBoundsException("The given row index " + + rowIndex + " does not exist."); + } + } + + /** + * Recalculates the dimensions for all elements that require manual + * calculations. Also updates the dimension caches. + * <p> + * <em>Note:</em> This method has the <strong>side-effect</strong> + * automatically makes sure that an appropriate amount of escalator rows are + * present. So, if the body area grows, more <strong>escalator rows might be + * inserted</strong>. Conversely, if the body area shrinks, + * <strong>escalator rows might be removed</strong>. + */ + private void recalculateElementSizes() { + if (!isAttached()) { + return; + } + + Profiler.enter("Escalator.recalculateElementSizes"); + widthOfEscalator = Math.max(0, WidgetUtil + .getRequiredWidthBoundingClientRectDouble(getElement())); + heightOfEscalator = Math.max(0, WidgetUtil + .getRequiredHeightBoundingClientRectDouble(getElement())); + + header.recalculateSectionHeight(); + body.recalculateSectionHeight(); + footer.recalculateSectionHeight(); + + scroller.recalculateScrollbarsForVirtualViewport(); + body.verifyEscalatorCount(); + Profiler.leave("Escalator.recalculateElementSizes"); + } + + /** + * Snap deltas of x and y to the major four axes (up, down, left, right) + * with a threshold of a number of degrees from those axes. + * + * @param deltaX + * the delta in the x axis + * @param deltaY + * the delta in the y axis + * @param thresholdRatio + * the threshold in ratio (0..1) between x and y for when to snap + * @return a two-element array: <code>[snappedX, snappedY]</code> + */ + private static double[] snapDeltas(final double deltaX, + final double deltaY, final double thresholdRatio) { + + final double[] array = new double[2]; + if (deltaX != 0 && deltaY != 0) { + final double aDeltaX = Math.abs(deltaX); + final double aDeltaY = Math.abs(deltaY); + final double yRatio = aDeltaY / aDeltaX; + final double xRatio = aDeltaX / aDeltaY; + + array[0] = (xRatio < thresholdRatio) ? 0 : deltaX; + array[1] = (yRatio < thresholdRatio) ? 0 : deltaY; + } else { + array[0] = deltaX; + array[1] = deltaY; + } + + return array; + } + + /** + * Adds an event handler that gets notified when the range of visible rows + * changes e.g. because of scrolling or row resizing. + * + * @param rowVisibilityChangeHandler + * the event handler + * @return a handler registration for the added handler + */ + public HandlerRegistration addRowVisibilityChangeHandler( + RowVisibilityChangeHandler rowVisibilityChangeHandler) { + return addHandler(rowVisibilityChangeHandler, + RowVisibilityChangeEvent.TYPE); + } + + private void fireRowVisibilityChangeEvent() { + if (!body.visualRowOrder.isEmpty()) { + int visibleRangeStart = body.getLogicalRowIndex(body.visualRowOrder + .getFirst()); + int visibleRangeEnd = body.getLogicalRowIndex(body.visualRowOrder + .getLast()) + 1; + + int visibleRowCount = visibleRangeEnd - visibleRangeStart; + fireEvent(new RowVisibilityChangeEvent(visibleRangeStart, + visibleRowCount)); + } else { + fireEvent(new RowVisibilityChangeEvent(0, 0)); + } + } + + /** + * Gets the range of currently visible rows. + * + * @return range of visible rows + */ + public Range getVisibleRowRange() { + if (!body.visualRowOrder.isEmpty()) { + return Range.withLength( + body.getLogicalRowIndex(body.visualRowOrder.getFirst()), + body.visualRowOrder.size()); + } else { + return Range.withLength(0, 0); + } + } + + /** + * Returns the widget from a cell node or <code>null</code> if there is no + * widget in the cell + * + * @param cellNode + * The cell node + */ + static Widget getWidgetFromCell(Node cellNode) { + Node possibleWidgetNode = cellNode.getFirstChild(); + if (possibleWidgetNode != null + && possibleWidgetNode.getNodeType() == Node.ELEMENT_NODE) { + @SuppressWarnings("deprecation") + com.google.gwt.user.client.Element castElement = (com.google.gwt.user.client.Element) possibleWidgetNode + .cast(); + Widget w = WidgetUtil.findWidget(castElement, null); + + // Ensure findWidget did not traverse past the cell element in the + // DOM hierarchy + if (cellNode.isOrHasChild(w.getElement())) { + return w; + } + } + return null; + } + + @Override + public void setStylePrimaryName(String style) { + super.setStylePrimaryName(style); + + verticalScrollbar.setStylePrimaryName(style); + horizontalScrollbar.setStylePrimaryName(style); + + UIObject.setStylePrimaryName(tableWrapper, style + "-tablewrapper"); + UIObject.setStylePrimaryName(headerDeco, style + "-header-deco"); + UIObject.setStylePrimaryName(footerDeco, style + "-footer-deco"); + UIObject.setStylePrimaryName(horizontalScrollbarDeco, style + + "-horizontal-scrollbar-deco"); + + header.setStylePrimaryName(style); + body.setStylePrimaryName(style); + footer.setStylePrimaryName(style); + } + + /** + * Sets the number of rows that should be visible in Escalator's body, while + * {@link #getHeightMode()} is {@link HeightMode#ROW}. + * <p> + * If Escalator is currently not in {@link HeightMode#ROW}, the given value + * is remembered, and applied once the mode is applied. + * + * @param rows + * the number of rows that should be visible in Escalator's body + * @throws IllegalArgumentException + * if {@code rows} is ≤ 0, + * {@link Double#isInifinite(double) infinite} or + * {@link Double#isNaN(double) NaN}. + * @see #setHeightMode(HeightMode) + */ + public void setHeightByRows(double rows) throws IllegalArgumentException { + if (rows <= 0) { + throw new IllegalArgumentException( + "The number of rows must be a positive number."); + } else if (Double.isInfinite(rows)) { + throw new IllegalArgumentException( + "The number of rows must be finite."); + } else if (Double.isNaN(rows)) { + throw new IllegalArgumentException("The number must not be NaN."); + } + + heightByRows = rows; + applyHeightByRows(); + } + + /** + * Gets the amount of rows in Escalator's body that are shown, while + * {@link #getHeightMode()} is {@link HeightMode#ROW}. + * <p> + * By default, it is 10. + * + * @return the amount of rows that are being shown in Escalator's body + * @see #setHeightByRows(double) + */ + public double getHeightByRows() { + return heightByRows; + } + + /** + * Reapplies the row-based height of the Grid, if Grid currently should + * define its height that way. + */ + private void applyHeightByRows() { + if (heightMode != HeightMode.ROW) { + return; + } + + double headerHeight = header.heightOfSection; + double footerHeight = footer.heightOfSection; + double bodyHeight = body.getDefaultRowHeight() * heightByRows; + double scrollbar = horizontalScrollbar.showsScrollHandle() ? horizontalScrollbar + .getScrollbarThickness() : 0; + + double totalHeight = headerHeight + bodyHeight + scrollbar + + footerHeight; + setHeightInternal(totalHeight + "px"); + } + + /** + * Defines the mode in which the Escalator widget's height is calculated. + * <p> + * If {@link HeightMode#CSS} is given, Escalator will respect the values + * given via {@link #setHeight(String)}, and behave as a traditional Widget. + * <p> + * If {@link HeightMode#ROW} is given, Escalator will make sure that the + * {@link #getBody() body} will display as many rows as + * {@link #getHeightByRows()} defines. <em>Note:</em> If headers/footers are + * inserted or removed, the widget will resize itself to still display the + * required amount of rows in its body. It also takes the horizontal + * scrollbar into account. + * + * @param heightMode + * the mode in to which Escalator should be set + */ + public void setHeightMode(HeightMode heightMode) { + /* + * This method is a workaround for the fact that Vaadin re-applies + * widget dimensions (height/width) on each state change event. The + * original design was to have setHeight an setHeightByRow be equals, + * and whichever was called the latest was considered in effect. + * + * But, because of Vaadin always calling setHeight on the widget, this + * approach doesn't work. + */ + + if (heightMode != this.heightMode) { + this.heightMode = heightMode; + + switch (this.heightMode) { + case CSS: + setHeight(heightByCss); + break; + case ROW: + setHeightByRows(heightByRows); + break; + default: + throw new IllegalStateException("Unimplemented feature " + + "- unknown HeightMode: " + this.heightMode); + } + } + } + + /** + * Returns the current {@link HeightMode} the Escalator is in. + * <p> + * Defaults to {@link HeightMode#CSS}. + * + * @return the current HeightMode + */ + public HeightMode getHeightMode() { + return heightMode; + } + + /** + * Returns the {@link RowContainer} which contains the element. + * + * @param element + * the element to check for + * @return the container the element is in or <code>null</code> if element + * is not present in any container. + */ + public RowContainer findRowContainer(Element element) { + if (getHeader().getElement() != element + && getHeader().getElement().isOrHasChild(element)) { + return getHeader(); + } else if (getBody().getElement() != element + && getBody().getElement().isOrHasChild(element)) { + return getBody(); + } else if (getFooter().getElement() != element + && getFooter().getElement().isOrHasChild(element)) { + return getFooter(); + } + return null; + } + + /** + * Sets whether a scroll direction is locked or not. + * <p> + * If a direction is locked, the escalator will refuse to scroll in that + * direction. + * + * @param direction + * the orientation of the scroll to set the lock status + * @param locked + * <code>true</code> to lock, <code>false</code> to unlock + */ + public void setScrollLocked(ScrollbarBundle.Direction direction, + boolean locked) { + switch (direction) { + case HORIZONTAL: + horizontalScrollbar.setLocked(locked); + break; + case VERTICAL: + verticalScrollbar.setLocked(locked); + break; + default: + throw new UnsupportedOperationException("Unexpected value: " + + direction); + } + } + + /** + * Checks whether or not an direction is locked for scrolling. + * + * @param direction + * the direction of the scroll of which to check the lock status + * @return <code>true</code> iff the direction is locked + */ + public boolean isScrollLocked(ScrollbarBundle.Direction direction) { + switch (direction) { + case HORIZONTAL: + return horizontalScrollbar.isLocked(); + case VERTICAL: + return verticalScrollbar.isLocked(); + default: + throw new UnsupportedOperationException("Unexpected value: " + + direction); + } + } + + /** + * Adds a scroll handler to this escalator + * + * @param handler + * the scroll handler to add + * @return a handler registration for the registered scroll handler + */ + public HandlerRegistration addScrollHandler(ScrollHandler handler) { + return addHandler(handler, ScrollEvent.TYPE); + } + + @Override + public boolean isWorkPending() { + return body.domSorter.waiting + || columnAutoWidthAssignScheduler.isScheduled + || verticalScrollbar.isWorkPending() + || horizontalScrollbar.isWorkPending(); + } + + @Override + public void onResize() { + if (isAttached() && !layoutIsScheduled) { + layoutIsScheduled = true; + Scheduler.get().scheduleDeferred(layoutCommand); + } + } + + /** + * Gets the maximum number of body rows that can be visible on the screen at + * once. + * + * @return the maximum capacity + */ + public int getMaxVisibleRowCount() { + return body.getMaxEscalatorRowCapacity(); + } + + /** + * Gets the escalator's inner width. This is the entire width in pixels, + * without the vertical scrollbar. + * + * @return escalator's inner width + */ + public double getInnerWidth() { + return WidgetUtil + .getRequiredWidthBoundingClientRectDouble(tableWrapper); + } + + /** + * Resets all cached pixel sizes and reads new values from the DOM. This + * methods should be used e.g. when styles affecting the dimensions of + * elements in this escalator have been changed. + */ + public void resetSizesFromDom() { + header.autodetectRowHeightNow(); + body.autodetectRowHeightNow(); + footer.autodetectRowHeightNow(); + + for (int i = 0; i < columnConfiguration.getColumnCount(); i++) { + columnConfiguration.setColumnWidth(i, + columnConfiguration.getColumnWidth(i)); + } + } +} diff --git a/client/src/com/vaadin/client/widgets/Grid.java b/client/src/com/vaadin/client/widgets/Grid.java new file mode 100644 index 0000000000..d6dfdee3b3 --- /dev/null +++ b/client/src/com/vaadin/client/widgets/Grid.java @@ -0,0 +1,5867 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.widgets; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.google.gwt.core.client.Scheduler; +import com.google.gwt.core.client.Scheduler.ScheduledCommand; +import com.google.gwt.core.shared.GWT; +import com.google.gwt.dom.client.BrowserEvents; +import com.google.gwt.dom.client.DivElement; +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.EventTarget; +import com.google.gwt.dom.client.Style; +import com.google.gwt.dom.client.Style.Unit; +import com.google.gwt.dom.client.TableCellElement; +import com.google.gwt.dom.client.TableRowElement; +import com.google.gwt.dom.client.Touch; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.event.dom.client.KeyCodes; +import com.google.gwt.event.dom.client.KeyEvent; +import com.google.gwt.event.dom.client.MouseEvent; +import com.google.gwt.event.logical.shared.ValueChangeEvent; +import com.google.gwt.event.logical.shared.ValueChangeHandler; +import com.google.gwt.event.shared.HandlerRegistration; +import com.google.gwt.touch.client.Point; +import com.google.gwt.user.client.DOM; +import com.google.gwt.user.client.Event; +import com.google.gwt.user.client.Timer; +import com.google.gwt.user.client.ui.Button; +import com.google.gwt.user.client.ui.CheckBox; +import com.google.gwt.user.client.ui.HasEnabled; +import com.google.gwt.user.client.ui.HasWidgets; +import com.google.gwt.user.client.ui.ResizeComposite; +import com.google.gwt.user.client.ui.Widget; +import com.vaadin.client.DeferredWorker; +import com.vaadin.client.WidgetUtil; +import com.vaadin.client.data.DataChangeHandler; +import com.vaadin.client.data.DataSource; +import com.vaadin.client.renderers.ComplexRenderer; +import com.vaadin.client.renderers.ObjectRenderer; +import com.vaadin.client.renderers.Renderer; +import com.vaadin.client.renderers.WidgetRenderer; +import com.vaadin.client.ui.SubPartAware; +import com.vaadin.client.widget.escalator.Cell; +import com.vaadin.client.widget.escalator.ColumnConfiguration; +import com.vaadin.client.widget.escalator.EscalatorUpdater; +import com.vaadin.client.widget.escalator.FlyweightCell; +import com.vaadin.client.widget.escalator.Row; +import com.vaadin.client.widget.escalator.RowContainer; +import com.vaadin.client.widget.escalator.RowVisibilityChangeEvent; +import com.vaadin.client.widget.escalator.RowVisibilityChangeHandler; +import com.vaadin.client.widget.escalator.ScrollbarBundle.Direction; +import com.vaadin.client.widget.grid.CellReference; +import com.vaadin.client.widget.grid.CellStyleGenerator; +import com.vaadin.client.widget.grid.DataAvailableEvent; +import com.vaadin.client.widget.grid.DataAvailableHandler; +import com.vaadin.client.widget.grid.EditorHandler; +import com.vaadin.client.widget.grid.EditorHandler.EditorRequest; +import com.vaadin.client.widget.grid.EditorHandler.EditorRequest.RequestCallback; +import com.vaadin.client.widget.grid.EventCellReference; +import com.vaadin.client.widget.grid.RendererCellReference; +import com.vaadin.client.widget.grid.RowReference; +import com.vaadin.client.widget.grid.RowStyleGenerator; +import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler; +import com.vaadin.client.widget.grid.events.AbstractGridMouseEventHandler; +import com.vaadin.client.widget.grid.events.BodyClickHandler; +import com.vaadin.client.widget.grid.events.BodyDoubleClickHandler; +import com.vaadin.client.widget.grid.events.BodyKeyDownHandler; +import com.vaadin.client.widget.grid.events.BodyKeyPressHandler; +import com.vaadin.client.widget.grid.events.BodyKeyUpHandler; +import com.vaadin.client.widget.grid.events.FooterClickHandler; +import com.vaadin.client.widget.grid.events.FooterDoubleClickHandler; +import com.vaadin.client.widget.grid.events.FooterKeyDownHandler; +import com.vaadin.client.widget.grid.events.FooterKeyPressHandler; +import com.vaadin.client.widget.grid.events.FooterKeyUpHandler; +import com.vaadin.client.widget.grid.events.GridClickEvent; +import com.vaadin.client.widget.grid.events.GridDoubleClickEvent; +import com.vaadin.client.widget.grid.events.GridKeyDownEvent; +import com.vaadin.client.widget.grid.events.GridKeyPressEvent; +import com.vaadin.client.widget.grid.events.GridKeyUpEvent; +import com.vaadin.client.widget.grid.events.HeaderClickHandler; +import com.vaadin.client.widget.grid.events.HeaderDoubleClickHandler; +import com.vaadin.client.widget.grid.events.HeaderKeyDownHandler; +import com.vaadin.client.widget.grid.events.HeaderKeyPressHandler; +import com.vaadin.client.widget.grid.events.HeaderKeyUpHandler; +import com.vaadin.client.widget.grid.events.ScrollEvent; +import com.vaadin.client.widget.grid.events.ScrollHandler; +import com.vaadin.client.widget.grid.events.SelectAllEvent; +import com.vaadin.client.widget.grid.events.SelectAllHandler; +import com.vaadin.client.widget.grid.selection.HasSelectionHandlers; +import com.vaadin.client.widget.grid.selection.SelectionEvent; +import com.vaadin.client.widget.grid.selection.SelectionHandler; +import com.vaadin.client.widget.grid.selection.SelectionModel; +import com.vaadin.client.widget.grid.selection.SelectionModel.Multi; +import com.vaadin.client.widget.grid.selection.SelectionModelMulti; +import com.vaadin.client.widget.grid.selection.SelectionModelNone; +import com.vaadin.client.widget.grid.selection.SelectionModelSingle; +import com.vaadin.client.widget.grid.sort.Sort; +import com.vaadin.client.widget.grid.sort.SortEvent; +import com.vaadin.client.widget.grid.sort.SortHandler; +import com.vaadin.client.widget.grid.sort.SortOrder; +import com.vaadin.client.widgets.Escalator.AbstractRowContainer; +import com.vaadin.client.widgets.Grid.Editor.State; +import com.vaadin.shared.data.sort.SortDirection; +import com.vaadin.shared.ui.grid.GridConstants; +import com.vaadin.shared.ui.grid.GridStaticCellType; +import com.vaadin.shared.ui.grid.HeightMode; +import com.vaadin.shared.ui.grid.Range; +import com.vaadin.shared.ui.grid.ScrollDestination; +import com.vaadin.shared.util.SharedUtil; + +/** + * A data grid view that supports columns and lazy loading of data rows from a + * data source. + * + * <h1>Columns</h1> + * <p> + * Each column in Grid is represented by a {@link Column}. Each + * {@code GridColumn} has a custom implementation for + * {@link Column#getValue(Object)} that gets the row object as an argument, and + * returns the value for that particular column, extracted from the row object. + * <p> + * Each column also has a Renderer. Its function is to take the value that is + * given by the {@code GridColumn} and display it to the user. A simple column + * might have a {@link com.vaadin.client.renderers.TextRenderer TextRenderer} + * that simply takes in a {@code String} and displays it as the cell's content. + * A more complex renderer might be + * {@link com.vaadin.client.renderers.ProgressBarRenderer ProgressBarRenderer} + * that takes in a floating point number, and displays a progress bar instead, + * based on the given number. + * <p> + * <em>See:</em> {@link #addColumn(Column)}, {@link #addColumn(Column, int)} and + * {@link #addColumns(Column...)}. <em>Also</em> + * {@link Column#setRenderer(Renderer)}. + * + * <h1>Data Sources</h1> + * <p> + * Grid gets its data from a {@link DataSource}, providing row objects to Grid + * from a user-defined endpoint. It can be either a local in-memory data source + * (e.g. {@link com.vaadin.client.widget.grid.datasources.ListDataSource + * ListDataSource}) or even a remote one, retrieving data from e.g. a REST API + * (see {@link com.vaadin.client.data.AbstractRemoteDataSource + * AbstractRemoteDataSource}). + * + * + * @param <T> + * The row type of the grid. The row type is the POJO type from where + * the data is retrieved into the column cells. + * @since 7.4 + * @author Vaadin Ltd + */ +public class Grid<T> extends ResizeComposite implements + HasSelectionHandlers<T>, SubPartAware, DeferredWorker, HasWidgets, + HasEnabled { + + /** + * Enum describing different sections of Grid. + */ + public enum Section { + HEADER, BODY, FOOTER + } + + /** + * Abstract base class for Grid header and footer sections. + * + * @param <ROWTYPE> + * the type of the rows in the section + */ + protected abstract static class StaticSection<ROWTYPE extends StaticSection.StaticRow<?>> { + + /** + * A header or footer cell. Has a simple textual caption. + * + */ + static class StaticCell { + + private Object content = null; + + private int colspan = 1; + + private StaticSection<?> section; + + private GridStaticCellType type = GridStaticCellType.TEXT; + + private String styleName = null; + + /** + * Sets the text displayed in this cell. + * + * @param text + * a plain text caption + */ + public void setText(String text) { + this.content = text; + this.type = GridStaticCellType.TEXT; + section.requestSectionRefresh(); + } + + /** + * Returns the text displayed in this cell. + * + * @return the plain text caption + */ + public String getText() { + if (type != GridStaticCellType.TEXT) { + throw new IllegalStateException( + "Cannot fetch Text from a cell with type " + type); + } + return (String) content; + } + + protected StaticSection<?> getSection() { + assert section != null; + return section; + } + + protected void setSection(StaticSection<?> section) { + this.section = section; + } + + /** + * Returns the amount of columns the cell spans. By default is 1. + * + * @return The amount of columns the cell spans. + */ + public int getColspan() { + return colspan; + } + + /** + * Sets the amount of columns the cell spans. Must be more or equal + * to 1. By default is 1. + * + * @param colspan + * the colspan to set + */ + public void setColspan(int colspan) { + if (colspan < 1) { + throw new IllegalArgumentException( + "Colspan cannot be less than 1"); + } + + this.colspan = colspan; + section.requestSectionRefresh(); + } + + /** + * Returns the html inside the cell. + * + * @throws IllegalStateException + * if trying to retrive HTML from a cell with a type + * other than {@link GridStaticCellType#HTML}. + * @return the html content of the cell. + */ + public String getHtml() { + if (type != GridStaticCellType.HTML) { + throw new IllegalStateException( + "Cannot fetch HTML from a cell with type " + type); + } + return (String) content; + } + + /** + * Sets the content of the cell to the provided html. All previous + * content is discarded and the cell type is set to + * {@link GridStaticCellType#HTML}. + * + * @param html + * The html content of the cell + */ + public void setHtml(String html) { + this.content = html; + this.type = GridStaticCellType.HTML; + section.requestSectionRefresh(); + } + + /** + * Returns the widget in the cell. + * + * @throws IllegalStateException + * if the cell is not {@link GridStaticCellType#WIDGET} + * + * @return the widget in the cell + */ + public Widget getWidget() { + if (type != GridStaticCellType.WIDGET) { + throw new IllegalStateException( + "Cannot fetch Widget from a cell with type " + type); + } + return (Widget) content; + } + + /** + * Set widget as the content of the cell. The type of the cell + * becomes {@link GridStaticCellType#WIDGET}. All previous content + * is discarded. + * + * @param widget + * The widget to add to the cell. Should not be + * previously attached anywhere (widget.getParent == + * null). + */ + public void setWidget(Widget widget) { + this.content = widget; + this.type = GridStaticCellType.WIDGET; + section.requestSectionRefresh(); + } + + /** + * Returns the type of the cell. + * + * @return the type of content the cell contains. + */ + public GridStaticCellType getType() { + return type; + } + + /** + * Returns the custom style name for this cell. + * + * @return the style name or null if no style name has been set + */ + public String getStyleName() { + return styleName; + } + + /** + * Sets a custom style name for this cell. + * + * @param styleName + * the style name to set or null to not use any style + * name + */ + public void setStyleName(String styleName) { + this.styleName = styleName; + section.requestSectionRefresh(); + + } + + } + + /** + * Abstract base class for Grid header and footer rows. + * + * @param <CELLTYPE> + * the type of the cells in the row + */ + abstract static class StaticRow<CELLTYPE extends StaticCell> { + + private Map<Column<?, ?>, CELLTYPE> cells = new HashMap<Column<?, ?>, CELLTYPE>(); + + private StaticSection<?> section; + + /** + * Map from set of spanned columns to cell meta data. + */ + private Map<Set<Column<?, ?>>, CELLTYPE> cellGroups = new HashMap<Set<Column<?, ?>>, CELLTYPE>(); + + /** + * A custom style name for the row or null if none is set. + */ + private String styleName = null; + + /** + * Returns the cell on given GridColumn. If the column is merged + * returned cell is the cell for the whole group. + * + * @param column + * the column in grid + * @return the cell on given column, merged cell for merged columns, + * null if not found + */ + public CELLTYPE getCell(Column<?, ?> column) { + Set<Column<?, ?>> cellGroup = getCellGroupForColumn(column); + if (cellGroup != null) { + return cellGroups.get(cellGroup); + } + return cells.get(column); + } + + /** + * Merges columns cells in a row + * + * @param columns + * the columns which header should be merged + * @return the remaining visible cell after the merge, or the cell + * on first column if all are hidden + */ + public CELLTYPE join(Column<?, ?>... columns) { + if (columns.length <= 1) { + throw new IllegalArgumentException( + "You can't merge less than 2 columns together."); + } + + HashSet<Column<?, ?>> columnGroup = new HashSet<Column<?, ?>>(); + for (Column<?, ?> column : columns) { + if (!cells.containsKey(column)) { + throw new IllegalArgumentException( + "Given column does not exists on row " + column); + } else if (getCellGroupForColumn(column) != null) { + throw new IllegalStateException( + "Column is already in a group."); + } + columnGroup.add(column); + } + + CELLTYPE joinedCell = createCell(); + cellGroups.put(columnGroup, joinedCell); + joinedCell.setSection(getSection()); + + calculateColspans(); + + return joinedCell; + } + + /** + * Merges columns cells in a row + * + * @param cells + * The cells to merge. Must be from the same row. + * @return The remaining visible cell after the merge, or the first + * cell if all columns are hidden + */ + public CELLTYPE join(CELLTYPE... cells) { + if (cells.length <= 1) { + throw new IllegalArgumentException( + "You can't merge less than 2 cells together."); + } + + Column<?, ?>[] columns = new Column<?, ?>[cells.length]; + + int j = 0; + for (Column<?, ?> column : this.cells.keySet()) { + CELLTYPE cell = this.cells.get(column); + if (!this.cells.containsValue(cells[j])) { + throw new IllegalArgumentException( + "Given cell does not exists on row"); + } else if (cell.equals(cells[j])) { + columns[j++] = column; + if (j == cells.length) { + break; + } + } + } + + return join(columns); + } + + private Set<Column<?, ?>> getCellGroupForColumn(Column<?, ?> column) { + for (Set<Column<?, ?>> group : cellGroups.keySet()) { + if (group.contains(column)) { + return group; + } + } + return null; + } + + void calculateColspans() { + + // Reset all cells + for (CELLTYPE cell : this.cells.values()) { + cell.setColspan(1); + } + + List<Column<?, ?>> columnOrder = new ArrayList<Column<?, ?>>( + section.grid.getColumns()); + // Set colspan for grouped cells + for (Set<Column<?, ?>> group : cellGroups.keySet()) { + if (!checkCellGroupAndOrder(columnOrder, group)) { + cellGroups.get(group).setColspan(1); + } else { + int colSpan = group.size(); + cellGroups.get(group).setColspan(colSpan); + } + } + + } + + private boolean checkCellGroupAndOrder( + List<Column<?, ?>> columnOrder, Set<Column<?, ?>> cellGroup) { + if (!columnOrder.containsAll(cellGroup)) { + return false; + } + + for (int i = 0; i < columnOrder.size(); ++i) { + if (!cellGroup.contains(columnOrder.get(i))) { + continue; + } + + for (int j = 1; j < cellGroup.size(); ++j) { + if (!cellGroup.contains(columnOrder.get(i + j))) { + return false; + } + } + return true; + } + return false; + } + + protected void addCell(Column<?, ?> column) { + CELLTYPE cell = createCell(); + cell.setSection(getSection()); + cells.put(column, cell); + } + + protected void removeCell(Column<?, ?> column) { + cells.remove(column); + } + + protected abstract CELLTYPE createCell(); + + protected StaticSection<?> getSection() { + return section; + } + + protected void setSection(StaticSection<?> section) { + this.section = section; + } + + /** + * Returns the custom style name for this row. + * + * @return the style name or null if no style name has been set + */ + public String getStyleName() { + return styleName; + } + + /** + * Sets a custom style name for this row. + * + * @param styleName + * the style name to set or null to not use any style + * name + */ + public void setStyleName(String styleName) { + this.styleName = styleName; + section.requestSectionRefresh(); + } + } + + private Grid<?> grid; + + private List<ROWTYPE> rows = new ArrayList<ROWTYPE>(); + + private boolean visible = true; + + /** + * Creates and returns a new instance of the row type. + * + * @return the created row + */ + protected abstract ROWTYPE createRow(); + + /** + * Informs the grid that this section should be re-rendered. + * <p> + * <b>Note</b> that re-render means calling update() on each cell, + * preAttach()/postAttach()/preDetach()/postDetach() is not called as + * the cells are not removed from the DOM. + */ + protected abstract void requestSectionRefresh(); + + /** + * Sets the visibility of the whole section. + * + * @param visible + * true to show this section, false to hide + */ + public void setVisible(boolean visible) { + this.visible = visible; + requestSectionRefresh(); + } + + /** + * Returns the visibility of this section. + * + * @return true if visible, false otherwise. + */ + public boolean isVisible() { + return visible; + } + + /** + * Inserts a new row at the given position. Shifts the row currently at + * that position and any subsequent rows down (adds one to their + * indices). + * + * @param index + * the position at which to insert the row + * @return the new row + * + * @throws IndexOutOfBoundsException + * if the index is out of bounds + * @see #appendRow() + * @see #prependRow() + * @see #removeRow(int) + * @see #removeRow(StaticRow) + */ + public ROWTYPE addRowAt(int index) { + ROWTYPE row = createRow(); + row.setSection(this); + for (int i = 0; i < getGrid().getColumnCount(); ++i) { + row.addCell(grid.getColumn(i)); + } + rows.add(index, row); + + requestSectionRefresh(); + return row; + } + + /** + * Adds a new row at the top of this section. + * + * @return the new row + * @see #appendRow() + * @see #addRowAt(int) + * @see #removeRow(int) + * @see #removeRow(StaticRow) + */ + public ROWTYPE prependRow() { + return addRowAt(0); + } + + /** + * Adds a new row at the bottom of this section. + * + * @return the new row + * @see #prependRow() + * @see #addRowAt(int) + * @see #removeRow(int) + * @see #removeRow(StaticRow) + */ + public ROWTYPE appendRow() { + return addRowAt(rows.size()); + } + + /** + * Removes the row at the given position. + * + * @param index + * the position of the row + * + * @throws IndexOutOfBoundsException + * if the index is out of bounds + * @see #addRowAt(int) + * @see #appendRow() + * @see #prependRow() + * @see #removeRow(StaticRow) + */ + public void removeRow(int index) { + rows.remove(index); + requestSectionRefresh(); + } + + /** + * Removes the given row from the section. + * + * @param row + * the row to be removed + * + * @throws IllegalArgumentException + * if the row does not exist in this section + * @see #addRowAt(int) + * @see #appendRow() + * @see #prependRow() + * @see #removeRow(int) + */ + public void removeRow(ROWTYPE row) { + try { + removeRow(rows.indexOf(row)); + } catch (IndexOutOfBoundsException e) { + throw new IllegalArgumentException( + "Section does not contain the given row"); + } + } + + /** + * Returns the row at the given position. + * + * @param index + * the position of the row + * @return the row with the given index + * + * @throws IndexOutOfBoundsException + * if the index is out of bounds + */ + public ROWTYPE getRow(int index) { + try { + return rows.get(index); + } catch (IndexOutOfBoundsException e) { + throw new IllegalArgumentException("Row with index " + index + + " does not exist"); + } + } + + /** + * Returns the number of rows in this section. + * + * @return the number of rows + */ + public int getRowCount() { + return rows.size(); + } + + protected List<ROWTYPE> getRows() { + return rows; + } + + protected int getVisibleRowCount() { + return isVisible() ? getRowCount() : 0; + } + + protected void addColumn(Column<?, ?> column) { + for (ROWTYPE row : rows) { + row.addCell(column); + } + } + + protected void removeColumn(Column<?, ?> column) { + for (ROWTYPE row : rows) { + row.removeCell(column); + } + } + + protected void setGrid(Grid<?> grid) { + this.grid = grid; + } + + protected Grid<?> getGrid() { + assert grid != null; + return grid; + } + } + + /** + * Represents the header section of a Grid. A header consists of a single + * header row containing a header cell for each column. Each cell has a + * simple textual caption. + */ + protected static class Header extends StaticSection<HeaderRow> { + private HeaderRow defaultRow; + + private boolean markAsDirty = false; + + @Override + public void removeRow(int index) { + HeaderRow removedRow = getRow(index); + super.removeRow(index); + if (removedRow == defaultRow) { + setDefaultRow(null); + } + } + + /** + * Sets the default row of this header. The default row is a special + * header row providing a user interface for sorting columns. + * + * @param row + * the new default row, or null for no default row + * + * @throws IllegalArgumentException + * this header does not contain the row + */ + public void setDefaultRow(HeaderRow row) { + if (row == defaultRow) { + return; + } + if (row != null && !getRows().contains(row)) { + throw new IllegalArgumentException( + "Cannot set a default row that does not exist in the container"); + } + if (defaultRow != null) { + defaultRow.setDefault(false); + } + if (row != null) { + row.setDefault(true); + } + defaultRow = row; + requestSectionRefresh(); + } + + /** + * Returns the current default row of this header. The default row is a + * special header row providing a user interface for sorting columns. + * + * @return the default row or null if no default row set + */ + public HeaderRow getDefaultRow() { + return defaultRow; + } + + @Override + protected HeaderRow createRow() { + return new HeaderRow(); + } + + @Override + protected void requestSectionRefresh() { + markAsDirty = true; + + /* + * Defer the refresh so if we multiple times call refreshSection() + * (for example when updating cell values) we only get one actual + * refresh in the end. + */ + Scheduler.get().scheduleFinally(new Scheduler.ScheduledCommand() { + + @Override + public void execute() { + if (markAsDirty) { + markAsDirty = false; + getGrid().refreshHeader(); + } + } + }); + } + + /** + * Returns the events consumed by the header + * + * @return a collection of BrowserEvents + */ + public Collection<String> getConsumedEvents() { + return Arrays.asList(BrowserEvents.TOUCHSTART, + BrowserEvents.TOUCHMOVE, BrowserEvents.TOUCHEND, + BrowserEvents.TOUCHCANCEL, BrowserEvents.CLICK); + } + } + + /** + * A single row in a grid header section. + * + */ + public static class HeaderRow extends StaticSection.StaticRow<HeaderCell> { + + private boolean isDefault = false; + + protected void setDefault(boolean isDefault) { + this.isDefault = isDefault; + } + + public boolean isDefault() { + return isDefault; + } + + @Override + protected HeaderCell createCell() { + return new HeaderCell(); + } + } + + /** + * A single cell in a grid header row. Has a textual caption. + * + */ + public static class HeaderCell extends StaticSection.StaticCell { + } + + /** + * Represents the footer section of a Grid. The footer is always empty. + */ + protected static class Footer extends StaticSection<FooterRow> { + private boolean markAsDirty = false; + + @Override + protected FooterRow createRow() { + return new FooterRow(); + } + + @Override + protected void requestSectionRefresh() { + markAsDirty = true; + + /* + * Defer the refresh so if we multiple times call refreshSection() + * (for example when updating cell values) we only get one actual + * refresh in the end. + */ + Scheduler.get().scheduleFinally(new Scheduler.ScheduledCommand() { + + @Override + public void execute() { + if (markAsDirty) { + markAsDirty = false; + getGrid().refreshFooter(); + } + } + }); + } + } + + /** + * A single cell in a grid Footer row. Has a textual caption. + * + */ + public static class FooterCell extends StaticSection.StaticCell { + } + + /** + * A single row in a grid Footer section. + * + */ + public static class FooterRow extends StaticSection.StaticRow<FooterCell> { + + @Override + protected FooterCell createCell() { + return new FooterCell(); + } + } + + /** + * An editor UI for Grid rows. A single Grid row at a time can be opened for + * editing. + */ + protected static class Editor<T> { + + public static final int KEYCODE_SHOW = KeyCodes.KEY_ENTER; + public static final int KEYCODE_HIDE = KeyCodes.KEY_ESCAPE; + + protected enum State { + INACTIVE, ACTIVATING, BINDING, ACTIVE, SAVING + } + + private Grid<T> grid; + private EditorHandler<T> handler; + + private DivElement editorOverlay = DivElement.as(DOM.createDiv()); + + private Map<Column<?, T>, Widget> columnToWidget = new HashMap<Column<?, T>, Widget>(); + + private boolean enabled = false; + private State state = State.INACTIVE; + private int rowIndex = -1; + private String styleName = null; + + private HandlerRegistration scrollHandler; + + private Button saveButton; + private Button cancelButton; + + private static final int SAVE_TIMEOUT_MS = 5000; + private final Timer saveTimeout = new Timer() { + @Override + public void run() { + getLogger().warning( + "Editor save action is taking longer than expected (" + + SAVE_TIMEOUT_MS + "ms). Does your " + + EditorHandler.class.getSimpleName() + + " remember to call success() or fail()?"); + } + }; + + private final RequestCallback<T> saveRequestCallback = new RequestCallback<T>() { + @Override + public void onSuccess(EditorRequest<T> request) { + if (state == State.SAVING) { + cleanup(); + cancel(); + } + } + + @Override + public void onError(EditorRequest<T> request) { + if (state == State.SAVING) { + cleanup(); + + // TODO probably not the most correct thing to do... + getLogger().warning( + "An error occurred when trying to save the " + + "modified row"); + } + } + + private void cleanup() { + state = State.ACTIVE; + setButtonsEnabled(true); + saveTimeout.cancel(); + } + }; + + private static final int BIND_TIMEOUT_MS = 5000; + private final Timer bindTimeout = new Timer() { + @Override + public void run() { + getLogger().warning( + "Editor bind action is taking longer than expected (" + + BIND_TIMEOUT_MS + "ms). Does your " + + EditorHandler.class.getSimpleName() + + " remember to call success() or fail()?"); + } + }; + private final RequestCallback<T> bindRequestCallback = new RequestCallback<T>() { + @Override + public void onSuccess(EditorRequest<T> request) { + if (state == State.BINDING) { + state = State.ACTIVE; + bindTimeout.cancel(); + + showOverlay(grid.getEscalator().getBody() + .getRowElement(request.getRowIndex())); + } + } + + @Override + public void onError(EditorRequest<T> request) { + if (state == State.BINDING) { + state = State.INACTIVE; + bindTimeout.cancel(); + + // TODO show something in the DOM as well? + getLogger().warning( + "An error occurred while trying to show the " + + "Grid editor"); + grid.getEscalator().setScrollLocked(Direction.VERTICAL, + false); + } + } + }; + + public int getRow() { + return rowIndex; + } + + /** + * Opens the editor over the row with the given index. + * + * @param rowIndex + * the index of the row to be edited + * + * @throws IllegalStateException + * if this editor is not enabled + * @throws IllegalStateException + * if this editor is already in edit mode + */ + public void editRow(int rowIndex) { + if (!enabled) { + throw new IllegalStateException( + "Cannot edit row: editor is not enabled"); + } + if (state != State.INACTIVE) { + throw new IllegalStateException( + "Cannot edit row: editor already in edit mode"); + } + + this.rowIndex = rowIndex; + + state = State.ACTIVATING; + + if (grid.getEscalator().getVisibleRowRange().contains(rowIndex)) { + show(); + } else { + grid.scrollToRow(rowIndex, ScrollDestination.MIDDLE); + } + } + + /** + * Cancels the currently active edit and hides the editor. Any changes + * that are not {@link #save() saved} are lost. + * + * @throws IllegalStateException + * if this editor is not enabled + * @throws IllegalStateException + * if this editor is not in edit mode + */ + public void cancel() { + if (!enabled) { + throw new IllegalStateException( + "Cannot cancel edit: editor is not enabled"); + } + if (state == State.INACTIVE) { + throw new IllegalStateException( + "Cannot cancel edit: editor is not in edit mode"); + } + hideOverlay(); + grid.getEscalator().setScrollLocked(Direction.VERTICAL, false); + + EditorRequest<T> request = new EditorRequest<T>(grid, rowIndex, + null); + handler.cancel(request); + state = State.INACTIVE; + } + + /** + * Saves any unsaved changes to the data source and hides the editor. + * + * @throws IllegalStateException + * if this editor is not enabled + * @throws IllegalStateException + * if this editor is not in edit mode + */ + public void save() { + if (!enabled) { + throw new IllegalStateException( + "Cannot save: editor is not enabled"); + } + if (state != State.ACTIVE) { + throw new IllegalStateException( + "Cannot save: editor is not in edit mode"); + } + + state = State.SAVING; + setButtonsEnabled(false); + saveTimeout.schedule(SAVE_TIMEOUT_MS); + EditorRequest<T> request = new EditorRequest<T>(grid, rowIndex, + saveRequestCallback); + handler.save(request); + } + + /** + * Returns the handler responsible for binding data and editor widgets + * to this editor. + * + * @return the editor handler or null if not set + */ + public EditorHandler<T> getHandler() { + return handler; + } + + /** + * Sets the handler responsible for binding data and editor widgets to + * this editor. + * + * @param rowHandler + * the new editor handler + * + * @throws IllegalStateException + * if this editor is currently in edit mode + */ + public void setHandler(EditorHandler<T> rowHandler) { + if (state != State.INACTIVE) { + throw new IllegalStateException( + "Cannot set EditorHandler: editor is currently in edit mode"); + } + handler = rowHandler; + } + + public boolean isEnabled() { + return enabled; + } + + /** + * Sets the enabled state of this editor. + * + * @param enabled + * true if enabled, false otherwise + * + * @throws IllegalStateException + * if in edit mode and trying to disable + * @throws IllegalStateException + * if the editor handler is not set + */ + public void setEnabled(boolean enabled) { + if (enabled == false && state != State.INACTIVE) { + throw new IllegalStateException( + "Cannot disable: editor is in edit mode"); + } else if (enabled == true && getHandler() == null) { + throw new IllegalStateException( + "Cannot enable: EditorHandler not set"); + } + this.enabled = enabled; + } + + protected void show() { + if (state == State.ACTIVATING) { + state = State.BINDING; + bindTimeout.schedule(BIND_TIMEOUT_MS); + EditorRequest<T> request = new EditorRequest<T>(grid, rowIndex, + bindRequestCallback); + handler.bind(request); + grid.getEscalator().setScrollLocked(Direction.VERTICAL, true); + } + } + + protected void setGrid(final Grid<T> grid) { + assert grid != null : "Grid cannot be null"; + assert this.grid == null : "Can only attach editor to Grid once"; + + this.grid = grid; + + grid.addDataAvailableHandler(new DataAvailableHandler() { + @Override + public void onDataAvailable(DataAvailableEvent event) { + if (event.getAvailableRows().contains(rowIndex)) { + show(); + } + } + }); + } + + protected State getState() { + return state; + } + + protected void setState(State state) { + this.state = state; + } + + /** + * Returns the editor widget associated with the given column. If the + * editor is not active, returns null. + * + * @param column + * the column + * @return the widget if the editor is open, null otherwise + */ + protected Widget getWidget(Column<?, T> column) { + return columnToWidget.get(column); + } + + /** + * Opens the editor overlay over the given table row. + * + * @param tr + * the row to be edited + */ + protected void showOverlay(TableRowElement tr) { + + DivElement tableWrapper = DivElement.as(tr.getParentElement() + .getParentElement().getParentElement()); + + AbstractRowContainer body = (AbstractRowContainer) grid + .getEscalator().getBody(); + + double rowTop = body.getRowTop(tr); + int bodyTop = body.getElement().getAbsoluteTop(); + int wrapperTop = tableWrapper.getAbsoluteTop(); + + double width = WidgetUtil + .getRequiredWidthBoundingClientRectDouble(tr); + double height = WidgetUtil + .getRequiredHeightBoundingClientRectDouble(tr); + setBounds(editorOverlay, tr.getOffsetLeft(), rowTop + bodyTop + - wrapperTop, width, height); + + updateHorizontalScrollPosition(); + + scrollHandler = grid.addScrollHandler(new ScrollHandler() { + @Override + public void onScroll(ScrollEvent event) { + updateHorizontalScrollPosition(); + } + }); + + tableWrapper.appendChild(editorOverlay); + + for (int i = 0; i < tr.getCells().getLength(); i++) { + Element cell = createCell(tr.getCells().getItem(i)); + + editorOverlay.appendChild(cell); + + Column<?, T> column = grid.getColumn(i); + if (column == grid.selectionColumn) { + continue; + } + + Widget editor = getHandler().getWidget(column); + if (editor != null) { + columnToWidget.put(column, editor); + attachWidget(editor, cell); + } + } + + saveButton = new Button(); + saveButton.setText("Save"); + saveButton.setStylePrimaryName("v-nativebutton"); + saveButton.addStyleName(styleName + "-save"); + saveButton.addClickHandler(new ClickHandler() { + @Override + public void onClick(ClickEvent event) { + save(); + } + }); + setBounds(saveButton.getElement(), 0, tr.getOffsetHeight() + 5, 50, + 25); + attachWidget(saveButton, editorOverlay); + + cancelButton = new Button(); + cancelButton.setText("Cancel"); + cancelButton.setStylePrimaryName("v-nativebutton"); + cancelButton.addStyleName(styleName + "-cancel"); + cancelButton.addClickHandler(new ClickHandler() { + @Override + public void onClick(ClickEvent event) { + cancel(); + } + }); + setBounds(cancelButton.getElement(), 55, tr.getOffsetHeight() + 5, + 50, 25); + attachWidget(cancelButton, editorOverlay); + } + + protected void hideOverlay() { + for (Widget w : columnToWidget.values()) { + setParent(w, null); + } + columnToWidget.clear(); + + editorOverlay.removeAllChildren(); + editorOverlay.removeFromParent(); + + scrollHandler.removeHandler(); + } + + protected void setStylePrimaryName(String primaryName) { + if (styleName != null) { + editorOverlay.removeClassName(styleName); + } + styleName = primaryName + "-editor"; + editorOverlay.addClassName(styleName); + } + + /** + * Creates an editor cell corresponding to the given table cell. The + * returned element is empty and has the same dimensions and position as + * the table cell. + * + * @param td + * the table cell used as a reference + * @return an editor cell corresponding to the given cell + */ + protected Element createCell(TableCellElement td) { + DivElement cell = DivElement.as(DOM.createDiv()); + double width = WidgetUtil + .getRequiredWidthBoundingClientRectDouble(td); + double height = WidgetUtil + .getRequiredHeightBoundingClientRectDouble(td); + setBounds(cell, td.getOffsetLeft(), td.getOffsetTop(), width, + height); + return cell; + } + + private void attachWidget(Widget w, Element parent) { + parent.appendChild(w.getElement()); + setParent(w, grid); + } + + private static void setBounds(Element e, double left, double top, + double width, double height) { + Style style = e.getStyle(); + style.setLeft(left, Unit.PX); + style.setTop(top, Unit.PX); + style.setWidth(width, Unit.PX); + style.setHeight(height, Unit.PX); + } + + private void updateHorizontalScrollPosition() { + editorOverlay.getStyle().setLeft(-grid.getScrollLeft(), Unit.PX); + } + + protected void setGridEnabled(boolean enabled) { + // TODO: This should be informed to handler as well so possible + // fields can be disabled. + setButtonsEnabled(enabled); + } + + private void setButtonsEnabled(boolean enabled) { + saveButton.setEnabled(enabled); + cancelButton.setEnabled(enabled); + } + } + + public static abstract class AbstractGridKeyEvent<HANDLER extends AbstractGridKeyEventHandler> + extends KeyEvent<HANDLER> { + + private Grid<?> grid; + private final Type<HANDLER> associatedType = new Type<HANDLER>( + getBrowserEventType(), this); + private final CellReference<?> targetCell; + + public AbstractGridKeyEvent(Grid<?> grid, CellReference<?> targetCell) { + this.grid = grid; + this.targetCell = targetCell; + } + + protected abstract String getBrowserEventType(); + + /** + * Gets the Grid instance for this event. + * + * @return grid + */ + public Grid<?> getGrid() { + return grid; + } + + /** + * Gets the focused cell for this event. + * + * @return focused cell + */ + public CellReference<?> getFocusedCell() { + return targetCell; + } + + @Override + protected void dispatch(HANDLER handler) { + EventTarget target = getNativeEvent().getEventTarget(); + if (Element.is(target) + && !grid.isElementInChildWidget(Element.as(target))) { + + Section section = Section.FOOTER; + final RowContainer container = grid.cellFocusHandler.containerWithFocus; + if (container == grid.escalator.getHeader()) { + section = Section.HEADER; + } else if (container == grid.escalator.getBody()) { + section = Section.BODY; + } + + doDispatch(handler, section); + } + } + + protected abstract void doDispatch(HANDLER handler, Section section); + + @Override + public Type<HANDLER> getAssociatedType() { + return associatedType; + } + } + + public static abstract class AbstractGridMouseEvent<HANDLER extends AbstractGridMouseEventHandler> + extends MouseEvent<HANDLER> { + + private Grid<?> grid; + private final CellReference<?> targetCell; + private final Type<HANDLER> associatedType = new Type<HANDLER>( + getBrowserEventType(), this); + + public AbstractGridMouseEvent(Grid<?> grid, CellReference<?> targetCell) { + this.grid = grid; + this.targetCell = targetCell; + } + + protected abstract String getBrowserEventType(); + + /** + * Gets the Grid instance for this event. + * + * @return grid + */ + public Grid<?> getGrid() { + return grid; + } + + /** + * Gets the reference of target cell for this event. + * + * @return target cell + */ + public CellReference<?> getTargetCell() { + return targetCell; + } + + @Override + protected void dispatch(HANDLER handler) { + EventTarget target = getNativeEvent().getEventTarget(); + if (!Element.is(target)) { + // Target is not an element + return; + } + + Element targetElement = Element.as(target); + if (grid.isElementInChildWidget(targetElement)) { + // Target is some widget inside of Grid + return; + } + + final RowContainer container = grid.escalator + .findRowContainer(targetElement); + if (container == null) { + // No container for given element + return; + } + + Section section = Section.FOOTER; + if (container == grid.escalator.getHeader()) { + section = Section.HEADER; + } else if (container == grid.escalator.getBody()) { + section = Section.BODY; + } + + doDispatch(handler, section); + } + + protected abstract void doDispatch(HANDLER handler, Section section); + + @Override + public Type<HANDLER> getAssociatedType() { + return associatedType; + } + } + + private static final String CUSTOM_STYLE_PROPERTY_NAME = "customStyle"; + + private EventCellReference<T> eventCell = new EventCellReference<T>(this); + private GridKeyDownEvent keyDown = new GridKeyDownEvent(this, eventCell); + private GridKeyUpEvent keyUp = new GridKeyUpEvent(this, eventCell); + private GridKeyPressEvent keyPress = new GridKeyPressEvent(this, eventCell); + private GridClickEvent clickEvent = new GridClickEvent(this, eventCell); + private GridDoubleClickEvent doubleClickEvent = new GridDoubleClickEvent( + this, eventCell); + + private class CellFocusHandler { + + private RowContainer containerWithFocus = escalator.getBody(); + private int rowWithFocus = 0; + private Range cellFocusRange = Range.withLength(0, 1); + private int lastFocusedBodyRow = 0; + private int lastFocusedHeaderRow = 0; + private int lastFocusedFooterRow = 0; + private TableCellElement cellWithFocusStyle = null; + private TableRowElement rowWithFocusStyle = null; + + public CellFocusHandler() { + sinkEvents(getNavigationEvents()); + } + + private Cell getFocusedCell() { + return new Cell(rowWithFocus, cellFocusRange.getStart(), + cellWithFocusStyle); + } + + /** + * Sets style names for given cell when needed. + */ + public void updateFocusedCellStyle(FlyweightCell cell, + RowContainer cellContainer) { + int cellRow = cell.getRow(); + int cellColumn = cell.getColumn(); + int colSpan = cell.getColSpan(); + boolean columnHasFocus = Range.withLength(cellColumn, colSpan) + .intersects(cellFocusRange); + + if (cellContainer == containerWithFocus) { + // Cell is in the current container + if (cellRow == rowWithFocus && columnHasFocus) { + if (cellWithFocusStyle != cell.getElement()) { + // Cell is correct but it does not have focused style + if (cellWithFocusStyle != null) { + // Remove old focus style + setStyleName(cellWithFocusStyle, + cellFocusStyleName, false); + } + cellWithFocusStyle = cell.getElement(); + + // Add focus style to correct cell. + setStyleName(cellWithFocusStyle, cellFocusStyleName, + true); + } + } else if (cellWithFocusStyle == cell.getElement()) { + // Due to escalator reusing cells, a new cell has the same + // element but is not the focused cell. + setStyleName(cellWithFocusStyle, cellFocusStyleName, false); + cellWithFocusStyle = null; + } + } + } + + /** + * Sets focus style for the given row if needed. + * + * @param row + * a row object + */ + public void updateFocusedRowStyle(Row row) { + if (rowWithFocus == row.getRow() + && containerWithFocus == escalator.getBody()) { + if (row.getElement() != rowWithFocusStyle) { + // Row should have focus style but does not have it. + if (rowWithFocusStyle != null) { + setStyleName(rowWithFocusStyle, rowFocusStyleName, + false); + } + rowWithFocusStyle = row.getElement(); + setStyleName(rowWithFocusStyle, rowFocusStyleName, true); + } + } else if (rowWithFocusStyle == row.getElement() + || (containerWithFocus != escalator.getBody() && rowWithFocusStyle != null)) { + // Remove focus style. + setStyleName(rowWithFocusStyle, rowFocusStyleName, false); + rowWithFocusStyle = null; + } + } + + /** + * Sets the currently focused. + * + * @param row + * the index of the row having focus + * @param column + * the index of the column having focus + * @param container + * the row container having focus + */ + private void setCellFocus(int row, int column, RowContainer container) { + if (row == rowWithFocus && cellFocusRange.contains(column) + && container == this.containerWithFocus) { + refreshRow(rowWithFocus); + return; + } + + int oldRow = rowWithFocus; + rowWithFocus = row; + Range oldRange = cellFocusRange; + + if (container == escalator.getBody()) { + scrollToRow(rowWithFocus); + cellFocusRange = Range.withLength(column, 1); + } else { + int i = 0; + Element cell = container.getRowElement(rowWithFocus) + .getFirstChildElement(); + do { + int colSpan = cell + .getPropertyInt(FlyweightCell.COLSPAN_ATTR); + Range cellRange = Range.withLength(i, colSpan); + if (cellRange.contains(column)) { + cellFocusRange = cellRange; + break; + } + cell = cell.getNextSiblingElement(); + ++i; + } while (cell != null); + } + + if (column >= escalator.getColumnConfiguration() + .getFrozenColumnCount()) { + escalator.scrollToColumn(column, ScrollDestination.ANY, 10); + } + + if (this.containerWithFocus == container) { + if (oldRange.equals(cellFocusRange) && oldRow != rowWithFocus) { + refreshRow(oldRow); + } else { + refreshHeader(); + refreshFooter(); + } + } else { + RowContainer oldContainer = this.containerWithFocus; + this.containerWithFocus = container; + + if (oldContainer == escalator.getBody()) { + lastFocusedBodyRow = oldRow; + } else if (oldContainer == escalator.getHeader()) { + lastFocusedHeaderRow = oldRow; + } else { + lastFocusedFooterRow = oldRow; + } + + if (!oldRange.equals(cellFocusRange)) { + refreshHeader(); + refreshFooter(); + if (oldContainer == escalator.getBody()) { + oldContainer.refreshRows(oldRow, 1); + } + } else { + oldContainer.refreshRows(oldRow, 1); + } + } + refreshRow(rowWithFocus); + } + + /** + * Sets focus on a cell. + * + * <p> + * <em>Note</em>: cell focus is not the same as JavaScript's + * {@code document.activeElement}. + * + * @param cell + * a cell object + */ + public void setCellFocus(CellReference<T> cell) { + setCellFocus(cell.getRowIndex(), cell.getColumnIndex(), + escalator.findRowContainer(cell.getElement())); + } + + /** + * Gets list of events that can be used for cell focusing. + * + * @return list of navigation related event types + */ + public Collection<String> getNavigationEvents() { + return Arrays.asList(BrowserEvents.KEYDOWN, BrowserEvents.CLICK); + } + + /** + * Handle events that can move the cell focus. + */ + public void handleNavigationEvent(Event event, CellReference<T> cell) { + if (event.getType().equals(BrowserEvents.CLICK)) { + setCellFocus(cell); + // Grid should have focus when clicked. + getElement().focus(); + } else if (event.getType().equals(BrowserEvents.KEYDOWN)) { + int newRow = rowWithFocus; + RowContainer newContainer = containerWithFocus; + int newColumn = cellFocusRange.getStart(); + + switch (event.getKeyCode()) { + case KeyCodes.KEY_DOWN: + ++newRow; + break; + case KeyCodes.KEY_UP: + --newRow; + break; + case KeyCodes.KEY_RIGHT: + if (cellFocusRange.getEnd() >= getColumns().size()) { + return; + } + newColumn = cellFocusRange.getEnd(); + break; + case KeyCodes.KEY_LEFT: + if (newColumn == 0) { + return; + } + --newColumn; + break; + case KeyCodes.KEY_TAB: + if (event.getShiftKey()) { + newContainer = getPreviousContainer(containerWithFocus); + } else { + newContainer = getNextContainer(containerWithFocus); + } + + if (newContainer == containerWithFocus) { + return; + } + break; + default: + return; + } + + if (newContainer != containerWithFocus) { + if (newContainer == escalator.getBody()) { + newRow = lastFocusedBodyRow; + } else if (newContainer == escalator.getHeader()) { + newRow = lastFocusedHeaderRow; + } else { + newRow = lastFocusedFooterRow; + } + } else if (newRow < 0) { + newContainer = getPreviousContainer(newContainer); + + if (newContainer == containerWithFocus) { + newRow = 0; + } else if (newContainer == escalator.getBody()) { + newRow = getLastVisibleRowIndex(); + } else { + newRow = newContainer.getRowCount() - 1; + } + } else if (newRow >= containerWithFocus.getRowCount()) { + newContainer = getNextContainer(newContainer); + + if (newContainer == containerWithFocus) { + newRow = containerWithFocus.getRowCount() - 1; + } else if (newContainer == escalator.getBody()) { + newRow = getFirstVisibleRowIndex(); + } else { + newRow = 0; + } + } + + if (newContainer.getRowCount() == 0) { + /* + * There are no rows in the container. Can't change the + * focused cell. + */ + return; + } + + event.preventDefault(); + event.stopPropagation(); + + setCellFocus(newRow, newColumn, newContainer); + } + + } + + private RowContainer getPreviousContainer(RowContainer current) { + if (current == escalator.getFooter()) { + current = escalator.getBody(); + } else if (current == escalator.getBody()) { + current = escalator.getHeader(); + } else { + return current; + } + + if (current.getRowCount() == 0) { + return getPreviousContainer(current); + } + return current; + } + + private RowContainer getNextContainer(RowContainer current) { + if (current == escalator.getHeader()) { + current = escalator.getBody(); + } else if (current == escalator.getBody()) { + current = escalator.getFooter(); + } else { + return current; + } + + if (current.getRowCount() == 0) { + return getNextContainer(current); + } + return current; + } + + private void refreshRow(int row) { + containerWithFocus.refreshRows(row, 1); + } + + /** + * Offsets the focused cell's range. + * + * @param offset + * offset for fixing focused cell's range + */ + public void offsetRangeBy(int offset) { + cellFocusRange = cellFocusRange.offsetBy(offset); + } + + /** + * Informs {@link CellFocusHandler} that certain range of rows has been + * added to the Grid body. {@link CellFocusHandler} will fix indices + * accordingly. + * + * @param added + * a range of added rows + */ + public void rowsAddedToBody(Range added) { + boolean bodyHasFocus = (containerWithFocus == escalator.getBody()); + boolean insertionIsAboveFocusedCell = (added.getStart() <= rowWithFocus); + if (bodyHasFocus && insertionIsAboveFocusedCell) { + rowWithFocus += added.length(); + refreshRow(rowWithFocus); + } + } + + /** + * Informs {@link CellFocusHandler} that certain range of rows has been + * removed from the Grid body. {@link CellFocusHandler} will fix indices + * accordingly. + * + * @param removed + * a range of removed rows + */ + public void rowsRemovedFromBody(Range removed) { + if (containerWithFocus != escalator.getBody()) { + return; + } else if (!removed.contains(rowWithFocus)) { + if (removed.getStart() > rowWithFocus) { + return; + } + rowWithFocus = rowWithFocus - removed.length(); + } else { + if (containerWithFocus.getRowCount() > removed.getEnd()) { + rowWithFocus = removed.getStart(); + } else if (removed.getStart() > 0) { + rowWithFocus = removed.getStart() - 1; + } else { + if (escalator.getHeader().getRowCount() > 0) { + rowWithFocus = lastFocusedHeaderRow; + containerWithFocus = escalator.getHeader(); + } else if (escalator.getFooter().getRowCount() > 0) { + rowWithFocus = lastFocusedFooterRow; + containerWithFocus = escalator.getFooter(); + } + } + } + refreshRow(rowWithFocus); + } + } + + public final class SelectionColumn extends Column<Boolean, T> { + private boolean initDone = false; + + SelectionColumn(final Renderer<Boolean> selectColumnRenderer) { + super(selectColumnRenderer); + } + + void initDone() { + if (getSelectionModel() instanceof SelectionModel.Multi + && header.getDefaultRow() != null) { + /* + * TODO: Currently the select all check box is shown when multi + * selection is in use. This might result in malfunctions if no + * SelectAllHandlers are present. + * + * Later on this could be fixed so that it check such handlers + * exist. + */ + final SelectionModel.Multi<T> model = (Multi<T>) getSelectionModel(); + final CheckBox checkBox = new CheckBox(); + checkBox.addValueChangeHandler(new ValueChangeHandler<Boolean>() { + + @Override + public void onValueChange(ValueChangeEvent<Boolean> event) { + if (event.getValue()) { + fireEvent(new SelectAllEvent<T>(model)); + } else { + model.deselectAll(); + } + } + }); + header.getDefaultRow().getCell(this).setWidget(checkBox); + } + + setWidth(-1); + + initDone = true; + } + + @Override + public Column<Boolean, T> setWidth(double pixels) { + if (pixels != getWidth() && initDone) { + throw new UnsupportedOperationException("The selection " + + "column cannot be modified after init"); + } else { + super.setWidth(pixels); + } + + return this; + } + + @Override + public Boolean getValue(T row) { + return Boolean.valueOf(isSelected(row)); + } + + @Override + public Column<Boolean, T> setExpandRatio(int ratio) { + throw new UnsupportedOperationException( + "can't change the expand ratio of the selection column"); + } + + @Override + public int getExpandRatio() { + return 0; + } + + @Override + public Column<Boolean, T> setMaximumWidth(double pixels) { + throw new UnsupportedOperationException( + "can't change the maximum width of the selection column"); + } + + @Override + public double getMaximumWidth() { + return -1; + } + + @Override + public Column<Boolean, T> setMinimumWidth(double pixels) { + throw new UnsupportedOperationException( + "can't change the minimum width of the selection column"); + } + + @Override + public double getMinimumWidth() { + return -1; + } + } + + /** + * Helper class for performing sorting through the user interface. Controls + * the sort() method, reporting USER as the event originator. This is a + * completely internal class, and is, as such, safe to re-name should a more + * descriptive name come to mind. + */ + private final class UserSorter { + + private final Timer timer; + private boolean scheduledMultisort; + private Column<?, T> column; + + private UserSorter() { + timer = new Timer() { + + @Override + public void run() { + UserSorter.this.sort(column, scheduledMultisort); + } + }; + } + + /** + * Toggle sorting for a cell. If the multisort parameter is set to true, + * the cell's sort order is modified as a natural part of a multi-sort + * chain. If false, the sorting order is set to ASCENDING for that + * cell's column. If that column was already the only sorted column in + * the Grid, the sort direction is flipped. + * + * @param cell + * a valid cell reference + * @param multisort + * whether the sort command should act as a multi-sort stack + * or not + */ + public void sort(Column<?, ?> column, boolean multisort) { + + if (!columns.contains(column)) { + throw new IllegalArgumentException( + "Given column is not a column in this grid. " + + column.toString()); + } + + if (!column.isSortable()) { + return; + } + + final SortOrder so = getSortOrder(column); + + if (multisort) { + + // If the sort order exists, replace existing value with its + // opposite + if (so != null) { + final int idx = sortOrder.indexOf(so); + sortOrder.set(idx, so.getOpposite()); + } else { + // If it doesn't, just add a new sort order to the end of + // the list + sortOrder.add(new SortOrder(column)); + } + + } else { + + // Since we're doing single column sorting, first clear the + // list. Then, if the sort order existed, add its opposite, + // otherwise just add a new sort value + + int items = sortOrder.size(); + sortOrder.clear(); + if (so != null && items == 1) { + sortOrder.add(so.getOpposite()); + } else { + sortOrder.add(new SortOrder(column)); + } + } + + // sortOrder has been changed; tell the Grid to re-sort itself by + // user request. + Grid.this.sort(true); + } + + /** + * Perform a sort after a delay. + * + * @param delay + * delay, in milliseconds + */ + public void sortAfterDelay(int delay, boolean multisort) { + column = eventCell.getColumn(); + scheduledMultisort = multisort; + timer.schedule(delay); + } + + /** + * Check if a delayed sort command has been issued but not yet carried + * out. + * + * @return a boolean value + */ + public boolean isDelayedSortScheduled() { + return timer.isRunning(); + } + + /** + * Cancel a scheduled sort. + */ + public void cancelDelayedSort() { + timer.cancel(); + } + + } + + /** @see Grid#autoColumnWidthsRecalculator */ + private class AutoColumnWidthsRecalculator { + + private final ScheduledCommand calculateCommand = new ScheduledCommand() { + + @Override + public void execute() { + if (!isScheduled) { + // something cancelled running this. + return; + } + + if (header.markAsDirty || footer.markAsDirty) { + if (rescheduleCount < 10) { + /* + * Headers and footers are rendered as finally, this way + * we re-schedule this loop as finally, at the end of + * the queue, so that the headers have a chance to + * render themselves. + */ + Scheduler.get().scheduleFinally(this); + rescheduleCount++; + } else { + /* + * We've tried too many times reschedule finally. Seems + * like something is being deferred. Let the queue + * execute and retry again. + */ + rescheduleCount = 0; + Scheduler.get().scheduleDeferred(this); + } + } else if (dataIsBeingFetched) { + Scheduler.get().scheduleDeferred(this); + } else { + calculate(); + } + } + }; + + private int rescheduleCount = 0; + private boolean isScheduled; + + /** + * Calculates and applies column widths, taking into account fixed + * widths and column expand rules + * + * @param immediately + * <code>true</code> if the widths should be executed + * immediately (ignoring lazy loading completely), or + * <code>false</code> if the command should be run after a + * while (duplicate non-immediately invocations are ignored). + * @see Column#setWidth(double) + * @see Column#setExpandRatio(int) + * @see Column#setMinimumWidth(double) + * @see Column#setMaximumWidth(double) + */ + public void schedule() { + if (!isScheduled) { + isScheduled = true; + Scheduler.get().scheduleFinally(calculateCommand); + } + } + + private void calculate() { + isScheduled = false; + rescheduleCount = 0; + + assert !dataIsBeingFetched : "Trying to calculate column widths even though data is still being fetched."; + /* + * At this point we assume that no data is being fetched anymore. + * Everything's rendered in the DOM. Now we just make sure + * everything fits as it should. + */ + + /* + * Quick optimization: if the sum of fixed widths and minimum widths + * is greater than the grid can display, we already know that things + * will be squeezed and no expansion will happen. + */ + if (gridWasTooNarrowAndEverythingWasFixedAlready()) { + return; + } + + boolean someColumnExpands = false; + int totalRatios = 0; + double reservedPixels = 0; + final Set<Column<?, ?>> columnsToExpand = new HashSet<Column<?, ?>>(); + + /* + * Set all fixed widths and also calculate the size-to-fit widths + * for the autocalculated columns. + * + * This way we know with how many pixels we have left to expand the + * rest. + */ + for (Column<?, ?> column : getColumns()) { + final double widthAsIs = column.getWidth(); + final boolean isFixedWidth = widthAsIs >= 0; + final double widthFixed = Math.max(widthAsIs, + column.getMinimumWidth()); + final int expandRatio = column.getExpandRatio(); + + if (isFixedWidth) { + column.doSetWidth(widthFixed); + } else { + column.doSetWidth(-1); + final double newWidth = column.getWidthActual(); + final double maxWidth = getMaxWidth(column); + boolean shouldExpand = newWidth < maxWidth + && expandRatio > 0; + if (shouldExpand) { + totalRatios += expandRatio; + columnsToExpand.add(column); + someColumnExpands = true; + } + } + reservedPixels += column.getWidthActual(); + } + + /* + * If no column has a positive expand ratio, all columns with a + * negative expand ratio has an expand ratio. Columns with 0 expand + * ratio are excluded. + * + * This means that if we only define one column to have 0 expand, it + * will be the only one not to expand, while all the others expand. + */ + if (!someColumnExpands) { + assert totalRatios == 0 : "totalRatios should've been 0"; + assert columnsToExpand.isEmpty() : "columnsToExpand should've been empty"; + for (Column<?, ?> column : getColumns()) { + final double width = column.getWidth(); + final int expandRatio = column.getExpandRatio(); + if (width < 0 && expandRatio < 0) { + totalRatios++; + columnsToExpand.add(column); + } + } + } + + /* + * Now that we know how many pixels we need at the very least, we + * can distribute the remaining pixels to all columns according to + * their expand ratios. + */ + double pixelsToDistribute = escalator.getInnerWidth() + - reservedPixels; + if (pixelsToDistribute <= 0 || totalRatios <= 0) { + return; + } + + /* + * Check for columns that hit their max width. Adjust + * pixelsToDistribute and totalRatios accordingly. Recheck. Stop + * when no new columns hit their max width + */ + boolean aColumnHasMaxedOut; + do { + aColumnHasMaxedOut = false; + final double widthPerRatio = pixelsToDistribute / totalRatios; + final Iterator<Column<?, ?>> i = columnsToExpand.iterator(); + while (i.hasNext()) { + final Column<?, ?> column = i.next(); + final int expandRatio = getExpandRatio(column, + someColumnExpands); + final double autoWidth = column.getWidthActual(); + final double maxWidth = getMaxWidth(column); + final double widthCandidate = autoWidth + widthPerRatio + * expandRatio; + + if (maxWidth <= widthCandidate) { + column.doSetWidth(maxWidth); + totalRatios -= expandRatio; + pixelsToDistribute -= maxWidth - autoWidth; + i.remove(); + aColumnHasMaxedOut = true; + } + } + } while (aColumnHasMaxedOut); + + if (totalRatios <= 0 && columnsToExpand.isEmpty()) { + return; + } + assert pixelsToDistribute > 0 : "We've run out of pixels to distribute (" + + pixelsToDistribute + + "px to " + + totalRatios + + " ratios between " + columnsToExpand.size() + " columns)"; + assert totalRatios > 0 && !columnsToExpand.isEmpty() : "Bookkeeping out of sync. Ratios: " + + totalRatios + " Columns: " + columnsToExpand.size(); + + /* + * If we still have anything left, distribute the remaining pixels + * to the remaining columns. + */ + final double widthPerRatio = pixelsToDistribute / totalRatios; + for (Column<?, ?> column : columnsToExpand) { + final int expandRatio = getExpandRatio(column, + someColumnExpands); + final double autoWidth = column.getWidthActual(); + final double totalWidth = autoWidth + widthPerRatio + * expandRatio; + column.doSetWidth(totalWidth); + + totalRatios -= expandRatio; + } + assert totalRatios == 0 : "Bookkeeping error: there were still some ratios left undistributed: " + + totalRatios; + + /* + * Check the guarantees for minimum width and scoot back the columns + * that don't care. + */ + boolean minWidthsCausedReflows; + do { + minWidthsCausedReflows = false; + + /* + * First, let's check which columns were too cramped, and expand + * them. Also keep track on how many pixels we grew - we need to + * remove those pixels from other columns + */ + double pixelsToRemoveFromOtherColumns = 0; + for (Column<?, T> column : getColumns()) { + /* + * We can't iterate over columnsToExpand, even though that + * would be convenient. This is because some column without + * an expand ratio might still have a min width - those + * wouldn't show up in that set. + */ + + double minWidth = getMinWidth(column); + double currentWidth = column.getWidthActual(); + boolean hasAutoWidth = column.getWidth() < 0; + if (hasAutoWidth && currentWidth < minWidth) { + column.doSetWidth(minWidth); + pixelsToRemoveFromOtherColumns += (minWidth - currentWidth); + minWidthsCausedReflows = true; + + /* + * Remove this column form the set if it exists. This + * way we make sure that it doesn't get shrunk in the + * next step. + */ + columnsToExpand.remove(column); + } + } + + /* + * Now we need to shrink the remaining columns according to + * their ratios. Recalculate the sum of remaining ratios. + */ + totalRatios = 0; + for (Column<?, ?> column : columnsToExpand) { + totalRatios += getExpandRatio(column, someColumnExpands); + } + final double pixelsToRemovePerRatio = pixelsToRemoveFromOtherColumns + / totalRatios; + for (Column<?, ?> column : columnsToExpand) { + final double pixelsToRemove = pixelsToRemovePerRatio + * getExpandRatio(column, someColumnExpands); + column.doSetWidth(column.getWidthActual() - pixelsToRemove); + } + + } while (minWidthsCausedReflows); + } + + private boolean gridWasTooNarrowAndEverythingWasFixedAlready() { + double freeSpace = escalator.getInnerWidth(); + for (Column<?, ?> column : getColumns()) { + if (column.getWidth() >= 0) { + freeSpace -= column.getWidth(); + } else if (column.getMinimumWidth() >= 0) { + freeSpace -= column.getMinimumWidth(); + } + } + + if (freeSpace < 0) { + for (Column<?, ?> column : getColumns()) { + column.doSetWidth(column.getWidth()); + + boolean wasFixedWidth = column.getWidth() <= 0; + boolean newWidthIsSmallerThanMinWidth = column + .getWidthActual() < getMinWidth(column); + if (wasFixedWidth && newWidthIsSmallerThanMinWidth) { + column.doSetWidth(column.getMinimumWidth()); + } + } + } + + return freeSpace < 0; + } + + private int getExpandRatio(Column<?, ?> column, + boolean someColumnExpands) { + int expandRatio = column.getExpandRatio(); + if (expandRatio > 0) { + return expandRatio; + } else if (expandRatio < 0) { + assert !someColumnExpands : "No columns should've expanded"; + return 1; + } else { + assert false : "this method should've not been called at all if expandRatio is 0"; + return 0; + } + } + + /** + * Returns the maximum width of the column, or {@link Double#MAX_VALUE} + * if defined as negative. + */ + private double getMaxWidth(Column<?, ?> column) { + double maxWidth = column.getMaximumWidth(); + if (maxWidth >= 0) { + return maxWidth; + } else { + return Double.MAX_VALUE; + } + } + + /** + * Returns the minimum width of the column, or {@link Double#MIN_VALUE} + * if defined as negative. + */ + private double getMinWidth(Column<?, ?> column) { + double minWidth = column.getMinimumWidth(); + if (minWidth >= 0) { + return minWidth; + } else { + return Double.MIN_VALUE; + } + } + + /** + * Check whether the auto width calculation is currently scheduled. + * + * @return <code>true</code> if auto width calculation is currently + * scheduled + */ + public boolean isScheduled() { + return isScheduled; + } + } + + /** + * Escalator used internally by grid to render the rows + */ + private Escalator escalator = GWT.create(Escalator.class); + + private final Header header = GWT.create(Header.class); + + private final Footer footer = GWT.create(Footer.class); + + /** + * List of columns in the grid. Order defines the visible order. + */ + private List<Column<?, T>> columns = new ArrayList<Column<?, T>>(); + + /** + * The datasource currently in use. <em>Note:</em> it is <code>null</code> + * on initialization, but not after that. + */ + private DataSource<T> dataSource; + + /** + * Currently available row range in DataSource. + */ + private Range currentDataAvailable = Range.withLength(0, 0); + + /** + * The number of frozen columns, 0 freezes the selection column if + * displayed, -1 also prevents selection col from freezing. + */ + private int frozenColumnCount = 0; + + /** + * Current sort order. The (private) sort() method reads this list to + * determine the order in which to present rows. + */ + private List<SortOrder> sortOrder = new ArrayList<SortOrder>(); + + private Renderer<Boolean> selectColumnRenderer = null; + + private SelectionColumn selectionColumn; + + private String rowStripeStyleName; + private String rowHasDataStyleName; + private String rowSelectedStyleName; + private String cellFocusStyleName; + private String rowFocusStyleName; + + /** + * Current selection model. + */ + private SelectionModel<T> selectionModel; + + protected final CellFocusHandler cellFocusHandler; + + private final UserSorter sorter = new UserSorter(); + + private final Editor<T> editor = GWT.create(Editor.class); + + private boolean dataIsBeingFetched = false; + + /** + * The cell a click event originated from + * <p> + * This is a workaround to make Chrome work like Firefox. In Chrome, + * normally if you start a drag on one cell and release on: + * <ul> + * <li>that same cell, the click event is that {@code <td>}. + * <li>a cell on that same row, the click event is the parent {@code <tr>}. + * <li>a cell on another row, the click event is the table section ancestor + * ({@code <thead>}, {@code <tbody>} or {@code <tfoot>}). + * </ul> + * + * @see #onBrowserEvent(Event) + */ + private Cell cellOnPrevMouseDown; + + /** + * A scheduled command to re-evaluate the widths of <em>all columns</em> + * that have calculated widths. Most probably called because + * minwidth/maxwidth/expandratio has changed. + */ + private final AutoColumnWidthsRecalculator autoColumnWidthsRecalculator = new AutoColumnWidthsRecalculator(); + + private boolean enabled = true; + + /** + * Enumeration for easy setting of selection mode. + */ + public enum SelectionMode { + + /** + * Shortcut for {@link SelectionModelSingle}. + */ + SINGLE { + + @Override + protected <T> SelectionModel<T> createModel() { + return new SelectionModelSingle<T>(); + } + }, + + /** + * Shortcut for {@link SelectionModelMulti}. + */ + MULTI { + + @Override + protected <T> SelectionModel<T> createModel() { + return new SelectionModelMulti<T>(); + } + }, + + /** + * Shortcut for {@link SelectionModelNone}. + */ + NONE { + + @Override + protected <T> SelectionModel<T> createModel() { + return new SelectionModelNone<T>(); + } + }; + + protected abstract <T> SelectionModel<T> createModel(); + } + + /** + * Base class for grid columns internally used by the Grid. The user should + * use {@link Column} when creating new columns. + * + * @param <C> + * the column type + * + * @param <T> + * the row type + */ + public static abstract class Column<C, T> { + + /** + * The default renderer for grid columns. + * <p> + * The first time this renderer is called, a warning is displayed, + * informing the developer to use a manually defined renderer for their + * column. + */ + private final class DefaultObjectRenderer extends ObjectRenderer { + boolean warned = false; + private final String DEFAULT_RENDERER_WARNING = "This column uses " + + "a dummy default ObjectRenderer. A more suitable " + + "renderer should be set using the setRenderer() " + + "method."; + + @Override + public void render(RendererCellReference cell, Object data) { + if (!warned && !(data instanceof String)) { + getLogger().warning( + Column.this.toString() + ": " + + DEFAULT_RENDERER_WARNING); + warned = true; + } + + super.render(cell, data); + } + } + + /** + * the column is associated with + */ + private Grid<T> grid; + + /** + * Width of column in pixels as {@link #setWidth(double)} has been + * called + */ + private double widthUser = GridConstants.DEFAULT_COLUMN_WIDTH_PX; + + /** + * Renderer for rendering a value into the cell + */ + private Renderer<? super C> bodyRenderer; + + private boolean sortable = false; + + private String headerCaption = ""; + + private double minimumWidthPx = GridConstants.DEFAULT_MIN_WIDTH; + private double maximumWidthPx = GridConstants.DEFAULT_MAX_WIDTH; + private int expandRatio = GridConstants.DEFAULT_EXPAND_RATIO; + + /** + * Constructs a new column with a simple TextRenderer. + */ + public Column() { + setRenderer(new DefaultObjectRenderer()); + } + + /** + * Constructs a new column with a simple TextRenderer. + * + * @param caption + * The header caption for this column + * + * @throws IllegalArgumentException + * if given header caption is null + */ + public Column(String caption) throws IllegalArgumentException { + this(); + setHeaderCaption(caption); + } + + /** + * Constructs a new column with a custom renderer. + * + * @param renderer + * The renderer to use for rendering the cells + * + * @throws IllegalArgumentException + * if given Renderer is null + */ + public Column(Renderer<? super C> renderer) + throws IllegalArgumentException { + setRenderer(renderer); + } + + /** + * Constructs a new column with a custom renderer. + * + * @param renderer + * The renderer to use for rendering the cells + * @param caption + * The header caption for this column + * + * @throws IllegalArgumentException + * if given Renderer or header caption is null + */ + public Column(String caption, Renderer<? super C> renderer) + throws IllegalArgumentException { + this(renderer); + setHeaderCaption(caption); + } + + /** + * Internally used by the grid to set itself + * + * @param grid + */ + private void setGrid(Grid<T> grid) { + if (this.grid != null && grid != null) { + // Trying to replace grid + throw new IllegalStateException("Column already is attached " + + "to a grid. Remove the column first from the grid " + + "and then add it. (in: " + toString() + ")"); + } + + if (this.grid != null) { + this.grid.autoColumnWidthsRecalculator.schedule(); + } + this.grid = grid; + if (this.grid != null) { + this.grid.autoColumnWidthsRecalculator.schedule(); + updateHeader(); + } + } + + /** + * Sets a header caption for this column. + * + * @param caption + * The header caption for this column + * @return the column itself + * + * @throws IllegalArgumentException + * if given caption text is null + */ + public Column<C, T> setHeaderCaption(String caption) { + if (caption == null) { + throw new IllegalArgumentException("Caption cannot be null."); + } + + if (!this.headerCaption.equals(caption)) { + this.headerCaption = caption; + if (grid != null) { + updateHeader(); + } + } + + return this; + } + + private void updateHeader() { + HeaderRow row = grid.getHeader().getDefaultRow(); + if (row != null) { + row.getCell(this).setText(headerCaption); + } + } + + /** + * Returns the data that should be rendered into the cell. By default + * returning Strings and Widgets are supported. If the return type is a + * String then it will be treated as preformatted text. + * <p> + * To support other types you will need to pass a custom renderer to the + * column via the column constructor. + * + * @param row + * The row object that provides the cell content. + * + * @return The cell content + */ + public abstract C getValue(T row); + + /** + * The renderer to render the cell width. By default renders the data as + * a String or adds the widget into the cell if the column type is of + * widget type. + * + * @return The renderer to render the cell content with + */ + public Renderer<? super C> getRenderer() { + return bodyRenderer; + } + + /** + * Sets a custom {@link Renderer} for this column. + * + * @param renderer + * The renderer to use for rendering the cells + * @return the column itself + * + * @throws IllegalArgumentException + * if given Renderer is null + */ + public Column<C, T> setRenderer(Renderer<? super C> renderer) + throws IllegalArgumentException { + if (renderer == null) { + throw new IllegalArgumentException("Renderer cannot be null."); + } + bodyRenderer = renderer; + + if (grid != null) { + grid.refreshBody(); + } + + return this; + } + + /** + * Sets the pixel width of the column. Use a negative value for the grid + * to autosize column based on content and available space. + * <p> + * This action is done "finally", once the current execution loop + * returns. This is done to reduce overhead of unintentionally always + * recalculate all columns, when modifying several columns at once. + * + * @param pixels + * the width in pixels or negative for auto sizing + */ + public Column<C, T> setWidth(double pixels) { + if (widthUser != pixels) { + widthUser = pixels; + scheduleColumnWidthRecalculator(); + } + return this; + } + + void doSetWidth(double pixels) { + if (grid != null) { + int index = grid.columns.indexOf(this); + ColumnConfiguration conf = grid.escalator + .getColumnConfiguration(); + conf.setColumnWidth(index, pixels); + } + } + + /** + * Returns the pixel width of the column as given by the user. + * <p> + * <em>Note:</em> If a negative value was given to + * {@link #setWidth(double)}, that same negative value is returned here. + * + * @return pixel width of the column, or a negative number if the column + * width has been automatically calculated. + * @see #setWidth(double) + * @see #getWidthActual() + */ + public double getWidth() { + return widthUser; + } + + /** + * Returns the effective pixel width of the column. + * <p> + * This differs from {@link #getWidth()} only when the column has been + * automatically resized. + * + * @return pixel width of the column. + */ + public double getWidthActual() { + return grid.escalator.getColumnConfiguration() + .getColumnWidthActual(grid.columns.indexOf(this)); + } + + void reapplyWidth() { + setWidth(getWidth()); + } + + /** + * Enables sort indicators for the grid. + * <p> + * <b>Note:</b>The API can still sort the column even if this is set to + * <code>false</code>. + * + * @param sortable + * <code>true</code> when column sort indicators are visible. + * @return the column itself + */ + public Column<C, T> setSortable(boolean sortable) { + if (this.sortable != sortable) { + this.sortable = sortable; + if (grid != null) { + grid.refreshHeader(); + } + } + + return this; + } + + /** + * Are sort indicators shown for the column. + * + * @return <code>true</code> if the column is sortable + */ + public boolean isSortable() { + return sortable; + } + + @Override + public String toString() { + String details = ""; + + if (headerCaption != null && !headerCaption.isEmpty()) { + details += "header:\"" + headerCaption + "\" "; + } else { + details += "header:empty "; + } + + if (grid != null) { + int index = grid.getColumns().indexOf(this); + if (index != -1) { + details += "attached:#" + index + " "; + } else { + details += "attached:unindexed "; + } + } else { + details += "detached "; + } + + details += "sortable:" + sortable + " "; + + return getClass().getSimpleName() + "[" + details.trim() + "]"; + } + + /** + * Sets the minimum width for this column. + * <p> + * This defines the minimum guaranteed pixel width of the column + * <em>when it is set to expand</em>. + * <p> + * This action is done "finally", once the current execution loop + * returns. This is done to reduce overhead of unintentionally always + * recalculate all columns, when modifying several columns at once. + * + * @param pixels + * the minimum width + * @return this column + */ + public Column<C, T> setMinimumWidth(double pixels) { + final double maxwidth = getMaximumWidth(); + if (pixels >= 0 && pixels > maxwidth && maxwidth >= 0) { + throw new IllegalArgumentException("New minimum width (" + + pixels + ") was greater than maximum width (" + + maxwidth + ")"); + } + + if (minimumWidthPx != pixels) { + minimumWidthPx = pixels; + scheduleColumnWidthRecalculator(); + } + return this; + } + + /** + * Sets the maximum width for this column. + * <p> + * This defines the maximum allowed pixel width of the column + * <em>when it is set to expand</em>. + * <p> + * This action is done "finally", once the current execution loop + * returns. This is done to reduce overhead of unintentionally always + * recalculate all columns, when modifying several columns at once. + * + * @param pixels + * the maximum width + * @param immediately + * <code>true</code> if the widths should be executed + * immediately (ignoring lazy loading completely), or + * <code>false</code> if the command should be run after a + * while (duplicate non-immediately invocations are ignored). + * @return this column + */ + public Column<C, T> setMaximumWidth(double pixels) { + final double minwidth = getMinimumWidth(); + if (pixels >= 0 && pixels < minwidth && minwidth >= 0) { + throw new IllegalArgumentException("New maximum width (" + + pixels + ") was less than minimum width (" + minwidth + + ")"); + } + + if (maximumWidthPx != pixels) { + maximumWidthPx = pixels; + scheduleColumnWidthRecalculator(); + } + return this; + } + + /** + * Sets the ratio with which the column expands. + * <p> + * By default, all columns expand equally (treated as if all of them had + * an expand ratio of 1). Once at least one column gets a defined expand + * ratio, the implicit expand ratio is removed, and only the defined + * expand ratios are taken into account. + * <p> + * If a column has a defined width ({@link #setWidth(double)}), it + * overrides this method's effects. + * <p> + * <em>Example:</em> A grid with three columns, with expand ratios 0, 1 + * and 2, respectively. The column with a <strong>ratio of 0 is exactly + * as wide as its contents requires</strong>. The column with a ratio of + * 1 is as wide as it needs, <strong>plus a third of any excess + * space</strong>, bceause we have 3 parts total, and this column + * reservs only one of those. The column with a ratio of 2, is as wide + * as it needs to be, <strong>plus two thirds</strong> of the excess + * width. + * <p> + * This action is done "finally", once the current execution loop + * returns. This is done to reduce overhead of unintentionally always + * recalculate all columns, when modifying several columns at once. + * + * @param expandRatio + * the expand ratio of this column. {@code 0} to not have it + * expand at all. A negative number to clear the expand + * value. + * @return this column + */ + public Column<C, T> setExpandRatio(int ratio) { + if (expandRatio != ratio) { + expandRatio = ratio; + scheduleColumnWidthRecalculator(); + } + return this; + } + + /** + * Clears the column's expand ratio. + * <p> + * Same as calling {@link #setExpandRatio(int) setExpandRatio(-1)} + * + * @return this column + */ + public Column<C, T> clearExpandRatio() { + return setExpandRatio(-1); + } + + /** + * Gets the minimum width for this column. + * + * @return the minimum width for this column + * @see #setMinimumWidth(double) + */ + public double getMinimumWidth() { + return minimumWidthPx; + } + + /** + * Gets the maximum width for this column. + * + * @return the maximum width for this column + * @see #setMaximumWidth(double) + */ + public double getMaximumWidth() { + return maximumWidthPx; + } + + /** + * Gets the expand ratio for this column. + * + * @return the expand ratio for this column + * @see #setExpandRatio(int) + */ + public int getExpandRatio() { + return expandRatio; + } + + private void scheduleColumnWidthRecalculator() { + if (grid != null) { + grid.autoColumnWidthsRecalculator.schedule(); + } else { + /* + * NOOP + * + * Since setGrid() will call reapplyWidths as the colum is + * attached to a grid, it will call setWidth, which, in turn, + * will call this method again. Therefore, it's guaranteed that + * the recalculation is scheduled eventually, once the column is + * attached to a grid. + */ + } + } + } + + protected class BodyUpdater implements EscalatorUpdater { + + @Override + public void preAttach(Row row, Iterable<FlyweightCell> cellsToAttach) { + int rowIndex = row.getRow(); + rowReference.set(rowIndex, getDataSource().getRow(rowIndex), + row.getElement()); + for (FlyweightCell cell : cellsToAttach) { + Renderer<?> renderer = findRenderer(cell); + if (renderer instanceof ComplexRenderer) { + try { + rendererCellReference.set(cell, + getColumn(cell.getColumn())); + ((ComplexRenderer<?>) renderer) + .init(rendererCellReference); + } catch (RuntimeException e) { + getLogger().log( + Level.SEVERE, + "Error initing cell in column " + + cell.getColumn(), e); + } + } + } + } + + @Override + public void postAttach(Row row, Iterable<FlyweightCell> attachedCells) { + for (FlyweightCell cell : attachedCells) { + Renderer<?> renderer = findRenderer(cell); + if (renderer instanceof WidgetRenderer) { + try { + WidgetRenderer<?, ?> widgetRenderer = (WidgetRenderer<?, ?>) renderer; + + Widget widget = widgetRenderer.createWidget(); + assert widget != null : "WidgetRenderer.createWidget() returned null. It should return a widget."; + assert widget.getParent() == null : "WidgetRenderer.createWidget() returned a widget which already is attached."; + assert cell.getElement().getChildCount() == 0 : "Cell content should be empty when adding Widget"; + + // Physical attach + cell.getElement().appendChild(widget.getElement()); + + // Logical attach + setParent(widget, Grid.this); + } catch (RuntimeException e) { + getLogger().log( + Level.SEVERE, + "Error attaching child widget in column " + + cell.getColumn(), e); + } + } + } + } + + @Override + public void update(Row row, Iterable<FlyweightCell> cellsToUpdate) { + int rowIndex = row.getRow(); + TableRowElement rowElement = row.getElement(); + T rowData = dataSource.getRow(rowIndex); + + boolean hasData = rowData != null; + + /* + * TODO could be more efficient to build a list of all styles that + * should be used and update the element only once instead of + * attempting to update only the ones that have changed. + */ + + // Assign stylename for rows with data + boolean usedToHaveData = rowElement + .hasClassName(rowHasDataStyleName); + + if (usedToHaveData != hasData) { + setStyleName(rowElement, rowHasDataStyleName, hasData); + } + + boolean isEvenIndex = (row.getRow() % 2 == 0); + setStyleName(rowElement, rowStripeStyleName, !isEvenIndex); + + rowReference.set(rowIndex, rowData, rowElement); + + if (hasData) { + setStyleName(rowElement, rowSelectedStyleName, + isSelected(rowData)); + + if (rowStyleGenerator != null) { + try { + String rowStylename = rowStyleGenerator + .getStyle(rowReference); + setCustomStyleName(rowElement, rowStylename); + } catch (RuntimeException e) { + getLogger().log( + Level.SEVERE, + "Error generating styles for row " + + row.getRow(), e); + } + } else { + // Remove in case there was a generator previously + setCustomStyleName(rowElement, null); + } + } else if (usedToHaveData) { + setStyleName(rowElement, rowSelectedStyleName, false); + + setCustomStyleName(rowElement, null); + } + + cellFocusHandler.updateFocusedRowStyle(row); + + for (FlyweightCell cell : cellsToUpdate) { + Column<?, T> column = getColumn(cell.getColumn()); + + assert column != null : "Column was not found from cell (" + + cell.getColumn() + "," + cell.getRow() + ")"; + + cellFocusHandler.updateFocusedCellStyle(cell, + escalator.getBody()); + + if (hasData && cellStyleGenerator != null) { + try { + cellReference.set(cell.getColumn(), column); + String generatedStyle = cellStyleGenerator + .getStyle(cellReference); + setCustomStyleName(cell.getElement(), generatedStyle); + } catch (RuntimeException e) { + getLogger().log( + Level.SEVERE, + "Error generating style for cell in column " + + cell.getColumn(), e); + } + } else if (hasData || usedToHaveData) { + setCustomStyleName(cell.getElement(), null); + } + + Renderer renderer = column.getRenderer(); + + try { + rendererCellReference.set(cell, column); + if (renderer instanceof ComplexRenderer) { + // Hide cell content if needed + ComplexRenderer clxRenderer = (ComplexRenderer) renderer; + if (hasData) { + if (!usedToHaveData) { + // Prepare cell for rendering + clxRenderer.setContentVisible( + rendererCellReference, true); + } + + Object value = column.getValue(rowData); + clxRenderer.render(rendererCellReference, value); + + } else { + // Prepare cell for no data + clxRenderer.setContentVisible( + rendererCellReference, false); + } + + } else if (hasData) { + // Simple renderers just render + Object value = column.getValue(rowData); + renderer.render(rendererCellReference, value); + + } else { + // Clear cell if there is no data + cell.getElement().removeAllChildren(); + } + } catch (RuntimeException e) { + getLogger().log( + Level.SEVERE, + "Error rendering cell in column " + + cell.getColumn(), e); + } + } + } + + @Override + public void preDetach(Row row, Iterable<FlyweightCell> cellsToDetach) { + for (FlyweightCell cell : cellsToDetach) { + Renderer renderer = findRenderer(cell); + if (renderer instanceof WidgetRenderer) { + try { + Widget w = WidgetUtil.findWidget(cell.getElement() + .getFirstChildElement(), Widget.class); + if (w != null) { + + // Logical detach + setParent(w, null); + + // Physical detach + cell.getElement().removeChild(w.getElement()); + } + } catch (RuntimeException e) { + getLogger().log( + Level.SEVERE, + "Error detaching widget in column " + + cell.getColumn(), e); + } + } + } + } + + @Override + public void postDetach(Row row, Iterable<FlyweightCell> detachedCells) { + int rowIndex = row.getRow(); + // Passing null row data since it might not exist in the data source + // any more + rowReference.set(rowIndex, null, row.getElement()); + for (FlyweightCell cell : detachedCells) { + Renderer renderer = findRenderer(cell); + if (renderer instanceof ComplexRenderer) { + try { + rendererCellReference.set(cell, + getColumn(cell.getColumn())); + ((ComplexRenderer) renderer) + .destroy(rendererCellReference); + } catch (RuntimeException e) { + getLogger().log( + Level.SEVERE, + "Error destroying cell in column " + + cell.getColumn(), e); + } + } + } + } + } + + protected class StaticSectionUpdater implements EscalatorUpdater { + + private StaticSection<?> section; + private RowContainer container; + + public StaticSectionUpdater(StaticSection<?> section, + RowContainer container) { + super(); + this.section = section; + this.container = container; + } + + @Override + public void update(Row row, Iterable<FlyweightCell> cellsToUpdate) { + StaticSection.StaticRow<?> staticRow = section.getRow(row.getRow()); + final List<Column<?, T>> columns = getColumns(); + + setCustomStyleName(row.getElement(), staticRow.getStyleName()); + + for (FlyweightCell cell : cellsToUpdate) { + final StaticSection.StaticCell metadata = staticRow + .getCell(columns.get(cell.getColumn())); + + // Decorate default row with sorting indicators + if (staticRow instanceof HeaderRow) { + addSortingIndicatorsToHeaderRow((HeaderRow) staticRow, cell); + } + + // Assign colspan to cell before rendering + cell.setColSpan(metadata.getColspan()); + + TableCellElement element = cell.getElement(); + switch (metadata.getType()) { + case TEXT: + element.setInnerText(metadata.getText()); + break; + case HTML: + element.setInnerHTML(metadata.getHtml()); + break; + case WIDGET: + preDetach(row, Arrays.asList(cell)); + element.setInnerHTML(""); + postAttach(row, Arrays.asList(cell)); + break; + } + setCustomStyleName(element, metadata.getStyleName()); + + cellFocusHandler.updateFocusedCellStyle(cell, container); + } + } + + private void addSortingIndicatorsToHeaderRow(HeaderRow headerRow, + FlyweightCell cell) { + + cleanup(cell); + + Column<?, ?> column = getColumn(cell.getColumn()); + SortOrder sortingOrder = getSortOrder(column); + if (!headerRow.isDefault() || !column.isSortable() + || sortingOrder == null) { + // Only apply sorting indicators to sortable header columns in + // the default header row + return; + } + + Element cellElement = cell.getElement(); + + if (SortDirection.ASCENDING == sortingOrder.getDirection()) { + cellElement.addClassName("sort-asc"); + } else { + cellElement.addClassName("sort-desc"); + } + + int sortIndex = Grid.this.getSortOrder().indexOf(sortingOrder); + if (sortIndex > -1 && Grid.this.getSortOrder().size() > 1) { + // Show sort order indicator if column is + // sorted and other sorted columns also exists. + cellElement.setAttribute("sort-order", + String.valueOf(sortIndex + 1)); + } + } + + /** + * Finds the sort order for this column + */ + private SortOrder getSortOrder(Column<?, ?> column) { + for (SortOrder order : Grid.this.getSortOrder()) { + if (order.getColumn() == column) { + return order; + } + } + return null; + } + + private void cleanup(FlyweightCell cell) { + Element cellElement = cell.getElement(); + cellElement.removeAttribute("sort-order"); + cellElement.removeClassName("sort-desc"); + cellElement.removeClassName("sort-asc"); + } + + @Override + public void preAttach(Row row, Iterable<FlyweightCell> cellsToAttach) { + } + + @Override + public void postAttach(Row row, Iterable<FlyweightCell> attachedCells) { + StaticSection.StaticRow<?> gridRow = section.getRow(row.getRow()); + List<Column<?, T>> columns = getColumns(); + + for (FlyweightCell cell : attachedCells) { + StaticSection.StaticCell metadata = gridRow.getCell(columns + .get(cell.getColumn())); + /* + * If the cell contains widgets that are not currently attach + * then attach them now. + */ + if (GridStaticCellType.WIDGET.equals(metadata.getType())) { + final Widget widget = metadata.getWidget(); + final Element cellElement = cell.getElement(); + + if (!widget.isAttached()) { + + // Physical attach + cellElement.appendChild(widget.getElement()); + + // Logical attach + setParent(widget, Grid.this); + } + } + } + } + + @Override + public void preDetach(Row row, Iterable<FlyweightCell> cellsToDetach) { + if (section.getRowCount() > row.getRow()) { + StaticSection.StaticRow<?> gridRow = section.getRow(row + .getRow()); + List<Column<?, T>> columns = getColumns(); + for (FlyweightCell cell : cellsToDetach) { + StaticSection.StaticCell metadata = gridRow.getCell(columns + .get(cell.getColumn())); + + if (GridStaticCellType.WIDGET.equals(metadata.getType()) + && metadata.getWidget().isAttached()) { + + Widget widget = metadata.getWidget(); + + // Logical detach + setParent(widget, null); + + // Physical detach + widget.getElement().removeFromParent(); + } + } + } + } + + @Override + public void postDetach(Row row, Iterable<FlyweightCell> detachedCells) { + } + }; + + /** + * Creates a new instance. + */ + public Grid() { + initWidget(escalator); + getElement().setTabIndex(0); + cellFocusHandler = new CellFocusHandler(); + + setStylePrimaryName("v-grid"); + + escalator.getHeader().setEscalatorUpdater(createHeaderUpdater()); + escalator.getBody().setEscalatorUpdater(createBodyUpdater()); + escalator.getFooter().setEscalatorUpdater(createFooterUpdater()); + + header.setGrid(this); + HeaderRow defaultRow = header.appendRow(); + header.setDefaultRow(defaultRow); + + footer.setGrid(this); + + editor.setGrid(this); + + setSelectionMode(SelectionMode.SINGLE); + + escalator.addScrollHandler(new ScrollHandler() { + @Override + public void onScroll(ScrollEvent event) { + fireEvent(new ScrollEvent()); + } + }); + + escalator + .addRowVisibilityChangeHandler(new RowVisibilityChangeHandler() { + @Override + public void onRowVisibilityChange( + RowVisibilityChangeEvent event) { + if (dataSource != null && dataSource.size() != 0) { + dataIsBeingFetched = true; + dataSource.ensureAvailability( + event.getFirstVisibleRow(), + event.getVisibleRowCount()); + } + } + }); + + // Default action on SelectionEvents. Refresh the body so changed + // become visible. + addSelectionHandler(new SelectionHandler<T>() { + + @Override + public void onSelect(SelectionEvent<T> event) { + refreshBody(); + } + }); + + // Sink header events and key events + sinkEvents(getHeader().getConsumedEvents()); + sinkEvents(Arrays.asList(BrowserEvents.KEYDOWN, BrowserEvents.KEYUP, + BrowserEvents.KEYPRESS, BrowserEvents.DBLCLICK, + BrowserEvents.MOUSEDOWN)); + + // Make ENTER and SHIFT+ENTER in the header perform sorting + addHeaderKeyUpHandler(new HeaderKeyUpHandler() { + @Override + public void onKeyUp(GridKeyUpEvent event) { + if (event.getNativeKeyCode() != KeyCodes.KEY_ENTER) { + return; + } + + sorter.sort(event.getFocusedCell().getColumn(), + event.isShiftKeyDown()); + } + }); + + addDataAvailableHandler(new DataAvailableHandler() { + @Override + public void onDataAvailable(DataAvailableEvent event) { + dataIsBeingFetched = false; + } + }); + } + + @Override + public boolean isEnabled() { + return enabled; + } + + @Override + public void setEnabled(boolean enabled) { + if (enabled == this.enabled) { + return; + } + + this.enabled = enabled; + getElement().setTabIndex(enabled ? 0 : -1); + + // Editor save and cancel buttons need to be disabled. + boolean editorOpen = editor.getState() != State.INACTIVE; + if (editorOpen) { + editor.setGridEnabled(enabled); + } + + getEscalator().setScrollLocked(Direction.VERTICAL, + !enabled || editorOpen); + getEscalator().setScrollLocked(Direction.HORIZONTAL, !enabled); + } + + @Override + public void setStylePrimaryName(String style) { + super.setStylePrimaryName(style); + escalator.setStylePrimaryName(style); + editor.setStylePrimaryName(style); + + String rowStyle = getStylePrimaryName() + "-row"; + rowHasDataStyleName = rowStyle + "-has-data"; + rowSelectedStyleName = rowStyle + "-selected"; + rowStripeStyleName = rowStyle + "-stripe"; + + cellFocusStyleName = getStylePrimaryName() + "-cell-focused"; + rowFocusStyleName = getStylePrimaryName() + "-row-focused"; + + if (isAttached()) { + refreshHeader(); + refreshBody(); + refreshFooter(); + } + } + + /** + * Creates the escalator updater used to update the header rows in this + * grid. The updater is invoked when header rows or columns are added or + * removed, or the content of existing header cells is changed. + * + * @return the new header updater instance + * + * @see GridHeader + * @see Grid#getHeader() + */ + protected EscalatorUpdater createHeaderUpdater() { + return new StaticSectionUpdater(header, escalator.getHeader()); + } + + /** + * Creates the escalator updater used to update the body rows in this grid. + * The updater is invoked when body rows or columns are added or removed, + * the content of body cells is changed, or the body is scrolled to expose + * previously hidden content. + * + * @return the new body updater instance + */ + protected EscalatorUpdater createBodyUpdater() { + return new BodyUpdater(); + } + + /** + * Creates the escalator updater used to update the footer rows in this + * grid. The updater is invoked when header rows or columns are added or + * removed, or the content of existing header cells is changed. + * + * @return the new footer updater instance + * + * @see GridFooter + * @see #getFooter() + */ + protected EscalatorUpdater createFooterUpdater() { + return new StaticSectionUpdater(footer, escalator.getFooter()); + } + + /** + * Refreshes header or footer rows on demand + * + * @param rows + * The row container + * @param firstRowIsVisible + * is the first row visible + * @param isHeader + * <code>true</code> if we refreshing the header, else assumed + * the footer + */ + private void refreshRowContainer(RowContainer rows, StaticSection<?> section) { + + // Add or Remove rows on demand + int rowDiff = section.getVisibleRowCount() - rows.getRowCount(); + if (rowDiff > 0) { + rows.insertRows(0, rowDiff); + } else if (rowDiff < 0) { + rows.removeRows(0, -rowDiff); + } + + // Refresh all the rows + if (rows.getRowCount() > 0) { + rows.refreshRows(0, rows.getRowCount()); + } + } + + /** + * Refreshes all header rows + */ + void refreshHeader() { + refreshRowContainer(escalator.getHeader(), header); + } + + /** + * Refreshes all body rows + */ + private void refreshBody() { + escalator.getBody().refreshRows(0, escalator.getBody().getRowCount()); + } + + /** + * Refreshes all footer rows + */ + void refreshFooter() { + refreshRowContainer(escalator.getFooter(), footer); + } + + /** + * Adds columns as the last columns in the grid. + * + * @param columns + * the columns to add + */ + public void addColumns(Column<?, T>... columns) { + int count = getColumnCount(); + for (Column<?, T> column : columns) { + addColumn(column, count++); + } + } + + /** + * Adds a column as the last column in the grid. + * + * @param column + * the column to add + * @return given column + */ + public <C extends Column<?, T>> C addColumn(C column) { + addColumn(column, getColumnCount()); + return column; + } + + /** + * Inserts a column into a specific position in the grid. + * + * @param index + * the index where the column should be inserted into + * @param column + * the column to add + * @return given column + * + * @throws IllegalStateException + * if Grid's current selection model renders a selection column, + * and {@code index} is 0. + */ + public <C extends Column<?, T>> C addColumn(C column, int index) { + if (column == selectionColumn) { + throw new IllegalArgumentException("The selection column many " + + "not be added manually"); + } else if (selectionColumn != null && index == 0) { + throw new IllegalStateException("A column cannot be inserted " + + "before the selection column"); + } + + addColumnSkipSelectionColumnCheck(column, index); + return column; + } + + private void addColumnSkipSelectionColumnCheck(Column<?, T> column, + int index) { + // Register column with grid + columns.add(index, column); + + header.addColumn(column); + footer.addColumn(column); + + // Register this grid instance with the column + ((Column<?, T>) column).setGrid(this); + + // Add to escalator + escalator.getColumnConfiguration().insertColumns(index, 1); + + // Reapply column width + column.reapplyWidth(); + + // Sink all renderer events + Set<String> events = new HashSet<String>(); + events.addAll(getConsumedEventsForRenderer(column.getRenderer())); + + sinkEvents(events); + } + + private void sinkEvents(Collection<String> events) { + assert events != null; + + int eventsToSink = 0; + for (String typeName : events) { + int typeInt = Event.getTypeInt(typeName); + if (typeInt < 0) { + // Type not recognized by typeInt + sinkBitlessEvent(typeName); + } else { + eventsToSink |= typeInt; + } + } + + if (eventsToSink > 0) { + sinkEvents(eventsToSink); + } + } + + private Renderer<?> findRenderer(FlyweightCell cell) { + Column<?, T> column = getColumn(cell.getColumn()); + assert column != null : "Could not find column at index:" + + cell.getColumn(); + return column.getRenderer(); + } + + /** + * Removes a column from the grid. + * + * @param column + * the column to remove + */ + public void removeColumn(Column<?, T> column) { + if (column != null && column.equals(selectionColumn)) { + throw new IllegalArgumentException( + "The selection column may not be removed manually."); + } + + removeColumnSkipSelectionColumnCheck(column); + } + + private void removeColumnSkipSelectionColumnCheck(Column<?, T> column) { + int columnIndex = columns.indexOf(column); + + // Remove from column configuration + escalator.getColumnConfiguration().removeColumns(columnIndex, 1); + + updateFrozenColumns(); + + header.removeColumn(column); + footer.removeColumn(column); + + // de-register column with grid + ((Column<?, T>) column).setGrid(null); + + columns.remove(columnIndex); + } + + /** + * Returns the amount of columns in the grid. + * + * @return The number of columns in the grid + */ + public int getColumnCount() { + return columns.size(); + } + + /** + * Returns a list of columns in the grid. + * + * @return A unmodifiable list of the columns in the grid + */ + public List<Column<?, T>> getColumns() { + return Collections + .unmodifiableList(new ArrayList<Column<?, T>>(columns)); + } + + /** + * Returns a column by its index in the grid. + * + * @param index + * the index of the column + * @return The column in the given index + * @throws IllegalArgumentException + * if the column index does not exist in the grid + */ + public Column<?, T> getColumn(int index) throws IllegalArgumentException { + if (index < 0 || index >= columns.size()) { + throw new IllegalStateException("Column not found."); + } + return columns.get(index); + } + + /** + * Returns current index of given column + * + * @param column + * column in grid + * @return column index, or <code>-1</code> if not in this Grid + */ + protected int indexOfColumn(Column<?, T> column) { + return columns.indexOf(column); + } + + /** + * Returns the header section of this grid. The default header contains a + * single row displaying the column captions. + * + * @return the header + */ + protected Header getHeader() { + return header; + } + + /** + * Gets the header row at given index. + * + * @param rowIndex + * 0 based index for row. Counted from top to bottom + * @return header row at given index + * @throws IllegalArgumentException + * if no row exists at given index + */ + public HeaderRow getHeaderRow(int rowIndex) { + return header.getRow(rowIndex); + } + + /** + * Inserts a new row at the given position to the header section. Shifts the + * row currently at that position and any subsequent rows down (adds one to + * their indices). + * + * @param index + * the position at which to insert the row + * @return the new row + * + * @throws IllegalArgumentException + * if the index is less than 0 or greater than row count + * @see #appendHeaderRow() + * @see #prependHeaderRow() + * @see #removeHeaderRow(HeaderRow) + * @see #removeHeaderRow(int) + */ + public HeaderRow addHeaderRowAt(int index) { + return header.addRowAt(index); + } + + /** + * Adds a new row at the bottom of the header section. + * + * @return the new row + * @see #prependHeaderRow() + * @see #addHeaderRowAt(int) + * @see #removeHeaderRow(HeaderRow) + * @see #removeHeaderRow(int) + */ + public HeaderRow appendHeaderRow() { + return header.appendRow(); + } + + /** + * Returns the current default row of the header section. The default row is + * a special header row providing a user interface for sorting columns. + * Setting a header caption for column updates cells in the default header. + * + * @return the default row or null if no default row set + */ + public HeaderRow getDefaultHeaderRow() { + return header.getDefaultRow(); + } + + /** + * Gets the row count for the header section. + * + * @return row count + */ + public int getHeaderRowCount() { + return header.getRowCount(); + } + + /** + * Adds a new row at the top of the header section. + * + * @return the new row + * @see #appendHeaderRow() + * @see #addHeaderRowAt(int) + * @see #removeHeaderRow(HeaderRow) + * @see #removeHeaderRow(int) + */ + public HeaderRow prependHeaderRow() { + return header.prependRow(); + } + + /** + * Removes the given row from the header section. + * + * @param row + * the row to be removed + * + * @throws IllegalArgumentException + * if the row does not exist in this section + * @see #removeHeaderRow(int) + * @see #addHeaderRowAt(int) + * @see #appendHeaderRow() + * @see #prependHeaderRow() + */ + public void removeHeaderRow(HeaderRow row) { + header.removeRow(row); + } + + /** + * Removes the row at the given position from the header section. + * + * @param index + * the position of the row + * + * @throws IllegalArgumentException + * if no row exists at given index + * @see #removeHeaderRow(HeaderRow) + * @see #addHeaderRowAt(int) + * @see #appendHeaderRow() + * @see #prependHeaderRow() + */ + public void removeHeaderRow(int rowIndex) { + header.removeRow(rowIndex); + } + + /** + * Sets the default row of the header. The default row is a special header + * row providing a user interface for sorting columns. + * + * @param row + * the new default row, or null for no default row + * + * @throws IllegalArgumentException + * header does not contain the row + */ + public void setDefaultHeaderRow(HeaderRow row) { + header.setDefaultRow(row); + } + + /** + * Sets the visibility of the header section. + * + * @param visible + * true to show header section, false to hide + */ + public void setHeaderVisible(boolean visible) { + header.setVisible(visible); + } + + /** + * Returns the visibility of the header section. + * + * @return true if visible, false otherwise. + */ + public boolean isHeaderVisible() { + return header.isVisible(); + } + + /* Grid Footers */ + + /** + * Returns the footer section of this grid. The default footer is empty. + * + * @return the footer + */ + protected Footer getFooter() { + return footer; + } + + /** + * Gets the footer row at given index. + * + * @param rowIndex + * 0 based index for row. Counted from top to bottom + * @return footer row at given index + * @throws IllegalArgumentException + * if no row exists at given index + */ + public FooterRow getFooterRow(int rowIndex) { + return footer.getRow(rowIndex); + } + + /** + * Inserts a new row at the given position to the footer section. Shifts the + * row currently at that position and any subsequent rows down (adds one to + * their indices). + * + * @param index + * the position at which to insert the row + * @return the new row + * + * @throws IllegalArgumentException + * if the index is less than 0 or greater than row count + * @see #appendFooterRow() + * @see #prependFooterRow() + * @see #removeFooterRow(FooterRow) + * @see #removeFooterRow(int) + */ + public FooterRow addFooterRowAt(int index) { + return footer.addRowAt(index); + } + + /** + * Adds a new row at the bottom of the footer section. + * + * @return the new row + * @see #prependFooterRow() + * @see #addFooterRowAt(int) + * @see #removeFooterRow(FooterRow) + * @see #removeFooterRow(int) + */ + public FooterRow appendFooterRow() { + return footer.appendRow(); + } + + /** + * Gets the row count for the footer. + * + * @return row count + */ + public int getFooterRowCount() { + return footer.getRowCount(); + } + + /** + * Adds a new row at the top of the footer section. + * + * @return the new row + * @see #appendFooterRow() + * @see #addFooterRowAt(int) + * @see #removeFooterRow(FooterRow) + * @see #removeFooterRow(int) + */ + public FooterRow prependFooterRow() { + return footer.prependRow(); + } + + /** + * Removes the given row from the footer section. + * + * @param row + * the row to be removed + * + * @throws IllegalArgumentException + * if the row does not exist in this section + * @see #removeFooterRow(int) + * @see #addFooterRowAt(int) + * @see #appendFooterRow() + * @see #prependFooterRow() + */ + public void removeFooterRow(FooterRow row) { + footer.removeRow(row); + } + + /** + * Removes the row at the given position from the footer section. + * + * @param index + * the position of the row + * + * @throws IllegalArgumentException + * if no row exists at given index + * @see #removeFooterRow(FooterRow) + * @see #addFooterRowAt(int) + * @see #appendFooterRow() + * @see #prependFooterRow() + */ + public void removeFooterRow(int rowIndex) { + footer.removeRow(rowIndex); + } + + /** + * Sets the visibility of the footer section. + * + * @param visible + * true to show footer section, false to hide + */ + public void setFooterVisible(boolean visible) { + footer.setVisible(visible); + } + + /** + * Returns the visibility of the footer section. + * + * @return true if visible, false otherwise. + */ + public boolean isFooterVisible() { + return footer.isVisible(); + } + + protected Editor<T> getEditor() { + return editor; + } + + protected Escalator getEscalator() { + return escalator; + } + + /** + * {@inheritDoc} + * <p> + * <em>Note:</em> This method will change the widget's size in the browser + * only if {@link #getHeightMode()} returns {@link HeightMode#CSS}. + * + * @see #setHeightMode(HeightMode) + */ + @Override + public void setHeight(String height) { + escalator.setHeight(height); + } + + @Override + public void setWidth(String width) { + escalator.setWidth(width); + } + + /** + * Sets the data source used by this grid. + * + * @param dataSource + * the data source to use, not null + * @throws IllegalArgumentException + * if <code>dataSource</code> is <code>null</code> + */ + public void setDataSource(final DataSource<T> dataSource) + throws IllegalArgumentException { + if (dataSource == null) { + throw new IllegalArgumentException("dataSource can't be null."); + } + + selectionModel.reset(); + + if (this.dataSource != null) { + this.dataSource.setDataChangeHandler(null); + } + + this.dataSource = dataSource; + dataSource.setDataChangeHandler(new DataChangeHandler() { + @Override + public void dataUpdated(int firstIndex, int numberOfItems) { + escalator.getBody().refreshRows(firstIndex, numberOfItems); + } + + @Override + public void dataRemoved(int firstIndex, int numberOfItems) { + escalator.getBody().removeRows(firstIndex, numberOfItems); + Range removed = Range.withLength(firstIndex, numberOfItems); + cellFocusHandler.rowsRemovedFromBody(removed); + } + + @Override + public void dataAdded(int firstIndex, int numberOfItems) { + escalator.getBody().insertRows(firstIndex, numberOfItems); + Range added = Range.withLength(firstIndex, numberOfItems); + cellFocusHandler.rowsAddedToBody(added); + } + + @Override + public void dataAvailable(int firstIndex, int numberOfItems) { + currentDataAvailable = Range.withLength(firstIndex, + numberOfItems); + fireEvent(new DataAvailableEvent(currentDataAvailable)); + } + + @Override + public void resetDataAndSize(int newSize) { + RowContainer body = escalator.getBody(); + int oldSize = body.getRowCount(); + + if (newSize > oldSize) { + body.insertRows(oldSize, newSize - oldSize); + } else if (newSize < oldSize) { + body.removeRows(newSize, oldSize - newSize); + } + + if (newSize > 0) { + dataIsBeingFetched = true; + Range visibleRowRange = escalator.getVisibleRowRange(); + dataSource.ensureAvailability(visibleRowRange.getStart(), + visibleRowRange.length()); + } + + assert body.getRowCount() == newSize; + } + }); + + int previousRowCount = escalator.getBody().getRowCount(); + if (previousRowCount != 0) { + escalator.getBody().removeRows(0, previousRowCount); + } + + setEscalatorSizeFromDataSource(); + } + + private void setEscalatorSizeFromDataSource() { + assert escalator.getBody().getRowCount() == 0; + + int size = dataSource.size(); + if (size == -1 && isAttached()) { + // Exact size is not yet known, start with some reasonable guess + // just to get an initial backend request going + size = getEscalator().getMaxVisibleRowCount(); + } + if (size > 0) { + escalator.getBody().insertRows(0, size); + } + } + + /** + * Gets the {@Link DataSource} for this Grid. + * + * @return the data source used by this grid + */ + public DataSource<T> getDataSource() { + return dataSource; + } + + /** + * Sets the number of frozen columns in this grid. Setting the count to 0 + * means that no data columns will be frozen, but the built-in selection + * checkbox column will still be frozen if it's in use. Setting the count to + * -1 will also disable the selection column. + * <p> + * The default value is 0. + * + * @param numberOfColumns + * the number of columns that should be frozen + * + * @throws IllegalArgumentException + * if the column count is < -1 or > the number of visible + * columns + */ + public void setFrozenColumnCount(int numberOfColumns) { + if (numberOfColumns < -1 || numberOfColumns > getColumnCount()) { + throw new IllegalArgumentException( + "count must be between -1 and the current number of columns (" + + getColumnCount() + ")"); + } + + this.frozenColumnCount = numberOfColumns; + updateFrozenColumns(); + } + + private void updateFrozenColumns() { + int numberOfColumns = frozenColumnCount; + + if (numberOfColumns == -1) { + numberOfColumns = 0; + } else if (selectionColumn != null) { + numberOfColumns++; + } + + escalator.getColumnConfiguration() + .setFrozenColumnCount(numberOfColumns); + + } + + /** + * Gets the number of frozen columns in this grid. 0 means that no data + * columns will be frozen, but the built-in selection checkbox column will + * still be frozen if it's in use. -1 means that not even the selection + * column is frozen. + * + * @return the number of frozen columns + */ + public int getFrozenColumnCount() { + return frozenColumnCount; + } + + public HandlerRegistration addRowVisibilityChangeHandler( + RowVisibilityChangeHandler handler) { + /* + * Reusing Escalator's RowVisibilityChangeHandler, since a scroll + * concept is too abstract. e.g. the event needs to be re-sent when the + * widget is resized. + */ + return escalator.addRowVisibilityChangeHandler(handler); + } + + /** + * Scrolls to a certain row, using {@link ScrollDestination#ANY}. + * + * @param rowIndex + * zero-based index of the row to scroll to. + * @throws IllegalArgumentException + * if rowIndex is below zero, or above the maximum value + * supported by the data source. + */ + public void scrollToRow(int rowIndex) throws IllegalArgumentException { + scrollToRow(rowIndex, ScrollDestination.ANY, + GridConstants.DEFAULT_PADDING); + } + + /** + * Scrolls to a certain row, using user-specified scroll destination. + * + * @param rowIndex + * zero-based index of the row to scroll to. + * @param destination + * desired destination placement of scrolled-to-row. See + * {@link ScrollDestination} for more information. + * @throws IllegalArgumentException + * if rowIndex is below zero, or above the maximum value + * supported by the data source. + */ + public void scrollToRow(int rowIndex, ScrollDestination destination) + throws IllegalArgumentException { + scrollToRow(rowIndex, destination, + destination == ScrollDestination.MIDDLE ? 0 + : GridConstants.DEFAULT_PADDING); + } + + /** + * Scrolls to a certain row using only user-specified parameters. + * + * @param rowIndex + * zero-based index of the row to scroll to. + * @param destination + * desired destination placement of scrolled-to-row. See + * {@link ScrollDestination} for more information. + * @param paddingPx + * number of pixels to overscroll. Behavior depends on + * destination. + * @throws IllegalArgumentException + * if {@code destination} is {@link ScrollDestination#MIDDLE} + * and padding is nonzero, because having a padding on a + * centered row is undefined behavior, or if rowIndex is below + * zero or above the row count of the data source. + */ + private void scrollToRow(int rowIndex, ScrollDestination destination, + int paddingPx) throws IllegalArgumentException { + int maxsize = escalator.getBody().getRowCount() - 1; + + if (rowIndex < 0) { + throw new IllegalArgumentException("Row index (" + rowIndex + + ") is below zero!"); + } + + if (rowIndex > maxsize) { + throw new IllegalArgumentException("Row index (" + rowIndex + + ") is above maximum (" + maxsize + ")!"); + } + + escalator.scrollToRow(rowIndex, destination, paddingPx); + } + + /** + * Scrolls to the beginning of the very first row. + */ + public void scrollToStart() { + scrollToRow(0, ScrollDestination.START); + } + + /** + * Scrolls to the end of the very last row. + */ + public void scrollToEnd() { + scrollToRow(escalator.getBody().getRowCount() - 1, + ScrollDestination.END); + } + + /** + * Sets the vertical scroll offset. + * + * @param px + * the number of pixels this grid should be scrolled down + */ + public void setScrollTop(double px) { + escalator.setScrollTop(px); + } + + /** + * Gets the vertical scroll offset + * + * @return the number of pixels this grid is scrolled down + */ + public double getScrollTop() { + return escalator.getScrollTop(); + } + + /** + * Gets the horizontal scroll offset + * + * @return the number of pixels this grid is scrolled to the right + */ + public double getScrollLeft() { + return escalator.getScrollLeft(); + } + + private static final Logger getLogger() { + return Logger.getLogger(Grid.class.getName()); + } + + /** + * Sets the number of rows that should be visible in Grid's body, while + * {@link #getHeightMode()} is {@link HeightMode#ROW}. + * <p> + * If Grid is currently not in {@link HeightMode#ROW}, the given value is + * remembered, and applied once the mode is applied. + * + * @param rows + * The height in terms of number of rows displayed in Grid's + * body. If Grid doesn't contain enough rows, white space is + * displayed instead. + * @throws IllegalArgumentException + * if {@code rows} is zero or less + * @throws IllegalArgumentException + * if {@code rows} is {@link Double#isInifinite(double) + * infinite} + * @throws IllegalArgumentException + * if {@code rows} is {@link Double#isNaN(double) NaN} + * + * @see #setHeightMode(HeightMode) + */ + public void setHeightByRows(double rows) throws IllegalArgumentException { + escalator.setHeightByRows(rows); + } + + /** + * Gets the amount of rows in Grid's body that are shown, while + * {@link #getHeightMode()} is {@link HeightMode#ROW}. + * <p> + * By default, it is {@value Escalator#DEFAULT_HEIGHT_BY_ROWS}. + * + * @return the amount of rows that should be shown in Grid's body, while in + * {@link HeightMode#ROW}. + * @see #setHeightByRows(double) + */ + public double getHeightByRows() { + return escalator.getHeightByRows(); + } + + /** + * Defines the mode in which the Grid widget's height is calculated. + * <p> + * If {@link HeightMode#CSS} is given, Grid will respect the values given + * via {@link #setHeight(String)}, and behave as a traditional Widget. + * <p> + * If {@link HeightMode#ROW} is given, Grid will make sure that the body + * will display as many rows as {@link #getHeightByRows()} defines. + * <em>Note:</em> If headers/footers are inserted or removed, the widget + * will resize itself to still display the required amount of rows in its + * body. It also takes the horizontal scrollbar into account. + * + * @param heightMode + * the mode in to which Grid should be set + */ + public void setHeightMode(HeightMode heightMode) { + /* + * This method is a workaround for the fact that Vaadin re-applies + * widget dimensions (height/width) on each state change event. The + * original design was to have setHeight an setHeightByRow be equals, + * and whichever was called the latest was considered in effect. + * + * But, because of Vaadin always calling setHeight on the widget, this + * approach doesn't work. + */ + + escalator.setHeightMode(heightMode); + } + + /** + * Returns the current {@link HeightMode} the Grid is in. + * <p> + * Defaults to {@link HeightMode#CSS}. + * + * @return the current HeightMode + */ + public HeightMode getHeightMode() { + return escalator.getHeightMode(); + } + + private Set<String> getConsumedEventsForRenderer(Renderer<?> renderer) { + Set<String> events = new HashSet<String>(); + if (renderer instanceof ComplexRenderer) { + Collection<String> consumedEvents = ((ComplexRenderer<?>) renderer) + .getConsumedEvents(); + if (consumedEvents != null) { + events.addAll(consumedEvents); + } + } + return events; + } + + @Override + public void onBrowserEvent(Event event) { + if (!isEnabled()) { + return; + } + + EventTarget target = event.getEventTarget(); + + if (!Element.is(target)) { + return; + } + + Element e = Element.as(target); + RowContainer container = escalator.findRowContainer(e); + Cell cell; + + String eventType = event.getType(); + if (container == null) { + if (eventType.equals(BrowserEvents.KEYDOWN) + || eventType.equals(BrowserEvents.KEYUP) + || eventType.equals(BrowserEvents.KEYPRESS)) { + cell = cellFocusHandler.getFocusedCell(); + container = cellFocusHandler.containerWithFocus; + } else { + // Click in a location that does not contain cells. + return; + } + } else { + cell = container.getCell(e); + if (eventType.equals(BrowserEvents.MOUSEDOWN)) { + cellOnPrevMouseDown = cell; + } else if (cell == null && eventType.equals(BrowserEvents.CLICK)) { + /* + * Chrome has an interesting idea on click targets (see + * cellOnPrevMouseDown javadoc). Firefox, on the other hand, has + * the mousedown target as the click target. + */ + cell = cellOnPrevMouseDown; + } + } + + assert cell != null : "received " + eventType + + "-event with a null cell target"; + eventCell.set(cell); + + // Editor can steal focus from Grid and is still handled + if (handleEditorEvent(event, container)) { + return; + } + + // Fire GridKeyEvents and GridClickEvents. Pass the event to escalator. + super.onBrowserEvent(event); + + if (!isElementInChildWidget(e)) { + + // Sorting through header Click / KeyUp + if (handleHeaderDefaultRowEvent(event, container)) { + return; + } + + if (handleRendererEvent(event, container)) { + return; + } + + if (handleNavigationEvent(event, container)) { + return; + } + + if (handleCellFocusEvent(event, container)) { + return; + } + } + } + + private boolean isElementInChildWidget(Element e) { + Widget w = WidgetUtil.findWidget(e, null); + + if (w == this) { + return false; + } + + /* + * If e is directly inside this grid, but the grid is wrapped in a + * Composite, findWidget is not going to find this, only the wrapper. + * Thus we need to check its parents to see if we encounter this; if we + * don't, the found widget is actually a parent of this, so we should + * return false. + */ + while (w != null && w != this) { + w = w.getParent(); + } + return w != null; + } + + private boolean handleEditorEvent(Event event, RowContainer container) { + + if (editor.getState() != Editor.State.INACTIVE) { + if (event.getTypeInt() == Event.ONKEYDOWN + && event.getKeyCode() == Editor.KEYCODE_HIDE) { + editor.cancel(); + } + return true; + } + + if (container == escalator.getBody() && editor.isEnabled()) { + if (event.getTypeInt() == Event.ONDBLCLICK) { + editor.editRow(eventCell.getRowIndex()); + return true; + } else if (event.getTypeInt() == Event.ONKEYDOWN + && event.getKeyCode() == Editor.KEYCODE_SHOW) { + editor.editRow(cellFocusHandler.rowWithFocus); + return true; + } + } + return false; + } + + private boolean handleRendererEvent(Event event, RowContainer container) { + + if (container == escalator.getBody()) { + Column<?, T> gridColumn = eventCell.getColumn(); + boolean enterKey = event.getType().equals(BrowserEvents.KEYDOWN) + && event.getKeyCode() == KeyCodes.KEY_ENTER; + boolean doubleClick = event.getType() + .equals(BrowserEvents.DBLCLICK); + + if (gridColumn.getRenderer() instanceof ComplexRenderer) { + ComplexRenderer<?> cplxRenderer = (ComplexRenderer<?>) gridColumn + .getRenderer(); + if (cplxRenderer.getConsumedEvents().contains(event.getType())) { + if (cplxRenderer.onBrowserEvent(eventCell, event)) { + return true; + } + } + + // Calls onActivate if KeyDown and Enter or double click + if ((enterKey || doubleClick) + && cplxRenderer.onActivate(eventCell)) { + return true; + } + } + } + return false; + } + + private boolean handleCellFocusEvent(Event event, RowContainer container) { + Collection<String> navigation = cellFocusHandler.getNavigationEvents(); + if (navigation.contains(event.getType())) { + cellFocusHandler.handleNavigationEvent(event, eventCell); + } + return false; + } + + private boolean handleNavigationEvent(Event event, RowContainer unused) { + if (!event.getType().equals(BrowserEvents.KEYDOWN)) { + // Only handle key downs + return false; + } + + int newRow = -1; + RowContainer container = escalator.getBody(); + switch (event.getKeyCode()) { + case KeyCodes.KEY_HOME: + if (container.getRowCount() > 0) { + newRow = 0; + } + break; + case KeyCodes.KEY_END: + if (container.getRowCount() > 0) { + newRow = container.getRowCount() - 1; + } + break; + case KeyCodes.KEY_PAGEUP: { + Range range = escalator.getVisibleRowRange(); + if (!range.isEmpty()) { + int firstIndex = getFirstVisibleRowIndex(); + newRow = firstIndex - range.length(); + if (newRow < 0) { + newRow = 0; + } + } + break; + } + case KeyCodes.KEY_PAGEDOWN: { + Range range = escalator.getVisibleRowRange(); + if (!range.isEmpty()) { + int lastIndex = getLastVisibleRowIndex(); + newRow = lastIndex + range.length(); + if (newRow >= container.getRowCount()) { + newRow = container.getRowCount() - 1; + } + } + break; + } + default: + return false; + } + + scrollToRow(newRow); + + return true; + } + + private Point rowEventTouchStartingPoint; + private CellStyleGenerator<T> cellStyleGenerator; + private RowStyleGenerator<T> rowStyleGenerator; + private RowReference<T> rowReference = new RowReference<T>(this); + private CellReference<T> cellReference = new CellReference<T>(rowReference); + private RendererCellReference rendererCellReference = new RendererCellReference( + (RowReference<Object>) rowReference); + + private boolean handleHeaderDefaultRowEvent(Event event, + RowContainer container) { + if (container != escalator.getHeader()) { + return false; + } + if (!getHeader().getRow(eventCell.getRowIndex()).isDefault()) { + return false; + } + if (!eventCell.getColumn().isSortable()) { + // Only handle sorting events if the column is sortable + return false; + } + + if (BrowserEvents.MOUSEDOWN.equals(event.getType()) + && event.getShiftKey()) { + // Don't select text when shift clicking on a header. + event.preventDefault(); + } + + if (BrowserEvents.TOUCHSTART.equals(event.getType())) { + if (event.getTouches().length() > 1) { + return false; + } + + event.preventDefault(); + + Touch touch = event.getChangedTouches().get(0); + rowEventTouchStartingPoint = new Point(touch.getClientX(), + touch.getClientY()); + + sorter.sortAfterDelay(GridConstants.LONG_TAP_DELAY, true); + + return true; + + } else if (BrowserEvents.TOUCHMOVE.equals(event.getType())) { + if (event.getTouches().length() > 1) { + return false; + } + + event.preventDefault(); + + Touch touch = event.getChangedTouches().get(0); + double diffX = Math.abs(touch.getClientX() + - rowEventTouchStartingPoint.getX()); + double diffY = Math.abs(touch.getClientY() + - rowEventTouchStartingPoint.getY()); + + // Cancel long tap if finger strays too far from + // starting point + if (diffX > GridConstants.LONG_TAP_THRESHOLD + || diffY > GridConstants.LONG_TAP_THRESHOLD) { + sorter.cancelDelayedSort(); + } + + return true; + + } else if (BrowserEvents.TOUCHEND.equals(event.getType())) { + if (event.getTouches().length() > 1) { + return false; + } + + if (sorter.isDelayedSortScheduled()) { + // Not a long tap yet, perform single sort + sorter.cancelDelayedSort(); + sorter.sort(eventCell.getColumn(), false); + } + + return true; + + } else if (BrowserEvents.TOUCHCANCEL.equals(event.getType())) { + if (event.getTouches().length() > 1) { + return false; + } + + sorter.cancelDelayedSort(); + + return true; + + } else if (BrowserEvents.CLICK.equals(event.getType())) { + + sorter.sort(eventCell.getColumn(), event.getShiftKey()); + + // Click events should go onward to cell focus logic + return false; + } else { + return false; + } + } + + @Override + public com.google.gwt.user.client.Element getSubPartElement(String subPart) { + // Parse SubPart string to type and indices + String[] splitArgs = subPart.split("\\["); + + String type = splitArgs[0]; + int[] indices = new int[splitArgs.length - 1]; + for (int i = 0; i < indices.length; ++i) { + String tmp = splitArgs[i + 1]; + indices[i] = Integer.parseInt(tmp.substring(0, tmp.length() - 1)); + } + + // Get correct RowContainer for type from Escalator + RowContainer container = null; + if (type.equalsIgnoreCase("header")) { + container = escalator.getHeader(); + } else if (type.equalsIgnoreCase("cell")) { + // If wanted row is not visible, we need to scroll there. + Range visibleRowRange = escalator.getVisibleRowRange(); + if (indices.length > 0 && !visibleRowRange.contains(indices[0])) { + try { + scrollToRow(indices[0]); + } catch (IllegalArgumentException e) { + getLogger().log(Level.SEVERE, e.getMessage()); + } + // Scrolling causes a lazy loading event. No element can + // currently be retrieved. + return null; + } + container = escalator.getBody(); + } else if (type.equalsIgnoreCase("footer")) { + container = escalator.getFooter(); + } else if (type.equalsIgnoreCase("editor")) { + if (editor.getState() != State.ACTIVE) { + // Editor is not there. + return null; + } + + if (indices.length == 0) { + return DOM.asOld(editor.editorOverlay); + } else if (indices.length == 1 && indices[0] < columns.size()) { + escalator.scrollToColumn(indices[0], ScrollDestination.ANY, 0); + return editor.getWidget(columns.get(indices[0])).getElement(); + } else { + return null; + } + } + + if (null != container) { + if (indices.length == 0) { + // No indexing. Just return the wanted container element + return DOM.asOld(container.getElement()); + } else { + try { + return DOM.asOld(getSubPart(container, indices)); + } catch (Exception e) { + getLogger().log(Level.SEVERE, e.getMessage()); + } + } + } + return null; + } + + private Element getSubPart(RowContainer container, int[] indices) { + Element targetElement = container.getRowElement(indices[0]); + + // Scroll wanted column to view if able + if (indices.length > 1 && targetElement != null) { + if (escalator.getColumnConfiguration().getFrozenColumnCount() <= indices[1]) { + escalator.scrollToColumn(indices[1], ScrollDestination.ANY, 0); + } + + targetElement = getCellFromRow(TableRowElement.as(targetElement), + indices[1]); + + for (int i = 2; i < indices.length && targetElement != null; ++i) { + targetElement = (Element) targetElement.getChild(indices[i]); + } + } + + return targetElement; + } + + private Element getCellFromRow(TableRowElement rowElement, int index) { + int childCount = rowElement.getCells().getLength(); + if (index < 0 || index >= childCount) { + return null; + } + + TableCellElement currentCell = null; + boolean indexInColspan = false; + int i = 0; + + while (!indexInColspan) { + currentCell = rowElement.getCells().getItem(i); + + // Calculate if this is the cell we are looking for + int colSpan = currentCell.getColSpan(); + indexInColspan = index < colSpan + i; + + // Increment by colspan to skip over hidden cells + i += colSpan; + } + return currentCell; + } + + @Override + public String getSubPartName(com.google.gwt.user.client.Element subElement) { + // Containers and matching SubPart types + List<RowContainer> containers = Arrays.asList(escalator.getHeader(), + escalator.getBody(), escalator.getFooter()); + List<String> containerType = Arrays.asList("header", "cell", "footer"); + + for (int i = 0; i < containers.size(); ++i) { + RowContainer container = containers.get(i); + boolean containerRow = (subElement.getTagName().equalsIgnoreCase( + "tr") && subElement.getParentElement() == container + .getElement()); + if (containerRow) { + // Wanted SubPart is row that is a child of containers root + // To get indices, we use a cell that is a child of this row + subElement = DOM.asOld(subElement.getFirstChildElement()); + } + + Cell cell = container.getCell(subElement); + if (cell != null) { + // Skip the column index if subElement was a child of root + return containerType.get(i) + "[" + cell.getRow() + + (containerRow ? "]" : "][" + cell.getColumn() + "]"); + } + } + + // Check if subelement is part of editor. + if (editor.getState() == State.ACTIVE) { + if (editor.editorOverlay.isOrHasChild(subElement)) { + int i = 0; + for (Column<?, T> column : columns) { + if (editor.getWidget(column).getElement() + .isOrHasChild(subElement)) { + return "editor[" + i + "]"; + } + ++i; + } + return "editor"; + } + } + + return null; + } + + private void setSelectColumnRenderer( + final Renderer<Boolean> selectColumnRenderer) { + if (this.selectColumnRenderer == selectColumnRenderer) { + return; + } + + if (this.selectColumnRenderer != null) { + if (this.selectColumnRenderer instanceof ComplexRenderer) { + // End of Life for the old selection column renderer. + ((ComplexRenderer<?>) this.selectColumnRenderer).destroy(); + } + + // Clear field so frozen column logic in the remove method knows + // what to do + Column<?, T> colToRemove = selectionColumn; + selectionColumn = null; + removeColumnSkipSelectionColumnCheck(colToRemove); + cellFocusHandler.offsetRangeBy(-1); + } + + this.selectColumnRenderer = selectColumnRenderer; + + if (selectColumnRenderer != null) { + cellFocusHandler.offsetRangeBy(1); + selectionColumn = new SelectionColumn(selectColumnRenderer); + + addColumnSkipSelectionColumnCheck(selectionColumn, 0); + selectionColumn.initDone(); + } else { + selectionColumn = null; + refreshBody(); + } + + updateFrozenColumns(); + } + + /** + * Sets the current selection model. + * <p> + * This function will call {@link SelectionModel#setGrid(Grid)}. + * + * @param selectionModel + * a selection model implementation. + * @throws IllegalArgumentException + * if selection model argument is null + */ + public void setSelectionModel(SelectionModel<T> selectionModel) { + + if (selectionModel == null) { + throw new IllegalArgumentException("Selection model can't be null"); + } + + if (this.selectionModel != null) { + // Detach selection model from Grid. + this.selectionModel.setGrid(null); + } + + this.selectionModel = selectionModel; + selectionModel.setGrid(this); + setSelectColumnRenderer(this.selectionModel + .getSelectionColumnRenderer()); + } + + /** + * Gets a reference to the current selection model. + * + * @return the currently used SelectionModel instance. + */ + public SelectionModel<T> getSelectionModel() { + return selectionModel; + } + + /** + * Sets current selection mode. + * <p> + * This is a shorthand method for {@link Grid#setSelectionModel}. + * + * @param mode + * a selection mode value + * @see {@link SelectionMode}. + */ + public void setSelectionMode(SelectionMode mode) { + SelectionModel<T> model = mode.createModel(); + setSelectionModel(model); + } + + /** + * Test if a row is selected. + * + * @param row + * a row object + * @return true, if the current selection model considers the provided row + * object selected. + */ + public boolean isSelected(T row) { + return selectionModel.isSelected(row); + } + + /** + * Select a row using the current selection model. + * <p> + * Only selection models implementing {@link SelectionModel.Single} and + * {@link SelectionModel.Multi} are supported; for anything else, an + * exception will be thrown. + * + * @param row + * a row object + * @return <code>true</code> iff the current selection changed + * @throws IllegalStateException + * if the current selection model is not an instance of + * {@link SelectionModel.Single} or {@link SelectionModel.Multi} + */ + @SuppressWarnings("unchecked") + public boolean select(T row) { + if (selectionModel instanceof SelectionModel.Single<?>) { + return ((SelectionModel.Single<T>) selectionModel).select(row); + } else if (selectionModel instanceof SelectionModel.Multi<?>) { + return ((SelectionModel.Multi<T>) selectionModel).select(row); + } else { + throw new IllegalStateException("Unsupported selection model"); + } + } + + /** + * Deselect a row using the current selection model. + * <p> + * Only selection models implementing {@link SelectionModel.Single} and + * {@link SelectionModel.Multi} are supported; for anything else, an + * exception will be thrown. + * + * @param row + * a row object + * @return <code>true</code> iff the current selection changed + * @throws IllegalStateException + * if the current selection model is not an instance of + * {@link SelectionModel.Single} or {@link SelectionModel.Multi} + */ + @SuppressWarnings("unchecked") + public boolean deselect(T row) { + if (selectionModel instanceof SelectionModel.Single<?>) { + return ((SelectionModel.Single<T>) selectionModel).deselect(row); + } else if (selectionModel instanceof SelectionModel.Multi<?>) { + return ((SelectionModel.Multi<T>) selectionModel).deselect(row); + } else { + throw new IllegalStateException("Unsupported selection model"); + } + } + + /** + * Gets last selected row from the current SelectionModel. + * <p> + * Only selection models implementing {@link SelectionModel.Single} are + * valid for this method; for anything else, use the + * {@link Grid#getSelectedRows()} method. + * + * @return a selected row reference, or null, if no row is selected + * @throws IllegalStateException + * if the current selection model is not an instance of + * {@link SelectionModel.Single} + */ + public T getSelectedRow() { + if (selectionModel instanceof SelectionModel.Single<?>) { + return ((SelectionModel.Single<T>) selectionModel).getSelectedRow(); + } else { + throw new IllegalStateException( + "Unsupported selection model; can not get single selected row"); + } + } + + /** + * Gets currently selected rows from the current selection model. + * + * @return a non-null collection containing all currently selected rows. + */ + public Collection<T> getSelectedRows() { + return selectionModel.getSelectedRows(); + } + + @Override + public HandlerRegistration addSelectionHandler( + final SelectionHandler<T> handler) { + return addHandler(handler, SelectionEvent.getType()); + } + + /** + * Sets the current sort order using the fluid Sort API. Read the + * documentation for {@link Sort} for more information. + * + * @param s + * a sort instance + */ + public void sort(Sort s) { + setSortOrder(s.build()); + } + + /** + * Sorts the Grid data in ascending order along one column. + * + * @param column + * a grid column reference + */ + public <C> void sort(Column<C, T> column) { + sort(column, SortDirection.ASCENDING); + } + + /** + * Sorts the Grid data along one column. + * + * @param column + * a grid column reference + * @param direction + * a sort direction value + */ + public <C> void sort(Column<C, T> column, SortDirection direction) { + sort(Sort.by(column, direction)); + } + + /** + * Sets the sort order to use. Setting this causes the Grid to re-sort + * itself. + * + * @param order + * a sort order list. If set to null, the sort order is cleared. + */ + public void setSortOrder(List<SortOrder> order) { + setSortOrder(order, false); + } + + private void setSortOrder(List<SortOrder> order, boolean userOriginated) { + if (order != sortOrder) { + sortOrder.clear(); + if (order != null) { + sortOrder.addAll(order); + } + } + sort(userOriginated); + } + + /** + * Get a copy of the current sort order array. + * + * @return a copy of the current sort order array + */ + public List<SortOrder> getSortOrder() { + return Collections.unmodifiableList(sortOrder); + } + + /** + * Finds the sorting order for this column + */ + private SortOrder getSortOrder(Column<?, ?> column) { + for (SortOrder order : getSortOrder()) { + if (order.getColumn() == column) { + return order; + } + } + return null; + } + + /** + * Register a GWT event handler for a sorting event. This handler gets + * called whenever this Grid needs its data source to provide data sorted in + * a specific order. + * + * @param handler + * a sort event handler + * @return the registration for the event + */ + public HandlerRegistration addSortHandler(SortHandler<T> handler) { + return addHandler(handler, SortEvent.getType()); + } + + /** + * Register a GWT event handler for a select all event. This handler gets + * called whenever Grid needs all rows selected. + * + * @param handler + * a select all event handler + */ + public HandlerRegistration addSelectAllHandler(SelectAllHandler<T> handler) { + return addHandler(handler, SelectAllEvent.getType()); + } + + /** + * Register a GWT event handler for a data available event. This handler + * gets called whenever the {@link DataSource} for this Grid has new data + * available. + * <p> + * This handle will be fired with the current available data after + * registration is done. + * + * @param handler + * a data available event handler + * @return the registartion for the event + */ + public HandlerRegistration addDataAvailableHandler( + final DataAvailableHandler handler) { + // Deferred call to handler with current row range + Scheduler.get().scheduleFinally(new ScheduledCommand() { + @Override + public void execute() { + if (!dataIsBeingFetched) { + handler.onDataAvailable(new DataAvailableEvent( + currentDataAvailable)); + } + } + }); + return addHandler(handler, DataAvailableEvent.TYPE); + } + + /** + * Register a BodyKeyDownHandler to this Grid. The event for this handler is + * fired when a KeyDown event occurs while cell focus is in the Body of this + * Grid. + * + * @param handler + * the key handler to register + * @return the registration for the event + */ + public HandlerRegistration addBodyKeyDownHandler(BodyKeyDownHandler handler) { + return addHandler(handler, keyDown.getAssociatedType()); + } + + /** + * Register a BodyKeyUpHandler to this Grid. The event for this handler is + * fired when a KeyUp event occurs while cell focus is in the Body of this + * Grid. + * + * @param handler + * the key handler to register + * @return the registration for the event + */ + public HandlerRegistration addBodyKeyUpHandler(BodyKeyUpHandler handler) { + return addHandler(handler, keyUp.getAssociatedType()); + } + + /** + * Register a BodyKeyPressHandler to this Grid. The event for this handler + * is fired when a KeyPress event occurs while cell focus is in the Body of + * this Grid. + * + * @param handler + * the key handler to register + * @return the registration for the event + */ + public HandlerRegistration addBodyKeyPressHandler( + BodyKeyPressHandler handler) { + return addHandler(handler, keyPress.getAssociatedType()); + } + + /** + * Register a HeaderKeyDownHandler to this Grid. The event for this handler + * is fired when a KeyDown event occurs while cell focus is in the Header of + * this Grid. + * + * @param handler + * the key handler to register + * @return the registration for the event + */ + public HandlerRegistration addHeaderKeyDownHandler( + HeaderKeyDownHandler handler) { + return addHandler(handler, keyDown.getAssociatedType()); + } + + /** + * Register a HeaderKeyUpHandler to this Grid. The event for this handler is + * fired when a KeyUp event occurs while cell focus is in the Header of this + * Grid. + * + * @param handler + * the key handler to register + * @return the registration for the event + */ + public HandlerRegistration addHeaderKeyUpHandler(HeaderKeyUpHandler handler) { + return addHandler(handler, keyUp.getAssociatedType()); + } + + /** + * Register a HeaderKeyPressHandler to this Grid. The event for this handler + * is fired when a KeyPress event occurs while cell focus is in the Header + * of this Grid. + * + * @param handler + * the key handler to register + * @return the registration for the event + */ + public HandlerRegistration addHeaderKeyPressHandler( + HeaderKeyPressHandler handler) { + return addHandler(handler, keyPress.getAssociatedType()); + } + + /** + * Register a FooterKeyDownHandler to this Grid. The event for this handler + * is fired when a KeyDown event occurs while cell focus is in the Footer of + * this Grid. + * + * @param handler + * the key handler to register + * @return the registration for the event + */ + public HandlerRegistration addFooterKeyDownHandler( + FooterKeyDownHandler handler) { + return addHandler(handler, keyDown.getAssociatedType()); + } + + /** + * Register a FooterKeyUpHandler to this Grid. The event for this handler is + * fired when a KeyUp event occurs while cell focus is in the Footer of this + * Grid. + * + * @param handler + * the key handler to register + * @return the registration for the event + */ + public HandlerRegistration addFooterKeyUpHandler(FooterKeyUpHandler handler) { + return addHandler(handler, keyUp.getAssociatedType()); + } + + /** + * Register a FooterKeyPressHandler to this Grid. The event for this handler + * is fired when a KeyPress event occurs while cell focus is in the Footer + * of this Grid. + * + * @param handler + * the key handler to register + * @return the registration for the event + */ + public HandlerRegistration addFooterKeyPressHandler( + FooterKeyPressHandler handler) { + return addHandler(handler, keyPress.getAssociatedType()); + } + + /** + * Register a BodyClickHandler to this Grid. The event for this handler is + * fired when a Click event occurs in the Body of this Grid. + * + * @param handler + * the click handler to register + * @return the registration for the event + */ + public HandlerRegistration addBodyClickHandler(BodyClickHandler handler) { + return addHandler(handler, clickEvent.getAssociatedType()); + } + + /** + * Register a HeaderClickHandler to this Grid. The event for this handler is + * fired when a Click event occurs in the Header of this Grid. + * + * @param handler + * the click handler to register + * @return the registration for the event + */ + public HandlerRegistration addHeaderClickHandler(HeaderClickHandler handler) { + return addHandler(handler, clickEvent.getAssociatedType()); + } + + /** + * Register a FooterClickHandler to this Grid. The event for this handler is + * fired when a Click event occurs in the Footer of this Grid. + * + * @param handler + * the click handler to register + * @return the registration for the event + */ + public HandlerRegistration addFooterClickHandler(FooterClickHandler handler) { + return addHandler(handler, clickEvent.getAssociatedType()); + } + + /** + * Register a BodyDoubleClickHandler to this Grid. The event for this + * handler is fired when a double click event occurs in the Body of this + * Grid. + * + * @param handler + * the double click handler to register + * @return the registration for the event + */ + public HandlerRegistration addBodyDoubleClickHandler( + BodyDoubleClickHandler handler) { + return addHandler(handler, doubleClickEvent.getAssociatedType()); + } + + /** + * Register a HeaderDoubleClickHandler to this Grid. The event for this + * handler is fired when a double click event occurs in the Header of this + * Grid. + * + * @param handler + * the double click handler to register + * @return the registration for the event + */ + public HandlerRegistration addHeaderDoubleClickHandler( + HeaderDoubleClickHandler handler) { + return addHandler(handler, doubleClickEvent.getAssociatedType()); + } + + /** + * Register a FooterDoubleClickHandler to this Grid. The event for this + * handler is fired when a double click event occurs in the Footer of this + * Grid. + * + * @param handler + * the double click handler to register + * @return the registration for the event + */ + public HandlerRegistration addFooterDoubleClickHandler( + FooterDoubleClickHandler handler) { + return addHandler(handler, doubleClickEvent.getAssociatedType()); + } + + /** + * Apply sorting to data source. + */ + private void sort(boolean userOriginated) { + refreshHeader(); + fireEvent(new SortEvent<T>(this, + Collections.unmodifiableList(sortOrder), userOriginated)); + } + + private int getLastVisibleRowIndex() { + int lastRowIndex = escalator.getVisibleRowRange().getEnd(); + int footerTop = escalator.getFooter().getElement().getAbsoluteTop(); + Element lastRow; + + do { + lastRow = escalator.getBody().getRowElement(--lastRowIndex); + } while (lastRow.getAbsoluteTop() > footerTop); + + return lastRowIndex; + } + + private int getFirstVisibleRowIndex() { + int firstRowIndex = escalator.getVisibleRowRange().getStart(); + int headerBottom = escalator.getHeader().getElement() + .getAbsoluteBottom(); + Element firstRow = escalator.getBody().getRowElement(firstRowIndex); + + while (firstRow.getAbsoluteBottom() < headerBottom) { + firstRow = escalator.getBody().getRowElement(++firstRowIndex); + } + + return firstRowIndex; + } + + /** + * Adds a scroll handler to this grid + * + * @param handler + * the scroll handler to add + * @return a handler registration for the registered scroll handler + */ + public HandlerRegistration addScrollHandler(ScrollHandler handler) { + return addHandler(handler, ScrollEvent.TYPE); + } + + @Override + public boolean isWorkPending() { + return escalator.isWorkPending() || dataIsBeingFetched + || autoColumnWidthsRecalculator.isScheduled(); + } + + /** + * Sets a new column order for the grid. All columns which are not ordered + * here will remain in the order they were before as the last columns of + * grid. + * + * @param orderedColumns + * array of columns in wanted order + */ + public void setColumnOrder(Column<?, T>... orderedColumns) { + ColumnConfiguration conf = getEscalator().getColumnConfiguration(); + + // Trigger ComplexRenderer.destroy for old content + conf.removeColumns(0, conf.getColumnCount()); + + List<Column<?, T>> newOrder = new ArrayList<Column<?, T>>(); + if (selectionColumn != null) { + newOrder.add(selectionColumn); + } + + int i = 0; + for (Column<?, T> column : orderedColumns) { + if (columns.contains(column)) { + newOrder.add(column); + ++i; + } else { + throw new IllegalArgumentException("Given column at index " + i + + " does not exist in Grid"); + } + } + + if (columns.size() != newOrder.size()) { + columns.removeAll(newOrder); + newOrder.addAll(columns); + } + columns = newOrder; + + // Do ComplexRenderer.init and render new content + conf.insertColumns(0, columns.size()); + + // Update column widths. + for (Column<?, T> column : columns) { + column.reapplyWidth(); + } + + // Recalculate all the colspans + for (HeaderRow row : header.getRows()) { + row.calculateColspans(); + } + for (FooterRow row : footer.getRows()) { + row.calculateColspans(); + } + } + + /** + * Sets the style generator that is used for generating styles for cells + * + * @param cellStyleGenerator + * the cell style generator to set, or <code>null</code> to + * remove a previously set generator + */ + public void setCellStyleGenerator(CellStyleGenerator<T> cellStyleGenerator) { + this.cellStyleGenerator = cellStyleGenerator; + refreshBody(); + } + + /** + * Gets the style generator that is used for generating styles for cells + * + * @return the cell style generator, or <code>null</code> if no generator is + * set + */ + public CellStyleGenerator<T> getCellStyleGenerator() { + return cellStyleGenerator; + } + + /** + * Sets the style generator that is used for generating styles for rows + * + * @param rowStyleGenerator + * the row style generator to set, or <code>null</code> to remove + * a previously set generator + */ + public void setRowStyleGenerator(RowStyleGenerator<T> rowStyleGenerator) { + this.rowStyleGenerator = rowStyleGenerator; + refreshBody(); + } + + /** + * Gets the style generator that is used for generating styles for rows + * + * @return the row style generator, or <code>null</code> if no generator is + * set + */ + public RowStyleGenerator<T> getRowStyleGenerator() { + return rowStyleGenerator; + } + + private static void setCustomStyleName(Element element, String styleName) { + assert element != null; + + String oldStyleName = element + .getPropertyString(CUSTOM_STYLE_PROPERTY_NAME); + + if (!SharedUtil.equals(oldStyleName, styleName)) { + if (oldStyleName != null) { + element.removeClassName(oldStyleName); + } + if (styleName != null) { + element.addClassName(styleName); + } + element.setPropertyString(CUSTOM_STYLE_PROPERTY_NAME, styleName); + } + + } + + /** + * Opens the editor over the row with the given index. + * + * @param rowIndex + * the index of the row to be edited + * + * @throws IllegalStateException + * if the editor is not enabled + * @throws IllegalStateException + * if the editor is already in edit mode + */ + public void editRow(int rowIndex) { + editor.editRow(rowIndex); + } + + /** + * Saves any unsaved changes in the editor to the data source. + * + * @throws IllegalStateException + * if the editor is not enabled + * @throws IllegalStateException + * if the editor is not in edit mode + */ + public void saveEditor() { + editor.save(); + } + + /** + * Cancels the currently active edit and hides the editor. Any changes that + * are not {@link #saveEditor() saved} are lost. + * + * @throws IllegalStateException + * if the editor is not enabled + * @throws IllegalStateException + * if the editor is not in edit mode + */ + public void cancelEditor() { + editor.cancel(); + } + + /** + * Returns the handler responsible for binding data and editor widgets to + * the editor. + * + * @return the editor handler or null if not set + */ + public EditorHandler<T> getEditorHandler() { + return editor.getHandler(); + } + + /** + * Sets the handler responsible for binding data and editor widgets to the + * editor. + * + * @param rowHandler + * the new editor handler + * + * @throws IllegalStateException + * if the editor is currently in edit mode + */ + public void setEditorHandler(EditorHandler<T> handler) { + editor.setHandler(handler); + } + + /** + * Returns the enabled state of the editor. + * + * @return true if editing is enabled, false otherwise + */ + public boolean isEditorEnabled() { + return editor.isEnabled(); + } + + /** + * Sets the enabled state of the editor. + * + * @param enabled + * true to enable editing, false to disable + * + * @throws IllegalStateException + * if in edit mode and trying to disable + * @throws IllegalStateException + * if the editor handler is not set + */ + public void setEditorEnabled(boolean enabled) { + editor.setEnabled(enabled); + } + + /** + * Returns the editor widget associated with the given column. If the editor + * is not active, returns null. + * + * @param column + * the column + * @return the widget if the editor is open, null otherwise + */ + public Widget getEditorWidget(Column<?, T> column) { + return editor.getWidget(column); + } + + @Override + protected void onAttach() { + super.onAttach(); + + if (getEscalator().getBody().getRowCount() == 0 && dataSource != null) { + setEscalatorSizeFromDataSource(); + } + } + + /** + * Grid does not support adding Widgets this way. + * <p> + * This method is implemented only because removing widgets from Grid (added + * via e.g. {@link Renderer}s) requires the {@link HasWidgets} interface. + * + * @param w + * irrelevant + * @throws UnsupportedOperationException + * always + */ + @Override + @Deprecated + public void add(Widget w) { + throw new UnsupportedOperationException( + "Cannot add widgets to Grid with this method"); + } + + /** + * Grid does not support clearing Widgets this way. + * <p> + * This method is implemented only because removing widgets from Grid (added + * via e.g. {@link Renderer}s) requires the {@link HasWidgets} interface. + * + * @throws UnsupportedOperationException + * always + */ + @Override + @Deprecated + public void clear() { + throw new UnsupportedOperationException( + "Cannot clear widgets from Grid this way"); + } + + /** + * Grid does not support iterating through Widgets this way. + * <p> + * This method is implemented only because removing widgets from Grid (added + * via e.g. {@link Renderer}s) requires the {@link HasWidgets} interface. + * + * @return never + * @throws UnsupportedOperationException + * always + */ + @Override + @Deprecated + public Iterator<Widget> iterator() { + throw new UnsupportedOperationException( + "Cannot iterate through widgets in Grid this way"); + } + + /** + * Grid does not support removing Widgets this way. + * <p> + * This method is implemented only because removing widgets from Grid (added + * via e.g. {@link Renderer}s) requires the {@link HasWidgets} interface. + * + * @return always <code>false</code> + */ + @Override + @Deprecated + public boolean remove(Widget w) { + /* + * This is the method that is the sole reason to have Grid implement + * HasWidget - when Vaadin removes a Component from the hierarchy, the + * corresponding Widget will call removeFromParent() on itself. GWT will + * check there that its parent (i.e. Grid) implements HasWidgets, and + * will call this remove(Widget) method. + * + * tl;dr: all this song and dance to make sure GWT's sanity checks + * aren't triggered, even though they effectively do nothing interesting + * from Grid's perspective. + */ + return false; + } + + /** + * Accesses the package private method Widget#setParent() + * + * @param widget + * The widget to access + * @param parent + * The parent to set + */ + private static native final void setParent(Widget widget, Grid<?> parent) + /*-{ + widget.@com.google.gwt.user.client.ui.Widget::setParent(Lcom/google/gwt/user/client/ui/Widget;)(parent); + }-*/; + + /** + * Resets all cached pixel sizes and reads new values from the DOM. This + * methods should be used e.g. when styles affecting the dimensions of + * elements in this grid have been changed. + */ + public void resetSizesFromDom() { + getEscalator().resetSizesFromDom(); + } +} diff --git a/client/tests/src/com/vaadin/client/ui/grid/ListDataSourceTest.java b/client/tests/src/com/vaadin/client/ui/grid/ListDataSourceTest.java new file mode 100644 index 0000000000..24ccd6c57e --- /dev/null +++ b/client/tests/src/com/vaadin/client/ui/grid/ListDataSourceTest.java @@ -0,0 +1,192 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.ui.grid; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Comparator; + +import org.easymock.EasyMock; +import org.junit.Test; + +import com.vaadin.client.data.DataChangeHandler; +import com.vaadin.client.widget.grid.datasources.ListDataSource; + +public class ListDataSourceTest { + + @Test + public void testDataSourceConstruction() throws Exception { + + ListDataSource<Integer> ds = new ListDataSource<Integer>(0, 1, 2, 3); + + assertEquals(4, ds.size()); + assertEquals(0, (int) ds.getRow(0)); + assertEquals(1, (int) ds.getRow(1)); + assertEquals(2, (int) ds.getRow(2)); + assertEquals(3, (int) ds.getRow(3)); + + ds = new ListDataSource<Integer>(Arrays.asList(0, 1, 2, 3)); + + assertEquals(4, ds.size()); + assertEquals(0, (int) ds.getRow(0)); + assertEquals(1, (int) ds.getRow(1)); + assertEquals(2, (int) ds.getRow(2)); + assertEquals(3, (int) ds.getRow(3)); + } + + @Test + public void testListAddOperation() throws Exception { + + ListDataSource<Integer> ds = new ListDataSource<Integer>(0, 1, 2, 3); + + DataChangeHandler handler = EasyMock + .createNiceMock(DataChangeHandler.class); + ds.setDataChangeHandler(handler); + + handler.dataAdded(4, 1); + EasyMock.expectLastCall(); + + EasyMock.replay(handler); + + ds.asList().add(4); + + assertEquals(5, ds.size()); + assertEquals(0, (int) ds.getRow(0)); + assertEquals(1, (int) ds.getRow(1)); + assertEquals(2, (int) ds.getRow(2)); + assertEquals(3, (int) ds.getRow(3)); + assertEquals(4, (int) ds.getRow(4)); + } + + @Test + public void testListAddAllOperation() throws Exception { + + ListDataSource<Integer> ds = new ListDataSource<Integer>(0, 1, 2, 3); + + DataChangeHandler handler = EasyMock + .createNiceMock(DataChangeHandler.class); + ds.setDataChangeHandler(handler); + + handler.dataAdded(4, 3); + EasyMock.expectLastCall(); + + EasyMock.replay(handler); + + ds.asList().addAll(Arrays.asList(4, 5, 6)); + + assertEquals(7, ds.size()); + assertEquals(0, (int) ds.getRow(0)); + assertEquals(1, (int) ds.getRow(1)); + assertEquals(2, (int) ds.getRow(2)); + assertEquals(3, (int) ds.getRow(3)); + assertEquals(4, (int) ds.getRow(4)); + assertEquals(5, (int) ds.getRow(5)); + assertEquals(6, (int) ds.getRow(6)); + } + + @Test + public void testListRemoveOperation() throws Exception { + + ListDataSource<Integer> ds = new ListDataSource<Integer>(0, 1, 2, 3); + + DataChangeHandler handler = EasyMock + .createNiceMock(DataChangeHandler.class); + ds.setDataChangeHandler(handler); + + handler.dataRemoved(3, 1); + EasyMock.expectLastCall(); + + EasyMock.replay(handler); + + ds.asList().remove(2); + + assertEquals(3, ds.size()); + assertEquals(0, (int) ds.getRow(0)); + assertEquals(1, (int) ds.getRow(1)); + assertEquals(3, (int) ds.getRow(2)); + } + + @Test + public void testListRemoveAllOperation() throws Exception { + + ListDataSource<Integer> ds = new ListDataSource<Integer>(0, 1, 2, 3); + + DataChangeHandler handler = EasyMock + .createNiceMock(DataChangeHandler.class); + ds.setDataChangeHandler(handler); + + handler.dataRemoved(0, 3); + EasyMock.expectLastCall(); + + EasyMock.replay(handler); + + ds.asList().removeAll(Arrays.asList(0, 2, 3)); + + assertEquals(1, ds.size()); + assertEquals(1, (int) ds.getRow(0)); + } + + @Test + public void testListClearOperation() throws Exception { + + ListDataSource<Integer> ds = new ListDataSource<Integer>(0, 1, 2, 3); + + DataChangeHandler handler = EasyMock + .createNiceMock(DataChangeHandler.class); + ds.setDataChangeHandler(handler); + + handler.dataRemoved(0, 4); + EasyMock.expectLastCall(); + + EasyMock.replay(handler); + + ds.asList().clear(); + + assertEquals(0, ds.size()); + } + + @Test(expected = IllegalStateException.class) + public void testFetchingNonExistantItem() { + ListDataSource<Integer> ds = new ListDataSource<Integer>(0, 1, 2, 3); + ds.ensureAvailability(5, 1); + } + + @Test(expected = UnsupportedOperationException.class) + public void testUnsupportedIteratorRemove() { + ListDataSource<Integer> ds = new ListDataSource<Integer>(0, 1, 2, 3); + ds.asList().iterator().remove(); + } + + @Test + public void sortColumn() { + ListDataSource<Integer> ds = new ListDataSource<Integer>(3, 4, 2, 3, 1); + + // TODO Should be simplified to sort(). No point in providing these + // trivial comparators. + ds.sort(new Comparator<Integer>() { + @Override + public int compare(Integer o1, Integer o2) { + return o1.compareTo(o2); + } + }); + + assertTrue(Arrays.equals(ds.asList().toArray(), new Integer[] { 1, 2, + 3, 3, 4 })); + } + +} diff --git a/client/tests/src/com/vaadin/client/ui/grid/PartitioningTest.java b/client/tests/src/com/vaadin/client/ui/grid/PartitioningTest.java new file mode 100644 index 0000000000..e97bb339e4 --- /dev/null +++ b/client/tests/src/com/vaadin/client/ui/grid/PartitioningTest.java @@ -0,0 +1,104 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed 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 com.vaadin.client.ui.grid; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import com.vaadin.shared.ui.grid.Range; + +@SuppressWarnings("static-method") +public class PartitioningTest { + + @Test + public void selfRangeTest() { + final Range range = Range.between(0, 10); + final Range[] partitioning = range.partitionWith(range); + + assertTrue("before is empty", partitioning[0].isEmpty()); + assertTrue("inside is self", partitioning[1].equals(range)); + assertTrue("after is empty", partitioning[2].isEmpty()); + } + + @Test + public void beforeRangeTest() { + final Range beforeRange = Range.between(0, 10); + final Range afterRange = Range.between(10, 20); + final Range[] partitioning = beforeRange.partitionWith(afterRange); + + assertTrue("before is self", partitioning[0].equals(beforeRange)); + assertTrue("inside is empty", partitioning[1].isEmpty()); + assertTrue("after is empty", partitioning[2].isEmpty()); + } + + @Test + public void afterRangeTest() { + final Range beforeRange = Range.between(0, 10); + final Range afterRange = Range.between(10, 20); + final Range[] partitioning = afterRange.partitionWith(beforeRange); + + assertTrue("before is empty", partitioning[0].isEmpty()); + assertTrue("inside is empty", partitioning[1].isEmpty()); + assertTrue("after is self", partitioning[2].equals(afterRange)); + } + + @Test + public void beforeAndInsideRangeTest() { + final Range beforeRange = Range.between(0, 10); + final Range afterRange = Range.between(5, 15); + final Range[] partitioning = beforeRange.partitionWith(afterRange); + + assertEquals("before", Range.between(0, 5), partitioning[0]); + assertEquals("inside", Range.between(5, 10), partitioning[1]); + assertTrue("after is empty", partitioning[2].isEmpty()); + } + + @Test + public void insideRangeTest() { + final Range fullRange = Range.between(0, 20); + final Range insideRange = Range.between(5, 15); + final Range[] partitioning = insideRange.partitionWith(fullRange); + + assertTrue("before is empty", partitioning[0].isEmpty()); + assertEquals("inside", Range.between(5, 15), partitioning[1]); + assertTrue("after is empty", partitioning[2].isEmpty()); + } + + @Test + public void insideAndBelowTest() { + final Range beforeRange = Range.between(0, 10); + final Range afterRange = Range.between(5, 15); + final Range[] partitioning = afterRange.partitionWith(beforeRange); + + assertTrue("before is empty", partitioning[0].isEmpty()); + assertEquals("inside", Range.between(5, 10), partitioning[1]); + assertEquals("after", Range.between(10, 15), partitioning[2]); + } + + @Test + public void aboveAndBelowTest() { + final Range fullRange = Range.between(0, 20); + final Range insideRange = Range.between(5, 15); + final Range[] partitioning = fullRange.partitionWith(insideRange); + + assertEquals("before", Range.between(0, 5), partitioning[0]); + assertEquals("inside", Range.between(5, 15), partitioning[1]); + assertEquals("after", Range.between(15, 20), partitioning[2]); + } +} |