Skip to main content
summaryrefslogtreecommitdiffstats
blob: 168b618592e45f26fa52f9c3ec13b73e58dd8cfd (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
/*******************************************************************************
 * 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 org.eclipse.babel.core.message.internal.MessagesBundleGroup;
import org.eclipse.babel.core.message.tree.internal.KeyTreeNode;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.RefactoringContribution;
import org.eclipse.ltk.core.refactoring.RefactoringCore;
import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;

/**
 * Refactoring descriptor for the rename resource bundle key refactoring.
 * <p>
 * An instance of this refactoring descriptor may be obtained by calling
 * {@link RefactoringContribution#createDescriptor()} on a refactoring
 * contribution requested by invoking
 * {@link RefactoringCore#getRefactoringContribution(String)} with the
 * refactoring id ({@link #ID}).
 */
public final class RenameKeyDescriptor extends RefactoringDescriptor {

    public static final String ID = "org.eclipse.babel.editor.refactoring.renameKey"; //$NON-NLS-1$

    /** The name attribute */
    private String fNewName;

    private KeyTreeNode fKeyNode;

    private MessagesBundleGroup fMessagesBundleGroup;

    /** Configures if references will be updated */
    private boolean fRenameChildKeys;

    /**
     * Creates a new refactoring descriptor.
     * <p>
     * Clients should not instantiated this class but use
     * {@link RefactoringCore#getRefactoringContribution(String)} with
     * {@link #ID} to get the contribution that can create the descriptor.
     * </p>
     */
    public RenameKeyDescriptor() {
        super(ID, null, "N/A", null, RefactoringDescriptor.STRUCTURAL_CHANGE
                | RefactoringDescriptor.MULTI_CHANGE);
        fNewName = null;
    }

    /**
     * Sets the new name to rename the resource to.
     * 
     * @param name
     *            the non-empty new name to set
     */
    public void setNewName(final String name) {
        Assert.isNotNull(name);
        Assert.isLegal(!"".equals(name), "Name must not be empty"); //$NON-NLS-1$//$NON-NLS-2$
        fNewName = name;
    }

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

    /**
     * Sets the project name of this refactoring.
     * <p>
     * Note: If the resource to be renamed is of type {@link IResource#PROJECT},
     * clients are required to to set the project name to <code>null</code>.
     * </p>
     * <p>
     * The default is to associate the refactoring with the workspace.
     * </p>
     * 
     * @param project
     *            the non-empty project name to set, or <code>null</code> for
     *            the workspace
     * 
     * @see #getProject()
     */
    // public void setProject(final String project) {
    // super.setProject(project);
    // }

    /**
     * If set to <code>true</code>, this rename will also rename child keys. The
     * default is to rename child keys.
     * 
     * @param renameChildKeys
     *            <code>true</code> if this rename will rename child keys
     */
    public void setRenameChildKeys(boolean renameChildKeys) {
        fRenameChildKeys = renameChildKeys;
    }

    public void setRenameChildKeys(KeyTreeNode keyNode,
            MessagesBundleGroup messagesBundleGroup) {
        this.fKeyNode = keyNode;
        this.fMessagesBundleGroup = messagesBundleGroup;
    }

    /**
     * Returns if this rename will also rename child keys
     * 
     * @return returns <code>true</code> if this rename will rename child keys
     */
    public boolean isRenameChildKeys() {
        return fRenameChildKeys;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ltk.core.refactoring.RefactoringDescriptor#createRefactoring
     * (org.eclipse.ltk.core.refactoring.RefactoringStatus)
     */
    public Refactoring createRefactoring(RefactoringStatus status)
            throws CoreException {

        String newName = getNewName();
        if (newName == null || newName.length() == 0) {
            status.addFatalError("The rename resource bundle key refactoring can not be performed as the new name is invalid");
            return null;
        }
        RenameKeyProcessor processor = new RenameKeyProcessor(fKeyNode,
                fMessagesBundleGroup);
        processor.setNewResourceName(newName);
        processor.setRenameChildKeys(fRenameChildKeys);

        return new RenameRefactoring(processor);
    }
}

Back to the top