Java tutorial
/* * Copyright (c) 2008-2016 Haulmont. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.haulmont.cuba.web.gui.components; import com.haulmont.bali.util.Preconditions; import com.haulmont.cuba.gui.ComponentsHelper; import com.haulmont.cuba.gui.components.Component; import com.haulmont.cuba.gui.components.Frame; import com.haulmont.cuba.gui.components.KeyCombination; import com.haulmont.cuba.gui.components.ScrollBoxLayout; import com.haulmont.cuba.web.toolkit.ui.CubaHorizontalActionsLayout; import com.haulmont.cuba.web.toolkit.ui.CubaScrollBoxLayout; import com.haulmont.cuba.web.toolkit.ui.CubaVerticalActionsLayout; import com.vaadin.event.ShortcutListener; import com.vaadin.server.Sizeable; import com.vaadin.shared.ui.MarginInfo; import com.vaadin.ui.AbstractOrderedLayout; import org.apache.commons.lang.StringUtils; import javax.annotation.Nullable; import java.util.*; public class WebScrollBoxLayout extends WebAbstractComponent<CubaScrollBoxLayout> implements ScrollBoxLayout { protected static final String SCROLLBOX_CONTENT_STYLENAME = "c-scrollbox-content"; protected static final String SCROLLBOX_STYLENAME = "c-scrollbox"; protected List<Component> ownComponents = new ArrayList<>(); protected Orientation orientation = Orientation.VERTICAL; protected ScrollBarPolicy scrollBarPolicy = ScrollBarPolicy.VERTICAL; protected Map<Component.ShortcutAction, ShortcutListener> shortcuts; public WebScrollBoxLayout() { component = new CubaScrollBoxLayout(); component.setWidth("100%"); component.setPrimaryStyleName(SCROLLBOX_STYLENAME); CubaVerticalActionsLayout content = new CubaVerticalActionsLayout(); content.setWidth("100%"); content.setStyleName(SCROLLBOX_CONTENT_STYLENAME); component.addComponent(content); getContent().setMargin(false); } protected AbstractOrderedLayout getContent() { return (AbstractOrderedLayout) component.getComponent(0); } @Override public void add(Component childComponent) { add(childComponent, ownComponents.size()); } @Override public void add(Component childComponent, int index) { if (childComponent.getParent() != null && childComponent.getParent() != this) { throw new IllegalStateException("Component already has parent"); } AbstractOrderedLayout newContent = null; if (orientation == Orientation.VERTICAL && !(getContent() instanceof CubaVerticalActionsLayout)) { newContent = new CubaVerticalActionsLayout(); newContent.setWidth("100%"); } else if (orientation == Orientation.HORIZONTAL && !(getContent() instanceof CubaHorizontalActionsLayout)) { newContent = new CubaHorizontalActionsLayout(); } if (newContent != null) { newContent.setMargin((getContent()).getMargin()); newContent.setSpacing((getContent()).isSpacing()); newContent.setStyleName(SCROLLBOX_CONTENT_STYLENAME); com.vaadin.ui.Component oldContent = component.getComponent(0); newContent.setWidth(oldContent.getWidth(), oldContent.getWidthUnits()); newContent.setHeight(oldContent.getHeight(), oldContent.getHeightUnits()); component.removeAllComponents(); component.addComponent(newContent); applyScrollBarsPolicy(scrollBarPolicy); } if (ownComponents.contains(childComponent)) { int existingIndex = getContent().getComponentIndex(WebComponentsHelper.getComposition(childComponent)); if (index > existingIndex) { index--; } remove(childComponent); } com.vaadin.ui.Component vComponent = WebComponentsHelper.getComposition(childComponent); getContent().addComponent(vComponent, index); getContent().setComponentAlignment(vComponent, WebWrapperUtils.toVaadinAlignment(childComponent.getAlignment())); if (frame != null) { if (childComponent instanceof BelongToFrame && ((BelongToFrame) childComponent).getFrame() == null) { ((BelongToFrame) childComponent).setFrame(frame); } else { frame.registerComponent(childComponent); } } if (index == ownComponents.size()) { ownComponents.add(childComponent); } else { ownComponents.add(index, childComponent); } childComponent.setParent(this); } @Override public int indexOf(Component component) { return ownComponents.indexOf(component); } @Nullable @Override public Component getComponent(int index) { return ownComponents.get(index); } @Override public void setStyleName(String styleName) { super.setStyleName(styleName); component.addStyleName(SCROLLBOX_STYLENAME); } @Override public String getStyleName() { return StringUtils.normalizeSpace(super.getStyleName().replace(SCROLLBOX_STYLENAME, "")); } @Override public void remove(Component childComponent) { getContent().removeComponent(childComponent.unwrapComposition(com.vaadin.ui.Component.class)); ownComponents.remove(childComponent); childComponent.setParent(null); } @Override public void removeAll() { getContent().removeAllComponents(); Component[] components = ownComponents.toArray(new Component[ownComponents.size()]); ownComponents.clear(); for (Component childComponent : components) { childComponent.setParent(null); } } @Override public void setFrame(Frame frame) { super.setFrame(frame); if (frame != null) { for (Component childComponent : ownComponents) { if (childComponent instanceof BelongToFrame && ((BelongToFrame) childComponent).getFrame() == null) { ((BelongToFrame) childComponent).setFrame(frame); } } } } @Override public void requestFocus() { } @Override public Component getOwnComponent(String id) { Preconditions.checkNotNullArgument(id); return ownComponents.stream().filter(component -> Objects.equals(id, component.getId())).findFirst() .orElse(null); } @Nullable @Override public Component getComponent(String id) { return ComponentsHelper.getComponent(this, id); } @Override public Collection<Component> getOwnComponents() { return Collections.unmodifiableCollection(ownComponents); } @Override public Collection<Component> getComponents() { return ComponentsHelper.getComponents(this); } @Override public Orientation getOrientation() { return orientation; } @Override public void setOrientation(Orientation orientation) { if (!Objects.equals(orientation, this.orientation)) { if (!ownComponents.isEmpty()) { throw new IllegalStateException( "Unable to change scrollBox orientation after adding components to it"); } this.orientation = orientation; } } @Override public ScrollBarPolicy getScrollBarPolicy() { return scrollBarPolicy; } @Override public void setScrollBarPolicy(ScrollBarPolicy scrollBarPolicy) { if (this.scrollBarPolicy != scrollBarPolicy) { applyScrollBarsPolicy(scrollBarPolicy); } this.scrollBarPolicy = scrollBarPolicy; } protected void applyScrollBarsPolicy(ScrollBarPolicy scrollBarPolicy) { switch (scrollBarPolicy) { case VERTICAL: getContent().setHeightUndefined(); getContent().setWidth(100, Sizeable.Unit.PERCENTAGE); break; case HORIZONTAL: getContent().setHeight(100, Sizeable.Unit.PERCENTAGE); getContent().setWidthUndefined(); break; case BOTH: getContent().setSizeUndefined(); break; case NONE: getContent().setSizeFull(); break; } } @Override public void setMargin(com.haulmont.cuba.gui.components.MarginInfo marginInfo) { MarginInfo vMargin = new MarginInfo(marginInfo.hasTop(), marginInfo.hasRight(), marginInfo.hasBottom(), marginInfo.hasLeft()); component.setMargin(vMargin); } @Override public com.haulmont.cuba.gui.components.MarginInfo getMargin() { MarginInfo vMargin = getContent().getMargin(); return new com.haulmont.cuba.gui.components.MarginInfo(vMargin.hasTop(), vMargin.hasRight(), vMargin.hasBottom(), vMargin.hasLeft()); } @Override public void setSpacing(boolean enabled) { getContent().setSpacing(enabled); } @Override public boolean getSpacing() { return getContent().isSpacing(); } @Override public void addShortcutAction(ShortcutAction action) { KeyCombination keyCombination = action.getShortcutCombination(); com.vaadin.event.ShortcutListener shortcut = new ContainerShortcutActionWrapper(action, this, keyCombination); component.addShortcutListener(shortcut); if (shortcuts == null) { shortcuts = new HashMap<>(); } shortcuts.put(action, shortcut); } @Override public void removeShortcutAction(ShortcutAction action) { if (shortcuts != null) { component.removeShortcutListener(shortcuts.remove(action)); if (shortcuts.isEmpty()) { shortcuts = null; } } } }