KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > preferences > OSGiFrameworkPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.preferences;
12
13 import org.eclipse.core.runtime.IConfigurationElement;
14 import org.eclipse.jface.dialogs.Dialog;
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.jface.preference.PreferencePage;
17 import org.eclipse.jface.viewers.ArrayContentProvider;
18 import org.eclipse.jface.viewers.ISelectionChangedListener;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.LabelProvider;
21 import org.eclipse.jface.viewers.SelectionChangedEvent;
22 import org.eclipse.jface.viewers.TableViewer;
23 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
24 import org.eclipse.pde.internal.core.PDEStateHelper;
25 import org.eclipse.pde.internal.ui.IHelpContextIds;
26 import org.eclipse.pde.internal.ui.IPreferenceConstants;
27 import org.eclipse.pde.internal.ui.PDEPlugin;
28 import org.eclipse.pde.internal.ui.PDEPluginImages;
29 import org.eclipse.pde.internal.ui.PDEUIMessages;
30 import org.eclipse.pde.internal.ui.launcher.OSGiFrameworkManager;
31 import org.eclipse.pde.internal.ui.search.ShowDescriptionAction;
32 import org.eclipse.pde.internal.ui.util.SWTUtil;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.graphics.Image;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Button;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Control;
42 import org.eclipse.swt.widgets.Display;
43 import org.eclipse.swt.widgets.Link;
44 import org.eclipse.ui.IWorkbench;
45 import org.eclipse.ui.IWorkbenchPreferencePage;
46 import org.eclipse.ui.PlatformUI;
47
48 public class OSGiFrameworkPreferencePage extends PreferencePage implements
49         IWorkbenchPreferencePage {
50     
51     class FrameworkLabelProvider extends LabelProvider {
52         private Image image;
53         
54         public FrameworkLabelProvider() {
55             image = PDEPluginImages.DESC_BUNDLE_OBJ.createImage();
56         }
57         
58         public Image getImage(Object JavaDoc element) {
59             return image;
60         }
61         
62         public String JavaDoc getText(Object JavaDoc element) {
63             if (element instanceof IConfigurationElement) {
64                 String JavaDoc name = ((IConfigurationElement)element).getAttribute(OSGiFrameworkManager.ATT_NAME);
65                 String JavaDoc id = ((IConfigurationElement)element).getAttribute(OSGiFrameworkManager.ATT_ID);
66                 return fDefaultFramework.equals(id) ? name + " " + PDEUIMessages.OSGiFrameworkPreferencePage_default : name; //$NON-NLS-1$
67
}
68             return super.getText(element);
69         }
70         
71         public void dispose() {
72             image.dispose();
73             super.dispose();
74         }
75     }
76
77     private TableViewer fTableViewer;
78     private Button fSetDefaultButton;
79     private String JavaDoc fDefaultFramework;
80
81     public OSGiFrameworkPreferencePage() {
82         setDefaultFramework();
83     }
84     
85     private void setDefaultFramework() {
86         IPreferenceStore store = PDEPlugin.getDefault().getPreferenceStore();
87         fDefaultFramework = store.getString(IPreferenceConstants.DEFAULT_OSGI_FRAMEOWRK);
88     }
89     
90     protected Control createContents(Composite parent) {
91         Composite container = new Composite(parent, SWT.NULL);
92         GridLayout layout = new GridLayout();
93         layout.numColumns = 2;
94         layout.verticalSpacing = 10;
95         container.setLayout(layout);
96
97         Link text = new Link(container, SWT.WRAP);
98         text.setText(PDEUIMessages.OSGiFrameworkPreferencePage_installed);
99         GridData gd = new GridData();
100         gd.horizontalSpan = 2;
101         text.setLayoutData(gd);
102         text.addSelectionListener(new SelectionAdapter() {
103             public void widgetSelected(SelectionEvent e) {
104                 IPluginExtensionPoint point = PDEStateHelper.findExtensionPoint(OSGiFrameworkManager.POINT_ID);
105                 if (point != null) {
106                     new ShowDescriptionAction(point, true).run();
107                 } else {
108                     Display.getDefault().beep();
109                 }
110             }
111         });
112
113         fTableViewer = new TableViewer(container, SWT.BORDER);
114         fTableViewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
115         fTableViewer.setContentProvider(new ArrayContentProvider());
116         fTableViewer.setLabelProvider(new FrameworkLabelProvider());
117         fTableViewer.setInput(PDEPlugin.getDefault().getOSGiFrameworkManager().getSortedFrameworks());
118         fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
119             public void selectionChanged(SelectionChangedEvent event) {
120                 IStructuredSelection ssel = (IStructuredSelection)event.getSelection();
121                 String JavaDoc id = ((IConfigurationElement)ssel.getFirstElement()).getAttribute("id"); //$NON-NLS-1$
122
fSetDefaultButton.setEnabled(ssel.size() == 1 && !fDefaultFramework.equals(id));
123             }
124         });
125     
126         Composite buttonContainer = new Composite(container, SWT.NONE);
127         layout = new GridLayout();
128         layout.marginWidth = layout.marginHeight = 0;
129         buttonContainer.setLayout(layout);
130         buttonContainer.setLayoutData(new GridData(GridData.FILL_VERTICAL));
131         
132         fSetDefaultButton = new Button(buttonContainer, SWT.PUSH);
133         fSetDefaultButton.setText(PDEUIMessages.OSGiFrameworkPreferencePage_setAs);
134         fSetDefaultButton.setLayoutData(new GridData(GridData.FILL | GridData.VERTICAL_ALIGN_BEGINNING));
135         SWTUtil.setButtonDimensionHint(fSetDefaultButton);
136         fSetDefaultButton.addSelectionListener(new SelectionAdapter() {
137             public void widgetSelected(SelectionEvent e) {
138                 IStructuredSelection ssel = (IStructuredSelection)fTableViewer.getSelection();
139                 IConfigurationElement element = (IConfigurationElement)ssel.getFirstElement();
140                 fDefaultFramework = element.getAttribute(OSGiFrameworkManager.ATT_ID);
141                 fTableViewer.refresh();
142                 fSetDefaultButton.setEnabled(false);
143             }
144         });
145         fSetDefaultButton.setEnabled(false);
146         Dialog.applyDialogFont(parent);
147         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.OSGI_PREFERENCE_PAGE);
148         return container;
149     }
150     
151     public boolean performOk() {
152         IPreferenceStore store = PDEPlugin.getDefault().getPreferenceStore();
153         store.setValue(IPreferenceConstants.DEFAULT_OSGI_FRAMEOWRK, fDefaultFramework);
154         PDEPlugin.getDefault().savePluginPreferences();
155         return super.performOk();
156     }
157     
158     protected void performDefaults() {
159         setDefaultFramework();
160         fTableViewer.refresh();
161     }
162
163     public void init(IWorkbench workbench) {
164     }
165     
166 }
167
Popular Tags