1 11 package org.eclipse.pde.internal.ui.editor.product; 12 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.jface.window.Window; 15 import org.eclipse.pde.core.IModelChangedEvent; 16 import org.eclipse.pde.internal.core.iproduct.IAboutInfo; 17 import org.eclipse.pde.internal.core.iproduct.IProduct; 18 import org.eclipse.pde.internal.core.iproduct.IProductModel; 19 import org.eclipse.pde.internal.ui.PDEPlugin; 20 import org.eclipse.pde.internal.ui.PDEUIMessages; 21 import org.eclipse.pde.internal.ui.editor.EditorUtilities; 22 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter; 23 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 24 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 25 import org.eclipse.pde.internal.ui.editor.PDESection; 26 import org.eclipse.pde.internal.ui.editor.validation.TextValidator; 27 import org.eclipse.pde.internal.ui.parts.FormEntry; 28 import org.eclipse.pde.internal.ui.util.FileExtensionFilter; 29 import org.eclipse.pde.internal.ui.util.FileValidator; 30 import org.eclipse.swt.SWT; 31 import org.eclipse.swt.dnd.Clipboard; 32 import org.eclipse.swt.layout.GridData; 33 import org.eclipse.swt.widgets.Composite; 34 import org.eclipse.swt.widgets.Control; 35 import org.eclipse.swt.widgets.Display; 36 import org.eclipse.swt.widgets.Text; 37 import org.eclipse.ui.IActionBars; 38 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 39 import org.eclipse.ui.forms.events.HyperlinkEvent; 40 import org.eclipse.ui.forms.widgets.FormToolkit; 41 import org.eclipse.ui.forms.widgets.Section; 42 import org.eclipse.ui.model.WorkbenchContentProvider; 43 import org.eclipse.ui.model.WorkbenchLabelProvider; 44 45 46 public class AboutSection extends PDESection { 47 48 private FormEntry fImageEntry; 49 private FormEntry fTextEntry; 50 51 private TextValidator fImageEntryValidator; 52 53 public AboutSection(PDEFormPage page, Composite parent) { 54 super(page, parent, Section.DESCRIPTION); 55 createClient(getSection(), page.getEditor().getToolkit()); 56 } 57 58 61 protected void createClient(Section section, FormToolkit toolkit) { 62 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 63 GridData data = new GridData(GridData.FILL_BOTH); 64 data.widthHint = 300; 65 data.heightHint = 70; 66 section.setLayoutData(data); 67 68 section.setText(PDEUIMessages.AboutSection_title); 69 section.setDescription(PDEUIMessages.AboutSection_desc); 70 71 Composite client = toolkit.createComposite(section); 72 client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 3)); 73 client.setLayoutData(new GridData(GridData.FILL_BOTH)); 74 75 IActionBars actionBars = getPage().getPDEEditor().getEditorSite().getActionBars(); 76 fImageEntry = new FormEntry(client, toolkit, PDEUIMessages.AboutSection_image, PDEUIMessages.AboutSection_browse, isEditable()); 77 fImageEntry.setEditable(isEditable()); 78 fImageEntryValidator = new TextValidator( 80 getManagedForm(), fImageEntry.getText(), getProject(), true) { 81 protected boolean validateControl() { 82 return validateImageEntry(); 83 } 84 }; 85 fImageEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) { 86 public void textValueChanged(FormEntry entry) { 87 getAboutInfo().setImagePath(entry.getValue()); 88 } 89 public void browseButtonSelected(FormEntry entry) { 90 handleBrowse(); 91 } 92 public void linkActivated(HyperlinkEvent e) { 93 EditorUtilities.openImage(fImageEntry.getValue(), getProduct().getDefiningPluginId()); 94 } 95 }); 96 97 int style = SWT.MULTI | SWT.WRAP | SWT.V_SCROLL; 100 fTextEntry = new FormEntry( 101 client, 102 toolkit, 103 PDEUIMessages.AboutSection_text, 104 style); 105 data = new GridData(GridData.FILL_BOTH); 107 data.horizontalSpan = 2; 108 data.horizontalIndent = FormLayoutFactory.CONTROL_HORIZONTAL_INDENT; 111 fTextEntry.getText().setLayoutData(data); 112 data = new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false); 114 fTextEntry.getLabel().setLayoutData(data); 115 fTextEntry.setEditable(isEditable()); 117 fTextEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) { 119 public void textValueChanged(FormEntry entry) { 120 getAboutInfo().setText(entry.getValue()); 121 } 122 }); 123 124 toolkit.paintBordersFor(client); 125 section.setClient(client); 126 getModel().addModelChangedListener(this); 128 } 129 130 133 private boolean validateImageEntry() { 134 return EditorUtilities.imageEntrySizeDoesNotExceed( 135 fImageEntryValidator, fImageEntry, getProduct(), 136 500, 330, 250, 330); 137 } 138 139 private void handleBrowse() { 140 ElementTreeSelectionDialog dialog = 141 new ElementTreeSelectionDialog( 142 getSection().getShell(), 143 new WorkbenchLabelProvider(), 144 new WorkbenchContentProvider()); 145 146 dialog.setValidator(new FileValidator()); 147 dialog.setAllowMultiple(false); 148 dialog.setTitle(PDEUIMessages.AboutSection_imgTitle); 149 dialog.setMessage(PDEUIMessages.AboutSection_imgMessage); 150 dialog.addFilter(new FileExtensionFilter("gif")); dialog.setInput(PDEPlugin.getWorkspace().getRoot()); 152 153 if (dialog.open() == Window.OK) { 154 IFile file = (IFile)dialog.getFirstResult(); 155 fImageEntry.setValue(file.getFullPath().toString()); 156 } 157 } 158 159 public void refresh() { 160 fImageEntry.setValue(getAboutInfo().getImagePath(), true); 161 fTextEntry.setValue(getAboutInfo().getText(), true); 162 super.refresh(); 163 } 164 165 public void commit(boolean onSave) { 166 fImageEntry.commit(); 167 fTextEntry.commit(); 168 super.commit(onSave); 169 } 170 171 public void cancelEdit() { 172 fImageEntry.cancelEdit(); 173 fTextEntry.cancelEdit(); 174 super.cancelEdit(); 175 } 176 177 private IAboutInfo getAboutInfo() { 178 IAboutInfo info = getProduct().getAboutInfo(); 179 if (info == null) { 180 info = getModel().getFactory().createAboutInfo(); 181 getProduct().setAboutInfo(info); 182 } 183 return info; 184 } 185 186 private IProduct getProduct() { 187 return getModel().getProduct(); 188 } 189 190 private IProductModel getModel() { 191 return (IProductModel)getPage().getPDEEditor().getAggregateModel(); 192 } 193 194 public boolean canPaste(Clipboard clipboard) { 195 Display d = getSection().getDisplay(); 196 Control c = d.getFocusControl(); 197 if (c instanceof Text) 198 return true; 199 return false; 200 } 201 202 205 public void modelChanged(IModelChangedEvent e) { 206 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 208 handleModelEventWorldChanged(e); 209 } 210 } 211 212 215 private void handleModelEventWorldChanged(IModelChangedEvent event) { 216 refresh(); 217 } 218 219 222 public void dispose() { 223 IProductModel model = getModel(); 224 if (model != null) { 225 model.removeModelChangedListener(this); 226 } 227 super.dispose(); 228 } 229 } 230 | Popular Tags |