gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.extension / src / main / java / org / gvsig / sextante / app / extension / AlgorithmsTreePanel.java @ 43
History | View | Annotate | Download (6.13 KB)
1 | 20 | nbrodin | package org.gvsig.sextante.app.extension; |
---|---|---|---|
2 | |||
3 | import java.awt.BorderLayout; |
||
4 | import java.awt.Color; |
||
5 | import java.awt.Component; |
||
6 | import java.awt.event.MouseAdapter; |
||
7 | import java.awt.event.MouseEvent; |
||
8 | import java.awt.event.MouseListener; |
||
9 | import java.text.Collator; |
||
10 | import java.util.HashMap; |
||
11 | import java.util.Iterator; |
||
12 | import java.util.Set; |
||
13 | |||
14 | import javax.swing.Icon; |
||
15 | import javax.swing.ImageIcon; |
||
16 | import javax.swing.JLabel; |
||
17 | import javax.swing.JPanel; |
||
18 | import javax.swing.JScrollPane; |
||
19 | import javax.swing.JTree; |
||
20 | import javax.swing.tree.DefaultMutableTreeNode; |
||
21 | import javax.swing.tree.DefaultTreeModel; |
||
22 | import javax.swing.tree.TreeCellRenderer; |
||
23 | import javax.swing.tree.TreePath; |
||
24 | |||
25 | import org.gvsig.andami.PluginServices; |
||
26 | |||
27 | import es.unex.sextante.core.GeoAlgorithm; |
||
28 | import es.unex.sextante.core.Sextante; |
||
29 | import es.unex.sextante.gui.core.SextanteGUI; |
||
30 | import es.unex.sextante.gui.modeler.ModelAlgorithmIO; |
||
31 | import es.unex.sextante.modeler.ModelAlgorithm; |
||
32 | |||
33 | public class AlgorithmsTreePanel extends JPanel{ |
||
34 | |||
35 | private JTree jTree; |
||
36 | private JScrollPane jScrollPane; |
||
37 | private ButtonDefinitionDialog m_Dialog;
|
||
38 | |||
39 | public AlgorithmsTreePanel(ButtonDefinitionDialog dialog) {
|
||
40 | |||
41 | super();
|
||
42 | |||
43 | m_Dialog = dialog; |
||
44 | |||
45 | initGUI(); |
||
46 | |||
47 | } |
||
48 | |||
49 | private void initGUI() { |
||
50 | |||
51 | BorderLayout thisLayout = new BorderLayout(); |
||
52 | this.setLayout(thisLayout);
|
||
53 | jTree = new JTree(); |
||
54 | jTree.setCellRenderer(new MyCellRenderer());
|
||
55 | MouseListener ml = new MouseAdapter() { |
||
56 | |||
57 | public void mousePressed(MouseEvent e) { |
||
58 | TreePath path = jTree.getPathForLocation(e.getX(), e.getY());
|
||
59 | if (path!=null){ |
||
60 | try{
|
||
61 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); |
||
62 | GeoAlgorithm alg = ((GeoAlgorithm)node.getUserObject()); |
||
63 | m_Dialog.setCommand(alg); |
||
64 | } |
||
65 | catch(Exception ex){ |
||
66 | //
|
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | }; |
||
71 | jTree.addMouseListener(ml); |
||
72 | |||
73 | fillTree(); |
||
74 | jScrollPane = new JScrollPane(jTree,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, |
||
75 | JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
||
76 | |||
77 | this.add(jScrollPane, BorderLayout.CENTER); |
||
78 | |||
79 | } |
||
80 | |||
81 | private void fillTree(){ |
||
82 | |||
83 | DefaultMutableTreeNode node;
|
||
84 | DefaultMutableTreeNode mainNode = new DefaultMutableTreeNode( |
||
85 | Sextante.getText( "Procedures"));
|
||
86 | DefaultMutableTreeNode toolsNode = new DefaultMutableTreeNode( |
||
87 | PluginServices.getText(this, "Algorithms")); |
||
88 | DefaultMutableTreeNode child;
|
||
89 | HashMap map = new HashMap(); |
||
90 | String sGroup;
|
||
91 | String sKey;
|
||
92 | HashMap algs = Sextante.getAlgorithms();
|
||
93 | Set set = algs.keySet();
|
||
94 | Iterator iter = set.iterator();
|
||
95 | while (iter.hasNext()) {
|
||
96 | sKey = (String) iter.next();
|
||
97 | GeoAlgorithm alg = (GeoAlgorithm) algs.get(sKey); |
||
98 | child = new DefaultMutableTreeNode(alg); |
||
99 | sGroup = (alg.getGroup()); |
||
100 | if (map.containsKey(sGroup)){
|
||
101 | node = (DefaultMutableTreeNode)map.get(sGroup);
|
||
102 | } |
||
103 | else{
|
||
104 | node = new DefaultMutableTreeNode(sGroup); |
||
105 | map.put(sGroup, node); |
||
106 | } |
||
107 | addNodeInSortedOrder(node, child); |
||
108 | } |
||
109 | |||
110 | set= map.keySet(); |
||
111 | iter = set.iterator(); |
||
112 | |||
113 | while(iter.hasNext()){
|
||
114 | sKey = (String) iter.next();
|
||
115 | node = (DefaultMutableTreeNode) map.get(sKey);
|
||
116 | addNodeInSortedOrder(toolsNode, node); |
||
117 | } |
||
118 | |||
119 | mainNode.add(toolsNode); |
||
120 | |||
121 | DefaultMutableTreeNode modelsNode = new DefaultMutableTreeNode( |
||
122 | PluginServices.getText(this, "Models")); |
||
123 | GeoAlgorithm[] models = ModelAlgorithmIO.loadModelsAsAlgorithms(
|
||
124 | SextanteGUI.getModelsFolder()); |
||
125 | if (models != null && models.length != 0){ |
||
126 | for (int i = 0; i < models.length; i++){ |
||
127 | if (models[i].isSuitableForModelling()){
|
||
128 | sGroup = models[i].getGroup(); |
||
129 | node = (DefaultMutableTreeNode) map.get(sGroup);
|
||
130 | if (node == null){ |
||
131 | node = new DefaultMutableTreeNode(sGroup); |
||
132 | map.put(sGroup, node); |
||
133 | addNodeInSortedOrder(toolsNode,node); |
||
134 | } |
||
135 | child = new DefaultMutableTreeNode(models[i]); |
||
136 | addNodeInSortedOrder(node, child); |
||
137 | } |
||
138 | } |
||
139 | } |
||
140 | |||
141 | jTree.setModel(new DefaultTreeModel(mainNode)); |
||
142 | |||
143 | } |
||
144 | |||
145 | private void addNodeInSortedOrder(DefaultMutableTreeNode parent, |
||
146 | DefaultMutableTreeNode child){
|
||
147 | |||
148 | int n = parent.getChildCount();
|
||
149 | if(n==0){ |
||
150 | parent.add(child); |
||
151 | return;
|
||
152 | } |
||
153 | Collator collator = Collator.getInstance(); |
||
154 | collator.setStrength(Collator.PRIMARY);
|
||
155 | DefaultMutableTreeNode node=null; |
||
156 | for(int i=0;i<n;i++){ |
||
157 | node = (DefaultMutableTreeNode)parent.getChildAt(i);
|
||
158 | try{
|
||
159 | if(collator.compare(node.toString(),child.toString())>0){ |
||
160 | parent.insert(child, i); |
||
161 | return;
|
||
162 | } |
||
163 | }catch(Exception e){} |
||
164 | } |
||
165 | parent.add(child); |
||
166 | |||
167 | } |
||
168 | |||
169 | static class MyCellRenderer extends JLabel implements TreeCellRenderer { |
||
170 | |||
171 | ImageIcon m_AlgorithmIcon = new ImageIcon(SextanteGUI.class.getClassLoader() |
||
172 | .getResource("images/module2.png"));
|
||
173 | ImageIcon m_ModelIcon = new ImageIcon(getClass().getClassLoader() |
||
174 | .getResource("images/model.png"));
|
||
175 | |||
176 | protected Icon getCustomIcon(Object value) { |
||
177 | |||
178 | Object obj = ((DefaultMutableTreeNode)value).getUserObject(); |
||
179 | if (obj instanceof GeoAlgorithm){ |
||
180 | if (obj instanceof ModelAlgorithm){ |
||
181 | return m_ModelIcon;
|
||
182 | } |
||
183 | else{
|
||
184 | return m_AlgorithmIcon;
|
||
185 | } |
||
186 | } |
||
187 | else{
|
||
188 | return null; |
||
189 | } |
||
190 | |||
191 | } |
||
192 | |||
193 | public Component getTreeCellRendererComponent(JTree tree, |
||
194 | Object value,
|
||
195 | boolean sel,
|
||
196 | boolean expanded,
|
||
197 | boolean leaf,
|
||
198 | int row,
|
||
199 | boolean hasFocus) {
|
||
200 | |||
201 | String sName;
|
||
202 | |||
203 | setFont(tree.getFont()); |
||
204 | this.setIcon(getCustomIcon(value));
|
||
205 | |||
206 | sName = tree.convertValueToText(value, sel, expanded, |
||
207 | leaf, row, hasFocus); |
||
208 | |||
209 | |||
210 | setEnabled(tree.isEnabled()); |
||
211 | setText(sName); |
||
212 | if (!leaf){
|
||
213 | setFont(new java.awt.Font("Tahoma",1,11)); |
||
214 | setForeground(Color.black);
|
||
215 | } |
||
216 | else{
|
||
217 | if (sel){
|
||
218 | setForeground(Color.blue);
|
||
219 | } |
||
220 | else{
|
||
221 | setForeground(Color.black);
|
||
222 | } |
||
223 | } |
||
224 | |||
225 | return this; |
||
226 | |||
227 | } |
||
228 | |||
229 | } |
||
230 | |||
231 | |||
232 | |||
233 | } |