Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.timesupport / org.gvsig.timesupport.swing / org.gvsig.timesupport.swing.impl / src / main / java / org / gvsig / timesupport / swing / impl / panel / TimePanel.java @ 43610

History | View | Annotate | Download (17.4 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
 */
22

    
23
 /*
24
* AUTHORS (In addition to CIT):
25
* 2010 {Prodevelop}   {Task}
26
 */
27
package org.gvsig.timesupport.swing.impl.panel;
28

    
29
import java.awt.Component;
30
import java.util.List;
31

    
32
import javax.swing.BoxLayout;
33
import javax.swing.JPanel;
34
import org.apache.commons.collections4.CollectionUtils;
35

    
36
import org.gvsig.i18n.Messages;
37
import org.gvsig.timesupport.AbsoluteInstant;
38
import org.gvsig.timesupport.AbsoluteInterval;
39
import org.gvsig.timesupport.AbsoluteIntervalTypeNotRegisteredException;
40
import org.gvsig.timesupport.Instant;
41
import org.gvsig.timesupport.Interval;
42
import org.gvsig.timesupport.RelativeInstant;
43
import org.gvsig.timesupport.TimeSupportLocator;
44
import org.gvsig.timesupport.TimeSupportManager;
45
import org.gvsig.timesupport.swing.impl.components.AbsoluteTemporalComponent;
46
import org.gvsig.timesupport.swing.impl.components.ITemporalComponent;
47
import org.gvsig.timesupport.swing.impl.components.RelativeTemporalComponent;
48
import org.gvsig.timesupport.swing.impl.rdv.TimeAdjustmentListener;
49
import org.gvsig.timesupport.swing.impl.rdv.TimeEvent;
50
import org.gvsig.timesupport.swing.impl.rdv.TimeSlider;
51

    
52
/**
53
 * Window main panel (control Relative and Absolute time instances)
54
 *
55
 * @author <a href="mailto:Pablo.Viciano@uji.es">Pablo Viciano Negre</a>
56
 * @version $Id$
57
 *
58
 */
59
public class TimePanel extends JPanel implements TimeAdjustmentListener {
60

    
61
    private static final long serialVersionUID = -1711925095164453822L;
62

    
63
    /**
64
     * Time mode {Relative or absolute}
65
     */
66
    public static enum TIME_MODE {
67
        RELATIVE, ABSOLUTE
68
    };
69
    private TIME_MODE _mode;
70
    private TimeSlider slider = null;
71
    private ITemporalComponent jComponentStart = null;
72
    private ITemporalComponent jComponentEnd = null;
73
    private Instant startDate = null;
74
    private Instant endDate = null;
75
    private Instant startBackupForInterval = null;
76
    private Instant endBackupForInterval = null;
77
    private Interval interval = null;
78
    private boolean eventSynchronized = false;
79

    
80
    private List<Instant> instants = null;
81
    private int instantsPosition;
82
    private Instant actual = null;
83
    private Instant instAux = null;
84
    private boolean updateSlider = false;
85
    private static TimeSupportManager timeSupportManager = TimeSupportLocator
86
        .getManager();
87

    
88
    private TimeAdjustmentListener listener = null;
89

    
90
    public TimePanel() {
91
        super();
92
        _mode = TIME_MODE.RELATIVE;
93
    }
94

    
95
    /**
96
     * Intializes the panel
97
     *
98
     * @param start
99
     * @param end
100
     */
101
    public void initialize(Instant start, Instant end) {
102

    
103
        createComponents(start, end);
104
        startBackupForInterval = start;
105
        endBackupForInterval = end;
106
    }
107

    
108
    /**
109
     * Set instants to all controls
110
     *
111
     * @param start
112
     * @param end
113
     */
114
    public void setInstants(Instant start, Instant end) {
115
        startDate = start;
116
        endDate = end;
117

    
118
        getJComponentStart().setTimes(Messages.getText("lbl_begin_position"),
119
            startDate, startDate, endDate);
120

    
121
        getJComponentEnd().setTimes(Messages.getText("lbl_end_position"),
122
            endDate, startDate, endDate);
123

    
124
        getTimeSlider().setValues(startDate, endDate);
125
    }
126

    
127
    /**
128
     * Set the time Mode
129
     *
130
     * @param mode
131
     * @param start
132
     * @param end
133
     */
134
    public void setTimeMode(TIME_MODE mode, Instant start, Instant end) {
135
        _mode = mode;
136
        initialize(start, end);
137
    }
138

    
139
    /**
140
     * Get the current TIME_MODE
141
     *
142
     * @return
143
     */
144
    public TIME_MODE getTimeMode() {
145
        return _mode;
146
    }
147

    
148
    /**
149
     * Set the listener to capture all events {@link TimeAdjustmentListener}
150
     *
151
     * @param listener
152
     */
153
    public void setListener(TimeAdjustmentListener listener) {
154
        this.listener = listener;
155
    }
156

    
157
    /**
158
     * Get the listener {@link TimeAdjustmentListener}
159
     *
160
     * @return
161
     */
162
    public TimeAdjustmentListener getListener() {
163
        return this.listener;
164
    }
165

    
166
    /**
167
     * Set if works with instants (true) or interval (false)
168
     *
169
     * @param changeable
170
     */
171
    public void setValueChangeableSlider(boolean changeable) {
172
        if( changeable == true ) { //Instant
173
            if( interval != null ) {
174
                startBackupForInterval = startDate;
175
                startBackupForInterval = endDate;
176
                setInstants(interval.getStart(), interval.getEnd());
177
            }
178
        } else { //Interval
179
            if( interval != null ) {
180
                setInstants(startBackupForInterval, endBackupForInterval);
181
            }
182
        }
183
        getTimeSlider().setValueChangeable(changeable);
184
    }
185

    
186
    /**
187
     * Set the valid instants
188
     *
189
     * @param instants
190
     */
191
    public void setInstants(List<Instant> instants) {
192
        this.instants = instants;
193
        instantsPosition = 0;
194
        actual = null;
195
    }
196

    
197
    public void timeChanged(TimeEvent event) {
198
        // TODO Auto-generated method stub
199
        if( !isUpdating() ) {
200
            setUpdating(true);
201
            if( event.getSource() instanceof TimeSlider ) {
202
                if( getTimeSlider().getValueChangeable() ) {
203
                    if( CollectionUtils.isNotEmpty(this.instants) && !updateSlider ) {
204
                        updateSlider = true;
205
                        if( !getTimeSlider().isValueAdjusting() ) {
206
                            getTimeSlider().setCurrentInstant(getApproximateInstant(getTimeSlider().getCurrentInstant()));
207
                            getJComponentStart().setCurrentInstant(getTimeSlider().getCurrentInstant());
208
                        }
209
                        updateSlider = false;
210
                    } else {
211
                        getJComponentStart().setCurrentInstant(getTimeSlider().getCurrentInstant());
212
                    }
213

    
214
                }
215

    
216
            } else if( event.getSource() instanceof ITemporalComponent ) {
217

    
218
                ITemporalComponent calendar = (ITemporalComponent) event.getSource();
219
                if( calendar == getJComponentStart() ) {
220
                    if( !getTimeSlider().getValueChangeable() ) {
221

    
222
                        try {// this try is for the daylight saving (it creates
223
                            // exceptions)
224
                            if( !updateSlider ) {
225
                                getTimeSlider().setStartInstant((Instant) getJComponentStart().getTime());
226
                            }
227
                        } catch (IllegalArgumentException e) {
228
                            e.printStackTrace();
229
                        }
230
                    }
231
                } else {
232
                    if( !getTimeSlider().getValueChangeable() ) {
233

    
234
                        try {// this try is for the daylight saving (it creates
235
                            // exceptions)
236
                            if( !updateSlider ) {
237
                                getTimeSlider().setEndInstant((Instant) getJComponentEnd().getTime());
238
                            }
239
                        } catch (IllegalArgumentException e) {
240
                            e.printStackTrace();
241
                        }
242
                    }
243
                }
244
                updateCalendars();
245

    
246
            } else {
247
                try {// this try is for the daylight saving (it creates exceptions)
248
                    if( CollectionUtils.isEmpty(this.instants) ) {
249
                        getTimeSlider().setCurrentInstant((Instant) getJComponentStart().getTime());
250
                    }
251
                } catch (IllegalArgumentException e) {
252
                } finally {
253
                }
254
            }
255
            if( getTimeSlider().getValueChangeable() && listener != null ) {
256
                listener.timeChanged(new TimeEvent(this));
257
            }
258
            setUpdating(false);
259
        }
260
    }
261

    
262
    public void rangeChanged(TimeEvent event) {
263
        // TODO Auto-generated method stub
264
        if( event.getSource() instanceof TimeSlider ) {
265
            getJComponentStart().setCurrentInstant(getTimeSlider().getStartInstant());
266
            getJComponentEnd().setCurrentInstant(getTimeSlider().getEndInstant());
267
        }
268
        if( !getTimeSlider().getValueChangeable() && listener != null ) {
269
            listener.rangeChanged(new TimeEvent(this));
270
        }
271
    }
272

    
273
    /**
274
     * Update slider (intervals)
275
     */
276
    public void fireRangeInterval() {
277
        getTimeSlider().fireRangeChanged();
278
    }
279

    
280
    /**
281
     * Update slider (instants)
282
     */
283
    public void fireValueInstant() {
284
        getTimeSlider().fireValueChanged();
285
    }
286

    
287
    public void boundsChanged(TimeEvent event) {
288
        // TODO Auto-generated method stub
289

    
290
    }
291

    
292
    @Override
293
    public void setEnabled(boolean enabled) {
294
        super.setEnabled(enabled);
295
        getJComponentStart().setEnabled(enabled);
296
        getJComponentEnd().setEnabled(enabled);
297
        getTimeSlider().setEnabled(enabled);
298
    }
299

    
300
    public void resetValues() {
301
        getTimeSlider().resetAllDates();
302
        getJComponentStart().resetValues();
303
        getJComponentEnd().resetValues();
304
    }
305

    
306
    public void setEnabledEndCalendar(boolean enabled) {
307
        getJComponentEnd().setEnabled(enabled);
308
    }
309

    
310
    /**
311
     * Get the current interval
312
     *
313
     * @return
314
     */
315
    public Interval getInterval() {
316

    
317
        if( datesCorrect() ) {
318
            if( _mode == TIME_MODE.RELATIVE ) {
319
                return timeSupportManager.createRelativeInterval(
320
                    (RelativeInstant) getJComponentStart().getTime(), (RelativeInstant) getJComponentEnd()
321
                    .getTime());
322
            } else {
323
                try {
324
                    AbsoluteInstant start = (AbsoluteInstant) getJComponentStart().getTime();
325
                    AbsoluteInstant end = (AbsoluteInstant) getJComponentEnd().getTime();
326
                    return createAbsoluteInterval(start, end);
327
                } catch (AbsoluteIntervalTypeNotRegisteredException e) {
328
                    // TODO Auto-generated catch block
329
                    e.printStackTrace();
330
                    return null;
331
                }
332
            }
333
        } else {
334
            return null;
335
        }
336

    
337
    }
338

    
339
    /**
340
     * Get the current instant
341
     *
342
     * @return
343
     */
344
    public Instant getInstant() {
345

    
346
        if( datesCorrect() ) {
347
            try {// this try is for the daylight saving (it creates exceptions)
348
                return (Instant) getJComponentStart().getTime();
349
            } catch (IllegalArgumentException e) {
350
                return null;
351
            }
352
        } else {
353
            return null;
354
        }
355

    
356
    }
357

    
358
    public void setInterval(Interval interval) {
359
        this.interval = interval;
360
    }
361

    
362
    /*
363
     * PRIVATE METHODS
364
     */
365
    private void createUI() {
366
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
367

    
368
        add((Component) getJComponentStart());
369
        add(getTimeSlider());
370
        add((Component) getJComponentEnd());
371

    
372
        getTimeSlider().setValueChangeable(false);
373

    
374
        getJComponentStart().setListener(this);
375
        getJComponentEnd().setListener(this);
376
        getTimeSlider().addTimeAdjustmentListener(this);
377
    }
378

    
379
    private void createComponents(Instant start, Instant end) {
380
        removeAll();
381
        revalidate();
382
        repaint();
383
        jComponentStart = null;
384
        jComponentEnd = null;
385
        slider = null;
386
        setInstants(start, end);
387
        createUI();
388

    
389
    }
390

    
391
    @SuppressWarnings("unused")
392
    private boolean datesCorrect() {
393
        if( getJComponentStart().isEnabled() && getJComponentEnd().isEnabled() ) {
394
            try {
395
                Instant init = (Instant) getJComponentStart().getTime();
396
                Instant end = (Instant) getJComponentEnd().getTime();
397
                return true;
398
            } catch (IllegalArgumentException e) {
399
                return false;
400
            }
401
        }
402
        return true;
403
    }
404

    
405
    private TimeSlider getTimeSlider() {
406
        if( slider == null ) {
407
            if( _mode == TIME_MODE.RELATIVE ) {
408
                slider = new TimeSlider(false);
409
            } else {
410
                slider = new TimeSlider(true);
411
            }
412
        }
413
        return slider;
414
    }
415

    
416
    private ITemporalComponent getJComponentStart() {
417
        if( jComponentStart == null ) {
418
            if( _mode == TIME_MODE.RELATIVE ) {
419
                jComponentStart = new RelativeTemporalComponent();
420
            } else {
421
                jComponentStart = new AbsoluteTemporalComponent();
422
            }
423
        }
424

    
425
        return jComponentStart;
426
    }
427

    
428
    private ITemporalComponent getJComponentEnd() {
429
        if( jComponentEnd == null ) {
430
            if( _mode == TIME_MODE.RELATIVE ) {
431
                jComponentEnd = new RelativeTemporalComponent();
432
            } else {
433
                jComponentEnd = new AbsoluteTemporalComponent();
434
            }
435
        }
436
        return jComponentEnd;
437
    }
438

    
439
    private void updateCalendars() {
440
        try {
441
            Instant start = (Instant) getJComponentStart().getTime();
442
            Instant end = (Instant) getJComponentEnd().getTime();
443
            getJComponentStart().setEndTime(end);
444
            getJComponentEnd().setStartTime(start);
445
        } catch (IllegalArgumentException e) {
446
        }
447
    }
448

    
449
    private boolean isUpdating() {
450
        return eventSynchronized;
451
    }
452

    
453
    private void setUpdating(boolean value) {
454
        eventSynchronized = value;
455
    }
456

    
457
    private AbsoluteInterval createAbsoluteInterval(AbsoluteInstant start, AbsoluteInstant end) throws AbsoluteIntervalTypeNotRegisteredException {
458
        long millisStart = start.toStandardDuration().getMillis();
459
        long millisEnd = end.toStandardDuration().getMillis();
460

    
461
        long millisInterval = millisEnd - millisStart;
462

    
463
        int years = 0, months = 0, weeks = 0, days = 0, hours = 0, minutes = 0, seconds = 0, millis = 0;
464

    
465
        long mod = millisInterval % TimeSlider.MILLIS_BY_YEAR;
466
        if( (millisInterval / TimeSlider.MILLIS_BY_YEAR) > 0 ) {
467
            years = (int) (millisInterval / TimeSlider.MILLIS_BY_YEAR);
468
            millisInterval = mod;
469
        }
470

    
471
        mod = millisInterval % TimeSlider.MILLIS_BY_MONTH;
472
        if( (millisInterval / TimeSlider.MILLIS_BY_MONTH) > 0 ) {
473
            months = (int) (millisInterval / TimeSlider.MILLIS_BY_MONTH);
474
            millisInterval = mod;
475
        }
476

    
477
        mod = millisInterval % TimeSlider.MILLIS_BY_WEEK;
478
        if( (millisInterval / TimeSlider.MILLIS_BY_WEEK) > 0 ) {
479
            weeks = (int) (millisInterval / TimeSlider.MILLIS_BY_WEEK);
480
            millisInterval = mod;
481
        }
482

    
483
        mod = millisInterval % TimeSlider.MILLIS_BY_DAY;
484
        if( (millisInterval / TimeSlider.MILLIS_BY_DAY) > 0 ) {
485
            if( (int) (millisInterval / TimeSlider.MILLIS_BY_DAY) > 0 ) {
486
                days = (int) (millisInterval / TimeSlider.MILLIS_BY_DAY);
487
            }
488
            millisInterval = mod;
489
        }
490

    
491
        mod = millisInterval % TimeSlider.MILLIS_BY_HOUR;
492
        if( (millisInterval / TimeSlider.MILLIS_BY_HOUR) > 0 ) {
493
            hours = (int) (millisInterval / TimeSlider.MILLIS_BY_HOUR);
494
            millisInterval = mod;
495
        }
496

    
497
        mod = millisInterval % TimeSlider.MILLIS_BY_MINUTE;
498
        if( (millisInterval / TimeSlider.MILLIS_BY_MINUTE) > 0 ) {
499
            seconds = (int) (millisInterval / TimeSlider.MILLIS_BY_MINUTE);
500
            millisInterval = mod;
501
        }
502

    
503
        mod = millisInterval % TimeSlider.MILLIS_BY_SECOND;
504
        if( (millisInterval / TimeSlider.MILLIS_BY_SECOND) > 0 ) {
505
            seconds = (int) (millisInterval / TimeSlider.MILLIS_BY_SECOND);
506
            millisInterval = mod;
507
        }
508

    
509
        mod = millisInterval;
510
        if( mod > 0 ) {
511
            millis = (int) mod;
512
        }
513

    
514
        return timeSupportManager.createAbsoluteInterval(years, months, weeks, days, hours, minutes, seconds, millis);
515

    
516
    }
517

    
518
    private Instant getApproximateInstant(Instant instant) {
519

    
520
        int index = 0;
521

    
522
        if( instant.isBefore(instants.get(0)) ) {
523
            return instants.get(0);
524
        }
525

    
526
        for( Instant ins : instants ) {
527
            if( ins.isEqual(instant) ) {
528
                return ins;
529
            } else if( ins.isAfter(instant) ) {
530
                break;
531
            }
532
            index++;
533
        }
534
        if( index == instants.size() ) {
535
            return instants.get(instants.size() - 1);
536
        }
537

    
538
        return instants.get(index);
539
    }
540
}