123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566 |
- /*
- * Copyright (c) 2006-2013 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.ui_swing.components.inputfields;
-
- import com.dmdirc.addons.ui_swing.SwingController;
- import com.dmdirc.addons.ui_swing.UIUtilities;
- import com.dmdirc.addons.ui_swing.components.colours.ColourPickerDialog;
- import com.dmdirc.addons.ui_swing.components.frames.InputTextFrame;
- import com.dmdirc.interfaces.ui.InputField;
- import com.dmdirc.interfaces.ui.InputValidationListener;
- import com.dmdirc.util.collections.ListenerList;
-
- import java.awt.Color;
- import java.awt.FontMetrics;
- import java.awt.KeyboardFocusManager;
- import java.awt.Window;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.beans.PropertyChangeEvent;
- import java.beans.PropertyChangeListener;
- import java.util.concurrent.Callable;
-
- import javax.swing.JComponent;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- import javax.swing.text.BadLocationException;
-
- import net.miginfocom.layout.PlatformDefaults;
- import net.miginfocom.swing.MigLayout;
-
- /** Swing input field. */
- public class SwingInputField extends JComponent implements InputField,
- KeyListener, InputValidationListener, PropertyChangeListener {
-
- /** Serial version UID. */
- private static final long serialVersionUID = 1;
- /** Colour picker. */
- private ColourPickerDialog colourPicker;
- /** Input field text field. */
- private final JTextField textField;
- /** Line wrap indicator. */
- private final JLabel wrapIndicator;
- /** Error indicator. */
- private final JLabel errorIndicator;
- /** Listener list. */
- private final ListenerList listeners;
- /** Parent Window. */
- private final Window parentWindow;
- /** Swing controller. */
- private final SwingController controller;
-
- /**
- * Instantiates a new swing input field.
- *
- * @param textFrame Parent textFrame
- * @param parentWindow Parent window
- */
- public SwingInputField(final InputTextFrame textFrame,
- final Window parentWindow) {
- super();
-
- controller = textFrame.getController();
- this.parentWindow = parentWindow;
-
- listeners = new ListenerList();
-
- textField = new JTextField();
- textField.setFocusTraversalKeysEnabled(false);
- textField.addKeyListener(this);
- textField.setOpaque(true);
- wrapIndicator =
- new JLabel(textFrame.getIconManager().getIcon("linewrap"));
- wrapIndicator.setVisible(false);
- errorIndicator =
- new JLabel(textFrame.getIconManager().getIcon("input-error"));
- errorIndicator.setVisible(false);
-
- setLayout(new MigLayout("ins 0, hidemode 3"));
-
- final FontMetrics fm = textField.getFontMetrics(textField.getFont());
- add(textField, "grow, push, hmin " + (fm.getMaxAscent() + fm.
- getMaxDescent() + PlatformDefaults.getPanelInsets(0).getValue()));
- add(wrapIndicator, "");
- add(errorIndicator, "");
-
- setActionMap(textField.getActionMap());
- setInputMap(SwingInputField.WHEN_FOCUSED,
- textField.getInputMap(SwingInputField.WHEN_FOCUSED));
- setInputMap(SwingInputField.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
- textField.getInputMap(SwingInputField.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT));
- setInputMap(SwingInputField.WHEN_IN_FOCUSED_WINDOW,
- textField.getInputMap(SwingInputField.WHEN_IN_FOCUSED_WINDOW));
- KeyboardFocusManager.getCurrentKeyboardFocusManager()
- .addPropertyChangeListener(this);
- }
-
- /** {@inheritDoc} */
- @Override
- public void requestFocus() {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- textField.requestFocus();
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public boolean requestFocusInWindow() {
- return UIUtilities.invokeAndWait(new Callable<Boolean>() {
-
- /** {@inheritDoc} */
- @Override
- public Boolean call() {
- return textField.requestFocusInWindow();
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public void showColourPicker(final boolean irc, final boolean hex) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- if (controller.getGlobalConfig().getOptionBool("general",
- "showcolourdialog")) {
- colourPicker = new ColourPickerDialog(controller
- .getIconManager(), irc, hex, parentWindow);
- colourPicker.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(final ActionEvent actionEvent) {
- try {
- textField.getDocument().
- insertString(textField.getCaretPosition(),
- actionEvent.getActionCommand(), null);
- } catch (final BadLocationException ex) {
- //Ignore, wont happen
- }
- colourPicker.dispose();
- colourPicker = null;
- }
- });
- colourPicker.display();
- colourPicker.setLocation((int) textField.getLocationOnScreen().
- getX(),
- (int) textField.getLocationOnScreen().getY() -
- colourPicker.getHeight());
- }
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public void hideColourPicker() {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- if (colourPicker != null) {
- colourPicker.dispose();
- colourPicker = null;
- }
- }
- });
- }
-
- /**
- * Returns the textfield for this inputfield.
- *
- * @return JTextField
- */
- public JTextField getTextField() {
- return UIUtilities.invokeAndWait(new Callable<JTextField>() {
-
- /** {@inheritDoc} */
- @Override
- public JTextField call() {
- return textField;
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public void addActionListener(final ActionListener listener) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- textField.addActionListener(listener);
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public void addKeyListener(final KeyListener listener) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- listeners.add(KeyListener.class, listener);
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public void removeActionListener(final ActionListener listener) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- textField.removeActionListener(listener);
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public void removeKeyListener(final KeyListener listener) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- listeners.remove(KeyListener.class, listener);
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public String getSelectedText() {
- return UIUtilities.invokeAndWait(new Callable<String>() {
-
- /** {@inheritDoc} */
- @Override
- public String call() {
- return textField.getSelectedText();
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public int getSelectionEnd() {
- return UIUtilities.invokeAndWait(new Callable<Integer>() {
-
- /** {@inheritDoc} */
- @Override
- public Integer call() {
- return textField.getSelectionEnd();
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public int getSelectionStart() {
- return UIUtilities.invokeAndWait(new Callable<Integer>() {
-
- /** {@inheritDoc} */
- @Override
- public Integer call() {
- return textField.getSelectionStart();
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public String getText() {
- return UIUtilities.invokeAndWait(new Callable<String>() {
-
- /** {@inheritDoc} */
- @Override
- public String call() {
- return textField.getText();
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public void setText(final String text) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- textField.setText(text);
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public int getCaretPosition() {
- return UIUtilities.invokeAndWait(new Callable<Integer>() {
-
- /** {@inheritDoc} */
- @Override
- public Integer call() {
- return textField.getCaretPosition();
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public void setCaretPosition(final int position) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- textField.setCaretPosition(position);
- }
- });
- }
-
- /**
- * Replaces the selection with the specified text.
- *
- * @param clipboard Text to replace selection with
- */
- public void replaceSelection(final String clipboard) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- textField.replaceSelection(clipboard);
- }
- });
- }
-
- /**
- * Sets the caret colour to the specified coloour.
- *
- * @param optionColour Colour for the caret
- */
- public void setCaretColor(final Color optionColour) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- textField.setCaretColor(optionColour);
- }
- });
- }
-
- /**
- * Sets the foreground colour to the specified coloour.
- *
- * @param optionColour Colour for the caret
- */
- @Override
- public void setForeground(final Color optionColour) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- textField.setForeground(optionColour);
- }
- });
- }
-
- /**
- * Sets the background colour to the specified coloour.
- *
- * @param optionColour Colour for the caret
- */
- @Override
- public void setBackground(final Color optionColour) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- textField.setBackground(optionColour);
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public boolean hasFocus() {
- return UIUtilities.invokeAndWait(new Callable<Boolean>() {
-
- /** {@inheritDoc} */
- @Override
- public Boolean call() {
- return textField.hasFocus();
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public boolean isFocusOwner() {
- return UIUtilities.invokeAndWait(new Callable<Boolean>() {
-
- /** {@inheritDoc} */
- @Override
- public Boolean call() {
- return textField.isFocusOwner();
- }
- });
- }
-
- /**
- * Sets the start index of the selection for this component.
- *
- * @param selectionStart Start index
- */
- public void setSelectionStart(final int selectionStart) {
- UIUtilities.invokeLater(new Runnable() {
-
- @Override
- public void run() {
- textField.setSelectionStart(selectionStart);
- }
- });
- }
-
- /**
- * Sets the end index of the selection for this component.
- *
- * @param selectionEnd End index
- */
- public void setSelectionEnd(final int selectionEnd) {
- UIUtilities.invokeLater(new Runnable() {
-
- @Override
- public void run() {
- textField.setSelectionEnd(selectionEnd);
- }
- });
- }
-
- /**
- * {@inheritDoc}
- *
- * @param e Key event
- */
- @Override
- public void keyTyped(final KeyEvent e) {
- for (final KeyListener listener : listeners.get(KeyListener.class)) {
- listener.keyTyped(e);
- }
- }
-
- /**
- * {@inheritDoc}
- *
- * @param e Key event
- */
- @Override
- public void keyPressed(final KeyEvent e) {
- for (final KeyListener listener : listeners.get(KeyListener.class)) {
- listener.keyPressed(e);
- }
- }
-
- /**
- * {@inheritDoc}
- *
- * @param e Key event
- */
- @Override
- public void keyReleased(final KeyEvent e) {
- for (final KeyListener listener : listeners.get(KeyListener.class)) {
- listener.keyReleased(e);
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public void illegalCommand(final String reason) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- errorIndicator.setVisible(true);
- errorIndicator.setToolTipText(reason);
- wrapIndicator.setVisible(false);
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public void legalCommand() {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- errorIndicator.setVisible(false);
- errorIndicator.setToolTipText(null);
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public void wrappedText(final int count) {
- UIUtilities.invokeLater(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- wrapIndicator.setVisible(count > 1);
- wrapIndicator.setToolTipText(count + " lines");
- errorIndicator.setVisible(false);
- }
- });
- }
-
- /** {@inheritDoc} */
- @Override
- public void propertyChange(final PropertyChangeEvent evt) {
- if (!isFocusOwner()) {
- hideColourPicker();
- }
- }
- }
|