2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.uiDesigner
.actions
;
18 import com
.intellij
.openapi
.actionSystem
.AnAction
;
19 import com
.intellij
.openapi
.actionSystem
.AnActionEvent
;
20 import com
.intellij
.openapi
.actionSystem
.DefaultActionGroup
;
21 import com
.intellij
.openapi
.actionSystem
.Presentation
;
22 import com
.intellij
.openapi
.actionSystem
.ex
.ComboBoxAction
;
23 import com
.intellij
.openapi
.util
.IconLoader
;
24 import com
.intellij
.uiDesigner
.FormEditingUtil
;
25 import com
.intellij
.uiDesigner
.UIDesignerBundle
;
26 import com
.intellij
.uiDesigner
.designSurface
.GuiEditor
;
27 import org
.jetbrains
.annotations
.Nullable
;
28 import org
.jetbrains
.annotations
.NotNull
;
31 import java
.util
.Arrays
;
32 import java
.util
.Comparator
;
33 import java
.util
.Locale
;
38 public class ChooseLocaleAction
extends ComboBoxAction
{
39 private GuiEditor myLastEditor
;
40 private Presentation myPresentation
;
42 public ChooseLocaleAction() {
43 getTemplatePresentation().setText("");
44 getTemplatePresentation().setDescription(UIDesignerBundle
.message("choose.locale.description"));
45 getTemplatePresentation().setIcon(IconLoader
.getIcon("/com/intellij/uiDesigner/icons/chooseLocale.png"));
48 @Override public JComponent
createCustomComponent(Presentation presentation
) {
49 myPresentation
= presentation
;
50 return super.createCustomComponent(presentation
);
54 protected DefaultActionGroup
createPopupActionGroup(JComponent button
) {
55 DefaultActionGroup group
= new DefaultActionGroup();
56 GuiEditor editor
= myLastEditor
;
58 Locale
[] locales
= FormEditingUtil
.collectUsedLocales(editor
.getModule(), editor
.getRootContainer());
59 if (locales
.length
> 1 || (locales
.length
== 1 && locales
[0].getDisplayName().length() > 0)) {
60 Arrays
.sort(locales
, new Comparator
<Locale
>() {
61 public int compare(final Locale o1
, final Locale o2
) {
62 return o1
.getDisplayName().compareTo(o2
.getDisplayName());
65 for(Locale locale
: locales
) {
66 group
.add(new SetLocaleAction(editor
, locale
, true));
70 group
.add(new SetLocaleAction(editor
, new Locale(""), false));
76 @Nullable private GuiEditor
getEditor(final AnActionEvent e
) {
77 myLastEditor
= FormEditingUtil
.getActiveEditor(e
.getDataContext());
81 public void update(AnActionEvent e
) {
82 e
.getPresentation().setVisible(getEditor(e
) != null);
85 private class SetLocaleAction
extends AnAction
{
86 private final GuiEditor myEditor
;
87 private final Locale myLocale
;
88 private final boolean myUpdateText
;
90 public SetLocaleAction(final GuiEditor editor
, final Locale locale
, final boolean updateText
) {
91 super(locale
.getDisplayName().length() == 0
92 ? UIDesignerBundle
.message("choose.locale.default")
93 : locale
.getDisplayName());
94 myUpdateText
= updateText
;
99 public void actionPerformed(AnActionEvent e
) {
100 myEditor
.setStringDescriptorLocale(myLocale
);
102 myPresentation
.setText(getTemplatePresentation().getText());