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

package example;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Objects;
import java.util.stream.Stream;
import javax.swing.*;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());

    JDesktopPane desktop = new JDesktopPane();
    desktop.setDesktopManager(new DefaultDesktopManager() {
      @Override public void iconifyFrame(JInternalFrame f) {
        Rectangle r = this.getBoundsForIconOf(f);
        r.width = f.getDesktopIcon().getPreferredSize().width;
        f.getDesktopIcon().setBounds(r);
        super.iconifyFrame(f);
      }
    });
    desktop.add(createFrame("looooooooooooong title #", 1));
    desktop.add(createFrame("#", 0));

    add(desktop);
    add(new JButton(new AbstractAction("add") {
      private int num = 2;
      @Override public void actionPerformed(ActionEvent e) {
        JInternalFrame f = createFrame("#", num);
        desktop.add(f);
        desktop.getDesktopManager().activateFrame(f);
        num++;
      }
    }), BorderLayout.SOUTH);
    setPreferredSize(new Dimension(320, 240));
  }

  public JInternalFrame createFrame(String t, int i) {
    JInternalFrame f = new JInternalFrame(t + i, true, true, true, true);
    f.setDesktopIcon(new JInternalFrame.JDesktopIcon(f) {
      @Override public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();
        String title = f.getTitle();
        Font font = getFont();
        if (Objects.nonNull(font)) {
          testWidth();

          // @see javax/swing/plaf/basic/BasicInternalFrameTitlePane.java Handler#minimumLayoutSize(Container)
          // Calculate width.
          int buttonsW = 22;
          if (f.isClosable()) {
            buttonsW += 19;
          }
          if (f.isMaximizable()) {
            buttonsW += 19;
          }
          if (f.isIconifiable()) {
            buttonsW += 19;
          }
          // buttonsW = Math.max(buttonsW, buttonsW2);

          FontMetrics fm = getFontMetrics(font);
          int titleW = SwingUtilities.computeStringWidth(fm, title);
          Insets i = getInsets();
          d.width = buttonsW + i.left + i.right + titleW + 2 + 2 + 2; // 2: Magic number of gap between icons
          d.height = Math.min(27, d.height); // 27: Magic number for NimbusLookAndFeel
          System.out.println("BasicInternalFrameTitlePane: " + d.width);
        }
        return d;
      }

      private void testWidth() {
        Dimension dim = getLayout().minimumLayoutSize(this);
        System.out.println("minimumLayoutSize: " + dim.width);

        int buttonsW = SwingUtils.descendants(this)
            .filter(AbstractButton.class::isInstance)
            .mapToInt(c -> c.getPreferredSize().width)
            .sum();
        System.out.println("Total width of all buttons: " + buttonsW);
      }
    });
    f.setSize(200, 100);
    f.setVisible(true);
    f.setLocation(5 + 40 * i, 5 + 50 * i);
    return f;
  }

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

  private static void createAndShowGui() {
    try {
      UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
    } 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);
  }
}

final class SwingUtils {
  private SwingUtils() {
    /* Singleton */
  }

  public static Stream<Component> descendants(Container parent) {
    return Stream.of(parent.getComponents())
        .filter(Container.class::isInstance).map(Container.class::cast)
        .flatMap(c -> Stream.concat(Stream.of(c), descendants(c)));
  }
}

@@ -129,55 +129,70 @@
 ava.
-awt.event.ActionEvent;%0Aimport java.util.Objects
+util.Objects;%0Aimport java.util.concurrent.atomic.AtomicInteger
 ;%0Aim
@@ -341,25 +341,24 @@
 rLayout());%0A
-%0A
     JDesktop
@@ -822,58 +822,80 @@
     
-add(desktop);%0A    add(new JButton(new AbstractActi
+AtomicInteger idx = new AtomicInteger(2);%0A    JButton button = new JButt
 on(%22
@@ -903,101 +903,46 @@
 dd%22)
- %7B
+;
 %0A    
-  private int num = 2;%0A      @Override public void actionPerformed(ActionEvent e)
+button.addActionListener(e -%3E
  %7B%0A
-  
     
@@ -983,16 +983,32 @@
 #%22, 
-num
+idx.getAndIncrement()
 );%0A
-  
     
@@ -1021,26 +1021,24 @@
 top.add(f);%0A
-  
       deskto
@@ -1085,33 +1085,45 @@
     
-    num++;%0A      %7D%0A    %7D)
+%7D);%0A%0A    add(desktop);%0A    add(button
 , Bo

use a lambda expression instead of an anonymous class(ActionListener)