001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.gui.se.tree;
017
018 import java.awt.Dimension;
019 import java.awt.GridLayout;
020 import java.awt.event.MouseListener;
021
022 import javax.swing.JPanel;
023 import javax.swing.JTree;
024 import javax.swing.ToolTipManager;
025 import javax.swing.event.TreeSelectionListener;
026 import javax.swing.tree.TreeModel;
027 import javax.swing.tree.TreePath;
028 import javax.swing.tree.TreeSelectionModel;
029
030
031 /**
032 * View for certain JTree.
033 *
034 * A View, which is a collection of classes representing the elements in the user interface
035 * (all of the things the user can see and respond to on the screen, such as buttons, display
036 * boxes, and so forth).
037 *
038 * @author Michael Meyling
039 */
040 public final class QedeqTreeView extends JPanel {
041
042 /** Reference to JTree. */
043 private final JTree theTree;
044
045 /**
046 * Constructor.
047 *
048 * @param treeModel model for this tree
049 */
050 public QedeqTreeView(final TreeModel treeModel) {
051 super(new GridLayout(1, 1));
052 // don't change these values without looking at the paint method of the TreeCellRenderer
053 theTree = new JTree(treeModel);
054 theTree.setEditable(false);
055 theTree.setRootVisible(false);
056 theTree.setExpandsSelectedPaths(true);
057 theTree.setScrollsOnExpand(true);
058 theTree.setShowsRootHandles(true);
059 theTree.getSelectionModel().setSelectionMode(
060 TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
061 // enable tool tips for the tree
062 ToolTipManager.sharedInstance().registerComponent(theTree);
063
064 theTree.setCellRenderer(new QedeqTreeCellRenderer());
065 /* Make tree ask for the height of each row. */
066 theTree.setRowHeight(-1);
067
068 this.add(theTree);
069 this.setMinimumSize(new Dimension(150, 100));
070 }
071
072 /* @see JTree#expandPath
073 */
074 public final void expandPath(final TreePath path) {
075 theTree.expandPath(path);
076 }
077
078 /* @see JTree#addTreeSelectionListener
079 */
080 public final void addTreeSelectionListener(final TreeSelectionListener listener) {
081 theTree.addTreeSelectionListener(listener);
082 }
083
084
085 /* @see JTree#setSelectionRow
086 */
087 public void setSelectionRow(final int row) {
088 theTree.setSelectionRow(row);
089 }
090
091 /* @see JTree#setSelectionPath
092 */
093 public void setSelectionPath(final TreePath path) {
094 theTree.setSelectionPath(path);
095 }
096
097 /* @see JTree#getSelectionPath
098 */
099 public TreePath getSelectionPath() {
100 return theTree.getSelectionPath();
101 }
102
103 /* @see JTree#getSelectionPaths
104 */
105 public TreePath[] getSelectionPaths() {
106 return theTree.getSelectionPaths();
107 }
108
109 /* @see JTree#getPathForLocation
110 */
111 public TreePath getPathForLocation(final int x, final int y) {
112 return theTree.getPathForLocation(x, y);
113 }
114
115 /* @see JTree#startEditingAtPath
116 */
117 public void startEditingAtPath(final TreePath path) {
118 theTree.startEditingAtPath(path);
119 }
120
121 /* @see JTree#treeAddMouseListener
122 */
123 public void treeAddMouseListener(final MouseListener mouseListener) {
124 theTree.addMouseListener(mouseListener);
125 }
126
127 }
|