001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.text.plugin;
011:
012: import java.net.MalformedURLException;
013: import java.net.URL;
014:
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.jface.text.IDocument;
017: import org.eclipse.pde.core.IModel;
018: import org.eclipse.pde.core.IWritable;
019: import org.eclipse.pde.core.build.IBuildModel;
020: import org.eclipse.pde.core.plugin.IExtensions;
021: import org.eclipse.pde.core.plugin.IExtensionsModelFactory;
022: import org.eclipse.pde.core.plugin.IPluginBase;
023: import org.eclipse.pde.core.plugin.IPluginModelBase;
024: import org.eclipse.pde.core.plugin.IPluginModelFactory;
025: import org.eclipse.pde.internal.core.NLResourceHelper;
026: import org.eclipse.pde.internal.core.PDEManager;
027: import org.eclipse.pde.internal.core.text.IDocumentElementNode;
028: import org.eclipse.pde.internal.core.text.XMLEditingModel;
029: import org.xml.sax.helpers.DefaultHandler;
030:
031: public abstract class PluginModelBase extends XMLEditingModel implements
032: IPluginModelBase {
033:
034: private PluginBaseNode fPluginBase;
035: private boolean fIsEnabled;
036: private PluginDocumentHandler fHandler;
037: private IPluginModelFactory fFactory;
038: private String fLocalization;
039:
040: public PluginModelBase(IDocument document, boolean isReconciling) {
041: super (document, isReconciling);
042: fFactory = new PluginDocumentNodeFactory(this );
043: }
044:
045: /* (non-Javadoc)
046: * @see org.eclipse.pde.core.plugin.IPluginModelBase#createPluginBase()
047: */
048: public IPluginBase createPluginBase(boolean isFragment) {
049: if (isFragment) {
050: fPluginBase = new FragmentNode();
051: fPluginBase.setXMLTagName("fragment"); //$NON-NLS-1$
052: } else {
053: fPluginBase = new PluginNode();
054: fPluginBase.setXMLTagName("plugin"); //$NON-NLS-1$
055: }
056: fPluginBase.setInTheModel(true);
057: fPluginBase.setModel(this );
058: return fPluginBase;
059: }
060:
061: protected IWritable getRoot() {
062: return getPluginBase();
063: }
064:
065: public IPluginBase createPluginBase() {
066: return createPluginBase(isFragmentModel());
067: }
068:
069: /* (non-Javadoc)
070: * @see org.eclipse.pde.core.plugin.IPluginModelBase#getBuildModel()
071: */
072: public IBuildModel getBuildModel() {
073: return null;
074: }
075:
076: /* (non-Javadoc)
077: * @see org.eclipse.pde.core.plugin.IPluginModelBase#getPluginBase()
078: */
079: public IPluginBase getPluginBase() {
080: return getPluginBase(true);
081: }
082:
083: public IExtensions getExtensions() {
084: return getPluginBase();
085: }
086:
087: /* (non-Javadoc)
088: * @see org.eclipse.pde.core.plugin.IPluginModelBase#getPluginBase(boolean)
089: */
090: public IPluginBase getPluginBase(boolean createIfMissing) {
091: if (!fLoaded && createIfMissing) {
092: createPluginBase();
093: try {
094: load();
095: } catch (CoreException e) {
096: }
097: }
098: return fPluginBase;
099: }
100:
101: public IExtensions getExtensions(boolean createIfMissing) {
102: return getPluginBase(createIfMissing);
103: }
104:
105: /* (non-Javadoc)
106: * @see org.eclipse.pde.core.plugin.IPluginModelBase#isEnabled()
107: */
108: public boolean isEnabled() {
109: return fIsEnabled;
110: }
111:
112: /* (non-Javadoc)
113: * @see org.eclipse.pde.core.plugin.IPluginModelBase#setEnabled(boolean)
114: */
115: public void setEnabled(boolean enabled) {
116: fIsEnabled = enabled;
117: }
118:
119: /* (non-Javadoc)
120: * @see org.eclipse.pde.core.plugin.IPluginModelBase#getPluginFactory()
121: */
122: public IPluginModelFactory getPluginFactory() {
123: return fFactory;
124: }
125:
126: /* (non-Javadoc)
127: * @see org.eclipse.pde.core.plugin.IPluginModelBase#getNLLookupLocation()
128: */
129: public URL getNLLookupLocation() {
130: try {
131: String installLocation = getInstallLocation();
132: return installLocation == null ? null : new URL(
133: "file:" + installLocation); //$NON-NLS-1$
134: } catch (MalformedURLException e) {
135: }
136: return null;
137: }
138:
139: /* (non-Javadoc)
140: * @see org.eclipse.pde.core.plugin.ISharedPluginModel#getFactory()
141: */
142: public IExtensionsModelFactory getFactory() {
143: return fFactory;
144: }
145:
146: /* (non-Javadoc)
147: * @see org.eclipse.pde.internal.ui.model.AbstractEditingModel#createNLResourceHelper()
148: */
149: protected NLResourceHelper createNLResourceHelper() {
150: URL[] locations = PDEManager.getNLLookupLocations(this );
151: return (locations.length == 0) ? null : new NLResourceHelper(
152: fLocalization == null ? "plugin" : fLocalization, //$NON-NLS-1$
153: locations);
154: }
155:
156: /* (non-Javadoc)
157: * @see org.eclipse.pde.internal.ui.model.XMLEditingModel#createDocumentHandler(org.eclipse.pde.core.IModel)
158: */
159: protected DefaultHandler createDocumentHandler(IModel model,
160: boolean reconciling) {
161: if (fHandler == null)
162: fHandler = new PluginDocumentHandler(this , reconciling);
163: return fHandler;
164: }
165:
166: public IDocumentElementNode getLastErrorNode() {
167: if (fHandler != null)
168: return fHandler.getLastErrorNode();
169: return null;
170: }
171:
172: public void setLocalization(String localization) {
173: fLocalization = localization;
174: }
175: }
|