001: /*******************************************************************************
002: * Copyright (c) 2000, 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: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.feature;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.jface.viewers.ISelection;
014: import org.eclipse.jface.viewers.IStructuredSelection;
015: import org.eclipse.pde.core.IModelChangedEvent;
016: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
017: import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
018: import org.eclipse.pde.internal.ui.PDEPlugin;
019: import org.eclipse.pde.internal.ui.PDEUIMessages;
020: import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
021: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
022: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
023: import org.eclipse.pde.internal.ui.editor.PDESection;
024: import org.eclipse.pde.internal.ui.parts.FormEntry;
025: import org.eclipse.swt.SWT;
026: import org.eclipse.swt.events.SelectionAdapter;
027: import org.eclipse.swt.events.SelectionEvent;
028: import org.eclipse.swt.layout.GridData;
029: import org.eclipse.swt.widgets.Button;
030: import org.eclipse.swt.widgets.Composite;
031: import org.eclipse.ui.forms.IFormPart;
032: import org.eclipse.ui.forms.IManagedForm;
033: import org.eclipse.ui.forms.IPartSelectionListener;
034: import org.eclipse.ui.forms.widgets.FormToolkit;
035: import org.eclipse.ui.forms.widgets.Section;
036:
037: public class PluginDetailsSection extends PDESection implements
038: IFormPart, IPartSelectionListener {
039: protected IFeaturePlugin fInput;
040:
041: private FormEntry fNameText;
042:
043: private FormEntry fVersionText;
044:
045: private FormEntry fdownloadSizeText;
046:
047: private FormEntry fInstallSizeText;
048:
049: private Button fUnpackButton;
050:
051: private boolean fBlockNotification;
052:
053: public PluginDetailsSection(PDEFormPage page, Composite parent) {
054: this (page, parent,
055: PDEUIMessages.SiteEditor_PluginDetailsSection_title,
056: PDEUIMessages.SiteEditor_PluginDetailsSection_desc,
057: SWT.NULL);
058: }
059:
060: public PluginDetailsSection(PDEFormPage page, Composite parent,
061: String title, String desc, int toggleStyle) {
062: super (page, parent, Section.DESCRIPTION | toggleStyle);
063: getSection().setText(title);
064: getSection().setDescription(desc);
065: createClient(getSection(), page.getManagedForm().getToolkit());
066: }
067:
068: public void cancelEdit() {
069: fVersionText.cancelEdit();
070: fdownloadSizeText.cancelEdit();
071: fInstallSizeText.cancelEdit();
072: super .cancelEdit();
073: }
074:
075: public void commit(boolean onSave) {
076: fVersionText.commit();
077: fdownloadSizeText.commit();
078: fInstallSizeText.commit();
079: super .commit(onSave);
080: }
081:
082: public void createClient(Section section, FormToolkit toolkit) {
083:
084: section.setLayout(FormLayoutFactory.createClearGridLayout(
085: false, 1));
086: GridData data = new GridData(GridData.FILL_HORIZONTAL
087: | GridData.VERTICAL_ALIGN_BEGINNING);
088: section.setLayoutData(data);
089:
090: Composite container = toolkit.createComposite(section);
091: container.setLayout(FormLayoutFactory
092: .createSectionClientGridLayout(false, 2));
093: container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
094:
095: fNameText = new FormEntry(
096: container,
097: toolkit,
098: PDEUIMessages.SiteEditor_PluginDetailsSection_pluginLabel,
099: null, false);
100: limitTextWidth(fNameText);
101: fNameText.setEditable(false);
102: fNameText.getText().setEnabled(false);
103:
104: fVersionText = new FormEntry(container, toolkit,
105: PDEUIMessages.FeatureEditor_SpecSection_version, null,
106: false);
107: fVersionText.setFormEntryListener(new FormEntryAdapter(this ) {
108: public void textValueChanged(FormEntry text) {
109: if (fInput != null)
110: try {
111: fInput.setVersion(text.getValue());
112: } catch (CoreException e) {
113: PDEPlugin.logException(e);
114: }
115: }
116: });
117: limitTextWidth(fVersionText);
118: fVersionText.setEditable(isEditable());
119:
120: fdownloadSizeText = new FormEntry(
121: container,
122: toolkit,
123: PDEUIMessages.SiteEditor_PluginDetailsSection_downloadSize,
124: null, false);
125: fdownloadSizeText.setFormEntryListener(new FormEntryAdapter(
126: this ) {
127:
128: public void textValueChanged(FormEntry text) {
129: if (fInput != null)
130: try {
131: fInput
132: .setDownloadSize(getLong(text
133: .getValue()));
134: } catch (CoreException e) {
135: PDEPlugin.logException(e);
136: }
137: }
138: });
139: limitTextWidth(fdownloadSizeText);
140: fdownloadSizeText.setEditable(isEditable());
141:
142: fInstallSizeText = new FormEntry(
143: container,
144: toolkit,
145: PDEUIMessages.SiteEditor_PluginDetailsSection_installSize,
146: null, false);
147: fInstallSizeText
148: .setFormEntryListener(new FormEntryAdapter(this ) {
149:
150: public void textValueChanged(FormEntry text) {
151: if (fInput != null)
152: try {
153: fInput.setInstallSize(getLong(text
154: .getValue()));
155: } catch (CoreException e) {
156: PDEPlugin.logException(e);
157: }
158: }
159: });
160: limitTextWidth(fInstallSizeText);
161: fInstallSizeText.setEditable(isEditable());
162:
163: fUnpackButton = toolkit.createButton(container,
164: PDEUIMessages.SiteEditor_PluginDetailsSection_unpack,
165: SWT.CHECK);
166: GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
167: gd.horizontalSpan = 2;
168: fUnpackButton.setLayoutData(gd);
169: fUnpackButton.addSelectionListener(new SelectionAdapter() {
170: public void widgetSelected(SelectionEvent e) {
171: try {
172: if (!fBlockNotification)
173: fInput.setUnpack(fUnpackButton.getSelection());
174: } catch (CoreException ex) {
175: PDEPlugin.logException(ex);
176: }
177: }
178: });
179:
180: toolkit.paintBordersFor(container);
181: section.setClient(container);
182: }
183:
184: public void dispose() {
185: IFeatureModel model = (IFeatureModel) getPage().getModel();
186: if (model != null)
187: model.removeModelChangedListener(this );
188: super .dispose();
189: }
190:
191: /*
192: * (non-Javadoc)
193: *
194: * @see org.eclipse.ui.forms.AbstractFormPart#initialize(org.eclipse.ui.forms.IManagedForm)
195: */
196: public void initialize(IManagedForm form) {
197: IFeatureModel model = (IFeatureModel) getPage().getModel();
198: if (model != null)
199: model.addModelChangedListener(this );
200: super .initialize(form);
201: }
202:
203: protected void limitTextWidth(FormEntry entry) {
204: GridData gd = (GridData) entry.getText().getLayoutData();
205: gd.widthHint = 30;
206: }
207:
208: public void modelChanged(IModelChangedEvent e) {
209: markStale();
210: }
211:
212: public void refresh() {
213: update();
214: super .refresh();
215: }
216:
217: public void selectionChanged(IFormPart part, ISelection selection) {
218: if (selection instanceof IStructuredSelection
219: && ((IStructuredSelection) selection).size() == 1) {
220: IStructuredSelection s = ((IStructuredSelection) selection);
221: Object o = s.getFirstElement();
222: if (o instanceof IFeaturePlugin) {
223: fInput = (IFeaturePlugin) o;
224: } else {
225: fInput = null;
226: }
227: } else {
228: fInput = null;
229: }
230: update();
231: }
232:
233: public void setFocus() {
234: if (fdownloadSizeText != null)
235: fdownloadSizeText.getText().setFocus();
236: }
237:
238: private void update() {
239: if (fInput != null) {
240: fNameText.setValue(fInput.getLabel());
241: fVersionText.setValue(fInput.getVersion(), true);
242: fdownloadSizeText
243: .setValue(
244: fInput.getDownloadSize() >= 0 ? "" + fInput.getDownloadSize() : null, true); //$NON-NLS-1$
245: fInstallSizeText
246: .setValue(
247: fInput.getInstallSize() >= 0 ? "" + fInput.getInstallSize() : null, true); //$NON-NLS-1$
248: fBlockNotification = true;
249: fUnpackButton.setSelection(fInput.isUnpack());
250: fBlockNotification = false;
251:
252: } else {
253: fNameText.setValue(null);
254: fVersionText.setValue(null, true);
255: fdownloadSizeText.setValue(null, true);
256: fInstallSizeText.setValue(null, true);
257: fBlockNotification = true;
258: fUnpackButton.setSelection(true);
259: fBlockNotification = false;
260: }
261: boolean editable = fInput != null && isEditable();
262: fVersionText.setEditable(editable);
263: fdownloadSizeText.setEditable(editable);
264: fInstallSizeText.setEditable(editable);
265: fUnpackButton.setEnabled(editable);
266: }
267:
268: public boolean isEditable() {
269: return getPage().getPDEEditor().getAggregateModel()
270: .isEditable();
271: }
272:
273: private long getLong(String svalue) {
274: if (svalue == null)
275: return 0;
276: try {
277: return Long.parseLong(svalue);
278: } catch (NumberFormatException e) {
279: return 0;
280: }
281: }
282: }
|