]>
Commit | Line | Data |
---|---|---|
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 | *******************************************************************************/ | |
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 | ||
23 | import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; | |
24 | import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField; | |
25 | ||
26 | /** | |
27 | * | |
28 | */ | |
29 | public 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 | } |