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.IFeatureData;
017: import org.eclipse.pde.internal.core.ifeature.IFeatureEntry;
018: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
019: import org.eclipse.pde.internal.ui.PDEPlugin;
020: import org.eclipse.pde.internal.ui.PDEUIMessages;
021: import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
022: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
023: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
024: import org.eclipse.pde.internal.ui.editor.PDESection;
025: import org.eclipse.pde.internal.ui.parts.FormEntry;
026: import org.eclipse.swt.SWT;
027: import org.eclipse.swt.layout.GridData;
028: import org.eclipse.swt.widgets.Composite;
029: import org.eclipse.ui.forms.IFormPart;
030: import org.eclipse.ui.forms.IManagedForm;
031: import org.eclipse.ui.forms.IPartSelectionListener;
032: import org.eclipse.ui.forms.widgets.FormToolkit;
033: import org.eclipse.ui.forms.widgets.Section;
034:
035: public class DataDetailsSection extends PDESection implements
036: IFormPart, IPartSelectionListener {
037: protected IFeatureEntry fInput;
038:
039: private FormEntry fdownloadSizeText;
040:
041: private FormEntry fInstallSizeText;
042:
043: public DataDetailsSection(PDEFormPage page, Composite parent) {
044: this (page, parent,
045: PDEUIMessages.SiteEditor_DataDetailsSection_title,
046: PDEUIMessages.SiteEditor_DataDetailsSection_desc,
047: SWT.NULL);
048: }
049:
050: public DataDetailsSection(PDEFormPage page, Composite parent,
051: String title, String desc, int toggleStyle) {
052: super (page, parent, Section.DESCRIPTION | toggleStyle);
053: getSection().setText(title);
054: getSection().setDescription(desc);
055: createClient(getSection(), page.getManagedForm().getToolkit());
056: }
057:
058: public void cancelEdit() {
059: fdownloadSizeText.cancelEdit();
060: fInstallSizeText.cancelEdit();
061: super .cancelEdit();
062: }
063:
064: public void commit(boolean onSave) {
065: fdownloadSizeText.commit();
066: fInstallSizeText.commit();
067: super .commit(onSave);
068: }
069:
070: public void createClient(Section section, FormToolkit toolkit) {
071:
072: section.setLayout(FormLayoutFactory.createClearGridLayout(
073: false, 1));
074: GridData data = new GridData(GridData.FILL_BOTH);
075: section.setLayoutData(data);
076:
077: Composite container = toolkit.createComposite(section);
078: container.setLayout(FormLayoutFactory
079: .createSectionClientGridLayout(false, 2));
080: container.setLayoutData(new GridData(GridData.FILL_BOTH));
081:
082: fdownloadSizeText = new FormEntry(
083: container,
084: toolkit,
085: PDEUIMessages.SiteEditor_DataDetailsSection_downloadSize,
086: null, false);
087: fdownloadSizeText.setFormEntryListener(new FormEntryAdapter(
088: this ) {
089:
090: public void textValueChanged(FormEntry text) {
091: if (fInput != null)
092: try {
093: fInput
094: .setDownloadSize(getLong(text
095: .getValue()));
096: } catch (CoreException e) {
097: PDEPlugin.logException(e);
098: }
099: }
100: });
101: limitTextWidth(fdownloadSizeText);
102: fdownloadSizeText.setEditable(isEditable());
103:
104: fInstallSizeText = new FormEntry(
105: container,
106: toolkit,
107: PDEUIMessages.SiteEditor_DataDetailsSection_installSize,
108: null, false);
109: fInstallSizeText
110: .setFormEntryListener(new FormEntryAdapter(this ) {
111:
112: public void textValueChanged(FormEntry text) {
113: if (fInput != null)
114: try {
115: fInput.setInstallSize(getLong(text
116: .getValue()));
117: } catch (CoreException e) {
118: PDEPlugin.logException(e);
119: }
120: }
121: });
122: limitTextWidth(fInstallSizeText);
123: fInstallSizeText.setEditable(isEditable());
124:
125: toolkit.paintBordersFor(container);
126: section.setClient(container);
127: }
128:
129: public void dispose() {
130: IFeatureModel model = (IFeatureModel) getPage().getModel();
131: if (model != null)
132: model.removeModelChangedListener(this );
133: super .dispose();
134: }
135:
136: /*
137: * (non-Javadoc)
138: *
139: * @see org.eclipse.ui.forms.AbstractFormPart#initialize(org.eclipse.ui.forms.IManagedForm)
140: */
141: public void initialize(IManagedForm form) {
142: IFeatureModel model = (IFeatureModel) getPage().getModel();
143: if (model != null)
144: model.addModelChangedListener(this );
145: super .initialize(form);
146: }
147:
148: protected void limitTextWidth(FormEntry entry) {
149: GridData gd = (GridData) entry.getText().getLayoutData();
150: gd.widthHint = 30;
151: }
152:
153: public void modelChanged(IModelChangedEvent e) {
154: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
155: markStale();
156: } else if (e.getChangeType() == IModelChangedEvent.CHANGE
157: && e.getChangedObjects().length > 0
158: && e.getChangedObjects()[0] instanceof IFeatureData
159: && e.getChangedObjects()[0] == fInput) {
160: markStale();
161: }
162: }
163:
164: public void refresh() {
165: update();
166: super .refresh();
167: }
168:
169: public void selectionChanged(IFormPart part, ISelection selection) {
170: if (selection instanceof IStructuredSelection
171: && !selection.isEmpty()) {
172: Object o = ((IStructuredSelection) selection)
173: .getFirstElement();
174: if (o instanceof IFeatureData) {
175: fInput = (IFeatureData) o;
176: } else {
177: fInput = null;
178: }
179: } else
180: fInput = null;
181: update();
182: }
183:
184: public void setFocus() {
185: if (fdownloadSizeText != null)
186: fdownloadSizeText.getText().setFocus();
187: }
188:
189: private void update() {
190: if (fInput != null) {
191: fdownloadSizeText
192: .setValue(
193: fInput.getDownloadSize() >= 0 ? "" + fInput.getDownloadSize() : null, true); //$NON-NLS-1$
194: fInstallSizeText
195: .setValue(
196: fInput.getInstallSize() >= 0 ? "" + fInput.getInstallSize() : null, true); //$NON-NLS-1$
197: } else {
198: fdownloadSizeText.setValue(null, true);
199: fInstallSizeText.setValue(null, true);
200: }
201: fdownloadSizeText.setEditable(fInput != null && isEditable());
202: fInstallSizeText.setEditable(fInput != null && isEditable());
203: }
204:
205: public boolean isEditable() {
206: return getPage().getPDEEditor().getAggregateModel()
207: .isEditable();
208: }
209:
210: private long getLong(String svalue) {
211: if (svalue == null)
212: return 0;
213: try {
214: return Long.parseLong(svalue);
215: } catch (NumberFormatException e) {
216: return 0;
217: }
218: }
219: }
|