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.

BrowserWindowOpenerConnector.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2000-2016 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.extensions;
  17. import java.util.Map.Entry;
  18. import com.google.gwt.event.dom.client.ClickEvent;
  19. import com.google.gwt.event.dom.client.ClickHandler;
  20. import com.google.gwt.http.client.URL;
  21. import com.google.gwt.user.client.Window;
  22. import com.google.gwt.user.client.ui.Widget;
  23. import com.vaadin.client.ComponentConnector;
  24. import com.vaadin.client.ServerConnector;
  25. import com.vaadin.server.BrowserWindowOpener;
  26. import com.vaadin.shared.ui.BrowserWindowOpenerState;
  27. import com.vaadin.shared.ui.Connect;
  28. import com.vaadin.shared.util.SharedUtil;
  29. /**
  30. * Client-side code for {@link BrowserWindowOpener}
  31. *
  32. * @author Vaadin Ltd
  33. * @since 7.0.0
  34. */
  35. @Connect(BrowserWindowOpener.class)
  36. public class BrowserWindowOpenerConnector extends AbstractExtensionConnector
  37. implements ClickHandler {
  38. @Override
  39. protected void extend(ServerConnector target) {
  40. final Widget targetWidget = ((ComponentConnector) target).getWidget();
  41. targetWidget.addDomHandler(this, ClickEvent.getType());
  42. }
  43. @Override
  44. public BrowserWindowOpenerState getState() {
  45. return (BrowserWindowOpenerState) super.getState();
  46. }
  47. @Override
  48. public void onClick(ClickEvent event) {
  49. String url = getResourceUrl(BrowserWindowOpenerState.locationResource);
  50. url = addParametersAndFragment(url);
  51. if (url != null) {
  52. Window.open(url, getState().target, getState().features);
  53. }
  54. }
  55. private String addParametersAndFragment(String url) {
  56. if (url == null) {
  57. return null;
  58. }
  59. if (!getState().parameters.isEmpty()) {
  60. StringBuilder params = new StringBuilder();
  61. for (Entry<String, String> entry : getState().parameters
  62. .entrySet()) {
  63. if (params.length() != 0) {
  64. params.append('&');
  65. }
  66. params.append(URL.encodeQueryString(entry.getKey()));
  67. params.append('=');
  68. String value = entry.getValue();
  69. if (value != null) {
  70. params.append(URL.encodeQueryString(value));
  71. }
  72. }
  73. url = SharedUtil.addGetParameters(url, params.toString());
  74. }
  75. if (getState().uriFragment != null) {
  76. // Replace previous fragment or just add to the end of the url
  77. url = url.replaceFirst("#.*|$", "#" + getState().uriFragment);
  78. }
  79. return url;
  80. }
  81. }