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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2006-2013 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.serverlistdialog;
  23. import com.dmdirc.addons.serverlists.ServerGroupItem;
  24. import com.dmdirc.addons.ui_swing.SwingController;
  25. import com.dmdirc.addons.ui_swing.components.LockedLayer;
  26. import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
  27. import com.dmdirc.ui.core.util.URLHandler;
  28. import java.awt.color.ColorSpace;
  29. import java.awt.event.ActionEvent;
  30. import java.awt.event.ActionListener;
  31. import java.awt.image.ColorConvertOp;
  32. import javax.swing.JButton;
  33. import net.miginfocom.swing.MigLayout;
  34. import org.jdesktop.jxlayer.JXLayer;
  35. import org.jdesktop.jxlayer.plaf.effect.BufferedImageOpEffect;
  36. /**
  37. * Dialog to show and edit server lists.
  38. */
  39. public final class ServerListDialog extends StandardDialog implements
  40. ActionListener, ServerListListener {
  41. /** Serial version UID. */
  42. private static final long serialVersionUID = 2;
  43. /** Server list model. */
  44. private final ServerListModel model;
  45. /** Connect button. */
  46. private final JButton connectButton;
  47. /** Info lock. */
  48. private final LockedLayer<Info> infoLock;
  49. /** Perform lock. */
  50. private final LockedLayer<Perform> performLock;
  51. /** Profile lock. */
  52. private final LockedLayer<Profiles> profileLock;
  53. /** Settings lock. */
  54. private final LockedLayer<Settings> settingsLock;
  55. /** Info layer. */
  56. private final JXLayer<Info> infoLayer;
  57. /** Perform layer. */
  58. private final JXLayer<Perform> performLayer;
  59. /** Profile layer. */
  60. private final JXLayer<Profiles> profileLayer;
  61. /** Settings layer. */
  62. private final JXLayer<Settings> settingsLayer;
  63. /** Help panel. */
  64. private final Help help;
  65. /**
  66. * Creates a new server list dialog.
  67. *
  68. * @param controller Swing controller
  69. * @param modalityType Desired modality
  70. * @param urlHandler The URL Handler to use to handle clicked links
  71. */
  72. public ServerListDialog(final SwingController controller,
  73. final URLHandler urlHandler) {
  74. super(controller, controller.getMainFrame(), ModalityType.MODELESS);
  75. setTitle("Server List");
  76. model = new ServerListModel(controller.getPluginManager(), controller.getMain().getServerManager());
  77. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  78. connectButton = new JButton("Connect");
  79. profileLock = new LockedLayer<Profiles>(new BufferedImageOpEffect(
  80. new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY),
  81. null)));
  82. performLock = new LockedLayer<Perform>(new BufferedImageOpEffect(
  83. new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY),
  84. null)));
  85. settingsLock = new LockedLayer<Settings>(new BufferedImageOpEffect(
  86. new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY),
  87. null)));
  88. infoLock = new LockedLayer<Info>(new BufferedImageOpEffect(
  89. new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY),
  90. null)));
  91. profileLayer = new JXLayer<Profiles>(new Profiles(model, controller),
  92. profileLock);
  93. performLayer = new JXLayer<Perform>(new Perform(controller, model),
  94. performLock);
  95. settingsLayer = new JXLayer<Settings>(new Settings(controller, model),
  96. settingsLock);
  97. infoLayer = new JXLayer<Info>(new Info(model, urlHandler), infoLock);
  98. help = new Help();
  99. lockLayers();
  100. setLayout(new MigLayout("fill, wrap 2, wmin 600, wmax 600"));
  101. add(new Tree(controller, model, this),
  102. "grow, spany 4, wmax 150, wmin 150");
  103. add(help, "pos 160 0.5al");
  104. add(infoLayer, "growx, pushx");
  105. add(settingsLayer, "grow, push, gaptop unrel, gapbottom unrel");
  106. add(performLayer, "grow, push");
  107. add(profileLayer, "growx, pushx");
  108. add(connectButton, "skip 1, split 3, right, gapright unrel*2, "
  109. + "sgx button");
  110. add(getLeftButton(), "right, sgx button");
  111. add(getRightButton(), "right, sgx button");
  112. addListeners();
  113. }
  114. /**
  115. * Adds requires listeners to objects.
  116. */
  117. private void addListeners() {
  118. model.addServerListListener(this);
  119. connectButton.addActionListener(this);
  120. getOkButton().addActionListener(this);
  121. getCancelButton().addActionListener(this);
  122. }
  123. /**
  124. * {@inheritDoc}
  125. *
  126. * @param e Action event
  127. */
  128. @Override
  129. public void actionPerformed(final ActionEvent e) {
  130. if (e.getSource() == getOkButton()) {
  131. model.dialogClosed(true);
  132. dispose();
  133. } else if (e.getSource() == getCancelButton()) {
  134. model.dialogClosed(false);
  135. dispose();
  136. } else if (e.getSource() == connectButton) {
  137. model.getSelectedItem().connect();
  138. }
  139. }
  140. /**
  141. * Lock or unlock layers.
  142. */
  143. private void lockLayers() {
  144. final boolean lock = !model.hasItems()
  145. || model.getSelectedItem() == null;
  146. performLock.setLocked(lock);
  147. settingsLock.setLocked(lock);
  148. infoLock.setLocked(lock);
  149. profileLock.setLocked(lock);
  150. connectButton.setEnabled(!lock);
  151. performLayer.setVisible(!lock);
  152. settingsLayer.setVisible(!lock);
  153. infoLayer.setVisible(!lock);
  154. profileLayer.setVisible(!lock);
  155. help.setVisible(lock);
  156. }
  157. /** {@inheritDoc} */
  158. @Override
  159. public void serverGroupChanged(final ServerGroupItem item) {
  160. lockLayers();
  161. }
  162. /** {@inheritDoc} */
  163. @Override
  164. public void dialogClosed(final boolean save) {
  165. if (save) {
  166. model.save();
  167. }
  168. }
  169. /** {@inheritDoc} */
  170. @Override
  171. public void serverGroupAdded(final ServerGroupItem parent,
  172. final ServerGroupItem group) {
  173. lockLayers();
  174. }
  175. /** {@inheritDoc} */
  176. @Override
  177. public void serverGroupRemoved(final ServerGroupItem parent,
  178. final ServerGroupItem group) {
  179. lockLayers();
  180. }
  181. }