KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > tagging > TaggingMenu


1 package org.columba.core.gui.tagging;
2
3 import java.awt.Color JavaDoc;
4 import java.awt.Graphics2D JavaDoc;
5 import java.awt.event.ActionEvent JavaDoc;
6 import java.awt.event.ActionListener JavaDoc;
7 import java.awt.image.BufferedImage JavaDoc;
8 import java.util.Iterator JavaDoc;
9
10 import javax.swing.Icon JavaDoc;
11 import javax.swing.ImageIcon JavaDoc;
12 import javax.swing.JCheckBoxMenuItem JavaDoc;
13 import javax.swing.JMenuItem JavaDoc;
14
15 import org.columba.api.gui.frame.IFrameMediator;
16 import org.columba.core.gui.menu.IMenu;
17 import org.columba.core.main.Bootstrap;
18 import org.columba.core.resourceloader.GlobalResourceLoader;
19 import org.columba.core.tagging.TagManager;
20 import org.columba.core.tagging.api.ITag;
21 import org.columba.core.tagging.api.ITagEvent;
22 import org.columba.core.tagging.api.ITagListener;
23
24 /**
25  * Abstract base class for a tagging menu.
26  * <p>
27  *
28  * @author fdietz
29  *
30  * @author frd
31  */

32 public abstract class TaggingMenu extends IMenu {
33
34     public TaggingMenu(IFrameMediator controller, String JavaDoc name, String JavaDoc id) {
35         super(controller, name, id);
36
37         createSubMenu();
38
39         // update menu if tags are changed
40
TagManager.getInstance().addTagListener(new MyTagListener());
41     }
42
43     protected void createSubMenu() {
44
45         if (!Bootstrap.ENABLE_TAGS)
46             return;
47
48         // TODO (@author hubms): implement custom menuitem renderer
49
JMenuItem JavaDoc item = new JMenuItem JavaDoc(GlobalResourceLoader.getString("org.columba.core.i18n.dialog",
50                 "tagging", "TaggingMenu.none"));
51         item.addActionListener(new ActionListener JavaDoc() {
52             public void actionPerformed(ActionEvent JavaDoc e) {
53                 // fire callback to remove all tags from selected items
54
removeAllTags();
55             }
56         });
57         add(item);
58         addSeparator();
59
60         // don't want to have two separators
61
boolean tags = false;
62
63         // add all existing tags to the menu
64
for (Iterator JavaDoc<ITag> iter = TagManager.getInstance().getAllTags(); iter
65                 .hasNext();) {
66             final ITag tag = iter.next();
67             final JCheckBoxMenuItem JavaDoc menuItem = new JCheckBoxMenuItem JavaDoc(tag
68                     .getName());
69
70             // mark tag, if the current selection is tagged with it
71
final boolean tagged = isTagged(tag);
72
73             if ( tag.getDescription() != null)
74                 menuItem.setToolTipText(tag.getDescription());
75             
76             if ( tag.getColor() != null )
77                 menuItem.setIcon(createIcon(tag.getColor()));
78             
79             menuItem.setSelected(tagged);
80
81             menuItem.addActionListener(new ActionListener JavaDoc() {
82                 public void actionPerformed(ActionEvent JavaDoc e) {
83                     if (menuItem.isSelected())
84                         assignTag(tag.getId());
85                     else
86                         removeTag(tag.getId());
87                 }
88             });
89             add(menuItem);
90
91             tags = true;
92         }
93
94         if (tags)
95             addSeparator();
96
97         JMenuItem JavaDoc editTagItem = new JMenuItem JavaDoc(GlobalResourceLoader.getString(
98                 "org.columba.core.i18n.dialog", "tagging", "TaggingMenu.edit"));
99         editTagItem.addActionListener(new ActionListener JavaDoc() {
100             public void actionPerformed(ActionEvent JavaDoc e) {
101                 // not implemented yet
102
}
103         });
104         add(editTagItem);
105
106     }
107
108     // listener updates menu in case the tags where changed
109
class MyTagListener implements ITagListener {
110         MyTagListener() {
111         }
112
113         public void tagChanged(ITagEvent event) {
114             String JavaDoc tagId = event.getId();
115             updateMenu();
116         }
117
118         public void tagAdded(ITagEvent event) {
119             String JavaDoc tagId = event.getId();
120             updateMenu();
121         }
122
123         public void tagDeleted(ITagEvent event) {
124             String JavaDoc tagId = event.getId();
125             updateMenu();
126         }
127
128         // real stupid recreation of whole menu model
129
private void updateMenu() {
130             removeAll();
131             createSubMenu();
132         }
133     }
134     
135     private Icon JavaDoc createIcon(Color JavaDoc color) {
136         int width = 16;
137         int height = 16;
138         BufferedImage JavaDoc image = new BufferedImage JavaDoc(width, height,
139                 BufferedImage.TYPE_INT_ARGB);
140
141         Graphics2D JavaDoc graphics = (Graphics2D JavaDoc) image.getGraphics();
142         graphics.setColor(darker(color));
143         graphics.drawRect(1, 1, width - 3, height - 3);
144         graphics.setColor(color);
145         graphics.fillRect(2, 2, width - 4, height - 4);
146         graphics.dispose();
147
148         return new ImageIcon JavaDoc(image);
149     }
150
151     private final static double FACTOR = 0.90;
152
153     private Color JavaDoc darker(Color JavaDoc c) {
154         return new Color JavaDoc(Math.max((int) (c.getRed() * FACTOR), 0), Math.max(
155                 (int) (c.getGreen() * FACTOR), 0), Math.max(
156                 (int) (c.getBlue() * FACTOR), 0));
157     }
158     
159
160     /** **************** overwrite callback methods ************************ */
161
162     /**
163      * Check if the current selection has the specific tag assigned to it.
164      *
165      * @param tag
166      * @return
167      */

168     protected abstract boolean isTagged(ITag tag);
169
170     /**
171      * Method is called if tag should be added to selected elements
172      *
173      * @param tagId
174      */

175     protected abstract void assignTag(String JavaDoc tagId);
176
177     /**
178      * Method is called if tag should be removed from selected elements
179      *
180      * @param tagId
181      */

182     protected abstract void removeTag(String JavaDoc tagId);
183
184     /**
185      * Methid is called if all tags should be removed from the selected elements
186      *
187      */

188     protected abstract void removeAllTags();
189
190 }
191
Popular Tags