View Javadoc

1   /*
2    * $Id: URINode.java,v 1.12 2007/07/04 19:42:48 edankert Exp $
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.output;
22  
23  import java.net.URI;
24  
25  import javax.swing.Icon;
26  import javax.swing.ImageIcon;
27  import javax.swing.JFrame;
28  import javax.swing.JPopupMenu;
29  
30  import org.bounce.RunnableAction;
31  import org.bounce.MenuUtilities;
32  import org.bounce.image.ImageLoader;
33  import org.bounce.util.URIUtils;
34  
35  /***
36   * The node for a URI/File.
37   *
38   * @version	$Revision: 1.12 $, $Date: 2007/07/04 19:42:48 $
39   * @author Edwin Dankert <edankert@gmail.com>
40   */
41  public class URINode extends ResultNode {
42  	private static final long serialVersionUID = -624571332730624325L;
43      
44      private static final ImageIcon ICON = ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/obj16/simplefile_obj.gif");
45      private static final ImageIcon VALID_ICON = ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/obj16/simplefile_valid_obj.gif");
46      private static final ImageIcon ERROR_ICON = ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/obj16/simplefile_error_obj.gif");
47      private static final ImageIcon WARNING_ICON = ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/obj16/simplefile_warning_obj.gif");
48  
49      private URI uri = null;
50      private boolean valid = false;
51  
52      private int total = 0;
53      private int errors = 0;
54      private int warnings = 0;
55      private int fatals = 0;
56      private int messages = 0;
57      private int others = 0;
58  	
59  	public URINode( URI uri) {
60  		this.uri = uri;
61  	}
62  	
63      public URINode( URI uri, boolean valid) {
64          this.uri = uri;
65          this.valid = valid;
66      }
67  
68      public String getName() {
69          return URIUtils.getName( uri);
70  	}
71      
72      public boolean isValid() {
73          return valid;
74      }
75  
76      public boolean isWarning() {
77          return warnings > 0;
78      }
79  	
80      public boolean isError() {
81          return errors+fatals > 0;
82      }
83  
84      public String getValue() {
85          count();
86  
87          StringBuffer buffer = new StringBuffer(getName());
88          
89          if ( total > 1) {
90              buffer.append( " (");
91              boolean multiple = false;
92              
93              if ( others > 1) {
94                  buffer.append( others);
95                  buffer.append( " matches");
96                  multiple = true;
97              }
98              if ( fatals > 0) {
99                  if (multiple) {
100                     buffer.append( ", ");
101                 }
102                 buffer.append( fatals);
103                 if ( fatals > 1) {
104                     buffer.append( " fatals");
105                 } else {
106                     buffer.append( " fatal");
107                 }
108                 multiple = true;
109             }
110             if ( errors > 0) {
111                 if (multiple) {
112                     buffer.append( ", ");
113                 }
114 
115                 buffer.append( errors);
116                 if ( errors > 1) {
117                     buffer.append( " errors");
118                 } else {
119                     buffer.append( " error");
120                 }
121                 multiple = true;
122             }
123             if ( warnings > 0) {
124                 if (multiple) {
125                     buffer.append( ", ");
126                 }
127                 buffer.append( warnings);
128 
129                 if ( warnings > 1) {
130                     buffer.append( " warnings");
131                 } else {
132                     buffer.append( " warning");
133                 }
134                 multiple = true;
135             }
136             if ( messages > 0) {
137                 if (multiple) {
138                     buffer.append( ", ");
139                 }
140                 buffer.append( messages);
141                 if ( messages > 1) {
142                     buffer.append( " messages");
143                 } else {
144                     buffer.append( " message");
145                 }
146                 multiple = true;
147             }
148             
149             buffer.append( ")");
150         }
151 
152         return buffer.toString();
153 	}
154 	
155 	public String getDescription() {
156         count();
157 
158         StringBuffer buffer = new StringBuffer(URIUtils.toString(uri));
159         if ( total > 1) {
160             buffer.append( " (");
161             if ( others > 1) {
162                 buffer.append( others);
163                 buffer.append( " matches");
164             }
165             if ( messages > 0) {
166                 buffer.append( messages);
167                 buffer.append( " messages");
168             }
169             if ( warnings > 0) {
170                 buffer.append( warnings);
171                 buffer.append( " warnings");
172             }
173             if ( errors > 0) {
174                 buffer.append( errors);
175                 buffer.append( " errors");
176             }
177             if ( fatals > 0) {
178                 buffer.append( fatals);
179                 buffer.append( " fatals");
180             }
181             
182             buffer.append( ")");
183         }
184 
185         return buffer.toString();
186 	}
187     
188 	public Icon getIcon() {
189         if ( isError()) {
190             return ERROR_ICON;
191         } else if ( isWarning()) {
192             return WARNING_ICON;
193         } else if ( isValid()) {
194             return VALID_ICON;
195         }
196 
197         return ICON;
198 	}
199 	
200 	public URI getURI() {
201 		return uri;
202 	}
203     
204     public String getContext() {
205         return "";
206     }
207 
208     public void count() {
209         if ( total != getChildCount()) {
210             total = getChildCount();
211             errors = 0;
212             warnings = 0;
213             fatals = 0;
214             messages = 0;
215             others = 0;
216             
217             for ( int i = 0; i < getChildCount(); i++) {
218                 if ( getChildAt(i) instanceof ErrorNode) {
219                     errors++;
220                 } else if ( getChildAt(i) instanceof WarningNode) {
221                     warnings++;
222                 } else if ( getChildAt(i) instanceof FatalNode) {
223                     fatals++;
224                 } else if ( getChildAt(i) instanceof MessageNode) {
225                     messages++;
226                 } else {
227                     others++;
228                 }
229             }
230         }
231     }
232     
233     @Override
234     public JPopupMenu getPopupMenu(JFrame parent) {
235         JPopupMenu popup = new JPopupMenu();
236         popup.add(getOpenURIAction(uri, 0, 0));
237         popup.add(getEditURIAction(uri, 0, 0));
238         popup.addSeparator();
239         popup.add(getCopyAction());
240         popup.addSeparator();
241         popup.add(getPropertiesAction(null));
242         
243         MenuUtilities.alignMenu(popup);
244 
245         return popup;
246     }
247 
248     @Override
249     public RunnableAction getDefaultAction(JFrame parent) {
250         return getOpenURIAction(uri, 0, 0);
251     }
252     
253     public String getCopyValue() {
254         return URIUtils.toString(uri);
255     }
256 }