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

package example;

import java.awt.*;
import java.beans.PropertyVetoException;
import javax.swing.*;

public final class MainPanel extends JPanel {
  private static final Dimension ICON_SIZE = new Dimension(150, 40);
  private final String info = String.format("JDesktopIcon: %dx%d", ICON_SIZE.width, ICON_SIZE.height);
  public final JCheckBox check = new JCheckBox(info);
  private int num;

  private MainPanel() {
    super(new BorderLayout());
    check.setOpaque(false);

    JDesktopPane desktop = new JDesktopPane();
    desktop.setDesktopManager(new DefaultDesktopManager() {
      @Override protected Rectangle getBoundsForIconOf(JInternalFrame f) {
        Rectangle r = super.getBoundsForIconOf(f);
        // TEST: r.width = 200;
        System.out.println(r.getSize());
        return r;
      }
    });

    JButton button = new JButton("add");
    button.addActionListener(e -> {
      JInternalFrame f = createFrame("#" + num, num * 10, num * 10);
      desktop.add(f);
      desktop.getDesktopManager().activateFrame(f);
      num++;
    });

    JMenuBar mb = new JMenuBar();
    mb.add(LookAndFeelUtil.createLookAndFeelMenu());
    mb.add(button);
    mb.add(Box.createHorizontalGlue());
    mb.add(check);

    addIconifiedFrame(desktop, createFrame("Frame", 30, 10));
    addIconifiedFrame(desktop, createFrame("Frame", 50, 30));
    add(desktop);
    add(mb, BorderLayout.NORTH);
    setPreferredSize(new Dimension(320, 240));
  }

  private JInternalFrame createFrame(String t, int x, int y) {
    JInternalFrame f = new JInternalFrame(t, true, true, true, true);
    f.setDesktopIcon(new JInternalFrame.JDesktopIcon(f) {
      @Override public Dimension getPreferredSize() {
        if (!check.isSelected()) {
          return super.getPreferredSize();
        }
        // Java 9 error: package com.sun.java.swing.plaf.motif is not visible
        // if (getUI() instanceof MotifDesktopIconUI) {
        if (getUI().getClass().getName().contains("MotifDesktopIconUI")) {
          return new Dimension(64, 64 + 32);
        } else {
          return ICON_SIZE;
        }
      }
    });
    f.setSize(200, 100);
    f.setLocation(x, y);
    f.setVisible(true);
    return f;
  }

  private static void addIconifiedFrame(JDesktopPane desktop, JInternalFrame f) {
    desktop.add(f);
    try {
      f.setIcon(true);
    } catch (PropertyVetoException ex) {
      throw new IllegalStateException(ex);
    }
  }

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

  private static void createAndShowGui() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      UIManager.put("DesktopIcon.width", ICON_SIZE.width);
      // TEST:
      // Font font = UIManager.getFont("InternalFrame.titleFont");
      // UIManager.put("InternalFrame.titleFont", font.deriveFont(30f));
    } 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);
  }
}

// @see https://java.net/projects/swingset3/sources/svn/content/trunk/SwingSet3/src/com/sun/swingset3/SwingSet3.java
final class LookAndFeelUtil {
  private static String lookAndFeel = UIManager.getLookAndFeel().getClass().getName();

  private LookAndFeelUtil() {
    /* Singleton */
  }

  public static JMenu createLookAndFeelMenu() {
    JMenu menu = new JMenu("LookAndFeel");
    ButtonGroup lafGroup = new ButtonGroup();
    for (UIManager.LookAndFeelInfo lafInfo: UIManager.getInstalledLookAndFeels()) {
      menu.add(createLookAndFeelItem(lafInfo.getName(), lafInfo.getClassName(), lafGroup));
    }
    return menu;
  }

  private static JMenuItem createLookAndFeelItem(String lafName, String lafClassName, ButtonGroup lafGroup) {
    JRadioButtonMenuItem lafItem = new JRadioButtonMenuItem(lafName, lafClassName.equals(lookAndFeel));
    lafItem.setActionCommand(lafClassName);
    lafItem.setHideActionText(true);
    lafItem.addActionListener(e -> {
      ButtonModel m = lafGroup.getSelection();
      try {
        setLookAndFeel(m.getActionCommand());
      } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
        ex.printStackTrace();
        Toolkit.getDefaultToolkit().beep();
      }
    });
    lafGroup.add(lafItem);
    return lafItem;
  }

  private static void setLookAndFeel(String lookAndFeel) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
    String oldLookAndFeel = LookAndFeelUtil.lookAndFeel;
    if (!oldLookAndFeel.equals(lookAndFeel)) {
      UIManager.setLookAndFeel(lookAndFeel);
      LookAndFeelUtil.lookAndFeel = lookAndFeel;
      updateLookAndFeel();
      // firePropertyChange("lookAndFeel", oldLookAndFeel, lookAndFeel);
    }
  }

  private static void updateLookAndFeel() {
    for (Window window: Window.getWindows()) {
      SwingUtilities.updateComponentTreeUI(window);
    }
  }
}

@@ -154,16 +154,66 @@
 eption;%0A
+import java.util.concurrent.atomic.AtomicInteger;%0A
 import j
@@ -502,27 +502,8 @@
 fo);
-%0A  private int num;
 %0A%0A  
@@ -921,24 +921,69 @@
  %7D%0A    %7D);%0A%0A
+    AtomicInteger idx = new AtomicInteger();%0A
     JButton 
@@ -1043,24 +1043,61 @@
 ener(e -%3E %7B%0A
+      int i = idx.getAndIncrement();%0A
       JInter
@@ -1131,26 +1131,20 @@
 %22 + 
-num, num
+i, i
  * 10, 
-num
+i
  * 1
@@ -1225,21 +1225,8 @@
 f);%0A
-      num++;%0A
     

use AtomicInteger instead of int for counters