Created
May 3, 2016 16:47
-
-
Save mkgl/c6178761bfb9000062f8910bdaf51804 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This file Copyright (c) 2016 Magnolia International | |
* Ltd. (http://www.magnolia-cms.com). All rights reserved. | |
* | |
* | |
* This file is dual-licensed under both the Magnolia | |
* Network Agreement and the GNU General Public License. | |
* You may elect to use one or the other of these licenses. | |
* | |
* This file is distributed in the hope that it will be | |
* useful, but AS-IS and WITHOUT ANY WARRANTY; without even the | |
* implied warranty of MERCHANTABILITY or FITNESS FOR A | |
* PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. | |
* Redistribution, except as permitted by whichever of the GPL | |
* or MNA you select, is prohibited. | |
* | |
* 1. For the GPL license (GPL), you can redistribute and/or | |
* modify this file under the terms of the GNU General | |
* Public License, Version 3, as published by the Free Software | |
* Foundation. You should have received a copy of the GNU | |
* General Public License, Version 3 along with this program; | |
* if not, write to the Free Software Foundation, Inc., 51 | |
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
* | |
* 2. For the Magnolia Network Agreement (MNA), this file | |
* and the accompanying materials are made available under the | |
* terms of the MNA which accompanies this distribution, and | |
* is available at http://www.magnolia-cms.com/mna.html | |
* | |
* Any modifications to this file must keep this entire header | |
* intact. | |
* | |
*/ | |
package me.mkgl.sandbox.vaadin; | |
import static com.vaadin.server.Sizeable.Unit.PIXELS; | |
import static java.util.stream.Collectors.*; | |
import java.util.Arrays; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.Optional; | |
import java.util.TimeZone; | |
import javax.servlet.annotation.WebServlet; | |
import com.vaadin.annotations.Theme; | |
import com.vaadin.annotations.VaadinServletConfiguration; | |
import com.vaadin.data.Property; | |
import com.vaadin.data.util.BeanItemContainer; | |
import com.vaadin.server.Page; | |
import com.vaadin.server.VaadinRequest; | |
import com.vaadin.server.VaadinServlet; | |
import com.vaadin.server.WebBrowser; | |
import com.vaadin.shared.ui.combobox.FilteringMode; | |
import com.vaadin.shared.ui.datefield.Resolution; | |
import com.vaadin.ui.AbstractSelect.ItemCaptionMode; | |
import com.vaadin.ui.ComboBox; | |
import com.vaadin.ui.DateField; | |
import com.vaadin.ui.HorizontalLayout; | |
import com.vaadin.ui.Notification; | |
import com.vaadin.ui.PopupDateField; | |
import com.vaadin.ui.UI; | |
import com.vaadin.ui.VerticalLayout; | |
@Theme("valo") | |
public class DateFieldUI extends UI { | |
@WebServlet(value = "/date/*", asyncSupported = true) | |
@VaadinServletConfiguration(productionMode = false, ui = DateFieldUI.class) | |
public static class Servlet extends VaadinServlet { | |
} | |
@Override | |
protected void init(VaadinRequest request) { | |
setSizeFull(); | |
VerticalLayout layout = new VerticalLayout(); | |
layout.setMargin(true); | |
layout.setSpacing(true); | |
layout.setSizeFull(); | |
WebBrowser browserInfo = Page.getCurrent().getWebBrowser(); | |
int rawOffset = browserInfo.getRawTimezoneOffset(); | |
boolean dst = browserInfo.isDSTInEffect(); | |
int dstSavings = browserInfo.getDSTSavings(); | |
Date requestDate = browserInfo.getCurrentDate(); | |
List<TimeZone> timeZoneCandidates = Arrays.stream(TimeZone.getAvailableIDs()) | |
// List<TimeZone> timeZoneCandidates = Arrays.stream(TimeZone.getAvailableIDs(rawOffset)) | |
.map(TimeZone::getTimeZone) | |
// .filter(z -> z.inDaylightTime(requestDate) == dst) | |
// .filter(z -> z.getDSTSavings() == dstSavings) | |
.collect(toList()); | |
System.out.println(timeZoneCandidates.size()); | |
System.out.println(timeZoneCandidates.stream() | |
.map(TimeZone::getID) | |
.collect(joining("\n", "\n-----\n", "\n-----\n"))); | |
Optional<TimeZone> timeZone = timeZoneCandidates.stream().findFirst(); | |
System.out.println(timeZone); | |
DateField testDate = new PopupDateField("Date"); | |
testDate.setResolution(Resolution.MINUTE); | |
testDate.setWidth(400, PIXELS); | |
testDate.addValueChangeListener(this::logDate); | |
ComboBox testTimeZone = new ComboBox("Time zone", new BeanItemContainer<>(TimeZone.class, timeZoneCandidates)); | |
testTimeZone.setItemCaptionMode(ItemCaptionMode.PROPERTY); | |
testTimeZone.setItemCaptionPropertyId("ID"); | |
testTimeZone.setNullSelectionAllowed(false); | |
testTimeZone.setFilteringMode(FilteringMode.CONTAINS); | |
testTimeZone.addValueChangeListener(e -> { | |
TimeZone newTimeZone = (TimeZone) e.getProperty().getValue(); | |
testDate.setTimeZone(newTimeZone); | |
logTimeZone(newTimeZone); | |
}); | |
if (timeZone.isPresent()) { | |
testTimeZone.setValue(timeZone.get()); | |
} | |
testDate.setValue(requestDate); | |
HorizontalLayout testDateRow = new HorizontalLayout(testTimeZone, testDate); | |
testDateRow.setCaption("Test schedule"); | |
testDateRow.setSpacing(true); | |
layout.addComponent(testDateRow); | |
setContent(layout); | |
} | |
private void logDate(Property.ValueChangeEvent e) { | |
String message = "Date changed to " + e.getProperty().getValue() + " (Server time)"; | |
Notification.show(message); | |
System.out.println(message); | |
} | |
private void logTimeZone(TimeZone newTimeZone) { | |
String message = "Time zone changed to " + newTimeZone.getID(); | |
Notification.show(message); | |
System.out.println(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment