KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > SearchTermIconManager


1 package net.suberic.pooka.gui;
2 import java.awt.Component JavaDoc;
3 import javax.swing.*;
4 import javax.mail.Message JavaDoc;
5 import javax.mail.search.SearchTerm JavaDoc;
6 import java.util.MissingResourceException JavaDoc;
7 import java.util.Vector JavaDoc;
8 import net.suberic.pooka.Pooka;
9 import net.suberic.pooka.SearchTermManager;
10
11 /**
12  * This defines a set of icons and values that can apply to a given Message.
13  *
14  * An example:
15  * FolderTable.Status.type=SearchTerm
16  * FolderTable.Status.size=15
17  * FolderTable.Status.value=Deleted:New:Answered
18  * FolderTable.Status.Deleted.searchTerm=Deleted
19  * FolderTable.Status.Deleted.icon=net/suberic/pooka/gui/images/SmallRedCircle.gif
20  * FolderTable.Status.New.searchTerm=Recent:and:Not:Seen
21  * FolderTable.Status.New.icon=net/suberic/pooka/gui/images/SmallGreenCircle.gif
22  * FolderTable.Status.Answered.searchTerm=Answered
23  * FolderTable.Status.Answered.icon=net/suberic/pooka/gui/images/SmallBlueCircle.gif
24  */

25 public class SearchTermIconManager {
26     SearchTerm JavaDoc[] terms;
27     Component JavaDoc[] icons;
28     protected Component JavaDoc blankImage = new JLabel();
29     
30     public SearchTermIconManager(String JavaDoc definitionProperty) {
31     SearchTermManager manager = Pooka.getSearchManager();
32     createTermsAndIcons(definitionProperty, manager);
33         ((JLabel)blankImage).setOpaque(true);
34     }
35
36   /**
37    * Populates the terms and icons arrays.
38    */

39   private void createTermsAndIcons(String JavaDoc property, SearchTermManager manager) {
40     // i'm lazy.
41
Vector JavaDoc iconVector = new Vector JavaDoc();
42     Vector JavaDoc termVector = new Vector JavaDoc();
43     
44     Vector JavaDoc items = Pooka.getResources().getPropertyAsVector(property + ".value", "");
45     for (int i = 0; i < items.size(); i++) {
46       String JavaDoc subProperty = property + "." + (String JavaDoc) items.elementAt(i);
47       Component JavaDoc currentIcon = loadImage(Pooka.getProperty(subProperty + ".icon", ""));
48       if (currentIcon != null) {
49     SearchTerm JavaDoc currentTerm = null;
50     try {
51       currentTerm = createSearchTerm(subProperty , manager);
52     } catch (java.text.ParseException JavaDoc pe) {
53       
54     }
55     if (currentTerm != null) {
56       iconVector.add(currentIcon);
57       termVector.add(currentTerm);
58     }
59       }
60     }
61     
62     terms = new SearchTerm JavaDoc[termVector.size()];
63     icons = new Component JavaDoc[iconVector.size()];
64     for (int i = 0; i < termVector.size() ; i++) {
65       terms[i] = (SearchTerm JavaDoc)termVector.elementAt(i);
66       icons[i] = (Component JavaDoc)iconVector.elementAt(i);
67     }
68   }
69   
70   /**
71    * This returns the icon for the given value.
72    */

73   public Component JavaDoc getIcon(int value) {
74     if (value < 0 || value >= icons.length || icons[value] == null) {
75       return blankImage;
76     } else
77       return icons[value];
78   }
79   
80     /**
81      * This calculates the int value for the given Message. It does this
82      * by running the SearchTerm on each message. The value of the first
83      * match is returned. If no matches are found, then the next value
84      * (number of SearchTerms + 1) is returned.
85      */

86     public int getValue(Message JavaDoc m) {
87     if (terms != null) {
88         for (int i = 0; i < terms.length; i++) {
89         if (terms[i] != null && terms[i].match(m))
90             return i;
91         }
92         return terms.length;
93     } else
94         return -1;
95     }
96     
97     /**
98      * Creates an appropriate SearchTerm from the given property.
99      */

100     public SearchTerm JavaDoc createSearchTerm(String JavaDoc propertyName, SearchTermManager manager) throws java.text.ParseException JavaDoc {
101     return manager.generateSearchTermFromProperty(propertyName);
102     }
103
104     /**
105      * This attempts to load an image from the given ImageFile.
106      */

107     public Component JavaDoc loadImage(String JavaDoc imageKey) {
108       Component JavaDoc returnValue = null;
109       ImageIcon icon = Pooka.getUIFactory().getIconManager().getIcon(imageKey);
110       if (icon != null) {
111     returnValue = new JLabel(icon);
112     ((JLabel)returnValue).setOpaque(true);
113     
114       } else {
115     returnValue = null;
116       }
117       
118       return returnValue;
119     }
120     
121 }
122
Popular Tags