de.alpharogroup.swing.panels.tree.JTreePanel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swing-components Show documentation
Show all versions of swing-components Show documentation
Several swing components and utilities
The newest version!
/**
* The MIT License
*
* Copyright (C) 2015 Asterios Raptis
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package de.alpharogroup.swing.panels.tree;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeSelectionModel;
import de.alpharogroup.model.api.Model;
import de.alpharogroup.swing.base.BasePanel;
import de.alpharogroup.swing.components.factories.DimensionFactory;
import lombok.Getter;
/**
* The abstract class {@link JTreePanel} provides a {@link JTree} that is already embedded in a
* {@link JScrollPane}. Additionally it provides factory methods that can be overwritten to provide
* specific behavior.
*
* @param
* the generic type of the model object
*/
@Getter
public abstract class JTreePanel extends BasePanel
{
/** The serialVersionUID. */
private static final long serialVersionUID = 1L;
/** The {@link JScrollPane} for the {@link JTree}. */
protected JScrollPane scrTree;
/** The {@link JTree}. */
protected JTree tree;
/**
* Instantiates a new {@link JTreePanel} object.
*/
public JTreePanel()
{
}
/**
* Instantiates a new new {@link JTreePanel} object.
*
* @param model
* the model
*/
public JTreePanel(final Model model)
{
super(model);
}
/**
*
* Factory method for creating the new {@link Dimension}. This method is invoked in the
* constructor from the derived classes and can be overridden so users can provide their own
* version of a {@link Dimension}
*
* @param width
* the width
* @param height
* the height
*
* @return the new {@link Dimension}
*/
protected Dimension newPreferredSize(int width, int height)
{
return DimensionFactory.newDimension(width, height);
}
/**
* New tree.
*
* @return the j tree
*/
protected JTree newTree()
{
JTree tree = new JTree();
tree.setModel(newTreeModel(getModel()));
tree.setEditable(true);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent e)
{
int selRow = tree.getRowForLocation(e.getX(), e.getY());
if (selRow != -1)
{
if (e.getClickCount() == 1)
{
onSingleClick(e);
}
else if (e.getClickCount() == 2)
{
onDoubleClick(e);
}
}
}
});
return tree;
}
/**
* Abstract factory callback method that have to be overwritten to provide the specific
* {@link TreeModel} for the {@link JTree}
*
* @param model
* the model
* @return the tree model
*/
protected abstract TreeModel newTreeModel(final Model model);
/**
* Factory method for creating the new {@link JScrollPane}. This method is invoked in the
* constructor from the derived classes and can be overridden so users can provide their own
* version of a {@link JScrollPane}
*
* @return the new {@link JScrollPane}
*/
protected JScrollPane newTreeScrollPane()
{
JScrollPane scrDbTree = new JScrollPane();
return scrDbTree;
}
/**
* The callback method on double click.
*
* @param event
* the mouse event
*/
protected void onDoubleClick(MouseEvent event)
{
}
/**
* {@inheritDoc}
*/
@Override
protected void onInitializeComponents()
{
super.onInitializeComponents();
scrTree = newTreeScrollPane();
tree = newTree();
setPreferredSize(newPreferredSize(420, 560));
scrTree.setViewportView(tree);
}
/**
* {@inheritDoc}
*/
@Override
protected void onInitializeLayout()
{
super.onInitializeLayout();
}
/**
* The callback method on single click.
*
* @param event
* the mouse event
*/
protected void onSingleClick(MouseEvent event)
{
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy