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

📄 balloonwindow.java

📁 采用java SWT图形库的特效代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************
 * Copyright (c) 2004 Stefan Zeiger and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.novocode.com/legal/epl-v10.html
 * 
 * Contributors:
 *     Stefan Zeiger (szeiger@novocode.com) - initial API and implementation
 *******************************************************************************/

package com.novocode.naf.swt.custom;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Widget;


/**
 * A Shell wrapper which creates balloon popup windows.
 * 
 * <p>By default, a balloon window has no title bar or system controls.
 * The following styles are supported:</p>
 * 
 * <ul>
 *   <li>SWT.ON_TOP - Keep the window on top of other windows</li>
 *   <li>SWT.TOOL - Add a drop shadow to the window (on supported platforms)</li>
 *   <li>SWT.CLOSE - Show a "close" control on the title bar (implies SWT.TITLE)</li>
 *   <li>SWT.TITLE - Show a title bar</li>
 * </ul>
 * 
 * @author Stefan Zeiger (szeiger@novocode.com)
 * @since Jul 2, 2004
 * @version $Id: BalloonWindow.java,v 1.4 2005/06/25 19:01:18 szeiger Exp $
 */

public class BalloonWindow
{
  private final Shell shell;
  private final Composite contents;
  private Label titleLabel;
  private Canvas titleImageLabel;
  private final int style;
  private int preferredAnchor = SWT.BOTTOM | SWT.RIGHT;
  private boolean autoAnchor = true;
  private int locX = Integer.MIN_VALUE, locY = Integer.MIN_VALUE;
  private int marginLeft = 12, marginRight = 12, marginTop = 5, marginBottom = 10;
  private int titleSpacing = 3, titleWidgetSpacing = 8;
  private ToolBar systemControlsBar;
  private ArrayList selectionControls = new ArrayList();
  private boolean addedGlobalListener;
  private ArrayList selectionListeners = new ArrayList();


  public BalloonWindow(Shell parent, int style)
  {
    this(null, parent, style);
  }


  public BalloonWindow(Display display, int style)
  {
    this(display, null, style);
  }
  
  
  private BalloonWindow(Display display, Shell parent, final int style)
  {
    this.style = style;
    int shellStyle = style & (SWT.ON_TOP | SWT.TOOL);
    this.shell = (display != null)
      ? new Shell(display, SWT.NO_TRIM | shellStyle)
      : new Shell(parent, SWT.NO_TRIM | shellStyle);
    this.contents = new Composite(shell, SWT.NONE);
    
    final Color c = new Color(shell.getDisplay(), 255, 255, 225);
    shell.setBackground(c);
    shell.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
    contents.setBackground(shell.getBackground());
    contents.setForeground(shell.getForeground());

    selectionControls.add(shell);
    selectionControls.add(contents);

    final Listener globalListener = new Listener()
    {
      public void handleEvent(Event event)
      {
        Widget w = event.widget;
        for(int i=selectionControls.size()-1; i>= 0; i--)
        {
          if(selectionControls.get(i) == w)
          {
            if((style & SWT.CLOSE) != 0)
            {
              for(int j=selectionListeners.size()-1; j>= 0; j--)
                ((Listener)selectionListeners.get(j)).handleEvent(event);
            }
            else
            {
              shell.close();
            }
            event.doit = false;
          }
        }
      }
    };

    shell.addListener(SWT.Show, new Listener()
    {
      public void handleEvent(Event event)
      {
        if(!addedGlobalListener)
        {
          shell.getDisplay().addFilter(SWT.MouseDown, globalListener);
          addedGlobalListener = true;
        }
      }
    });

    shell.addListener(SWT.Hide, new Listener()
    {
      public void handleEvent(Event event)
      {
        if(addedGlobalListener)
        {
          shell.getDisplay().removeFilter(SWT.MouseDown, globalListener);
          addedGlobalListener = false;
        }
      }
    });

    shell.addListener(SWT.Dispose, new Listener()
    {
      public void handleEvent(Event event)
      {
        if(addedGlobalListener)
        {
          shell.getDisplay().removeFilter(SWT.MouseDown, globalListener);
          addedGlobalListener = false;
        }
        c.dispose();
      }
    });
  }


  /**
   * Adds a control to the list of controls which close the balloon window.
   * The background, title image and title text are included by default.
   */

  public void addSelectionControl(Control c)
  {
    selectionControls.add(c);
  }


  public void addListener(int type, Listener l)
  {
    if(type == SWT.Selection) selectionListeners.add(l);
  }


  /**
   * Set the location of the anchor. This must be one of the following values:
   * SWT.NONE, SWT.LEFT|SWT.TOP, SWT.RIGHT|SWT.TOP, SWT.LEFT|SWT.BOTTOM, SWT.RIGHT|SWT.BOTTOM
   */

  public void setAnchor(int anchor)
  {
    switch(anchor)
    {
      case SWT.NONE:
      case SWT.LEFT|SWT.TOP:
      case SWT.RIGHT|SWT.TOP:
      case SWT.LEFT|SWT.BOTTOM:
      case SWT.RIGHT|SWT.BOTTOM:
        break;
      default:
        throw new IllegalArgumentException("Illegal anchor value "+anchor);
    }
    this.preferredAnchor = anchor;
  }
  
  
  public void setAutoAnchor(boolean autoAnchor)
  {
    this.autoAnchor = autoAnchor;
  }


  public void setLocation(int x, int y)
  {
    this.locX = x;
    this.locY = y;
  }


  public void setLocation(Point p)
  {
    this.locX = p.x;
    this.locY = p.y;
  }


  public void setText(String title)
  {
    shell.setText(title);
  }


  public void setImage(Image image)
  {
    shell.setImage(image);
  }


  public void setMargins(int marginLeft, int marginRight, int marginTop, int marginBottom)
  {
    this.marginLeft = marginLeft;
    this.marginRight = marginRight;
    this.marginTop = marginTop;
    this.marginBottom = marginBottom;
  }


  public void setMargins(int marginX, int marginY)
  {
    setMargins(marginX, marginX, marginY, marginY);
  }


  public void setMargins(int margin)
  {
    setMargins(margin, margin, margin, margin);
  }


  public void setTitleSpacing(int titleSpacing)
  {
    this.titleSpacing = titleSpacing;
  }


  public void setTitleWidgetSpacing(int titleImageSpacing)
  {
    this.titleWidgetSpacing = titleImageSpacing;
  }


  public Shell getShell() { return shell; }
  
  
  public Composite getContents() { return contents; }


  public void prepareForOpen()
  {
    Point contentsSize = contents.getSize();
    Point titleSize = new Point(0, 0);

    boolean showTitle = ((style & (SWT.CLOSE | SWT.TITLE)) != 0);
    if(showTitle)
    {
      if(titleLabel == null)
      {
        titleLabel = new Label(shell, SWT.NONE);
        titleLabel.setBackground(shell.getBackground());
        titleLabel.setForeground(shell.getForeground());
        FontData[] fds = shell.getFont().getFontData();
        for(int i=0; i<fds.length; i++)
        {
          fds[i].setStyle(fds[i].getStyle() | SWT.BOLD);
        }
        final Font font = new Font(shell.getDisplay(), fds);
        titleLabel.addListener(SWT.Dispose, new Listener()
        {
          public void handleEvent(Event event)
          {
            font.dispose();
          }
        });
        titleLabel.setFont(font);
        selectionControls.add(titleLabel);

⌨️ 快捷键说明

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