⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ngshell.java

📁 Novocode的 SWT 控件框架 丰富了MDI功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

        case CENTER_PARENT:
          if(parentShell == null) bounds = display.getBounds();
          else bounds = parentShell.getBounds();
          break;

        case CENTER_PARENT_CLIENT_AREA:
          if(parentShell == null) bounds = display.getClientArea();
          else
          {
            Rectangle ca = parentShell.getClientArea();
            Rectangle loc = parentShell.getBounds();
            Rectangle tr = parentShell.computeTrim(ca.x, ca.y, ca.width, ca.height);
            bounds = new Rectangle(loc.x - tr.x, loc.y - tr.y, ca.width, ca.height);
          }
          break;

        case CENTER_DISPLAY:
          bounds = display.getBounds();
          break;

        case CENTER_DISPLAY_CLIENT_AREA:
          bounds = display.getClientArea();
          break;
      }

      if(bounds != null)
      {
        Rectangle rect = shell.getBounds();
        int x = bounds.x + (bounds.width - rect.width) / 2;
        int y = bounds.y + (bounds.height - rect.height) / 2;
        shell.setLocation(x, y);
      }
    }

    if(windowStateModel != null)
    {
      /* [TODO] Pluralized -> Minimized and Minimized -> Pluralized do not trigger an event.
       * These transitions need to be recognized. If everything else fails, check the state when
       * the shell is being closed. */
      shell.addControlListener(new ControlAdapter()
      {
        public void controlMoved(ControlEvent e)
        {
          int state = windowStateModel.getState();
          if(shell.getMaximized()) state = IWindowStateModel.STATE_MAXIMIZED;
          else if(shell.getMinimized())
          {
            if(state == IWindowStateModel.STATE_MAXIMIZED)
              state = IWindowStateModel.STATE_MINIMIZED_AFTER_MAXIMIZED;
            else if(state == IWindowStateModel.STATE_PLURALIZED || state == IWindowStateModel.STATE_DEFAULTBOUNDS) // [TODO] support creating maximized shells
              state = IWindowStateModel.STATE_MINIMIZED_AFTER_PLURALIZED;
          }
          else
          {
            state = IWindowStateModel.STATE_PLURALIZED;
            windowStateModel.setPluralizedBounds(shell.getBounds());
          }
          windowStateModel.setState(state);
        }
  
        public void controlResized(ControlEvent e)
        {
          int state = windowStateModel.getState();
          if(shell.getMaximized()) state = IWindowStateModel.STATE_MAXIMIZED;
          else if(shell.getMinimized())
          {
            if(state == IWindowStateModel.STATE_MAXIMIZED)
              state = IWindowStateModel.STATE_MINIMIZED_AFTER_MAXIMIZED;
            else if(state == IWindowStateModel.STATE_PLURALIZED)
              state = IWindowStateModel.STATE_MINIMIZED_AFTER_PLURALIZED;
          }
          else
          {
            state = IWindowStateModel.STATE_PLURALIZED;
            windowStateModel.setPluralizedBounds(shell.getBounds());
          }
          windowStateModel.setState(state);
        }
      });
  
      final IChangeListener cl = new IChangeListener()
      {
        public void stateChanged(ChangeEvent e)
        {
          Rectangle rect = windowStateModel.getPluralizedBounds();
          int state = windowStateModel.getState();
          switch(state)
          {
            case IWindowStateModel.STATE_PLURALIZED:
              if(shell.getMaximized()) shell.setMaximized(false);
              if(shell.getMinimized()) shell.setMinimized(false);
              if(!shell.getBounds().equals(rect)) shell.setBounds(rect);
              break;
            case IWindowStateModel.STATE_MAXIMIZED:
              if(!shell.getMaximized()) shell.setMaximized(true);
              break;
            case IWindowStateModel.STATE_MINIMIZED_AFTER_MAXIMIZED:
            case IWindowStateModel.STATE_MINIMIZED_AFTER_PLURALIZED:
              if(!shell.getMinimized()) shell.setMinimized(true);
              break;
            case IWindowStateModel.STATE_DEFAULTBOUNDS:
              // do nothing
              break;
          }
        }
      };
      SWTUtil.registerModel(shell, windowStateModel, cl);
      
      if(windowStateModel.getState() == IWindowStateModel.STATE_DEFAULTBOUNDS)
      {
        windowStateModel.setPluralizedBounds(shell.getBounds()); // [TODO] support creating maximized shells
        if(shell.getMinimized()) windowStateModel.setState(IWindowStateModel.STATE_MINIMIZED_AFTER_PLURALIZED);
        else if(shell.getMaximized()) windowStateModel.setState(IWindowStateModel.STATE_MAXIMIZED);
        else windowStateModel.setState(IWindowStateModel.STATE_PLURALIZED);
      }
    }

    Map<Integer, PreparedActionListener> keyDownMap = null;
    Map<Integer, PreparedActionListener> mouseDownMap = null;
    Map<Integer, PreparedActionListener> mouseUpMap = null;
    for(String type : getModelTypes())
    {
      if(type.startsWith("KeyDown "))
      {
        String accel = type.substring(8);
        IActionListener actionListener = getModel(type, wi.models);
        if(actionListener != null)
        {
          int code = SWTUtil.decodeAccelerator(accel);
          if(code != 0)
          {
            ActionEvent ae = new ActionEvent(this, getModelBinding(type).getAttribute("command"), wi);
            if(keyDownMap == null) keyDownMap = new HashMap<Integer, PreparedActionListener>();
            keyDownMap.put(code, new PreparedActionListener(actionListener, ae));
            //System.out.println("Adding global KeyDown listener for "+accel+" (code="+code+")");
          }
        }
      }
      else if(type.startsWith("MouseDown "))
      {
        String button = type.substring(10);
        IActionListener actionListener = getModel(type, wi.models);
        if(actionListener != null)
        {
          int code = SWTUtil.decodeMouseButton(button);
          if(code != 0)
          {
            ActionEvent ae = new ActionEvent(this, getModelBinding(type).getAttribute("command"), wi);
            if(mouseDownMap == null) mouseDownMap = new HashMap<Integer, PreparedActionListener>();
            mouseDownMap.put(code, new PreparedActionListener(actionListener, ae));
            //System.out.println("Adding global MouseDown listener for "+button+" (code="+code+")");
          }
        }
      }
      else if(type.startsWith("MouseUp "))
      {
        String button = type.substring(8);
        IActionListener actionListener = getModel(type, wi.models);
        if(actionListener != null)
        {
          int code = SWTUtil.decodeMouseButton(button);
          if(code != 0)
          {
            ActionEvent ae = new ActionEvent(this, getModelBinding(type).getAttribute("command"), wi);
            if(mouseUpMap == null) mouseUpMap = new HashMap<Integer, PreparedActionListener>();
            mouseUpMap.put(code, new PreparedActionListener(actionListener, ae));
            //System.out.println("Adding global MouseUp listener for "+button+" (code="+code+")");
          }
        }
      }
    }
    if(keyDownMap != null)
    {
      final Map<Integer, PreparedActionListener> finalMap = keyDownMap;
      SWTUtil.registerShellEventFilter(shell, SWT.KeyDown, new Listener()
      {
        public void handleEvent(Event event)
        {
          int code = event.stateMask | (event.keyCode != 0 ? event.keyCode : event.character);
          //System.out.println(event);
          //System.out.println("KeyDown in shell '"+title+"': keyCode="+event.keyCode+", character="+(int)(event.character)+", code="+code+", stateMask="+event.stateMask);
          PreparedActionListener action = finalMap.get(code);
          if(action != null)
          {
            action.performPreparedAction();
            event.doit = false;
          }
        }
      });
    }
    if(mouseDownMap != null)
    {
      final Map<Integer, PreparedActionListener> finalMap = mouseDownMap;
      SWTUtil.registerShellEventFilter(shell, SWT.MouseDown, new Listener()
      {
        public void handleEvent(Event event)
        {
          //System.out.println("MouseDown in shell '"+title+"': button="+event.button+" event="+event);
          PreparedActionListener action = finalMap.get(event.button);
          if(action != null)
          {
            action.performPreparedAction();
            event.doit = false;
          }
        }
      });
    }
    if(mouseUpMap != null)
    {
      final Map<Integer, PreparedActionListener> finalMap = mouseUpMap;
      SWTUtil.registerShellEventFilter(shell, SWT.MouseUp, new Listener()
      {
        public void handleEvent(Event event)
        {
          //System.out.println("MouseUp in shell '"+title+"': button="+event.button+" event="+event);
          PreparedActionListener action = finalMap.get(event.button);
          if(action != null)
          {
            action.performPreparedAction();
            event.doit = false;
          }
        }
      });
    }

    final IObjectReadModel<String> titleModel = getModel("title", wi.models);
    if(titleModel != null)
    {
      ModelBinding titleModelBinding = getModelBinding("title");
      final String prefix = titleModelBinding.getAttribute("prefix");
      final String suffix = titleModelBinding.getAttribute("suffix");
      final IChangeListener cl = new IChangeListener()
      {
        public void stateChanged(ChangeEvent e)
        {
          String s = titleModel.getValue();
          if(s == null) s = "";
          if(prefix != null) s = prefix + s;
          if(suffix != null) s = s + suffix;
          if(s.equals(shell.getText())) return;
          shell.setText(s);
        }
      };
      SWTUtil.registerModel(shell, titleModel, cl);
      cl.stateChanged(null);
    }

    final IActionListener closeModel = getModel("close", wi.models);
    if(closeModel != null)
    {
      final ActionEvent ae = new ActionEvent(this, getModelBinding("close").getAttribute("command"), wi);
      shell.addShellListener(new ShellAdapter()
      {
        public void shellClosed(ShellEvent e)
        {
          closeModel.performAction(ae);
          e.doit = false;
        }
      });
    }
    
    return wi;
  }


  protected Object createDefaultModel(ModelBinding mb)
  {
    if("window".equals(mb.type)) return new DefaultWindowStateModel();
    else if(mb.type.startsWith("KeyDown ") || mb.type.startsWith("MouseDown ") || mb.type.startsWith("MouseUp ")) return new DefaultActionBroadcaster(mb.id);
    else return super.createDefaultModel(mb);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -