001: /*******************************************************************************
002: * Copyright (c) 2000, 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.fix;
011:
012: import java.util.HashMap;
013: import java.util.Iterator;
014: import java.util.Map;
015:
016: import org.eclipse.core.runtime.IAdaptable;
017: import org.eclipse.core.runtime.preferences.IEclipsePreferences;
018: import org.eclipse.core.runtime.preferences.IScopeContext;
019: import org.eclipse.core.runtime.preferences.InstanceScope;
020:
021: import org.eclipse.core.resources.IProject;
022: import org.eclipse.core.resources.ProjectScope;
023:
024: import org.eclipse.swt.SWT;
025: import org.eclipse.swt.events.SelectionAdapter;
026: import org.eclipse.swt.events.SelectionEvent;
027: import org.eclipse.swt.layout.GridData;
028: import org.eclipse.swt.layout.GridLayout;
029: import org.eclipse.swt.widgets.Button;
030: import org.eclipse.swt.widgets.Composite;
031: import org.eclipse.swt.widgets.Control;
032: import org.eclipse.swt.widgets.Link;
033: import org.eclipse.swt.widgets.Shell;
034:
035: import org.eclipse.jface.preference.IPreferencePageContainer;
036:
037: import org.eclipse.ui.dialogs.PreferencesUtil;
038: import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
039:
040: import org.eclipse.jdt.core.IJavaProject;
041: import org.eclipse.jdt.core.JavaCore;
042:
043: import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
044: import org.eclipse.jdt.internal.corext.fix.CleanUpPostSaveListener;
045: import org.eclipse.jdt.internal.corext.fix.CleanUpPreferenceUtil;
046: import org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring;
047:
048: import org.eclipse.jdt.ui.JavaUI;
049:
050: import org.eclipse.jdt.internal.ui.javaeditor.saveparticipant.AbstractSaveParticipantPreferenceConfiguration;
051: import org.eclipse.jdt.internal.ui.preferences.BulletListBlock;
052: import org.eclipse.jdt.internal.ui.preferences.CodeFormatterPreferencePage;
053: import org.eclipse.jdt.internal.ui.preferences.ImportOrganizePreferencePage;
054: import org.eclipse.jdt.internal.ui.util.PixelConverter;
055:
056: /**
057: * Preference configuration UI for the clean up save participant.
058: *
059: * @since 3.3
060: */
061: public class CleanUpSaveParticipantPreferenceConfiguration extends
062: AbstractSaveParticipantPreferenceConfiguration {
063:
064: private static final int INDENT = 10;
065:
066: private IScopeContext fContext;
067: private Map fSettings;
068: private BulletListBlock fSelectedActionsText;
069: private Button fFormatCodeButton;
070: private Button fOrganizeImportsButton;
071: private Shell fShell;
072: private Link fFormatConfigLink;
073: private Link fOrganizeImportsConfigLink;
074: private IPreferencePageContainer fContainer;
075: private Button fAdditionalActionButton;
076: private Button fConfigureButton;
077:
078: /**
079: * {@inheritDoc}
080: */
081: public Control createConfigControl(final Composite parent,
082: IPreferencePageContainer container) {
083: fContainer = container;
084: fShell = parent.getShell();
085:
086: final Composite composite = new Composite(parent, SWT.NONE);
087: GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
088: gridData.horizontalIndent = INDENT;
089: composite.setLayoutData(gridData);
090: GridLayout gridLayout = new GridLayout(1, false);
091: gridLayout.marginHeight = 0;
092: gridLayout.marginWidth = 0;
093: composite.setLayout(gridLayout);
094:
095: fFormatCodeButton = new Button(composite, SWT.CHECK);
096: fFormatCodeButton
097: .setText(SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_SaveActionPreferencePage_FormatSource_Checkbox);
098: fFormatCodeButton.setLayoutData(new GridData(SWT.FILL,
099: SWT.FILL, true, true));
100: fFormatCodeButton.addSelectionListener(new SelectionAdapter() {
101: /**
102: * {@inheritDoc}
103: */
104: public void widgetSelected(SelectionEvent e) {
105: changeSettingsValue(
106: CleanUpConstants.FORMAT_SOURCE_CODE,
107: fFormatCodeButton.getSelection());
108: }
109: });
110:
111: PixelConverter pixelConverter = new PixelConverter(parent);
112: int heightOneHalf = (int) Math.round(pixelConverter
113: .convertHeightInCharsToPixels(1) * 1.5);
114:
115: fFormatConfigLink = new Link(composite, SWT.NONE);
116: fFormatConfigLink
117: .setText(SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_ConfigureFormatter_Link);
118: GridData gridData2 = new GridData(SWT.LEFT, SWT.TOP, false,
119: true);
120: gridData2.horizontalIndent = 20;
121: gridData2.minimumHeight = heightOneHalf;
122: fFormatConfigLink.setLayoutData(gridData2);
123:
124: fOrganizeImportsButton = new Button(composite, SWT.CHECK);
125: fOrganizeImportsButton
126: .setText(SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_SaveActionPreferencePage_OrganizeImports_Checkbox);
127: fOrganizeImportsButton.setLayoutData(new GridData(SWT.FILL,
128: SWT.FILL, true, true));
129: fOrganizeImportsButton
130: .addSelectionListener(new SelectionAdapter() {
131: /**
132: * {@inheritDoc}
133: */
134: public void widgetSelected(SelectionEvent e) {
135: changeSettingsValue(
136: CleanUpConstants.ORGANIZE_IMPORTS,
137: fOrganizeImportsButton.getSelection());
138: }
139: });
140:
141: fOrganizeImportsConfigLink = new Link(composite, SWT.NONE);
142: fOrganizeImportsConfigLink
143: .setText(SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_ConfigureImports_Link);
144: GridData gridData3 = new GridData(SWT.LEFT, SWT.TOP, false,
145: true);
146: gridData3.horizontalIndent = 20;
147: gridData3.minimumHeight = heightOneHalf;
148: fOrganizeImportsConfigLink.setLayoutData(gridData3);
149:
150: fAdditionalActionButton = new Button(composite, SWT.CHECK);
151: fAdditionalActionButton
152: .setText(SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_AdditionalActions_Checkbox);
153:
154: createAdvancedComposite(composite);
155: fAdditionalActionButton
156: .addSelectionListener(new SelectionAdapter() {
157: /**
158: * {@inheritDoc}
159: */
160: public void widgetSelected(SelectionEvent e) {
161: changeSettingsValue(
162: CleanUpConstants.CLEANUP_ON_SAVE_ADDITIONAL_OPTIONS,
163: fAdditionalActionButton.getSelection());
164: }
165: });
166:
167: return composite;
168: }
169:
170: private Composite createAdvancedComposite(final Composite parent) {
171: Composite composite = new Composite(parent, SWT.NONE);
172: GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
173: gridData.horizontalIndent = INDENT;
174: composite.setLayoutData(gridData);
175: GridLayout layout = new GridLayout(2, false);
176: layout.marginHeight = 0;
177: layout.marginWidth = 0;
178: composite.setLayout(layout);
179:
180: fSelectedActionsText = new BulletListBlock();
181: final GridData data = (GridData) fSelectedActionsText
182: .createControl(composite).getLayoutData();
183: data.heightHint = new PixelConverter(composite)
184: .convertHeightInCharsToPixels(8);
185:
186: fConfigureButton = new Button(composite, SWT.NONE);
187: fConfigureButton
188: .setText(SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_Configure_Button);
189: fConfigureButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP,
190: false, false));
191: fConfigureButton.addSelectionListener(new SelectionAdapter() {
192: /**
193: * {@inheritDoc}
194: */
195: public void widgetSelected(SelectionEvent e) {
196: new SaveActionSelectionDialog(parent.getShell(),
197: fSettings).open();
198: settingsChanged();
199: }
200:
201: });
202:
203: return composite;
204: }
205:
206: /**
207: * {@inheritDoc}
208: */
209: public void initialize(final IScopeContext context,
210: IAdaptable element) {
211: fContext = context;
212: fSettings = CleanUpPreferenceUtil
213: .loadSaveParticipantOptions(context);
214:
215: settingsChanged();
216:
217: IJavaProject javaProject = null;
218: if (element != null) {
219: IProject project = (IProject) element
220: .getAdapter(IProject.class);
221: if (project != null) {
222: IJavaProject jProject = JavaCore.create(project);
223: if (jProject != null && jProject.exists()) {
224: javaProject = jProject;
225: }
226: }
227: }
228:
229: configurePreferenceLink(fFormatConfigLink, javaProject,
230: CodeFormatterPreferencePage.PREF_ID,
231: CodeFormatterPreferencePage.PROP_ID);
232: configurePreferenceLink(fOrganizeImportsConfigLink,
233: javaProject, ImportOrganizePreferencePage.PREF_ID,
234: ImportOrganizePreferencePage.PROP_ID);
235:
236: super .initialize(context, element);
237: }
238:
239: /**
240: * {@inheritDoc}
241: */
242: public void dispose() {
243: super .dispose();
244: }
245:
246: /**
247: * {@inheritDoc}
248: */
249: public void performDefaults() {
250: fSettings = CleanUpPreferenceUtil
251: .loadSaveParticipantOptions(new InstanceScope());
252: settingsChanged();
253: }
254:
255: /**
256: * {@inheritDoc}
257: */
258: public void performOk() {
259: super .performOk();
260:
261: if (!ProjectScope.SCOPE.equals(fContext.getName())
262: || hasSettingsInScope(fContext))
263: CleanUpPreferenceUtil.saveSaveParticipantOptions(fContext,
264: fSettings);
265: }
266:
267: /**
268: * {@inheritDoc}
269: */
270: public void enableProjectSettings() {
271: super .enableProjectSettings();
272:
273: CleanUpPreferenceUtil.saveSaveParticipantOptions(fContext,
274: fSettings);
275:
276: updateAdvancedEnableState();
277: }
278:
279: /**
280: * {@inheritDoc}
281: */
282: public void disableProjectSettings() {
283: super .disableProjectSettings();
284:
285: IEclipsePreferences node = fContext.getNode(JavaUI.ID_PLUGIN);
286:
287: Map settings = CleanUpConstants.getSaveParticipantSettings();
288: for (Iterator iterator = settings.keySet().iterator(); iterator
289: .hasNext();) {
290: String key = (String) iterator.next();
291: node
292: .remove(CleanUpPreferenceUtil.SAVE_PARTICIPANT_KEY_PREFIX
293: + key);
294: }
295:
296: updateAdvancedEnableState();
297: }
298:
299: /**
300: * {@inheritDoc}
301: */
302: protected String getPostSaveListenerId() {
303: return CleanUpPostSaveListener.POSTSAVELISTENER_ID;
304: }
305:
306: /**
307: * {@inheritDoc}
308: */
309: protected String getPostSaveListenerName() {
310: return SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_CleanUpActionsTopNodeName_Checkbox;
311: }
312:
313: /**
314: * {@inheritDoc}
315: */
316: protected void enableConfigControl(boolean isEnabled) {
317: super .enableConfigControl(isEnabled);
318:
319: updateAdvancedEnableState();
320: }
321:
322: private void settingsChanged() {
323: fFormatCodeButton.setSelection(CleanUpConstants.TRUE
324: .equals(fSettings
325: .get(CleanUpConstants.FORMAT_SOURCE_CODE)));
326: fOrganizeImportsButton.setSelection(CleanUpConstants.TRUE
327: .equals(fSettings
328: .get(CleanUpConstants.ORGANIZE_IMPORTS)));
329: fAdditionalActionButton
330: .setSelection(CleanUpConstants.TRUE
331: .equals(fSettings
332: .get(CleanUpConstants.CLEANUP_ON_SAVE_ADDITIONAL_OPTIONS)));
333:
334: updateAdvancedEnableState();
335:
336: Map settings = new HashMap(fSettings);
337: settings.put(CleanUpConstants.FORMAT_SOURCE_CODE,
338: CleanUpConstants.FALSE);
339: settings.put(CleanUpConstants.ORGANIZE_IMPORTS,
340: CleanUpConstants.FALSE);
341:
342: final ICleanUp[] cleanUps = CleanUpRefactoring
343: .createCleanUps(settings);
344:
345: if (cleanUps.length == 0) {
346: fSelectedActionsText
347: .setText(SaveParticipantMessages.CleanUpSaveParticipantPreferenceConfiguration_NoActionEnabled_Info);
348: } else {
349: StringBuffer buf = new StringBuffer();
350:
351: boolean first = true;
352: for (int i = 0; i < cleanUps.length; i++) {
353: String[] descriptions = cleanUps[i].getDescriptions();
354: if (descriptions != null) {
355: for (int j = 0; j < descriptions.length; j++) {
356: if (first) {
357: first = false;
358: } else {
359: buf.append('\n');
360: }
361: buf.append(descriptions[j]);
362: }
363: }
364: }
365: fSelectedActionsText.setText(buf.toString());
366: }
367: }
368:
369: private void updateAdvancedEnableState() {
370: boolean additionalOptionEnabled = isEnabled(fContext)
371: && CleanUpConstants.TRUE
372: .equals(fSettings
373: .get(CleanUpConstants.CLEANUP_ON_SAVE_ADDITIONAL_OPTIONS));
374: boolean additionalEnabled = additionalOptionEnabled
375: && (!ProjectScope.SCOPE.equals(fContext.getName()) || hasSettingsInScope(fContext));
376: fSelectedActionsText.setEnabled(additionalEnabled);
377: fConfigureButton.setEnabled(additionalEnabled);
378: }
379:
380: private void configurePreferenceLink(Link link,
381: final IJavaProject javaProject, final String preferenceId,
382: final String propertyId) {
383: link.addSelectionListener(new SelectionAdapter() {
384: public void widgetSelected(SelectionEvent e) {
385: if (fContainer instanceof IWorkbenchPreferenceContainer) {
386: IWorkbenchPreferenceContainer container = (IWorkbenchPreferenceContainer) fContainer;
387: if (javaProject != null) {
388: container.openPage(propertyId, null);
389: } else {
390: container.openPage(preferenceId, null);
391: }
392: } else {
393: PreferencesUtil.createPreferenceDialogOn(fShell,
394: preferenceId, null, null);
395: }
396: }
397: });
398: }
399:
400: private void changeSettingsValue(String key, boolean enabled) {
401: String value;
402: if (enabled) {
403: value = CleanUpConstants.TRUE;
404: } else {
405: value = CleanUpConstants.FALSE;
406: }
407: fSettings.put(key, value);
408: settingsChanged();
409: }
410: }
|