1 package freeguide.plugins.ui.vertical.simple.filter;
2 
3 import freeguide.common.lib.fgspecific.data.TVProgramme;
4 
5 import freeguide.plugins.ui.vertical.simple.VerticalViewer;
6 
7 import java.text.SimpleDateFormat;
8 
9 import java.util.Date;
10 
11 /**
12  * Shows only programs which start and end in a given timeframe
13  *
14  * @author Christian Weiske (cweiske at cweiske.de)
15  */
16 public class TimeFilter extends ProgrammeFilter
17 {
18     /**
19      * The time at which the program has to start (or later) -1 means
20      * that this field is not used for filtering. If it's not -1, the meaning
21      * is "HHmm" as integer (2-digit hour with 24 hour format)
22      */
23     protected int nStartTime = -1;
24 
25     /**
26      * The time at which the program may start at last -1 deactivates
27      * that field, normal format is "HHmm"
28      */
29     protected int nEndTime = -1;
30 
31     /** The date formatter */
32     protected SimpleDateFormat time = new SimpleDateFormat( "HHmm" );
33 
34     /**
35      * Decides if the given program shall be shown or gets filtered
36      * out.
37      *
38      * @param programme DOCUMENT ME!
39      *
40      * @return DOCUMENT_ME!
41      */
showProgramme( TVProgramme programme )42     public boolean showProgramme( TVProgramme programme )
43     {
44         //I think that here is too expensive, but tell me how to make it faster..
45         int nTime =
46             Integer.parseInt(
47                 time.format( new Date( programme.getStart(  ) ) ) );
48 
49         return ( ( this.nStartTime == -1 ) || ( nTime >= this.nStartTime ) )
50         && ( ( this.nEndTime == -1 ) || ( nTime <= this.nEndTime ) );
51     }
52 
53     //public boolean showProgramme(TVProgramme programme)
54     /**
55      * DOCUMENT_ME!
56      *
57      * @return DOCUMENT_ME!
58      */
getStartTime( )59     public int getStartTime(  )
60     {
61         return nStartTime;
62     }
63 
64     //public int getStartTime()
65     /**
66      * DOCUMENT_ME!
67      *
68      * @param nStartTime DOCUMENT_ME!
69      */
setStartTime( int nStartTime )70     public void setStartTime( int nStartTime )
71     {
72         setStartTime( nStartTime, true );
73     }
74 
75     //public void setStartTime(int nStartTime)
76     /**
77      * DOCUMENT_ME!
78      *
79      * @param nStartTime DOCUMENT_ME!
80      * @param bNotify DOCUMENT_ME!
81      */
setStartTime( int nStartTime, boolean bNotify )82     public void setStartTime( int nStartTime, boolean bNotify )
83     {
84         this.nStartTime = nStartTime;
85 
86         if( bNotify )
87         {
88             this.notifyFilterChange(  );
89         }
90     }
91 
92     //public void setStartTime(int nStartTime, boolean bNotify)
93     /**
94      * DOCUMENT_ME!
95      *
96      * @return DOCUMENT_ME!
97      */
getEndTime( )98     public int getEndTime(  )
99     {
100         return nEndTime;
101     }
102 
103     //public int getEndTime()
104     /**
105      * DOCUMENT_ME!
106      *
107      * @param nEndTime DOCUMENT_ME!
108      */
setEndTime( int nEndTime )109     public void setEndTime( int nEndTime )
110     {
111         setEndTime( nEndTime, true );
112     }
113 
114     //public void setEndTime(int nEndTime)
115     /**
116      * DOCUMENT_ME!
117      *
118      * @param nEndTime DOCUMENT_ME!
119      * @param bNotify DOCUMENT_ME!
120      */
setEndTime( int nEndTime, boolean bNotify )121     public void setEndTime( int nEndTime, boolean bNotify )
122     {
123         this.nEndTime = nEndTime;
124 
125         if( bNotify )
126         {
127             this.notifyFilterChange(  );
128         }
129     }
130 
131     //public void setEndTime(int nEndTime, boolean bNotify)
132     /**
133      * sets the title dependent on the start and ending time
134      */
setTitle( )135     protected void setTitle(  )
136     {
137         if( ( this.nStartTime == -1 ) && ( this.nEndTime == -1 ) )
138         {
139             this.setTitle(
140                 VerticalViewer.getInstance(  ).getLocalizer(  )
141                               .getString( "timefilter.anytime" ) );
142         }
143         else
144         {
145             StringBuffer strTitle = new StringBuffer( 20 );
146             strTitle.append(
147                 VerticalViewer.getInstance(  )
148                               .getLocalizedMessage( "timefilter.begins" ) )
149                     .append( " " );
150 
151             if( this.nStartTime != -1 )
152             {
153                 strTitle.append( getTimeFromInt( this.nStartTime ) );
154 
155                 if( this.nEndTime != -1 )
156                 {
157                     strTitle.append( " - " )
158                             .append( getTimeFromInt( this.nEndTime ) );
159                 }
160             }
161             else
162             {
163                 strTitle.append(
164                     VerticalViewer.getInstance(  )
165                                   .getLocalizedMessage( "timefilter.until" ) )
166                         .append( " " ).append(
167                     getTimeFromInt( this.nEndTime ) );
168             }
169 
170             this.setTitle( strTitle.toString(  ) );
171         }
172     }
173 
174     //protected void setTitle()
175     /**
176      * Make a time string "HH:mm" out of the TimeFilter time integer
177      *
178      * @param nTime DOCUMENT ME!
179      *
180      * @return DOCUMENT_ME!
181      */
getTimeFromInt( int nTime )182     public static String getTimeFromInt( int nTime )
183     {
184         StringBuffer strTime = new StringBuffer( 5 );
185         strTime.append( nTime );
186 
187         int length = strTime.length(  );
188 
189         for( int nA = 0; nA < ( 4 - length ); nA++ )
190         {
191             strTime.insert( 0, '0' );
192         }
193 
194         strTime.insert( 2, ':' );
195 
196         return strTime.toString(  );
197     }
198 
199     //public static String getTimeFromInt(int nTime)
200     /**
201      * Our own notification method because we have to set the title.
202      */
notifyFilterChange( )203     public void notifyFilterChange(  )
204     {
205         this.setTitle(  );
206         super.notifyFilterChange(  );
207     }
208 
209     //public void notifyFilterChange()
210     /**
211      * DOCUMENT_ME!
212      *
213      * @return DOCUMENT_ME!
214      */
exportSettings( )215     public String exportSettings(  )
216     {
217         return "" + ( ( this.nStartTime * 10000 ) + this.nEndTime );
218     }
219 
220     //public String exportSettings()
221     /**
222      * DOCUMENT_ME!
223      *
224      * @param strValue DOCUMENT_ME!
225      */
importSettings( String strValue )226     public void importSettings( String strValue )
227     {
228         int nValue = Integer.parseInt( strValue );
229         this.nEndTime = nValue % 10000;
230         this.nStartTime = ( nValue - this.nEndTime ) / 10000;
231 
232         this.notifyFilterChange(  );
233     }
234 
235     //public void importSettings(String strValue)
236     /**
237      * DOCUMENT_ME!
238      */
deactivate( )239     public void deactivate(  )
240     {
241         this.nStartTime = -1;
242         this.nEndTime = -1;
243         this.notifyFilterChange(  );
244     }
245 
246     //public void deactivate()
247     /**
248      * DOCUMENT_ME!
249      *
250      * @return DOCUMENT_ME!
251      */
isDeactivated( )252     public boolean isDeactivated(  )
253     {
254         return ( this.nStartTime == -1 ) && ( this.nEndTime == -1 );
255     }
256 
257     //public boolean isDeactivated()
258 }
259 //public class TimeFilter extends ProgrammeFilter
260