KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > features > FormatAction


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
20 package org.netbeans.modules.languages.features;
21
22 import java.util.Iterator JavaDoc;
23 import org.netbeans.api.languages.ASTItem;
24 import org.netbeans.api.languages.ASTPath;
25 import org.netbeans.api.languages.LanguagesManager;
26 import org.netbeans.api.languages.ParseException;
27 import org.netbeans.api.languages.ASTToken;
28 import java.awt.event.ActionEvent JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import javax.swing.text.BadLocationException JavaDoc;
34 import javax.swing.text.JTextComponent JavaDoc;
35 import org.netbeans.api.lexer.Token;
36 import org.netbeans.api.lexer.TokenHierarchy;
37 import org.netbeans.api.lexer.TokenSequence;
38 import org.netbeans.editor.BaseAction;
39 import org.netbeans.modules.editor.NbEditorDocument;
40 import org.netbeans.api.languages.ASTNode;
41 import org.netbeans.api.languages.ParseException;
42 import org.netbeans.api.languages.ASTToken;
43 import org.netbeans.modules.languages.Feature;
44 import org.netbeans.modules.languages.Language;
45 import org.netbeans.modules.languages.LanguagesManagerImpl;
46 import org.netbeans.modules.languages.LanguagesManagerImpl;
47 import org.netbeans.modules.languages.ParserManagerImpl;
48 import org.openide.ErrorManager;
49
50 /**
51  *
52  * @author Jan Jancura
53  */

54 public class FormatAction extends BaseAction {
55     
56     public FormatAction () {
57         super("Format");
58     }
59     
60     public void actionPerformed (ActionEvent JavaDoc e, JTextComponent JavaDoc component) {
61         try {
62             NbEditorDocument doc = (NbEditorDocument) component.getDocument ();
63             ASTNode root = ParserManagerImpl.get (doc).getAST ();
64             if (root == null) return;
65             StringBuilder JavaDoc sb = new StringBuilder JavaDoc ();
66             Map JavaDoc<String JavaDoc,String JavaDoc> indents = new HashMap JavaDoc<String JavaDoc,String JavaDoc> ();
67             indents.put ("i", ""); // NOI18N
68
indents.put ("ii", " "); // NOI18N
69
indent (
70                 root,
71                 new ArrayList JavaDoc<ASTItem> (),
72                 sb,
73                 indents,
74                 null,
75                 false,
76                 doc
77             );
78             doc.remove (0, doc.getLength ());
79             doc.insertString (0, sb.toString (), null);
80         } catch (ParseException ex) {
81             ErrorManager.getDefault ().notify (ex);
82         } catch (BadLocationException JavaDoc ex) {
83             ErrorManager.getDefault ().notify (ex);
84         }
85     }
86     
87     // uncomment to disable the action for languages without grammar definition
88
/*
89     public boolean isEnabled() {
90         JTextComponent component = getTextComponent(null);
91         if (component == null)
92             return false;
93         NbEditorDocument doc = (NbEditorDocument) component.getDocument ();
94         String mimeType = (String) doc.getProperty ("mimeType"); // NOI18N
95         try {
96             Language language = LanguagesManager.getDefault().getLanguage(mimeType);
97             return !language.getAnalyser().getRules().isEmpty();
98         } catch (ParseException e) {
99             ErrorManager.getDefault().notify(e);
100             return false;
101         }
102     }
103      */

104     
105     private static void indent (
106         ASTItem item,
107         List JavaDoc<ASTItem> path,
108         StringBuilder JavaDoc sb,
109         Map JavaDoc<String JavaDoc,String JavaDoc> indents,
110         ASTToken whitespace,
111         boolean firstIndented,
112         NbEditorDocument document
113     ) {
114         try {
115             Language language = LanguagesManagerImpl.get ().getLanguage (item.getMimeType ());
116
117             path.add (item);
118             ASTPath path2 = ASTPath.create (path);
119             Iterator JavaDoc<ASTItem> it = item.getChildren ().iterator ();
120             while (it.hasNext ()) {
121                 ASTItem e = it.next ();
122
123                 // compute indent
124
String JavaDoc indent = null;
125                 if (e instanceof ASTToken) {
126                     ASTToken token = (ASTToken) e;
127                     if (language.getSkipTokenTypes ().contains (token.getType ())) { // NOI18N
128
whitespace = (ASTToken) e;
129                         firstIndented = false;
130                         continue;
131                     }
132                 }
133                 Feature format = language.getFeature ("FORMAT", path2);
134                 if (format != null)
135                     indent = (String JavaDoc) format.getValue ();
136
137                 // indent
138
// if (e instanceof ASTNode)
139
// System.out.println("indent " + indent + " " + firstIndented + " : " + ((ASTNode) e).getNT () + " wh:" + whitespace);
140
// else
141
// System.out.println("indent " + indent + " " + firstIndented + " : " + e + " wh:" + whitespace);
142

143                 if (indent != null) {
144                     if (indent.equals ("NewLine"))
145                         sb.append ("\n");
146                 }
147                 
148                 // add child
149
if (e instanceof ASTToken)
150                     sb.append (((ASTToken) e).getIdentifier ());
151                 else
152                     indent (
153                         (ASTNode) e,
154                         path,
155                         sb,
156                         indents,
157                         whitespace,
158                         firstIndented || indent != null,
159                         null
160                     );
161             }// for children
162
path.remove (path.size () - 1);
163         } catch (ParseException ex) {
164         }
165     }
166     
167     private static String JavaDoc chars (int length) {
168         StringBuilder JavaDoc sb = new StringBuilder JavaDoc ();
169         for (int i = 0; i < length; i++) sb.append (' ');
170         return sb.toString ();
171     }
172 }
173
Popular Tags