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.

AliasSubstitutionsPanel.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2006-2014 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.ui_swing.dialogs.aliases;
  23. import com.dmdirc.actions.ActionSubstitutor;
  24. import com.dmdirc.actions.ActionSubstitutorFactory;
  25. import com.dmdirc.actions.CoreActionType;
  26. import com.dmdirc.addons.ui_swing.components.substitutions.Substitution;
  27. import com.dmdirc.addons.ui_swing.components.substitutions.SubstitutionLabel;
  28. import com.dmdirc.addons.ui_swing.components.substitutions.SubstitutionsPanel;
  29. import com.dmdirc.interfaces.actions.ActionType;
  30. import java.util.ArrayList;
  31. import java.util.Map.Entry;
  32. import javax.swing.SwingUtilities;
  33. /**
  34. * Lists substitutions for aliases.
  35. */
  36. public class AliasSubstitutionsPanel extends SubstitutionsPanel<ActionType> {
  37. /**
  38. * A version number for this class. It should be changed whenever the class structure is changed
  39. * (or anything else that would prevent serialized objects being unserialized with the new
  40. * class).
  41. */
  42. private static final long serialVersionUID = 1;
  43. /** Factory to use to create {@link ActionSubstitutor}s. */
  44. private final ActionSubstitutorFactory substitutorFactory;
  45. /**
  46. * Instantiates the panel.
  47. *
  48. * @param substitutorFactory Factory to use to create {@link ActionSubstitutor}s.
  49. */
  50. public AliasSubstitutionsPanel(final ActionSubstitutorFactory substitutorFactory) {
  51. super("Substitutions may be used in the response field",
  52. SubstitutionsPanel.Alignment.VERTICAL,
  53. CoreActionType.UNKNOWN_COMMAND);
  54. this.substitutorFactory = substitutorFactory;
  55. }
  56. /**
  57. * Sets the action type for this substitution panel.
  58. *
  59. * @param type New action type
  60. */
  61. @Override
  62. public void setType(final ActionType type) {
  63. SwingUtilities.invokeLater(new Runnable() {
  64. /** {@inheritDoc} */
  65. @Override
  66. public void run() {
  67. substitutions = new ArrayList<>();
  68. if (type != null) {
  69. final ActionSubstitutor sub = substitutorFactory.getActionSubstitutor(type);
  70. for (final Entry<String, String> entry : sub.getComponentSubstitutions().
  71. entrySet()) {
  72. substitutions.add(new SubstitutionLabel(new Substitution(entry.getValue(),
  73. entry.getKey())));
  74. }
  75. for (final String entry : sub.getConfigSubstitutions()) {
  76. substitutions.add(new SubstitutionLabel(new Substitution(entry,
  77. entry)));
  78. }
  79. for (final Entry<String, String> entry : sub.getServerSubstitutions().
  80. entrySet()) {
  81. substitutions.add(new SubstitutionLabel(new Substitution(entry.getValue(),
  82. entry.getKey())));
  83. }
  84. for (int i = 1; i < 4; i++) {
  85. substitutions.add(new SubstitutionLabel(new Substitution("Argument #" + i,
  86. Integer.toString(i))));
  87. }
  88. for (int i = 1; i < 4; i++) {
  89. substitutions.add(new SubstitutionLabel(new Substitution("Argument #" + i
  90. + " onwards", i + "-")));
  91. }
  92. }
  93. layoutComponents();
  94. validate();
  95. layoutComponents();
  96. }
  97. });
  98. }
  99. }