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.

UIAttachDialog.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.ui;
  23. import com.dmdirc.plugins.Service;
  24. import com.dmdirc.plugins.ServiceManager;
  25. import java.awt.BorderLayout;
  26. import java.awt.Component;
  27. import java.awt.event.ActionEvent;
  28. import java.awt.event.ActionListener;
  29. import java.util.List;
  30. import javax.swing.BorderFactory;
  31. import javax.swing.Box;
  32. import javax.swing.BoxLayout;
  33. import javax.swing.DefaultListCellRenderer;
  34. import javax.swing.DefaultListModel;
  35. import javax.swing.JButton;
  36. import javax.swing.JDialog;
  37. import javax.swing.JList;
  38. import javax.swing.JPanel;
  39. import javax.swing.WindowConstants;
  40. import javax.swing.event.ListSelectionEvent;
  41. import javax.swing.event.ListSelectionListener;
  42. /**
  43. * Simple dialog to pick and load a UI in an existing client.
  44. */
  45. public class UIAttachDialog extends JDialog implements ActionListener, ListSelectionListener {
  46. /** Java Serialisation version ID. */
  47. private static final long serialVersionUID = 1;
  48. /** Sensible component gap. */
  49. private static final int GAP = 5;
  50. /** Services list. */
  51. private final JList<Service> list;
  52. /** Service manager to use to find UI services. */
  53. private final ServiceManager serviceManager;
  54. /**
  55. * Creates a new dialog allowing the user to select and load a UI.
  56. *
  57. * @param serviceManager Service manager to use to find UI services.
  58. */
  59. public UIAttachDialog(final ServiceManager serviceManager) {
  60. this.serviceManager = serviceManager;
  61. list = initList();
  62. setLayout(new BorderLayout());
  63. add(list, BorderLayout.CENTER);
  64. add(initButtonPanel(), BorderLayout.PAGE_END);
  65. }
  66. /** Initialises the model with a list of UIs. */
  67. private JList<Service> initList() {
  68. final DefaultListModel<Service> model = new DefaultListModel<>();
  69. final JList<Service> newList = new JList<>(model);
  70. newList.setCellRenderer(new ServiceRenderer());
  71. newList.addListSelectionListener(this);
  72. final List<Service> services = serviceManager.getServicesByType("ui");
  73. services.forEach(model::addElement);
  74. newList.setSelectedIndex(0);
  75. return newList;
  76. }
  77. /** Initialises the OK and and cancel button panel. */
  78. private JPanel initButtonPanel() {
  79. final JPanel panel = new JPanel();
  80. final BoxLayout box = new BoxLayout(panel, BoxLayout.LINE_AXIS);
  81. final JButton ok = new JButton("OK");
  82. final JButton cancel = new JButton("Cancel");
  83. ok.setActionCommand("OK");
  84. ok.addActionListener(this);
  85. cancel.setActionCommand("Cancel");
  86. cancel.addActionListener(this);
  87. panel.setLayout(box);
  88. panel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
  89. panel.add(Box.createHorizontalGlue());
  90. if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
  91. panel.add(ok);
  92. panel.add(Box.createHorizontalStrut(GAP));
  93. panel.add(cancel);
  94. } else {
  95. panel.add(cancel);
  96. panel.add(Box.createHorizontalStrut(GAP));
  97. panel.add(ok);
  98. }
  99. return panel;
  100. }
  101. /** Displays this dialog. */
  102. public void display() {
  103. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  104. pack();
  105. setVisible(true);
  106. }
  107. @Override
  108. public void actionPerformed(final ActionEvent e) {
  109. if ("OK".equals(e.getActionCommand())) {
  110. if (list.getSelectedValue() == null) {
  111. return;
  112. }
  113. list.getSelectedValue().activate();
  114. }
  115. dispose();
  116. }
  117. @Override
  118. public void valueChanged(final ListSelectionEvent e) {
  119. if (!e.getValueIsAdjusting()) {
  120. list.setSelectedIndex(e.getLastIndex());
  121. }
  122. }
  123. /**
  124. * Renders plugin services properly.
  125. */
  126. private static class ServiceRenderer extends DefaultListCellRenderer {
  127. /** Java Serialisation version ID. */
  128. private static final long serialVersionUID = 1;
  129. @Override
  130. public Component getListCellRendererComponent(final JList<?> list,
  131. final Object value, final int index, final boolean isSelected,
  132. final boolean cellHasFocus) {
  133. final Component label = super.getListCellRendererComponent(list,
  134. value, index, isSelected, cellHasFocus);
  135. if (value instanceof Service) {
  136. setText(((Service) value).getName());
  137. }
  138. return label;
  139. }
  140. }
  141. }