001: /*
002: * DependencyTreeCellRenderer.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.renderer;
013:
014: import java.awt.Color;
015: import java.awt.Component;
016: import java.awt.Graphics;
017:
018: import javax.swing.Icon;
019: import javax.swing.ImageIcon;
020: import javax.swing.JLabel;
021: import javax.swing.JTree;
022: import javax.swing.SwingConstants;
023: import javax.swing.UIManager;
024: import javax.swing.tree.DefaultMutableTreeNode;
025: import javax.swing.tree.TreeCellRenderer;
026:
027: import workbench.db.DependencyNode;
028: import workbench.gui.WbSwingUtilities;
029: import workbench.resource.ResourceMgr;
030: import workbench.resource.Settings;
031:
032: /**
033: *
034: * @author support@sql-workbench.net
035: */
036: public class DependencyTreeCellRenderer extends JLabel implements
037: TreeCellRenderer {
038: private Color selectedForeground;
039: private Color selectedBackground;
040: private Color unselectedForeground;
041: private Color unselectedBackground;
042: private ImageIcon fk;
043: private ImageIcon table;
044: private boolean isSelected;
045:
046: public DependencyTreeCellRenderer() {
047: this .setBorder(WbSwingUtilities.EMPTY_BORDER);
048: this .setVerticalAlignment(SwingConstants.TOP);
049: this .setHorizontalAlignment(SwingConstants.LEFT);
050: this .fk = ResourceMgr.getPicture("key");
051: this .table = ResourceMgr.getPicture("table");
052: this .selectedForeground = UIManager
053: .getColor("Tree.selectionForeground");
054: this .selectedBackground = UIManager
055: .getColor("Tree.selectionBackground");
056: this .unselectedForeground = UIManager
057: .getColor("Tree.textForeground");
058: this .unselectedBackground = UIManager
059: .getColor("Tree.textBackground");
060: }
061:
062: public Component getTreeCellRendererComponent(JTree tree,
063: Object value, boolean selected, boolean expanded,
064: boolean leaf, int row, boolean hasFocus) {
065: this .isSelected = selected;
066: if (selected) {
067: this .setForeground(this .selectedForeground);
068: } else {
069: this .setForeground(this .unselectedForeground);
070: //this.setBackground(this.unselectedBackground);
071: }
072:
073: if (value instanceof DefaultMutableTreeNode) {
074: DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
075: Object o = node.getUserObject();
076: if (o instanceof DependencyNode) {
077: this .setIcon(table);
078: DependencyNode depnode = (DependencyNode) o;
079: String uaction = depnode.getUpdateAction();
080: String daction = depnode.getDeleteAction();
081: if (uaction.length() > 0 || daction.length() > 0) {
082: StringBuilder tooltip = new StringBuilder(50);
083: tooltip.append("<html>");
084: boolean needBreak = false;
085: if (uaction.length() > 0) {
086: tooltip.append("ON UPDATE ");
087: tooltip.append(uaction);
088: needBreak = true;
089: }
090: if (daction.length() > 0) {
091: if (needBreak)
092: tooltip.append("<br>");
093: tooltip.append("ON DELETE ");
094: tooltip.append(daction);
095: }
096: tooltip.append("</html>");
097: setToolTipText(tooltip.toString());
098: } else {
099: setToolTipText(null);
100: }
101: } else {
102: this .setIcon(fk);
103: this .setToolTipText(null);
104: }
105: } else {
106: this .setIcon(null);
107: }
108: this .setText(value.toString());
109:
110: return this ;
111: }
112:
113: public void paint(Graphics g) {
114: Color bColor;
115:
116: if (this .isSelected) {
117: bColor = this .selectedBackground;
118: } else {
119: bColor = this .unselectedBackground;
120: if (bColor == null)
121: bColor = getBackground();
122: }
123: int imageOffset = -1;
124: if (bColor != null) {
125:
126: imageOffset = getLabelStart();
127: Color oldColor = g.getColor();
128: g.setColor(bColor);
129: if (getComponentOrientation().isLeftToRight()) {
130: g.fillRect(imageOffset, 0,
131: getWidth() - 1 - imageOffset, getHeight());
132: } else {
133: g.fillRect(0, 0, getWidth() - 1 - imageOffset,
134: getHeight());
135: }
136: g.setColor(oldColor);
137: }
138: super .paint(g);
139: }
140:
141: private int getLabelStart() {
142: Icon currentI = getIcon();
143: if (currentI != null && getText() != null) {
144: return currentI.getIconWidth()
145: + Math.max(0, getIconTextGap() - 1);
146: }
147: return 0;
148: }
149:
150: }
|