001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.widgets;
003
004import javax.swing.JTextField;
005import javax.swing.text.Document;
006
007/**
008 * Subclass of {@link JTextField} that adds a "native" context menu (cut/copy/paste/select all).
009 * @since 5886
010 */
011public class JosmTextField extends JTextField {
012
013    /**
014     * Constructs a new <code>JosmTextField</code> that uses the given text
015     * storage model and the given number of columns.
016     * This is the constructor through which the other constructors feed.
017     * If the document is <code>null</code>, a default model is created.
018     *
019     * @param doc  the text storage to use; if this is <code>null</code>,
020     *      a default will be provided by calling the
021     *      <code>createDefaultModel</code> method
022     * @param text  the initial string to display, or <code>null</code>
023     * @param columns  the number of columns to use to calculate
024     *   the preferred width &gt;= 0; if <code>columns</code>
025     *   is set to zero, the preferred width will be whatever
026     *   naturally results from the component implementation
027     * @exception IllegalArgumentException if <code>columns</code> &lt; 0
028     */
029    public JosmTextField(Document doc, String text, int columns) {
030        super(doc, text, columns);
031        TextContextualPopupMenu.enableMenuFor(this);
032        // Fix minimum size when columns are specified
033        if (columns > 0) {
034            setMinimumSize(getPreferredSize());
035        }
036    }
037
038    /**
039     * Constructs a new <code>JosmTextField</code> initialized with the
040     * specified text and columns.  A default model is created.
041     *
042     * @param text the text to be displayed, or <code>null</code>
043     * @param columns  the number of columns to use to calculate
044     *   the preferred width; if columns is set to zero, the
045     *   preferred width will be whatever naturally results from
046     *   the component implementation
047     */
048    public JosmTextField(String text, int columns) {
049        this(null, text, columns);
050    }
051
052    /**
053     * Constructs a new <code>JosmTextField</code> initialized with the
054     * specified text. A default model is created and the number of
055     * columns is 0.
056     *
057     * @param text the text to be displayed, or <code>null</code>
058     */
059    public JosmTextField(String text) {
060        this(null, text, 0);
061    }
062
063    /**
064     * Constructs a new empty <code>JosmTextField</code> with the specified
065     * number of columns.
066     * A default model is created and the initial string is set to
067     * <code>null</code>.
068     *
069     * @param columns  the number of columns to use to calculate
070     *   the preferred width; if columns is set to zero, the
071     *   preferred width will be whatever naturally results from
072     *   the component implementation
073     */
074    public JosmTextField(int columns) {
075        this(null, null, columns);
076    }
077
078    /**
079     * Constructs a new <code>JosmTextField</code>.  A default model is created,
080     * the initial string is <code>null</code>,
081     * and the number of columns is set to 0.
082     */
083    public JosmTextField() {
084        this(null, null, 0);
085    }
086}