Skip to main content
summaryrefslogtreecommitdiffstats
blob: c6a8bf1e2bb570cd52a0592e5640c5d6bbed9ba5 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*******************************************************************************
 * 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
 *    Alexej Strelzow - TapiJI integration, fixed issues 37, 48
 ******************************************************************************/
package org.eclipse.babel.editor.i18n;

import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.eclipse.babel.core.message.IMessagesBundle;
import org.eclipse.babel.core.message.manager.IMessagesEditorListener;
import org.eclipse.babel.core.message.manager.RBManager;
import org.eclipse.babel.editor.IMessagesEditorChangeListener;
import org.eclipse.babel.editor.internal.MessagesEditor;
import org.eclipse.babel.editor.internal.MessagesEditorChangeAdapter;
import org.eclipse.babel.editor.util.UIUtils;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IWorkbenchPart;

/**
 * Internationalization page where one can edit all resource bundle entries at
 * once for all supported locales.
 * 
 * @author Pascal Essiembre
 */
public class I18NPage extends ScrolledComposite implements ISelectionProvider {

    /** Minimum height of text fields. */
    private static final int TEXT_MIN_HEIGHT = 90;

    private final MessagesEditor editor;
    private final SideNavComposite keysComposite;
    private final Composite valuesComposite;
    private final Map<Locale, I18NEntry> entryComposites = new HashMap<Locale, I18NEntry>();
    private Composite entriesComposite;

    // private Composite parent;
    private boolean keyTreeVisible = true;

    // private final StackLayout layout = new StackLayout();
    private final SashForm sashForm;

    /**
     * Constructor.
     * 
     * @param parent
     *            parent component.
     * @param style
     *            style to apply to this component
     * @param resourceMediator
     *            resource manager
     */
    public I18NPage(Composite parent, int style, final MessagesEditor editor) {
        super(parent, style);
        this.editor = editor;
        sashForm = new SashForm(this, SWT.SMOOTH);
        sashForm.setBackground(UIUtils
                .getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
        editor.getEditorSite().getPage().addPartListener(new IPartListener() {
            public void partActivated(IWorkbenchPart part) {
                if (part == editor) {
                    sashForm.setBackground(UIUtils
                            .getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
                }
            }

            public void partDeactivated(IWorkbenchPart part) {
                if (part == editor) {
                    sashForm.setBackground(UIUtils
                            .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));

                }
            }

            public void partBroughtToTop(IWorkbenchPart part) {
            }

            public void partClosed(IWorkbenchPart part) {
            }

            public void partOpened(IWorkbenchPart part) {
            }
        });

        setContent(sashForm);

        keysComposite = new SideNavComposite(sashForm, editor);

        valuesComposite = createValuesComposite(sashForm);

        sashForm.setWeights(new int[] { 25, 75 });

        setExpandHorizontal(true);
        setExpandVertical(true);
        setMinWidth(400);

        RBManager instance = RBManager.getInstance(editor.getBundleGroup()
                .getProjectName());
        instance.addMessagesEditorListener(new IMessagesEditorListener() {

            public void onSave() {
                // TODO Auto-generated method stub

            }

            public void onModify() {
                // TODO Auto-generated method stub
            }

            public void onResourceChanged(IMessagesBundle bundle) {
                I18NEntry i18nEntry = entryComposites.get(bundle.getLocale());
                if (i18nEntry != null && !getSelection().isEmpty()) {
                    i18nEntry.updateKey(String
                            .valueOf(((IStructuredSelection) getSelection())
                                    .getFirstElement()));
                }
            }

        });
    }

    /**
     * @see org.eclipse.swt.widgets.Widget#dispose()
     */
    public void dispose() {
        keysComposite.dispose();
        super.dispose();
    }

    public void setKeyTreeVisible(boolean visible) {
        keyTreeVisible = visible;
        if (visible) {
            sashForm.setMaximizedControl(null);
        } else {
            sashForm.setMaximizedControl(valuesComposite);
        }
        for (IMessagesEditorChangeListener listener : editor
                .getChangeListeners()) {
            listener.keyTreeVisibleChanged(visible);
        }
    }

    public boolean isKeyTreeVisible() {
        return keyTreeVisible;
    }

    private Composite createValuesComposite(SashForm parent) {
        final ScrolledComposite scrolledComposite = new ScrolledComposite(
                parent, SWT.V_SCROLL | SWT.H_SCROLL);
        scrolledComposite.setExpandHorizontal(true);
        scrolledComposite.setExpandVertical(true);
        scrolledComposite.setSize(SWT.DEFAULT, 100);

        entriesComposite = new Composite(scrolledComposite, SWT.BORDER);
        scrolledComposite.setContent(entriesComposite);
        scrolledComposite.setMinSize(entriesComposite.computeSize(SWT.DEFAULT,
                editor.getBundleGroup().getLocales().length * TEXT_MIN_HEIGHT));

        entriesComposite.setLayout(new GridLayout(1, false));
        Locale[] locales = editor.getBundleGroup().getLocales();
        UIUtils.sortLocales(locales);
        locales = UIUtils.filterLocales(locales);
        for (int i = 0; i < locales.length; i++) {
            Locale locale = locales[i];
            addI18NEntry(editor, locale);
        }

        editor.addChangeListener(new MessagesEditorChangeAdapter() {
            public void selectedKeyChanged(String oldKey, String newKey) {
                boolean isKey = newKey != null
                        && editor.getBundleGroup().isMessageKey(newKey);
                // scrolledComposite.setBackground(isKey);
            }
        });

        return scrolledComposite;
    }

    public void addI18NEntry(MessagesEditor editor, Locale locale) {
        I18NEntry i18NEntry = new I18NEntry(entriesComposite, editor, locale);
        // entryComposite.addFocusListener(localBehaviour);
        entryComposites.put(locale, i18NEntry);
        entriesComposite.layout();
    }

    public void selectLocale(Locale locale) {
        Collection<Locale> locales = entryComposites.keySet();
        for (Locale entryLocale : locales) {
            I18NEntry entry = entryComposites.get(entryLocale);

            // TODO add equivalent method on entry composite
            // Text textBox = entry.getTextBox();
            // if (BabelUtils.equals(locale, entryLocale)) {
            // textBox.selectAll();
            // textBox.setFocus();
            // } else {
            // textBox.clearSelection();
            // }
        }
    }

    public void addSelectionChangedListener(ISelectionChangedListener listener) {
        keysComposite.getTreeViewer().addSelectionChangedListener(listener);

    }

    public ISelection getSelection() {
        return keysComposite.getTreeViewer().getSelection();
    }

    public void removeSelectionChangedListener(
            ISelectionChangedListener listener) {
        keysComposite.getTreeViewer().removeSelectionChangedListener(listener);
    }

    public void setSelection(ISelection selection) {
        keysComposite.getTreeViewer().setSelection(selection);
    }

    public TreeViewer getTreeViewer() {
        return keysComposite.getTreeViewer();
    }
}

Back to the top