KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > LinkSection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
31  * This class can be used to show a standard section with an array of links.
32  * Links are objects from editor pages, and each one will select the owning
33  * page and reveal the element in it. If the number of objects from the content
34  * provider is greated than the preset limit, only the first 'limit' number of
35  * links will be shown, and a 'More...' button will show up (this is a change
36  * from 2.1 where 'More...' was visible all the time).
37  */

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 JavaDoc 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     /**
59      * @param page
60      * @param parent
61      * @param style
62      */

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     /*
70      * (non-Javadoc)
71      *
72      * @see org.eclipse.pde.internal.ui.neweditor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section,
73      * org.eclipse.ui.forms.widgets.FormToolkit)
74      */

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...", //$NON-NLS-1$
91
SWT.PUSH);
92         moreButton.addSelectionListener(new SelectionAdapter() {
93             public void widgetSelected(SelectionEvent e) {
94                 BusyIndicator.showWhile(getSection().getDisplay(),
95                         new Runnable JavaDoc() {
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 JavaDoc[] 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 JavaDoc[] 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 JavaDoc obj) {
120         Hyperlink link = find(obj);
121         if (link != null)
122             link.dispose();
123     }
124     private Hyperlink find(Object JavaDoc 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 JavaDoc[] links) {
134         for (int i = 0; i < links.length; i++) {
135             update(links[i]);
136         }
137         reflow();
138     }
139     private void update(Object JavaDoc object) {
140         Hyperlink link = find(object);
141         if (link != null)
142             update(link, object);
143     }
144     private void update(Hyperlink hyperlink, Object JavaDoc object) {
145         String JavaDoc text = labelProvider != null
146                 ? labelProvider.getText(object)
147                 : object.toString();
148         Image image = labelProvider != null
149                 ? labelProvider.getImage(object)
150                 : null;
151         String JavaDoc 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         // dispose old links
161
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 JavaDoc[] 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); //$NON-NLS-1$
184
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 JavaDoc 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 JavaDoc 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 JavaDoc object = link.getData();
223         getPage().getEditor().setActivePage(morePageId, object);
224     }
225     public void setMorePageId(String JavaDoc 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