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

net.vectorpublish.desktop.vp.LayerImpl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2016, Peter Rader. All rights reserved.
 *  ___ ___               __                 ______         __     __  __         __
 * |   |   |.-----..----.|  |_ .-----..----.|   __ \.--.--.|  |--.|  ||__|.-----.|  |--.
 * |   |   ||  -__||  __||   _||  _  ||   _||    __/|  |  ||  _  ||  ||  ||__ --||     |
 *  \_____/ |_____||____||____||_____||__|  |___|   |_____||_____||__||__||_____||__|__|
 *
 * http://www.gnu.org/licenses/gpl-3.0.html
 */
package net.vectorpublish.desktop.vp;

import java.awt.GraphicsEnvironment;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import net.vectorpublish.desktop.vp.api.LayerTreeNode;
import net.vectorpublish.desktop.vp.api.layer.Layer;
import net.vectorpublish.desktop.vp.api.layer.dnd.LayerDragNDropTransferHandler;
import net.vectorpublish.desktop.vp.api.ui.DocumentSelectionChangeListener;
import net.vectorpublish.desktop.vp.api.ui.UserInterface;
import net.vectorpublish.desktop.vp.api.vpd.DocumentNode;
import net.vectorpublish.desktop.vp.api.vpd.LayerName;
import net.vectorpublish.desktop.vp.api.vpd.VectorPublishNode;

/**
 * The default tree implementation to display layers. *
 * 

* If the environment is headless, the drag'n'drop features are not available! * * @see Layer */ @Named public class LayerImpl extends Layer implements DocumentSelectionChangeListener, KeyListener { @Inject private final UserInterface ui = null; private final DefaultTreeModel model; @Inject private final LayerNodeRenderer renderer = null; @Inject private final LayerPopup popup = null; private boolean groupExanding; @Inject private final NodeSelectionChangeImpl changeListener = null; @Inject private final LayerDragNDropTransferHandler dragNDropHandler = null; public LayerImpl() { super(); model = (DefaultTreeModel) getModel(); setRootVisible(false); addKeyListener(this); addTreeSelectionListener(changeListener); addTreeExpansionListener(changeListener); } @Override public String convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { if (value instanceof LayerName) { final LayerName layer = (LayerName) value; return layer.getLayerName(); } return super.convertValueToText(value, selected, expanded, leaf, row, hasFocus); } public void expandRecursive(LayerTreeNode vectorPublishNode) { expandPath(new TreePath(unfold(vectorPublishNode))); final Enumeration children = vectorPublishNode.children(); while (children.hasMoreElements()) { expandRecursive(children.nextElement()); } } @Override public DocumentNode getDocument() { return (DocumentNode) model.getRoot(); } @Override public Set getSelection() { final TreePath[] g = getSelectionModel().getSelectionPaths(); final LinkedHashSet lst = new LinkedHashSet<>(g.length); for (final TreePath treePath : g) { lst.add((VectorPublishNode) treePath.getLastPathComponent()); } return Collections.unmodifiableSet(lst); } @PostConstruct private void insertIntoWindow() { setCellRenderer(renderer); ui.setLayerTree(this); setComponentPopupMenu(popup); if (!GraphicsEnvironment.isHeadless()) { setTransferHandler(dragNDropHandler); setDragEnabled(true); } } @Override public void keyPressed(KeyEvent e) { if (e.getKeyChar() == '*') { changeListener.startMassiveExpanding(); try { for (final VectorPublishNode vectorPublishNode : getSelection()) { expandRecursive(vectorPublishNode); } } finally { changeListener.finishMassiveExpanding(); } } } /* * (non-Javadoc) * * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent) */ @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent e) { } @Override public void notifyDocumentSelected(DocumentNode doc) { model.setRoot(doc); } @Override public void reload() { model.reload(); repaint(); } @Override public void setRoot(DocumentNode doc) { model.setRoot(doc); setRootVisible(true); } @Override public void setSelectionPath(TreePath path) { synchronized (this) { super.setSelectionPath(path); } } private Object[] unfold(LayerTreeNode vectorPublishNode) { final ArrayList list = new ArrayList<>(); do { list.add(0, vectorPublishNode); vectorPublishNode = vectorPublishNode.getParent(); } while (vectorPublishNode != null); return list.toArray(); } }