01: package net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree;
02:
03: import net.sourceforge.squirrel_sql.client.session.ISession;
04: import net.sourceforge.squirrel_sql.client.session.properties.SessionProperties;
05: import net.sourceforge.squirrel_sql.client.resources.SquirrelResources;
06:
07: import javax.swing.tree.DefaultTreeCellRenderer;
08: import javax.swing.*;
09: import java.awt.*;
10: import java.beans.PropertyChangeEvent;
11: import java.beans.PropertyChangeListener;
12:
13: public class ObjectTreeCellRenderer extends DefaultTreeCellRenderer {
14: private ObjectTreeModel _model;
15: private ISession _session;
16:
17: private JPanel _pnlFilterRootNodeRendererComponent;
18: private JLabel _filterHint;
19:
20: GridBagConstraints _gbcLeft = new GridBagConstraints(0, 0, 1, 1, 1,
21: 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
22: new Insets(0, 0, 0, 0), 0, 0);
23: GridBagConstraints _gbcRight = new GridBagConstraints(1, 0, 1, 1,
24: 0, 0, GridBagConstraints.NORTHWEST,
25: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
26:
27: public ObjectTreeCellRenderer(ObjectTreeModel model,
28: ISession session) {
29:
30: _model = model;
31: _session = session;
32:
33: _session.getProperties().addPropertyChangeListener(
34: new PropertyChangeListener() {
35: public void propertyChange(PropertyChangeEvent evt) {
36: onPropertyChanged(evt);
37: }
38: });
39:
40: initFilter();
41:
42: _pnlFilterRootNodeRendererComponent = new JPanel(
43: new GridBagLayout());
44:
45: }
46:
47: private void onPropertyChanged(PropertyChangeEvent evt) {
48: if (SessionProperties.IPropertyNames.OBJECT_FILTER.equals(evt
49: .getPropertyName())) {
50: initFilter();
51: }
52: }
53:
54: private void initFilter() {
55: String filter = _session.getProperties().getObjectFilter();
56: if (null != filter && 0 < filter.trim().length()) {
57: _filterHint = new JLabel("= " + filter);
58: final SquirrelResources rsrc = _session.getApplication()
59: .getResources();
60: final ImageIcon icon = rsrc.getIcon("Filter");
61: _filterHint.setIcon(icon);
62: } else {
63: _filterHint = null;
64: }
65:
66: _model.nodeChanged(_model.getRootObjectTreeNode());
67: }
68:
69: public Component getTreeCellRendererComponent(JTree tree,
70: Object value, boolean sel, boolean expanded, boolean leaf,
71: int row, boolean hasFocus) {
72: JLabel ret = (JLabel) super .getTreeCellRendererComponent(tree,
73: value, sel, expanded, leaf, row, hasFocus);
74:
75: if (null != _filterHint && _model.isRootNode(value)) {
76: _pnlFilterRootNodeRendererComponent.removeAll();
77: _pnlFilterRootNodeRendererComponent
78: .setBackground(new Color(255, 204, 204));
79:
80: _pnlFilterRootNodeRendererComponent.add(ret, _gbcLeft);
81: _pnlFilterRootNodeRendererComponent.add(ret);
82:
83: _pnlFilterRootNodeRendererComponent.add(_filterHint,
84: _gbcRight);
85: _pnlFilterRootNodeRendererComponent.add(_filterHint);
86:
87: return _pnlFilterRootNodeRendererComponent;
88:
89: } else {
90: return ret;
91: }
92: }
93: }
|