View Javadoc

1   /*
2    * Created on 07.05.2004
3    *
4    * Adds attributes not included in standard VCal but required 
5    * for internal ICalendar management
6    */
7   package net.stff.ical.beans;
8   
9   import java.awt.Color;
10  import java.util.Collection;
11  import java.util.Date;
12  import java.util.Iterator;
13  import java.util.List;
14  import java.util.ResourceBundle;
15  import java.util.Vector;
16  
17  
18  /***
19   * @author buntekuh
20   *
21   */
22  public class ICal extends VCal{
23  
24      private Long id;
25  	private static String DEFAULTCOLOR= "777777"; 
26      private String name;
27      private String color;
28      private boolean shown;
29      private List categories;
30      private List Stati;
31      private String user;
32      
33       public ICal(){}
34      
35      public ICal(String name, String user){
36          super();
37          this.name= name;
38          this.user= user;
39          this.color= DEFAULTCOLOR;
40  	    
41          Vector v;
42  
43  	    ResourceBundle bundle= ResourceBundle.getBundle("resources.application");
44  		String[] values= bundle.getString("ical.ical.status.values").split(",");
45  		v= new Vector();
46  		for (int n=0;n<values.length;n++){
47  		    v.add(values[n]);
48  		}
49  		setStati(v);
50  		
51  		values= bundle.getString("ical.ical.categories.values").split(",");
52  		v= new Vector();
53  		for (int n=0;n<values.length;n++){
54  		    v.add(values[n]);
55  		}
56  		setCategories(v);		
57  
58      }
59  
60      /***
61  	 * @return Returns the id.
62  	 */
63  	public Long getId() {
64  		return id;
65  	}
66  	/***
67  	 * @param id The id to set.
68  	 */
69  	public void setId(Long id) {
70  		this.id = id;
71  	}
72  	   /***
73       * @return Returns the user.
74       */
75      public String getUser() {
76          return user;
77      }
78      /***
79       * @param user The user to set.
80       */
81      public void setUser(String user) {
82          this.user = user;
83      }
84      /***
85       * @return Returns the color.
86       */
87      public String getColor() {
88          return color;
89      }
90      /***
91       * @return Returns the color.
92       */
93      public Color getColorObj() {
94          return convert(color);
95      }
96      /***
97       * @param color The color to set.
98       */
99      public void setColor(String color) {
100         this.color = strip(color);
101     }
102     /***
103      * @param color The color to set.
104      */
105     public void setColor(Color c) {
106         this.color = convert(c);
107     }
108     /***
109      * @return Returns the name.
110      */
111     public String getName() {
112         return name;
113     }
114     /***
115      * @param name The name to set.
116      */
117     public void setName(String name) {
118         this.name = name;
119     }
120     
121 	/***
122 	 * @return Returns the categories.
123 	 */
124 	public List getCategories() {
125 		return categories;
126 	}
127 	/***
128 	 * @param categories The categories to set.
129 	 */
130 	public void setCategories(List categories) {
131 		this.categories = categories;
132 	}
133 	/***
134 	 * @return Returns the stati.
135 	 */
136 	public List getStati() {
137 		return Stati;
138 	}
139 	/***
140 	 * @param stati The stati to set.
141 	 */
142 	public void setStati(List stati) {
143 		Stati = stati;
144 	}
145 
146     public boolean equals(Object o){
147         try{
148         	ICal cal= (ICal)o;
149             if ((cal.getName().equals(name)) && (cal.getUser().equals(user))) return true;
150             return false;
151         }
152         catch(Exception e){
153             return false;
154         }
155     }
156     public int hashCode(){
157         String code= name+"stff"+user;
158         return code.hashCode();
159     }
160 
161     public static String strip(String s){
162         if (s.startsWith("#")){
163             try{
164                 return s.substring(1, s.length());
165             }
166             catch(Exception e){
167                 return null;
168             }
169         }
170         return s;
171     }
172     
173     public static Color convert(String s){
174         
175         s= strip(s);
176         
177         if (s.length() != 6) return null;
178         
179         try{
180             int r, g, b;
181             r= Integer.parseInt(s.substring(0, 2), 16);
182             g= Integer.parseInt(s.substring(2, 4), 16);
183             b= Integer.parseInt(s.substring(4, 6), 16);
184             return new Color(r, g, b);
185                        
186         }
187         catch(Exception e){
188             return null;
189         }
190     }
191 
192     public static String convert(Color c){
193         StringBuffer buf= new StringBuffer();
194         buf.append(Integer.toHexString(c.getRed()));
195         buf.append(Integer.toHexString(c.getGreen()));
196         buf.append(Integer.toHexString(c.getBlue()));
197         return buf.toString();
198     }
199     /***
200      * @return Returns the shown.
201      */
202     public boolean isShown() {
203         return shown;
204     }
205     /***
206      * @param shown The shown to set.
207      */
208     public void setShown(boolean shown) {
209         this.shown = shown;
210     }
211     
212     public IEvent getEvent(String uid){
213         Iterator i= events.iterator();
214         while (i.hasNext()){
215             try{
216                 IEvent event= (IEvent)i.next();
217                 if (event.getUid().equals(uid)) return event;
218             }
219             catch(Exception e){
220                 continue;
221             }
222         }
223         return null;
224     }
225     
226     public Collection getEventsForPeriod(Date s, Date e, int which){
227         
228         Vector v= new Vector();
229         
230         Iterator i= events.iterator();
231         while (i.hasNext()){
232             IEvent event= (IEvent)i.next();
233             //System.out.println(event.getDtstart().toString()+":"+s.toString()+"_"+e.toString());
234             if (event.startsWithin(s, e, which)){
235                 v.add(event);
236             }
237         }
238         return v;
239         
240     }
241         
242 }