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

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