| /** |
| * |
| * Copyright (c) 2011, 2016 - 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 v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| * |
| */ |
| package org.eclipse.osbp.utils.vaadin; |
| |
| import com.vaadin.ui.Button; |
| import com.vaadin.ui.Button.ClickEvent; |
| import com.vaadin.ui.ComponentContainer; |
| |
| public class ToogleButtonGroup implements Button.ClickListener { |
| |
| private static final long serialVersionUID = 2979031557232247622L; |
| |
| public static class ToolButton extends Button { |
| private static final long serialVersionUID = 5343059734182170255L; |
| |
| public ToolButton(String name, String styleName) { |
| this(name, styleName, true); |
| } |
| |
| public ToolButton(String name, String styleName, Button.ClickListener clickListener) { |
| super(name, clickListener); |
| addStyleName("C-TOOLBUTTON"); |
| addStyleName(styleName); |
| } |
| |
| public ToolButton(String name, String styleName, boolean enabled) { |
| super(name); |
| addStyleName("C-TOOLBUTTON"); |
| addStyleName(styleName); |
| setEnabled(enabled); |
| } |
| } |
| |
| public static class ToggleButtonDefinition { |
| |
| protected final String fText; |
| protected final Object fId; |
| protected final String fStyleName; |
| protected final Button.ClickListener fClickListener; |
| protected Button fButton; |
| |
| public ToggleButtonDefinition(String text, Object id, String styleName, Button.ClickListener clickListener) { |
| fText = text; |
| fId = id; |
| fStyleName = styleName; |
| fClickListener = clickListener; |
| } |
| } |
| |
| private final ToggleButtonDefinition[] fToggleButtons; |
| |
| public ToogleButtonGroup(String groupStyleName, ToggleButtonDefinition... toggleButtons) { |
| fToggleButtons = toggleButtons; |
| for (int i = 0; i < toggleButtons.length; i++) { |
| toggleButtons[i].fButton = new ToolButton(toggleButtons[i].fText, toggleButtons[i].fStyleName, toggleButtons[i].fClickListener); |
| toggleButtons[i].fButton.addClickListener(this); |
| toggleButtons[i].fButton.addStyleName(groupStyleName); |
| toggleButtons[i].fButton.addStyleName("C-TOGGLE-GROUP"); |
| if (i == 0) { |
| toggleButtons[i].fButton.addStyleName("C-FIRST"); |
| } |
| else if (i == toggleButtons.length-1) { |
| toggleButtons[i].fButton.addStyleName("C-LAST"); |
| } |
| } |
| } |
| |
| public void addToParent(ComponentContainer parent) { |
| for (ToggleButtonDefinition toggleButton : fToggleButtons) { |
| parent.addComponent(toggleButton.fButton); |
| } |
| } |
| |
| public void fireClick(Object id) { |
| for (ToggleButtonDefinition toggleButton : fToggleButtons) { |
| if (id.equals(toggleButton.fId)) { |
| ClickEvent event = new ClickEvent(toggleButton.fButton); |
| for (Object clickListener : toggleButton.fButton.getListeners(ClickEvent.class)) { |
| if (clickListener instanceof Button.ClickListener) { |
| ((Button.ClickListener)clickListener).buttonClick(event); |
| } |
| } |
| } |
| } |
| } |
| |
| public void fireActive(Object id) { |
| for (ToggleButtonDefinition toggleButton : fToggleButtons) { |
| if (id.equals(toggleButton.fId)) { |
| toggleButton.fButton.addStyleName("C-ACTIVE"); |
| } |
| else { |
| toggleButton.fButton.removeStyleName("C-ACTIVE"); |
| } |
| } |
| } |
| |
| @Override |
| public void buttonClick(ClickEvent event) { |
| for (ToggleButtonDefinition toggleButton : fToggleButtons) { |
| if (event.getSource().equals(toggleButton.fButton)) { |
| fireActive(toggleButton.fId); |
| } |
| } |
| } |
| } |