KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > feature > PluginDetailsSection


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.feature;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.pde.core.IModelChangedEvent;
17 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
18 import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
19 import org.eclipse.pde.internal.ui.PDEPlugin;
20 import org.eclipse.pde.internal.ui.PDEUIMessages;
21 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
22 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
23 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
24 import org.eclipse.pde.internal.ui.editor.PDESection;
25 import org.eclipse.pde.internal.ui.parts.FormEntry;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.ui.forms.IFormPart;
33 import org.eclipse.ui.forms.IManagedForm;
34 import org.eclipse.ui.forms.IPartSelectionListener;
35 import org.eclipse.ui.forms.widgets.FormToolkit;
36 import org.eclipse.ui.forms.widgets.Section;
37
38 public class PluginDetailsSection extends PDESection implements IFormPart,
39 IPartSelectionListener {
40     protected IFeaturePlugin fInput;
41
42     private FormEntry fNameText;
43     
44     private FormEntry fVersionText;
45
46     private FormEntry fdownloadSizeText;
47
48     private FormEntry fInstallSizeText;
49
50     private Button fUnpackButton;
51
52     private boolean fBlockNotification;
53
54     public PluginDetailsSection(PDEFormPage page, Composite parent) {
55         this(page, parent, PDEUIMessages.SiteEditor_PluginDetailsSection_title,
56                 PDEUIMessages.SiteEditor_PluginDetailsSection_desc, SWT.NULL);
57     }
58
59     public PluginDetailsSection(PDEFormPage page, Composite parent,
60             String JavaDoc title, String JavaDoc desc, int toggleStyle) {
61         super(page, parent, Section.DESCRIPTION | toggleStyle);
62         getSection().setText(title);
63         getSection().setDescription(desc);
64         createClient(getSection(), page.getManagedForm().getToolkit());
65     }
66
67     public void cancelEdit() {
68         fVersionText.cancelEdit();
69         fdownloadSizeText.cancelEdit();
70         fInstallSizeText.cancelEdit();
71         super.cancelEdit();
72     }
73
74     public void commit(boolean onSave) {
75         fVersionText.commit();
76         fdownloadSizeText.commit();
77         fInstallSizeText.commit();
78         super.commit(onSave);
79     }
80
81     public void createClient(Section section, FormToolkit toolkit) {
82
83         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
84         GridData data = new GridData(GridData.FILL_HORIZONTAL
85                 | GridData.VERTICAL_ALIGN_BEGINNING);
86         section.setLayoutData(data);
87
88         Composite container = toolkit.createComposite(section);
89         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
90         container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
91
92         fNameText = new FormEntry(container, toolkit, PDEUIMessages.SiteEditor_PluginDetailsSection_pluginLabel, null, false);
93         limitTextWidth(fNameText);
94         fNameText.setEditable(false);
95         fNameText.getText().setEnabled(false);
96         
97         fVersionText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_SpecSection_version, null, false);
98         fVersionText.setFormEntryListener(new FormEntryAdapter(this) {
99             public void textValueChanged(FormEntry text) {
100                 if (fInput != null)
101                     try {
102                         fInput.setVersion(text.getValue());
103                     } catch (CoreException e) {
104                         PDEPlugin.logException(e);
105                     }
106             }
107         });
108         limitTextWidth(fVersionText);
109         fVersionText.setEditable(isEditable());
110
111         fdownloadSizeText = new FormEntry(container, toolkit, PDEUIMessages.SiteEditor_PluginDetailsSection_downloadSize, null, false);
112         fdownloadSizeText.setFormEntryListener(new FormEntryAdapter(this) {
113
114             public void textValueChanged(FormEntry text) {
115                 if (fInput != null)
116                     try {
117                         fInput.setDownloadSize(getLong(text.getValue()));
118                     } catch (CoreException e) {
119                         PDEPlugin.logException(e);
120                     }
121             }
122         });
123         limitTextWidth(fdownloadSizeText);
124         fdownloadSizeText.setEditable(isEditable());
125
126         fInstallSizeText = new FormEntry(container, toolkit, PDEUIMessages.SiteEditor_PluginDetailsSection_installSize, null, false);
127         fInstallSizeText.setFormEntryListener(new FormEntryAdapter(this) {
128
129             public void textValueChanged(FormEntry text) {
130                 if (fInput != null)
131                     try {
132                         fInput.setInstallSize(getLong(text.getValue()));
133                     } catch (CoreException e) {
134                         PDEPlugin.logException(e);
135                     }
136             }
137         });
138         limitTextWidth(fInstallSizeText);
139         fInstallSizeText.setEditable(isEditable());
140
141         fUnpackButton = toolkit.createButton(container, PDEUIMessages.SiteEditor_PluginDetailsSection_unpack, SWT.CHECK);
142         GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
143         gd.horizontalSpan = 2;
144         fUnpackButton.setLayoutData(gd);
145         fUnpackButton.addSelectionListener(new SelectionAdapter() {
146             public void widgetSelected(SelectionEvent e) {
147                 try {
148                     if (!fBlockNotification)
149                         fInput.setUnpack(fUnpackButton.getSelection());
150                 } catch (CoreException ex) {
151                     PDEPlugin.logException(ex);
152                 }
153             }
154         });
155
156         toolkit.paintBordersFor(container);
157         section.setClient(container);
158     }
159
160     public void dispose() {
161         IFeatureModel model = (IFeatureModel) getPage().getModel();
162         if (model != null)
163             model.removeModelChangedListener(this);
164         super.dispose();
165     }
166
167     /*
168      * (non-Javadoc)
169      *
170      * @see org.eclipse.ui.forms.AbstractFormPart#initialize(org.eclipse.ui.forms.IManagedForm)
171      */

172     public void initialize(IManagedForm form) {
173         IFeatureModel model = (IFeatureModel) getPage().getModel();
174         if (model != null)
175             model.addModelChangedListener(this);
176         super.initialize(form);
177     }
178
179     protected void limitTextWidth(FormEntry entry) {
180         GridData gd = (GridData) entry.getText().getLayoutData();
181         gd.widthHint = 30;
182     }
183
184     public void modelChanged(IModelChangedEvent e) {
185         markStale();
186     }
187
188     public void refresh() {
189         update();
190         super.refresh();
191     }
192
193     public void selectionChanged(IFormPart part, ISelection selection) {
194         if (selection instanceof IStructuredSelection && ((IStructuredSelection) selection).size() == 1) {
195             IStructuredSelection s = ((IStructuredSelection) selection);
196             Object JavaDoc o = s.getFirstElement();
197             if (o instanceof IFeaturePlugin) {
198                 fInput = (IFeaturePlugin) o;
199             } else {
200                 fInput = null;
201             }
202         } else {
203             fInput = null;
204         }
205         update();
206     }
207
208     public void setFocus() {
209         if (fdownloadSizeText != null)
210             fdownloadSizeText.getText().setFocus();
211     }
212
213     private void update() {
214         if (fInput != null) {
215             fNameText.setValue(fInput.getLabel());
216             fVersionText.setValue(fInput.getVersion(), true);
217             fdownloadSizeText
218             .setValue(
219                     fInput.getDownloadSize() >= 0 ? "" + fInput.getDownloadSize() : null, true); //$NON-NLS-1$
220
fInstallSizeText
221             .setValue(
222                     fInput.getInstallSize() >= 0 ? "" + fInput.getInstallSize() : null, true); //$NON-NLS-1$
223
fBlockNotification = true;
224             fUnpackButton.setSelection(fInput.isUnpack());
225             fBlockNotification = false;
226
227         } else {
228             fNameText.setValue(null);
229             fVersionText.setValue(null, true);
230             fdownloadSizeText.setValue(null, true);
231             fInstallSizeText.setValue(null, true);
232             fBlockNotification = true;
233             fUnpackButton.setSelection(true);
234             fBlockNotification = false;
235         }
236         boolean editable = fInput != null && isEditable();
237         fVersionText.setEditable(editable);
238         fdownloadSizeText.setEditable(editable);
239         fInstallSizeText.setEditable(editable);
240         fUnpackButton.setEnabled(editable);
241     }
242
243     public boolean isEditable() {
244         return getPage().getPDEEditor().getAggregateModel().isEditable();
245     }
246
247     private long getLong(String JavaDoc svalue) {
248         if (svalue == null)
249             return 0;
250         try {
251             return Long.parseLong(svalue);
252         } catch (NumberFormatException JavaDoc e) {
253             return 0;
254         }
255     }
256 }
257
Popular Tags