You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractSelectionConnector.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.connectors.selection;
  17. import com.vaadin.client.ServerConnector;
  18. import com.vaadin.client.connectors.components.AbstractListingConnector;
  19. import com.vaadin.client.connectors.data.HasSelection;
  20. import com.vaadin.client.data.selection.SelectionModel;
  21. import com.vaadin.client.extensions.AbstractExtensionConnector;
  22. import com.vaadin.shared.data.typed.DataProviderConstants;
  23. import elemental.json.JsonObject;
  24. public abstract class AbstractSelectionConnector extends
  25. AbstractExtensionConnector {
  26. private SelectionModel model = null;
  27. @Override
  28. protected void extend(ServerConnector target) {
  29. if (!(target instanceof HasSelection)) {
  30. throw new UnsupportedOperationException(
  31. "SelectionModel extending a Connector without HasDataSource");
  32. }
  33. // TODO: Provide SelectionModel API
  34. // TODO: Should this use "Registration" approach for easy and safe
  35. // removal?
  36. model = createSelectionModel();
  37. ((HasSelection) target).setSelectionModel(getSelectionModel());
  38. }
  39. /**
  40. * Creates a selection model object to be used by the Connector.
  41. *
  42. * @return created selection model
  43. */
  44. protected abstract SelectionModel createSelectionModel();
  45. protected static String getKey(JsonObject item) {
  46. return item.getString(DataProviderConstants.KEY);
  47. }
  48. protected static boolean jsonEquals(JsonObject a, JsonObject b) {
  49. final String key = DataProviderConstants.KEY;
  50. if (a != null && b != null) {
  51. if (a.hasKey(key) && b.hasKey(key)) {
  52. return a.getString(key).equals(b.getString(key));
  53. }
  54. }
  55. return a == b;
  56. }
  57. protected static boolean hasSelectedKey(JsonObject item) {
  58. String key = DataProviderConstants.SELECTED;
  59. return item.hasKey(key) && item.getBoolean(key);
  60. }
  61. @Override
  62. public AbstractListingConnector getParent() {
  63. return (AbstractListingConnector) super.getParent();
  64. }
  65. protected SelectionModel getSelectionModel() {
  66. return model;
  67. }
  68. }