KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > dialogs > GenerateHashCodeEqualsDialog


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.dialogs;
12
13 import org.eclipse.core.runtime.IStatus;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Shell;
22
23 import org.eclipse.jface.viewers.ITreeContentProvider;
24 import org.eclipse.jface.viewers.Viewer;
25
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
28
29 import org.eclipse.jdt.core.IType;
30 import org.eclipse.jdt.core.JavaModelException;
31 import org.eclipse.jdt.core.dom.IVariableBinding;
32
33 import org.eclipse.jdt.internal.corext.util.Messages;
34
35 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
36 import org.eclipse.jdt.internal.ui.JavaUIMessages;
37 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
38 import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider;
39
40 /**
41  * Dialog for the generate hashCode() and equals() action.
42  *
43  * @since 3.2
44  */

45 public class GenerateHashCodeEqualsDialog extends SourceActionDialog {
46
47     /**
48      * Content provider for the generate hashCode() and equals() tree viewer.
49      *
50      * @since 3.2
51      *
52      */

53     private static class GenerateHashCodeEqualsContentProvider implements ITreeContentProvider {
54
55         IVariableBinding[] fBindings;
56
57         public GenerateHashCodeEqualsContentProvider(IVariableBinding[] allFields) {
58             this.fBindings= allFields;
59         }
60
61         public void dispose() {
62         }
63
64         public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
65             return new Object JavaDoc[0];
66         }
67
68         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
69             return fBindings;
70         }
71
72         public Object JavaDoc getParent(Object JavaDoc element) {
73             return null;
74         }
75
76         public boolean hasChildren(Object JavaDoc element) {
77             return false;
78         }
79
80         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
81         }
82
83     }
84
85     /**
86      * Validator for the input of the generate hashCode() and equals() dialog.
87      *
88      * @since 3.2
89      *
90      */

91     private static class GenerateHashCodeEqualsValidator implements ISelectionStatusValidator {
92
93         private static int fNumFields;
94
95         public GenerateHashCodeEqualsValidator(int entries) {
96             fNumFields= entries;
97         }
98
99         public IStatus validate(Object JavaDoc[] selection) {
100             int count= 0;
101             for (int index= 0; index < selection.length; index++) {
102                 if (selection[index] instanceof IVariableBinding)
103                     count++;
104             }
105             if (count == 0)
106                 return new StatusInfo(IStatus.ERROR, JavaUIMessages.GenerateHashCodeEqualsDialog_select_at_least_one_field);
107
108             return new StatusInfo(IStatus.INFO, Messages.format(JavaUIMessages.GenerateHashCodeEqualsDialog_selectioninfo_more, new String JavaDoc[] { String.valueOf(count), String.valueOf(fNumFields)}));
109         }
110     }
111
112     private static final String JavaDoc SETTINGS_INSTANCEOF= "InstanceOf"; //$NON-NLS-1$
113

114     private boolean fUseInstanceOf;
115
116     public GenerateHashCodeEqualsDialog(Shell shell, CompilationUnitEditor editor, IType type, IVariableBinding[] allFields, IVariableBinding[] selectedFields) throws JavaModelException {
117         super(shell, new BindingLabelProvider(), new GenerateHashCodeEqualsContentProvider(allFields), editor, type, false);
118         setEmptyListMessage(JavaUIMessages.GenerateHashCodeEqualsDialog_no_entries);
119
120         setInitialSelections(selectedFields);
121
122         setTitle(JavaUIMessages.GenerateHashCodeEqualsDialog_dialog_title);
123         setMessage(JavaUIMessages.GenerateHashCodeEqualsDialog_select_fields_to_include);
124         setValidator(new GenerateHashCodeEqualsValidator(allFields.length));
125         setSize(60, 18);
126         setInput(new Object JavaDoc());
127
128         fUseInstanceOf= asBoolean(getDialogSettings().get(SETTINGS_INSTANCEOF), false);
129     }
130
131     /**
132      * {@inheritDoc}
133      */

134     public boolean close() {
135         getDialogSettings().put(SETTINGS_INSTANCEOF, fUseInstanceOf);
136         return super.close();
137     }
138
139     protected void configureShell(Shell shell) {
140         super.configureShell(shell);
141         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, IJavaHelpContextIds.GENERATE_HASHCODE_EQUALS_SELECTION_DIALOG);
142     }
143
144     /**
145      * {@inheritDoc}
146      */

147     protected Composite createCommentSelection(final Composite parent) {
148         final Composite composite= super.createCommentSelection(parent);
149
150         Button button= new Button(composite, SWT.CHECK);
151         button.setText(JavaUIMessages.GenerateHashCodeEqualsDialog_instanceof_button);
152
153         button.addSelectionListener(new SelectionAdapter() {
154
155             public void widgetSelected(SelectionEvent event) {
156                 setUseInstanceOf((((Button) event.widget).getSelection()));
157             }
158         });
159         button.setSelection(isUseInstanceOf());
160         GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
161         data.horizontalSpan= 2;
162         button.setLayoutData(data);
163
164         return composite;
165     }
166
167     public boolean isUseInstanceOf() {
168         return fUseInstanceOf;
169     }
170
171     public void setUseInstanceOf(boolean use) {
172         fUseInstanceOf= use;
173     }
174 }
Popular Tags