PageRenderTime 62ms CodeModel.GetById 12ms app.highlight 44ms RepoModel.GetById 2ms app.codeStats 0ms

/webportal/src/main/java/org/ala/spatial/analysis/web/EnvironmentalList.java

http://alageospatialportal.googlecode.com/
Java | 379 lines | 316 code | 52 blank | 11 comment | 109 complexity | a365d0ec2418e70499c69dd20421a5fe MD5 | raw file
  1package org.ala.spatial.analysis.web;
  2
  3import au.org.emii.portal.composer.MapComposer;
  4import au.org.emii.portal.menu.MapLayer;
  5import au.org.emii.portal.util.LayerUtilities;
  6import java.util.ArrayList;
  7import java.util.Comparator;
  8import java.util.Set;
  9import net.sf.json.JSONArray;
 10import net.sf.json.JSONObject;
 11import org.ala.spatial.util.CommonData;
 12import org.ala.spatial.util.ListEntry;
 13import org.zkoss.zk.ui.event.Event;
 14import org.zkoss.zk.ui.event.EventListener;
 15import org.zkoss.zul.Image;
 16import org.zkoss.zul.Listbox;
 17import org.zkoss.zul.Listcell;
 18import org.zkoss.zul.Listitem;
 19import org.zkoss.zul.ListitemRenderer;
 20import org.zkoss.zul.SimpleListModel;
 21
 22/**
 23 *
 24 * @author ajay
 25 */
 26public class EnvironmentalList extends Listbox {
 27
 28    ArrayList<ListEntry> listEntries;
 29    float[] threasholds = {0.1f, 0.3f, 1.0f};
 30    SimpleListModel listModel;
 31    MapComposer mapComposer;
 32    boolean includeAnalysisLayers;
 33    boolean disableContextualLayers;
 34    boolean singleDomain;
 35
 36    public void init(MapComposer mc, boolean includeAnalysisLayers, boolean disableContextualLayers, boolean singleDomain) {
 37        mapComposer = mc;
 38        this.includeAnalysisLayers = includeAnalysisLayers;
 39        this.disableContextualLayers = disableContextualLayers;
 40        this.singleDomain = singleDomain;
 41
 42        try {
 43            setupListEntries();
 44
 45            setupList();
 46
 47        } catch (Exception e) {
 48            e.printStackTrace();
 49        }
 50    }
 51
 52    void setupListEntries() {
 53        listEntries = new ArrayList<ListEntry>();
 54        JSONArray ja = CommonData.getLayerListJSONArray();
 55        for (int i = 0; i < ja.size(); i++) {
 56            JSONObject jo = ja.getJSONObject(i);
 57            listEntries.add(
 58                    new ListEntry(jo.getString("name"),
 59                    (jo.containsKey("displayname") ? jo.getString("displayname") : jo.getString("name")),
 60                    (jo.containsKey("classification1") ? jo.getString("classification1") : ""),
 61                    (jo.containsKey("classification2") ? jo.getString("classification2") : ""),
 62                    (jo.containsKey("type") ? jo.getString("type") : ""),
 63                    (jo.containsKey("domain") ? jo.getString("domain") : ""),
 64                    jo));
 65        }
 66
 67        if (includeAnalysisLayers) {         //add
 68            for (MapLayer ml : mapComposer.getAnalysisLayers()) {
 69                ListEntry le = null;
 70                if (ml.getSubType() == LayerUtilities.ALOC) {
 71                    le = new ListEntry(ml.getName(), ml.getDisplayName(), "Analysis", "Classification", "Contextual", null, null);
 72                } else if (ml.getSubType() == LayerUtilities.MAXENT) {
 73                    le = new ListEntry(ml.getName(), ml.getDisplayName(), "Analysis", "Prediction", "Environmental", null, null);
 74                } else if (ml.getSubType() == LayerUtilities.GDM) {
 75                    le = new ListEntry(ml.getName(), ml.getDisplayName(), "Analysis", "GDM", "Environmental", null, null);
 76                } else if (ml.getSubType() == LayerUtilities.ODENSITY) {
 77                    le = new ListEntry(ml.getName(), ml.getDisplayName(), "Analysis", "Occurrence Density", "Environmental", null, null);
 78                } else if (ml.getSubType() == LayerUtilities.SRICHNESS) {
 79                    le = new ListEntry(ml.getName(), ml.getDisplayName(), "Analysis", "Species Richness", "Environmental", null, null);
 80                }
 81                if (le != null) {
 82                    listEntries.add(le);
 83                }
 84            }
 85        }
 86
 87        java.util.Collections.sort(listEntries, new Comparator<ListEntry>() {
 88
 89            @Override
 90            public int compare(ListEntry e1, ListEntry e2) {
 91                return (e1.catagory1 + " " + e1.catagory2 + " " + e1.displayname).compareTo(e2.catagory1 + " " + e2.catagory2 + " " + e2.displayname);
 92            }
 93        });
 94    }
 95
 96    public void setupList() {
 97        try {
 98            setItemRenderer(new ListitemRenderer() {
 99
100                @Override
101                public void render(Listitem li, Object data) {                    
102                    Listcell n = new Listcell(((ListEntry) data).catagoryNames());
103                    //n.setHflex("1");
104                    n.setParent(li);
105                    n = new Listcell(((ListEntry) data).displayname);
106                    //n.setHflex("1");
107                    n.setParent(li);
108
109                    Listcell lc = new Listcell();
110                    lc.setParent(li);
111                    lc.setValue(((ListEntry) data));
112
113                    String type = ((ListEntry) data).type;
114                    if (disableContextualLayers && type.equalsIgnoreCase("contextual")) {
115                        li.setDisabled(true);
116                    }
117
118                    Image img = new Image();
119                    img.setSrc("/img/information.png");
120
121                    img.addEventListener("onClick", new EventListener() {
122
123                        @Override
124                        public void onEvent(Event event) throws Exception {
125                            //re-toggle the checked flag (issue 572)
126                            Listitem li = (Listitem) event.getTarget().getParent().getParent();
127                            li.getListbox().toggleItemSelection(li);
128                            EnvironmentalList el = (EnvironmentalList) li.getParent();
129                            el.updateDistances();
130
131                            String s = ((ListEntry) ((Listcell) event.getTarget().getParent()).getValue()).name;
132                            String metadata = CommonData.layersServer + "/layers/view/more/" + s;
133                            mapComposer.activateLink(metadata, "Metadata", false);
134
135                        }
136                    });
137                    img.setParent(lc);
138
139                    //String type = ((ListEntry) data).type;
140
141                    if (type.equalsIgnoreCase("environmental")) {
142                        float value = ((ListEntry) data).value;
143                        lc = new Listcell(" ");
144                        if (threasholds[0] > value) {
145                            lc.setSclass("lcRed");//setStyle("background: #bb2222;");
146                        } else if (threasholds[1] > value) {
147                            lc.setSclass("lcYellow");//lc.setStyle("background: #ffff22;");
148                        } else if (1 >= value) {
149                            lc.setSclass("lcGreen");//lc.setStyle("background: #22aa22;");
150                        } else {
151                            lc.setSclass("lcWhite");//setStyle("background: #ffffff;");
152                        }
153                        lc.setParent(li);
154                    }
155                }
156
157                ;
158            });
159
160            listModel = new SimpleListModel(listEntries);
161            setModel(listModel);
162
163            //renderAll();
164
165        } catch (Exception e) {
166            System.out.println("error setting up env list");
167            e.printStackTrace(System.out);
168        }
169    }
170
171    @Override
172    public void toggleItemSelection(Listitem item) {
173        super.toggleItemSelection(item);
174        //update minimum distances here
175    }
176
177    public void updateDistances() {
178        if (listEntries == null) {
179            return;
180        }
181
182        String fieldId;
183
184        for (ListEntry le : listEntries) {
185            //set to 2 for contextual and 'no association distance'
186            if (le.type.equalsIgnoreCase("contextual")
187                    || le.layerObject == null
188                    || !le.layerObject.containsKey("fields")
189                    || (fieldId = getFieldId(le.layerObject)) == null
190                    || CommonData.getDistancesMap().get(fieldId) == null) {
191                le.value = 2;
192            } else {
193                le.value = 1;
194            }
195        }
196
197        for (Object o : getSelectedItems()) {
198            ListEntry l = listEntries.get(((Listitem) o).getIndex());
199            l.value = 0;
200            String[] domain;
201            if (l.type.equalsIgnoreCase("environmental")
202                    && l.layerObject != null && l.layerObject.containsKey("fields")
203                    && (fieldId = getFieldId(l.layerObject)) != null
204                    && CommonData.getDistancesMap().get(fieldId) != null
205                    && (domain = getDomain(l.layerObject)) != null) {
206                for (ListEntry le : listEntries) {
207                    if (le.layerObject != null && le.layerObject.containsKey("fields")
208                            && (!singleDomain || isSameDomain(getDomain(le.layerObject), domain))) {
209                        String fieldId2 = getFieldId(le.layerObject);
210
211                        Double d = CommonData.getDistancesMap().get(fieldId).get(fieldId2);
212                        if (d != null) {
213                            le.value = (float) Math.min(le.value, d.doubleValue());
214                        }
215                    }
216                }
217            }
218        }
219
220        for (int i = 0; i < listEntries.size(); i++) {
221            float value = listEntries.get(i).value;
222            String type = listEntries.get(i).type;
223            Listcell lc = (Listcell) (getItemAtIndex(i).getLastChild());
224            if (type.equalsIgnoreCase("environmental")) {
225                if (threasholds[0] > value) {
226                    lc.setSclass("lcRed");//setStyle("background: #bb2222;");
227                } else if (threasholds[1] > value) {
228                    lc.setSclass("lcYellow");//lc.setStyle("background: #ffff22;");
229                } else if (1 >= value) {
230                    lc.setSclass("lcGreen");//lc.setStyle("background: #22aa22;");
231                } else {
232                    lc.setSclass("lcWhite");//lc.setStyle("background: #ffffff;");
233                }
234            }
235        }
236
237        forceDomain();
238    }
239
240    void forceDomain() {
241        String[] firstDomain = getFirstDomain();
242        String[] thisDomain;
243
244        if (!singleDomain || firstDomain == null) {
245            for (int i = 0; i < listEntries.size(); i++) {
246                boolean defaultDisable = disableContextualLayers && listEntries.get(i).type.equalsIgnoreCase("contextual");
247                getItemAtIndex(i).setDisabled(defaultDisable);
248            }
249            return;
250        }
251
252        for (int i = 0; i < listEntries.size(); i++) {
253            ListEntry l = listEntries.get(i);
254            if (l.layerObject != null
255                    && (thisDomain = getDomain(l.layerObject)) != null) {
256                boolean defaultDisable = disableContextualLayers && listEntries.get(i).type.equalsIgnoreCase("contextual");
257                boolean match = false;
258                for (String d1 : firstDomain) {
259                    for (String d2 : thisDomain) {
260                        if (d1.equalsIgnoreCase(d2)) {
261                            match = true;
262                        }
263                    }
264                }
265                getItemAtIndex(i).setDisabled(defaultDisable || !match);
266                if (!match && getItemAtIndex(i).isSelected()) {
267                    toggleItemSelection(getItemAtIndex(i));
268                }
269            }
270        }
271    }
272
273    String[] getFirstDomain() {
274        String[] domain = null;
275
276        for (Object o : getSelectedItems()) {
277            ListEntry l = listEntries.get(((Listitem) o).getIndex());
278            if (l.type.equalsIgnoreCase("environmental")
279                    && l.layerObject != null) {
280                domain = getDomain(l.layerObject);
281                if (domain != null) {
282                    break;
283                }
284            }
285        }
286
287        return domain;
288    }
289
290    String[] getDomain(JSONObject layerObject) {
291        if (!layerObject.containsKey("domain")) {
292            return null;
293        }
294        String[] d = layerObject.getString("domain").split(",");
295        for (int i = 0; i < d.length; i++) {
296            d[i] = d[i].trim();
297        }
298        return d;
299    }
300
301    String getFieldId(JSONObject layerObject) {
302        String fieldId = null;
303        try {
304            JSONArray ja = (JSONArray) layerObject.get("fields");
305            for (int i = 0; i < ja.size(); i++) {
306                JSONObject jo = (JSONObject) ja.get(i);
307                if (true) { //jo.getString("analysis").equalsIgnoreCase("true")) {
308                    fieldId = jo.getString("id");
309                    break;
310                }
311            }
312        } catch (Exception e) {
313            e.printStackTrace();
314        }
315        return fieldId;
316    }
317
318    public void onSelect(Event event) {
319        updateDistances();
320    }
321
322    public String[] getSelectedLayers() {
323        Set selectedItems = getSelectedItems();
324        String[] selected = new String[selectedItems.size()];
325        int i = 0;
326        System.out.print("getSelectedLayers: ");
327        for (Object o : selectedItems) {
328            selected[i] = listEntries.get(((Listitem) o).getIndex()).name;
329            i++;
330            System.out.print(listEntries.get(((Listitem) o).getIndex()).displayname + ", " + listEntries.get(((Listitem) o).getIndex()).name);
331        }
332        System.out.println("");
333        return selected;
334    }
335
336    public void selectLayers(String[] layers) {
337        String[] firstDomain = getFirstDomain();
338
339        for (int i = 0; i < listEntries.size(); i++) {
340            for (int j = 0; j < layers.length; j++) {
341                if (listEntries.get(i).displayname.equalsIgnoreCase(layers[j])
342                        || listEntries.get(i).name.equalsIgnoreCase(layers[j])) {
343                    if (!getItemAtIndex(i).isSelected() && (!singleDomain || isSameDomain(firstDomain, getDomain(listEntries.get(i).layerObject)))) {
344                        toggleItemSelection(getItemAtIndex(i));
345                    }
346                    break;
347                }
348            }
349        }
350
351        updateDistances();
352    }
353
354    @Override
355    public void clearSelection() {
356        updateDistances();
357        super.clearSelection();
358    }
359
360    public boolean getIncludeAnalysisLayers() {
361        return includeAnalysisLayers;
362    }
363
364    private boolean isSameDomain(String[] domain1, String[] domain2) {
365        if (domain1 == null || domain2 == null) {
366            return true;
367        }
368
369        for (String s1 : domain1) {
370            for (String s2 : domain2) {
371                if (s1.equalsIgnoreCase(s2)) {
372                    return true;
373                }
374            }
375        }
376
377        return false;
378    }
379}