1 11 package org.eclipse.jdt.internal.ui.refactoring.code; 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.layout.GridData; 17 import org.eclipse.swt.layout.GridLayout; 18 import org.eclipse.swt.widgets.Button; 19 import org.eclipse.swt.widgets.Composite; 20 import org.eclipse.swt.widgets.Control; 21 import org.eclipse.swt.widgets.Label; 22 23 import org.eclipse.jface.dialogs.Dialog; 24 import org.eclipse.jface.preference.IPreferenceStore; 25 import org.eclipse.jface.resource.JFaceResources; 26 27 import org.eclipse.jface.text.Document; 28 import org.eclipse.jface.text.DocumentEvent; 29 import org.eclipse.jface.text.IDocumentListener; 30 31 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 32 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage; 33 34 import org.eclipse.jdt.core.JavaModelException; 35 36 import org.eclipse.jdt.internal.corext.refactoring.code.ReplaceInvocationsRefactoring; 37 38 import org.eclipse.jdt.ui.JavaElementLabels; 39 import org.eclipse.jdt.ui.PreferenceConstants; 40 import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration; 41 42 import org.eclipse.jdt.internal.ui.JavaPlugin; 43 import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer; 44 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 45 import org.eclipse.jdt.internal.ui.util.PixelConverter; 46 47 public class ReplaceInvocationsInputPage extends UserInputWizardPage { 48 49 public static final String PAGE_NAME= "ReplaceInvocationsInputPage"; 51 private ReplaceInvocationsRefactoring fRefactoring; 52 53 private static final long LABEL_FLAGS= JavaElementLabels.M_PRE_TYPE_PARAMETERS | JavaElementLabels.M_PRE_RETURNTYPE | JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES | JavaElementLabels.M_EXCEPTIONS; 54 55 public ReplaceInvocationsInputPage() { 56 super(PAGE_NAME); 57 } 58 59 public void createControl(Composite parent) { 60 initializeDialogUnits(parent); 61 fRefactoring= (ReplaceInvocationsRefactoring) getRefactoring(); 62 63 Composite result= new Composite(parent, SWT.NONE); 64 setControl(result); 65 GridLayout layout= new GridLayout(); 66 result.setLayout(layout); 67 68 createMethodSignature(result); 69 70 Label separator= new Label(parent, SWT.NONE); 71 GridData gridData= new GridData(SWT.FILL, SWT.FILL, false, false); 72 gridData.heightHint= 5; 73 separator.setLayoutData(gridData); 74 75 Label bodyLabel= new Label(result, SWT.NONE); 76 bodyLabel.setText(RefactoringMessages.ReplaceInvocationsInputPage_replaceInvocationsBy); 77 78 createBody(result); 79 80 Button replaceAll= new Button(result, SWT.CHECK); 81 replaceAll.setText(RefactoringMessages.ReplaceInvocationsInputPage_replaceAll); 82 boolean canSingle= fRefactoring.canReplaceSingle(); 83 replaceAll.setEnabled(false); replaceAll.setSelection(! canSingle); 86 replaceAll.addSelectionListener(new SelectionAdapter() { 87 public void widgetSelected(SelectionEvent event) { 88 boolean all= ((Button) event.widget).getSelection(); 89 changeMode(all ? ReplaceInvocationsRefactoring.Mode.REPLACE_ALL : ReplaceInvocationsRefactoring.Mode.REPLACE_SINGLE); 90 } 91 }); 92 93 Dialog.applyDialogFont(result); 94 } 95 96 private void createMethodSignature(Composite parent) { 97 IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore(); 98 JavaSourceViewer signatureViewer= new JavaSourceViewer(parent, null, null, false, SWT.READ_ONLY | SWT.WRAP , store); 99 signatureViewer.configure(new JavaSourceViewerConfiguration(JavaPlugin.getDefault().getJavaTextTools().getColorManager(), store, null, null)); 100 signatureViewer.getTextWidget().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT)); 101 signatureViewer.getTextWidget().setBackground(parent.getBackground()); 102 String signatureLabel= JavaElementLabels.getElementLabel(fRefactoring.getMethod(), LABEL_FLAGS); 103 signatureViewer.setDocument(new Document(signatureLabel)); 104 signatureViewer.setEditable(false); 105 106 Control signatureControl= signatureViewer.getControl(); 107 PixelConverter pixelConverter= new PixelConverter(signatureControl); 108 GridData gdata= new GridData(GridData.FILL_HORIZONTAL); 109 gdata.widthHint= pixelConverter.convertWidthInCharsToPixels(50); 110 signatureControl.setLayoutData(gdata); 111 } 112 113 private void createBody(Composite parent) { 114 IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore(); 115 JavaSourceViewer bodyEditor= new JavaSourceViewer(parent, null, null, false, SWT.V_SCROLL | SWT.WRAP | SWT.BORDER, store); 116 bodyEditor.configure(new JavaSourceViewerConfiguration(JavaPlugin.getDefault().getJavaTextTools().getColorManager(), store, null, null)); 117 bodyEditor.getTextWidget().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT)); 118 Document bodyDocument= new Document(getInitialBody()); 119 bodyEditor.setDocument(bodyDocument); 120 bodyEditor.setEditable(true); 121 122 Control bodyControl= bodyEditor.getControl(); 123 PixelConverter pixelConverter= new PixelConverter(bodyControl); 124 GridData gdata= new GridData(GridData.FILL_BOTH); 125 gdata.widthHint= pixelConverter.convertWidthInCharsToPixels(50); 126 gdata.minimumHeight= pixelConverter.convertHeightInCharsToPixels(5); 127 bodyControl.setLayoutData(gdata); 128 bodyControl.setFocus(); 129 130 bodyDocument.addDocumentListener(new IDocumentListener() { 131 public void documentAboutToBeChanged(DocumentEvent event) { 132 } 133 public void documentChanged(DocumentEvent event) { 134 try { 135 fRefactoring.setBody(event.getDocument().get(), fRefactoring.getMethod().getParameterNames()); 136 } catch (JavaModelException ex) { 137 JavaPlugin.log(ex); 139 } 140 } 141 }); 142 } 143 144 private String getInitialBody() { 145 return ""; 148 } 149 150 private void changeMode(ReplaceInvocationsRefactoring.Mode mode) { 151 RefactoringStatus status; 152 try { 153 status= fRefactoring.setCurrentMode(mode); 154 } catch (JavaModelException e) { 155 status= RefactoringStatus.createFatalErrorStatus(e.getMessage()); 156 } 157 setPageComplete(status); 158 } 159 } 160 | Popular Tags |