KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.ByteArrayInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import org.eclipse.core.filebuffers.FileBuffers;
19 import org.eclipse.core.filebuffers.ITextFileBuffer;
20 import org.eclipse.core.filebuffers.ITextFileBufferManager;
21 import org.eclipse.core.resources.IContainer;
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IFolder;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.NullProgressMonitor;
27 import org.eclipse.jface.text.BadLocationException;
28 import org.eclipse.jface.text.IDocument;
29 import org.eclipse.jface.text.TextUtilities;
30 import org.eclipse.osgi.util.NLS;
31 import org.eclipse.pde.core.IBaseModel;
32 import org.eclipse.pde.core.plugin.IPluginModelBase;
33 import org.eclipse.pde.internal.core.ibundle.IBundle;
34 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModel;
35 import org.eclipse.pde.internal.core.util.CoreUtility;
36 import org.eclipse.pde.internal.ui.util.ModelModification;
37 import org.eclipse.pde.internal.ui.util.PDEModelUtility;
38 import org.eclipse.text.edits.InsertEdit;
39 import org.eclipse.text.edits.MalformedTreeException;
40 import org.eclipse.text.edits.MultiTextEdit;
41 import org.eclipse.text.edits.ReplaceEdit;
42 import org.eclipse.ui.actions.WorkspaceModifyOperation;
43
44 public class ExternalizeStringsOperation extends WorkspaceModifyOperation {
45
46     private Object JavaDoc[] fChangeFiles;
47     
48     public ExternalizeStringsOperation(Object JavaDoc[] changeFiles) {
49         fChangeFiles = changeFiles;
50     }
51     protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException JavaDoc, InterruptedException JavaDoc {
52         for (int i = 0; i < fChangeFiles.length; i++) {
53             if (fChangeFiles[i] instanceof ModelChangeFile) {
54                 ModelChangeFile changeFile = (ModelChangeFile)fChangeFiles[i];
55                 ModelChange change = changeFile.getModel();
56                 IFile pFile = change.getPropertiesFile();
57                 checkPropertiesFile(pFile);
58                 if (!change.localizationSet())
59                     addBundleLocalization(change, monitor);
60                 
61                 ITextFileBufferManager pManager = FileBuffers.getTextFileBufferManager();
62                 try {
63                     pManager.connect(pFile.getFullPath(), monitor);
64                     ITextFileBuffer pBuffer = pManager.getTextFileBuffer(pFile.getFullPath());
65                     IDocument pDoc = pBuffer.getDocument();
66                     MultiTextEdit pEdit = new MultiTextEdit();
67                     
68                     doReplace(changeFile, pDoc, pEdit, monitor);
69                     
70                     pEdit.apply(pDoc);
71                     pBuffer.commit(monitor, true);
72                     
73                 } catch (MalformedTreeException e) {
74                 } catch (BadLocationException e) {
75                 } finally {
76                     pManager.disconnect(pFile.getFullPath(), monitor);
77                 }
78             }
79         }
80     }
81     private void doReplace(ModelChangeFile changeFile, IDocument pDoc, MultiTextEdit pEdit, IProgressMonitor monitor) throws CoreException {
82         IFile uFile = changeFile.getFile();
83         ITextFileBufferManager uManager = FileBuffers.getTextFileBufferManager();
84         try {
85             uManager.connect(uFile.getFullPath(), monitor);
86             ITextFileBuffer uBuffer = uManager.getTextFileBuffer(uFile.getFullPath());
87             IDocument uDoc = uBuffer.getDocument();
88             MultiTextEdit uEdit = new MultiTextEdit();
89             
90             Iterator JavaDoc iter = changeFile.getChanges().iterator();
91             
92             while (iter.hasNext()) {
93                 ModelChangeElement changeElement = (ModelChangeElement)iter.next();
94                 if (changeElement.isExternalized()) {
95                     uEdit.addChild(new ReplaceEdit(changeElement.getOffset(),
96                             changeElement.getLength(),
97                             changeElement.getExternKey()));
98                     pEdit.addChild(getPropertiesInsertEdit(pDoc, changeElement));
99                 }
100             }
101             uEdit.apply(uDoc);
102             uBuffer.commit(monitor, true);
103             
104         } catch (MalformedTreeException e) {
105         } catch (BadLocationException e) {
106         } finally {
107             uManager.disconnect(uFile.getFullPath(), monitor);
108         }
109     }
110     
111     private void addBundleLocalization(ModelChange change, IProgressMonitor mon) {
112         IPluginModelBase base = change.getParentModel();
113         IFile manifest = base.getUnderlyingResource().getProject().getFile(PDEModelUtility.F_MANIFEST_FP);
114         final String JavaDoc localiz = change.getBundleLocalization();
115         PDEModelUtility.modifyModel(new ModelModification(manifest) {
116             protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException {
117                 if (model instanceof IBundlePluginModel) {
118                     IBundlePluginModel bundleModel = (IBundlePluginModel) model;
119                     IBundle bundle = bundleModel.getBundleModel().getBundle();
120                     bundle.setLocalization(localiz);
121                 }
122             }
123         }, mon);
124     }
125     
126     public static InsertEdit getPropertiesInsertEdit(IDocument doc, ModelChangeElement element) {
127         String JavaDoc nl = TextUtilities.getDefaultLineDelimiter(doc);
128         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(nl);
129         sb.append(element.getKey());
130         sb.append(" = "); //$NON-NLS-1$
131
sb.append(StringHelper.preparePropertiesString(element.getValue(), nl.toCharArray()));
132         return new InsertEdit(doc.getLength(), sb.toString());
133     }
134     
135     public static void checkPropertiesFile(IFile file) {
136         if (!file.exists()) {
137             String JavaDoc propertiesFileComment = NLS.bind("#Properties file for {0}", file.getProject().getName()); //$NON-NLS-1$
138
ByteArrayInputStream JavaDoc pStream = new ByteArrayInputStream JavaDoc(propertiesFileComment.getBytes());
139             try {
140                 IContainer container = file.getParent();
141                 if (!container.exists())
142                     // project will exists, therefore we can assume if !IContainer.exist(), the object is an IFolder
143
CoreUtility.createFolder((IFolder)container);
144                 file.create(pStream, true, new NullProgressMonitor());
145                 pStream.close();
146             } catch (CoreException e1) {
147             } catch (IOException JavaDoc e) {
148             }
149         }
150     }
151  }
152
Popular Tags