package com.vci.client.common; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Insets; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JRootPane; import javax.swing.LookAndFeel; import javax.swing.UIManager; import com.vci.client.LogonApplication; import com.vci.client.logon.client.StatusBar; import com.vci.client.ui.swing.VCISwingUtil; import com.vci.client.ui.swing.VCITitleBar; import com.vci.client.ui.swing.components.VCIJButton; import com.vci.client.ui.swing.components.VCIJFrame; import com.vci.client.ui.swing.components.VCIJLabel; import com.vci.client.ui.swing.components.VCIJPanel; import com.vci.client.ui.swing.components.VCIJTextArea; public class PositionMessager { private int x = 0; private int y = 0; private int width = 320; private int height = 200; private int scrollPosition = 3; private int scrollLazy = 10; private int showTimes = 3000; private boolean pause = false; private boolean showing = false; private boolean closeing = false; private Frame frame = null; private String title = ""; private String content = ""; public PositionMessager(){ setScrollPosition(scrollPosition); setScrollLazy(scrollLazy); setShowTimes(showTimes); setPause(false); } public void show(String content){ show("信息提示", content); } public void show(final String title, final String content){ setTitle(title); setContent(content); new Thread(new Runnable() { @Override public void run() { showThreadRun(title, content); } }).start(); } private void showThreadRun(String title, String content){ frame = getShowFrame(title, content); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(LogonApplication.frame.getGraphicsConfiguration()); x = (int) (dim.getWidth() - width - 3); y = (int) (dim.getHeight() - screenInsets.bottom - 3); StatusBar.getInstance().setStatusMessage(content); frame.setSize(new Dimension(width, height)); frame.setVisible(true); show(frame); } private void show(Frame frame){ setShowing(true); btnClose.setEnabled(false); for (int i = 0; i <= height; ) { try { frame.setLocation(x, y - i); Thread.sleep(getScrollLazy()); if(isPause()) continue; i += getScrollPosition(); } catch (InterruptedException ex) { ex.printStackTrace(); } } btnClose.setEnabled(true); setShowing(false); try { Thread.sleep(getShowTimes()); } catch (InterruptedException e) { e.printStackTrace(); } close(frame); } private void close(){ new Thread(new Runnable() { @Override public void run() { close(frame); } }).start(); } private void close(Frame frame){ if(isShowing()) return; if(isCloseing()) return; x = frame.getX(); y = frame.getY(); setCloseing(true); for (int i = 0; i <= height; ) { try { frame.setLocation(x, y + i); Thread.sleep(getScrollLazy()); if(isPause()) continue; i += getScrollPosition(); } catch (InterruptedException ex) { ex.printStackTrace(); } } setCloseing(false); frame.dispose(); frame.setVisible(false); } private Frame getShowFrame(String title, String content){ VCIJFrame frame = new VCIJFrame(title); frame.setUndecorated(true); // 禁用默认的关闭处理程序 frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // 不显示最大、最小化按钮图标 frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); // 加入自定义的关闭处理程序 frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { close(); } }); // 加入鼠标事件,控制滚动是否是否需要暂停 frame.addMouseListener(getMouseAdapter()); frame.setResizable(false); frame.setAlwaysOnTop(true); frame.setLayout(new BorderLayout()); frame.add(getContentPanel(), BorderLayout.CENTER); return frame; } private VCIJPanel getContentPanel(){ VCIJPanel pal = new VCIJPanel(new BorderLayout()); pal.setBackground(Color.black); if(isNeedAddTitleBar()){ pal.add(getTopButtonPanel(), BorderLayout.NORTH); } pal.add(getCenterContentPanel(), BorderLayout.CENTER); pal.add(getSouthButtonPanel(), BorderLayout.SOUTH); pal.addMouseListener(getMouseAdapter()); return pal; } private boolean isNeedAddTitleBar(){ boolean res = true; LookAndFeel laf = UIManager.getLookAndFeel(); if(laf.getClass().getSimpleName().contains("PlasticLookAndFeel")){ res = false; } return res; } private VCIJPanel getTopButtonPanel(){ VCIJPanel pal = new VCIJPanel(new BorderLayout()); VCITitleBar tb = new VCITitleBar(getTitle(), "information.png"); tb.setPreferredSize(new Dimension(100, 25)); tb.addMouseListener(getMouseAdapter()); pal.addMouseListener(getMouseAdapter()); pal.add(tb, BorderLayout.NORTH); return pal; } private VCIJPanel getCenterContentPanel(){ VCIJPanel pal = new VCIJPanel(new BorderLayout(20, 0)); VCIJTextArea txt = new VCIJTextArea(getContent()); txt.setWrapStyleWord(true); txt.setLineWrap(true); txt.setEditable(false); txt.setBorder(BorderFactory.createEmptyBorder()); txt.addMouseListener(getMouseAdapter()); pal.addMouseListener(getMouseAdapter()); pal.add(new VCIJLabel(" "), BorderLayout.NORTH); pal.add(new VCIJLabel(""), BorderLayout.WEST); pal.add(txt, BorderLayout.CENTER); pal.add(new VCIJLabel(""), BorderLayout.EAST); pal.add(new VCIJLabel(""), BorderLayout.SOUTH); pal.setBorder(BorderFactory.createEmptyBorder()); return pal; } private VCIJPanel getSouthButtonPanel(){ VCIJPanel pal = new VCIJPanel(new FlowLayout(FlowLayout.CENTER)); pal.add(getCloseButton()); pal.addMouseListener(getMouseAdapter()); return pal; } private VCIJButton btnClose = VCISwingUtil.createVCIJButton("close", "关闭", "关闭", "close.png"); private VCIJButton getCloseButton(){ btnClose.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); btnClose.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { close(); } }); btnClose.addMouseListener(getMouseAdapter()); return btnClose; } private MouseAdapter getMouseAdapter(){ return new MouseAdapter() { public void mouseMoved(MouseEvent e) { setPauseAndCurosor(e, true); } public void mouseClicked(MouseEvent e) { setPauseAndCurosor(e, true); } public void mouseEntered(MouseEvent e) { setPauseAndCurosor(e, true); } public void mouseExited(MouseEvent e) { setPauseAndCurosor(e, false); } }; } private boolean mouseLoctionIsCloseButtonRegion(MouseEvent e){ boolean res = false; int mex = e.getX(); int mey = e.getY(); int imgRegWidth = 32; int imgRegHeight = 16; if(frame.getWidth() - imgRegWidth <= mex && mex <= frame.getWidth() && 0 <= mey && mey <= imgRegHeight){ res = true; } return res; } private void setPauseAndCurosor(MouseEvent e, boolean pause){ Object source = e.getSource(); if(mouseLoctionIsCloseButtonRegion(e)){ pause = true; } setPause(pause); boolean sourceIsComponent = Component.class.isAssignableFrom(source.getClass()); if(sourceIsComponent){ Component compt = (Component)source; if(pause){ compt.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } else { compt.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } } } public int getScrollPosition() { return scrollPosition; } public void setScrollPosition(int scrollPosition) { this.scrollPosition = scrollPosition; } public int getScrollLazy() { return scrollLazy; } public void setScrollLazy(int scrollLazy) { this.scrollLazy = scrollLazy; } public int getShowTimes() { return showTimes; } public void setShowTimes(int showTimes) { this.showTimes = showTimes; } public boolean isPause() { return pause; } public void setPause(boolean pause) { this.pause = pause; } public boolean isShowing() { return showing; } public void setShowing(boolean showing) { this.showing = showing; } public boolean isCloseing() { return closeing; } public void setCloseing(boolean closeing) { this.closeing = closeing; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }