Revision 6900 org.gvsig.raster/branches/org.gvsig.raster.2.4/org.gvsig.raster/org.gvsig.raster.lib/org.gvsig.raster.lib.legend/org.gvsig.raster.lib.legend.impl/src/main/java/org/gvsig/raster/lib/legend/impl/DefaultColorInterpretation.java

View differences:

DefaultColorInterpretation.java
2 2

  
3 3
import java.util.List;
4 4

  
5
import org.gvsig.raster.lib.legend.api.ColorInterpretation;
5
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation;
6
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretationNotification;
7
import org.gvsig.raster.lib.legend.api.colortable.ColorTable;
8
import org.gvsig.raster.lib.legend.api.colortable.ColorTableNotification;
9
import org.gvsig.raster.lib.legend.impl.colorinterpretation.DefaultColorInterpretationNotification;
6 10
import org.gvsig.tools.ToolsLocator;
7 11
import org.gvsig.tools.dynobject.DynStruct;
12
import org.gvsig.tools.observer.Notification;
13
import org.gvsig.tools.observer.Observable;
14
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable;
8 15
import org.gvsig.tools.persistence.PersistenceManager;
9 16
import org.gvsig.tools.persistence.PersistentState;
10 17
import org.gvsig.tools.persistence.exception.PersistenceException;
......
13 20
 * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
14 21
 *
15 22
 */
16
public class DefaultColorInterpretation implements ColorInterpretation {
23
public class DefaultColorInterpretation extends BaseWeakReferencingObservable implements ColorInterpretation {
17 24

  
18 25
    /**
19 26
     * Persistence definition name
......
27 34
        "Persitence color interpretation definition description";
28 35

  
29 36
    private static final String COLOR_INTERPRETATION_PERSISTENCE_FIELD = "colorInterpretation";
37
    private static final String COLOR_TABLE_PERSISTENCE_FIELD = "colorTable";
38
    private static final String PALETTE_BAND_PERSISTENCE_FIELD = "paletteBand";
30 39

  
40

  
31 41
    private String[] colorInterpretation = null;
32 42

  
43
    private ColorTable colorTable;
44

  
45
    private int paletteBand;
46

  
33 47
    /**
34 48
     * Empty constructor
35 49
     */
36 50
    public DefaultColorInterpretation() {
37
        colorInterpretation = new String[0];
51
        this.colorInterpretation = new String[0];
52
        this.paletteBand = -1;
53
        this.colorTable = null;
54

  
38 55
    }
39 56

  
40 57
    /**
......
53 70
        this.colorInterpretation = new String[bandColorInterpretations.length];
54 71
        System.arraycopy(bandColorInterpretations, 0, this.colorInterpretation, 0,
55 72
            this.colorInterpretation.length);
73
        this.paletteBand = -1;
74
        this.colorTable = null;
56 75
    }
57 76

  
58 77
    /**
......
65 84
     */
66 85
    public DefaultColorInterpretation(int nBands) {
67 86
        this.colorInterpretation = new String[nBands];
87
        this.paletteBand = -1;
88
        this.colorTable = null;
68 89
    }
69 90

  
70 91
    /**
......
72 93
     */
73 94
    public DefaultColorInterpretation(String bandColorInterpretation) {
74 95

  
96
        this.paletteBand = -1;
97
        this.colorTable = null;
75 98
        if (bandColorInterpretation.equals(RGB)) {
76 99
            this.colorInterpretation = new String[] { RED_BAND, GREEN_BAND, BLUE_BAND };
77 100
        } else if (bandColorInterpretation.equals(BGR)) {
......
82 105
            this.colorInterpretation = new String[] { GRAY_BAND };
83 106
        }else if (bandColorInterpretation.equals(PALETTE)) {
84 107
            this.colorInterpretation = new String[] { PALETTE_BAND };
108
            this.paletteBand = 0;
85 109
        }else if (bandColorInterpretation.equals(HSL)) {
86 110
            this.colorInterpretation = new String[] { HUE_BAND, SATURATION_BAND, LIGHTNESS_BAND };
87 111
        }else if (bandColorInterpretation.equals(CMYK)) {
......
99 123
    @Override
100 124
    public void setColorInterpValue(int band, String value) {
101 125

  
126
        String[] oldColorInterpretation = new String[this.colorInterpretation.length];
127
        System.arraycopy(this.colorInterpretation, 0, oldColorInterpretation, 0, this.colorInterpretation.length);
102 128
        if (band < 0 || band >= this.colorInterpretation.length) {
103 129
            throw new IllegalArgumentException("Band is out of color interpretation bands");
104 130
        }
......
108 134
        }
109 135

  
110 136
        this.colorInterpretation[band] = value;
137
        this.notifyObservers(new DefaultColorInterpretationNotification(ColorInterpretationNotification.CHANGED_COLOR_INTERPRETATION_VALUE,
138
            new Object[] { oldColorInterpretation, this.colorInterpretation }));
111 139
    }
112 140

  
113 141
    @Override
......
287 315
    }
288 316

  
289 317
    @Override
290
    public boolean isPalette() {
291
        if (colorInterpretation != null && this.length() == 1
292
            && colorInterpretation[0].equals(PALETTE_BAND)) {
293
            return true;
294
        }
295
        return false;
296
    }
297

  
298
    @Override
299 318
    public boolean hasAnyPaletteBand() {
300 319
        if (colorInterpretation != null) {
301 320
            for (int i = 0; i < colorInterpretation.length; i++) {
......
390 409

  
391 410
    @Override
392 411
    public void addColorInterpretation(ColorInterpretation ci) {
412
        String[] oldColorInterpretation = this.colorInterpretation;
393 413
        String[] newCI = new String[colorInterpretation.length + ci.length()];
394 414
        for (int i = 0; i < colorInterpretation.length; i++)
395 415
            newCI[i] = colorInterpretation[i];
......
397 417
            newCI[colorInterpretation.length + i] = ci.get(i);
398 418
        }
399 419
        this.colorInterpretation = newCI;
420
        this.notifyObservers(new DefaultColorInterpretationNotification(ColorInterpretationNotification.ADDED_COLOR_INTERPRETATION,
421
            new Object[] { oldColorInterpretation, this.colorInterpretation }));
400 422

  
401 423
    }
402 424

  
......
409 431
                    PERSISTENCE_DESCRIPTION, null, null);
410 432
            definition.addDynFieldList(COLOR_INTERPRETATION_PERSISTENCE_FIELD)
411 433
                .setClassOfItems(String.class).setMandatory(false);
434
            definition.addDynFieldInt(PALETTE_BAND_PERSISTENCE_FIELD).setMandatory(false);
435
            definition.addDynFieldObject(COLOR_TABLE_PERSISTENCE_FIELD).setClassOfValue(ColorTable.class);
412 436
        }
413 437
    }
414 438

  
......
421 445
        List<String> descriptions =
422 446
            (List<String>) state.get(COLOR_INTERPRETATION_PERSISTENCE_FIELD);
423 447
        this.colorInterpretation = descriptions.toArray(new String[descriptions.size()]);
448
        this.setPaletteBand(state.getInt(PALETTE_BAND_PERSISTENCE_FIELD, -1));
449
        this.setPalette((ColorTable)state.get(COLOR_TABLE_PERSISTENCE_FIELD));
424 450
    }
425 451

  
426 452
    @Override
427 453
    public void copyFrom(ColorInterpretation colorInterpretation) {
428
        this.colorInterpretation = colorInterpretation.getValues();
454
        String[] oldColorInterpretation = new String[this.length()];
455
        System.arraycopy(this.colorInterpretation, 0, oldColorInterpretation, 0, length());
456
        System.arraycopy(colorInterpretation.getValues(), 0, this.colorInterpretation, 0, colorInterpretation.length());
457
        this.paletteBand = colorInterpretation.getPaletteBand();
458

  
459
        this.notifyObservers(new DefaultColorInterpretationNotification(ColorInterpretationNotification.COPIED_FROM_COLOR_INTERPRETATION,
460
            new Object[] { oldColorInterpretation, this.colorInterpretation }));
429 461
    }
430 462

  
431 463
    @Override
432 464
    public Object clone() throws CloneNotSupportedException {
433
        ColorInterpretation cloned = (ColorInterpretation) super.clone();
465
        ColorInterpretation cloned = new DefaultColorInterpretation(this.length());
434 466
        cloned.copyFrom(this);
467
        if(this.colorTable!=null){
468
            cloned.setPalette((ColorTable)this.colorTable.clone());
469
        }
435 470
        return cloned;
436 471
    }
437 472

  
......
511 546
        return false;
512 547
    }
513 548

  
549
    @Override
550
    public boolean isPalette() {
551
        if(this.paletteBand>=0 && this.paletteBand<this.length() && this.colorTable!=null){
552
            return true;
553
        }
554
        return false;
555
    }
556

  
557
    @Override
558
    public void setPalette(ColorTable colorTable) {
559
        ColorTable oldColorTable = this.colorTable;
560
        if (colorTable != null && !colorTable.equals(oldColorTable)) {
561
            if(oldColorTable!=null){
562
                oldColorTable.deleteObserver(this);
563
            }
564
            this.colorTable = colorTable;
565
            this.colorTable.addObserver(this);
566
            this.notifyObservers(new DefaultColorInterpretationNotification(
567
                ColorInterpretationNotification.SETTED_PALETTE, new Object[] { oldColorTable, colorTable }));
568
        }
569
    }
570

  
571
    @Override
572
    public void setPaletteBand(int band) {
573
        if (band != this.paletteBand) {
574
            int oldPaletteBand = this.paletteBand;
575
            if (band >= 0 && band < this.length()) {
576
                this.paletteBand = band;
577
            } else {
578
                this.paletteBand = -1;
579
            }
580
            this.notifyObservers(new DefaultColorInterpretationNotification(
581
                ColorInterpretationNotification.SETTED_PALETTE_BAND, new Object[] { new Integer(oldPaletteBand),
582
                    new Integer(this.paletteBand) }));
583
        }
584
    }
585

  
586
    @Override
587
    public int getPaletteBand() {
588
        return this.paletteBand;
589
    }
590

  
591
    @Override
592
    public ColorTable getPalette() {
593
        return this.colorTable;
594
    }
595

  
596
    @Override
597
    public void update(Observable observable, Object notification) {
598
        if (notification instanceof ColorTableNotification && observable instanceof ColorTable) {
599
            Notification colorTableNotification = (Notification) notification;
600
            if (colorTableNotification.getType().equals(ColorTableNotification.CHANGED_INTERPOLATED_COLOR_TABLE)
601
                || colorTableNotification.getType().equals(ColorTableNotification.COMPRESSED_COLOR_TABLE)
602
                || colorTableNotification.getType().equals(ColorTableNotification.COPIED_FROM_COLOR_TABLE)
603
                || colorTableNotification.getType().equals(ColorTableNotification.REMOVED_DUPLICATED_VALUES_COLOR_TABLE)
604
                || colorTableNotification.getType().equals(ColorTableNotification.SETTED_CLASS_VALUES_COLOR_TABLE)) {
605
                this.notifyObservers(new DefaultColorInterpretationNotification(ColorInterpretationNotification.CHANGED_PALETTE,
606
                    new Object[] { observable }));
607
            }
608
        }
609
    }
610

  
514 611
}

Also available in: Unified diff