KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > DelegateUIHelper


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.refactoring;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.SelectionAdapter;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.widgets.Button;
17 import org.eclipse.swt.widgets.Composite;
18
19 import org.eclipse.ltk.core.refactoring.Refactoring;
20
21 import org.eclipse.jdt.internal.corext.refactoring.tagging.IDelegateUpdating;
22
23 import org.eclipse.jdt.internal.ui.JavaPlugin;
24
25 /**
26  * This is a helper class to keep a consistent design between refactorings
27  * capable of creating delegates.
28  *
29  * @since 3.2
30  *
31  */

32 public class DelegateUIHelper {
33
34     public static Button generateDeprecateDelegateCheckbox(Composite parent, Refactoring refactoring) {
35         final IDelegateUpdating updating= (IDelegateUpdating) refactoring.getAdapter(IDelegateUpdating.class);
36         if (updating == null || !updating.canEnableDelegateUpdating())
37             return null;
38         final Button button= createCheckbox(parent, getDeprecateDelegateCheckBoxTitle(), loadDeprecateDelegateSetting(updating));
39         updating.setDeprecateDelegates(button.getSelection());
40         button.addSelectionListener(new SelectionAdapter() {
41
42             public void widgetSelected(SelectionEvent e) {
43                 updating.setDeprecateDelegates(button.getSelection());
44             }
45         });
46         return button;
47     }
48
49     public static Button generateLeaveDelegateCheckbox(Composite parent, Refactoring refactoring, boolean plural) {
50         final IDelegateUpdating updating= (IDelegateUpdating) refactoring.getAdapter(IDelegateUpdating.class);
51         if (updating == null || !updating.canEnableDelegateUpdating())
52             return null;
53         final Button button= createCheckbox(parent, updating.getDelegateUpdatingTitle(plural), loadLeaveDelegateSetting(updating));
54         updating.setDelegateUpdating(button.getSelection());
55         button.addSelectionListener(new SelectionAdapter() {
56
57             public void widgetSelected(SelectionEvent e) {
58                 updating.setDelegateUpdating(button.getSelection());
59             }
60         });
61         return button;
62     }
63
64     public static void saveLeaveDelegateSetting(Button button) {
65         saveBooleanSetting(DELEGATE_UPDATING, button);
66     }
67
68     public static void saveDeprecateDelegateSetting(Button button) {
69         saveBooleanSetting(DELEGATE_DEPRECATION, button);
70     }
71
72     public static boolean loadLeaveDelegateSetting(IDelegateUpdating refactoring) {
73         return getBooleanSetting(DELEGATE_UPDATING, refactoring.getDelegateUpdating());
74     }
75
76     public static boolean loadDeprecateDelegateSetting(IDelegateUpdating refactoring) {
77         return getBooleanSetting(DELEGATE_DEPRECATION, refactoring.getDeprecateDelegates());
78     }
79
80     public static String JavaDoc getDeprecateDelegateCheckBoxTitle() {
81         return RefactoringMessages.DelegateCreator_deprecate_delegates;
82     }
83
84     // ************** Helper methods *******************
85

86     /**
87      * Dialog settings key (value is of type boolean).
88      */

89     public static final String JavaDoc DELEGATE_UPDATING= "delegateUpdating"; //$NON-NLS-1$
90

91     /**
92      * Dialog settings key (value is of type boolean).
93      */

94     public static final String JavaDoc DELEGATE_DEPRECATION= "delegateDeprecation"; //$NON-NLS-1$
95

96     private DelegateUIHelper() {
97         // no instances
98
}
99
100     private static Button createCheckbox(Composite parent, String JavaDoc title, boolean value) {
101         Button checkBox= new Button(parent, SWT.CHECK);
102         checkBox.setText(title);
103         checkBox.setSelection(value);
104         return checkBox;
105     }
106
107     private static boolean getBooleanSetting(String JavaDoc key, boolean defaultValue) {
108         String JavaDoc update= JavaPlugin.getDefault().getDialogSettings().get(key);
109         if (update != null)
110             return Boolean.valueOf(update).booleanValue();
111         else
112             return defaultValue;
113     }
114
115     private static void saveBooleanSetting(String JavaDoc key, Button button) {
116         if (button != null && !button.isDisposed() && button.getEnabled())
117             JavaPlugin.getDefault().getDialogSettings().put(key, button.getSelection());
118     }
119 }
120
Popular Tags