// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import javax.swing.event.MouseInputListener;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new GridLayout(1, 2));
    add(makeTitledPanel("Default", new JScrollPane(new JList<>(makeModel()))));
    add(makeTitledPanel("clearSelection", new JScrollPane(makeList(makeModel()))));
    setPreferredSize(new Dimension(320, 240));
  }

  private static ListModel<String> makeModel() {
    DefaultListModel<String> model = new DefaultListModel<>();
    model.addElement("aaaaaaa");
    model.addElement("bbbbbbbbbbbbb");
    model.addElement("cccccccccc");
    model.addElement("ddddddddd");
    model.addElement("eeeeeeeeee");
    return model;
  }

  private static JList<String> makeList(ListModel<String> model) {
    return new JList<String>(model) {
      private transient MouseInputListener listener;

      @Override public void updateUI() {
        removeMouseListener(listener);
        removeMouseMotionListener(listener);
        setForeground(null);
        setBackground(null);
        setSelectionForeground(null);
        setSelectionBackground(null);
        super.updateUI();
        listener = new ClearSelectionListener();
        addMouseListener(listener);
        addMouseMotionListener(listener);
      }
    };
    // list.putClientProperty("List.isFileList", Boolean.TRUE);
    // list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    // list.setFixedCellWidth(64);
    // list.setFixedCellHeight(64);
    // list.setVisibleRowCount(0);
    // return list;
  }

  private static Component makeTitledPanel(String title, Component c) {
    JPanel p = new JPanel(new BorderLayout());
    p.setBorder(BorderFactory.createTitledBorder(title));
    p.add(c);
    return p;
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(MainPanel::createAndShowGui);
  }

  private static void createAndShowGui() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
      Toolkit.getDefaultToolkit().beep();
    }
    JFrame frame = new JFrame("@title@");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

class ClearSelectionListener extends MouseInputAdapter {
  private boolean startOutside;

  private static <E> void clearSelectionAndFocus(JList<E> list) {
    list.clearSelection();
    list.getSelectionModel().setAnchorSelectionIndex(-1);
    list.getSelectionModel().setLeadSelectionIndex(-1);
  }

  private static <E> boolean contains(JList<E> list, Point pt) {
    for (int i = 0; i < list.getModel().getSize(); i++) {
      if (list.getCellBounds(i, i).contains(pt)) {
        return true;
      }
    }
    return false;
  }

  @Override public void mousePressed(MouseEvent e) {
    JList<?> list = (JList<?>) e.getComponent();
    startOutside = !contains(list, e.getPoint());
    if (startOutside) {
      clearSelectionAndFocus(list);
    }
  }

  @Override public void mouseReleased(MouseEvent e) {
    startOutside = false;
  }

  @Override public void mouseDragged(MouseEvent e) {
    JList<?> list = (JList<?>) e.getComponent();
    if (contains(list, e.getPoint())) {
      startOutside = false;
    } else if (startOutside) {
      clearSelectionAndFocus(list);
    }
  }
}

@@ -716,15 +716,17 @@
 nt(%22
-aaaaaaa
+000000000
 %22);%0A
@@ -751,21 +751,17 @@
 nt(%22
-bbbbbbbbbbbbb
+111111111
 %22);%0A
@@ -786,18 +786,18 @@
 nt(%22
-cccccccccc
+2222222222
 %22);%0A
@@ -822,17 +822,13 @@
 nt(%22
-ddddddddd
+33333
 %22);%0A
@@ -853,18 +853,19 @@
 nt(%22
-eeeeeeeeee
+44444444444
 %22);%0A
@@ -903,29 +903,28 @@
 e static
+ %3CE%3E
  JList%3C
-String
+E
 %3E makeLi
@@ -932,30 +932,25 @@
 t(ListModel%3C
-String
+E
 %3E model) %7B%0A 
@@ -969,22 +969,17 @@
 w JList%3C
-String
+E
 %3E(model)
@@ -1034,17 +1034,16 @@
 stener;%0A
-%0A
       @O

use the type parameter E to static generic method instead of type String