KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > nls > GetNonExternalizedStringsOperation


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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 package org.eclipse.pde.internal.ui.nls;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.SubProgressMonitor;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.pde.core.IBaseModel;
25 import org.eclipse.pde.core.plugin.IPluginAttribute;
26 import org.eclipse.pde.core.plugin.IPluginBase;
27 import org.eclipse.pde.core.plugin.IPluginElement;
28 import org.eclipse.pde.core.plugin.IPluginExtension;
29 import org.eclipse.pde.core.plugin.IPluginModelBase;
30 import org.eclipse.pde.core.plugin.IPluginObject;
31 import org.eclipse.pde.core.plugin.IPluginParent;
32 import org.eclipse.pde.core.plugin.ISharedExtensionsModel;
33 import org.eclipse.pde.core.plugin.PluginRegistry;
34 import org.eclipse.pde.internal.core.ICoreConstants;
35 import org.eclipse.pde.internal.core.PDECore;
36 import org.eclipse.pde.internal.core.WorkspaceModelManager;
37 import org.eclipse.pde.internal.core.ibundle.IBundle;
38 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
39 import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
40 import org.eclipse.pde.internal.core.ischema.ISchema;
41 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
42 import org.eclipse.pde.internal.core.ischema.ISchemaElement;
43 import org.eclipse.pde.internal.core.schema.SchemaRegistry;
44 import org.eclipse.pde.internal.core.text.IDocumentAttribute;
45 import org.eclipse.pde.internal.core.text.IDocumentNode;
46 import org.eclipse.pde.internal.ui.PDEUIMessages;
47 import org.eclipse.pde.internal.ui.util.ModelModification;
48 import org.eclipse.pde.internal.ui.util.PDEModelUtility;
49
50 public class GetNonExternalizedStringsOperation implements IRunnableWithProgress {
51
52     private ISelection fSelection;
53     private ArrayList JavaDoc fSelectedModels;
54     private ModelChangeTable fModelChangeTable;
55     private boolean fCanceled;
56     
57     public GetNonExternalizedStringsOperation(ISelection selection) {
58         fSelection = selection;
59     }
60
61     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
62         
63         if (fSelection instanceof IStructuredSelection) {
64             Object JavaDoc[] elems = ((IStructuredSelection) fSelection).toArray();
65             fSelectedModels = new ArrayList JavaDoc(elems.length);
66             for (int i = 0; i < elems.length; i++) {
67                 if (elems[i] instanceof IFile)
68                     elems[i] = ((IFile) elems[i]).getProject();
69                 
70                 if (elems[i] instanceof IProject
71                         && WorkspaceModelManager.isPluginProject((IProject)elems[i])
72                         && !WorkspaceModelManager.isBinaryProject((IProject)elems[i]))
73                     fSelectedModels.add(elems[i]);
74             }
75         
76             fModelChangeTable = new ModelChangeTable();
77             
78             IPluginModelBase[] pluginModels = PluginRegistry.getWorkspaceModels();
79             monitor.beginTask(PDEUIMessages.GetNonExternalizedStringsOperation_taskMessage, pluginModels.length);
80             for (int i = 0; i < pluginModels.length && !fCanceled; i++) {
81                 IProject project = pluginModels[i].getUnderlyingResource().getProject();
82                 if (!WorkspaceModelManager.isBinaryProject(project))
83                     getUnExternalizedStrings(project, new SubProgressMonitor(monitor, 1));
84             }
85         }
86     }
87     
88     private void getUnExternalizedStrings(IProject project, IProgressMonitor monitor) {
89         PDEModelUtility.modifyModel(new ModelModification(project) {
90             protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException {
91                 if (model instanceof IBundlePluginModelBase)
92                     inspectManifest((IBundlePluginModelBase)model, monitor);
93                 
94                 if (monitor.isCanceled()) {
95                     fCanceled = true;
96                     return;
97                 }
98                 
99                 if (model instanceof IPluginModelBase)
100                     inspectXML((IPluginModelBase)model, monitor);
101                 
102                 if (monitor.isCanceled()) {
103                     fCanceled = true;
104                     return;
105                 }
106             }
107         }, monitor);
108         monitor.done();
109     }
110     
111     private void inspectManifest(IBundlePluginModelBase model, IProgressMonitor monitor) throws CoreException {
112         IFile manifestFile = (IFile)model.getBundleModel().getUnderlyingResource();
113         IBundle bundle = model.getBundleModel().getBundle();
114         for (int i = 0; i < ICoreConstants.TRANSLATABLE_HEADERS.length; i++) {
115             IManifestHeader header = bundle.getManifestHeader(ICoreConstants.TRANSLATABLE_HEADERS[i]);
116             if (header != null && isNotTranslated(header.getValue()))
117                 fModelChangeTable.addToChangeTable(model, manifestFile, header, selected(manifestFile));
118         }
119     }
120     
121     private void inspectXML(IPluginModelBase model, IProgressMonitor monitor) throws CoreException {
122         IFile file;
123         if (model instanceof IBundlePluginModelBase) {
124             ISharedExtensionsModel extModel = ((IBundlePluginModelBase)model).getExtensionsModel();
125             if (extModel == null)
126                 return;
127             file = (IFile)extModel.getUnderlyingResource();
128         } else
129             file = (IFile)model.getUnderlyingResource();
130         
131         IPluginBase base = model.getPluginBase();
132         if (base instanceof IDocumentNode) {
133             // old style xml plugin
134
// check xml name declaration
135
IDocumentAttribute attr = ((IDocumentNode)base).getDocumentAttribute(IPluginObject.P_NAME);
136             if (attr != null && isNotTranslated(attr.getAttributeValue()))
137                 fModelChangeTable.addToChangeTable(model, file, attr, selected(file));
138             
139             // check xml provider declaration
140
attr = ((IDocumentNode)base).getDocumentAttribute(IPluginBase.P_PROVIDER);
141             if (attr != null && isNotTranslated(attr.getAttributeValue()))
142                 fModelChangeTable.addToChangeTable(model, file, attr, selected(file));
143         }
144         
145         SchemaRegistry registry = PDECore.getDefault().getSchemaRegistry();
146         IPluginExtension[] extensions = model.getPluginBase().getExtensions();
147         for (int i = 0; i < extensions.length; i++) {
148             ISchema schema = registry.getSchema(extensions[i].getPoint());
149             if (schema != null)
150                 inspectExtension(schema, extensions[i], model, file);
151         }
152     }
153     
154     
155     private void inspectExtension(ISchema schema, IPluginParent parent, IPluginModelBase memModel, IFile file) {
156         IPluginObject[] children = parent.getChildren();
157         for (int i = 0; i < children.length; i++) {
158             IPluginElement child = (IPluginElement)children[i];
159             ISchemaElement schemaElement = schema.findElement(child.getName());
160             if (schemaElement != null) {
161                 if (schemaElement.hasTranslatableContent())
162                     if (isNotTranslated(child.getText()))
163                         fModelChangeTable.addToChangeTable(memModel, file, child, selected(file));
164                 
165                 IPluginAttribute[] attributes = child.getAttributes();
166                 for (int j = 0; j < attributes.length; j++) {
167                     IPluginAttribute attr = attributes[j];
168                     ISchemaAttribute attInfo = schemaElement.getAttribute(attr.getName());
169                     if (attInfo != null && attInfo.isTranslatable())
170                         if (isNotTranslated(attr.getValue()))
171                             fModelChangeTable.addToChangeTable(memModel, file, attr, selected(file));
172                 }
173             }
174             inspectExtension(schema, child, memModel, file);
175         }
176     }
177     
178     private boolean isNotTranslated(String JavaDoc value) {
179         if (value == null)
180             return false;
181         if (value.length() > 0 && value.charAt(0) == '%')
182             return false;
183         return true;
184     }
185
186     protected ModelChangeTable getChangeTable() {
187         return fModelChangeTable;
188     }
189
190     public boolean wasCanceled() {
191         return fCanceled;
192     }
193     
194     private boolean selected(IFile file) {
195         return fSelectedModels.contains(file.getProject());
196     }
197 }
198
199
Popular Tags