<commit_before>//
// WindowManager.java
//

/*
VisBio application for visualization of multidimensional
biological image data. Copyright (C) 2002-2005 <NAME>.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package loci.visbio;

import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import loci.visbio.state.*;
import loci.visbio.util.*;
import org.w3c.dom.Element;

/**
 * WindowManager is the manager encapsulating VisBio's window logic,
 * including docking, resizing, minimization and cursor control.
 */
public class WindowManager extends LogicManager implements WindowListener {

  // -- Constants --

  /** String for window docking option. */
  public static final String DOCKING = "Window docking (buggy)";

  /** String for global minimization option. */
  public static final String HIDE_ALL =
    "Hide all windows when main window is minimized";


  // -- Fields --

  /** Table for keeping track of registered windows. */
  protected Hashtable windows = new Hashtable();

  /** List of windows that were visible before VisBio was minimized. */
  protected Vector visible = new Vector();

  /** Number of queued wait cursors. */
  protected int waiting = 0;

  /** Object enabling docking between windows. */
  protected Docker docker;

  /** Whether window docking features are enabled. */
  protected boolean docking = false;

  /** Whether minimizing main VisBio window hides all other windows. */
  protected boolean hideAll = false;

  /** Whether to distribute the menu bar across all registered frames. */
  protected boolean distributed;


  // -- Fields - initial state --

  /** Table of window states read during state restore. */
  protected Hashtable windowStates = new Hashtable();


  // -- Constructor --

  /** Constructs a window manager. */
  public WindowManager(VisBioFrame bio) { super(bio); }


  // -- WindowManager API methods --

  /** Registers a window with the window manager. */
  public void addWindow(Window w) { addWindow(w, true); }

  /**
   * Registers a window with the window manager. The pack flag indicates that
   * the window should be packed prior to being shown for the first time.
   */
  public void addWindow(Window w, boolean pack) {
    if (w instanceof Frame) ((Frame) w).setIconImage(bio.getIcon());
    WindowInfo winfo = new WindowInfo(w, pack);
    String wname = SwingUtil.getWindowTitle(w);
    WindowState ws = (WindowState) windowStates.get(wname);
    if (ws != null) {
      winfo.setState(ws);
      windowStates.remove(wname);
    }
    windows.put(w, winfo);
    if (distributed && w instanceof JFrame) {
      JFrame f = (JFrame) w;
      if (f.getJMenuBar() == null) {
        f.setJMenuBar(SwingUtil.cloneMenuBar(bio.getJMenuBar()));
      }
    }
    docker.addWindow(w);
  }

  /** Removes the window from the window manager and disposes of it. */
  public void disposeWindow(Window w) {
    w.setVisible(false);
    String wname = SwingUtil.getWindowTitle(w);
    windowStates.remove(wname);
    windows.remove(w);
    docker.removeWindow(w);
    w.dispose();
  }

  /** Toggles the cursor between hourglass and normal pointer mode. */
  public void setWaitCursor(boolean wait) {
    boolean doCursor = false;
    if (wait) {
      // set wait cursor
      if (waiting == 0) doCursor = true;
      waiting++;
    }
    else {
      waiting--;
      // set normal cursor<commit_msg>Fix potential NullPointerException.<commit_after>//
// WindowManager.java
//

/*
VisBio application for visualization of multidimensional
biological image data. Copyright (C) 2002-2005 <NAME>.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package loci.visbio;

import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import loci.visbio.state.*;
import loci.visbio.util.*;
import org.w3c.dom.Element;

/**
 * WindowManager is the manager encapsulating VisBio's window logic,
 * including docking, resizing, minimization and cursor control.
 */
public class WindowManager extends LogicManager implements WindowListener {

  // -- Constants --

  /** String for window docking option. */
  public static final String DOCKING = "Window docking (buggy)";

  /** String for global minimization option. */
  public static final String HIDE_ALL =
    "Hide all windows when main window is minimized";


  // -- Fields --

  /** Table for keeping track of registered windows. */
  protected Hashtable windows = new Hashtable();

  /** List of windows that were visible before VisBio was minimized. */
  protected Vector visible = new Vector();

  /** Number of queued wait cursors. */
  protected int waiting = 0;

  /** Object enabling docking between windows. */
  protected Docker docker;

  /** Whether window docking features are enabled. */
  protected boolean docking = false;

  /** Whether minimizing main VisBio window hides all other windows. */
  protected boolean hideAll = false;

  /** Whether to distribute the menu bar across all registered frames. */
  protected boolean distributed;


  // -- Fields - initial state --

  /** Table of window states read during state restore. */
  protected Hashtable windowStates = new Hashtable();


  // -- Constructor --

  /** Constructs a window manager. */
  public WindowManager(VisBioFrame bio) { super(bio); }


  // -- WindowManager API methods --

  /** Registers a window with the window manager. */
  public void addWindow(Window w) { addWindow(w, true); }

  /**
   * Registers a window with the window manager. The pack flag indicates that
   * the window should be packed prior to being shown for the first time.
   */
  public void addWindow(Window w, boolean pack) {
    if (w instanceof Frame) ((Frame) w).setIconImage(bio.getIcon());
    WindowInfo winfo = new WindowInfo(w, pack);
    String wname = SwingUtil.getWindowTitle(w);
    WindowState ws = (WindowState) windowStates.get(wname);
    if (ws != null) {
      winfo.setState(ws);
      windowStates.remove(wname);
    }
    windows.put(w, winfo);
    if (distributed && w instanceof JFrame) {
      JFrame f = (JFrame) w;
      if (f.getJMenuBar() == null) {
        f.setJMenuBar(SwingUtil.cloneMenuBar(bio.getJMenuBar()));
      }
    }
    docker.addWindow(w);
  }

  /** Removes the window from the window manager and disposes of it. */
  public void disposeWindow(Window w) {
    if (w == null) return;
    w.setVisible(false);
    String wname = SwingUtil.getWindowTitle(w);
    windowStates.remove(wname);
    windows.remove(w);
    docker.removeWindow(w);
    w.dispose();
  }

  /** Toggles the cursor between hourglass and normal pointer mode. */
  public void setWaitCursor(boolean wait) {
    boolean doCursor = false;
    if (wait) {
      // set wait cursor
      if (waiting == 0) doCursor = true;
      waiting++;
    }
    else {
      waiting--;
      // set normal cursor
{'fraction_non_alphanumeric': 0.0981980892490023, 'fraction_numerical': 0.006288547587374531, 'mean_word_length': 3.2694889003613836, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4601413', 'n_tokens_mistral': 2373, 'n_tokens_neox': 2214, 'n_words': 903}