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

package example;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.function.Function;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.MouseInputListener;

public class MainPanel extends JPanel {
  protected final JLayeredPane layeredPane = new JLayeredPane() {
    @Override public boolean isOptimizedDrawingEnabled() {
      return false;
    }
  };
  protected final JToolBar toolbar = new JToolBar("Resizable Components");
  protected final Point pt = new Point();

  public MainPanel() {
    super(new BorderLayout());
    JPopupMenu popup = new JPopupMenu() {
      @Override public void show(Component c, int x, int y) {
        pt.setLocation(x, y);
        super.show(c, x, y);
      }
    };
    popup.add("table").addActionListener(e -> createTable());
    popup.add("tree").addActionListener(e -> createTree());

    layeredPane.setComponentPopupMenu(popup);
    // ??? for 1.5.0
    // layeredPane.addMouseListener(new MouseAdapter() {
    //   /* Dummy listener */
    // });
    add(layeredPane);
    toolbar.add(new AbstractAction("add table") {
      @Override public void actionPerformed(ActionEvent e) {
        pt.setLocation(pt.x + 20, pt.y + 20);
        createTable();
      }
    });
    toolbar.addSeparator();
    toolbar.add(new AbstractAction("add tree") {
      @Override public void actionPerformed(ActionEvent e) {
        pt.setLocation(pt.x + 20, pt.y + 20);
        createTree();
      }
    });
    add(toolbar, BorderLayout.NORTH);
    setPreferredSize(new Dimension(320, 240));
  }

  protected final void createTree() {
    JTree tree = new JTree();
    tree.setVisibleRowCount(8);
    Component c = new JScrollPane(tree);
    Dimension r = c.getPreferredSize();
    JResizer resizer = new JResizer(new BorderLayout());
    resizer.add(c);
    resizer.setBounds(pt.x, pt.y, r.width, r.height);
    layeredPane.add(resizer);
    layeredPane.moveToFront(resizer);
  }

  protected final void createTable() {
    JTable table = new JTable(12, 3);
    table.setPreferredScrollableViewportSize(new Dimension(160, 160));
    Component c = new JScrollPane(table);
    Dimension r = c.getPreferredSize();
    JResizer resizer = new JResizer(new BorderLayout());
    resizer.add(c);
    resizer.setBounds(pt.x, pt.y, r.width, r.height);
    layeredPane.add(resizer);
    layeredPane.moveToFront(resizer);
  }

  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 JResizer extends JPanel { // implements Serializable {
  private transient MouseInputListener resizeListener;

  protected JResizer(LayoutManager layout) {
    super(layout);
  }

  @Override public void updateUI() {
    removeMouseListener(resizeListener);
    removeMouseMotionListener(resizeListener);
    super.updateUI();
    resizeListener = new ResizeMouseListener();
    addMouseListener(resizeListener);
    addMouseMotionListener(resizeListener);
    setBorder(new DefaultResizableBorder());
  }
  // private void writeObject(ObjectOutputStream out) throws IOException {}
  // private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {}
  // private void readObjectNoData() throws ObjectStreamException {}
  // private void readObject() {
  //   this.resizeListener = new ResizeMouseListener();
  // }
  // private Object readResolve() {
  //   this.resizeListener = new ResizeMouseListener();
  //   return this;
  // }

  @Override public void setBorder(Border border) {
    removeMouseListener(resizeListener);
    removeMouseMotionListener(resizeListener);
    if (border instanceof ResizableBorder) {
      addMouseListener(resizeListener);
      addMouseMotionListener(resizeListener);
    }
    super.setBorder(border);
  }
}

// Resizable Components - <NAME>'s Weblog
// http://www.jroller.com/santhosh/entry/resizable_components
interface ResizableBorder extends Border {
  Cursor getResizeCursor(MouseEvent e);
}

class DefaultResizableBorder implements ResizableBorder, SwingConstants {
  private static final int SIZE = 6;

  private enum Locations {
    NORTH(Cursor.N_RESIZE_CURSOR, r -> new Point(r.x + r.width / 2 - SIZE / 2, r.y)),
    SOUTH(Cursor.S_RESIZE_CURSOR, r -> new Point(r.x + r.width / 2 - SIZE / 2, r.y + r.height - SIZE)),
    WEST(Cursor.W_RESIZE_CURSOR, r -> new Point(r.x, r.y + r.height / 2 - SIZE / 2)),
    EAST(Cursor.E_RESIZE_CURSOR, r -> new Point(r.x + r.width - SIZE, r.y + r.height / 2 - SIZE / 2)),
    NORTH_WEST(Cursor.NW_RESIZE_CURSOR, r -> new Point(r.x, r.y)),
    NORTH_EAST(Cursor.NE_RESIZE_CURSOR, r -> new Point(r.x + r.width - SIZE, r.y)),
    SOUTH_WEST(Cursor.SW_RESIZE_CURSOR, r -> new Point(r.x, r.y + r.height - SIZE)),
    SOUTH_EAST(Cursor.SE_RESIZE_CURSOR, r -> new Point(r.x + r.width - SIZE, r.y + r.height - SIZE));

    private final int cursor;
    @SuppressWarnings("ImmutableEnumChecker")
    private final Function<Rectangle, Point> location;

    Locations(int cursor, Function<Rectangle, Point> getPoint) {
      this.cursor = cursor;
      this.location = getPoint;
    }

    public Point getPoint(Rectangle r) {
      return location.apply(r);
    }

    public Cursor getCursor() {
      return Cursor.getPredefinedCursor(cursor);<commit_msg>ErrprProne: ImmutableEnumChecker<commit_after>// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.MouseInputListener;

public class MainPanel extends JPanel {
  protected final JLayeredPane layeredPane = new JLayeredPane() {
    @Override public boolean isOptimizedDrawingEnabled() {
      return false;
    }
  };
  protected final JToolBar toolbar = new JToolBar("Resizable Components");
  protected final Point pt = new Point();

  public MainPanel() {
    super(new BorderLayout());
    JPopupMenu popup = new JPopupMenu() {
      @Override public void show(Component c, int x, int y) {
        pt.setLocation(x, y);
        super.show(c, x, y);
      }
    };
    popup.add("table").addActionListener(e -> createTable());
    popup.add("tree").addActionListener(e -> createTree());

    layeredPane.setComponentPopupMenu(popup);
    // ??? for 1.5.0
    // layeredPane.addMouseListener(new MouseAdapter() {
    //   /* Dummy listener */
    // });
    add(layeredPane);
    toolbar.add(new AbstractAction("add table") {
      @Override public void actionPerformed(ActionEvent e) {
        pt.setLocation(pt.x + 20, pt.y + 20);
        createTable();
      }
    });
    toolbar.addSeparator();
    toolbar.add(new AbstractAction("add tree") {
      @Override public void actionPerformed(ActionEvent e) {
        pt.setLocation(pt.x + 20, pt.y + 20);
        createTree();
      }
    });
    add(toolbar, BorderLayout.NORTH);
    setPreferredSize(new Dimension(320, 240));
  }

  protected final void createTree() {
    JTree tree = new JTree();
    tree.setVisibleRowCount(8);
    Component c = new JScrollPane(tree);
    Dimension r = c.getPreferredSize();
    JResizer resizer = new JResizer(new BorderLayout());
    resizer.add(c);
    resizer.setBounds(pt.x, pt.y, r.width, r.height);
    layeredPane.add(resizer);
    layeredPane.moveToFront(resizer);
  }

  protected final void createTable() {
    JTable table = new JTable(12, 3);
    table.setPreferredScrollableViewportSize(new Dimension(160, 160));
    Component c = new JScrollPane(table);
    Dimension r = c.getPreferredSize();
    JResizer resizer = new JResizer(new BorderLayout());
    resizer.add(c);
    resizer.setBounds(pt.x, pt.y, r.width, r.height);
    layeredPane.add(resizer);
    layeredPane.moveToFront(resizer);
  }

  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 JResizer extends JPanel { // implements Serializable {
  private transient MouseInputListener resizeListener;

  protected JResizer(LayoutManager layout) {
    super(layout);
  }

  @Override public void updateUI() {
    removeMouseListener(resizeListener);
    removeMouseMotionListener(resizeListener);
    super.updateUI();
    resizeListener = new ResizeMouseListener();
    addMouseListener(resizeListener);
    addMouseMotionListener(resizeListener);
    setBorder(new DefaultResizableBorder());
  }
  // private void writeObject(ObjectOutputStream out) throws IOException {}
  // private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {}
  // private void readObjectNoData() throws ObjectStreamException {}
  // private void readObject() {
  //   this.resizeListener = new ResizeMouseListener();
  // }
  // private Object readResolve() {
  //   this.resizeListener = new ResizeMouseListener();
  //   return this;
  // }

  @Override public void setBorder(Border border) {
    removeMouseListener(resizeListener);
    removeMouseMotionListener(resizeListener);
    if (border instanceof ResizableBorder) {
      addMouseListener(resizeListener);
      addMouseMotionListener(resizeListener);
    }
    super.setBorder(border);
  }
}

// Resizable Components - <NAME>'s Weblog
// http://www.jroller.com/santhosh/entry/resizable_components
interface ResizableBorder extends Border {
  Cursor getResizeCursor(MouseEvent e);
}

class DefaultResizableBorder implements ResizableBorder, SwingConstants {
  private static final int SIZE = 6;

  private enum Locations {
    NORTH(Cursor.N_RESIZE_CURSOR) {
      @Override public Point getPoint(Rectangle r) {
        return new Point(r.x + r.width / 2 - SIZE / 2, r.y);
      }
    },
    SOUTH(Cursor.S_RESIZE_CURSOR) {
      @Override public Point getPoint(Rectangle r) {
        return new Point(r.x + r.width / 2 - SIZE / 2, r.y + r.height - SIZE);
      }
    },
    WEST(Cursor.W_RESIZE_CURSOR) {
      @Override public Point getPoint(Rectangle r) {
        return new Point(r.x, r.y + r.height / 2 - SIZE / 2);
      }
    },
    EAST(Cursor.E_RESIZE_CURSOR) {
      @Override public Point getPoint(Rectangle r) {
        return new Point(r.x + r.width - SIZE, r.y + r.height / 2 - SIZE / 2);
      }
    },
    NORTH_WEST(Cursor.NW_RESIZE_CURSOR) {
      @Override public Point getPoint(Rectangle r) {
        return new Point(r.x, r.y);
      }
    },
    NORTH_EAST(Cursor.NE_RESIZE_CURSOR) {
      @Override public Point getPoint(Rectangle r) {
        return new Point(r.x + r.width - SIZE, r.y);
      }
    },
    SOUTH_WEST(Cursor.SW_RESIZE_CURSOR) {
      @Override public Point getPoint(Rectangle r) {
        return new Point(r.x, r.y + r.height - SIZE);
      }
    },
    SOUTH_EAST(Cursor.SE_RESIZE_CURSOR) {
      @Override public Point getPoint(Rectangle r) {
        return new Point(r.x + r.width - SIZE, r.y + r.height - SIZE);
      }
    };

    private final int cursor;
    // @SuppressWarnings("ImmutableEnumChecker")

    Locations(int cursor) {
      this.cursor = cursor;
    }

    abstract Point getPoint(Rectangle r);

    public Cursor getCursor() {
      return Cursor.getPredefinedCursor(cursor);
{'fraction_non_alphanumeric': 0.12081889256065124, 'fraction_numerical': 0.006125574272588055, 'mean_word_length': 3.4140875133404482, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6031946', 'n_tokens_mistral': 3971, 'n_tokens_neox': 3831, 'n_words': 836}