1 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 45 public class GenerateHashCodeEqualsDialog extends SourceActionDialog { 46 47 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 [] getChildren(Object parentElement) { 65 return new Object [0]; 66 } 67 68 public Object [] getElements(Object inputElement) { 69 return fBindings; 70 } 71 72 public Object getParent(Object element) { 73 return null; 74 } 75 76 public boolean hasChildren(Object element) { 77 return false; 78 } 79 80 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 81 } 82 83 } 84 85 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 [] 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 [] { String.valueOf(count), String.valueOf(fNumFields)})); 109 } 110 } 111 112 private static final String SETTINGS_INSTANCEOF= "InstanceOf"; 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 ()); 127 128 fUseInstanceOf= asBoolean(getDialogSettings().get(SETTINGS_INSTANCEOF), false); 129 } 130 131 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 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 |