Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / ContextMenuUtils.java @ 2491

History | View | Annotate | Download (4.23 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.tools.swing.impl;
7

    
8
import java.awt.event.ActionEvent;
9
import java.beans.PropertyChangeEvent;
10
import javax.swing.AbstractAction;
11
import javax.swing.Action;
12
import javax.swing.JComponent;
13
import javax.swing.JMenuItem;
14
import javax.swing.JPopupMenu;
15
import javax.swing.text.DefaultEditorKit;
16
import javax.swing.text.JTextComponent;
17
import org.gvsig.tools.ToolsLocator;
18
import org.gvsig.tools.i18n.I18nManager;
19
import static org.gvsig.tools.swing.impl.ClearButtonUtils.getTextComponent;
20

    
21
/**
22
 *
23
 * @author jjdelcerro
24
 */
25
public class ContextMenuUtils {
26

    
27
    public static void setDefaultContextMenu(final JComponent component) {
28
        ContextMenuUtils.setDefaultContextMenu(component, null);
29
    }
30

    
31
    public static void setDefaultContextMenu(final JComponent component, String title) {
32
        final JTextComponent text = getTextComponent(component);
33
        JPopupMenu popupMenu = new JPopupMenu();
34
        I18nManager i18nManager = ToolsLocator.getI18nManager();
35

    
36
        Action copyAction = text.getActionMap().get(DefaultEditorKit.copyAction);
37
        Action cutAction = text.getActionMap().get(DefaultEditorKit.cutAction);
38
        Action pasteAction = text.getActionMap().get(DefaultEditorKit.pasteAction);
39
        Action selectAllAction = text.getActionMap().get(DefaultEditorKit.selectAllAction);
40

    
41
        if (copyAction == null
42
                && cutAction == null
43
                && pasteAction == null
44
                && selectAllAction == null) {
45
            copyAction = text.getActionMap().get(i18nManager.getTranslation("copy"));
46
            cutAction = text.getActionMap().get(i18nManager.getTranslation("cut"));
47
            pasteAction = text.getActionMap().get(i18nManager.getTranslation("paste"));
48
            selectAllAction = text.getActionMap().get(i18nManager.getTranslation("SelectAll"));
49
        } else {
50
            copyAction.putValue(Action.NAME, i18nManager.getTranslation("copy"));
51
            cutAction.putValue(Action.NAME, i18nManager.getTranslation("cut"));
52
            pasteAction.putValue(Action.NAME, i18nManager.getTranslation("paste"));
53
            selectAllAction.putValue(Action.NAME, i18nManager.getTranslation("SelectAll"));
54
        }
55

    
56
        final String title2;
57
        if (title == null) {
58
            title2 = i18nManager.getTranslation("text_editor");
59
        } else {
60
            title2 = title;
61
        }
62
        Action textEditorAction = new AbstractAction(i18nManager.getTranslation("text_editor")) {
63
            @Override
64
            public void actionPerformed(ActionEvent e) {
65
                DefaultZoomDialog dialog = new DefaultZoomDialog(text, title2, text.getText());
66
                dialog.setEditable(text.isEditable());
67
                dialog.setAlwaysOnTop(true);
68
                dialog.setVisible(true);
69
                if (text.isEditable() && text.isEnabled()) {
70
                    text.setText(dialog.getText());
71
                }
72
            }
73
        };
74
        final JMenuItem menuCut = new JMenuItem(cutAction);
75
        final JMenuItem menuPaste = new JMenuItem(pasteAction);
76

    
77
        popupMenu.add(textEditorAction);
78
        popupMenu.addSeparator();
79

    
80
        popupMenu.add(menuCut);
81
        popupMenu.add(copyAction);
82
        popupMenu.add(menuPaste);
83
        popupMenu.add(selectAllAction);
84

    
85
        if (text.isEnabled() && text.isEditable()) {
86
            menuCut.setEnabled(true);
87
            menuPaste.setEnabled(true);
88
        } else {
89
            menuCut.setEnabled(false);
90
            menuPaste.setEnabled(false);
91
        }
92
        text.addPropertyChangeListener((PropertyChangeEvent evt) -> {
93
          if (evt == null) {
94
            return;
95
          }
96
          if (evt.getPropertyName().equalsIgnoreCase("enabled")) {
97
            boolean enabled = text.isEnabled();
98
            menuCut.setEnabled(enabled);
99
            menuPaste.setEnabled(enabled);
100
          } else if (evt.getPropertyName().equalsIgnoreCase("editable")) {
101
            boolean editable = text.isEditable();
102
            menuCut.setEnabled(editable);
103
            menuPaste.setEnabled(editable);
104
          }
105
        });
106
        text.setComponentPopupMenu(popupMenu);
107
    }
108
    
109
}