KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > i18n > TranslateSuggestionProvider


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.tasklist.i18n;
21
22 import java.text.MessageFormat JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.logging.*;
27 import javax.swing.text.BadLocationException JavaDoc;
28 import javax.swing.text.Document JavaDoc;
29 import javax.swing.text.StyledDocument JavaDoc;
30 import org.netbeans.api.mdr.MDRepository;
31 import org.netbeans.jmi.javamodel.Element;
32 import org.netbeans.jmi.javamodel.Resource;
33 import org.netbeans.jmi.javamodel.StringLiteral;
34 import org.netbeans.modules.javacore.api.JavaModel;
35
36 import org.openide.ErrorManager;
37 import org.openide.filesystems.FileObject;
38 import org.openide.loaders.DataObject;
39 import org.openide.loaders.DataObjectNotFoundException;
40 import org.openide.util.NbBundle;
41
42 import org.netbeans.modules.tasklist.client.*;
43 import org.netbeans.modules.tasklist.providers.DocumentSuggestionProvider;
44 import org.netbeans.modules.tasklist.providers.SuggestionContext;
45 import org.netbeans.modules.tasklist.core.TLUtils;
46 import org.netbeans.modules.tasklist.core.util.TextPositionsMapper;
47 import org.openide.text.NbDocument;
48
49
50 /**
51  * This class scans for Java string tokens without a // NOI18N comment
52  *
53  * @author tl
54  */

55 public class TranslateSuggestionProvider extends DocumentSuggestionProvider {
56     public final static String JavaDoc TYPE = "nb-tasklist-i18n"; // NOI18N
57
public static final Logger LOGGER = TLUtils.getLogger(TranslateSuggestionProvider.class);
58     private static final String JavaDoc TEXT = NbBundle.getMessage(
59         TranslateSuggestionProvider.class, "ProblemText"); // NOI18N
60

61     static {
62         LOGGER.setLevel(Level.FINE);
63         Thread JavaDoc t = new Thread JavaDoc(new TranslateOpenProjectsScanner());
64         t.setPriority(Thread.MIN_PRIORITY);
65         t.setDaemon(true);
66         t.start();
67     }
68
69     /** The list of tasks we're currently showing in the tasklist */
70     private List JavaDoc showingTasks = null;
71
72     public TranslateSuggestionProvider() {
73     }
74     
75     public String JavaDoc getType() {
76         return TYPE;
77     }
78
79     public void rescan(SuggestionContext env, Object JavaDoc request) {
80         List JavaDoc newTasks = scan(env);
81         SuggestionManager manager = SuggestionManager.getDefault();
82
83         if ((newTasks == null) && (showingTasks == null)) {
84             return;
85         }
86         manager.register(TYPE, newTasks, showingTasks);
87         showingTasks = newTasks;
88     }
89
90     public List JavaDoc scan(SuggestionContext env) {
91         SuggestionManager manager = SuggestionManager.getDefault();
92         if (!manager.isEnabled(TYPE)) {
93             return null;
94         }
95         TranslateFileChecker c = new TranslateFileChecker(env.getFileObject());
96         TranslateFileChecker.Error[] err = c.run();
97         List JavaDoc tasks = new ArrayList JavaDoc(err.length);
98         
99         for (int i = 0; i < err.length; i++) {
100             SuggestionPerformer action = new AddI18NCommentPerformer();
101             String JavaDoc text = MessageFormat.format(TEXT, new Object JavaDoc[] {
102                 err[i].constant
103             });
104             SuggestionAgent problem = SuggestionManager.getDefault().
105                 createSuggestion(null, TYPE, text, action, null);
106             try {
107                 DataObject dataObject = DataObject.find(env.getFileObject());
108                 problem.setLine(TLUtils.getLineByNumber(dataObject,
109                     err[i].line + 1));
110             } catch (DataObjectNotFoundException e) {
111                 // ignore
112
ErrorManager.getDefault().notify(e);
113             }
114             tasks.add(problem.getSuggestion());
115         }
116         
117         return tasks;
118     }
119
120     public void clear(SuggestionContext env,
121                       Object JavaDoc request) {
122         // Remove existing items
123
if (showingTasks != null) {
124             SuggestionManager manager = SuggestionManager.getDefault();
125             manager.register(TYPE, null, showingTasks);
126             showingTasks = null;
127         }
128     }
129
130 }
131
Popular Tags