All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gwt.material.design.addins.client.tree.MaterialTree Maven / Gradle / Ivy

There is a newer version: 2.8.3
Show newest version
/*
 * #%L
 * GwtMaterial
 * %%
 * Copyright (C) 2015 - 2017 GwtMaterialDesign
 * %%
 * 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.
 * #L%
 */
package gwt.material.design.addins.client.tree;

import com.google.gwt.dom.client.Document;
import com.google.gwt.event.logical.shared.*;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Widget;
import gwt.material.design.addins.client.MaterialAddins;
import gwt.material.design.addins.client.base.constants.AddinsCssName;
import gwt.material.design.client.MaterialDesignBase;
import gwt.material.design.client.base.MaterialWidget;

//@formatter:off

/**
 * MaterialTree is a component that wraps all the tree items that provide lists of
 * event handlers like open/close and selection event.
 * 

*

XML Namespace Declaration

*
 * {@code
 * xmlns:ma='urn:import:gwt.material.design.addins.client'
 * }
 * 
*

*

UiBinder Usage:

*
 * {@code
 * 
 *     
 *     
 *         <-- Image support -->
 *         
 *         
 *         
 *     
 *     
 * 
 * }
 * 
* * @author kevzlou7979 * @author Ben Dol * @see Tree View */ // @formatter:on public class MaterialTree extends MaterialWidget implements HasCloseHandlers, HasOpenHandlers, HasSelectionHandlers { static { if (MaterialAddins.isDebug()) { MaterialDesignBase.injectCss(MaterialTreeDebugClientBundle.INSTANCE.treeCssDebug()); } else { MaterialDesignBase.injectCss(MaterialTreeClientBundle.INSTANCE.treeCss()); } } private MaterialTreeItem selectedItem; public MaterialTree() { super(Document.get().createDivElement(), AddinsCssName.TREE); } @Override protected void onLoad() { super.onLoad(); // Ensure all children know we are the root. for (Widget child : getChildren()) { if (child instanceof MaterialTreeItem) { ((MaterialTreeItem) child).setTree(this); } } // Add selection event registerHandler(addSelectionHandler(event -> { for (Widget item : getChildren()) { if (item instanceof MaterialTreeItem) { clearSelectedStyles((MaterialTreeItem) item); } } MaterialTreeItem treeItem = event.getSelectedItem(); treeItem.addStyleName(AddinsCssName.SELECTED); setSelectedItem(treeItem); })); } @Override protected void add(Widget child, com.google.gwt.user.client.Element container) { if (child instanceof MaterialTreeItem) { super.add(child, container); ((MaterialTreeItem) child).setTree(this); } else { throw new IllegalArgumentException("MaterialTree can only contain MaterialTreeItem"); } } @Override protected void insert(Widget child, com.google.gwt.user.client.Element container, int beforeIndex, boolean domInsert) { if (child instanceof MaterialTreeItem) { super.insert(child, container, beforeIndex, domInsert); ((MaterialTreeItem) child).setTree(this); } else { throw new IllegalArgumentException("MaterialTree can only contain MaterialTreeItem"); } } protected void clearSelectedStyles(MaterialTreeItem item) { item.removeStyleName(AddinsCssName.SELECTED); for (MaterialTreeItem treeItem : item.getTreeItems()) { clearSelectedStyles(treeItem); } } public MaterialTreeItem getSelectedItem() { return selectedItem; } public void setSelectedItem(MaterialTreeItem selectedItem) { this.selectedItem = selectedItem; } /** * Expand all tree item's content */ public void expand() { for (Widget w : getChildren()) { if (w instanceof MaterialTreeItem) { expandItems((MaterialTreeItem) w); } } } /** * Recursive function to expand each tree item. */ protected void expandItems(MaterialTreeItem item) { item.expand(); item.setHide(true); item.getTreeItems().forEach(this::expandItems); } /** * Collapse all tree item's content */ public void collapse() { for (Widget w : getChildren()) { if (w instanceof MaterialTreeItem) { collapseItems((MaterialTreeItem) w); } } } /** * Deselect selected tree item */ public void deselectSelectedItem() { // Check whether tree has selected item if (selectedItem != null) { clearSelectedStyles(selectedItem); setSelectedItem(null); SelectionEvent.fire(this, null); } } /** * Recursive function to collapse each tree item. */ protected void collapseItems(MaterialTreeItem item) { item.collapse(); item.setHide(false); item.getTreeItems().forEach(this::collapseItems); } @Override public HandlerRegistration addCloseHandler(final CloseHandler handler) { return addHandler(new CloseHandler() { @Override public void onClose(CloseEvent event) { if (isEnabled()) { handler.onClose(event); } } }, CloseEvent.getType()); } @Override public HandlerRegistration addOpenHandler(final OpenHandler handler) { return addHandler(new OpenHandler() { @Override public void onOpen(OpenEvent event) { if (isEnabled()) { handler.onOpen(event); } } }, OpenEvent.getType()); } @Override public HandlerRegistration addSelectionHandler(final SelectionHandler handler) { return addHandler(new SelectionHandler() { @Override public void onSelection(SelectionEvent event) { if (isEnabled()) { handler.onSelection(event); } } }, SelectionEvent.getType()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy