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.

ServerGroupItemBase.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.serverlists;
  23. import com.dmdirc.interfaces.config.ConfigProvider;
  24. import com.dmdirc.interfaces.config.IdentityController;
  25. /**
  26. * Abstract base class for {@link ServerGroupItem}s.
  27. *
  28. * @since 0.6.4
  29. */
  30. public abstract class ServerGroupItemBase implements ServerGroupItem {
  31. /** The controller to read/write settings with. */
  32. private final IdentityController identityController;
  33. /** Whether or not this item has been modified. */
  34. private boolean modified;
  35. /** The name of the item. */
  36. private String name;
  37. /** The name of the profile to use. */
  38. private String profile;
  39. public ServerGroupItemBase(final IdentityController identityController) {
  40. this.identityController = identityController;
  41. }
  42. /** {@inheritDoc} */
  43. @Override
  44. public boolean isModified() {
  45. return modified;
  46. }
  47. /** {@inheritDoc} */
  48. @Override
  49. public void setModified(final boolean isModified) {
  50. this.modified = isModified;
  51. }
  52. /** {@inheritDoc} */
  53. @Override
  54. public String getName() {
  55. return name;
  56. }
  57. /** {@inheritDoc} */
  58. @Override
  59. public void setName(final String name) {
  60. setModified(true);
  61. this.name = name;
  62. }
  63. /** {@inheritDoc} */
  64. @Override
  65. public String getPath() {
  66. if (getParent() != null) {
  67. return getParent().getPath() + " → " + getName();
  68. }
  69. return getName();
  70. }
  71. /** {@inheritDoc} */
  72. @Override
  73. public String getProfile() {
  74. return profile;
  75. }
  76. /** {@inheritDoc} */
  77. @Override
  78. public void setProfile(final String profile) {
  79. setModified(true);
  80. this.profile = profile;
  81. }
  82. /**
  83. * Returns the parent group of this item, or
  84. * <code>null</code> if the item is a root group.
  85. *
  86. * @return This item's parent group
  87. */
  88. protected abstract ServerGroup getParent();
  89. /**
  90. * Returns the {@link ConfigProvider} which corresponds to this server's desired profile.
  91. *
  92. * @return This server's profile identity
  93. */
  94. protected ConfigProvider getProfileIdentity() {
  95. if (profile != null) {
  96. for (ConfigProvider identity : identityController.getProvidersByType("profile")) {
  97. if (profile.equals(identity.getName())) {
  98. return identity;
  99. }
  100. }
  101. }
  102. if (getParent() == null) {
  103. return identityController.getProvidersByType("profile").get(0);
  104. } else {
  105. return getParent().getProfileIdentity();
  106. }
  107. }
  108. }