/src/main/java/org/nsesa/editor/gwt/amendment/client/ui/document/amendments/filter/AmendmentsFilterController.java

https://bitbucket.org/pluppens/nsesa-editor · Java · 156 lines · 81 code · 17 blank · 58 comment · 5 complexity · f4e7a64a45c0827e3aa66fe0395d5443 MD5 · raw file

  1. /**
  2. * Copyright 2013 European Parliament
  3. *
  4. * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
  5. * You may not use this work except in compliance with the Licence.
  6. * You may obtain a copy of the Licence at:
  7. *
  8. * http://joinup.ec.europa.eu/software/page/eupl
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the Licence for the specific language governing permissions and limitations under the Licence.
  13. */
  14. package org.nsesa.editor.gwt.amendment.client.ui.document.amendments.filter;
  15. import com.google.gwt.event.dom.client.ChangeEvent;
  16. import com.google.gwt.event.dom.client.ChangeHandler;
  17. import com.google.gwt.event.shared.HandlerRegistration;
  18. import com.google.inject.Inject;
  19. import com.google.inject.Singleton;
  20. import com.google.inject.name.Named;
  21. import org.nsesa.editor.gwt.amendment.client.ui.amendment.AmendmentController;
  22. import org.nsesa.editor.gwt.amendment.client.ui.document.amendments.filter.resources.Constants;
  23. import org.nsesa.editor.gwt.core.client.event.filter.FilterRequestEvent;
  24. import org.nsesa.editor.gwt.core.client.ui.document.DocumentController;
  25. import org.nsesa.editor.gwt.core.client.ui.document.DocumentEventBus;
  26. import org.nsesa.editor.gwt.core.client.util.Filter;
  27. import org.nsesa.editor.gwt.core.client.util.Selection;
  28. import java.util.Arrays;
  29. import java.util.LinkedHashMap;
  30. import java.util.Map;
  31. /**
  32. * <code>AmendmentsFilterController</code> class is responsible to set up the filters that will be available in
  33. * {@link AmendmentsFilterView} view and to raise {@link FilterRequestEvent} events as soon as the user select a
  34. * filter form the view.
  35. *
  36. * @author <a href="mailto:stelian.groza@gmail.com">Stelian Groza</a>
  37. * Date: 26/11/12 13:44
  38. */
  39. @Singleton
  40. public class AmendmentsFilterController {
  41. /**
  42. * Used to fire GWT events
  43. */
  44. private DocumentEventBus documentEventBus;
  45. /**
  46. * The view associated to this controller
  47. */
  48. private AmendmentsFilterView view;
  49. private int amendmentsPerPage;
  50. /**
  51. * A map with registered filters
  52. */
  53. private final Map<String, Filter<AmendmentController>> filters = new LinkedHashMap<String, Filter<AmendmentController>>();
  54. private HandlerRegistration changeHandlerRegistration;
  55. private final Constants constants;
  56. private final Selection<AmendmentController> ALL = new Selection.AllSelection<AmendmentController>();
  57. private final Selection<AmendmentController> APPLIED = new Selection<AmendmentController>() {
  58. @Override
  59. public boolean select(AmendmentController amendmentController) {
  60. return amendmentController.getDocumentController() != null;
  61. }
  62. };
  63. private final Selection<AmendmentController> LANGUAGE = new Selection<AmendmentController>() {
  64. @Override
  65. public boolean select(AmendmentController amendmentController) {
  66. final DocumentController documentController = amendmentController.getDocumentController();
  67. return documentController != null && documentController.getDocument().getLanguageIso().equalsIgnoreCase(amendmentController.getModel().getLanguageISO());
  68. }
  69. };
  70. /**
  71. * Create <code>AmendmentsFilterController</code> with the given parameters
  72. *
  73. * @param documentEventBus The event bus linked to the controller
  74. * @param view The associated view
  75. */
  76. @Inject
  77. public AmendmentsFilterController(DocumentEventBus documentEventBus, AmendmentsFilterView view,
  78. @Named("amendmentsPerPage") int amendmentsPerPage,
  79. Constants constants) {
  80. this.documentEventBus = documentEventBus;
  81. this.view = view;
  82. this.amendmentsPerPage = amendmentsPerPage;
  83. this.constants = constants;
  84. registerFilterActions();
  85. registerListeners();
  86. this.view.setFilters(Arrays.asList(filters.keySet().toArray(new String[filters.size()])));
  87. }
  88. /**
  89. * Register the actions that will be displayed in the view
  90. */
  91. protected void registerFilterActions() {
  92. registerFilterAction(constants.amendmentsHeaderFilterAll(),
  93. new Filter<AmendmentController>(0, amendmentsPerPage, AmendmentController.ORDER_COMPARATOR, ALL));
  94. registerFilterAction(constants.amendmentsHeaderFilterApplied(),
  95. new Filter<AmendmentController>(0, amendmentsPerPage, AmendmentController.ORDER_COMPARATOR, APPLIED));
  96. registerFilterAction(constants.amendmentsHeaderFilterLanguage(),
  97. new Filter<AmendmentController>(0, amendmentsPerPage, AmendmentController.ORDER_COMPARATOR, LANGUAGE));
  98. }
  99. /**
  100. * Add a filter in the list of filter actions available in the view
  101. *
  102. * @param filterName The filter name as String
  103. * @param filter The filter representation
  104. */
  105. public void registerFilterAction(String filterName, Filter<AmendmentController> filter) {
  106. filters.put(filterName, filter);
  107. }
  108. /**
  109. * Add a change handler for {@link org.nsesa.editor.gwt.amendment.client.ui.document.amendments.filter.AmendmentsFilterView#getFilter()}
  110. * and raise a new {@link FilterRequestEvent} GWT event
  111. */
  112. protected void registerListeners() {
  113. changeHandlerRegistration = view.getFilter().addChangeHandler(new ChangeHandler() {
  114. @Override
  115. public void onChange(ChangeEvent event) {
  116. Filter<AmendmentController> filter = filters.get(view.getSelectedFilter());
  117. if (filter != null) {
  118. //set the start page to 0
  119. filter.setStart(0);
  120. documentEventBus.fireEvent(new FilterRequestEvent(filter));
  121. }
  122. }
  123. });
  124. }
  125. /**
  126. * Removes all registered event handlers from the event bus and UI.
  127. */
  128. public void removeListeners() {
  129. changeHandlerRegistration.removeHandler();
  130. }
  131. /**
  132. * Returns the view associated to the controller
  133. *
  134. * @return the view as AmendmentsFilterView
  135. */
  136. public AmendmentsFilterView getView() {
  137. return view;
  138. }
  139. }