KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > nls > RenameKeysDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 JavaDoc;
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 /**
26  *
27  */

28 public class RenameKeysDialog extends StatusDialog {
29
30     private StringDialogField fNameField;
31     private List JavaDoc fSelectedSubstitutions;
32     private int fCommonPrefixLength;
33     
34     /**
35      * @param parent
36      */

37     public RenameKeysDialog(Shell parent, List JavaDoc selectedSubstitutions) {
38         super(parent);
39         setTitle(NLSUIMessages.RenameKeysDialog_title);
40
41         fSelectedSubstitutions= selectedSubstitutions;
42         String JavaDoc 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     /* (non-Javadoc)
56      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
57      */

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     /* (non-Javadoc)
67      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
68      */

69     protected void okPressed() {
70         String JavaDoc prefix= fNameField.getText();
71         for (int i= 0; i < fSelectedSubstitutions.size(); i++) {
72             NLSSubstitution sub= (NLSSubstitution) fSelectedSubstitutions.get(i);
73             String JavaDoc newKey= prefix + sub.getKey().substring(fCommonPrefixLength);
74             sub.setKey(newKey);
75         }
76         super.okPressed();
77     }
78         
79     private String JavaDoc getInitialPrefix(List JavaDoc selectedSubstitutions) {
80         String JavaDoc prefix= null;
81         for (int i= 0; i < selectedSubstitutions.size(); i++) {
82             NLSSubstitution sub= (NLSSubstitution) selectedSubstitutions.get(i);
83             String JavaDoc 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 JavaDoc getCommonPrefix(String JavaDoc a, String JavaDoc b) {
97         String JavaDoc 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