1 11 package org.eclipse.pde.internal.ui.editor.feature; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.jface.viewers.ISelection; 15 import org.eclipse.jface.viewers.IStructuredSelection; 16 import org.eclipse.pde.core.IModelChangedEvent; 17 import org.eclipse.pde.internal.core.ifeature.IFeatureData; 18 import org.eclipse.pde.internal.core.ifeature.IFeatureEntry; 19 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 20 import org.eclipse.pde.internal.ui.PDEPlugin; 21 import org.eclipse.pde.internal.ui.PDEUIMessages; 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.parts.FormEntry; 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.layout.GridData; 29 import org.eclipse.swt.widgets.Composite; 30 import org.eclipse.ui.forms.IFormPart; 31 import org.eclipse.ui.forms.IManagedForm; 32 import org.eclipse.ui.forms.IPartSelectionListener; 33 import org.eclipse.ui.forms.widgets.FormToolkit; 34 import org.eclipse.ui.forms.widgets.Section; 35 36 public class DataDetailsSection extends PDESection implements IFormPart, 37 IPartSelectionListener { 38 protected IFeatureEntry fInput; 39 40 private FormEntry fdownloadSizeText; 41 42 private FormEntry fInstallSizeText; 43 44 public DataDetailsSection(PDEFormPage page, Composite parent) { 45 this(page, parent, PDEUIMessages.SiteEditor_DataDetailsSection_title, 46 PDEUIMessages.SiteEditor_DataDetailsSection_desc, SWT.NULL); 47 } 48 49 public DataDetailsSection(PDEFormPage page, Composite parent, String title, 50 String desc, int toggleStyle) { 51 super(page, parent, Section.DESCRIPTION | toggleStyle); 52 getSection().setText(title); 53 getSection().setDescription(desc); 54 createClient(getSection(), page.getManagedForm().getToolkit()); 55 } 56 57 public void cancelEdit() { 58 fdownloadSizeText.cancelEdit(); 59 fInstallSizeText.cancelEdit(); 60 super.cancelEdit(); 61 } 62 63 public void commit(boolean onSave) { 64 fdownloadSizeText.commit(); 65 fInstallSizeText.commit(); 66 super.commit(onSave); 67 } 68 69 public void createClient(Section section, FormToolkit toolkit) { 70 71 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 72 GridData data = new GridData(GridData.FILL_BOTH); 73 section.setLayoutData(data); 74 75 Composite container = toolkit.createComposite(section); 76 container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2)); 77 container.setLayoutData(new GridData(GridData.FILL_BOTH)); 78 79 fdownloadSizeText = new FormEntry(container, toolkit, PDEUIMessages.SiteEditor_DataDetailsSection_downloadSize, null, false); 80 fdownloadSizeText.setFormEntryListener(new FormEntryAdapter(this) { 81 82 public void textValueChanged(FormEntry text) { 83 if (fInput != null) 84 try { 85 fInput.setDownloadSize(getLong(text.getValue())); 86 } catch (CoreException e) { 87 PDEPlugin.logException(e); 88 } 89 } 90 }); 91 limitTextWidth(fdownloadSizeText); 92 fdownloadSizeText.setEditable(isEditable()); 93 94 fInstallSizeText = new FormEntry(container, toolkit, PDEUIMessages.SiteEditor_DataDetailsSection_installSize, null, false); 95 fInstallSizeText.setFormEntryListener(new FormEntryAdapter(this) { 96 97 public void textValueChanged(FormEntry text) { 98 if (fInput != null) 99 try { 100 fInput.setInstallSize(getLong(text.getValue())); 101 } catch (CoreException e) { 102 PDEPlugin.logException(e); 103 } 104 } 105 }); 106 limitTextWidth(fInstallSizeText); 107 fInstallSizeText.setEditable(isEditable()); 108 109 toolkit.paintBordersFor(container); 110 section.setClient(container); 111 } 112 113 public void dispose() { 114 IFeatureModel model = (IFeatureModel) getPage().getModel(); 115 if (model != null) 116 model.removeModelChangedListener(this); 117 super.dispose(); 118 } 119 120 125 public void initialize(IManagedForm form) { 126 IFeatureModel model = (IFeatureModel) getPage().getModel(); 127 if (model != null) 128 model.addModelChangedListener(this); 129 super.initialize(form); 130 } 131 132 protected void limitTextWidth(FormEntry entry) { 133 GridData gd = (GridData) entry.getText().getLayoutData(); 134 gd.widthHint = 30; 135 } 136 137 public void modelChanged(IModelChangedEvent e) { 138 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 139 markStale(); 140 } else if (e.getChangeType() == IModelChangedEvent.CHANGE 141 && e.getChangedObjects().length > 0 142 && e.getChangedObjects()[0] instanceof IFeatureData 143 && e.getChangedObjects()[0] == fInput) { 144 markStale(); 145 } 146 } 147 148 public void refresh() { 149 update(); 150 super.refresh(); 151 } 152 153 public void selectionChanged(IFormPart part, ISelection selection) { 154 if (selection instanceof IStructuredSelection && !selection.isEmpty()) { 155 Object o = ((IStructuredSelection) selection).getFirstElement(); 156 if (o instanceof IFeatureData) { 157 fInput = (IFeatureData) o; 158 } else { 159 fInput = null; 160 } 161 } else 162 fInput = null; 163 update(); 164 } 165 166 public void setFocus() { 167 if (fdownloadSizeText != null) 168 fdownloadSizeText.getText().setFocus(); 169 } 170 171 private void update() { 172 if (fInput != null) { 173 fdownloadSizeText 174 .setValue( 175 fInput.getDownloadSize() >= 0 ? "" + fInput.getDownloadSize() : null, true); fInstallSizeText 177 .setValue( 178 fInput.getInstallSize() >= 0 ? "" + fInput.getInstallSize() : null, true); } else { 180 fdownloadSizeText.setValue(null, true); 181 fInstallSizeText.setValue(null, true); 182 } 183 fdownloadSizeText.setEditable(fInput != null && isEditable()); 184 fInstallSizeText.setEditable(fInput != null && isEditable()); 185 } 186 187 public boolean isEditable() { 188 return getPage().getPDEEditor().getAggregateModel().isEditable(); 189 } 190 191 private long getLong(String svalue) { 192 if (svalue == null) 193 return 0; 194 try { 195 return Long.parseLong(svalue); 196 } catch (NumberFormatException e) { 197 return 0; 198 } 199 } 200 } 201 | Popular Tags |