Skip to main content
summaryrefslogtreecommitdiffstats
blob: f30272eb83ea979cc53eb885e2a1745b948b8676 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*******************************************************************************
 * Copyright (c) 2009 Nigel Westbury
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Nigel Westbury - initial implementation
 ******************************************************************************/
package org.eclipse.babel.editor.refactoring;

import java.text.MessageFormat;
import java.util.Collection;

import org.eclipse.babel.core.message.internal.MessagesBundleGroup;
import org.eclipse.babel.core.message.tree.internal.KeyTreeNode;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.ChangeDescriptor;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;

/**
 * {@link Change} that renames a resource bundle key.
 */
public class RenameKeyChange extends Change {

    private final MessagesBundleGroup fMessagesBundleGroup;

    private final String fNewName;

    private final boolean fRenameChildKeys;

    private final KeyTreeNode fKeyTreeNode;

    private ChangeDescriptor fDescriptor;

    /**
     * Creates the change.
     * 
     * @param keyTreeNode
     *            the node in the model to rename
     * @param newName
     *            the new name. Must not be empty
     * @param renameChildKeys
     *            true if child keys are also to be renamed, false if just this
     *            one key is to be renamed
     */
    protected RenameKeyChange(MessagesBundleGroup messageBundleGroup,
            KeyTreeNode keyTreeNode, String newName, boolean renameChildKeys) {
        if (keyTreeNode == null || newName == null || newName.length() == 0) {
            throw new IllegalArgumentException();
        }

        fMessagesBundleGroup = messageBundleGroup;
        fKeyTreeNode = keyTreeNode;
        fNewName = newName;
        fRenameChildKeys = renameChildKeys;
        fDescriptor = null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ltk.core.refactoring.Change#getDescriptor()
     */
    public ChangeDescriptor getDescriptor() {
        return fDescriptor;
    }

    /**
     * Sets the change descriptor to be returned by
     * {@link Change#getDescriptor()}.
     * 
     * @param descriptor
     *            the change descriptor
     */
    public void setDescriptor(ChangeDescriptor descriptor) {
        fDescriptor = descriptor;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ltk.core.refactoring.Change#getName()
     */
    public String getName() {
        return MessageFormat.format("Rename {0} to {1}", new Object[] {
                fKeyTreeNode.getMessageKey(), fNewName });
    }

    /**
     * Returns the new name.
     * 
     * @return return the new name
     */
    public String getNewName() {
        return fNewName;
    }

    /**
     * This implementation of {@link Change#isValid(IProgressMonitor)} tests the
     * modified resource using the validation method specified by
     * {@link #setValidationMethod(int)}.
     */
    public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException,
            OperationCanceledException {
        pm.beginTask("", 2); //$NON-NLS-1$
        try {
            RefactoringStatus result = new RefactoringStatus();
            return result;
        } finally {
            pm.done();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ltk.core.refactoring.Change#initializeValidationData(org.
     * eclipse.core.runtime.IProgressMonitor)
     */
    public void initializeValidationData(IProgressMonitor pm) {
    }

    public Object getModifiedElement() {
        return "what is this for?";
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ltk.core.refactoring.Change#perform(org.eclipse.core.runtime
     * .IProgressMonitor)
     */
    public Change perform(IProgressMonitor pm) throws CoreException {
        try {
            pm.beginTask("Rename resource bundle key", 1);

            // Find the root - we will need this later
            KeyTreeNode root = (KeyTreeNode) fKeyTreeNode.getParent();
            while (root.getName() != null) {
                root = (KeyTreeNode) root.getParent();
            }

            if (fRenameChildKeys) {
                String key = fKeyTreeNode.getMessageKey();
                String keyPrefix = fKeyTreeNode.getMessageKey() + ".";
                Collection<KeyTreeNode> branchNodes = fKeyTreeNode.getBranch();
                for (KeyTreeNode branchNode : branchNodes) {
                    String oldKey = branchNode.getMessageKey();
                    if (oldKey.equals(key) || oldKey.startsWith(keyPrefix)) {
                        String newKey = fNewName
                                + oldKey.substring(key.length());
                        fMessagesBundleGroup.renameMessageKeys(oldKey, newKey);
                    }
                }
            } else {
                fMessagesBundleGroup.renameMessageKeys(
                        fKeyTreeNode.getMessageKey(), fNewName);
            }

            String oldName = fKeyTreeNode.getMessageKey();

            // Find the node that was created with the new name
            String segments[] = fNewName.split("\\.");
            KeyTreeNode renamedKey = root;
            for (String segment : segments) {
                renamedKey = (KeyTreeNode) renamedKey.getChild(segment);
            }

            assert (renamedKey != null);
            return new RenameKeyChange(fMessagesBundleGroup, renamedKey,
                    oldName, fRenameChildKeys);
        } finally {
            pm.done();
        }
    }
}

Back to the top