[Vaadin] – Create a simple twin column modal multi-select dialog

Vaadin is an open source Web application framework for rich Internet applications. In contrast to JavaScript libraries and browser-plugin based solutions, it features a server-side architecture, which means that the majority of the logic runs on the servers. Ajax technology is used at the browser-side to ensure a rich and interactive user experience. On the client-side Vaadin is built on top of and can be extended with Google Web Toolkit.

To let the user interact and enter or change data, you can use simple text fields. But, to keep data consitent, sometimes you want to make only specific values available and let the user select one ore more of the values.

Here is a small sample of a twin column modal select dialog box. It uses only Vaadins basic features, so no 3rd party plugins are needed.

Here is, how it looks

TwinColl

And here is the source code

package org.bluemix.challenge;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.ShortcutAction;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.TwinColSelect;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.CloseEvent;
import com.vaadin.ui.Window.CloseListener;

@Theme("mytheme")
@Widgetset("org.bluemix.challenge.MyAppWidgetset")
@SuppressWarnings("serial")
public class MyUI extends UI {
	private static final int OPTION_COUNT = 6;

	public String selectedOptions = "";

	@Override
	protected void init(VaadinRequest vaadinRequest) {

		final VerticalLayout layout = new VerticalLayout();
		layout.setMargin(true);
		setContent(layout);

		Button button = new Button("Click Me");
		button.addClickListener(new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {

				final Window dialog = new Window("Select one or more ...");
				dialog.setWidth(400.0f, Unit.PIXELS);
				dialog.setModal(true);
				dialog.setClosable(false);
				dialog.setResizable(false);

				// register esc key as close shortcut
				dialog.setCloseShortcut(ShortcutAction.KeyCode.ESCAPE);

				TwinColSelect twinColl = new TwinColSelect();

				for (int i = 0; i < OPTION_COUNT; i++) {
					twinColl.addItem(i);
					twinColl.setItemCaption(i, "Option " + i);
				}

				twinColl.setRows(OPTION_COUNT);
				twinColl.setNullSelectionAllowed(true);
				twinColl.setMultiSelect(true);
				twinColl.setImmediate(true);
				twinColl.setLeftColumnCaption("Available options");
				twinColl.setRightColumnCaption("Selected options");
				twinColl.setWidth(95.0f, Unit.PERCENTAGE);

				twinColl.addValueChangeListener(new ValueChangeListener() {

					@Override
					public void valueChange(ValueChangeEvent event) {
						selectedOptions = String.valueOf(event.getProperty().getValue());
						
					}
				});

				final FormLayout dialogContent = new FormLayout();

				dialogContent.addComponents(twinColl);
				dialogContent.addComponent(new Button("Done", new Button.ClickListener() {

					public void buttonClick(ClickEvent event) {
						UI.getCurrent().removeWindow(dialog);
					}
				}));

				dialog.addCloseListener(new CloseListener() {
					@Override
					public void windowClose(CloseEvent e) {
						System.out.println(selectedOptions);
					}
				});

				dialog.setContent(dialogContent);
				UI.getCurrent().addWindow(dialog);
			}
		});
		layout.addComponent(button);
	}

	@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
	@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
	public static class MyUIServlet extends VaadinServlet {
		private static final long serialVersionUID = 452468769467758600L;
	}
}

When the dialog is closed, it prints the selected options to the console.

The dialog can be closed by hitting the ESC key. Also here the code returns the selected options ( if any )

dialog.setCloseShortcut(ShortcutAction.KeyCode.ESCAPE);

This is just a basic sample; you can create a custom component and also pass the available options using a bean or whatever suites best for your need.

 


Latest Windows 10 update completely wrecked my dev environment

I am doing dev with Visual Studio and other tools and programs on a Windows 10 VM. Today, I decided to check for updates and install them, because I had not done any update for the past 2 month.

In general, these updates do not d any harm to the installed system, but today was different. The update took very long, and it looked like just the same when upgrading from Windows 8.1 to Windows 10. A couple of restarts, hints that all my data is still there, where I put it, and other nice hints that nothing special will happen.

Half an hour later, the system showed the logon screen. After login, The desktop was empty and an error message appeared, telling me, that The system can no longer access my shared MacBook drive. Network settings were completely overwritten, all system environment variables gone. Visual Studio had no longer any clue, where to find my libraries …

So I had to go back to the last working Windows version. To my very surprise, this worked. And, it was quick. It took only 5 minutes to restore the prior version.

restorewin

All my tools and drives are back.