KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > correction > ExternalizeResolution


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.correction;
12
13 import org.eclipse.core.filebuffers.FileBuffers;
14 import org.eclipse.core.filebuffers.ITextFileBuffer;
15 import org.eclipse.core.filebuffers.ITextFileBufferManager;
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IMarker;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.pde.core.IBaseModel;
25 import org.eclipse.pde.core.plugin.IPluginModelBase;
26 import org.eclipse.pde.internal.core.PDEManager;
27 import org.eclipse.pde.internal.core.ibundle.IBundle;
28 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
29 import org.eclipse.pde.internal.ui.PDEPlugin;
30 import org.eclipse.pde.internal.ui.PDEUIMessages;
31 import org.eclipse.pde.internal.ui.nls.ExternalizeStringsOperation;
32 import org.eclipse.pde.internal.ui.nls.ModelChange;
33 import org.eclipse.pde.internal.ui.nls.ModelChangeElement;
34 import org.eclipse.pde.internal.ui.util.ModelModification;
35 import org.eclipse.pde.internal.ui.util.PDEModelUtility;
36 import org.eclipse.text.edits.MalformedTreeException;
37 import org.osgi.framework.Constants;
38
39 public class ExternalizeResolution extends AbstractXMLMarkerResolution {
40
41     public ExternalizeResolution(int resolutionType, IMarker marker) {
42         super(resolutionType, marker);
43     }
44
45     protected void createChange(IPluginModelBase model) {
46         Object JavaDoc node = findNode(model);
47         ModelChange change = new ModelChange(model, true);
48         ModelChangeElement element = new ModelChangeElement(change, node);
49         if (element.updateValue()) {
50             String JavaDoc localization = PDEManager.getBundleLocalization(model);
51             if (localization == null)
52                 addLocalization(model, localization = "plugin"); //$NON-NLS-1$
53
localization += ModelChange.LOCALIZATION_FILE_SUFFIX;
54             IProject project = model.getUnderlyingResource().getProject();
55             IFile file = project.getFile(localization);
56             ExternalizeStringsOperation.checkPropertiesFile(file);
57             try {
58                 ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
59                 manager.connect(file.getFullPath(), null);
60                 ITextFileBuffer buffer = manager.getTextFileBuffer(file.getFullPath());
61                 if (buffer.isDirty())
62                     buffer.commit(null, true);
63                 
64                 IDocument document = buffer.getDocument();
65                 ExternalizeStringsOperation.getPropertiesInsertEdit(document, element).apply(document);
66                 buffer.commit(null, true);
67             } catch (CoreException e) {
68                 PDEPlugin.log(e);
69             } catch (MalformedTreeException e) {
70                 PDEPlugin.log(e);
71             } catch (BadLocationException e) {
72                 PDEPlugin.log(e);
73             } finally {
74                 try {
75                     FileBuffers.getTextFileBufferManager().disconnect(file.getFullPath(), null);
76                 } catch (CoreException e) {
77                     PDEPlugin.log(e);
78                 }
79             }
80         }
81     }
82
83     public String JavaDoc getLabel() {
84         if (isAttrNode())
85             return NLS.bind(PDEUIMessages.ExternalizeResolution_attrib, getNameOfNode());
86         if (fLocationPath.charAt(0) == '(')
87             return NLS.bind(PDEUIMessages.ExternalizeResolution_text, getNameOfNode());
88         return NLS.bind(PDEUIMessages.ExternalizeResolution_header, fLocationPath);
89     }
90     
91         private void addLocalization(IPluginModelBase model, String JavaDoc localizationValue) {
92         // should always be IBundlePluginModelBase. Only time wasn't was when we only passed in plugin.xml to ModelModification contructor.
93
// Now that we pass in both the Manifest and plugin.xml if we are externalizing the a plugin.xml string (see run(IMarker)),
94
// model should always be IBundlePluginModelBase
95
if (model instanceof IBundlePluginModelBase) {
96             IBundle bundle = ((IBundlePluginModelBase)model).getBundleModel().getBundle();
97             bundle.setHeader(Constants.BUNDLE_LOCALIZATION, localizationValue);
98         }
99     }
100     
101     public void run(IMarker marker) {
102         fResource = marker.getResource();
103         IFile file = ((IFile)marker.getResource());
104         ModelModification modification = null;
105         // if file we are externalizing is not manifest, try to pass manifest in if it exists
106
if (!file.getName().equals(PDEModelUtility.F_MANIFEST)) {
107             IFile manifest = file.getProject().getFile(PDEModelUtility.F_MANIFEST_FP);
108             if (manifest.exists()) {
109                 modification = new ModelModification(manifest, file) {
110                     protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException {
111                         createChange(model);
112                     }
113                 };
114             }
115         }
116         if (modification == null) {
117             modification = new ModelModification(file) {
118                 protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException {
119                     createChange(model);
120                 }
121             };
122         }
123         PDEModelUtility.modifyModel(modification, null);
124     }
125
126 }
127
Popular Tags