001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 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: * Peter Friese <peter.friese@gentleware.com> - bug 191769, 199431
011: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.plugin;
012:
013: import org.eclipse.core.resources.IProject;
014: import org.eclipse.core.resources.IResource;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.jdt.core.search.IJavaSearchConstants;
017: import org.eclipse.jdt.ui.IJavaElementSearchConstants;
018: import org.eclipse.pde.core.plugin.IPlugin;
019: import org.eclipse.pde.core.plugin.IPluginModelBase;
020: import org.eclipse.pde.internal.core.ICoreConstants;
021: import org.eclipse.pde.internal.core.TargetPlatformHelper;
022: import org.eclipse.pde.internal.core.bundle.BundlePluginBase;
023: import org.eclipse.pde.internal.core.ibundle.IBundle;
024: import org.eclipse.pde.internal.core.ibundle.IBundleModel;
025: import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
026: import org.eclipse.pde.internal.core.text.bundle.Bundle;
027: import org.eclipse.pde.internal.core.text.bundle.LazyStartHeader;
028: import org.eclipse.pde.internal.ui.PDEPlugin;
029: import org.eclipse.pde.internal.ui.PDEUIMessages;
030: import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
031: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
032: import org.eclipse.pde.internal.ui.editor.contentassist.TypeFieldAssistDisposer;
033: import org.eclipse.pde.internal.ui.parts.FormEntry;
034: import org.eclipse.pde.internal.ui.util.PDEJavaHelperUI;
035: import org.eclipse.swt.SWT;
036: import org.eclipse.swt.events.SelectionAdapter;
037: import org.eclipse.swt.events.SelectionEvent;
038: import org.eclipse.swt.widgets.Button;
039: import org.eclipse.swt.widgets.Composite;
040: import org.eclipse.ui.IActionBars;
041: import org.eclipse.ui.forms.editor.FormEditor;
042: import org.eclipse.ui.forms.events.HyperlinkEvent;
043: import org.eclipse.ui.forms.widgets.FormToolkit;
044: import org.eclipse.ui.forms.widgets.TableWrapData;
045:
046: public class PluginGeneralInfoSection extends GeneralInfoSection {
047:
048: private FormEntry fClassEntry;
049: private Button fLazyStart;
050: private TypeFieldAssistDisposer fTypeFieldAssistDisposer;
051:
052: public PluginGeneralInfoSection(PDEFormPage page, Composite parent) {
053: super (page, parent);
054: }
055:
056: protected String getSectionDescription() {
057: return PDEUIMessages.ManifestEditor_PluginSpecSection_desc;
058: }
059:
060: protected void createSpecificControls(Composite parent,
061: FormToolkit toolkit, IActionBars actionBars) {
062: createClassEntry(parent, toolkit, actionBars);
063: FormEditor formEditor = getPage().getEditor();
064: if (isBundle() && (formEditor instanceof ManifestEditor)
065: && ((ManifestEditor) formEditor).isEquinox()) {
066: createLazyStart(parent, toolkit, actionBars);
067: createSingleton(parent, toolkit, actionBars,
068: PDEUIMessages.PluginGeneralInfoSection_singleton);
069: }
070: }
071:
072: protected void addListeners() {
073: if (isBundle()) {
074: IBundleModel model = getBundle().getModel();
075: if (model != null)
076: model.addModelChangedListener(this );
077: }
078: super .addListeners();
079: }
080:
081: protected void removeListeners() {
082: if (isBundle()) {
083: IBundleModel model = getBundle().getModel();
084: if (model != null)
085: model.removeModelChangedListener(this );
086: }
087: super .removeListeners();
088: }
089:
090: private void createLazyStart(Composite parent, FormToolkit toolkit,
091: IActionBars actionBars) {
092: fLazyStart = toolkit.createButton(parent,
093: PDEUIMessages.PluginGeneralInfoSection_lazyStart,
094: SWT.CHECK);
095: TableWrapData td = new TableWrapData();
096: td.colspan = 3;
097: fLazyStart.setLayoutData(td);
098: fLazyStart.setEnabled(isEditable());
099: fLazyStart.addSelectionListener(new SelectionAdapter() {
100: public void widgetSelected(SelectionEvent e) {
101: IManifestHeader header = getLazyStartHeader();
102: if (header instanceof LazyStartHeader)
103: ((LazyStartHeader) header).setLazyStart(fLazyStart
104: .getSelection());
105: else
106: getBundle()
107: .setHeader(
108: getLazyStartHeaderName(),
109: Boolean.toString(fLazyStart
110: .getSelection()));
111: }
112: });
113: }
114:
115: private void createClassEntry(Composite client,
116: FormToolkit toolkit, IActionBars actionBars) {
117: boolean isEditable = isEditable();
118: fClassEntry = new FormEntry(client, toolkit,
119: PDEUIMessages.GeneralInfoSection_class,
120: PDEUIMessages.GeneralInfoSection_browse, //
121: isEditable());
122: fClassEntry.setFormEntryListener(new FormEntryAdapter(this ,
123: actionBars) {
124: public void textValueChanged(FormEntry entry) {
125: try {
126: ((IPlugin) getPluginBase()).setClassName(entry
127: .getValue());
128: } catch (CoreException e) {
129: PDEPlugin.logException(e);
130: }
131: }
132:
133: public void linkActivated(HyperlinkEvent e) {
134: String value = fClassEntry.getValue();
135: IProject project = getPage().getPDEEditor()
136: .getCommonProject();
137: value = PDEJavaHelperUI.createClass(value, project,
138: createJavaAttributeValue(), false);
139: if (value != null)
140: fClassEntry.setValue(value);
141: }
142:
143: public void browseButtonSelected(FormEntry entry) {
144: doOpenSelectionDialog(entry.getValue());
145: }
146: });
147: fClassEntry.setEditable(isEditable);
148:
149: if (isEditable) {
150: fTypeFieldAssistDisposer = PDEJavaHelperUI
151: .addTypeFieldAssistToText(fClassEntry.getText(),
152: getProject(), IJavaSearchConstants.CLASS);
153: }
154: }
155:
156: private void doOpenSelectionDialog(String className) {
157: IResource resource = getPluginBase().getModel()
158: .getUnderlyingResource();
159: String type = PDEJavaHelperUI
160: .selectType(resource,
161: IJavaElementSearchConstants.CONSIDER_CLASSES,
162: className);
163: if (type != null)
164: fClassEntry.setValue(type);
165: }
166:
167: private JavaAttributeValue createJavaAttributeValue() {
168: IProject project = getPage().getPDEEditor().getCommonProject();
169: IPluginModelBase model = (IPluginModelBase) getPage()
170: .getModel();
171: return new JavaAttributeValue(project, model, null, fClassEntry
172: .getValue());
173: }
174:
175: public void cancelEdit() {
176: fClassEntry.cancelEdit();
177: super .cancelEdit();
178: }
179:
180: public void commit(boolean onSave) {
181: fClassEntry.commit();
182: super .commit(onSave);
183: }
184:
185: public void refresh() {
186: IPluginModelBase model = (IPluginModelBase) getPage()
187: .getModel();
188: // if we are refactoring, the Manifest moves before the editor closes. This could cause the model to be null on a refresh()
189: if (model == null)
190: return;
191: IPlugin plugin = (IPlugin) model.getPluginBase();
192: // Only update this field if it already has not been modified
193: // This will prevent the cursor from being set to position 0 after
194: // accepting a field assist proposal using \r
195: if (fClassEntry.isDirty() == false) {
196: fClassEntry.setValue(plugin.getClassName(), true);
197: }
198: if (fLazyStart != null) {
199: IManifestHeader header = getLazyStartHeader();
200: fLazyStart.setSelection(header instanceof LazyStartHeader
201: && ((LazyStartHeader) header).isLazyStart());
202: }
203: super .refresh();
204: }
205:
206: private IManifestHeader getLazyStartHeader() {
207: IBundle bundle = getBundle();
208: if (bundle instanceof Bundle) {
209: IManifestHeader header = bundle
210: .getManifestHeader(ICoreConstants.ECLIPSE_LAZYSTART);
211: if (header == null)
212: header = bundle
213: .getManifestHeader(ICoreConstants.ECLIPSE_AUTOSTART);
214: return header;
215: }
216: return null;
217: }
218:
219: private String getLazyStartHeaderName() {
220: if (TargetPlatformHelper.getTargetVersion() >= 3.2
221: && BundlePluginBase
222: .getBundleManifestVersion(getBundle()) >= 2)
223: return ICoreConstants.ECLIPSE_LAZYSTART;
224: return ICoreConstants.ECLIPSE_AUTOSTART;
225: }
226:
227: /* (non-Javadoc)
228: * @see org.eclipse.pde.internal.ui.editor.plugin.GeneralInfoSection#dispose()
229: */
230: public void dispose() {
231: super.dispose();
232: if (fTypeFieldAssistDisposer != null) {
233: fTypeFieldAssistDisposer.dispose();
234: }
235: }
236:
237: }
|