1 package freeguide.plugins.ui.vertical.simple.filter.gui;
2 
3 import freeguide.plugins.ui.vertical.simple.VerticalViewer;
4 import freeguide.plugins.ui.vertical.simple.filter.ProgrammeFilter;
5 import freeguide.plugins.ui.vertical.simple.filter.TimeFilter;
6 import freeguide.plugins.ui.vertical.simple.filter.gui.helper.CompTitledPane;
7 import freeguide.plugins.ui.vertical.simple.filter.gui.helper.SettingDialog;
8 
9 import java.awt.*;
10 import java.awt.event.ActionEvent;
11 import java.awt.event.ActionListener;
12 import java.awt.event.AdjustmentEvent;
13 import java.awt.event.AdjustmentListener;
14 
15 import javax.swing.*;
16 
17 /**
18  * Dialog in which the user can configure a TimeFilter. The layout has
19  * been copied shamelessly from nextVEPG.
20  *
21  * @author Christian Weiske (cweiske at cweiske.de)
22  */
23 public class TimeDialog extends JDialog implements AdjustmentListener,
24     ActionListener, SettingDialog
25 {
26     /** The filter to modify */
27     TimeFilter timeFilter;
28 
29     /** GUI elements */
30     protected JLabel lblStartValue;
31     protected JLabel lblEndValue;
32     protected JScrollBar scrStart;
33     protected JScrollBar scrEnd;
34     protected JCheckBox chkUseStart;
35     protected JCheckBox chkUseEnd;
36     protected JButton btnOk;
37     protected JButton btnCancel;
38     protected boolean bClosedWithOk = false;
39 
40     /**
41      * Creates a new TimeDialog object.
42      *
43      * @throws HeadlessException DOCUMENT ME!
44      */
TimeDialog( )45     public TimeDialog(  ) throws HeadlessException
46     {
47         super(  );
48     }
49 
50     //public TimeDialog() throws HeadlessException
51     /**
52      * DOCUMENT_ME!
53      *
54      * @param filter DOCUMENT_ME!
55      */
setFilter( ProgrammeFilter filter )56     public void setFilter( ProgrammeFilter filter )
57     {
58         this.timeFilter = (TimeFilter)filter;
59     }
60 
61     //public void setFilter(ProgrammeFilter filter)
62     /**
63      * DOCUMENT_ME!
64      */
init( )65     public void init(  )
66     {
67         this.setModal( true );
68         this.buildLayout(  );
69         this.loadFilterSettings(  );
70     }
71 
72     //public void init()
73     /**
74      * Create the GUI elements
75      */
buildLayout( )76     protected void buildLayout(  )
77     {
78         this.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
79         this.getContentPane(  ).setLayout( new BorderLayout(  ) );
80         this.setTitle(
81             VerticalViewer.getInstance(  )
82                           .getLocalizedMessage( "timedialog.title" ) );
83         this.setSize( 400, 200 );
84 
85         /** Buttons */
86         JPanel pnlButtons = new JPanel( new BorderLayout(  ) );
87         this.btnOk = new JButton(
88                 VerticalViewer.getInstance(  )
89                               .getLocalizedMessage( "timedialog.ok" ) );
90         this.btnOk.setDefaultCapable( true );
91         this.btnOk.addActionListener( this );
92         this.btnCancel = new JButton(
93                 VerticalViewer.getInstance(  )
94                               .getLocalizedMessage( "timedialog.cancel" ) );
95         this.btnCancel.addActionListener( this );
96 
97         pnlButtons.add( btnCancel, BorderLayout.WEST );
98         pnlButtons.add( btnOk, BorderLayout.EAST );
99         pnlButtons.setBorder( BorderFactory.createEmptyBorder( 10, 1, 1, 1 ) );
100 
101         this.getContentPane(  ).add( pnlButtons, BorderLayout.SOUTH );
102 
103         /** Controls */
104         JPanel contentPane;
105 
106         this.chkUseStart = new JCheckBox(
107                 VerticalViewer.getInstance(  )
108                               .getLocalizedMessage( "timedialog.check.from" ) );
109 
110         CompTitledPane pnlFrom = new CompTitledPane( chkUseStart );
111         this.lblStartValue = new JLabel( "00:00" );
112         this.scrStart = new JScrollBar( JScrollBar.HORIZONTAL, 0, 0, 0, 287 );
113         this.scrStart.addAdjustmentListener( this );
114         this.scrStart.setBlockIncrement( 12 );
115         contentPane = pnlFrom.getContentPane(  );
116         contentPane.setLayout( new BorderLayout(  ) );
117         contentPane.add( this.lblStartValue, BorderLayout.WEST );
118         contentPane.add( this.scrStart, BorderLayout.CENTER );
119 
120         this.chkUseEnd = new JCheckBox(
121                 VerticalViewer.getInstance(  )
122                               .getLocalizedMessage( "timedialog.check.to" ) );
123 
124         CompTitledPane pnlTo = new CompTitledPane( this.chkUseEnd );
125         this.lblEndValue = new JLabel( "00:00" );
126         this.scrEnd = new JScrollBar( JScrollBar.HORIZONTAL, 0, 0, 0, 287 );
127         this.scrEnd.setBlockIncrement( 12 );
128         this.scrEnd.addAdjustmentListener( this );
129         contentPane = pnlTo.getContentPane(  );
130         contentPane.setLayout( new BorderLayout(  ) );
131         contentPane.add( this.lblEndValue, BorderLayout.WEST );
132         contentPane.add( this.scrEnd, BorderLayout.CENTER );
133 
134         Box pnlSettings = Box.createVerticalBox(  );
135         pnlSettings.add( pnlFrom );
136         pnlSettings.add( pnlTo );
137 
138         this.getContentPane(  ).add( pnlSettings, BorderLayout.CENTER );
139     }
140 
141     //protected void buildLayout()
142     /**
143      * Loads the settings that the filter already has into the GUI
144      */
loadFilterSettings( )145     protected void loadFilterSettings(  )
146     {
147         int nStart = this.timeFilter.getStartTime(  );
148 
149         if( nStart != -1 )
150         {
151             this.chkUseStart.setSelected( true );
152             this.scrStart.setValue( getScrollIntFromTimeInt( nStart ) );
153         }
154         else
155         {
156             this.chkUseStart.setSelected( false );
157         }
158 
159         int nEnd = this.timeFilter.getEndTime(  );
160 
161         if( nEnd != -1 )
162         {
163             this.chkUseEnd.setSelected( true );
164             this.scrEnd.setValue( getScrollIntFromTimeInt( nEnd ) );
165         }
166         else
167         {
168             this.chkUseEnd.setSelected( false );
169         }
170     }
171 
172     //protected void loadFilterSettings()
173     /**
174      * A scrollbar value has been changed
175      *
176      * @param adjustmentEvent DOCUMENT ME!
177      */
adjustmentValueChanged( AdjustmentEvent adjustmentEvent )178     public void adjustmentValueChanged( AdjustmentEvent adjustmentEvent )
179     {
180         if( adjustmentEvent.getSource(  ) == this.scrStart )
181         {
182             int nStart = this.scrStart.getValue(  );
183             this.lblStartValue.setText(
184                 TimeFilter.getTimeFromInt( getTimeIntFromScrollInt( nStart ) ) );
185             this.chkUseStart.setSelected( true );
186 
187             if(
188                 this.chkUseEnd.isSelected(  )
189                     && ( nStart > this.scrEnd.getValue(  ) ) )
190             {
191                 this.scrEnd.setValue( nStart );
192             }
193         }
194         else
195         {
196             int nEnd = this.scrEnd.getValue(  );
197             this.lblEndValue.setText(
198                 TimeFilter.getTimeFromInt( getTimeIntFromScrollInt( nEnd ) ) );
199             this.chkUseEnd.setSelected( true );
200 
201             if(
202                 this.chkUseStart.isSelected(  )
203                     && ( nEnd < this.scrStart.getValue(  ) ) )
204             {
205                 this.scrStart.setValue( nEnd );
206             }
207         }
208     }
209 
210     //public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent)
211     /**
212      * Converts a scroll bar integer to a "time integer" Scrollbars
213      * use a step of 1 to increment 5 minutes, and this methods converts from
214      * the scrollbar value to a TimeFilter value of e.g. "2015" for "20:15"
215      *
216      * @param nScroll DOCUMENT ME!
217      *
218      * @return DOCUMENT_ME!
219      */
getTimeIntFromScrollInt( int nScroll )220     protected static int getTimeIntFromScrollInt( int nScroll )
221     {
222         int nMinutes = nScroll % 12;
223 
224         return ( ( nScroll - nMinutes ) / 12 * 100 ) + ( nMinutes * 5 );
225     }
226 
227     //protected static int getTimeIntFromScrollInt(int nScroll)
228     /**
229      * Reverse function to getTimeIntFromScrollInt(int nScroll)
230      *
231      * @param nTime DOCUMENT ME!
232      *
233      * @return DOCUMENT_ME!
234      */
getScrollIntFromTimeInt( int nTime )235     protected static int getScrollIntFromTimeInt( int nTime )
236     {
237         int nMinutes = nTime % 100;
238 
239         return ( ( nTime - nMinutes ) / 100 * 12 ) + ( nMinutes / 5 );
240     }
241 
242     //protected static int getScrollIntFromTimeInt(int nTime)
243     /**
244      * A button has been clicked
245      *
246      * @param actionEvent DOCUMENT ME!
247      */
actionPerformed( ActionEvent actionEvent )248     public void actionPerformed( ActionEvent actionEvent )
249     {
250         if( actionEvent.getSource(  ) == this.btnOk )
251         {
252             //OK - apply the settings to the filter
253             if( this.chkUseStart.isSelected(  ) )
254             {
255                 this.timeFilter.setStartTime(
256                     getTimeIntFromScrollInt( this.scrStart.getValue(  ) ) );
257             }
258             else
259             {
260                 this.timeFilter.setStartTime( -1, false );
261             }
262 
263             if( this.chkUseEnd.isSelected(  ) )
264             {
265                 this.timeFilter.setEndTime(
266                     getTimeIntFromScrollInt( this.scrEnd.getValue(  ) ) );
267             }
268             else
269             {
270                 this.timeFilter.setEndTime( -1, false );
271             }
272 
273             this.timeFilter.notifyFilterChange(  );
274             this.bClosedWithOk = true;
275             this.dispose(  );
276         }
277         else
278         {
279             //Should be Cancel button
280             this.dispose(  );
281         }
282     }
283 
284     //public void actionPerformed(ActionEvent actionEvent)
285     /**
286      * DOCUMENT_ME!
287      *
288      * @return DOCUMENT_ME!
289      */
isClosedWithOk( )290     public boolean isClosedWithOk(  )
291     {
292         return bClosedWithOk;
293     }
294 
295     //public boolean isClosedWithOk()
296     /**
297      * DOCUMENT_ME!
298      *
299      * @param show DOCUMENT ME!
300      */
setVisible( boolean show )301     public void setVisible( boolean show )
302     {
303         if( show )
304         {
305             //reset that
306             this.bClosedWithOk = false;
307         }
308 
309         super.setVisible( show );
310     }
311 
312     /**
313      * To test the dialog in development
314      *
315      * @param args DOCUMENT ME!
316      */
main( String[] args )317     public static void main( String[] args )
318     {
319         TimeFilter filter = new TimeFilter(  );
320         filter.setStartTime( 2015 );
321         filter.setEndTime( -1 );
322 
323         TimeDialog dialog = new TimeDialog(  );
324         dialog.setFilter( filter );
325         dialog.init(  );
326         dialog.setVisible( true );
327         System.exit( 0 );
328     }
329 
330     //public static void main(String[] args)
331 }
332 //public class TimeDialog extends JDialog
333