KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > PreferenceLinkArea


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.ui.dialogs;
12
13 import com.ibm.icu.text.MessageFormat;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.jface.preference.IPreferenceNode;
17 import org.eclipse.jface.preference.PreferenceManager;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Link;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.internal.WorkbenchMessages;
26 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
27
28 /**
29  * The PreferenceLinkArea is the link area used to open a specific preference
30  * page.
31  *
32  * @since 3.1
33  */

34 public class PreferenceLinkArea extends Object JavaDoc {
35
36     private Link pageLink;
37
38     /**
39      * Create a new instance of the receiver
40      *
41      * @param parent
42      * the parent Composite
43      * @param style
44      * the SWT style
45      * @param pageId
46      * the page id
47      * @param message
48      * the message to use as text. If this message has {0} in
49      * its value it will be bound with the displayed name of
50      * the preference page. This message must be well formed
51      * html if you wish to link to another page.
52      * @param pageContainer -
53      * The container another page will be opened in.
54      * @param pageData -
55      * The data to apply to the page.
56      */

57     public PreferenceLinkArea(Composite parent, int style, final String JavaDoc pageId,
58             String JavaDoc message, final IWorkbenchPreferenceContainer pageContainer,
59             final Object JavaDoc pageData) {
60         pageLink = new Link(parent, style);
61
62         IPreferenceNode node = getPreferenceNode(pageId);
63         String JavaDoc result;
64         if (node == null) {
65             result = NLS.bind(
66                     WorkbenchMessages.PreferenceNode_NotFound, pageId);
67         } else {
68             result = MessageFormat.format(message, new String JavaDoc[] { node
69                     .getLabelText() });
70             
71             //Only add the selection listener if the node is found
72
pageLink.addSelectionListener(new SelectionAdapter() {
73                 /*
74                  * (non-Javadoc)
75                  *
76                  * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
77                  */

78                 public void widgetSelected(SelectionEvent e) {
79                     pageContainer.openPage(pageId, pageData);
80                 }
81             });
82         }
83         pageLink.setText(result);
84
85     }
86
87     /**
88      * Get the preference node with pageId.
89      *
90      * @param pageId
91      * @return IPreferenceNode
92      */

93     private IPreferenceNode getPreferenceNode(String JavaDoc pageId) {
94         Iterator JavaDoc iterator = PlatformUI.getWorkbench().getPreferenceManager()
95                 .getElements(PreferenceManager.PRE_ORDER).iterator();
96         while (iterator.hasNext()) {
97             IPreferenceNode next = (IPreferenceNode) iterator.next();
98             if (next.getId().equals(pageId)) {
99                 return next;
100             }
101         }
102         return null;
103     }
104
105     /**
106      * Return the control for the receiver.
107      *
108      * @return Control
109      */

110     public Control getControl() {
111         return pageLink;
112     }
113 }
114
Popular Tags