You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NickColourInputDialog.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.nickcolours;
  23. import com.dmdirc.addons.ui_swing.components.GenericTableModel;
  24. import com.dmdirc.addons.ui_swing.components.IconManager;
  25. import com.dmdirc.addons.ui_swing.components.colours.ColourChooser;
  26. import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
  27. import com.dmdirc.ui.messages.ColourManager;
  28. import java.awt.Color;
  29. import java.awt.Window;
  30. import javax.swing.JButton;
  31. import javax.swing.JLabel;
  32. import javax.swing.JTextField;
  33. import javax.swing.WindowConstants;
  34. import net.miginfocom.swing.MigLayout;
  35. /**
  36. * New nick colour input dialog.
  37. */
  38. public class NickColourInputDialog extends StandardDialog {
  39. /** A version number for this class. */
  40. private static final long serialVersionUID = 1;
  41. /** Whether or not this is a new entry (as opposed to editing an old one). */
  42. private boolean isnew;
  43. /** The row we're editing, if this isn't a new entry. */
  44. private final int row;
  45. private final ColourManager colourManager;
  46. /** The table model to modify entries in. */
  47. private final GenericTableModel<NickColourEntry> model;
  48. /** nickname textfield. */
  49. private JTextField nickname;
  50. /** network textfield. */
  51. private JTextField network;
  52. /** text colour input. */
  53. private ColourChooser textColour;
  54. /**
  55. * Creates a new instance of NickColourInputDialog.
  56. *
  57. * @param parentWindow The window that owns this dialog.
  58. * @param colourManager The colour manager to use to retrieve colours.
  59. * @param iconManager The icon manager to use for the dialog icon.
  60. * @param model The table model to modify entries in
  61. * @param row The row of the table we're editing
  62. * @param nickname The nickname that's currently set
  63. * @param network The network that's currently set
  64. * @param textcolour The text colour that's currently set
  65. */
  66. public NickColourInputDialog(
  67. final Window parentWindow,
  68. final ColourManager colourManager,
  69. final IconManager iconManager,
  70. final GenericTableModel<NickColourEntry> model, final int row,
  71. final String nickname, final String network,
  72. final Color textcolour) {
  73. super(parentWindow, ModalityType.MODELESS);
  74. this.colourManager = colourManager;
  75. this.model = model;
  76. this.row = row;
  77. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  78. initComponents(colourManager, iconManager, nickname, network, textcolour);
  79. initListeners();
  80. layoutComponents();
  81. setTitle("Nick colour editor");
  82. display();
  83. }
  84. /**
  85. * Creates a new instance of NickColourInputDialog.
  86. *
  87. * @param parentWindow The window that owns this dialog.
  88. * @param colourManager The colour manager to use to retrieve colours.
  89. * @param iconManager The icon manager to use for the dialog icon.
  90. * @param model The table model to modify entries in
  91. */
  92. public NickColourInputDialog(
  93. final Window parentWindow,
  94. final ColourManager colourManager,
  95. final IconManager iconManager,
  96. final GenericTableModel<NickColourEntry> model) {
  97. this(parentWindow, colourManager, iconManager, model, -1, "", "", null);
  98. isnew = true;
  99. }
  100. /**
  101. * Initialises the components.
  102. *
  103. * @param defaultNickname The default value for the nickname text field
  104. * @param defaultNetwork The default value for the network text field
  105. * @param defaultTextColour The default value for the text colour option
  106. */
  107. private void initComponents(
  108. final ColourManager colourManager,
  109. final IconManager iconManager,
  110. final String defaultNickname,
  111. final String defaultNetwork, final Color defaultTextColour) {
  112. orderButtons(new JButton(), new JButton());
  113. nickname = new JTextField(defaultNickname);
  114. network = new JTextField(defaultNetwork);
  115. textColour = new ColourChooser(colourManager, iconManager, "", true, true);
  116. }
  117. /** Initialises the listeners. */
  118. private void initListeners() {
  119. getOkButton().addActionListener(e -> { saveSettings(); dispose(); });
  120. getCancelButton().addActionListener(e -> dispose());
  121. }
  122. /** Lays out the components. */
  123. private void layoutComponents() {
  124. setLayout(new MigLayout("wrap 2"));
  125. add(new JLabel("Nickname: "));
  126. add(nickname, "growx");
  127. add(new JLabel("Network: "));
  128. add(network, "growx");
  129. add(new JLabel("Text colour: "));
  130. add(textColour, "growx");
  131. add(getLeftButton(), "right");
  132. add(getRightButton(), "right");
  133. pack();
  134. }
  135. /** Saves settings. */
  136. public void saveSettings() {
  137. final Color colour = NickColourUtils.getColorFromString(colourManager,
  138. textColour.getColour());
  139. final NickColourEntry entry = NickColourEntry.create(network.getText().toLowerCase(),
  140. nickname.getText().toLowerCase(),
  141. new Color(colour.getRed(), colour.getGreen(), colour.getBlue()));
  142. if (isnew) {
  143. model.replaceValueAt(entry, row);
  144. } else {
  145. model.addValue(entry);
  146. }
  147. }
  148. }