1 11 package org.eclipse.pde.internal.ui.editor; 12 import org.eclipse.jface.viewers.IStructuredContentProvider; 13 import org.eclipse.pde.internal.ui.parts.ILinkLabelProvider; 14 import org.eclipse.swt.SWT; 15 import org.eclipse.swt.custom.BusyIndicator; 16 import org.eclipse.swt.events.SelectionAdapter; 17 import org.eclipse.swt.events.SelectionEvent; 18 import org.eclipse.swt.graphics.Image; 19 import org.eclipse.swt.layout.GridData; 20 import org.eclipse.swt.layout.GridLayout; 21 import org.eclipse.swt.widgets.Button; 22 import org.eclipse.swt.widgets.Composite; 23 import org.eclipse.swt.widgets.Control; 24 import org.eclipse.ui.forms.events.HyperlinkEvent; 25 import org.eclipse.ui.forms.events.IHyperlinkListener; 26 import org.eclipse.ui.forms.widgets.FormToolkit; 27 import org.eclipse.ui.forms.widgets.Hyperlink; 28 import org.eclipse.ui.forms.widgets.ImageHyperlink; 29 import org.eclipse.ui.forms.widgets.Section; 30 38 public class LinkSection extends PDESection { 39 private ILinkLabelProvider labelProvider; 40 private IStructuredContentProvider contentProvider; 41 private Composite linkContainer; 42 private Composite container; 43 private Button moreButton; 44 private String morePageId; 45 private int linkNumberLimit = 20; 46 private LinkHandler linkHandler; 47 class LinkHandler implements IHyperlinkListener { 48 public void linkActivated(HyperlinkEvent e) { 49 doLinkActivated((Hyperlink) e.widget); 50 } 51 public void linkEntered(HyperlinkEvent e) { 52 doEnter((Hyperlink) e.widget); 53 } 54 public void linkExited(HyperlinkEvent e) { 55 doExit((Hyperlink) e.widget); 56 } 57 } 58 63 public LinkSection(PDEFormPage page, Composite parent, int style) { 64 super(page, parent, style); 65 FormToolkit toolkit = page.getManagedForm().getToolkit(); 66 linkHandler = new LinkHandler(); 67 createClient(getSection(), toolkit); 68 } 69 75 protected void createClient(Section section, FormToolkit toolkit) { 76 container = toolkit.createComposite(section); 77 GridLayout layout = new GridLayout(); 78 layout.numColumns = 2; 79 container.setLayout(layout); 80 section.setClient(container); 81 linkContainer = toolkit.createComposite(container); 82 linkContainer.setLayoutData(new GridData(GridData.FILL_BOTH)); 83 GridLayout linkLayout = new GridLayout(); 84 linkLayout.marginWidth = 0; 85 linkLayout.marginHeight = 0; 86 linkLayout.verticalSpacing = 0; 87 linkContainer.setLayout(linkLayout); 88 } 89 private void createMoreButton() { 90 moreButton = getManagedForm().getToolkit().createButton(container, "More...", SWT.PUSH); 92 moreButton.addSelectionListener(new SelectionAdapter() { 93 public void widgetSelected(SelectionEvent e) { 94 BusyIndicator.showWhile(getSection().getDisplay(), 95 new Runnable () { 96 public void run() { 97 getPage().getEditor().setActivePage(morePageId); 98 } 99 }); 100 } 101 }); 102 moreButton 103 .setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING)); 104 } 105 public void add(Object [] links) { 106 for (int i = 0; i < links.length; i++) { 107 createLink(links[i]); 108 } 109 updateMoreState(linkContainer.getChildren().length > linkNumberLimit); 110 reflow(); 111 } 112 public void remove(Object [] links) { 113 for (int i = 0; i < links.length; i++) { 114 disposeLink(links[i]); 115 } 116 updateMoreState(linkContainer.getChildren().length > linkNumberLimit); 117 reflow(); 118 } 119 private void disposeLink(Object obj) { 120 Hyperlink link = find(obj); 121 if (link != null) 122 link.dispose(); 123 } 124 private Hyperlink find(Object object) { 125 Control[] children = linkContainer.getChildren(); 126 for (int i = 0; i < children.length; i++) { 127 Control child = children[i]; 128 if (child.getData().equals(object)) 129 return (Hyperlink) child; 130 } 131 return null; 132 } 133 public void update(Object [] links) { 134 for (int i = 0; i < links.length; i++) { 135 update(links[i]); 136 } 137 reflow(); 138 } 139 private void update(Object object) { 140 Hyperlink link = find(object); 141 if (link != null) 142 update(link, object); 143 } 144 private void update(Hyperlink hyperlink, Object object) { 145 String text = labelProvider != null 146 ? labelProvider.getText(object) 147 : object.toString(); 148 Image image = labelProvider != null 149 ? labelProvider.getImage(object) 150 : null; 151 String tooltip = labelProvider != null ? labelProvider 152 .getToolTipText(object) : text; 153 hyperlink.setText(text); 154 hyperlink.setToolTipText(tooltip); 155 if (hyperlink instanceof ImageHyperlink) 156 ((ImageHyperlink) hyperlink).setImage(image); 157 reflow(); 158 } 159 public void refresh() { 160 Control[] children = linkContainer.getChildren(); 162 for (int i = 0; i < children.length; i++) { 163 children[i].dispose(); 164 } 165 createLinks(); 166 reflow(); 167 } 168 private void reflow() { 169 linkContainer.layout(); 170 container.layout(); 171 getManagedForm().reflow(true); 172 } 173 private void createLinks() { 174 if (contentProvider == null) 175 return; 176 Object [] objects = contentProvider.getElements(getManagedForm().getInput()); 177 for (int i = 0; i < objects.length; i++) { 178 if (i == linkNumberLimit) 179 break; 180 createLink(objects[i]); 181 } 182 if (objects.length > linkNumberLimit) 183 getManagedForm().getToolkit().createLabel(linkContainer, "...", SWT.NULL); updateMoreState(objects.length > linkNumberLimit); 185 } 186 private void updateMoreState(boolean needMore) { 187 if (needMore && moreButton == null) { 188 createMoreButton(); 189 } 190 else if (!needMore && moreButton != null) { 191 moreButton.dispose(); 192 moreButton = null; 193 } 194 } 195 private void createLink(Object object) { 196 Image image = labelProvider != null 197 ? labelProvider.getImage(object) 198 : null; 199 Hyperlink hyperlink; 200 if (image != null) { 201 hyperlink = getManagedForm().getToolkit().createImageHyperlink( 202 linkContainer, SWT.NULL); 203 ((ImageHyperlink) hyperlink).setImage(image); 204 } else 205 hyperlink = getManagedForm().getToolkit().createHyperlink(linkContainer, 206 null, SWT.NULL); 207 update(hyperlink, object); 208 hyperlink.setData(object); 209 hyperlink.addHyperlinkListener(linkHandler); 210 } 211 private void doEnter(Hyperlink link) { 212 String statusText = labelProvider != null ? labelProvider 213 .getStatusText(link.getData()) : link.getText(); 214 getPage().getEditorSite().getActionBars().getStatusLineManager() 215 .setMessage(statusText); 216 } 217 private void doExit(Hyperlink link) { 218 getPage().getEditorSite().getActionBars().getStatusLineManager() 219 .setMessage(null); 220 } 221 protected void doLinkActivated(Hyperlink link) { 222 Object object = link.getData(); 223 getPage().getEditor().setActivePage(morePageId, object); 224 } 225 public void setMorePageId(String id) { 226 this.morePageId = id; 227 } 228 public void setLinkNumberLimit(int limit) { 229 this.linkNumberLimit = limit; 230 } 231 public void setContentProvider(IStructuredContentProvider contentProvider) { 232 this.contentProvider = contentProvider; 233 } 234 public void setLabelProvider(ILinkLabelProvider provider) { 235 this.labelProvider = provider; 236 } 237 } 238 | Popular Tags |