View Javadoc

1   /*
2    * $Id$
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  package org.xmlhammer.gui.util;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import javax.swing.Icon;
27  import javax.swing.ImageIcon;
28  import javax.swing.JTextField;
29  import javax.swing.event.ChangeEvent;
30  import javax.swing.event.ChangeListener;
31  import javax.swing.event.DocumentEvent;
32  import javax.swing.event.DocumentListener;
33  
34  import org.bounce.image.ImageLoader;
35  import org.bounce.util.URIUtils;
36  import org.xmlhammer.gui.Page;
37  import org.xmlhammer.gui.overview.OverviewNode;
38  import org.xmlhammer.gui.overview.OverviewTreeModel;
39  import org.xmlhammer.model.project.Parameter;
40  
41  public class URISelectionNode implements OverviewNode, DocumentListener, ChangeListener {
42      private static final ImageIcon PARAMETER_ICON = ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/eview16/stackframe.gif");
43  
44      private ArrayList<OverviewNode> nodeList = new ArrayList<OverviewNode>();
45      private URISelectionPane selectionPane = null;
46      private ArrayList<OverviewNode> parameterNodes = null;
47      private OverviewNode parentNode = null;
48      private String type = "uri";
49      private ParametersNode parametersNode = null;
50  
51      public URISelectionNode(OverviewNode parentNode, URISelectionPane selectionPane, String type) {
52          this.selectionPane = selectionPane;
53          ((JTextField)selectionPane.getURIField().getEditor().getEditorComponent()).getDocument().addDocumentListener( this);
54          this.parentNode = parentNode;
55          this.selectionPane.addListener(this);
56          
57          if (selectionPane.isParameters()) {
58              parametersNode = new ParametersNode();
59              nodeList.add(parametersNode);            
60              updateParameters();
61          }
62          
63          this.type = type;
64      }
65  
66      public String getNodeName() {
67          if (selectionPane.getURIField().isEmpty()) {
68              return selectionPane.getURIField().getEmptyValue();
69          }
70  
71          return URIUtils.getName(selectionPane.getURI()) + " ("+type+")";
72      }
73  
74      public ArrayList<OverviewNode> getChildNodes() {
75          return nodeList;
76      }
77  
78      public String getError() {
79          return selectionPane.getError();
80      }
81  
82      public OverviewNode getParentNode() {
83          return parentNode;
84      }
85  
86      public Icon getNodeIcon() {
87          return URIsPanel.TEXT_FIELD_ICON;
88      }
89  
90      public void nodeChanged() {
91          OverviewNode node = parentNode;
92          
93          while ( node != null && !(node.getParentNode() instanceof OverviewTreeModel)) {
94              node = node.getParentNode();
95          }
96          
97          if ( node != null) {
98              ((OverviewTreeModel)node.getParentNode()).nodePathChanged(this);
99          }
100     }
101 
102     public void updateParameters() {
103         parameterNodes = new ArrayList<OverviewNode>();
104         
105         List<Parameter> parameters = selectionPane.getParameters();
106         for (Parameter parameter : parameters) {
107             parameterNodes.add(new ParameterNode(parametersNode, parameter.getName(), parameter.getValue()));
108         }
109     }
110 
111     public void stateChanged(ChangeEvent arg0) {
112         // parameters have changed ...
113         updateParameters();
114 
115         OverviewNode node = parentNode;
116         Page page = null;
117         
118         while (node != null && !(node instanceof Page)) {
119             node = node.getParentNode();
120         }
121 
122         if (node != null) {
123             page = (Page)node;
124             
125             page.getProjectView().getOverviewPanel().selectNode(parametersNode);
126         }
127 
128         while (node != null && !(node.getParentNode() instanceof OverviewTreeModel)) {
129             node = node.getParentNode();
130         }
131         
132         if (node != null) {
133             ((OverviewTreeModel)node.getParentNode()).structureChanged(parametersNode);
134         }
135 
136         if (page != null) {
137             page.getProjectView().getOverviewPanel().expandNode(parametersNode);
138         }
139     }
140     
141     public class ParametersNode implements OverviewNode {
142         
143         public String getNodeName() {
144             return "Parameters";
145         }
146 
147         public ArrayList<OverviewNode> getChildNodes() {
148             return parameterNodes;
149         }
150 
151         public OverviewNode getParentNode() {
152             return URISelectionNode.this;
153         }
154 
155         public Icon getNodeIcon() {
156             return PARAMETER_ICON;
157         }
158         
159         public String getError() {
160             return null;
161         }
162     }
163 
164     public void insertUpdate(DocumentEvent arg0) {
165         nodeChanged();
166     }
167 
168     public void removeUpdate(DocumentEvent arg0) {
169         nodeChanged();
170     }
171 
172     public void changedUpdate(DocumentEvent arg0) {}
173 }