root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / table / LinkSelectionObserver.java @ 29598
History | View | Annotate | Download (3.11 KB)
1 |
package org.gvsig.app.project.documents.table; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
|
5 |
import org.gvsig.andami.messages.NotificationManager; |
6 |
import org.gvsig.fmap.dal.exception.DataException; |
7 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
8 |
import org.gvsig.fmap.dal.feature.Feature; |
9 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
10 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
11 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
12 |
import org.gvsig.tools.observer.Observable; |
13 |
import org.gvsig.tools.observer.Observer; |
14 |
|
15 |
|
16 |
public class LinkSelectionObserver implements Observer{ |
17 |
private int field1; |
18 |
private int field2; |
19 |
private FeatureStore fs1;
|
20 |
private FeatureStore fs2;
|
21 |
public LinkSelectionObserver(FeatureStore modelo, FeatureStore modelo2, String field1, String field2) { |
22 |
this.fs1=modelo;
|
23 |
this.fs2=modelo2;
|
24 |
try {
|
25 |
this.field1=fs1.getDefaultFeatureType().getIndex(field1);
|
26 |
this.field2=fs2.getDefaultFeatureType().getIndex(field2);
|
27 |
} catch (DataException e) {
|
28 |
// TODO Auto-generated catch block
|
29 |
e.printStackTrace(); |
30 |
} |
31 |
} |
32 |
public void update(Observable arg0, Object arg1) { |
33 |
try{
|
34 |
FeatureSet fCollection1 = (FeatureSet)fs1.getSelection(); |
35 |
FeatureSelection fCollection2 = (FeatureSelection)fs2.createSelection(); |
36 |
ArrayList idx = new ArrayList(); |
37 |
|
38 |
// Construimos el ?ndice
|
39 |
DisposableIterator iterator1 = null;
|
40 |
try {
|
41 |
iterator1 = fCollection1.iterator(); |
42 |
while (iterator1.hasNext()) {
|
43 |
Feature feature = (Feature) iterator1.next(); |
44 |
Object obj = feature.get(field1);
|
45 |
if (!idx.contains(obj)) {
|
46 |
idx.add(obj); |
47 |
} |
48 |
} |
49 |
} finally {
|
50 |
if (iterator1 != null) { |
51 |
iterator1.dispose(); |
52 |
} |
53 |
} |
54 |
// fCollection1.dispose();
|
55 |
// for (int i = bs1.nextSetBit(0); i >= 0;
|
56 |
// i = bs1.nextSetBit(i + 1)) {
|
57 |
// Value v = modelo1.getFieldValue((long) i, index1);
|
58 |
|
59 |
// if (idx.get(v) == null) {
|
60 |
// idx.put(v, new Integer(i));
|
61 |
// }
|
62 |
// }
|
63 |
FeatureSet set = null;
|
64 |
DisposableIterator iterator2 = null;
|
65 |
|
66 |
try {
|
67 |
set = fs2.getFeatureSet(); |
68 |
iterator2 = set.iterator(); |
69 |
while (iterator2.hasNext()) {
|
70 |
Feature feature = (Feature) iterator2.next(); |
71 |
Object obj=feature.get(field2);
|
72 |
if (idx.contains(obj)){
|
73 |
fCollection2.select(feature); |
74 |
} |
75 |
} |
76 |
// Integer pi = (Integer) idx.get(obj);
|
77 |
// if (pi != null) {
|
78 |
// bs2.set(i);
|
79 |
// }
|
80 |
|
81 |
// for (int i = 0; i < modelo2.getRowCount(); i++) {
|
82 |
// Value v = modelo2.getFieldValue(i, index2);
|
83 |
// Integer pi = (Integer) idx.get(v);
|
84 |
|
85 |
// if (pi != null) {
|
86 |
// bs2.set(i);
|
87 |
// }
|
88 |
// }
|
89 |
} catch (DataException e1) {
|
90 |
NotificationManager.addError(e1); |
91 |
return;
|
92 |
} finally {
|
93 |
if (iterator2 != null) { |
94 |
iterator2.dispose(); |
95 |
} |
96 |
if (set != null) { |
97 |
set.dispose(); |
98 |
} |
99 |
} |
100 |
|
101 |
// this applies the selection to the linked table
|
102 |
if (fs1!=fs2) {
|
103 |
// fs2.beginComplexNotification();
|
104 |
fs2.setSelection(fCollection2); |
105 |
// fs2.endComplexNotification();
|
106 |
//modelo2.fireSelectionEvents();
|
107 |
} |
108 |
} catch (DataException e2) {
|
109 |
NotificationManager.addError(e2); |
110 |
return;
|
111 |
} |
112 |
} |
113 |
|
114 |
} |