1 11 package org.eclipse.jdt.internal.ui.refactoring.nls; 12 13 import java.util.List ; 14 15 import org.eclipse.swt.widgets.Composite; 16 import org.eclipse.swt.widgets.Control; 17 import org.eclipse.swt.widgets.Shell; 18 19 import org.eclipse.jface.dialogs.StatusDialog; 20 21 import org.eclipse.jdt.internal.corext.refactoring.nls.NLSSubstitution; 22 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; 23 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField; 24 25 28 public class RenameKeysDialog extends StatusDialog { 29 30 private StringDialogField fNameField; 31 private List fSelectedSubstitutions; 32 private int fCommonPrefixLength; 33 34 37 public RenameKeysDialog(Shell parent, List selectedSubstitutions) { 38 super(parent); 39 setTitle(NLSUIMessages.RenameKeysDialog_title); 40 41 fSelectedSubstitutions= selectedSubstitutions; 42 String prefix= getInitialPrefix(selectedSubstitutions); 43 fCommonPrefixLength= prefix.length(); 44 45 fNameField= new StringDialogField(); 46 fNameField.setText(prefix); 47 48 if (prefix.length() == 0) { 49 fNameField.setLabelText(NLSUIMessages.RenameKeysDialog_description_noprefix); 50 } else { 51 fNameField.setLabelText(NLSUIMessages.RenameKeysDialog_description_withprefix + prefix + ':'); 52 } 53 } 54 55 58 protected Control createDialogArea(Composite parent) { 59 Composite composite= (Composite) super.createDialogArea(parent); 60 61 fNameField.doFillIntoGrid(composite, 2); 62 LayoutUtil.setHorizontalGrabbing(fNameField.getTextControl(null)); 63 return composite; 64 } 65 66 69 protected void okPressed() { 70 String prefix= fNameField.getText(); 71 for (int i= 0; i < fSelectedSubstitutions.size(); i++) { 72 NLSSubstitution sub= (NLSSubstitution) fSelectedSubstitutions.get(i); 73 String newKey= prefix + sub.getKey().substring(fCommonPrefixLength); 74 sub.setKey(newKey); 75 } 76 super.okPressed(); 77 } 78 79 private String getInitialPrefix(List selectedSubstitutions) { 80 String prefix= null; 81 for (int i= 0; i < selectedSubstitutions.size(); i++) { 82 NLSSubstitution sub= (NLSSubstitution) selectedSubstitutions.get(i); 83 String curr= sub.getKey(); 84 if (prefix == null) { 85 prefix= curr; 86 } else if (!curr.startsWith(prefix)) { 87 prefix= getCommonPrefix(prefix, curr); 88 if (prefix.length() == 0) { 89 return prefix; 90 } 91 } 92 } 93 return prefix; 94 } 95 96 private String getCommonPrefix(String a, String b) { 97 String shorter= a.length() <= b.length() ? a : b; 98 int len= shorter.length(); 99 for (int i= 0; i < len; i++) { 100 if (a.charAt(i) != b.charAt(i)) { 101 return a.substring(0, i); 102 } 103 } 104 return shorter; 105 } 106 107 108 } 109 | Popular Tags |