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.ui.internal.dialogs;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.jface.dialogs.MessageDialog;
015: import org.eclipse.jface.preference.PreferenceManager;
016: import org.eclipse.jface.viewers.ISelection;
017: import org.eclipse.jface.viewers.StructuredSelection;
018: import org.eclipse.osgi.util.NLS;
019: import org.eclipse.swt.widgets.Shell;
020: import org.eclipse.ui.PlatformUI;
021: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
022: import org.eclipse.ui.internal.WorkbenchMessages;
023: import org.eclipse.ui.internal.util.Util;
024: import org.eclipse.ui.model.IWorkbenchAdapter;
025:
026: /**
027: * This dialog is created and shown when 'Properties' action is performed while
028: * an object is selected. It shows one or more pages registered for object's
029: * type.
030: */
031: public class PropertyDialog extends FilteredPreferenceDialog {
032: private ISelection selection;
033:
034: // The id of the last page that was selected
035: private static String lastPropertyId = null;
036:
037: /**
038: * Create a new property dialog.
039: *
040: * @param shell
041: * the parent shell
042: * @param propertyPageId
043: * the property page id
044: * @param element
045: * the adaptable element
046: * @return the property dialog
047: */
048: public static PropertyDialog createDialogOn(Shell shell,
049: final String propertyPageId, Object element) {
050:
051: PropertyPageManager pageManager = new PropertyPageManager();
052: String title = "";//$NON-NLS-1$
053:
054: if (element == null) {
055: return null;
056: }
057: // load pages for the selection
058: // fill the manager with contributions from the matching contributors
059: PropertyPageContributorManager.getManager().contribute(
060: pageManager, element);
061: // testing if there are pages in the manager
062: Iterator pages = pageManager.getElements(
063: PreferenceManager.PRE_ORDER).iterator();
064: String name = getName(element);
065: if (!pages.hasNext()) {
066: MessageDialog
067: .openInformation(
068: shell,
069: WorkbenchMessages.PropertyDialog_messageTitle,
070: NLS
071: .bind(
072: WorkbenchMessages.PropertyDialog_noPropertyMessage,
073: name));
074: return null;
075: }
076: title = NLS.bind(
077: WorkbenchMessages.PropertyDialog_propertyMessage, name);
078: PropertyDialog propertyDialog = new PropertyDialog(shell,
079: pageManager, new StructuredSelection(element));
080:
081: if (propertyPageId != null) {
082: propertyDialog.setSelectedNode(propertyPageId);
083: }
084: propertyDialog.create();
085:
086: propertyDialog.getShell().setText(title);
087: PlatformUI.getWorkbench().getHelpSystem().setHelp(
088: propertyDialog.getShell(),
089: IWorkbenchHelpContextIds.PROPERTY_DIALOG);
090:
091: return propertyDialog;
092:
093: }
094:
095: /**
096: * Returns the name of the given element.
097: *
098: * @param element
099: * the element
100: * @return the name of the element
101: */
102: private static String getName(Object element) {
103: IWorkbenchAdapter adapter = (IWorkbenchAdapter) Util
104: .getAdapter(element, IWorkbenchAdapter.class);
105: if (adapter != null) {
106: return adapter.getLabel(element);
107: }
108: return "";//$NON-NLS-1$
109: }
110:
111: /**
112: * Create an instance of the receiver.
113: *
114: * @param parentShell
115: * @param mng
116: * @param selection
117: */
118: public PropertyDialog(Shell parentShell, PreferenceManager mng,
119: ISelection selection) {
120: super (parentShell, mng);
121: setSelection(selection);
122: }
123:
124: /**
125: * Returns selection in the "Properties" action context.
126: *
127: * @return the selection
128: */
129: public ISelection getSelection() {
130: return selection;
131: }
132:
133: /**
134: * Sets the selection that will be used to determine target object.
135: *
136: * @param newSelection
137: * the new selection
138: */
139: public void setSelection(ISelection newSelection) {
140: selection = newSelection;
141: }
142:
143: /**
144: * Get the name of the selected item preference
145: */
146: protected String getSelectedNodePreference() {
147: return lastPropertyId;
148: }
149:
150: /**
151: * Get the name of the selected item preference
152: */
153: protected void setSelectedNodePreference(String pageId) {
154: lastPropertyId = pageId;
155: }
156:
157: }
|