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.site;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.jface.dialogs.MessageDialog;
014: import org.eclipse.jface.viewers.ISelection;
015: import org.eclipse.jface.viewers.IStructuredSelection;
016: import org.eclipse.pde.core.IModelChangedEvent;
017: import org.eclipse.pde.internal.core.isite.ISiteFeature;
018: import org.eclipse.pde.internal.core.isite.ISiteModel;
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.dnd.Clipboard;
028: import org.eclipse.swt.dnd.RTFTransfer;
029: import org.eclipse.swt.dnd.TextTransfer;
030: import org.eclipse.swt.dnd.Transfer;
031: import org.eclipse.swt.dnd.TransferData;
032: import org.eclipse.swt.events.SelectionAdapter;
033: import org.eclipse.swt.events.SelectionEvent;
034: import org.eclipse.swt.layout.GridData;
035: import org.eclipse.swt.widgets.Button;
036: import org.eclipse.swt.widgets.Composite;
037: import org.eclipse.ui.forms.IFormPart;
038: import org.eclipse.ui.forms.IPartSelectionListener;
039: import org.eclipse.ui.forms.widgets.FormToolkit;
040: import org.eclipse.ui.forms.widgets.Section;
041:
042: public class FeatureDetailsSection extends PDESection implements
043: IFormPart, IPartSelectionListener {
044:
045: private static final String PROPERTY_TYPE = "type"; //$NON-NLS-1$
046:
047: private static final String PROPERTY_URL = "url"; //$NON-NLS-1$
048:
049: private ISiteFeature fCurrentSiteFeature;
050:
051: private Button fPatchCheckBox;
052:
053: private FormEntry fUrlText;
054:
055: public FeatureDetailsSection(PDEFormPage page, Composite parent) {
056: this (page, parent, PDEUIMessages.FeatureDetailsSection_title,
057: PDEUIMessages.FeatureDetailsSection_desc, SWT.NULL);
058: }
059:
060: public FeatureDetailsSection(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: private void applyIsPatch(boolean patch) throws CoreException {
069: if (fCurrentSiteFeature == null)
070: return;
071: fCurrentSiteFeature.setIsPatch(patch);
072: }
073:
074: private void applyValue(String property, String value)
075: throws CoreException {
076: if (fCurrentSiteFeature == null)
077: return;
078: if (property.equals(PROPERTY_URL))
079: fCurrentSiteFeature.setURL(value);
080: else if (property.equals(PROPERTY_TYPE))
081: fCurrentSiteFeature.setType(value);
082: }
083:
084: public void cancelEdit() {
085: fUrlText.cancelEdit();
086: super .cancelEdit();
087: }
088:
089: public boolean canPaste(Clipboard clipboard) {
090: TransferData[] types = clipboard.getAvailableTypes();
091: Transfer[] transfers = new Transfer[] {
092: TextTransfer.getInstance(), RTFTransfer.getInstance() };
093: for (int i = 0; i < types.length; i++) {
094: for (int j = 0; j < transfers.length; j++) {
095: if (transfers[j].isSupportedType(types[i]))
096: return true;
097: }
098: }
099: return false;
100: }
101:
102: private void clearField(String property) {
103: if (property.equals(PROPERTY_URL))
104: fUrlText.setValue(null, true);
105: }
106:
107: private void clearFields() {
108: fUrlText.setValue(null, true);
109: fPatchCheckBox.setSelection(false);
110: }
111:
112: public void commit(boolean onSave) {
113: try {
114: applyIsPatch(fPatchCheckBox.getSelection());
115: } catch (CoreException e) {
116: PDEPlugin.logException(e);
117: }
118:
119: super .commit(onSave);
120: }
121:
122: public void createClient(Section section, FormToolkit toolkit) {
123:
124: section.setLayout(FormLayoutFactory.createClearGridLayout(
125: false, 1));
126: Composite container = toolkit.createComposite(section);
127: container.setLayout(FormLayoutFactory
128: .createSectionClientGridLayout(false, 2));
129: container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
130:
131: GridData data = new GridData(GridData.FILL_HORIZONTAL);
132: section.setLayoutData(data);
133:
134: fUrlText = new FormEntry(container, toolkit,
135: PDEUIMessages.FeatureDetailsSection_url, null, false);
136: fUrlText.setFormEntryListener(new FormEntryAdapter(this ) {
137: public void textValueChanged(FormEntry text) {
138: try {
139: if (text.getValue().length() <= 0) {
140: setValue(PROPERTY_URL);
141: MessageDialog
142: .openError(
143: PDEPlugin
144: .getActiveWorkbenchShell(),
145: PDEUIMessages.FeatureDetailsSection_requiredURL_title,
146: PDEUIMessages.FeatureDetailsSection_requiredURL);
147: } else {
148: applyValue(PROPERTY_URL, text.getValue());
149: }
150: } catch (CoreException e) {
151: PDEPlugin.logException(e);
152: }
153: }
154: });
155: limitTextWidth(fUrlText);
156: fUrlText.getText().setEnabled(false);
157:
158: createPatchButton(toolkit, container);
159:
160: toolkit.paintBordersFor(container);
161: section.setClient(container);
162:
163: ISiteModel model = (ISiteModel) getPage().getModel();
164: if (model != null)
165: model.addModelChangedListener(this );
166: }
167:
168: private void createPatchButton(FormToolkit toolkit,
169: Composite container) {
170: fPatchCheckBox = toolkit.createButton(container,
171: PDEUIMessages.FeatureDetailsSection_patch, SWT.CHECK);
172: fPatchCheckBox.addSelectionListener(new SelectionAdapter() {
173: public void widgetSelected(SelectionEvent e) {
174: try {
175: applyIsPatch(fPatchCheckBox.getSelection());
176: } catch (CoreException ce) {
177: PDEPlugin.logException(ce);
178: }
179: }
180: });
181: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
182: gd.horizontalSpan = 2;
183: fPatchCheckBox.setLayoutData(gd);
184: fPatchCheckBox.setEnabled(isEditable());
185: }
186:
187: public void dispose() {
188: ISiteModel model = (ISiteModel) getPage().getModel();
189: if (model != null)
190: model.removeModelChangedListener(this );
191: super .dispose();
192: }
193:
194: private void limitTextWidth(FormEntry entry) {
195: GridData gd = (GridData) entry.getText().getLayoutData();
196: gd.widthHint = 30;
197: }
198:
199: public void modelChanged(IModelChangedEvent e) {
200: markStale();
201: }
202:
203: public void refresh() {
204: if (fCurrentSiteFeature == null) {
205: clearFields();
206: super .refresh();
207: return;
208: }
209: setValue(PROPERTY_URL);
210: setValue(PROPERTY_TYPE);
211: fPatchCheckBox.setSelection(fCurrentSiteFeature.isPatch());
212: super .refresh();
213: }
214:
215: public void selectionChanged(IFormPart part, ISelection selection) {
216: if (selection instanceof IStructuredSelection
217: && !selection.isEmpty()) {
218: Object o = ((IStructuredSelection) selection)
219: .getFirstElement();
220: if (o instanceof SiteFeatureAdapter) {
221: fCurrentSiteFeature = ((SiteFeatureAdapter) o).feature;
222: } else {
223: fCurrentSiteFeature = null;
224: }
225: } else
226: fCurrentSiteFeature = null;
227: refresh();
228: }
229:
230: public void setFocus() {
231: if (fUrlText != null)
232: fUrlText.getText().setFocus();
233: }
234:
235: private void setValue(String property) {
236: if (fCurrentSiteFeature == null) {
237: clearField(property);
238: } else {
239: if (property.equals(PROPERTY_URL))
240: fUrlText.setValue(fCurrentSiteFeature.getURL(), true);
241: }
242: }
243: }
|