View Javadoc

1   package org.glassbox.graphview;
2   
3   import java.awt.Color;
4   import java.awt.Component;
5   import java.awt.Graphics;
6   import javax.swing.Icon;
7   import javax.swing.Action;
8   
9   import de.fzi.wim.guibase.graphview.graph.*;
10  import de.fzi.wim.guibase.graphview.view.*;
11  
12  /***
13     An abstract implementation of <code>NodeButtonSet.Button</code> which supplies a button with an icon.
14   */
15  public abstract class IconButton implements NodeButtonSet.Button {
16      private Icon _icon;
17      private Color _border;
18      private Color _border_activated;
19      private Color _border_highlighted;
20      private Color _bg;
21      private Color _bg_activated;
22      private Color _bg_highlighted;
23      private Action _action;
24  
25      /***
26         Creates an <code>IconButton</code> with black border and white background.
27       */
28      public IconButton(Action action, Icon icon) {
29  	this (action, icon, Color.black, Color.white);
30      }
31  
32      /***
33         Creates an <code>IconButton</code> with the specified border and background color.
34       */
35      public IconButton(Action action, Icon icon, Color border, Color bg) {
36  	this(action, icon, border, border.brighter(), border.brighter(), bg, bg, bg.darker());
37      }
38  
39      /***
40         Creates an <code>IconButton</code> with the specified border and background colors.
41       */
42      public IconButton(Action action, Icon icon, Color border, Color borderhighlighted, Color bg, Color bgactivated) {
43  	this(action, icon, border, borderhighlighted, borderhighlighted, bg, bg, bgactivated);
44      }
45  
46      /***
47         Creates an <code>IconButton</code> with the specified border and background colors.
48       */
49      public IconButton(Action action, 
50  		      Icon icon, 
51  		      Color border, 
52  		      Color borderhighlighted, 
53  		      Color borderactivated, 
54  		      Color bg, 
55  		      Color bghighlighted, 
56  		      Color bgactivated) {
57  	_action = action;
58  	_icon = icon;
59  	_border = border;
60  	_border_highlighted = borderhighlighted;
61  	_border_activated = borderactivated;
62  	_bg = bg;
63  	_bg_highlighted = bghighlighted;
64  	_bg_activated = bgactivated;
65      }
66  
67      public abstract boolean affectsNode(Node node);
68      
69      public abstract String getToolTipText(Node node);
70  
71      public Action getAction() {
72  	return _action;
73      }
74  
75      public int getIconWidth() {
76  	return _icon.getIconWidth();
77      }
78  
79      public int getIconHeight() {
80  	return _icon.getIconHeight();
81      }
82  
83      public void paintIcon(Component c, Graphics g, int x, int y) {
84  	g.setColor(_bg);
85  	paintBackground(g, x, y);
86  	g.setColor(_border);
87  	paintBorder(g, x, y);
88  	_icon.paintIcon(c, g, x, y);
89      }
90  
91      public void paintRolloverIcon(Component c, Graphics g, int x, int y) {
92  	g.setColor(_bg_highlighted);
93  	paintBackground(g, x, y);
94  	g.setColor(_border_highlighted);
95  	paintBorder(g, x, y);
96  	_icon.paintIcon(c, g, x, y);
97      }
98  
99      public void paintPressedIcon(Component c, Graphics g, int x, int y) {
100 	g.setColor(_bg_activated);
101 	paintBackground(g, x, y);
102 	g.setColor(_border_activated);
103 	paintBorder(g, x, y);
104 	_icon.paintIcon(c, g, x, y);
105     }
106 
107     protected void paintBorder(Graphics g, int x, int y) {
108 	g.drawRect(x, y, getIconWidth()-1, getIconHeight()-1);
109     }
110 
111     protected void paintBackground(Graphics g, int x, int y) {
112 	g.fillRect(x, y, getIconWidth(), getIconHeight());
113     }
114 }