KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > PluginGeneralInfoSection


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.editor.plugin;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.jdt.core.search.IJavaSearchConstants;
17 import org.eclipse.jdt.ui.IJavaElementSearchConstants;
18 import org.eclipse.pde.core.plugin.IPlugin;
19 import org.eclipse.pde.core.plugin.IPluginModelBase;
20 import org.eclipse.pde.internal.core.ICoreConstants;
21 import org.eclipse.pde.internal.core.TargetPlatformHelper;
22 import org.eclipse.pde.internal.core.bundle.BundlePluginBase;
23 import org.eclipse.pde.internal.core.ibundle.IBundle;
24 import org.eclipse.pde.internal.core.ibundle.IBundleModel;
25 import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
26 import org.eclipse.pde.internal.core.text.bundle.Bundle;
27 import org.eclipse.pde.internal.core.text.bundle.LazyStartHeader;
28 import org.eclipse.pde.internal.ui.PDEPlugin;
29 import org.eclipse.pde.internal.ui.PDEUIMessages;
30 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
31 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
32 import org.eclipse.pde.internal.ui.editor.contentassist.TypeFieldAssistDisposer;
33 import org.eclipse.pde.internal.ui.parts.FormEntry;
34 import org.eclipse.pde.internal.ui.util.PDEJavaHelperUI;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.events.SelectionAdapter;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.widgets.Button;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.ui.IActionBars;
41 import org.eclipse.ui.forms.editor.FormEditor;
42 import org.eclipse.ui.forms.events.HyperlinkEvent;
43 import org.eclipse.ui.forms.widgets.FormToolkit;
44 import org.eclipse.ui.forms.widgets.TableWrapData;
45
46 public class PluginGeneralInfoSection extends GeneralInfoSection {
47
48     private FormEntry fClassEntry;
49     private Button fLazyStart;
50     private TypeFieldAssistDisposer fTypeFieldAssistDisposer;
51
52     public PluginGeneralInfoSection(PDEFormPage page, Composite parent) {
53         super(page, parent);
54     }
55     
56     protected String JavaDoc getSectionDescription() {
57         return PDEUIMessages.ManifestEditor_PluginSpecSection_desc;
58     }
59     
60     protected void createSpecificControls(Composite parent, FormToolkit toolkit, IActionBars actionBars) {
61         createClassEntry(parent, toolkit, actionBars);
62         FormEditor formEditor = getPage().getEditor();
63         if (isBundle() && (formEditor instanceof ManifestEditor)
64                 && ((ManifestEditor) formEditor).isEquinox()) {
65             createLazyStart(parent, toolkit, actionBars);
66         }
67     }
68
69     protected void addListeners() {
70         if (isBundle()) {
71             IBundleModel model = getBundle().getModel();
72             if (model != null)
73                 model.addModelChangedListener(this);
74         }
75         super.addListeners();
76     }
77
78     protected void removeListeners() {
79         if (isBundle()) {
80             IBundleModel model = getBundle().getModel();
81             if (model != null)
82                 model.removeModelChangedListener(this);
83         }
84         super.removeListeners();
85     }
86     
87     private void createLazyStart(Composite parent, FormToolkit toolkit, IActionBars actionBars) {
88         fLazyStart = toolkit.createButton(parent, PDEUIMessages.PluginGeneralInfoSection_lazyStart, SWT.CHECK);
89         TableWrapData td = new TableWrapData();
90         td.colspan = 3;
91         fLazyStart.setLayoutData(td);
92         fLazyStart.setEnabled(isEditable());
93         fLazyStart.addSelectionListener(new SelectionAdapter() {
94             public void widgetSelected(SelectionEvent e) {
95                 IManifestHeader header = getLazyStartHeader();
96                 if (header instanceof LazyStartHeader)
97                     ((LazyStartHeader)header).setLazyStart(fLazyStart.getSelection());
98                 else
99                     getBundle().setHeader(getLazyStartHeaderName(),
100                             Boolean.toString(fLazyStart.getSelection()));
101             }
102         });
103     }
104     
105     private void createClassEntry(Composite client, FormToolkit toolkit, IActionBars actionBars) {
106         boolean isEditable = isEditable();
107         fClassEntry = new FormEntry(
108                             client,
109                             toolkit,
110                             PDEUIMessages.GeneralInfoSection_class,
111                             PDEUIMessages.GeneralInfoSection_browse, //
112
isEditable());
113         fClassEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) {
114             public void textValueChanged(FormEntry entry) {
115                 try {
116                     ((IPlugin) getPluginBase()).setClassName(entry.getValue());
117                 } catch (CoreException e) {
118                     PDEPlugin.logException(e);
119                 }
120             }
121             public void linkActivated(HyperlinkEvent e) {
122                 String JavaDoc value = fClassEntry.getValue();
123                 IProject project = getPage().getPDEEditor().getCommonProject();
124                 value = PDEJavaHelperUI.createClass(value, project, createJavaAttributeValue(), false);
125                 if (value != null)
126                     fClassEntry.setValue(value);
127             }
128             public void browseButtonSelected(FormEntry entry) {
129                 doOpenSelectionDialog(entry.getValue());
130             }
131         });
132         fClassEntry.setEditable(isEditable);
133         
134         if (isEditable) {
135             fTypeFieldAssistDisposer = PDEJavaHelperUI.addTypeFieldAssistToText(
136                     fClassEntry.getText(),
137                     getProject(),
138                     IJavaSearchConstants.CLASS);
139         }
140     }
141     
142     private void doOpenSelectionDialog(String JavaDoc className) {
143         IResource resource = getPluginBase().getModel().getUnderlyingResource();
144         String JavaDoc type =
145             PDEJavaHelperUI.selectType(resource, IJavaElementSearchConstants.CONSIDER_CLASSES, className);
146         if (type != null)
147             fClassEntry.setValue(type);
148     }
149
150     private JavaAttributeValue createJavaAttributeValue() {
151         IProject project = getPage().getPDEEditor().getCommonProject();
152         IPluginModelBase model = (IPluginModelBase) getPage().getModel();
153         return new JavaAttributeValue(project, model, null, fClassEntry.getValue());
154     }
155     
156     public void cancelEdit() {
157         fClassEntry.cancelEdit();
158         super.cancelEdit();
159     }
160     
161     public void commit(boolean onSave) {
162         fClassEntry.commit();
163         super.commit(onSave);
164     }
165     
166     public void refresh() {
167         IPluginModelBase model = (IPluginModelBase) getPage().getModel();
168         // if we are refactoring, the Manifest moves before the editor closes. This could cause the model to be null on a refresh()
169
if (model == null)
170             return;
171         IPlugin plugin = (IPlugin)model.getPluginBase();
172         // Only update this field if it already has not been modified
173
// This will prevent the cursor from being set to position 0 after
174
// accepting a field assist proposal using \r
175
if (fClassEntry.isDirty() == false) {
176             fClassEntry.setValue(plugin.getClassName(), true);
177         }
178         if (fLazyStart != null) {
179             IManifestHeader header = getLazyStartHeader();
180             fLazyStart.setSelection(header instanceof LazyStartHeader
181                     && ((LazyStartHeader)header).isLazyStart());
182         }
183         super.refresh();
184     }
185     
186     private IManifestHeader getLazyStartHeader() {
187         IBundle bundle = getBundle();
188         if (bundle instanceof Bundle) {
189             IManifestHeader header = bundle.getManifestHeader(ICoreConstants.ECLIPSE_LAZYSTART);
190             if (header == null)
191                 header = bundle.getManifestHeader(ICoreConstants.ECLIPSE_AUTOSTART);
192             return header;
193         }
194         return null;
195     }
196     
197     private String JavaDoc getLazyStartHeaderName() {
198         if (TargetPlatformHelper.getTargetVersion() >= 3.2
199                 && BundlePluginBase.getBundleManifestVersion(getBundle()) >= 2)
200             return ICoreConstants.ECLIPSE_LAZYSTART;
201         return ICoreConstants.ECLIPSE_AUTOSTART;
202     }
203
204     /* (non-Javadoc)
205      * @see org.eclipse.pde.internal.ui.editor.plugin.GeneralInfoSection#dispose()
206      */

207     public void dispose() {
208         super.dispose();
209         if (fTypeFieldAssistDisposer != null) {
210             fTypeFieldAssistDisposer.dispose();
211         }
212     }
213     
214 }
215
Popular Tags