Revision 540 org.gvsig.toolbox/trunk/org.gvsig.toolbox/org.gvsig.toolbox.gui/src/main/java/es/unex/sextante/gui/toolbox/AlgorithmTreeCellRenderer.java

View differences:

AlgorithmTreeCellRenderer.java
1 1
package es.unex.sextante.gui.toolbox;
2 2

  
3
import java.awt.Color;
3
import es.unex.sextante.core.GeoAlgorithm;
4
import es.unex.sextante.gui.core.HasIcon;
5
import es.unex.sextante.gui.core.IAlgorithmProvider;
6
import es.unex.sextante.gui.core.NameAndIcon;
7
import es.unex.sextante.gui.core.SextanteGUI;
8
import es.unex.sextante.gui.core.ToolboxAction;
4 9
import java.awt.Component;
5 10
import java.util.ArrayList;
6 11
import java.util.HashMap;
7 12
import java.util.Iterator;
8 13
import java.util.Set;
9

  
10 14
import javax.swing.Icon;
11 15
import javax.swing.JLabel;
12 16
import javax.swing.JTree;
17
import javax.swing.UIManager;
13 18
import javax.swing.tree.DefaultMutableTreeNode;
14 19
import javax.swing.tree.TreeCellRenderer;
15 20

  
16
import es.unex.sextante.core.GeoAlgorithm;
17
import es.unex.sextante.gui.core.HasIcon;
18
import es.unex.sextante.gui.core.IAlgorithmProvider;
19
import es.unex.sextante.gui.core.NameAndIcon;
20
import es.unex.sextante.gui.core.SextanteGUI;
21
import es.unex.sextante.gui.core.ToolboxAction;
22

  
23

  
24 21
public class AlgorithmTreeCellRenderer
25
         extends
26
            JLabel
27
         implements
28
            TreeCellRenderer {
22
        extends
23
        JLabel
24
        implements
25
        TreeCellRenderer {
29 26

  
27
    public AlgorithmTreeCellRenderer() {
30 28

  
31
   public AlgorithmTreeCellRenderer() {
29
        setOpaque(false);
30
        setBackground(null);
32 31

  
33
      setOpaque(false);
34
      setBackground(null);
32
    }
35 33

  
36
   }
37

  
38

  
39 34
    protected Icon getCustomIcon(final Object value) {
40 35

  
41 36
        final ArrayList<IAlgorithmProvider> providers = SextanteGUI.getAlgorithmProviders();
......
79 74

  
80 75
    }
81 76

  
77
    public Component getTreeCellRendererComponent(final JTree tree,
78
            final Object value,
79
            final boolean sel,
80
            final boolean expanded,
81
            boolean leaf,
82
            final int row,
83
            final boolean hasFocus) {
82 84

  
83
   public Component getTreeCellRendererComponent(final JTree tree,
84
                                                 final Object value,
85
                                                 final boolean sel,
86
                                                 final boolean expanded,
87
                                                 boolean leaf,
88
                                                 final int row,
89
                                                 final boolean hasFocus) {
85
        String sName;
90 86

  
91
      String sName;
87
        this.setIcon(getCustomIcon(value));
92 88

  
93
      setFont(tree.getFont());
94
      this.setIcon(getCustomIcon(value));
89
        sName = tree.convertValueToText(value, sel, expanded, leaf, row, hasFocus);
95 90

  
96
      sName = tree.convertValueToText(value, sel, expanded, leaf, row, hasFocus);
97

  
98

  
99
      setEnabled(tree.isEnabled());
100
      setText(sName);
101
      try {
102
         if (!leaf) {
103
            // Check if the children algorithms are active
91
        setEnabled(tree.isEnabled());
92
        try {
104 93
            boolean activeAlg = false;
105
            final Object[] objs = SextanteGUI.getInputFactory().getDataObjects();
106
            for (int i = 0; !activeAlg && (i < tree.getModel().getChildCount(value)); i++) {
107
               final DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) tree.getModel().getChild(value, i);
108
               final Object childValue = childNode.getUserObject();
109
               if (childValue instanceof GeoAlgorithm) {
110
                  final GeoAlgorithm alg = (GeoAlgorithm) childValue;
111
                  if (alg.meetsDataRequirements(objs)) {
112
                     activeAlg = true;
113
                  }
114
               }
115
               else if (childValue instanceof ToolboxAction) {
116
                  final ToolboxAction ita = (ToolboxAction) childValue;
117
                  activeAlg = ita.isActive();
118
               }
119
               else {
120
                  activeAlg = true; //is a top level node. Should be black
121
               }
122
            }
94
            if (!leaf) {
95
                // Check if the children algorithms are active
96
                final Object[] objs = SextanteGUI.getInputFactory().getDataObjects();
97
                for (int i = 0; !activeAlg && (i < tree.getModel().getChildCount(value)); i++) {
98
                    final DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) tree.getModel().getChild(value, i);
99
                    final Object childValue = childNode.getUserObject();
100
                    if (childValue instanceof GeoAlgorithm) {
101
                        final GeoAlgorithm alg = (GeoAlgorithm) childValue;
102
                        if (alg.meetsDataRequirements(objs)) {
103
                            activeAlg = true;
104
                        }
105
                    } else if (childValue instanceof ToolboxAction) {
106
                        final ToolboxAction ita = (ToolboxAction) childValue;
107
                        activeAlg = ita.isActive();
108
                    } else {
109
                        activeAlg = true; //is a top level node. Should be black
110
                    }
111
                }
112
                setText("<html><b><font size=\"-1\">" + sName + "</font></b></html>");
123 113

  
124
            // Check if selected
125
            setFont(AlgorithmsPanel.TREE_FONT);
126
            if (sel) {
127
               setForeground(Color.blue);
114
            } else {
115
                final Object obj = ((DefaultMutableTreeNode) value).getUserObject();
116
                if (obj instanceof ToolboxAction) {
117
                    final ToolboxAction ita = (ToolboxAction) obj;
118
                    activeAlg = ita.isActive();
119
                } else {//geoalgorithm
120
                    final GeoAlgorithm alg = (GeoAlgorithm) obj;
121
                    final Object[] objs = SextanteGUI.getInputFactory().getDataObjects();
122
                    activeAlg = alg.meetsDataRequirements(objs);
123
                }
124
                setText("<html><font size=\"-1\">" + sName + "</font></html>");
128 125
            }
129
            else {
130
               setFont(AlgorithmsPanel.TREE_FONT);
131
               if (!activeAlg) {
132
                  setForeground(Color.gray);
133
               }
134
               else {
135
                  setForeground(Color.black);
136
               }
137
            }
138

  
139
         }
140
         else {
141
            boolean activeAlg = false;
142
            final Object obj = ((DefaultMutableTreeNode) value).getUserObject();
143
            if (obj instanceof ToolboxAction) {
144
               final ToolboxAction ita = (ToolboxAction) obj;
145
               activeAlg = ita.isActive();
146
            }
147
            else {//geoalgorithm
148
               final GeoAlgorithm alg = (GeoAlgorithm) obj;
149
               final Object[] objs = SextanteGUI.getInputFactory().getDataObjects();
150
               activeAlg = alg.meetsDataRequirements(objs);
151
            }
152

  
153 126
            if (sel) {
154
               setForeground(Color.blue);
127
                if (!activeAlg) {
128
                    setForeground(UIManager.getColor("Label.disabledForeground"));
129
                } else {
130
                    setForeground(UIManager.getColor("Tree.selectionForeground"));
131
                }
132
            } else {
133
                if (!activeAlg) {
134
                    setForeground(UIManager.getColor("Label.disabledForeground"));
135
                } else {
136
                    setForeground(UIManager.getColor("Tree.textForeground"));
137
                }
155 138
            }
156
            else {
157
               if (!activeAlg) {
158
                  setForeground(Color.gray);
159
               }
160
               else {
161
                  setForeground(Color.black);
162
               }
163
            }
164
         }
165
      }
166
      catch (final ClassCastException e) {
167
         setForeground(Color.black);
168
      }
169 139

  
170
      return this;
140
        } catch (final Throwable e) {
141
            setForeground(UIManager.getColor("Label.disabledForeground"));
142
        }
171 143

  
172
   }
144
        return this;
145

  
146
    }
173 147
}

Also available in: Unified diff