KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > PropertyDialog


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.ui.internal.dialogs;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.jface.preference.PreferenceManager;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.StructuredSelection;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
23 import org.eclipse.ui.internal.WorkbenchMessages;
24 import org.eclipse.ui.internal.util.Util;
25 import org.eclipse.ui.model.IWorkbenchAdapter;
26
27 /**
28  * This dialog is created and shown when 'Properties' action is performed while
29  * an object is selected. It shows one or more pages registered for object's
30  * type.
31  */

32 public class PropertyDialog extends FilteredPreferenceDialog {
33     private ISelection selection;
34
35     // The id of the last page that was selected
36
private static String JavaDoc lastPropertyId = null;
37
38     /**
39      * Create a new property dialog.
40      *
41      * @param shell
42      * the parent shell
43      * @param propertyPageId
44      * the property page id
45      * @param element
46      * the adaptable element
47      * @return the property dialog
48      */

49     public static PropertyDialog createDialogOn(Shell shell,
50             final String JavaDoc propertyPageId, Object JavaDoc element) {
51
52         PropertyPageManager pageManager = new PropertyPageManager();
53         String JavaDoc title = "";//$NON-NLS-1$
54

55         if (element == null) {
56             return null;
57         }
58         // load pages for the selection
59
// fill the manager with contributions from the matching contributors
60
PropertyPageContributorManager.getManager().contribute(pageManager,
61                 element);
62         // testing if there are pages in the manager
63
Iterator JavaDoc pages = pageManager.getElements(PreferenceManager.PRE_ORDER)
64                 .iterator();
65         String JavaDoc name = getName(element);
66         if (!pages.hasNext()) {
67             MessageDialog.openInformation(shell,
68                     WorkbenchMessages.PropertyDialog_messageTitle, NLS.bind(
69                             WorkbenchMessages.PropertyDialog_noPropertyMessage,
70                             name));
71             return null;
72         }
73         title = NLS
74                 .bind(WorkbenchMessages.PropertyDialog_propertyMessage, name);
75         PropertyDialog propertyDialog = new PropertyDialog(shell, pageManager,
76                 new StructuredSelection(element));
77
78         if (propertyPageId != null) {
79             propertyDialog.setSelectedNode(propertyPageId);
80         }
81         propertyDialog.create();
82
83         propertyDialog.getShell().setText(title);
84         PlatformUI.getWorkbench().getHelpSystem().setHelp(
85                 propertyDialog.getShell(),
86                 IWorkbenchHelpContextIds.PROPERTY_DIALOG);
87
88         return propertyDialog;
89
90     }
91
92     /**
93      * Returns the name of the given element.
94      *
95      * @param element
96      * the element
97      * @return the name of the element
98      */

99     private static String JavaDoc getName(Object JavaDoc element) {
100         IWorkbenchAdapter adapter = (IWorkbenchAdapter)Util.getAdapter(element, IWorkbenchAdapter.class);
101         if (adapter != null) {
102             return adapter.getLabel(element);
103         }
104         return "";//$NON-NLS-1$
105
}
106
107     /**
108      * Create an instance of the receiver.
109      *
110      * @param parentShell
111      * @param mng
112      * @param selection
113      */

114     public PropertyDialog(Shell parentShell, PreferenceManager mng,
115             ISelection selection) {
116         super(parentShell, mng);
117         setSelection(selection);
118     }
119
120     /**
121      * Returns selection in the "Properties" action context.
122      *
123      * @return the selection
124      */

125     public ISelection getSelection() {
126         return selection;
127     }
128
129     /**
130      * Sets the selection that will be used to determine target object.
131      *
132      * @param newSelection
133      * the new selection
134      */

135     public void setSelection(ISelection newSelection) {
136         selection = newSelection;
137     }
138
139     /**
140      * Get the name of the selected item preference
141      */

142     protected String JavaDoc getSelectedNodePreference() {
143         return lastPropertyId;
144     }
145
146     /**
147      * Get the name of the selected item preference
148      */

149     protected void setSelectedNodePreference(String JavaDoc pageId) {
150         lastPropertyId = pageId;
151     }
152
153 }
154
Popular Tags