KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > css > actions > CheckStyleAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.css.actions;
20
21 import java.awt.datatransfer.StringSelection JavaDoc;
22 import java.net.*;
23 import java.io.*;
24
25 import org.openide.*;
26 import org.openide.awt.StatusDisplayer;
27 import org.openide.nodes.Node;
28 import org.openide.util.*;
29 import org.openide.util.actions.*;
30 import org.openide.filesystems.*;
31 import org.openide.cookies.*;
32
33 import org.w3c.css.sac.*;
34
35 import org.netbeans.modules.css.*;
36
37 /**
38 * Action that reparses stylesheet and reports any syntax errors.
39 *
40 * @author Petr Kuzel
41 */

42 public class CheckStyleAction extends CookieAction implements ErrorHandler, DocumentHandler {
43
44     /** Serial Version UID */
45     private static final long serialVersionUID = -6638807099960633338L;
46     
47     private CSSObject csso; //currently processed object
48
private CSSDisplayer disp; //and its displayer
49

50     //check status
51
private boolean failed;
52     private int warnings;
53     
54     /** What triggers me?
55     * @return MODE_EXACTLY_ONE.
56     */

57     public int mode() {
58         return MODE_EXACTLY_ONE;
59     }
60
61     /** What triggers me?
62     * @return {CSSObject.class}
63     */

64     public Class JavaDoc[] cookieClasses() {
65         return new Class JavaDoc[] {CSSObject.class};
66     }
67
68     /** Action.
69     */

70     protected void performAction(final Node[] nodes) {
71         if (nodes.length != 1) return;
72         if (nodes[0] == null) return;
73         
74         failed = false;
75         warnings = 0;
76
77         disp = new CSSDisplayer();
78
79         Parser parser = new org.w3c.flute.parser.Parser();
80         parser.setErrorHandler(this);
81         parser.setDocumentHandler(this);
82         
83         for (int i=0; i<nodes.length; i++) {
84             csso = (CSSObject) nodes[i].getCookie(CSSObject.class);
85             
86             if (csso != null) {
87                 try {
88                     //save it first
89
//!!! it could use document
90
SaveCookie cake = (SaveCookie) csso.getCookie(SaveCookie.class);
91                     if (cake != null)
92                         cake.save();
93                     
94                     String JavaDoc uri = csso.getPrimaryFile().getURL().toExternalForm();
95
96                     parser.parseStyleSheet(uri);
97                 } catch (IOException ex) {
98                     // ??? provide better feedback TopManager.getDefault().getErrorManager().notify(ex);
99
failed = true;
100                 } catch (CSSParseException ex) {
101                     // ??? provide better feedback
102
failed = true;
103                 }
104             }
105         }
106
107         if ( ( failed == true ) ||
108              ( warnings > 0 ) ) {
109             disp.moveToFront();
110         }
111         StatusDisplayer.getDefault().setStatusText(Util.THIS.getString("TEXT_PART_CSS_checking") + getStatus() + "."); // NOI18N
112
}
113
114     
115     private String JavaDoc getStatus() {
116         return failed ? Util.THIS.getString("TEXT_PART_failed") : warnings>1 ? Util.THIS.getString("TEXT_PART_finished_with_warnings") : Util.THIS.getString("TEXT_PART_finished");
117     }
118     
119     public String JavaDoc getName() {
120         return Util.THIS.getString("NAME_check_CSS");
121     }
122     
123     protected String JavaDoc iconResource () {
124         return "org/netbeans/modules/css/resources/checkStyleAction.gif"; // NOI18N
125
}
126     
127     /** Get help context for the action.
128     */

129     public HelpCtx getHelpCtx() {
130         return new HelpCtx(getClass());
131     }
132
133     // ~~~~~~~~~~~~~~~~~~~~~ PARSER LISTENER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
134

135     public void warning(CSSParseException exception) throws CSSException {
136         disp.display(csso, exception);
137         warnings++;
138     }
139     
140     public void error(CSSParseException exception) throws CSSException {
141         disp.display(csso, exception);
142         failed = true;
143     }
144     
145     public void fatalError(CSSParseException exception) throws CSSException {
146         disp.display(csso, exception);
147         failed = true;
148     }
149     
150     // ~~~~~~~~~~~~~~~~~ VOID ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
151

152     public void startDocument(InputSource source) throws CSSException {
153     }
154     
155     public void endDocument(InputSource source) throws CSSException {
156     }
157     
158     public void comment(String JavaDoc text) throws CSSException {
159     }
160     
161     public void ignorableAtRule(String JavaDoc atRule) throws CSSException {
162     }
163     
164     public void namespaceDeclaration(String JavaDoc prefix,String JavaDoc uri) throws CSSException {
165     }
166     
167     public void importStyle(String JavaDoc uri,SACMediaList media,String JavaDoc defaultNamespaceURI) throws CSSException {
168     }
169     
170     public void startMedia(SACMediaList media) throws CSSException {
171     }
172     
173     public void endMedia(SACMediaList media) throws CSSException {
174     }
175
176     public void startPage(String JavaDoc name,String JavaDoc pseudo_page) throws CSSException {
177     }
178     
179     public void endPage(String JavaDoc name,String JavaDoc pseudo_page) throws CSSException {
180     }
181     
182     public void startFontFace() throws CSSException {
183     }
184     
185     public void endFontFace() throws CSSException {
186     }
187     
188     public void startSelector(SelectorList selectors) throws CSSException {
189     }
190     
191     public void endSelector(SelectorList selectors) throws CSSException {
192     }
193     
194     public void property(String JavaDoc name,LexicalUnit value,boolean important) throws CSSException {
195     }
196 }
197
Popular Tags