View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  
18  /*
19   * Project: KernelConfigurator
20   * Created on 24-feb-2004
21   *
22   * Copyright (c)2003 Grid Systems
23   */
24  package com.gridsystems.config.app;
25  
26  import java.util.ArrayList;
27  
28  import javax.swing.Icon;
29  
30  import com.gridsystems.config.Configurator;
31  import com.gridsystems.config.SwingConfiguratorView;
32  
33  /**
34   * Tree node implementation for use in Swing JTree models, and in console menu
35   * trees.
36   * <p>
37   * It provides helper methods for easier Configurator trees construction.
38   *
39   * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a>
40   * @version 1.0
41   */
42  public class ConfigNode implements Comparable {
43    /**
44     * Node label.
45     */
46    private String label;
47  
48    /**
49     * Configurator instance.
50     */
51    private Configurator cfg;
52  
53    /**
54     * Child nodes.
55     */
56    private ArrayList<ConfigNode> nodes;
57  
58    /**
59     * Creates a node with the specified label and configurator.
60     *
61     * @param label The node label
62     * @param cfg   The associated configurator instance. It can be null.
63     */
64    public ConfigNode(String label, Configurator cfg) {
65      this.label = label;
66      this.cfg = cfg;
67      this.nodes = new ArrayList<ConfigNode>();
68    }
69  
70    /**
71     * Gets this node label.
72     *
73     * @return The label
74     */
75    public String getLabel() {
76      return this.label;
77    }
78  
79    /**
80     * Gets the configurator instance associated to this node.
81     *
82     * @return The associated configurator, or null if none
83     */
84    public Configurator getConfigurator() {
85      return this.cfg;
86    }
87  
88    /**
89     * {@inheritDoc}
90     */
91    public String toString() {
92      return getLabel();
93    }
94  
95    /**
96     * Gets the icon for this configurator. It makes sense only on Swing view mode,
97     * so it MUST not be called when in other modes.
98     *
99     * @return An icon for this node.
100    */
101   public Icon getIcon() {
102     if (this.cfg != null && Configurator.getViewMode() == Configurator.MODE_SWING) {
103       SwingConfiguratorView view = (SwingConfiguratorView)this.cfg.getView();
104       Icon returnValue = (view == null) ? null : view.getSmallIcon();
105       return returnValue;
106     }
107     return null;
108   }
109 
110   /**
111    * Gets the number of children nodes.
112    *
113    * @return The number of children nodes
114    */
115   public int getChildCount() {
116     return this.nodes.size();
117   }
118 
119   /**
120    * Gets the ith child node.
121    *
122    * @param i The index of the child node to retrieve
123    * @return The child node at position i
124    */
125   public ConfigNode getChild(int i) {
126     return (ConfigNode)nodes.get(i);
127   }
128 
129   /**
130    * Gets the index at which the specified node is located.
131    *
132    * @param child The child node
133    * @return Its position in the children array
134    */
135   public int indexOf(ConfigNode child) {
136     return nodes.indexOf(child);
137   }
138 
139   /**
140    * Adds a configurator node in the specified path. It recursively constructs
141    * the node path to the configurator instance.
142    *
143    * @param path The path to the configurator node
144    * @param cfg  The configurator instance to associate to the leaf node
145    */
146   public void addNode(String path, Configurator cfg) {
147     int pos = path.indexOf('.');
148     String localLabel = (pos == -1) ? path : path.substring(0, pos);
149 
150     for (int i = 0; i < nodes.size(); i++) {
151       ConfigNode n = (ConfigNode)nodes.get(i);
152       if (n.getLabel().equals(localLabel)) {
153         if (pos == -1) {
154           // Leaf node
155           if (cfg != null) {
156             n.cfg = cfg;
157           }
158         } else {
159           n.addNode(path.substring(pos + 1), cfg);
160         }
161         return;
162       }
163     }
164 
165     // New node
166     if (pos == -1) {
167       ConfigNode n = new ConfigNode(localLabel, cfg);
168       nodes.add(n);
169     } else {
170       ConfigNode n = new ConfigNode(localLabel, null);
171       nodes.add(n);
172       n.addNode(path.substring(pos + 1), cfg);
173     }
174   }
175 
176   /**
177    * Adds a configurator into its path.
178    *
179    * @param cfg The configurator to add to the tree
180    */
181   public void addConfigurator(Configurator cfg) {
182     if (cfg != null && cfg.hasView()) {
183       String path = cfg.getPath();
184       addNode(path, cfg);
185     }
186   }
187 
188   /**
189    * {@inheritDoc}
190    */
191   public int compareTo(Object o) {
192     String s = o.toString();
193     return this.toString().compareTo(s);
194   }
195 }