| /** |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| */ |
| package org.eclipse.osbp.runtime.web.vaadin.components.dialogs; |
| |
| import org.eclipse.osbp.runtime.web.vaadin.common.resource.IResourceProvider; |
| |
| import com.vaadin.server.Resource; |
| import com.vaadin.ui.AbstractComponentContainer; |
| import com.vaadin.ui.Button; |
| import com.vaadin.ui.Button.ClickEvent; |
| import com.vaadin.ui.HorizontalLayout; |
| import com.vaadin.ui.NativeButton; |
| import com.vaadin.ui.UI; |
| import com.vaadin.ui.VerticalLayout; |
| import com.vaadin.ui.Window; |
| import com.vaadin.ui.Window.CloseEvent; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * The Class AbstractDialog. |
| */ |
| public abstract class AbstractDialog { |
| |
| /** The config. */ |
| protected final DialogConfig config; |
| |
| /** The options. */ |
| protected final Option[] options; |
| |
| /** The window. */ |
| protected Window window; |
| |
| /** The main layout. */ |
| protected VerticalLayout mainLayout; |
| |
| /** The custom area. */ |
| protected AbstractComponentContainer customArea; |
| |
| /** The options area. */ |
| protected HorizontalLayout optionsArea; |
| |
| /** |
| * Create resource for the given iconPath. |
| * |
| * @param iconPath |
| * the icon path |
| * @param resourceProvider |
| * the resource provider |
| * @return the resource |
| */ |
| protected static Resource createResource(String iconPath, |
| IResourceProvider resourceProvider) { |
| if (resourceProvider == null) { |
| return null; |
| } |
| return resourceProvider.getResource(iconPath); |
| } |
| |
| /** |
| * Instantiates a new abstract dialog. |
| * |
| * @param config |
| * the config |
| * @param options |
| * the options |
| */ |
| protected AbstractDialog(DialogConfig config, Option... options) { |
| this.config = config; |
| this.options = options; |
| } |
| |
| /** |
| * Open. |
| */ |
| @SuppressWarnings("serial") |
| protected void open() { |
| |
| prepareLayout(); |
| |
| configDialog(); |
| |
| prepareOptions(); |
| |
| window.addCloseListener(new Window.CloseListener() { |
| @Override |
| public void windowClose(CloseEvent e) { |
| close(); |
| } |
| }); |
| |
| UI.getCurrent().addWindow(window); |
| } |
| |
| /** |
| * Prepare the options. |
| */ |
| protected void prepareOptions() { |
| if (options != null) { |
| @SuppressWarnings("serial") |
| Button.ClickListener listener = new Button.ClickListener() { |
| @Override |
| public void buttonClick(ClickEvent event) { |
| Button button = event.getButton(); |
| execute((Option) button.getData()); |
| } |
| }; |
| |
| for (Option option : options) { |
| NativeButton button = new NativeButton(option.getName()); |
| optionsArea.addComponent(button); |
| button.setIcon(option.getIcon()); |
| button.setDescription(option.getDescription()); |
| button.setData(option); |
| button.addClickListener(listener); |
| } |
| } |
| } |
| |
| /** |
| * Config the dialog. |
| */ |
| protected void configDialog() { |
| |
| if (config != null) { |
| window.setCaption(config.getDialogName()); |
| window.setDescription(config.getDescription()); |
| |
| // use callback |
| config.config(window); |
| } |
| |
| } |
| |
| /** |
| * Prepare the main layout. |
| */ |
| protected abstract void prepareLayout(); |
| |
| /** |
| * Close the window. |
| */ |
| protected void close() { |
| if (window != null) { |
| window.close(); |
| window = null; |
| } |
| } |
| |
| /** |
| * Execute the runnable. |
| * |
| * @param option |
| * the option |
| */ |
| protected void execute(Option option) { |
| if (option.canExecute()) { |
| if (option.getRunnable() != null) { |
| option.getRunnable().run(); |
| } |
| close(); |
| } |
| } |
| |
| /** |
| * Information how to config the dialog. Clients may override |
| * {@link #config(Window)} to get access about the window before opening. So |
| * clients may set size, position,... |
| */ |
| public static class DialogConfig { |
| |
| /** The dialog name. */ |
| private final String dialogName; |
| |
| /** The message. */ |
| private final String message; |
| |
| /** The description. */ |
| private final String description; |
| |
| /** The icon. */ |
| private final Resource icon; |
| |
| /** |
| * Instantiates a new dialog config. |
| * |
| * @param dialogName |
| * the dialog name |
| * @param message |
| * the message |
| * @param description |
| * the description |
| * @param icon |
| * the icon |
| */ |
| public DialogConfig(String dialogName, String message, |
| String description, Resource icon) { |
| super(); |
| this.dialogName = dialogName; |
| this.message = message; |
| this.description = description; |
| this.icon = icon; |
| } |
| |
| /** |
| * Clients may override to config the window before opening. |
| * |
| * @param window |
| * the window |
| */ |
| public void config(Window window) { |
| |
| } |
| |
| /** |
| * Gets the dialog name. |
| * |
| * @return the dialogName |
| */ |
| protected String getDialogName() { |
| return dialogName; |
| } |
| |
| /** |
| * Gets the message. |
| * |
| * @return the message |
| */ |
| protected String getMessage() { |
| return message; |
| } |
| |
| /** |
| * Gets the description. |
| * |
| * @return the description |
| */ |
| protected String getDescription() { |
| return description; |
| } |
| |
| /** |
| * Gets the icon. |
| * |
| * @return the icon |
| */ |
| protected Resource getIcon() { |
| return icon; |
| } |
| |
| } |
| |
| /** |
| * Represents a single option. |
| */ |
| public static class Option { |
| |
| /** The name. */ |
| private String name; |
| |
| /** The description. */ |
| private String description; |
| |
| /** The icon. */ |
| private Resource icon; |
| |
| /** The runnable. */ |
| private Runnable runnable; |
| |
| /** |
| * Instantiates a new option. |
| * |
| * @param name |
| * the name |
| * @param description |
| * the description |
| * @param icon |
| * the icon |
| * @param runnable |
| * the runnable |
| */ |
| public Option(String name, String description, Resource icon, |
| Runnable runnable) { |
| super(); |
| this.name = name; |
| this.description = description; |
| this.icon = icon; |
| this.runnable = runnable; |
| } |
| |
| /** |
| * Gets the name. |
| * |
| * @return the name |
| */ |
| protected String getName() { |
| return name; |
| } |
| |
| /** |
| * Sets the name. |
| * |
| * @param name |
| * the name to set |
| */ |
| protected void setName(String name) { |
| this.name = name; |
| } |
| |
| /** |
| * Gets the description. |
| * |
| * @return the description |
| */ |
| protected String getDescription() { |
| return description; |
| } |
| |
| /** |
| * Sets the description. |
| * |
| * @param description |
| * the description to set |
| */ |
| protected void setDescription(String description) { |
| this.description = description; |
| } |
| |
| /** |
| * Gets the icon. |
| * |
| * @return the icon |
| */ |
| protected Resource getIcon() { |
| return icon; |
| } |
| |
| /** |
| * Sets the icon. |
| * |
| * @param icon |
| * the icon to set |
| */ |
| protected void setIcon(Resource icon) { |
| this.icon = icon; |
| } |
| |
| /** |
| * Gets the runnable. |
| * |
| * @return the runnable |
| */ |
| protected Runnable getRunnable() { |
| return runnable; |
| } |
| |
| /** |
| * Sets the runnable. |
| * |
| * @param runnable |
| * the runnable to set |
| */ |
| protected void setRunnable(Runnable runnable) { |
| this.runnable = runnable; |
| } |
| |
| /** |
| * Can execute. |
| * |
| * @return true, if successful |
| */ |
| public boolean canExecute() { |
| return true; |
| } |
| |
| } |
| |
| } |