001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.widgets; 003 004import java.awt.event.FocusEvent; 005import java.awt.event.FocusListener; 006 007import javax.swing.JTextArea; 008import javax.swing.text.Document; 009 010import org.openstreetmap.josm.Main; 011 012/** 013 * Subclass of {@link JTextArea} that adds a "native" context menu (cut/copy/paste/select all). 014 * @since 5886 015 */ 016public class JosmTextArea extends JTextArea implements FocusListener { 017 018 /** 019 * Constructs a new {@code JosmTextArea}. A default model is set, the initial string 020 * is null, and rows/columns are set to 0. 021 */ 022 public JosmTextArea() { 023 this(null, null, 0, 0); 024 } 025 026 /** 027 * Constructs a new {@code JosmTextArea} with the specified text displayed. 028 * A default model is created and rows/columns are set to 0. 029 * 030 * @param text the text to be displayed, or null 031 */ 032 public JosmTextArea(String text) { 033 this(null, text, 0, 0); 034 } 035 036 /** 037 * Constructs a new {@code JosmTextArea} with the given document model, and defaults 038 * for all of the other arguments (null, 0, 0). 039 * 040 * @param doc the model to use 041 */ 042 public JosmTextArea(Document doc) { 043 this(doc, null, 0, 0); 044 } 045 046 /** 047 * Constructs a new empty {@code JosmTextArea} with the specified number of 048 * rows and columns. A default model is created, and the initial 049 * string is null. 050 * 051 * @param rows the number of rows >= 0 052 * @param columns the number of columns >= 0 053 * @throws IllegalArgumentException if the rows or columns 054 * arguments are negative. 055 */ 056 public JosmTextArea(int rows, int columns) { 057 this(null, null, rows, columns); 058 } 059 060 /** 061 * Constructs a new {@code JosmTextArea} with the specified text and number 062 * of rows and columns. A default model is created. 063 * 064 * @param text the text to be displayed, or null 065 * @param rows the number of rows >= 0 066 * @param columns the number of columns >= 0 067 * @throws IllegalArgumentException if the rows or columns 068 * arguments are negative. 069 */ 070 public JosmTextArea(String text, int rows, int columns) { 071 this(null, text, rows, columns); 072 } 073 074 /** 075 * Constructs a new {@code JosmTextArea} with the specified number of rows 076 * and columns, and the given model. All of the constructors 077 * feed through this constructor. 078 * 079 * @param doc the model to use, or create a default one if null 080 * @param text the text to be displayed, null if none 081 * @param rows the number of rows >= 0 082 * @param columns the number of columns >= 0 083 * @throws IllegalArgumentException if the rows or columns 084 * arguments are negative. 085 */ 086 public JosmTextArea(Document doc, String text, int rows, int columns) { 087 super(doc, text, rows, columns); 088 TextContextualPopupMenu.enableMenuFor(this, true); 089 addFocusListener(this); 090 } 091 092 @Override 093 public void focusGained(FocusEvent e) { 094 if (Main.map != null) { 095 Main.map.keyDetector.setEnabled(false); 096 } 097 } 098 099 @Override 100 public void focusLost(FocusEvent e) { 101 if (Main.map != null) { 102 Main.map.keyDetector.setEnabled(true); 103 } 104 } 105}