Exemplo n.º 1
0
  /**
   * Subclasses should call this after having constructed their GUI. Then this method will attach a
   * copy of the main menu from <code>root.menuFactory</code> and restore bounds from preferences.
   *
   * @param root application root
   * @see MenuFactory#gimmeSomethingReal( AppWindow )
   */
  public void init() {
    if (initialized) throw new IllegalStateException("Window was already initialized.");

    // System.out.println( "init " + getClass().getName() );

    if (borrowMenuBar) {
      borrowMenuBar(wh.getMenuBarBorrower());
      wh.addBorrowListener(this);
    } else if (ownMenuBar) {
      setJMenuBar(wh.getMenuBarRoot().createBar(this));
    }
    //		AbstractApplication.getApplication().addComponent( getClass().getName(), this );

    winListener =
        new AbstractWindow.Adapter() {
          public void windowOpened(AbstractWindow.Event e) {
            // System.err.println( "shown" );
            if (classPrefs != null) classPrefs.putBoolean(KEY_VISIBLE, true);
            if (!initialized)
              System.err.println("WARNING: window not initialized (" + e.getWindow() + ")");
          }

          //			public void windowClosing( WindowEvent e )
          //			{
          //				classPrefs.putBoolean( PrefsUtil.KEY_VISIBLE, false );
          //			}

          public void windowClosed(AbstractWindow.Event e) {
            // System.err.println( "hidden" );
            if (classPrefs != null) classPrefs.putBoolean(KEY_VISIBLE, false);
          }

          public void windowActivated(AbstractWindow.Event e) {
            try {
              active = true;
              if (wh.usesInternalFrames() && ownMenuBar) {
                wh.getMasterFrame().setJMenuBar(bar);
              } else if (borrowMenuBar && (barBorrower != null)) {
                barBorrower.setJMenuBar(null);
                if (jf != null) {
                  jf.setJMenuBar(bar);
                } else if (jif != null) {
                  wh.getMasterFrame().setJMenuBar(bar);
                } else {
                  throw new IllegalStateException();
                }
              }
              if (tempFloating) {
                if (jif == null) {
                  // System.out.println( "activ " + enc_getClass().getName() );
                  ////							wh.removeWindow( AbstractWindow.this, null );
                  tempFloatingTimer.restart();
                  //							GUIUtil.setAlwaysOnTop( getWindow(), true );
                  ////							floating = true;
                  ////							wh.addWindow( AbstractWindow.this, null );
                } else {
                  jif.setLayer(JLayeredPane.MODAL_LAYER);
                }
                //					} else if( wh.usesFloating() ) {
                //						// tricky...
                //						// we need to do this because if the opposite's
                //						// window is tempFloating, it will reset
                //						// alwaysOnTop to false too late for the OS,
                //						// so this one is not jumping to the front
                //						// automatically upon activation...
                //						toFront();
              }
            }
            // seems to be a bug ... !
            catch (NullPointerException e1) {
              e1.printStackTrace();
            }
          }

          public void windowDeactivated(AbstractWindow.Event e) {
            // System.out.println( "deac2 " + enc_getClass().getName() );
            try {
              active = false;
              if (wh.usesInternalFrames() && ownMenuBar) {
                if (wh.getMasterFrame().getJMenuBar() == bar) wh.getMasterFrame().setJMenuBar(null);
              } else if (borrowMenuBar && (barBorrower != null)) {
                if (jf != null) {
                  jf.setJMenuBar(null);
                }
                barBorrower.setJMenuBar(bar);
              }
              if (tempFloating) {
                if (jif == null) {
                  // System.out.println( "deact " + enc_getClass().getName() );
                  //							wh.removeWindow( AbstractWindow.this, null );
                  GUIUtil.setAlwaysOnTop(getWindow(), false);
                  tempFloatingTimer.stop();
                  //							floating = false;
                  //							wh.addWindow( AbstractWindow.this, null );

                  // find the new active window (is valid only after
                  // the next event cycle) and re-put it in the front\
                  // coz setAlwaysOnTop came "too late"
                  EventQueue.invokeLater(
                      new Runnable() {
                        public void run() {
                          final AbstractWindow fw =
                              FloatingPaletteHandler.getInstance().getFocussedWindow();
                          if (fw != null) fw.toFront();
                        }
                      });
                } else {
                  jif.setLayer(JLayeredPane.DEFAULT_LAYER);
                }
              }
            }
            // seems to be a bug ... !
            catch (NullPointerException e1) {
              e1.printStackTrace();
            }
          }
        };
    addListener(winListener);

    if (autoUpdatePrefs()) {
      getClassPrefs(); // this creates the prefs
      restoreFromPrefs();
      cmpListener =
          new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
              classPrefs.put(KEY_SIZE, dimensionToString(e.getComponent().getSize()));
            }

            public void componentMoved(ComponentEvent e) {
              classPrefs.put(KEY_LOCATION, pointToString(e.getComponent().getLocation()));
            }

            public void componentShown(ComponentEvent e) {
              classPrefs.putBoolean(KEY_VISIBLE, true);
            }

            public void componentHidden(ComponentEvent e) {
              classPrefs.putBoolean(KEY_VISIBLE, false);
              // System.err.println( "hidden" );
            }
          };
      c.addComponentListener(cmpListener);
    } else {
      if (alwaysPackSize()) {
        pack();
      }
    }

    wh.addWindow(this, null);
    initialized = true;
  }