/
SignalElementManager.java
632 lines (565 loc) · 18.4 KB
/
SignalElementManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/*
* OpenBench LogicSniffer / SUMP project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*
* Copyright (C) 2006-2010 Michael Poppitz, www.sump.org
* Copyright (C) 2010 J.W. Janssen, www.lxtreme.nl
*/
package nl.lxtreme.ols.client.signaldisplay.signalelement;
import static nl.lxtreme.ols.client.signaldisplay.signalelement.SignalElement.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import nl.lxtreme.ols.api.*;
import nl.lxtreme.ols.api.data.*;
import nl.lxtreme.ols.client.signaldisplay.*;
import nl.lxtreme.ols.client.signaldisplay.laf.*;
import nl.lxtreme.ols.client.signaldisplay.signalelement.ISignalElementChangeListener.*;
/**
* Manager for all channels and their grouping.
* <p>
* This class is thread-safe.
* </p>
*/
public final class SignalElementManager implements IDataModelChangeListener
{
// INNER TYPES
/**
* Defines a measurer for signal elements.
*/
public static interface SignalElementMeasurer
{
public static final SignalElementMeasurer STRICT_MEASURER = new StrictChannelElementMeasurer();
public static final SignalElementMeasurer LOOSE_MEASURER = new LooseChannelElementMeasurer();
/**
* Determines whether a signal element at a given Y-position with a given
* height fits in the boundaries defined by [minY, maxY].
*
* @param aYpos
* the Y-position of the signal element, in pixels;
* @param aHeight
* the height of the signal element, in pixels;
* @param aMinY
* the minimum Y-position to fit in;
* @param aMaxY
* the maximum Y-position to fit in.
* @return <code>true</code> if the signal element would fit,
* <code>false</code> otherwise.
*/
boolean signalElementFits( int aYpos, int aHeight, int aMinY, int aMaxY );
}
/**
* Provides a loose channel element measurer, which selects channel elements
* that completely fit within the boundaries, and also channel elements that
* partly fit within the boundaries.
*/
private static class LooseChannelElementMeasurer implements SignalElementMeasurer
{
/**
* {@inheritDoc}
*/
@Override
public boolean signalElementFits( final int aYpos, final int aHeight, final int aMinY, final int aMaxY )
{
final int y1 = aYpos;
final int y2 = y1 + aHeight;
return ( ( ( y1 >= aMinY ) || ( y2 >= aMinY ) ) && ( ( y1 <= aMaxY ) || ( y2 <= aMaxY ) ) );
}
}
/**
* Provides a strict channel element measurer, which only selects channel
* elements that completely fit within the boundaries.
*/
private static class StrictChannelElementMeasurer implements SignalElementMeasurer
{
/**
* {@inheritDoc}
*/
@Override
public boolean signalElementFits( final int aYpos, final int aHeight, final int aMinY, final int aMaxY )
{
return ( aYpos >= aMinY ) && ( aYpos <= aMaxY );
}
}
// VARIABLES
/** the total set of channel groups. */
private final List<ElementGroup> groups;
/** the total set of channels. */
private final List<SignalElement> elements;
private final EventListenerList eventListeners;
private final Object lock = new Object();
// CONSTRUCTORS
/**
* Creates a new {@link SignalElementManager} instance.
*/
public SignalElementManager()
{
this.groups = new ArrayList<ElementGroup>();
this.elements = new ArrayList<SignalElement>();
this.eventListeners = new EventListenerList();
}
// METHODS
/**
* Adds a channel change listener.
*
* @param aListener
* the listener to add, cannot be <code>null</code>.
*/
public void addChannelChangeListener( final ISignalElementChangeListener aListener )
{
this.eventListeners.add( ISignalElementChangeListener.class, aListener );
}
/**
* Returns the absolute height of the screen.
*
* @param aHeightProvider
* the provider for the various element's heights, cannot be
* <code>null</code>.
* @return a screen height, in pixels, >= 0 && < {@value Integer#MAX_VALUE}.
*/
public int calculateScreenHeight()
{
int height = 0;
for ( ElementGroup cg : getGroups() )
{
if ( !cg.isVisible() )
{
continue;
}
for ( SignalElement element : cg.getElements() )
{
if ( element.isSignalGroup() )
{
height += element.getHeight();
}
else if ( element.isDigitalSignal() && cg.isShowDigitalSignals() )
{
height += element.getHeight();
}
else if ( element.isGroupSummary() && cg.isShowGroupSummary() )
{
height += element.getHeight();
}
else if ( element.isAnalogSignal() && cg.isShowAnalogSignal() )
{
height += element.getHeight();
}
}
}
return height;
}
/**
* {@inheritDoc}
*/
@Override
public void dataModelChanged( final DataSet aCapturedData )
{
// Make sure only a single thread at a time modifies us...
synchronized ( this.lock )
{
final Channel[] newChannelList = aCapturedData.getChannels();
// Reset channel groups so they align with the given data model...
final int groupCount = Math.max( 1, ( int )Math.ceil( newChannelList.length / ( double )Ols.CHANNELS_PER_BLOCK ) );
final int channelsPerGroup = ( int )Math.ceil( newChannelList.length / ( double )groupCount );
// TODO property change listeners on SignalElements!
this.elements.clear();
this.groups.clear();
for ( int g = 0; g < groupCount; g++ )
{
final ElementGroup group = addGroup( "Group " + ( g + 1 ) );
// We start with a signal group element...
addSignalElement( group, createSignalGroupElement( group ) );
for ( int c = 0; c < channelsPerGroup; c++ )
{
final int channelIdx = ( g * channelsPerGroup ) + c;
if ( newChannelList[channelIdx] == null )
{
continue;
}
addSignalElement( group, createDigitalSignalElement( newChannelList[channelIdx], group ) );
}
addSignalElement( group, createGroupSummaryElement( group ) );
addSignalElement( group, createAnalogScopeElement( group ) );
}
}
fireGroupStructureChangeEvent( getAssignedElements() );
}
/**
* Returns all signal elements.
*
* @return a collection of all signal elements, never <code>null</code>.
*/
public Collection<SignalElement> getAllElements()
{
Collection<SignalElement> result = new ArrayList<SignalElement>();
synchronized ( this.lock )
{
if ( this.elements != null )
{
result.addAll( this.elements );
}
}
return result;
}
/**
* Returns a sorted set of all assigned (not available) signal elements.
*
* @return a sorted set of all assigned signal elements, never
* <code>null</code>.
*/
public SortedSet<SignalElement> getAssignedElements()
{
SortedSet<SignalElement> channelIndexes = new TreeSet<SignalElement>();
synchronized ( this.lock )
{
for ( ElementGroup cg : this.groups )
{
channelIndexes.addAll( cg.getElements() );
}
}
return channelIndexes;
}
/**
* Returns all current channel groups.
*
* @return an array of channel groups, never <code>null</code>.
*/
public Collection<ElementGroup> getGroups()
{
Collection<ElementGroup> result;
synchronized ( this.groups )
{
result = Collections.unmodifiableCollection( this.groups );
}
return result;
}
/**
* Returns all signal elements the given range of all visible channel groups.
*
* @param aY
* the screen Y-coordinate;
* @param aHeight
* the screen height;
* @param aMeasurer
* the measurer to use to determine whether or not a signal element
* fits in the given dimensions.
* @return an array of channels, never <code>null</code>.
*/
public SignalElement[] getSignalElements( final int aY, final int aHeight, final SignalElementMeasurer aMeasurer )
{
final List<SignalElement> result = new ArrayList<SignalElement>();
final int yMin = aY;
final int yMax = aHeight + aY;
final int spacing = UIManager.getInt( UIManagerKeys.SIGNAL_ELEMENT_SPACING );
int yPos = 0;
for ( ElementGroup group : getGroups() )
{
if ( !group.isVisible() )
{
continue;
}
if ( yPos > yMax )
{
// Optimization: no need to continue after the requested end position...
break;
}
for ( SignalElement element : group.getElements() )
{
if ( element.isSignalGroup() )
{
int height = element.getHeight();
if ( aMeasurer.signalElementFits( yPos, height, yMin, yMax ) )
{
element.setYposition( yPos );
result.add( element );
}
yPos += height + spacing;
}
else if ( element.isDigitalSignal() && group.isShowDigitalSignals() )
{
// Does this individual channel fit?
int height = element.getHeight();
if ( aMeasurer.signalElementFits( yPos, height, yMin, yMax ) )
{
element.setYposition( yPos );
result.add( element );
}
yPos += height + spacing;
}
else if ( element.isGroupSummary() && group.isShowGroupSummary() )
{
int height = element.getHeight();
if ( aMeasurer.signalElementFits( yPos, height, yMin, yMax ) )
{
element.setYposition( yPos );
result.add( element );
}
yPos += height + spacing;
}
else if ( element.isAnalogSignal() && group.isShowAnalogSignal() )
{
int height = element.getHeight();
if ( aMeasurer.signalElementFits( yPos, height, yMin, yMax ) )
{
element.setYposition( yPos );
result.add( element );
}
yPos += height + spacing;
}
}
}
return result.toArray( new SignalElement[result.size()] );
}
/**
* Moves a given signal element from its current position to a new group with
* a new index.
*
* @param aMovedElement
* the signal element to move, cannot be <code>null</code>;
* @param aNewGroup
* the new element group to move the signal element to, can be equal
* to the current group of the moved signal element but never
* <code>null</code>;
* @param aNewIndex
* the new index of the moved signal element.
*/
public void moveElement( final SignalElement aMovedElement, final ElementGroup aNewGroup, final int aNewIndex )
{
if ( aMovedElement == null )
{
throw new IllegalArgumentException( "Moved signal element cannot be null!" );
}
if ( aNewGroup == null )
{
throw new IllegalArgumentException( "New group cannot be null!" );
}
if ( ( aNewIndex < 0 ) || ( aNewIndex > Ols.MAX_CHANNELS ) )
{
throw new IllegalArgumentException( "Invalid new index: " + aNewIndex + "!" );
}
final ElementGroup oldCG = aMovedElement.getGroup();
final int oldIndex = aMovedElement.getVirtualIndex();
// Perform the actual move itself...
aNewGroup.moveChannel( aMovedElement, aNewIndex );
// Fire an event to all interested listeners...
fireChannelMoveEvent( new ElementMoveEvent( aMovedElement, oldCG, oldIndex ) );
}
/**
* Removes a channel change listener.
*
* @param aListener
* the listener to remove, cannot be <code>null</code>.
*/
public void removeChannelChangeListener( final ISignalElementChangeListener aListener )
{
this.eventListeners.remove( ISignalElementChangeListener.class, aListener );
}
/**
* Fires a {@link ElementMoveEvent} to all interested listeners.
*
* @param aEvent
* the event to fire,cannot be <code>null</code>.
*/
final void fireChannelMoveEvent( final ElementMoveEvent aEvent )
{
final ISignalElementChangeListener[] listeners = this.eventListeners
.getListeners( ISignalElementChangeListener.class );
for ( ISignalElementChangeListener listener : listeners )
{
listener.signalElementMoved( aEvent );
}
}
/**
* Fires a "channelgroup structure changed"-event to all interested listeners.
*
* @param aSignalElements
* the event to fire,cannot be <code>null</code>.
*/
final void fireGroupStructureChangeEvent( final Collection<SignalElement> aSignalElements )
{
final ISignalElementChangeListener[] listeners = this.eventListeners
.getListeners( ISignalElementChangeListener.class );
for ( ISignalElementChangeListener listener : listeners )
{
listener.groupStructureChanged( aSignalElements );
}
}
/**
* Adds a new group to this manager.
*
* @param aName
* the name of the new group, cannot be <code>null</code> or empty.
* @return the newly added group, never <code>null</code>.
* @throws IllegalArgumentException
* in case the given name was <code>null</code> or empty;
* @throws IllegalStateException
* in case no signal elements are available for the new group.
*/
protected ElementGroup addGroup( final String aName )
{
ElementGroup result;
synchronized ( this.lock )
{
result = new ElementGroup( this.groups.size(), aName );
this.groups.add( result );
}
return result;
}
/**
* Adds a given signal element to the given group.
* <p>
* If the given group already contains the given signal element, then this
* method is effectively a no-op.
* </p>
*
* @param aGroup
* the group to add the signal element to, cannot be
* <code>null</code>;
* @param aSignalElement
* the signal element to add to the group, cannot be
* <code>null</code>.
* @throws IllegalArgumentException
* in case one of the given parameters was <code>null</code>.
*/
protected void addSignalElement( final ElementGroup aGroup, final SignalElement aSignalElement )
{
if ( aGroup == null )
{
throw new IllegalArgumentException( "Group cannot be null!" );
}
if ( aSignalElement == null )
{
throw new IllegalArgumentException( "Signal element cannot be null!" );
}
if ( aGroup.hasElement( aSignalElement ) )
{
// Nothing to do; we're done...
return;
}
synchronized ( this.lock )
{
if ( !this.elements.contains( aSignalElement ) )
{
this.elements.add( aSignalElement );
}
// Keep a reference to the former channel group...
final ElementGroup oldGroup = aSignalElement.getGroup();
// This will automatically remove the given channel from its former
// channel group...
aGroup.addElement( aSignalElement );
// When there are no more channels left in this channel group, remove
// it...
if ( ( oldGroup != null ) && !oldGroup.hasElements() )
{
this.groups.remove( oldGroup );
}
}
}
/**
* Returns a sorted set of all unassigned (= available) signal elements.
*
* @return a sorted set of unassigned signal elements, never <code>null</code>
* , but can be empty.
*/
protected List<SignalElement> getUnassignedElements()
{
List<SignalElement> channels = new ArrayList<SignalElement>( getAllElements() );
for ( ElementGroup cg : getGroups() )
{
channels.removeAll( cg.getElements() );
}
return channels;
}
/**
* Removes the group with the given name.
*
* @param aName
* the name of the group to remove, cannot be <code>null</code> or
* empty.
* @throws IllegalArgumentException
* in case the given name was <code>null</code> or empty.
*/
protected void removeGroup( final String aName )
{
if ( ( aName == null ) || aName.trim().isEmpty() )
{
throw new IllegalArgumentException( "Name cannot be null or empty!" );
}
synchronized ( this.groups )
{
ElementGroup group = getGroupByName( aName );
if ( group != null )
{
this.groups.remove( group );
}
}
}
/**
* Removes a given signal element from a given group.
*
* @param aGroup
* the group to remove the signal element from, cannot be
* <code>null</code>;
* @param aSignalElement
* the signal element to remove, cannot be <code>null</code>.
* @throws IllegalArgumentException
* in case one of the given parameters was <code>null</code>.
*/
protected void removeSignalElement( final ElementGroup aGroup, final SignalElement aSignalElement )
{
if ( aGroup == null )
{
throw new IllegalArgumentException( "Group cannot be null!" );
}
if ( aSignalElement == null )
{
throw new IllegalArgumentException( "Signal element cannot be null!" );
}
aGroup.removeElement( aSignalElement );
}
/**
* Returns the channel group with a given name.
*
* @param aName
* the name of the channel group to return, cannot be
* <code>null</code> or empty.
* @return the channel group with the given name, or <code>null</code> if no
* such channel group exists.
* @throws IllegalArgumentException
* in case the given name was <code>null</code> or empty.
*/
private ElementGroup getGroupByName( final String aName )
{
if ( ( aName == null ) || aName.trim().isEmpty() )
{
throw new IllegalArgumentException( "Name cannot be null or empty!" );
}
Iterator<ElementGroup> channelGroupIter;
synchronized ( this.lock )
{
channelGroupIter = this.groups.iterator();
}
while ( channelGroupIter.hasNext() )
{
ElementGroup cg = channelGroupIter.next();
if ( aName.equals( cg.getName() ) )
{
return cg;
}
}
return null;
}
}