1 /******************************************************************************* 2 * Copyright (c) 2000, 2012 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.jdt.internal.ui.refactoring.code; 15 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.events.SelectionAdapter; 18 import org.eclipse.swt.events.SelectionEvent; 19 import org.eclipse.swt.layout.GridData; 20 import org.eclipse.swt.layout.GridLayout; 21 import org.eclipse.swt.widgets.Button; 22 import org.eclipse.swt.widgets.Composite; 23 import org.eclipse.swt.widgets.Control; 24 import org.eclipse.swt.widgets.Label; 25 26 import org.eclipse.jface.dialogs.Dialog; 27 import org.eclipse.jface.layout.PixelConverter; 28 import org.eclipse.jface.preference.IPreferenceStore; 29 import org.eclipse.jface.resource.JFaceResources; 30 31 import org.eclipse.jface.text.Document; 32 import org.eclipse.jface.text.DocumentEvent; 33 import org.eclipse.jface.text.IDocumentListener; 34 35 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 36 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage; 37 38 import org.eclipse.jdt.core.JavaModelException; 39 40 import org.eclipse.jdt.internal.corext.refactoring.code.ReplaceInvocationsRefactoring; 41 42 import org.eclipse.jdt.ui.JavaElementLabels; 43 import org.eclipse.jdt.ui.PreferenceConstants; 44 import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration; 45 46 import org.eclipse.jdt.internal.ui.JavaPlugin; 47 import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer; 48 import org.eclipse.jdt.internal.ui.refactoring.InputPageUtil; 49 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 50 51 52 public class ReplaceInvocationsInputPage extends UserInputWizardPage { 53 54 public static final String PAGE_NAME= "ReplaceInvocationsInputPage";//$NON-NLS-1$ 55 56 private ReplaceInvocationsRefactoring fRefactoring; 57 58 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; 59 ReplaceInvocationsInputPage()60 public ReplaceInvocationsInputPage() { 61 super(PAGE_NAME); 62 } 63 64 @Override createControl(Composite parent)65 public void createControl(Composite parent) { 66 initializeDialogUnits(parent); 67 fRefactoring= (ReplaceInvocationsRefactoring) getRefactoring(); 68 69 Composite result= new Composite(parent, SWT.NONE); 70 setControl(result); 71 GridLayout layout= new GridLayout(); 72 result.setLayout(layout); 73 74 createMethodSignature(result); 75 76 Label separator= new Label(parent, SWT.NONE); 77 GridData gridData= new GridData(SWT.FILL, SWT.FILL, false, false); 78 gridData.heightHint= 5; 79 separator.setLayoutData(gridData); 80 81 Label bodyLabel= new Label(result, SWT.NONE); 82 bodyLabel.setText(RefactoringMessages.ReplaceInvocationsInputPage_replaceInvocationsBy); 83 84 createBody(result); 85 86 Button replaceAll= new Button(result, SWT.CHECK); 87 replaceAll.setText(RefactoringMessages.ReplaceInvocationsInputPage_replaceAll); 88 boolean canSingle= fRefactoring.canReplaceSingle(); 89 // replaceAll.setEnabled(canSingle); 90 replaceAll.setEnabled(false); // does not work for now... 91 replaceAll.setSelection(! canSingle); 92 replaceAll.addSelectionListener(new SelectionAdapter() { 93 @Override 94 public void widgetSelected(SelectionEvent event) { 95 boolean all= ((Button) event.widget).getSelection(); 96 changeMode(all ? ReplaceInvocationsRefactoring.Mode.REPLACE_ALL : ReplaceInvocationsRefactoring.Mode.REPLACE_SINGLE); 97 } 98 }); 99 100 Dialog.applyDialogFont(result); 101 } 102 createMethodSignature(Composite parent)103 private void createMethodSignature(Composite parent) { 104 JavaSourceViewer signatureViewer= InputPageUtil.createSignaturePreview(parent); 105 String signatureLabel= JavaElementLabels.getElementLabel(fRefactoring.getMethod(), LABEL_FLAGS); 106 signatureViewer.getDocument().set(signatureLabel); 107 } 108 createBody(Composite parent)109 private void createBody(Composite parent) { 110 IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore(); 111 JavaSourceViewer bodyEditor= new JavaSourceViewer(parent, null, null, false, SWT.V_SCROLL | SWT.WRAP | SWT.BORDER, store); 112 bodyEditor.configure(new JavaSourceViewerConfiguration(JavaPlugin.getDefault().getJavaTextTools().getColorManager(), store, null, null)); 113 bodyEditor.getTextWidget().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT)); 114 Document bodyDocument= new Document(getInitialBody()); 115 bodyEditor.setDocument(bodyDocument); 116 bodyEditor.setEditable(true); 117 118 Control bodyControl= bodyEditor.getControl(); 119 PixelConverter pixelConverter= new PixelConverter(bodyControl); 120 GridData gdata= new GridData(GridData.FILL_BOTH); 121 gdata.widthHint= pixelConverter.convertWidthInCharsToPixels(50); 122 gdata.minimumHeight= pixelConverter.convertHeightInCharsToPixels(5); 123 bodyControl.setLayoutData(gdata); 124 bodyControl.setFocus(); 125 126 bodyDocument.addDocumentListener(new IDocumentListener() { 127 @Override 128 public void documentAboutToBeChanged(DocumentEvent event) { 129 } 130 @Override 131 public void documentChanged(DocumentEvent event) { 132 try { 133 fRefactoring.setBody(event.getDocument().get(), fRefactoring.getMethod().getParameterNames()); 134 } catch (JavaModelException ex) { 135 // TODO Auto-generated catch block 136 JavaPlugin.log(ex); 137 } 138 } 139 }); 140 } 141 getInitialBody()142 private String getInitialBody() { 143 //TODO 144 return ""; //$NON-NLS-1$ 145 146 } 147 changeMode(ReplaceInvocationsRefactoring.Mode mode)148 private void changeMode(ReplaceInvocationsRefactoring.Mode mode) { 149 RefactoringStatus status; 150 try { 151 status= fRefactoring.setCurrentMode(mode); 152 } catch (JavaModelException e) { 153 status= RefactoringStatus.createFatalErrorStatus(e.getMessage()); 154 } 155 setPageComplete(status); 156 } 157 } 158