001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 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.jdt.internal.ui.preferences;
011:
012: import com.ibm.icu.text.Collator;
013:
014: import java.util.ArrayList;
015: import java.util.Arrays;
016: import java.util.Comparator;
017:
018: import org.eclipse.core.runtime.Assert;
019: import org.eclipse.core.runtime.IAdaptable;
020: import org.eclipse.core.runtime.preferences.IScopeContext;
021:
022: import org.eclipse.swt.SWT;
023: import org.eclipse.swt.layout.GridData;
024: import org.eclipse.swt.layout.GridLayout;
025: import org.eclipse.swt.widgets.Composite;
026: import org.eclipse.swt.widgets.Control;
027:
028: import org.eclipse.jface.preference.IPreferencePageContainer;
029: import org.eclipse.jface.preference.PreferencePage;
030:
031: import org.eclipse.jdt.internal.ui.JavaPlugin;
032: import org.eclipse.jdt.internal.ui.javaeditor.saveparticipant.ISaveParticipantPreferenceConfiguration;
033: import org.eclipse.jdt.internal.ui.javaeditor.saveparticipant.SaveParticipantDescriptor;
034: import org.eclipse.jdt.internal.ui.javaeditor.saveparticipant.SaveParticipantRegistry;
035:
036: /**
037: * Configures Java Editor save participants.
038: *
039: * @since 3.3
040: */
041: class SaveParticipantConfigurationBlock implements
042: IPreferenceAndPropertyConfigurationBlock {
043:
044: private interface IDelegateOperation {
045: public void run(ISaveParticipantPreferenceConfiguration block);
046: }
047:
048: private final PreferencePage fPreferencePage;
049: private final IScopeContext fContext;
050: private final ArrayList fConfigurations;
051:
052: public SaveParticipantConfigurationBlock(IScopeContext context,
053: PreferencePage preferencePage) {
054: Assert.isNotNull(context);
055: Assert.isNotNull(preferencePage);
056:
057: fContext = context;
058: fPreferencePage = preferencePage;
059: fConfigurations = new ArrayList();
060: }
061:
062: /*
063: * @see org.eclipse.jdt.internal.ui.preferences.IPreferenceConfigurationBlock#createControl(org.eclipse.swt.widgets.Composite)
064: * @since 3.3
065: */
066: public Control createControl(Composite parent) {
067: Composite composite = new Composite(parent, SWT.NONE);
068: composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true,
069: false));
070: GridLayout gridLayout = new GridLayout(2, false);
071: gridLayout.marginHeight = 0;
072: gridLayout.marginWidth = 0;
073: composite.setLayout(gridLayout);
074:
075: SaveParticipantRegistry registry = JavaPlugin.getDefault()
076: .getSaveParticipantRegistry();
077: SaveParticipantDescriptor[] descriptors = registry
078: .getSaveParticipantDescriptors();
079:
080: if (descriptors.length == 0)
081: return composite;
082:
083: Arrays.sort(descriptors, new Comparator() {
084: public int compare(Object o1, Object o2) {
085: SaveParticipantDescriptor d1 = (SaveParticipantDescriptor) o1;
086: SaveParticipantDescriptor d2 = (SaveParticipantDescriptor) o2;
087: return Collator.getInstance().compare(
088: d1.getPostSaveListener().getName(),
089: d2.getPostSaveListener().getName());
090: }
091: });
092:
093: IPreferencePageContainer container = fPreferencePage
094: .getContainer();
095: for (int i = 0; i < descriptors.length; i++) {
096: final SaveParticipantDescriptor descriptor = descriptors[i];
097: ISaveParticipantPreferenceConfiguration configuration = descriptor
098: .createPreferenceConfiguration();
099: configuration.createControl(composite, container);
100: fConfigurations.add(configuration);
101: }
102:
103: return composite;
104: }
105:
106: /*
107: * @see org.eclipse.jdt.internal.ui.preferences.IPreferenceConfigurationBlock#dispose()
108: */
109: public void dispose() {
110: delegateToPreferenceConfiguration(new IDelegateOperation() {
111: public void run(
112: ISaveParticipantPreferenceConfiguration block) {
113: block.dispose();
114: }
115: });
116: }
117:
118: /*
119: * @see org.eclipse.jdt.internal.ui.preferences.IPreferenceConfigurationBlock#initialize()
120: */
121: public void initialize() {
122: delegateToPreferenceConfiguration(new IDelegateOperation() {
123: public void run(
124: ISaveParticipantPreferenceConfiguration block) {
125: IAdaptable element = null;
126: if (fPreferencePage instanceof PropertyAndPreferencePage) {
127: element = ((PropertyAndPreferencePage) fPreferencePage)
128: .getElement();
129: }
130: block.initialize(fContext, element);
131: }
132: });
133: }
134:
135: /*
136: * @see org.eclipse.jdt.internal.ui.preferences.IPreferenceConfigurationBlock#performDefaults()
137: */
138: public void performDefaults() {
139: delegateToPreferenceConfiguration(new IDelegateOperation() {
140: public void run(
141: ISaveParticipantPreferenceConfiguration block) {
142: block.performDefaults();
143: }
144: });
145: }
146:
147: /*
148: * @see org.eclipse.jdt.internal.ui.preferences.IPreferenceConfigurationBlock#performOk()
149: */
150: public void performOk() {
151: delegateToPreferenceConfiguration(new IDelegateOperation() {
152: public void run(
153: ISaveParticipantPreferenceConfiguration block) {
154: block.performOk();
155: }
156: });
157: }
158:
159: /**
160: * {@inheritDoc}
161: */
162: public void enableProjectSettings() {
163: delegateToPreferenceConfiguration(new IDelegateOperation() {
164: public void run(
165: ISaveParticipantPreferenceConfiguration block) {
166: block.enableProjectSettings();
167: }
168: });
169: }
170:
171: /**
172: * {@inheritDoc}
173: */
174: public void disableProjectSettings() {
175: delegateToPreferenceConfiguration(new IDelegateOperation() {
176: public void run(
177: ISaveParticipantPreferenceConfiguration block) {
178: block.disableProjectSettings();
179: }
180: });
181: }
182:
183: private void delegateToPreferenceConfiguration(IDelegateOperation op) {
184: for (int i = 0; i < fConfigurations.size(); i++) {
185: ISaveParticipantPreferenceConfiguration block = (ISaveParticipantPreferenceConfiguration) fConfigurations
186: .get(i);
187: op.run(block);
188: }
189: }
190: }
|