aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/newxmlfile/AddTranslationDialog.java
blob: 0301b80fe2bad8ec3518080476913db2e3ed7631 (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.ide.eclipse.adt.internal.wizards.newxmlfile;

import static com.android.SdkConstants.FD_RES;
import static com.android.SdkConstants.FD_RES_VALUES;
import static com.android.SdkConstants.RES_QUALIFIER_SEP;

import com.android.ide.common.rendering.api.ResourceValue;
import com.android.ide.common.res2.ValueXmlHelper;
import com.android.ide.common.resources.LocaleManager;
import com.android.ide.common.resources.ResourceItem;
import com.android.ide.common.resources.configuration.FolderConfiguration;
import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.AdtUtils;
import com.android.ide.eclipse.adt.internal.editors.layout.configuration.FlagManager;
import com.android.ide.eclipse.adt.internal.editors.layout.gle2.ImageControl;
import com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderPreviewManager;
import com.android.ide.eclipse.adt.internal.resources.manager.ProjectResources;
import com.android.ide.eclipse.adt.internal.resources.manager.ResourceManager;
import com.android.resources.ResourceType;
import com.google.common.base.Charsets;
import com.google.common.collect.Maps;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.IBaseLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;

/**
 * Dialog which adds a new translation to the project
 */
public class AddTranslationDialog extends Dialog implements ControlListener, SelectionListener,
        TraverseListener {
    private static final int KEY_COLUMN = 0;
    private static final int DEFAULT_TRANSLATION_COLUMN = 1;
    private static final int NEW_TRANSLATION_COLUMN = 2;
    private final FolderConfiguration mConfiguration = new FolderConfiguration();
    private final IProject mProject;
    private String mTarget;
    private boolean mIgnore;
    private Map<String, String> mTranslations;
    private Set<String> mExistingLanguages;
    private String mSelectedLanguage;
    private String mSelectedRegion;

    private Table mTable;
    private Combo mLanguageCombo;
    private Combo mRegionCombo;
    private ImageControl mFlag;
    private Label mFile;
    private Button mOkButton;
    private Composite mErrorPanel;
    private Label mErrorLabel;
    private MyTableViewer mTableViewer;

    /**
     * Creates the dialog.
     * @param parentShell the parent shell
     * @param project the project to add translations into
     */
    public AddTranslationDialog(Shell parentShell, IProject project) {
        super(parentShell);
        setShellStyle(SWT.CLOSE | SWT.RESIZE | SWT.TITLE);
        mProject = project;
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        Composite container = (Composite) super.createDialogArea(parent);
        GridLayout gl_container = new GridLayout(6, false);
        gl_container.horizontalSpacing = 0;
        container.setLayout(gl_container);

        Label languageLabel = new Label(container, SWT.NONE);
        languageLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
        languageLabel.setText("Language:");
        mLanguageCombo = new Combo(container, SWT.READ_ONLY);
        GridData gd_mLanguageCombo = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
        gd_mLanguageCombo.widthHint = 150;
        mLanguageCombo.setLayoutData(gd_mLanguageCombo);

        Label regionLabel = new Label(container, SWT.NONE);
        GridData gd_regionLabel = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
        gd_regionLabel.horizontalIndent = 10;
        regionLabel.setLayoutData(gd_regionLabel);
        regionLabel.setText("Region:");
        mRegionCombo = new Combo(container, SWT.READ_ONLY);
        GridData gd_mRegionCombo = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gd_mRegionCombo.widthHint = 150;
        mRegionCombo.setLayoutData(gd_mRegionCombo);
        mRegionCombo.setEnabled(false);

        mFlag = new ImageControl(container, SWT.NONE, null);
        mFlag.setDisposeImage(false);
        GridData gd_mFlag = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gd_mFlag.exclude = true;
        gd_mFlag.widthHint = 32;
        gd_mFlag.horizontalIndent = 3;
        mFlag.setLayoutData(gd_mFlag);

        mFile = new Label(container, SWT.NONE);
        mFile.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        mTableViewer = new MyTableViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
        mTable = mTableViewer.getTable();
        mTable.setEnabled(false);
        mTable.setLinesVisible(true);
        mTable.setHeaderVisible(true);
        mTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 6, 2));
        mTable.addControlListener(this);
        mTable.addTraverseListener(this);
        // If you have difficulty opening up this form in WindowBuilder and it complains about
        // the next line, change the type of the mTableViewer field and the above
        // constructor call from MyTableViewer to TableViewer
        TableViewerColumn keyViewerColumn = new TableViewerColumn(mTableViewer, SWT.NONE);
        TableColumn keyColumn = keyViewerColumn.getColumn();
        keyColumn.setWidth(100);
        keyColumn.setText("Key");
        TableViewerColumn defaultViewerColumn = new TableViewerColumn(mTableViewer, SWT.NONE);
        TableColumn defaultColumn = defaultViewerColumn.getColumn();
        defaultColumn.setWidth(200);
        defaultColumn.setText("Default");
        TableViewerColumn translationViewerColumn = new TableViewerColumn(mTableViewer, SWT.NONE);
        TableColumn translationColumn = translationViewerColumn.getColumn();
        translationColumn.setWidth(200);
        translationColumn.setText("New Translation");

        mErrorPanel = new Composite(container, SWT.NONE);
        GridData gd_mErrorLabel = new GridData(SWT.FILL, SWT.CENTER, false, false, 6, 1);
        gd_mErrorLabel.exclude = true;
        mErrorPanel.setLayoutData(gd_mErrorLabel);

        translationViewerColumn.setEditingSupport(new TranslationEditingSupport(mTableViewer));

        fillLanguages();
        fillRegions();
        fillStrings();
        updateColumnWidths();
        validatePage();

        mLanguageCombo.addSelectionListener(this);
        mRegionCombo.addSelectionListener(this);

        return container;
    }

    /** Populates the table with keys and default strings */
    private void fillStrings() {
        ResourceManager manager = ResourceManager.getInstance();
        ProjectResources resources = manager.getProjectResources(mProject);
        mExistingLanguages = resources.getLanguages();

        Collection<ResourceItem> items = resources.getResourceItemsOfType(ResourceType.STRING);

        ResourceItem[] array = items.toArray(new ResourceItem[items.size()]);
        Arrays.sort(array);

        // TODO: Read in the actual XML files providing the default keys here
        // (they can be obtained via ResourceItem.getSourceFileList())
        // such that we can read all the attributes associated with each
        // item, and if it defines translatable=false, or the filename is
        // donottranslate.xml, we can ignore it, and in other cases just
        // duplicate all the attributes (such as "formatted=true", or other
        // local conventions such as "product=tablet", or "msgid="123123123",
        // etc.)

        mTranslations = Maps.newHashMapWithExpectedSize(items.size());
        IBaseLabelProvider labelProvider = new CellLabelProvider() {
            @Override
            public void update(ViewerCell cell) {
                Object element = cell.getElement();
                int index = cell.getColumnIndex();
                ResourceItem item = (ResourceItem) element;
                switch (index) {
                    case KEY_COLUMN: {
                        // Key
                        cell.setText(item.getName());
                        return;
                    }
                    case DEFAULT_TRANSLATION_COLUMN: {
                        // Default translation
                        ResourceValue value = item.getResourceValue(ResourceType.STRING,
                                mConfiguration, false);

                        if (value != null) {
                            cell.setText(value.getValue());
                            return;
                        }
                        break;
                    }
                    case NEW_TRANSLATION_COLUMN: {
                        // New translation
                        String translation = mTranslations.get(item.getName());
                        if (translation != null) {
                            cell.setText(translation);
                            return;
                        }
                        break;
                    }
                    default:
                        assert false : index;
                }
                cell.setText("");
            }
        };

        mTableViewer.setLabelProvider(labelProvider);
        mTableViewer.setContentProvider(new ArrayContentProvider());
        mTableViewer.setInput(array);
    }

    /** Populate the languages dropdown */
    private void fillLanguages() {
        List<String> languageCodes = LocaleManager.getLanguageCodes();
        List<String> labels = new ArrayList<String>();
        for (String code : languageCodes) {
            labels.add(code + ": " + LocaleManager.getLanguageName(code)); //$NON-NLS-1$
        }
        Collections.sort(labels);
        labels.add(0, "(Select)");
        mLanguageCombo.setItems(labels.toArray(new String[labels.size()]));
        mLanguageCombo.select(0);
    }

    /** Populate the regions dropdown */
    private void fillRegions() {
        // TODO: When you switch languages, offer some "default" usable options. For example,
        // when you choose English, offer the countries that use English, and so on. Unfortunately
        // we don't have good data about this, we'd just need to hardcode a few common cases.
        List<String> regionCodes = LocaleManager.getRegionCodes();
        List<String> labels = new ArrayList<String>();
        for (String code : regionCodes) {
            labels.add(code + ": " + LocaleManager.getRegionName(code)); //$NON-NLS-1$
        }
        Collections.sort(labels);
        labels.add(0, "Any");
        mRegionCombo.setItems(labels.toArray(new String[labels.size()]));
        mRegionCombo.select(0);
    }

    /** React to resizing by distributing the space evenly between the last two columns */
    private void updateColumnWidths() {
        Rectangle r = mTable.getClientArea();
        int availableWidth = r.width;
        // Distribute all available space to the last two columns
        int columnCount = mTable.getColumnCount();
        for (int i = 0; i < columnCount; i++) {
            TableColumn column = mTable.getColumn(i);
            availableWidth -= column.getWidth();
        }
        if (availableWidth != 0) {
            TableColumn column = mTable.getColumn(DEFAULT_TRANSLATION_COLUMN);
            column.setWidth(column.getWidth() + availableWidth / 2);
            column = mTable.getColumn(NEW_TRANSLATION_COLUMN);
            column.setWidth(column.getWidth() + availableWidth / 2 + availableWidth % 2);
        }
    }

    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        mOkButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
                // Don't make the OK button default as in most dialogs, since when you press
                // Return thinking you might edit a value it dismisses the dialog instead
                false);
        createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
        mOkButton.setEnabled(false);

        validatePage();
    }

    /**
     * Return the initial size of the dialog.
     */
    @Override
    protected Point getInitialSize() {
        return new Point(800, 600);
    }

    private void updateTarget() {
        if (mSelectedLanguage == null) {
            mTarget = null;
            mFile.setText("");
        } else {
            String folder = FD_RES + '/' + FD_RES_VALUES + RES_QUALIFIER_SEP + mSelectedLanguage;
            if (mSelectedRegion != null) {
                folder = folder + RES_QUALIFIER_SEP + 'r' + mSelectedRegion;
            }
            mTarget = folder + "/strings.xml"; //$NON-NLS-1$
            mFile.setText(String.format("Creating %1$s", mTarget));
        }
    }

    private void updateFlag() {
        if (mSelectedLanguage == null) {
            // Nothing selected
            ((GridData) mFlag.getLayoutData()).exclude = true;
        } else {
            FlagManager manager = FlagManager.get();
            Image flag = manager.getFlag(mSelectedLanguage, mSelectedRegion);
            if (flag != null) {
                ((GridData) mFlag.getLayoutData()).exclude = false;
                mFlag.setImage(flag);
            }
        }

        mFlag.getParent().layout(true);
        mFlag.getParent().redraw();
    }

    /** Actually create the new translation file and write it to disk */
    private void createTranslation() {
        List<String> keys = new ArrayList<String>(mTranslations.keySet());
        Collections.sort(keys);

        StringBuilder sb = new StringBuilder(keys.size() * 120);
        sb.append("<resources>\n\n");          //$NON-NLS-1$
        for (String key : keys) {
            String value = mTranslations.get(key);
            if (value == null || value.trim().isEmpty()) {
                continue;
            }
            sb.append("    <string name=\"");  //$NON-NLS-1$
            sb.append(key);
            sb.append("\">");                  //$NON-NLS-1$
            sb.append(ValueXmlHelper.escapeResourceString(value));
            sb.append("</string>\n");          //$NON-NLS-1$
        }
        sb.append("\n</resources>");           //$NON-NLS-1$

        IFile file = mProject.getFile(mTarget);

        try {
            IContainer parent = file.getParent();
            AdtUtils.ensureExists(parent);
            InputStream source = new ByteArrayInputStream(sb.toString().getBytes(Charsets.UTF_8));
            file.create(source, true, new NullProgressMonitor());
            AdtPlugin.openFile(file, null, true /*showEditorTab*/);

            // Ensure that the project resources updates itself to notice the new language.
            // In theory, this shouldn't be necessary.
            ResourceManager manager = ResourceManager.getInstance();
            IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
            IFolder folder = root.getFolder(parent.getFullPath());
            manager.getResourceFolder(folder);
            RenderPreviewManager.bumpRevision();
        } catch (CoreException e) {
            AdtPlugin.log(e, null);
        }
    }

    private void validatePage() {
        if (mOkButton == null) { // Early initialization
            return;
        }

        String message = null;

        if (mSelectedLanguage == null) {
            message = "Select a language";
        } else if (mExistingLanguages.contains(mSelectedLanguage)) {
            if (mSelectedRegion == null) {
                message = String.format("%1$s is already translated in this project",
                        LocaleManager.getLanguageName(mSelectedLanguage));
            } else {
                ResourceManager manager = ResourceManager.getInstance();
                ProjectResources resources = manager.getProjectResources(mProject);
                SortedSet<String> regions = resources.getRegions(mSelectedLanguage);
                if (regions.contains(mSelectedRegion)) {
                    message = String.format("%1$s (%2$s) is already translated in this project",
                            LocaleManager.getLanguageName(mSelectedLanguage),
                            LocaleManager.getRegionName(mSelectedRegion));
                }
            }
        } else {
            // Require all strings to be translated? No, some of these may not
            // be translatable (e.g. translatable=false, defined in donottranslate.xml, etc.)
            //int missing = mTable.getItemCount() - mTranslations.values().size();
            //if (missing > 0) {
            //    message = String.format("Missing %1$d translations", missing);
            //}
        }

        boolean valid = message == null;
        mTable.setEnabled(message == null);
        mOkButton.setEnabled(valid);
        showError(message);
    }

    private void showError(String error) {
        GridData data = (GridData) mErrorPanel.getLayoutData();

        boolean show = error != null;
        if (show == data.exclude) {
            if (show) {
                if (mErrorLabel == null) {
                    mErrorPanel.setLayout(new GridLayout(2, false));
                    IWorkbench workbench = PlatformUI.getWorkbench();
                    ISharedImages sharedImages = workbench.getSharedImages();
                    String iconName = ISharedImages.IMG_OBJS_ERROR_TSK;
                    Image image = sharedImages.getImage(iconName);
                    @SuppressWarnings("unused")
                    ImageControl icon = new ImageControl(mErrorPanel, SWT.NONE, image);

                    mErrorLabel = new Label(mErrorPanel, SWT.NONE);
                    mErrorLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
                            1, 1));
                }
                mErrorLabel.setText(error);
            }
            data.exclude = !show;
            mErrorPanel.getParent().layout(true);
        }
    }

    @Override
    protected void okPressed() {
        mTableViewer.applyEditorValue();

        super.okPressed();
        createTranslation();
    }

    // ---- Implements ControlListener ----

    @Override
    public void controlMoved(ControlEvent e) {
    }

    @Override
    public void controlResized(ControlEvent e) {
        if (mIgnore) {
            return;
        }

        updateColumnWidths();
    }

    // ---- Implements SelectionListener ----

    @Override
    public void widgetSelected(SelectionEvent e) {
        if (mIgnore) {
            return;
        }

        Object source = e.getSource();
        if (source == mLanguageCombo) {
            try {
                mIgnore = true;
                mRegionCombo.select(0);
                mSelectedRegion = null;
            } finally {
                mIgnore = false;
            }

            int languageIndex = mLanguageCombo.getSelectionIndex();
            if (languageIndex == 0) {
                mSelectedLanguage = null;
                mRegionCombo.setEnabled(false);
            } else {
                // This depends on the label format
                mSelectedLanguage = mLanguageCombo.getItem(languageIndex).substring(0, 2);
                mRegionCombo.setEnabled(true);
            }

            updateTarget();
            updateFlag();
        } else if (source == mRegionCombo) {
            int regionIndex = mRegionCombo.getSelectionIndex();
            if (regionIndex == 0) {
                mSelectedRegion = null;
            } else {
                mSelectedRegion = mRegionCombo.getItem(regionIndex).substring(0, 2);
            }

            updateTarget();
            updateFlag();
        }

        try {
            mIgnore = true;
            validatePage();
        } finally {
            mIgnore = false;
        }
    }

    @Override
    public void widgetDefaultSelected(SelectionEvent e) {
    }

    // ---- TraverseListener ----

    @Override
    public void keyTraversed(TraverseEvent e) {
        // If you press Return and we're not cell editing, start editing the current row
        if (e.detail == SWT.TRAVERSE_RETURN && !mTableViewer.isCellEditorActive()) {
            int index = mTable.getSelectionIndex();
            if (index != -1) {
                Object next = mTable.getItem(index).getData();
                mTableViewer.editElement(next, NEW_TRANSLATION_COLUMN);
            }
        }
    }

    /** Editing support for the translation column */
    private class TranslationEditingSupport extends EditingSupport {
        /**
         * When true, setValue is being called as part of a default action
         * (e.g. Return), not due to focus loss
         */
        private boolean mDefaultAction;

        private TranslationEditingSupport(ColumnViewer viewer) {
            super(viewer);
        }

        @Override
        protected void setValue(Object element, Object value) {
            ResourceItem item = (ResourceItem) element;
            mTranslations.put(item.getName(), value.toString());
            mTableViewer.update(element, null);
            validatePage();

            // If the user is pressing Return to finish editing a value (which is
            // not the only way this method can get called - for example, if you click
            // outside the cell while editing, the focus loss will also result in
            // this method getting called), then mDefaultAction is true, and we automatically
            // start editing the next row.
            if (mDefaultAction) {
                mTable.getDisplay().asyncExec(new Runnable() {
                    @Override
                    public void run() {
                        if (!mTable.isDisposed() && !mTableViewer.isCellEditorActive()) {
                            int index = mTable.getSelectionIndex();
                            if (index != -1 && index < mTable.getItemCount() - 1) {
                                Object next = mTable.getItem(index + 1).getData();
                                mTableViewer.editElement(next, NEW_TRANSLATION_COLUMN);
                            }
                        }
                    }
                });
            }
        }

        @Override
        protected Object getValue(Object element) {
            ResourceItem item = (ResourceItem) element;
            String value = mTranslations.get(item.getName());
            if (value == null) {
                return "";
            }
            return value;
        }

        @Override
        protected CellEditor getCellEditor(Object element) {
            return new TextCellEditor(mTable) {
                @Override
                protected void handleDefaultSelection(SelectionEvent event) {
                    try {
                        mDefaultAction = true;
                        super.handleDefaultSelection(event);
                    } finally {
                        mDefaultAction = false;
                    }
                }
            };
        }

        @Override
        protected boolean canEdit(Object element) {
            return true;
        }
    }

    private class MyTableViewer extends TableViewer {
        public MyTableViewer(Composite parent, int style) {
            super(parent, style);
        }

        // Make this public so we can call it to ensure values are applied before the dialog
        // is dismissed in {@link #okPressed}
        @Override
        public void applyEditorValue() {
            super.applyEditorValue();
        }
    }
}