package com.ts3client.ui; import com.formdev.flatlaf.util.ScaledEmptyBorder; import com.formdev.flatlaf.util.UIScale; import com.ts3client.net.IconRepository; import com.ts3client.net.TeamspeakConnection; import javax.swing.BorderFactory; import javax.swing.DefaultListModel; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.ListSelectionModel; import javax.swing.SwingUtilities; import javax.swing.Timer; import javax.swing.filechooser.FileNameExtensionFilter; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.Window; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; /** * TeamSpeak's icon viewer: the icons uploaded to this virtual server on one side (which * can be added to and removed from here) and the ones the icon pack ships on the other. * *

Server icons arrive through the {@link IconRepository}, which downloads them in the * background, so the lists are simply repainted until every icon has turned up. */ final class IconChooserDialog extends JDialog { /** How often the lists are repainted while icons are still being downloaded. */ private static final int REFRESH_MS = 300; /** Give up repainting once nothing has arrived for this long. */ private static final int REFRESH_TIMEOUT_MS = 20_000; private final TeamspeakConnection conn; private final GroupIcons groupIcons; private final DefaultListModel remoteModel = new DefaultListModel<>(); private final JList remoteList = new JList<>(remoteModel); private final JList localList = new JList<>(new DefaultListModel<>()); private final JButton deleteButton = new JButton("Delete"); private final JButton selectButton = new JButton("Select"); private final JLabel status = new JLabel(" "); private long chosenIconId; private boolean accepted; IconChooserDialog(Window owner, TeamspeakConnection conn, GroupIcons groupIcons) { super(owner, "Icons", ModalityType.APPLICATION_MODAL); this.conn = conn; this.groupIcons = groupIcons; configure(remoteList); configure(localList); DefaultListModel localModel = (DefaultListModel) localList.getModel(); for (Long id : IconRepository.BUNDLED_IDS) localModel.addElement(id); JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, group("Remote", remoteList, remoteButtons()), group("Local", localList, localFooter())); split.setResizeWeight(0.6); status.setBorder(new ScaledEmptyBorder(2, 8, 2, 8)); status.setForeground(Theme.chatSystem()); getContentPane().setLayout(new BorderLayout()); getContentPane().add(split, BorderLayout.CENTER); JPanel south = new JPanel(new BorderLayout()); south.add(status, BorderLayout.NORTH); south.add(buttons(), BorderLayout.SOUTH); getContentPane().add(south, BorderLayout.SOUTH); Dialogs.closeOnEscape(this); setDefaultCloseOperation(DISPOSE_ON_CLOSE); setSize(UIScale.scale(new Dimension(520, 360))); setLocationRelativeTo(owner); loadRemoteIcons(); startRefreshing(); } /** @return whether an icon was picked; {@link #getIconId()} then holds it */ boolean isAccepted() { return accepted; } long getIconId() { return chosenIconId; } private void configure(JList list) { list.setLayoutOrientation(JList.HORIZONTAL_WRAP); list.setVisibleRowCount(-1); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setFixedCellWidth(UIScale.scale(32)); list.setFixedCellHeight(UIScale.scale(32)); list.setCellRenderer(new IconCellRenderer()); list.addListSelectionListener(e -> onSelectionChanged(list)); list.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2 && list.getSelectedValue() != null) choose(list.getSelectedValue()); } }); } /** Keeps one list's selection exclusive: picking on one side clears the other. */ private void onSelectionChanged(JList list) { JList other = list == remoteList ? localList : remoteList; if (list.getSelectedValue() != null) other.clearSelection(); deleteButton.setEnabled(remoteList.getSelectedValue() != null); selectButton.setEnabled(selectedIcon() != 0); } private long selectedIcon() { Long remote = remoteList.getSelectedValue(); if (remote != null) return remote; Long local = localList.getSelectedValue(); return local == null ? 0 : local; } private JPanel group(String title, JList list, Component footer) { JPanel panel = new JPanel(new BorderLayout(0, UIScale.scale(4))); panel.setBorder(BorderFactory.createTitledBorder(title)); panel.add(new JScrollPane(list), BorderLayout.CENTER); panel.add(footer, BorderLayout.SOUTH); return panel; } private Component remoteButtons() { JButton upload = new JButton("Upload"); upload.addActionListener(a -> uploadIcon()); deleteButton.setEnabled(false); deleteButton.addActionListener(a -> deleteSelectedIcon()); JPanel row = new JPanel(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 4, 0)); row.add(upload); row.add(deleteButton); return row; } private Component localFooter() { com.ts3client.gfx.IconPack active = IconTheme.get().activePack(); JLabel pack = new JLabel("Icon Pack: " + (active == null ? "none" : active.name())); pack.setBorder(new ScaledEmptyBorder(0, 4, 0, 4)); return pack; } private JPanel buttons() { selectButton.setEnabled(false); selectButton.addActionListener(a -> choose(selectedIcon())); JButton cancel = new JButton("Cancel"); cancel.addActionListener(a -> dispose()); JPanel panel = new JPanel(new BorderLayout()); JPanel right = new JPanel(); right.add(selectButton); right.add(cancel); panel.add(right, BorderLayout.EAST); getRootPane().setDefaultButton(selectButton); return panel; } private void choose(long iconId) { if (iconId == 0) return; chosenIconId = iconId; accepted = true; dispose(); } private void loadRemoteIcons() { status.setText("Loading icons…"); conn.requestServerIcons((ids, error) -> SwingUtilities.invokeLater(() -> { if (error != null) { status.setText("Could not list the server's icons: " + error); return; } status.setText(" "); remoteModel.clear(); for (Long id : ids) remoteModel.addElement(id); })); } private void uploadIcon() { JFileChooser chooser = new JFileChooser(); chooser.setDialogTitle("Select an image to upload"); chooser.setFileFilter(new FileNameExtensionFilter("Images", "png", "jpg", "jpeg", "gif", "bmp", "svg")); if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) return; File file = chooser.getSelectedFile(); status.setText("Uploading " + file.getName() + "…"); conn.uploadIcon(file, (iconId, error) -> SwingUtilities.invokeLater(() -> { if (error != null) { status.setText(" "); JOptionPane.showMessageDialog(this, "Error uploading icon: " + error, "Error", JOptionPane.ERROR_MESSAGE); return; } loadRemoteIcons(); startRefreshing(); })); } private void deleteSelectedIcon() { Long id = remoteList.getSelectedValue(); if (id == null) return; int answer = JOptionPane.showConfirmDialog(this, "Permanently delete this icon from the server?", "Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (answer != JOptionPane.YES_OPTION) return; conn.deleteIcon(id, error -> SwingUtilities.invokeLater(() -> { if (error != null) { JOptionPane.showMessageDialog(this, "Failed to delete remote icon file: " + error, "Error", JOptionPane.ERROR_MESSAGE); return; } remoteModel.removeElement(id); })); } /** * Repaints while the repository is still fetching icons; it hands them over silently, * so the only way to show them as they land is to keep asking. */ private void startRefreshing() { Timer timer = new Timer(REFRESH_MS, null); long deadline = System.currentTimeMillis() + REFRESH_TIMEOUT_MS; timer.addActionListener(e -> { remoteList.repaint(); localList.repaint(); if (!isDisplayable() || System.currentTimeMillis() > deadline || allIconsLoaded()) timer.stop(); }); timer.start(); } private boolean allIconsLoaded() { for (int i = 0; i < remoteModel.size(); i++) { if (groupIcons.icon(remoteModel.get(i)) == null) return false; } return true; } /** Draws one icon, blank until the repository has it. */ private final class IconCellRenderer extends javax.swing.DefaultListCellRenderer { @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean selected, boolean focused) { super.getListCellRendererComponent(list, "", index, selected, focused); long id = value instanceof Long ? (Long) value : 0; ImageIcon icon = groupIcons.icon(id); setIcon(icon); setHorizontalAlignment(CENTER); setToolTipText("Icon " + id); return this; } } /** Convenience for callers that only need the picked id. */ static long pick(Window owner, TeamspeakConnection conn, GroupIcons groupIcons, long current) { IconChooserDialog dialog = new IconChooserDialog(owner, conn, groupIcons); dialog.setVisible(true); return dialog.isAccepted() ? dialog.getIconId() : current; } }