KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.util.Properties JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.pde.core.plugin.IFragmentModel;
17 import org.eclipse.pde.internal.core.text.IDocumentAttribute;
18 import org.eclipse.pde.internal.core.text.IDocumentTextNode;
19 import org.eclipse.pde.internal.core.text.bundle.ManifestHeader;
20 import org.eclipse.pde.internal.core.text.plugin.PluginAttribute;
21 import org.eclipse.pde.internal.core.text.plugin.PluginElementNode;
22 import org.eclipse.pde.internal.core.text.plugin.PluginExtensionPointNode;
23
24 public class ModelChangeElement {
25     
26     private static final String JavaDoc DELIM = "."; //$NON-NLS-1$
27
private static final String JavaDoc KEY_PREFIX = "%"; //$NON-NLS-1$
28
private static final String JavaDoc FRAGMENT_PREFIX = "f"; //$NON-NLS-1$
29

30     private String JavaDoc fValue = ""; //$NON-NLS-1$
31
private String JavaDoc fKey = ""; //$NON-NLS-1$
32
private int fOffset = 0;
33     private int fLength = 0;
34     private boolean fExternalized = true;
35     private ModelChange fParent;
36     private Object JavaDoc fUnderlying;
37     
38     public ModelChangeElement(ModelChange parent, Object JavaDoc incoming) {
39         fParent = parent;
40         fUnderlying = incoming;
41         if (incoming instanceof PluginElementNode) {
42             PluginElementNode elem = (PluginElementNode)incoming;
43             IDocumentTextNode text = elem.getTextNode();
44             fValue = elem.getText();
45             generateValidKey(elem.getParent().getName(), elem.getName());
46             fOffset = text.getOffset();
47             fLength = text.getLength();
48         } else if (incoming instanceof PluginAttribute) {
49             PluginAttribute attr = (PluginAttribute)incoming;
50             fValue = attr.getValue();
51             generateValidKey(attr.getEnclosingElement().getXMLTagName(), attr.getName());
52             fOffset = attr.getValueOffset();
53             fLength = attr.getValueLength();
54         } else if (incoming instanceof PluginExtensionPointNode) {
55             PluginExtensionPointNode extP = (PluginExtensionPointNode)incoming;
56             fValue = extP.getName();
57             generateValidKey("extension-point", "name"); //$NON-NLS-1$ //$NON-NLS-2$
58
IDocumentAttribute attr = extP.getDocumentAttribute("name"); //$NON-NLS-1$
59
fOffset = attr.getValueOffset();
60             fLength = attr.getValueLength();
61         } else if (incoming instanceof ManifestHeader) {
62             ManifestHeader header = (ManifestHeader)incoming;
63             fValue = header.getValue();
64             generateValidKey(header.getName());
65             fLength = fValue.length();
66             fOffset = header.getOffset() + header.getLength() - header.getLineLimiter().length() - fLength;
67         }
68     }
69     
70     public String JavaDoc getKey() {
71         return fKey;
72     }
73     public void setKey(String JavaDoc key) {
74         fKey = key;
75     }
76     public String JavaDoc getValue() {
77         return fValue;
78     }
79     public void setValue(String JavaDoc value) {
80         fValue = value;
81     }
82     public boolean isExternalized() {
83         return fExternalized;
84     }
85     public void setExternalized(boolean externalzied) {
86         fExternalized = externalzied;
87     }
88     public int getOffset() {
89         return fOffset;
90     }
91     public int getLength() {
92         return fLength;
93     }
94     
95     private void generateValidKey(String JavaDoc pre, String JavaDoc mid) {
96         generateValidKey(pre + DELIM + mid);
97     }
98
99     private void generateValidKey(String JavaDoc key) {
100         int suffix = 0;
101         Properties JavaDoc properties = fParent.getProperties();
102         String JavaDoc newKey = fParent.getParentModel() instanceof IFragmentModel ?
103                 key + DELIM + FRAGMENT_PREFIX :
104                 key + DELIM;
105         while (properties.containsKey(newKey + suffix))
106             suffix += 1;
107         properties.setProperty(newKey + suffix, fValue);
108         fKey = newKey + suffix;
109     }
110     public String JavaDoc getExternKey() {
111         return KEY_PREFIX + fKey;
112     }
113     
114     public boolean updateValue() {
115         try {
116             String JavaDoc key = getExternKey();
117             if (fUnderlying instanceof PluginElementNode) {
118                 PluginElementNode elem = (PluginElementNode)fUnderlying;
119                 elem.setText(key);
120             } else if (fUnderlying instanceof PluginAttribute) {
121                 PluginAttribute attr = (PluginAttribute)fUnderlying;
122                 String JavaDoc attrName = attr.getName();
123                 attr.getEnclosingElement().setXMLAttribute(attrName, key);
124             } else if (fUnderlying instanceof PluginExtensionPointNode) {
125                 PluginExtensionPointNode extP = (PluginExtensionPointNode)fUnderlying;
126                 extP.setName(key);
127             } else if (fUnderlying instanceof ManifestHeader) {
128                 ManifestHeader header = (ManifestHeader)fUnderlying;
129                 header.setValue(key);
130             } else
131                 return false;
132         } catch (CoreException e) {
133             return false;
134         }
135         return true;
136     }
137 }
138
Popular Tags