KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > SaveParticipantConfigurationBlock


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.jdt.internal.ui.preferences;
12
13 import com.ibm.icu.text.Collator;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.Comparator JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.IAdaptable;
21 import org.eclipse.core.runtime.preferences.IScopeContext;
22
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28
29 import org.eclipse.jface.preference.IPreferencePageContainer;
30 import org.eclipse.jface.preference.PreferencePage;
31
32 import org.eclipse.jdt.internal.ui.JavaPlugin;
33 import org.eclipse.jdt.internal.ui.javaeditor.saveparticipant.ISaveParticipantPreferenceConfiguration;
34 import org.eclipse.jdt.internal.ui.javaeditor.saveparticipant.SaveParticipantDescriptor;
35 import org.eclipse.jdt.internal.ui.javaeditor.saveparticipant.SaveParticipantRegistry;
36
37 /**
38  * Configures Java Editor save participants.
39  *
40  * @since 3.3
41  */

42 class SaveParticipantConfigurationBlock implements IPreferenceAndPropertyConfigurationBlock {
43     
44     private interface IDelegateOperation {
45         public void run(ISaveParticipantPreferenceConfiguration block);
46     }
47     
48     private final PreferencePage fPreferencePage;
49     private final IScopeContext fContext;
50     private final ArrayList JavaDoc fConfigurations;
51     
52     public SaveParticipantConfigurationBlock(IScopeContext context, PreferencePage preferencePage) {
53         Assert.isNotNull(context);
54         Assert.isNotNull(preferencePage);
55         
56         fContext= context;
57         fPreferencePage= preferencePage;
58         fConfigurations= new ArrayList JavaDoc();
59     }
60     
61     /*
62      * @see org.eclipse.jdt.internal.ui.preferences.IPreferenceConfigurationBlock#createControl(org.eclipse.swt.widgets.Composite)
63      * @since 3.3
64      */

65     public Control createControl(Composite parent) {
66         Composite composite= new Composite(parent, SWT.NONE);
67         composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
68         GridLayout gridLayout= new GridLayout(2, false);
69         gridLayout.marginHeight= 0;
70         gridLayout.marginWidth= 0;
71         composite.setLayout(gridLayout);
72         
73         SaveParticipantRegistry registry= JavaPlugin.getDefault().getSaveParticipantRegistry();
74         SaveParticipantDescriptor[] descriptors= registry.getSaveParticipantDescriptors();
75         
76         if (descriptors.length == 0)
77             return composite;
78         
79         Arrays.sort(descriptors, new Comparator JavaDoc() {
80             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
81                 SaveParticipantDescriptor d1= (SaveParticipantDescriptor)o1;
82                 SaveParticipantDescriptor d2= (SaveParticipantDescriptor)o2;
83                 return Collator.getInstance().compare(d1.getPostSaveListener().getName(), d2.getPostSaveListener().getName());
84             }
85         });
86         
87         IPreferencePageContainer container= fPreferencePage.getContainer();
88         for (int i= 0; i < descriptors.length; i++) {
89             final SaveParticipantDescriptor descriptor= descriptors[i];
90             ISaveParticipantPreferenceConfiguration configuration= descriptor.createPreferenceConfiguration();
91             configuration.createControl(composite, container);
92             fConfigurations.add(configuration);
93         }
94         
95         return composite;
96     }
97     
98     /*
99      * @see org.eclipse.jdt.internal.ui.preferences.IPreferenceConfigurationBlock#dispose()
100      */

101     public void dispose() {
102         delegateToPreferenceConfiguration(new IDelegateOperation() {
103             public void run(ISaveParticipantPreferenceConfiguration block) {
104                 block.dispose();
105             }
106         });
107     }
108     
109     /*
110      * @see org.eclipse.jdt.internal.ui.preferences.IPreferenceConfigurationBlock#initialize()
111      */

112     public void initialize() {
113         delegateToPreferenceConfiguration(new IDelegateOperation() {
114             public void run(ISaveParticipantPreferenceConfiguration block) {
115                 IAdaptable element= null;
116                 if (fPreferencePage instanceof PropertyAndPreferencePage) {
117                     element= ((PropertyAndPreferencePage)fPreferencePage).getElement();
118                 }
119                 block.initialize(fContext, element);
120             }
121         });
122     }
123     
124     /*
125      * @see org.eclipse.jdt.internal.ui.preferences.IPreferenceConfigurationBlock#performDefaults()
126      */

127     public void performDefaults() {
128         delegateToPreferenceConfiguration(new IDelegateOperation() {
129             public void run(ISaveParticipantPreferenceConfiguration block) {
130                 block.performDefaults();
131             }
132         });
133     }
134     
135     /*
136      * @see org.eclipse.jdt.internal.ui.preferences.IPreferenceConfigurationBlock#performOk()
137      */

138     public void performOk() {
139         delegateToPreferenceConfiguration(new IDelegateOperation() {
140             public void run(ISaveParticipantPreferenceConfiguration block) {
141                 block.performOk();
142             }
143         });
144     }
145     
146     /**
147      * {@inheritDoc}
148      */

149     public void enableProjectSettings() {
150         delegateToPreferenceConfiguration(new IDelegateOperation() {
151             public void run(ISaveParticipantPreferenceConfiguration block) {
152                 block.enableProjectSettings();
153             }
154         });
155     }
156     
157     /**
158      * {@inheritDoc}
159      */

160     public void disableProjectSettings() {
161         delegateToPreferenceConfiguration(new IDelegateOperation() {
162             public void run(ISaveParticipantPreferenceConfiguration block) {
163                 block.disableProjectSettings();
164             }
165         });
166     }
167     
168     private void delegateToPreferenceConfiguration(IDelegateOperation op) {
169         for (int i= 0; i < fConfigurations.size(); i++) {
170             ISaveParticipantPreferenceConfiguration block= (ISaveParticipantPreferenceConfiguration)fConfigurations.get(i);
171             op.run(block);
172         }
173     }
174 }
175
Popular Tags