KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > model > AntModelProblem


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ant.internal.ui.model;
13
14 import org.eclipse.ant.internal.ui.AntUIPlugin;
15 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants;
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.jface.text.Region;
18
19 public class AntModelProblem extends Region implements IProblem {
20     
21     public static final int NO_PROBLEM= -1;
22     public static final int SEVERITY_WARNING= 0;
23     public static final int SEVERITY_ERROR= 1;
24     public static final int SEVERITY_FATAL_ERROR= 2;
25     
26     private String JavaDoc fMessage;
27     private String JavaDoc fEscapedMessage;
28     private int fSeverity;
29     private int fAdjustedLength= -1;
30     private int fLineNumber= -1;
31     
32     public AntModelProblem(String JavaDoc message, int severity, int offset, int length, int lineNumber) {
33         super(offset, length);
34         fMessage= message;
35         fEscapedMessage= getEscaped(message);
36         fSeverity= severity;
37         fLineNumber= lineNumber;
38     }
39     
40     /* (non-Javadoc)
41      * @see org.eclipse.ant.internal.ui.editor.outline.IProblem#getMessage()
42      */

43     public String JavaDoc getMessage() {
44         return fEscapedMessage;
45     }
46     
47     /* (non-Javadoc)
48      * @see org.eclipse.ant.internal.ui.editor.outline.IProblem#isError()
49      */

50     public boolean isError() {
51         return fSeverity == SEVERITY_ERROR || fSeverity == SEVERITY_FATAL_ERROR;
52     }
53     
54     /* (non-Javadoc)
55      * @see org.eclipse.ant.internal.ui.editor.outline.IProblem#isWarning()
56      */

57     public boolean isWarning() {
58         return fSeverity == SEVERITY_WARNING;
59     }
60     
61     /* (non-Javadoc)
62      * @see org.eclipse.jface.text.IRegion#getLength()
63      */

64     public int getLength() {
65         if (fAdjustedLength != -1) {
66             return fAdjustedLength;
67         }
68         return super.getLength();
69     }
70     
71     /**
72      * Sets the length for this problem.
73      */

74     public void setLength(int adjustedLength) {
75         fAdjustedLength= adjustedLength;
76     }
77
78     /* (non-Javadoc)
79      * @see org.eclipse.ant.internal.ui.editor.outline.IProblem#getLineNumber()
80      */

81     public int getLineNumber() {
82         return fLineNumber;
83     }
84     
85     private void appendEscapedChar(StringBuffer JavaDoc buffer, char c) {
86         String JavaDoc replacement= getReplacement(c);
87         if (replacement != null) {
88             buffer.append(replacement);
89         } else {
90             buffer.append(c);
91         }
92     }
93     
94     private String JavaDoc getEscaped(String JavaDoc s) {
95         StringBuffer JavaDoc result= new StringBuffer JavaDoc(s.length() + 10);
96         for (int i= 0; i < s.length(); ++i) {
97             appendEscapedChar(result, s.charAt(i));
98         }
99         return result.toString();
100     }
101     
102     private String JavaDoc getReplacement(char c) {
103         // Encode special characters into the equivalent character references.
104
// Ensures that error messages that include special characters do not get
105
//incorrectly represented as HTML in the text hover (bug 56258)
106
switch (c) {
107             case '<' :
108                 return "&lt;"; //$NON-NLS-1$
109
case '>' :
110                 return "&gt;"; //$NON-NLS-1$
111
case '"' :
112                 return "&quot;"; //$NON-NLS-1$
113
case '&' :
114                 return "&amp;"; //$NON-NLS-1$
115
}
116         return null;
117     }
118     
119     /* (non-Javadoc)
120      * @see org.eclipse.ant.internal.ui.editor.outline.IProblem#getUnmodifiedMessage()
121      */

122     public String JavaDoc getUnmodifiedMessage() {
123         return fMessage;
124     }
125     
126     public static int getSeverity(String JavaDoc preferenceKey) {
127         IPreferenceStore store= AntUIPlugin.getDefault().getPreferenceStore();
128         String JavaDoc severityLevel= store.getString(preferenceKey);
129         if (severityLevel.length() == 0 || severityLevel.equals(AntEditorPreferenceConstants.BUILDFILE_ERROR)) {
130             return SEVERITY_ERROR;
131         } else if (severityLevel.equals(AntEditorPreferenceConstants.BUILDFILE_WARNING)) {
132             return SEVERITY_WARNING;
133         } else {
134             return NO_PROBLEM;
135         }
136     }
137     
138     /* (non-Javadoc)
139      * @see java.lang.Object#equals(java.lang.Object)
140      */

141     public boolean equals(Object JavaDoc o) {
142         boolean equal= super.equals(o);
143         if (equal) {
144             return ((AntModelProblem)o).getUnmodifiedMessage().equals(getUnmodifiedMessage());
145         }
146         return false;
147     }
148 }
149
Popular Tags