<commit_before>package org.minimalj.frontend.impl.swing;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;
import javax.swing.UIManager;

import org.minimalj.application.DevMode;
import org.minimalj.frontend.Frontend;
import org.minimalj.security.LoginAction;
import org.minimalj.security.LoginAction.LoginListener;
import org.minimalj.security.Subject;
import org.minimalj.util.resources.Resources;

/**
 * Manages:
 * <OL>
 * <LI>The open and closing of Frames
 * <LI>Can refresh all open Frames (reason for being refreshable)
 * </OL>
 * 
 * @author bruno
 * 
 */
public class FrameManager {
	private static Logger logger = Logger.getLogger(FrameManager.class.getName());
	private static FrameManager instance = new FrameManager();
	
	private List<SwingFrame> navigationFrames = new ArrayList<SwingFrame>();
	
	private FrameManager() {
	}
	
	//
	
	/**
	 * Sets the Look and Feel to the system default and sets the
	 * property "apple.laf.useScreenMenuBar" to true (mac specific)
	 */
	public static void setSystemLookAndFeel() {
		try {
			System.setProperty("apple.laf.useScreenMenuBar", "true");
			System.setProperty("com.apple.mrj.application.apple.menu.about.name", Resources.getString(Resources.APPLICATION_TITLE));
			
			String name = UIManager.getSystemLookAndFeelClassName();
			UIManager.setLookAndFeel(name);
		} catch (Exception e) {
			String s = "Couldn't set system LookandFeel";
			logger.log(Level.WARNING, s, e);
		}
	}
	
	public static FrameManager getInstance() {
		return instance;
	}

	public void openNavigationFrame(Subject subject) {
		final SwingFrame frame = new SwingFrame(subject);
		frame.setVisible(true);
		navigationFrames.add(frame);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) {
				navigationFrames.remove(frame);
			}
		});
		if (subject == null && Frontend.getInstance().loginAtStart()) {
			SwingFrame.activeFrameOverride = frame;
			LoginListener listener = new LoginListener() {
				@Override
				public void loginSucceded(Subject subject) {
					frame.setSubject(subject);
				}
				
				@Override
				public void loginCancelled() {
					frame.closeWindow();
				}
			};
			new LoginAction(listener).action();
			SwingFrame.activeFrameOverride = null;
		}
	}
	
	public List<SwingFrame> getNavigationFrames() {
		return navigationFrames;
	}
	
	// closing
	
	/**
	 * Asks the use about closing and then tries to close the Window
	 * 
	 * @param frameView NavigationFrameView to close
	 */
	public void closeWindowPerformed(SwingFrame frameView) {
		if (!navigationFrames.contains(frameView)) return;<commit_msg>FrameManager: listener to frame removed<commit_after>package org.minimalj.frontend.impl.swing;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;
import javax.swing.UIManager;

import org.minimalj.application.DevMode;
import org.minimalj.frontend.Frontend;
import org.minimalj.security.LoginAction;
import org.minimalj.security.LoginAction.LoginListener;
import org.minimalj.security.Subject;
import org.minimalj.util.resources.Resources;

/**
 * Manages:
 * <OL>
 * <LI>The open and closing of Frames
 * <LI>Can refresh all open Frames (reason for being refreshable)
 * </OL>
 * 
 * @author bruno
 * 
 */
public class FrameManager {
	private static Logger logger = Logger.getLogger(FrameManager.class.getName());
	private static FrameManager instance = new FrameManager();
	
	private List<SwingFrame> navigationFrames = new ArrayList<SwingFrame>();
	
	private FrameManager() {
	}
	
	//
	
	/**
	 * Sets the Look and Feel to the system default and sets the
	 * property "apple.laf.useScreenMenuBar" to true (mac specific)
	 */
	public static void setSystemLookAndFeel() {
		try {
			System.setProperty("apple.laf.useScreenMenuBar", "true");
			System.setProperty("com.apple.mrj.application.apple.menu.about.name", Resources.getString(Resources.APPLICATION_TITLE));
			
			String name = UIManager.getSystemLookAndFeelClassName();
			UIManager.setLookAndFeel(name);
		} catch (Exception e) {
			String s = "Couldn't set system LookandFeel";
			logger.log(Level.WARNING, s, e);
		}
	}
	
	public static FrameManager getInstance() {
		return instance;
	}

	public void openNavigationFrame(Subject subject) {
		final SwingFrame frame = new SwingFrame(subject);
		frame.setVisible(true);
		navigationFrames.add(frame);
		if (subject == null && Frontend.getInstance().loginAtStart()) {
			SwingFrame.activeFrameOverride = frame;
			LoginListener listener = new LoginListener() {
				@Override
				public void loginSucceded(Subject subject) {
					frame.setSubject(subject);
				}
				
				@Override
				public void loginCancelled() {
					frame.closeWindow();
					removeNavigationFrameView(frame);
				}
			};
			new LoginAction(listener).action();
			SwingFrame.activeFrameOverride = null;
		}
	}
	
	public List<SwingFrame> getNavigationFrames() {
		return navigationFrames;
	}
	
	// closing
	
	/**
	 * Asks the use about closing and then tries to close the Window
	 * 
	 * @param frameView NavigationFrameView to close
	 */
	public void closeWindowPerformed(SwingFrame frameView) {
		if (!navigationFrames.contains(frameView)) return;
{'fraction_non_alphanumeric': 0.1087920206871075, 'fraction_numerical': 0.0, 'mean_word_length': 4.340236686390533, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3834567', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1592, 'n_words': 380}