//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
*
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.dataviewer;
import com.nexirius.framework.datamodel.DataModelTreeNode;
import com.nexirius.framework.gadgets.ArrayLayout;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EtchedBorder;
import java.awt.*;
/**
* This class is used to render the tree nodes
*
* @author Marcel Baumann
*/
class TreeViewerCellRendererComponent
extends JPanel {
DataModelTreeNode item;
Icon icon;
boolean selected;
TreeViewerCellRenderer treeRenderer;
public TreeViewerCellRendererComponent(TreeViewerCellRenderer tree_renderer
, DataModelTreeNode item
, boolean selected
, boolean expanded
, boolean leaf
, int row
, boolean hasFocus) {
this.treeRenderer = tree_renderer;
this.item = item;
this.selected = selected;
init();
}
private JLabel createLabel(Icon icon, String text, String info, Color bg) {
JLabel ret = null;
ret = new JLabel(text, icon, SwingConstants.LEFT);
ret.setOpaque(true);
ret.setToolTipText(info);
if (this.selected) {
ret.setForeground(SystemColor.textHighlightText);
ret.setBackground(SystemColor.textHighlight);
}
if (bg != null) {
ret.setBackground(bg);
}
return ret;
}
private void init() {
setLayout(new ArrayLayout(true));
Icon icon = this.treeRenderer.getIcon(this.item.getIconId());
if (this.item.getColumns() >= 1) {
for (int i = 0; i < this.item.getColumns(); ++i) {
Color bg = this.treeRenderer.getColor(this.item.getBackgroundColorId(i));
JLabel label = null;
String text;
if (i == 0) {
text = this.treeRenderer.getText(this.item.getLabel(i));
} else {
text = this.item.getLabel(i);
icon = null;
}
label = createLabel(icon, text, this.item.getInfo(), bg);
add(label);
Dimension size = label.getPreferredSize();
size.width = this.treeRenderer.getColumnWidth();
label.setSize(size);
}
setSize(getPreferredSize());
} else {
add(createLabel(icon, this.treeRenderer.getText(this.item.getLabel()), this.item.getInfo(), this.treeRenderer.getColor(this.item.getBackgroundColorId())));
}
if (this.selected) {
Border border = getBorder();
Border margin = new EtchedBorder();
setBorder(new CompoundBorder(border, margin));
}
SwingUtilities.invokeLater(new Validator());
}
class Validator implements Runnable {
public void run() {
validate();
}
}
}