📄 gnometrayiconservice.java
字号:
* This is overridden to return false if the current Icon's Image is * not equal to the passed in Image <code>img</code>. * * @see java.awt.image.ImageObserver * @see java.awt.Component#imageUpdate(java.awt.Image, int, int, int, int, int) */ public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) { if (!isShowing() || !doesIconReferenceImage(icon, img)) { return false; } return super.imageUpdate(img, infoflags, x, y, w, h); } } class HWToolTip extends JWindow { Font font = new Font("Serif", 10, Font.PLAIN); JToolTip label; public HWToolTip(String caption, Window parent) { super(parent); setFocusableWindowState(false); setName("###overrideRedirect###"); label = new JToolTip(); label.setTipText(caption); getContentPane().add(label); } public void setCaption(String caption) { label.setTipText(caption); } public void show(int x, int y) { setLocation(x, y); Dimension d = label.getPreferredSize(); // pack does not seem to work ! setSize(d.width, d.height); setVisible(true); } } public void showBalloonMessage(String caption, String text, int type){ if( caption == null && text == null) throw new NullPointerException(); bmw.showBalloonMessage(caption, text, type); } class BalloonMessageWindow extends JWindow{ Dimension sd; Dimension pd; Dimension d; Point pp; Point p; boolean downToup = true; JLabel captionLabel = new JLabel(); JLabel textLabel = new JLabel(); private Thread showThread; private Thread hideThread; private int timeout = 10000; private int delay = 15; private int pixel = 2; private ActionListener hideAction; private javax.swing.Timer hideTimer; public BalloonMessageWindow(Window parent){ super(parent); JPanel outerPanel = new JPanel(); JPanel innerPanel = new JPanel(); this.getContentPane().add(outerPanel); outerPanel.setLayout(new BorderLayout()); outerPanel.add(innerPanel); outerPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, new Color(0x00CBDAF3), new Color(0x002F4A78))); innerPanel.setLayout(new BorderLayout()); innerPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, new Color(0x00CBDAF3), new Color(0x002F4A78))); captionLabel.setFont(new Font("SansSerif", Font.BOLD, 12)); textLabel.setFont(new Font("SansSerif", Font.PLAIN, 12)); innerPanel.setBackground(new Color(0x00FFFFE1)); innerPanel.setOpaque(true); textLabel.setBorder(BorderFactory.createEmptyBorder(0, 5, 3, 3)); ImageIcon x = new ImageIcon( GnomeTrayIconService.class.getResource("images/x.png")); ImageIcon xx = new ImageIcon( GnomeTrayIconService.class.getResource("images/xx.png")); ImageIcon xxx = new ImageIcon( GnomeTrayIconService.class.getResource("images/xxx.png")); JButton closeButton = new JButton(x); closeButton.setPreferredSize(new Dimension(x.getIconWidth(), x.getIconHeight())); closeButton.setRolloverEnabled(true); closeButton.setRolloverIcon(xx); closeButton.setPressedIcon(xxx); closeButton.setBorder(null); closeButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { BalloonMessageWindow.this.hideCurrentMessageWindowImmediately(); } }); closeButton.setToolTipText("Close"); JPanel closePanel = new JPanel(); closePanel.setOpaque(false); closePanel.setLayout(new BorderLayout()); closePanel.add(closeButton, BorderLayout.NORTH); JPanel topPanel = new JPanel(); topPanel.setLayout(new BorderLayout()); topPanel.setOpaque(false); topPanel.add(closePanel, BorderLayout.EAST); topPanel.add(captionLabel, BorderLayout.CENTER); topPanel.setBorder(BorderFactory.createEmptyBorder(3, 5, 0, 3)); innerPanel.add(topPanel, BorderLayout.NORTH); innerPanel.add(textLabel, BorderLayout.CENTER); outerPanel.addMouseListener(new MouseAdapter(){ public void mouseClicked(final MouseEvent e){ hideCurrentMessageWindowImmediately(); ListIterator li = balloonListeners.listIterator(0); while (li.hasNext()) { ActionListener al; al = (ActionListener) li.next(); al.actionPerformed(new ActionEvent(GnomeTrayIconService.this, ActionEvent.ACTION_PERFORMED, "PressAction", e.getWhen(), 0)); } } }); hideAction = new ActionListener(){ public void actionPerformed(ActionEvent e) { doHide(); } }; hideTimer = new javax.swing.Timer(timeout, hideAction); hideTimer.setRepeats(false); } private void hideCurrentMessageWindowImmediately(){ if(hideThread != null){ hideThread.interrupt(); // already in hiding. } else if(showThread != null){ hideTimer.stop(); showThread.interrupt(); this.setVisible(false); synchronized(GnomeTrayIconService.class){ GnomeTrayIconService.class.notify(); showThread = null; } } } public void showBalloonMessage(final String caption, final String text, final int type){ if(showThread != null){ Thread waitThread = new Thread(){ public void run(){ synchronized(GnomeTrayIconService.class){ try{ if(showThread != null) GnomeTrayIconService.class.wait(); }catch(InterruptedException ie){ ie.printStackTrace(); } } doShowMessageWindow(caption, text, type); } }; waitThread.start(); }else { this.doShowMessageWindow(caption, text, type); } } private void doShowMessageWindow(final String caption, final String text, final int type){ showThread = new Thread(){ public void run(){ doShow(caption, text, type); } }; showThread.start(); } private synchronized void doShow(String caption, String text, int type){ switch(type) { case 0: captionLabel.setIcon(new ImageIcon( GnomeTrayIconService.class.getResource("images/info.png"))); break; case 1: captionLabel.setIcon(new ImageIcon( GnomeTrayIconService.class.getResource("images/error.png"))); break; case 2: captionLabel.setIcon(new ImageIcon( GnomeTrayIconService.class.getResource("images/warning.png"))); break; default : captionLabel.setIcon(null); break; } captionLabel.setText(caption != null ? caption : ""); if(text != null){ text = "<html>"+text.replaceAll("\\n", "<br>")+"</html>"; textLabel.setText(text); }else{ textLabel.setText(""); } this.pack(); pp = frame.getLocation(); SwingUtilities.convertPointToScreen(pp, frame); p = new Point(); sd = Toolkit.getDefaultToolkit().getScreenSize(); pd = frame.getSize(); d = this.getSize(); // Adjust for the frame's size pp.x -= 2; pp.y -= 4; pd.width += 2; pd.height += 6; p.x = (pp.x + d.width) < sd.width ? pp.x : sd.width-d.width; downToup = (pp.y - d.height) > 0; p.y = (pp.y - d.height) > 0 ? pp.y - d.height : pp.y + pd.height; try{ if(downToup) this.setBounds(p.x, p.y+d.height, d.width, 1); else this.setBounds(p.x, p.y, d.width, 1); this.setVisible(true); for(int i=1; i < d.height; i+=pixel){ Thread.sleep(delay); if(downToup) this.setBounds(p.x, p.y+d.height-i, d.width, i); else this.setBounds(p.x, p.y, d.width, i); this.validate(); } if(p.y != this.getLocation().y || d.height != this.getSize().getHeight()){ this.setBounds(p.x, p.y, d.width, d.height); this.validate(); } hideTimer.start(); }catch(InterruptedException ie){} } private void doHide(){ hideThread = new Thread(){ public void run(){ p = getLocation(); d = getSize(); try{ for(int i=d.height; i>=1; i-=pixel){ Thread.sleep(delay); if(downToup){ setBounds(p.x, p.y+d.height-i, d.width, i); }else{ setSize(d.width, i); } validate(); } }catch(Exception e){} setVisible(false); synchronized(GnomeTrayIconService.class){ showThread = null; GnomeTrayIconService.class.notify(); } hideThread = null; } }; hideThread.start(); } } public void addBalloonActionListener(ActionListener al) { balloonListeners.add(al); } public void removeBalloonActionListener(ActionListener al) { balloonListeners.remove(al); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -