]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/nls/RenameKeysDialog.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui refactoring / org / eclipse / jdt / internal / ui / refactoring / nls / RenameKeysDialog.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 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 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.refactoring.nls;
12
13import java.util.List;
14
15import org.eclipse.swt.widgets.Composite;
16import org.eclipse.swt.widgets.Control;
17import org.eclipse.swt.widgets.Shell;
18
19import org.eclipse.jface.dialogs.StatusDialog;
20
21import org.eclipse.jdt.internal.corext.refactoring.nls.NLSSubstitution;
22
23import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
24import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
25
26/**
27 *
28 */
29public class RenameKeysDialog extends StatusDialog {
30
31 private StringDialogField fNameField;
32 private List<NLSSubstitution> fSelectedSubstitutions;
33 private int fCommonPrefixLength;
34
35 /**
36 * @param parent
37 */
38 public RenameKeysDialog(Shell parent, List<NLSSubstitution> selectedSubstitutions) {
39 super(parent);
40 setTitle(NLSUIMessages.RenameKeysDialog_title);
41
42 fSelectedSubstitutions= selectedSubstitutions;
43 String prefix= getInitialPrefix(selectedSubstitutions);
44 fCommonPrefixLength= prefix.length();
45
46 fNameField= new StringDialogField();
47 fNameField.setText(prefix);
48
49 if (prefix.length() == 0) {
50 fNameField.setLabelText(NLSUIMessages.RenameKeysDialog_description_noprefix);
51 } else {
52 fNameField.setLabelText(NLSUIMessages.RenameKeysDialog_description_withprefix + prefix + ':');
53 }
54 }
55
56 /* (non-Javadoc)
57 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
58 */
59 @Override
60 protected Control createDialogArea(Composite parent) {
61 Composite composite= (Composite) super.createDialogArea(parent);
62
63 fNameField.doFillIntoGrid(composite, 2);
64 LayoutUtil.setHorizontalGrabbing(fNameField.getTextControl(null));
65 return composite;
66 }
67
68 /* (non-Javadoc)
69 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
70 */
71 @Override
72 protected void okPressed() {
73 String prefix= fNameField.getText();
74 for (int i= 0; i < fSelectedSubstitutions.size(); i++) {
75 NLSSubstitution sub= fSelectedSubstitutions.get(i);
76 String newKey= prefix + sub.getKey().substring(fCommonPrefixLength);
77 sub.setKey(newKey);
78 }
79 super.okPressed();
80 }
81
82 private String getInitialPrefix(List<NLSSubstitution> selectedSubstitutions) {
83 String prefix= null;
84 for (int i= 0; i < selectedSubstitutions.size(); i++) {
85 NLSSubstitution sub= selectedSubstitutions.get(i);
86 String curr= sub.getKey();
87 if (prefix == null) {
88 prefix= curr;
89 } else if (!curr.startsWith(prefix)) {
90 prefix= getCommonPrefix(prefix, curr);
91 if (prefix.length() == 0) {
92 return prefix;
93 }
94 }
95 }
96 return prefix;
97 }
98
99 private String getCommonPrefix(String a, String b) {
100 String shorter= a.length() <= b.length() ? a : b;
101 int len= shorter.length();
102 for (int i= 0; i < len; i++) {
103 if (a.charAt(i) != b.charAt(i)) {
104 return a.substring(0, i);
105 }
106 }
107 return shorter;
108 }
109
110
111}