View Javadoc

1   /*
2    * $Id: ErrorNode.java,v 1.14 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) 2002 - 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.ImageIcon;
26  import javax.swing.JFrame;
27  import javax.swing.JPopupMenu;
28  
29  import org.bounce.RunnableAction;
30  import org.bounce.MenuUtilities;
31  import org.bounce.image.ImageLoader;
32  import org.bounce.util.URIUtils;
33  import org.xmlhammer.model.project.Error;
34  
35  /***
36   * The node for the XML tree, containing an XML element.
37   *
38   * @version	$Revision: 1.14 $, $Date: 2007/07/04 19:42:48 $
39   * @author Edwin Dankert <edankert@gmail.com>
40   */
41  public class ErrorNode extends ResultNode {
42  	private static final long serialVersionUID = 8931257161405562847L;
43  
44      private static final ImageIcon ICON = ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/obj16/error_obj.gif");
45      private static ExceptionDetailsDialog dialog = null;
46  
47  	private Error error = null;
48      private URI uri = null;
49  	
50  	public ErrorNode(URI uri, Error err) {
51  		error = err;
52          this.uri = uri;
53  	}
54  	
55  	public Error getError() {
56  		return error;
57  	}
58  
59  	public String getName() {
60  		return getValue();
61  	}
62      
63  	public String getValue() {
64          StringBuilder builder = new StringBuilder();
65          
66          if (error.getSystemId() != null) {
67              URI systemId = URIUtils.createURI(error.getSystemId());
68              
69              if (uri.compareTo(systemId) != 0) {
70                  builder.append(URIUtils.getName(systemId));
71                  builder.append(" - ");
72              }
73          }
74          
75          builder.append("[");
76          builder.append(getNumber(error.getLine()));
77          builder.append(":");
78          builder.append(getNumber(error.getColumn()));
79          builder.append("] ");
80          builder.append(error.getLocalizedMessage());
81          
82          return builder.toString();
83  	}
84  	
85  	public String getDescription() {
86  		return getValue();
87  	}
88  	
89  	public ImageIcon getIcon() {
90  		return ICON;
91  	}
92      
93      public String getContext() {
94          return "";
95      }
96      
97      @Override
98      public JPopupMenu getPopupMenu(JFrame parent) {
99          JPopupMenu popup = new JPopupMenu();
100         popup.add(getOpenURIAction(getOpenableURI(), getNumber(error.getLine()), getNumber(error.getColumn())));
101         popup.add(getEditURIAction(getOpenableURI(), getNumber(error.getLine()), getNumber(error.getColumn())));
102         popup.addSeparator();
103         popup.add(getCopyAction());
104         popup.addSeparator();
105         popup.add(getPropertiesAction(getDialog(parent)));
106         
107         MenuUtilities.alignMenu(popup);
108 
109         return popup;
110     }
111     
112     @Override
113     public RunnableAction getDefaultAction(JFrame parent) {
114         return getPropertiesAction(getDialog(parent));
115     }
116     
117     private ExceptionDetailsDialog getDialog(JFrame parent) {
118         if (dialog == null) {
119             dialog = new ExceptionDetailsDialog(parent, "Error Details");
120         }
121         
122         dialog.setException(error, uri, ICON);
123 
124         return dialog;
125     }
126 
127     private static int getNumber(Integer integer) {
128         if (integer != null) {
129             return integer;
130         } 
131         
132         return -1;
133     }
134     
135     public String getCopyValue() {
136         return "[error] "+getValue();
137     }
138     
139     private URI getOpenableURI() {
140         if (error.getSystemId() != null) {
141             return URIUtils.createURI(error.getSystemId());
142         }
143         
144         return uri;
145     }
146 }