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.pde.core.IModelChangedEvent;
014: import org.eclipse.pde.internal.core.isite.ISite;
015: import org.eclipse.pde.internal.core.isite.ISiteDescription;
016: import org.eclipse.pde.internal.core.isite.ISiteModel;
017: import org.eclipse.pde.internal.ui.PDEPlugin;
018: import org.eclipse.pde.internal.ui.PDEUIMessages;
019: import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
020: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
021: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
022: import org.eclipse.pde.internal.ui.editor.PDESection;
023: import org.eclipse.pde.internal.ui.parts.FormEntry;
024: import org.eclipse.swt.SWT;
025: import org.eclipse.swt.dnd.Clipboard;
026: import org.eclipse.swt.dnd.RTFTransfer;
027: import org.eclipse.swt.dnd.TextTransfer;
028: import org.eclipse.swt.dnd.Transfer;
029: import org.eclipse.swt.dnd.TransferData;
030: import org.eclipse.swt.layout.GridData;
031: import org.eclipse.swt.widgets.Composite;
032: import org.eclipse.ui.forms.widgets.FormToolkit;
033: import org.eclipse.ui.forms.widgets.Section;
034:
035: /**
036: *
037: */
038: public class DescriptionSection extends PDESection {
039: private FormEntry fURLEntry;
040: private FormEntry fDescEntry;
041:
042: public DescriptionSection(PDEFormPage page, Composite parent) {
043: super (page, parent, Section.DESCRIPTION);
044: getSection().setText(
045: PDEUIMessages.SiteEditor_DescriptionSection_header);
046: getSection().setDescription(
047: PDEUIMessages.SiteEditor_DescriptionSection_desc);
048: createClient(getSection(), page.getManagedForm().getToolkit());
049: }
050:
051: public void commit(boolean onSave) {
052: fURLEntry.commit();
053: fDescEntry.commit();
054: super .commit(onSave);
055: }
056:
057: public void createClient(Section section, FormToolkit toolkit) {
058:
059: section.setLayout(FormLayoutFactory.createClearGridLayout(
060: false, 1));
061: Composite container = toolkit.createComposite(section);
062: container.setLayout(FormLayoutFactory
063: .createSectionClientGridLayout(false, 2));
064: container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
065:
066: GridData data = new GridData(GridData.FILL_BOTH);
067: section.setLayoutData(data);
068:
069: fURLEntry = new FormEntry(container, toolkit,
070: PDEUIMessages.SiteEditor_DescriptionSection_urlLabel,
071: null, false);
072: fURLEntry.setFormEntryListener(new FormEntryAdapter(this ) {
073: public void textValueChanged(FormEntry text) {
074: setDescriptionURL(text.getValue());
075: }
076: });
077: fURLEntry.setEditable(isEditable());
078:
079: fDescEntry = new FormEntry(container, toolkit,
080: PDEUIMessages.SiteEditor_DescriptionSection_descLabel,
081: SWT.WRAP | SWT.MULTI);
082: GridData gd = new GridData(GridData.FILL_BOTH);
083: gd.widthHint = 200;
084: gd.heightHint = 64;
085: fDescEntry.getText().setLayoutData(gd);
086: fDescEntry.getLabel().setLayoutData(
087: new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
088: fDescEntry.setFormEntryListener(new FormEntryAdapter(this ) {
089: public void textValueChanged(FormEntry text) {
090: setDescriptionText(text.getValue());
091: }
092: });
093: fDescEntry.setEditable(isEditable());
094:
095: toolkit.paintBordersFor(container);
096: section.setClient(container);
097: initialize();
098: }
099:
100: private void setDescriptionURL(String text) {
101: ISiteModel model = (ISiteModel) getPage().getModel();
102: ISite site = model.getSite();
103: ISiteDescription description = site.getDescription();
104: boolean defined = false;
105: if (description == null) {
106: description = model.getFactory().createDescription(null);
107: defined = true;
108: }
109: try {
110: description.setURL(text);
111: if (defined) {
112: site.setDescription(description);
113: }
114: } catch (CoreException e) {
115: PDEPlugin.logException(e);
116: }
117: }
118:
119: private void setDescriptionText(String text) {
120: ISiteModel model = (ISiteModel) getPage().getModel();
121: ISite site = model.getSite();
122: ISiteDescription description = site.getDescription();
123: boolean defined = false;
124: if (description == null) {
125: description = model.getFactory().createDescription(null);
126: defined = true;
127: }
128: try {
129: description.setText(text);
130: if (defined) {
131: site.setDescription(description);
132: }
133: } catch (CoreException e) {
134: PDEPlugin.logException(e);
135: }
136: }
137:
138: public void dispose() {
139: ISiteModel model = (ISiteModel) getPage().getModel();
140: if (model != null)
141: model.removeModelChangedListener(this );
142: super .dispose();
143: }
144:
145: public void initialize() {
146: ISiteModel model = (ISiteModel) getPage().getModel();
147: refresh();
148: model.addModelChangedListener(this );
149: }
150:
151: public void modelChanged(IModelChangedEvent e) {
152: markStale();
153: }
154:
155: public void setFocus() {
156: if (fURLEntry != null)
157: fURLEntry.getText().setFocus();
158: }
159:
160: private void setIfDefined(FormEntry formText, String value) {
161: if (value != null) {
162: formText.setValue(value, true);
163: }
164: }
165:
166: public void refresh() {
167: ISiteModel model = (ISiteModel) getPage().getModel();
168: ISite site = model.getSite();
169: setIfDefined(fURLEntry, site.getDescription() != null ? site
170: .getDescription().getURL() : null);
171: setIfDefined(fDescEntry, site.getDescription() != null ? site
172: .getDescription().getText() : null);
173: super .refresh();
174: }
175:
176: public void cancelEdit() {
177: fURLEntry.cancelEdit();
178: fDescEntry.cancelEdit();
179: super .cancelEdit();
180: }
181:
182: /**
183: * @see org.eclipse.update.ui.forms.internal.FormSection#canPaste(Clipboard)
184: */
185: public boolean canPaste(Clipboard clipboard) {
186: TransferData[] types = clipboard.getAvailableTypes();
187: Transfer[] transfers = new Transfer[] {
188: TextTransfer.getInstance(), RTFTransfer.getInstance() };
189: for (int i = 0; i < types.length; i++) {
190: for (int j = 0; j < transfers.length; j++) {
191: if (transfers[j].isSupportedType(types[i]))
192: return true;
193: }
194: }
195: return false;
196: }
197: }
|