001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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;
011:
012: import org.eclipse.jface.viewers.IStructuredContentProvider;
013: import org.eclipse.pde.internal.ui.parts.ILinkLabelProvider;
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.custom.BusyIndicator;
016: import org.eclipse.swt.events.SelectionAdapter;
017: import org.eclipse.swt.events.SelectionEvent;
018: import org.eclipse.swt.graphics.Image;
019: import org.eclipse.swt.layout.GridData;
020: import org.eclipse.swt.layout.GridLayout;
021: import org.eclipse.swt.widgets.Button;
022: import org.eclipse.swt.widgets.Composite;
023: import org.eclipse.swt.widgets.Control;
024: import org.eclipse.ui.forms.events.HyperlinkEvent;
025: import org.eclipse.ui.forms.events.IHyperlinkListener;
026: import org.eclipse.ui.forms.widgets.FormToolkit;
027: import org.eclipse.ui.forms.widgets.Hyperlink;
028: import org.eclipse.ui.forms.widgets.ImageHyperlink;
029: import org.eclipse.ui.forms.widgets.Section;
030:
031: /**
032: * This class can be used to show a standard section with an array of links.
033: * Links are objects from editor pages, and each one will select the owning
034: * page and reveal the element in it. If the number of objects from the content
035: * provider is greated than the preset limit, only the first 'limit' number of
036: * links will be shown, and a 'More...' button will show up (this is a change
037: * from 2.1 where 'More...' was visible all the time).
038: */
039: public class LinkSection extends PDESection {
040: private ILinkLabelProvider labelProvider;
041: private IStructuredContentProvider contentProvider;
042: private Composite linkContainer;
043: private Composite container;
044: private Button moreButton;
045: private String morePageId;
046: private int linkNumberLimit = 20;
047: private LinkHandler linkHandler;
048:
049: class LinkHandler implements IHyperlinkListener {
050: public void linkActivated(HyperlinkEvent e) {
051: doLinkActivated((Hyperlink) e.widget);
052: }
053:
054: public void linkEntered(HyperlinkEvent e) {
055: doEnter((Hyperlink) e.widget);
056: }
057:
058: public void linkExited(HyperlinkEvent e) {
059: doExit((Hyperlink) e.widget);
060: }
061: }
062:
063: /**
064: * @param page
065: * @param parent
066: * @param style
067: */
068: public LinkSection(PDEFormPage page, Composite parent, int style) {
069: super (page, parent, style);
070: FormToolkit toolkit = page.getManagedForm().getToolkit();
071: linkHandler = new LinkHandler();
072: createClient(getSection(), toolkit);
073: }
074:
075: /*
076: * (non-Javadoc)
077: *
078: * @see org.eclipse.pde.internal.ui.neweditor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section,
079: * org.eclipse.ui.forms.widgets.FormToolkit)
080: */
081: protected void createClient(Section section, FormToolkit toolkit) {
082: container = toolkit.createComposite(section);
083: GridLayout layout = new GridLayout();
084: layout.numColumns = 2;
085: container.setLayout(layout);
086: section.setClient(container);
087: linkContainer = toolkit.createComposite(container);
088: linkContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
089: GridLayout linkLayout = new GridLayout();
090: linkLayout.marginWidth = 0;
091: linkLayout.marginHeight = 0;
092: linkLayout.verticalSpacing = 0;
093: linkContainer.setLayout(linkLayout);
094: }
095:
096: private void createMoreButton() {
097: moreButton = getManagedForm().getToolkit().createButton(
098: container, "More...", //$NON-NLS-1$
099: SWT.PUSH);
100: moreButton.addSelectionListener(new SelectionAdapter() {
101: public void widgetSelected(SelectionEvent e) {
102: BusyIndicator.showWhile(getSection().getDisplay(),
103: new Runnable() {
104: public void run() {
105: getPage().getEditor().setActivePage(
106: morePageId);
107: }
108: });
109: }
110: });
111: moreButton.setLayoutData(new GridData(
112: GridData.VERTICAL_ALIGN_BEGINNING));
113: }
114:
115: public void add(Object[] links) {
116: for (int i = 0; i < links.length; i++) {
117: createLink(links[i]);
118: }
119: updateMoreState(linkContainer.getChildren().length > linkNumberLimit);
120: reflow();
121: }
122:
123: public void remove(Object[] links) {
124: for (int i = 0; i < links.length; i++) {
125: disposeLink(links[i]);
126: }
127: updateMoreState(linkContainer.getChildren().length > linkNumberLimit);
128: reflow();
129: }
130:
131: private void disposeLink(Object obj) {
132: Hyperlink link = find(obj);
133: if (link != null)
134: link.dispose();
135: }
136:
137: private Hyperlink find(Object object) {
138: Control[] children = linkContainer.getChildren();
139: for (int i = 0; i < children.length; i++) {
140: Control child = children[i];
141: if (child.getData().equals(object))
142: return (Hyperlink) child;
143: }
144: return null;
145: }
146:
147: public void update(Object[] links) {
148: for (int i = 0; i < links.length; i++) {
149: update(links[i]);
150: }
151: reflow();
152: }
153:
154: private void update(Object object) {
155: Hyperlink link = find(object);
156: if (link != null)
157: update(link, object);
158: }
159:
160: private void update(Hyperlink hyperlink, Object object) {
161: String text = labelProvider != null ? labelProvider
162: .getText(object) : object.toString();
163: Image image = labelProvider != null ? labelProvider
164: .getImage(object) : null;
165: String tooltip = labelProvider != null ? labelProvider
166: .getToolTipText(object) : text;
167: hyperlink.setText(text);
168: hyperlink.setToolTipText(tooltip);
169: if (hyperlink instanceof ImageHyperlink)
170: ((ImageHyperlink) hyperlink).setImage(image);
171: reflow();
172: }
173:
174: public void refresh() {
175: // dispose old links
176: Control[] children = linkContainer.getChildren();
177: for (int i = 0; i < children.length; i++) {
178: children[i].dispose();
179: }
180: createLinks();
181: reflow();
182: }
183:
184: private void reflow() {
185: linkContainer.layout();
186: container.layout();
187: getManagedForm().reflow(true);
188: }
189:
190: private void createLinks() {
191: if (contentProvider == null)
192: return;
193: Object[] objects = contentProvider.getElements(getManagedForm()
194: .getInput());
195: for (int i = 0; i < objects.length; i++) {
196: if (i == linkNumberLimit)
197: break;
198: createLink(objects[i]);
199: }
200: if (objects.length > linkNumberLimit)
201: getManagedForm().getToolkit().createLabel(linkContainer,
202: "...", SWT.NULL); //$NON-NLS-1$
203: updateMoreState(objects.length > linkNumberLimit);
204: }
205:
206: private void updateMoreState(boolean needMore) {
207: if (needMore && moreButton == null) {
208: createMoreButton();
209: } else if (!needMore && moreButton != null) {
210: moreButton.dispose();
211: moreButton = null;
212: }
213: }
214:
215: private void createLink(Object object) {
216: Image image = labelProvider != null ? labelProvider
217: .getImage(object) : null;
218: Hyperlink hyperlink;
219: if (image != null) {
220: hyperlink = getManagedForm().getToolkit()
221: .createImageHyperlink(linkContainer, SWT.NULL);
222: ((ImageHyperlink) hyperlink).setImage(image);
223: } else
224: hyperlink = getManagedForm().getToolkit().createHyperlink(
225: linkContainer, null, SWT.NULL);
226: update(hyperlink, object);
227: hyperlink.setData(object);
228: hyperlink.addHyperlinkListener(linkHandler);
229: }
230:
231: private void doEnter(Hyperlink link) {
232: String statusText = labelProvider != null ? labelProvider
233: .getStatusText(link.getData()) : link.getText();
234: getPage().getEditorSite().getActionBars()
235: .getStatusLineManager().setMessage(statusText);
236: }
237:
238: private void doExit(Hyperlink link) {
239: getPage().getEditorSite().getActionBars()
240: .getStatusLineManager().setMessage(null);
241: }
242:
243: protected void doLinkActivated(Hyperlink link) {
244: Object object = link.getData();
245: getPage().getEditor().setActivePage(morePageId, object);
246: }
247:
248: public void setMorePageId(String id) {
249: this .morePageId = id;
250: }
251:
252: public void setLinkNumberLimit(int limit) {
253: this .linkNumberLimit = limit;
254: }
255:
256: public void setContentProvider(
257: IStructuredContentProvider contentProvider) {
258: this .contentProvider = contentProvider;
259: }
260:
261: public void setLabelProvider(ILinkLabelProvider provider) {
262: this.labelProvider = provider;
263: }
264: }
|