001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.refactoring;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.events.SelectionAdapter;
014: import org.eclipse.swt.events.SelectionEvent;
015: import org.eclipse.swt.widgets.Button;
016: import org.eclipse.swt.widgets.Composite;
017:
018: import org.eclipse.ltk.core.refactoring.Refactoring;
019:
020: import org.eclipse.jdt.internal.corext.refactoring.tagging.IDelegateUpdating;
021:
022: import org.eclipse.jdt.internal.ui.JavaPlugin;
023:
024: /**
025: * This is a helper class to keep a consistent design between refactorings
026: * capable of creating delegates.
027: *
028: * @since 3.2
029: *
030: */
031: public class DelegateUIHelper {
032:
033: public static Button generateDeprecateDelegateCheckbox(
034: Composite parent, Refactoring refactoring) {
035: final IDelegateUpdating updating = (IDelegateUpdating) refactoring
036: .getAdapter(IDelegateUpdating.class);
037: if (updating == null || !updating.canEnableDelegateUpdating())
038: return null;
039: final Button button = createCheckbox(parent,
040: getDeprecateDelegateCheckBoxTitle(),
041: loadDeprecateDelegateSetting(updating));
042: updating.setDeprecateDelegates(button.getSelection());
043: button.addSelectionListener(new SelectionAdapter() {
044:
045: public void widgetSelected(SelectionEvent e) {
046: updating.setDeprecateDelegates(button.getSelection());
047: }
048: });
049: return button;
050: }
051:
052: public static Button generateLeaveDelegateCheckbox(
053: Composite parent, Refactoring refactoring, boolean plural) {
054: final IDelegateUpdating updating = (IDelegateUpdating) refactoring
055: .getAdapter(IDelegateUpdating.class);
056: if (updating == null || !updating.canEnableDelegateUpdating())
057: return null;
058: final Button button = createCheckbox(parent, updating
059: .getDelegateUpdatingTitle(plural),
060: loadLeaveDelegateSetting(updating));
061: updating.setDelegateUpdating(button.getSelection());
062: button.addSelectionListener(new SelectionAdapter() {
063:
064: public void widgetSelected(SelectionEvent e) {
065: updating.setDelegateUpdating(button.getSelection());
066: }
067: });
068: return button;
069: }
070:
071: public static void saveLeaveDelegateSetting(Button button) {
072: saveBooleanSetting(DELEGATE_UPDATING, button);
073: }
074:
075: public static void saveDeprecateDelegateSetting(Button button) {
076: saveBooleanSetting(DELEGATE_DEPRECATION, button);
077: }
078:
079: public static boolean loadLeaveDelegateSetting(
080: IDelegateUpdating refactoring) {
081: return getBooleanSetting(DELEGATE_UPDATING, refactoring
082: .getDelegateUpdating());
083: }
084:
085: public static boolean loadDeprecateDelegateSetting(
086: IDelegateUpdating refactoring) {
087: return getBooleanSetting(DELEGATE_DEPRECATION, refactoring
088: .getDeprecateDelegates());
089: }
090:
091: public static String getDeprecateDelegateCheckBoxTitle() {
092: return RefactoringMessages.DelegateCreator_deprecate_delegates;
093: }
094:
095: // ************** Helper methods *******************
096:
097: /**
098: * Dialog settings key (value is of type boolean).
099: */
100: public static final String DELEGATE_UPDATING = "delegateUpdating"; //$NON-NLS-1$
101:
102: /**
103: * Dialog settings key (value is of type boolean).
104: */
105: public static final String DELEGATE_DEPRECATION = "delegateDeprecation"; //$NON-NLS-1$
106:
107: private DelegateUIHelper() {
108: // no instances
109: }
110:
111: private static Button createCheckbox(Composite parent,
112: String title, boolean value) {
113: Button checkBox = new Button(parent, SWT.CHECK);
114: checkBox.setText(title);
115: checkBox.setSelection(value);
116: return checkBox;
117: }
118:
119: private static boolean getBooleanSetting(String key,
120: boolean defaultValue) {
121: String update = JavaPlugin.getDefault().getDialogSettings()
122: .get(key);
123: if (update != null)
124: return Boolean.valueOf(update).booleanValue();
125: else
126: return defaultValue;
127: }
128:
129: private static void saveBooleanSetting(String key, Button button) {
130: if (button != null && !button.isDisposed()
131: && button.getEnabled())
132: JavaPlugin.getDefault().getDialogSettings().put(key,
133: button.getSelection());
134: }
135: }
|