Skip to main content
summaryrefslogtreecommitdiffstats
blob: cd4736902328da53d67829717d50316f16d297b4 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*******************************************************************************
 * Copyright (c) 2007 Pascal Essiembre.
 * 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:
 *    Pascal Essiembre - initial API and implementation
 ******************************************************************************/
package org.eclipse.babel.editor.internal;

import org.eclipse.babel.editor.IMessagesEditorChangeListener;
import org.eclipse.babel.editor.actions.FilterKeysAction;
import org.eclipse.babel.editor.actions.KeyTreeVisibleAction;
import org.eclipse.babel.editor.actions.NewLocaleAction;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionGroup;
import org.eclipse.ui.ide.IDEActionFactory;
import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;

/**
 * Manages the installation/deinstallation of global actions for multi-page
 * editors. Responsible for the redirection of global actions to the active
 * editor. Multi-page contributor replaces the contributors for the individual
 * editors in the multi-page editor.
 */
public class MessagesEditorContributor extends
        MultiPageEditorActionBarContributor {
    private IEditorPart activeEditorPart;

    private KeyTreeVisibleAction toggleKeyTreeAction;
    private NewLocaleAction newLocaleAction;
    private IMenuManager resourceBundleMenu;
    private static String resourceBundleMenuID;

    /** singleton: probably not the cleanest way to access it from anywhere. */
    public static ActionGroup FILTERS = new FilterKeysActionGroup();

    /**
     * Creates a multi-page contributor.
     */
    public MessagesEditorContributor() {
        super();
        createActions();
    }

    /**
     * Returns the action registed with the given text editor.
     * 
     * @param editor
     *            eclipse text editor
     * @param actionID
     *            action id
     * @return IAction or null if editor is null.
     */
    protected IAction getAction(ITextEditor editor, String actionID) {
        return (editor == null ? null : editor.getAction(actionID));
    }

    /**
     * @see MultiPageEditorActionBarContributor
     *      #setActivePage(org.eclipse.ui.IEditorPart)
     */
    public void setActivePage(IEditorPart part) {
        if (activeEditorPart == part) {
            return;
        }

        activeEditorPart = part;

        IActionBars actionBars = getActionBars();
        if (actionBars != null) {

            ITextEditor editor = (part instanceof ITextEditor) ? (ITextEditor) part
                    : null;

            actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(),
                    getAction(editor, ITextEditorActionConstants.DELETE));
            actionBars.setGlobalActionHandler(ActionFactory.UNDO.getId(),
                    getAction(editor, ITextEditorActionConstants.UNDO));
            actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(),
                    getAction(editor, ITextEditorActionConstants.REDO));
            actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(),
                    getAction(editor, ITextEditorActionConstants.CUT));
            actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(),
                    getAction(editor, ITextEditorActionConstants.COPY));
            actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(),
                    getAction(editor, ITextEditorActionConstants.PASTE));
            actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(),
                    getAction(editor, ITextEditorActionConstants.SELECT_ALL));
            actionBars.setGlobalActionHandler(ActionFactory.FIND.getId(),
                    getAction(editor, ITextEditorActionConstants.FIND));
            actionBars.setGlobalActionHandler(
                    IDEActionFactory.BOOKMARK.getId(),
                    getAction(editor, IDEActionFactory.BOOKMARK.getId()));
            actionBars.updateActionBars();
        }
    }

    private void createActions() {
        // sampleAction = new Action() {
        // public void run() {
        // MessageDialog.openInformation(null,
        // "ResourceBundle Editor Plug-in", "Sample Action Executed");
        // }
        // };
        // sampleAction.setText("Sample Action");
        // sampleAction.setToolTipText("Sample Action tool tip");
        // sampleAction.setImageDescriptor(
        // PlatformUI.getWorkbench().getSharedImages().
        // getImageDescriptor(IDE.SharedImages.IMG_OBJS_TASK_TSK));

        toggleKeyTreeAction = new KeyTreeVisibleAction();
        newLocaleAction = new NewLocaleAction();

        // toggleKeyTreeAction.setText("Show/Hide Key Tree");
        // toggleKeyTreeAction.setToolTipText("Click to show/hide the key tree");
        // toggleKeyTreeAction.setImageDescriptor(
        // PlatformUI.getWorkbench().getSharedImages().
        // getImageDescriptor(IDE.SharedImages.IMG_OPEN_MARKER));

    }

    /**
     * @see org.eclipse.ui.part.EditorActionBarContributor
     *      #contributeToMenu(org.eclipse.jface.action.IMenuManager)
     */
    public void contributeToMenu(IMenuManager manager) {
        // System.out.println("active editor part:" +activeEditorPart);
        // System.out.println("menu editor:" + rbEditor);
        resourceBundleMenu = new MenuManager("&Messages", resourceBundleMenuID);
        manager.prependToGroup(IWorkbenchActionConstants.M_EDIT,
                resourceBundleMenu);
        resourceBundleMenu.add(toggleKeyTreeAction);
        resourceBundleMenu.add(newLocaleAction);

        FILTERS.fillContextMenu(resourceBundleMenu);
    }

    /**
     * @see org.eclipse.ui.part.EditorActionBarContributor
     *      #contributeToToolBar(org.eclipse.jface.action.IToolBarManager)
     */
    public void contributeToToolBar(IToolBarManager manager) {
        // System.out.println("toolbar get page:" + getPage());
        // System.out.println("toolbar editor:" + rbEditor);
        manager.add(new Separator());
        // manager.add(sampleAction);
        manager.add(toggleKeyTreeAction);
        manager.add(newLocaleAction);

        ((FilterKeysActionGroup) FILTERS).fillActionBars(getActionBars());
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ui.part.MultiPageEditorActionBarContributor#setActiveEditor
     * (org.eclipse.ui.IEditorPart)
     */
    public void setActiveEditor(IEditorPart part) {
        super.setActiveEditor(part);
        AbstractMessagesEditor me = part instanceof AbstractMessagesEditor ? (AbstractMessagesEditor) part
                : null;
        toggleKeyTreeAction.setEditor(me);
        ((FilterKeysActionGroup) FILTERS).setActiveEditor(part);
        newLocaleAction.setEditor(me);
    }

    /**
     * Groups the filter actions together.
     */
    private static class FilterKeysActionGroup extends ActionGroup {
        FilterKeysAction[] filtersAction = new FilterKeysAction[4];

        public FilterKeysActionGroup() {
            filtersAction[0] = new FilterKeysAction(
                    IMessagesEditorChangeListener.SHOW_ALL);
            filtersAction[1] = new FilterKeysAction(
                    IMessagesEditorChangeListener.SHOW_ONLY_MISSING);
            filtersAction[2] = new FilterKeysAction(
                    IMessagesEditorChangeListener.SHOW_ONLY_MISSING_AND_UNUSED);
            filtersAction[3] = new FilterKeysAction(
                    IMessagesEditorChangeListener.SHOW_ONLY_UNUSED);
        }

        public void fillActionBars(IActionBars actionBars) {
            for (int i = 0; i < filtersAction.length; i++) {
                actionBars.getToolBarManager().add(filtersAction[i]);
            }
        }

        public void fillContextMenu(IMenuManager menu) {
            MenuManager filters = new MenuManager("Filters");
            for (int i = 0; i < filtersAction.length; i++) {
                filters.add(filtersAction[i]);
            }
            menu.add(filters);
        }

        public void updateActionBars() {
            for (int i = 0; i < filtersAction.length; i++) {
                filtersAction[i].update();
            }
        }

        public void setActiveEditor(IEditorPart part) {
            AbstractMessagesEditor me = part instanceof AbstractMessagesEditor ? (AbstractMessagesEditor) part
                    : null;
            for (int i = 0; i < filtersAction.length; i++) {
                filtersAction[i].setEditor(me);
            }
        }
    }

}

Back to the top