001: /*******************************************************************************
002: * Copyright (c) 2004, 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.ui.dialogs;
011:
012: import com.ibm.icu.text.MessageFormat;
013: import java.util.Iterator;
014:
015: import org.eclipse.jface.preference.IPreferenceNode;
016: import org.eclipse.jface.preference.PreferenceManager;
017: import org.eclipse.osgi.util.NLS;
018: import org.eclipse.swt.events.SelectionAdapter;
019: import org.eclipse.swt.events.SelectionEvent;
020: import org.eclipse.swt.widgets.Composite;
021: import org.eclipse.swt.widgets.Control;
022: import org.eclipse.swt.widgets.Link;
023: import org.eclipse.ui.PlatformUI;
024: import org.eclipse.ui.internal.WorkbenchMessages;
025: import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
026:
027: /**
028: * The PreferenceLinkArea is the link area used to open a specific preference
029: * page.
030: *
031: * @since 3.1
032: */
033: public class PreferenceLinkArea extends Object {
034:
035: private Link pageLink;
036:
037: /**
038: * Create a new instance of the receiver
039: *
040: * @param parent
041: * the parent Composite
042: * @param style
043: * the SWT style
044: * @param pageId
045: * the page id
046: * @param message
047: * the message to use as text. If this message has {0} in
048: * its value it will be bound with the displayed name of
049: * the preference page. This message must be well formed
050: * html if you wish to link to another page.
051: * @param pageContainer -
052: * The container another page will be opened in.
053: * @param pageData -
054: * The data to apply to the page.
055: */
056: public PreferenceLinkArea(Composite parent, int style,
057: final String pageId, String message,
058: final IWorkbenchPreferenceContainer pageContainer,
059: final Object pageData) {
060: pageLink = new Link(parent, style);
061:
062: IPreferenceNode node = getPreferenceNode(pageId);
063: String result;
064: if (node == null) {
065: result = NLS.bind(
066: WorkbenchMessages.PreferenceNode_NotFound, pageId);
067: } else {
068: result = MessageFormat.format(message, new String[] { node
069: .getLabelText() });
070:
071: //Only add the selection listener if the node is found
072: pageLink.addSelectionListener(new SelectionAdapter() {
073: /*
074: * (non-Javadoc)
075: *
076: * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
077: */
078: public void widgetSelected(SelectionEvent e) {
079: pageContainer.openPage(pageId, pageData);
080: }
081: });
082: }
083: pageLink.setText(result);
084:
085: }
086:
087: /**
088: * Get the preference node with pageId.
089: *
090: * @param pageId
091: * @return IPreferenceNode
092: */
093: private IPreferenceNode getPreferenceNode(String pageId) {
094: Iterator iterator = PlatformUI.getWorkbench()
095: .getPreferenceManager().getElements(
096: PreferenceManager.PRE_ORDER).iterator();
097: while (iterator.hasNext()) {
098: IPreferenceNode next = (IPreferenceNode) iterator.next();
099: if (next.getId().equals(pageId)) {
100: return next;
101: }
102: }
103: return null;
104: }
105:
106: /**
107: * Return the control for the receiver.
108: *
109: * @return Control
110: */
111: public Control getControl() {
112: return pageLink;
113: }
114: }
|