1 11 package org.eclipse.pde.internal.ui.editor.site; 12 import org.eclipse.core.runtime.CoreException; 13 import org.eclipse.pde.core.IModelChangedEvent; 14 import org.eclipse.pde.internal.core.isite.ISite; 15 import org.eclipse.pde.internal.core.isite.ISiteDescription; 16 import org.eclipse.pde.internal.core.isite.ISiteModel; 17 import org.eclipse.pde.internal.ui.PDEPlugin; 18 import org.eclipse.pde.internal.ui.PDEUIMessages; 19 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter; 20 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 21 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 22 import org.eclipse.pde.internal.ui.editor.PDESection; 23 import org.eclipse.pde.internal.ui.parts.FormEntry; 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.dnd.Clipboard; 26 import org.eclipse.swt.dnd.RTFTransfer; 27 import org.eclipse.swt.dnd.TextTransfer; 28 import org.eclipse.swt.dnd.Transfer; 29 import org.eclipse.swt.dnd.TransferData; 30 import org.eclipse.swt.layout.GridData; 31 import org.eclipse.swt.widgets.Composite; 32 import org.eclipse.ui.forms.widgets.FormToolkit; 33 import org.eclipse.ui.forms.widgets.Section; 34 35 38 public class DescriptionSection extends PDESection { 39 private FormEntry fURLEntry; 40 private FormEntry fDescEntry; 41 public DescriptionSection(PDEFormPage page, Composite parent) { 42 super(page, parent, Section.DESCRIPTION); 43 getSection() 44 .setText( 45 PDEUIMessages.SiteEditor_DescriptionSection_header); 46 getSection() 47 .setDescription( 48 PDEUIMessages.SiteEditor_DescriptionSection_desc); 49 createClient(getSection(), page.getManagedForm().getToolkit()); 50 } 51 public void commit(boolean onSave) { 52 fURLEntry.commit(); 53 fDescEntry.commit(); 54 super.commit(onSave); 55 } 56 public void createClient(Section section, FormToolkit toolkit) { 57 58 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 59 Composite container = toolkit.createComposite(section); 60 container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2)); 61 container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 62 63 GridData data = new GridData(GridData.FILL_BOTH); 64 section.setLayoutData(data); 65 66 fURLEntry = new FormEntry(container, toolkit, PDEUIMessages.SiteEditor_DescriptionSection_urlLabel, 67 null, false); 68 fURLEntry.setFormEntryListener(new FormEntryAdapter(this) { 69 public void textValueChanged(FormEntry text) { 70 setDescriptionURL(text.getValue()); 71 } 72 }); 73 fURLEntry.setEditable(isEditable()); 74 75 fDescEntry = new FormEntry(container, toolkit, PDEUIMessages.SiteEditor_DescriptionSection_descLabel, 76 SWT.WRAP | SWT.MULTI); 77 GridData gd = new GridData(GridData.FILL_BOTH); 78 gd.widthHint = 200; 79 gd.heightHint = 64; 80 fDescEntry.getText().setLayoutData(gd); 81 fDescEntry.getLabel().setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING)); 82 fDescEntry.setFormEntryListener(new FormEntryAdapter(this) { 83 public void textValueChanged(FormEntry text) { 84 setDescriptionText(text.getValue()); 85 } 86 }); 87 fDescEntry.setEditable(isEditable()); 88 89 toolkit.paintBordersFor(container); 90 section.setClient(container); 91 initialize(); 92 } 93 private void setDescriptionURL(String text) { 94 ISiteModel model = (ISiteModel) getPage().getModel(); 95 ISite site = model.getSite(); 96 ISiteDescription description = site.getDescription(); 97 boolean defined = false; 98 if (description == null) { 99 description = model.getFactory().createDescription(null); 100 defined = true; 101 } 102 try { 103 description.setURL(text); 104 if (defined) { 105 site.setDescription(description); 106 } 107 } catch (CoreException e) { 108 PDEPlugin.logException(e); 109 } 110 } 111 private void setDescriptionText(String text) { 112 ISiteModel model = (ISiteModel) getPage().getModel(); 113 ISite site = model.getSite(); 114 ISiteDescription description = site.getDescription(); 115 boolean defined = false; 116 if (description == null) { 117 description = model.getFactory().createDescription(null); 118 defined = true; 119 } 120 try { 121 description.setText(text); 122 if (defined) { 123 site.setDescription(description); 124 } 125 } catch (CoreException e) { 126 PDEPlugin.logException(e); 127 } 128 } 129 public void dispose() { 130 ISiteModel model = (ISiteModel) getPage().getModel(); 131 if (model!=null) 132 model.removeModelChangedListener(this); 133 super.dispose(); 134 } 135 public void initialize() { 136 ISiteModel model = (ISiteModel) getPage().getModel(); 137 refresh(); 138 model.addModelChangedListener(this); 139 } 140 public void modelChanged(IModelChangedEvent e) { 141 markStale(); 142 } 143 public void setFocus() { 144 if (fURLEntry != null) 145 fURLEntry.getText().setFocus(); 146 } 147 private void setIfDefined(FormEntry formText, String value) { 148 if (value != null) { 149 formText.setValue(value, true); 150 } 151 } 152 public void refresh() { 153 ISiteModel model = (ISiteModel) getPage().getModel(); 154 ISite site = model.getSite(); 155 setIfDefined(fURLEntry, site.getDescription() != null ? site 156 .getDescription().getURL() : null); 157 setIfDefined(fDescEntry, site.getDescription() != null ? site 158 .getDescription().getText() : null); 159 super.refresh(); 160 } 161 public void cancelEdit() { 162 fURLEntry.cancelEdit(); 163 fDescEntry.cancelEdit(); 164 super.cancelEdit(); 165 } 166 169 public boolean canPaste(Clipboard clipboard) { 170 TransferData[] types = clipboard.getAvailableTypes(); 171 Transfer[] transfers = new Transfer[]{TextTransfer.getInstance(), 172 RTFTransfer.getInstance()}; 173 for (int i = 0; i < types.length; i++) { 174 for (int j = 0; j < transfers.length; j++) { 175 if (transfers[j].isSupportedType(types[i])) 176 return true; 177 } 178 } 179 return false; 180 } 181 } 182 | Popular Tags |