Lines 10-38
Link Here
|
10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.pde.internal.ui.nls; |
11 |
package org.eclipse.pde.internal.ui.nls; |
12 |
|
12 |
|
13 |
import java.io.ByteArrayInputStream; |
|
|
14 |
import java.io.IOException; |
15 |
import java.lang.reflect.InvocationTargetException; |
13 |
import java.lang.reflect.InvocationTargetException; |
|
|
14 |
import java.util.HashMap; |
16 |
import java.util.Iterator; |
15 |
import java.util.Iterator; |
17 |
|
16 |
|
18 |
import org.eclipse.core.filebuffers.FileBuffers; |
17 |
import org.eclipse.core.filebuffers.FileBuffers; |
19 |
import org.eclipse.core.filebuffers.ITextFileBuffer; |
18 |
import org.eclipse.core.filebuffers.ITextFileBuffer; |
20 |
import org.eclipse.core.filebuffers.ITextFileBufferManager; |
19 |
import org.eclipse.core.filebuffers.ITextFileBufferManager; |
21 |
import org.eclipse.core.resources.IContainer; |
20 |
import org.eclipse.core.filebuffers.LocationKind; |
22 |
import org.eclipse.core.resources.IFile; |
21 |
import org.eclipse.core.resources.IFile; |
23 |
import org.eclipse.core.resources.IFolder; |
|
|
24 |
import org.eclipse.core.runtime.CoreException; |
22 |
import org.eclipse.core.runtime.CoreException; |
25 |
import org.eclipse.core.runtime.IProgressMonitor; |
23 |
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; |
24 |
import org.eclipse.jface.text.IDocument; |
29 |
import org.eclipse.jface.text.TextUtilities; |
25 |
import org.eclipse.jface.text.TextUtilities; |
|
|
26 |
import org.eclipse.ltk.core.refactoring.CompositeChange; |
27 |
import org.eclipse.ltk.core.refactoring.TextFileChange; |
30 |
import org.eclipse.osgi.util.NLS; |
28 |
import org.eclipse.osgi.util.NLS; |
31 |
import org.eclipse.pde.core.IBaseModel; |
29 |
import org.eclipse.pde.core.IBaseModel; |
32 |
import org.eclipse.pde.core.plugin.IPluginModelBase; |
30 |
import org.eclipse.pde.core.plugin.IPluginModelBase; |
33 |
import org.eclipse.pde.internal.core.ibundle.IBundle; |
31 |
import org.eclipse.pde.internal.core.ibundle.IBundle; |
34 |
import org.eclipse.pde.internal.core.ibundle.IBundlePluginModel; |
32 |
import org.eclipse.pde.internal.core.ibundle.IBundlePluginModel; |
35 |
import org.eclipse.pde.internal.core.util.CoreUtility; |
33 |
import org.eclipse.pde.internal.ui.PDEUIMessages; |
36 |
import org.eclipse.pde.internal.ui.util.ModelModification; |
34 |
import org.eclipse.pde.internal.ui.util.ModelModification; |
37 |
import org.eclipse.pde.internal.ui.util.PDEModelUtility; |
35 |
import org.eclipse.pde.internal.ui.util.PDEModelUtility; |
38 |
import org.eclipse.text.edits.InsertEdit; |
36 |
import org.eclipse.text.edits.InsertEdit; |
Lines 44-91
Link Here
|
44 |
public class ExternalizeStringsOperation extends WorkspaceModifyOperation { |
42 |
public class ExternalizeStringsOperation extends WorkspaceModifyOperation { |
45 |
|
43 |
|
46 |
private Object[] fChangeFiles; |
44 |
private Object[] fChangeFiles; |
|
|
45 |
private CompositeChange fParentChange; |
46 |
private HashMap fCompositeChanges; |
47 |
private HashMap fFileEdits; |
47 |
|
48 |
|
48 |
public ExternalizeStringsOperation(Object[] changeFiles) { |
49 |
public ExternalizeStringsOperation(Object[] changeFiles, CompositeChange parentChange) { |
49 |
fChangeFiles = changeFiles; |
50 |
fChangeFiles = changeFiles; |
|
|
51 |
fParentChange = parentChange; |
52 |
fCompositeChanges = new HashMap(); |
53 |
fFileEdits = new HashMap(); |
50 |
} |
54 |
} |
51 |
protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { |
55 |
protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { |
52 |
for (int i = 0; i < fChangeFiles.length; i++) { |
56 |
for (int i = 0; i < fChangeFiles.length; i++) { |
53 |
if (fChangeFiles[i] instanceof ModelChangeFile) { |
57 |
if (fChangeFiles[i] instanceof ModelChangeFile) { |
54 |
ModelChangeFile changeFile = (ModelChangeFile)fChangeFiles[i]; |
58 |
ModelChangeFile changeFile = (ModelChangeFile)fChangeFiles[i]; |
|
|
59 |
CompositeChange pluginChange = getChangeForPlugin(changeFile.getModel().getParentModel().getPluginBase().getId()); |
55 |
ModelChange change = changeFile.getModel(); |
60 |
ModelChange change = changeFile.getModel(); |
56 |
IFile pFile = change.getPropertiesFile(); |
61 |
IFile pFile = change.getPropertiesFile(); |
57 |
checkPropertiesFile(pFile); |
62 |
// if the properties file does not exist and we have not already made a TextFileChange |
|
|
63 |
// for it create the Change and insert a comment |
64 |
if (!pFile.exists() && !fFileEdits.containsKey(pFile)) |
65 |
getEditForFile(pFile, pluginChange).addChild(new InsertEdit(0, getPropertiesFileComment(pFile))); |
58 |
if (!change.localizationSet()) |
66 |
if (!change.localizationSet()) |
59 |
addBundleLocalization(change, monitor); |
67 |
addBundleLocalization(change, monitor, pluginChange); |
60 |
|
68 |
|
61 |
ITextFileBufferManager pManager = FileBuffers.getTextFileBufferManager(); |
69 |
ITextFileBufferManager pManager = FileBuffers.getTextFileBufferManager(); |
62 |
try { |
70 |
try { |
63 |
pManager.connect(pFile.getFullPath(), monitor); |
71 |
pManager.connect(pFile.getFullPath(), LocationKind.IFILE, monitor); |
64 |
ITextFileBuffer pBuffer = pManager.getTextFileBuffer(pFile.getFullPath()); |
72 |
ITextFileBuffer pBuffer = pManager.getTextFileBuffer(pFile.getFullPath(), LocationKind.IFILE); |
65 |
IDocument pDoc = pBuffer.getDocument(); |
73 |
IDocument pDoc = pBuffer.getDocument(); |
66 |
MultiTextEdit pEdit = new MultiTextEdit(); |
74 |
MultiTextEdit pEdit = getEditForFile(pFile, pluginChange); |
67 |
|
75 |
|
68 |
doReplace(changeFile, pDoc, pEdit, monitor); |
76 |
doReplace(changeFile, pDoc, pEdit, monitor, pluginChange); |
69 |
|
|
|
70 |
pEdit.apply(pDoc); |
71 |
pBuffer.commit(monitor, true); |
72 |
|
77 |
|
73 |
} catch (MalformedTreeException e) { |
78 |
} catch (MalformedTreeException e) { |
74 |
} catch (BadLocationException e) { |
|
|
75 |
} finally { |
79 |
} finally { |
76 |
pManager.disconnect(pFile.getFullPath(), monitor); |
80 |
pManager.disconnect(pFile.getFullPath(), LocationKind.IFILE, monitor); |
77 |
} |
81 |
} |
78 |
} |
82 |
} |
79 |
} |
83 |
} |
80 |
} |
84 |
} |
81 |
private void doReplace(ModelChangeFile changeFile, IDocument pDoc, MultiTextEdit pEdit, IProgressMonitor monitor) throws CoreException { |
85 |
private CompositeChange getChangeForPlugin(String pluginName) { |
|
|
86 |
if (fCompositeChanges.containsKey(pluginName)) |
87 |
return (CompositeChange) fCompositeChanges.get(pluginName); |
88 |
CompositeChange result = new CompositeChange(NLS.bind(PDEUIMessages.ExternalizeStringsOperation_pluginChangeName, pluginName)); |
89 |
fCompositeChanges.put(pluginName, result); |
90 |
fParentChange.add(result); |
91 |
return result; |
92 |
} |
93 |
private MultiTextEdit getEditForFile(IFile file, CompositeChange parentChange) { |
94 |
if (fFileEdits.containsKey(file)) |
95 |
return (MultiTextEdit) fFileEdits.get(file); |
96 |
MultiTextEdit edit = new MultiTextEdit(); |
97 |
TextFileChange change = new TextFileChange(file.getName(), file); |
98 |
change.setEdit(edit); |
99 |
// mark a plugin.xml or a fragment.xml as PLUGIN2 type so they will be compared |
100 |
// with the PluginContentMergeViewer |
101 |
String textType = file.getName().equals("plugin.xml") || //$NON-NLS-1$ |
102 |
file.getName().equals("fragment.xml") ? //$NON-NLS-1$ |
103 |
"PLUGIN2" : file.getFileExtension(); //$NON-NLS-1$ |
104 |
change.setTextType(textType); |
105 |
parentChange.add(change); |
106 |
fFileEdits.put(file, edit); |
107 |
return edit; |
108 |
} |
109 |
private void doReplace(ModelChangeFile changeFile, IDocument pDoc, MultiTextEdit pEdit, IProgressMonitor monitor, CompositeChange parentChange) throws CoreException { |
82 |
IFile uFile = changeFile.getFile(); |
110 |
IFile uFile = changeFile.getFile(); |
83 |
ITextFileBufferManager uManager = FileBuffers.getTextFileBufferManager(); |
|
|
84 |
try { |
111 |
try { |
85 |
uManager.connect(uFile.getFullPath(), monitor); |
112 |
MultiTextEdit uEdit = getEditForFile(uFile, parentChange); |
86 |
ITextFileBuffer uBuffer = uManager.getTextFileBuffer(uFile.getFullPath()); |
|
|
87 |
IDocument uDoc = uBuffer.getDocument(); |
88 |
MultiTextEdit uEdit = new MultiTextEdit(); |
89 |
|
113 |
|
90 |
Iterator iter = changeFile.getChanges().iterator(); |
114 |
Iterator iter = changeFile.getChanges().iterator(); |
91 |
|
115 |
|
Lines 98-118
Link Here
|
98 |
pEdit.addChild(getPropertiesInsertEdit(pDoc, changeElement)); |
122 |
pEdit.addChild(getPropertiesInsertEdit(pDoc, changeElement)); |
99 |
} |
123 |
} |
100 |
} |
124 |
} |
101 |
uEdit.apply(uDoc); |
|
|
102 |
uBuffer.commit(monitor, true); |
103 |
|
104 |
} catch (MalformedTreeException e) { |
125 |
} catch (MalformedTreeException e) { |
105 |
} catch (BadLocationException e) { |
|
|
106 |
} finally { |
107 |
uManager.disconnect(uFile.getFullPath(), monitor); |
108 |
} |
126 |
} |
109 |
} |
127 |
} |
110 |
|
128 |
|
111 |
private void addBundleLocalization(ModelChange change, IProgressMonitor mon) { |
129 |
private void addBundleLocalization(ModelChange change, IProgressMonitor mon, CompositeChange parent) { |
112 |
IPluginModelBase base = change.getParentModel(); |
130 |
IPluginModelBase base = change.getParentModel(); |
113 |
IFile manifest = base.getUnderlyingResource().getProject().getFile(PDEModelUtility.F_MANIFEST_FP); |
131 |
IFile manifest = base.getUnderlyingResource().getProject().getFile(PDEModelUtility.F_MANIFEST_FP); |
|
|
132 |
// if the edit for this manifest file is in the HashMap, then we must have added |
133 |
// the localization already since it is checked first (this must be the second or subsequent |
134 |
// change to the manifest for this plug-in) |
135 |
if (fFileEdits.containsKey(manifest)) |
136 |
return; |
114 |
final String localiz = change.getBundleLocalization(); |
137 |
final String localiz = change.getBundleLocalization(); |
115 |
PDEModelUtility.modifyModel(new ModelModification(manifest) { |
138 |
TextFileChange[] result = PDEModelUtility.changesForModelModication(new ModelModification(manifest) { |
116 |
protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException { |
139 |
protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException { |
117 |
if (model instanceof IBundlePluginModel) { |
140 |
if (model instanceof IBundlePluginModel) { |
118 |
IBundlePluginModel bundleModel = (IBundlePluginModel) model; |
141 |
IBundlePluginModel bundleModel = (IBundlePluginModel) model; |
Lines 121-126
Link Here
|
121 |
} |
144 |
} |
122 |
} |
145 |
} |
123 |
}, mon); |
146 |
}, mon); |
|
|
147 |
// this model change just adds localization to the manifest, so we will only have one change |
148 |
// with one edit |
149 |
if (result.length > 0 && result[0] != null) |
150 |
getEditForFile(manifest, parent).addChild(result[0].getEdit()); |
124 |
} |
151 |
} |
125 |
|
152 |
|
126 |
public static InsertEdit getPropertiesInsertEdit(IDocument doc, ModelChangeElement element) { |
153 |
public static InsertEdit getPropertiesInsertEdit(IDocument doc, ModelChangeElement element) { |
Lines 132-151
Link Here
|
132 |
return new InsertEdit(doc.getLength(), sb.toString()); |
159 |
return new InsertEdit(doc.getLength(), sb.toString()); |
133 |
} |
160 |
} |
134 |
|
161 |
|
135 |
public static void checkPropertiesFile(IFile file) { |
162 |
public static String getPropertiesFileComment(IFile file) { |
136 |
if (!file.exists()) { |
163 |
return NLS.bind("#Properties file for {0}", file.getProject().getName()); //$NON-NLS-1$ |
137 |
String propertiesFileComment = NLS.bind("#Properties file for {0}", file.getProject().getName()); //$NON-NLS-1$ |
|
|
138 |
ByteArrayInputStream pStream = new ByteArrayInputStream(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 e) { |
148 |
} |
149 |
} |
150 |
} |
164 |
} |
151 |
} |
165 |
} |