Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

NickColourInputDialog.java 6.3KB

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