PageRenderTime 75ms CodeModel.GetById 22ms app.highlight 45ms RepoModel.GetById 2ms app.codeStats 0ms

/v3.2/nimbits-tds/src/com/nimbits/client/panels/DiagramPanel.java

http://nimbits-server.googlecode.com/
Java | 923 lines | 645 code | 186 blank | 92 comment | 128 complexity | 34c6211ddf7b1928de5a7f18e328fcd7 MD5 | raw file
  1/*
  2 * Copyright (c) 2010 Tonic Solutions LLC.
  3 *
  4 * http://www.nimbits.com
  5 *
  6 *
  7 * Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  8 *
  9 * http://www.gnu.org/licenses/gpl.html
 10 *
 11 * Unless required by applicable law or agreed to in writing, software distributed under the license is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 12 */
 13
 14package com.nimbits.client.panels;
 15
 16import com.extjs.gxt.ui.client.event.IconButtonEvent;
 17import com.extjs.gxt.ui.client.event.SelectionListener;
 18import com.extjs.gxt.ui.client.util.DateWrapper;
 19import com.extjs.gxt.ui.client.widget.ContentPanel;
 20import com.extjs.gxt.ui.client.widget.Window;
 21import com.extjs.gxt.ui.client.widget.button.ToolButton;
 22import com.google.gwt.core.client.GWT;
 23import com.google.gwt.dom.client.Style;
 24import com.google.gwt.event.dom.client.*;
 25import com.google.gwt.http.client.*;
 26import com.google.gwt.user.client.Timer;
 27import com.google.gwt.user.client.rpc.AsyncCallback;
 28import com.google.gwt.user.client.ui.FlowPanel;
 29import com.nimbits.client.exception.NimbitsException;
 30import com.nimbits.client.model.ClientType;
 31import com.nimbits.client.model.Const;
 32import com.nimbits.client.model.common.CommonFactoryLocator;
 33import com.nimbits.client.model.diagram.Diagram;
 34import com.nimbits.client.model.diagram.DiagramModel;
 35import com.nimbits.client.model.diagram.DiagramName;
 36import com.nimbits.client.model.point.Point;
 37import com.nimbits.client.model.point.PointModel;
 38import com.nimbits.client.model.point.PointName;
 39import com.nimbits.client.model.value.Value;
 40import com.nimbits.client.service.datapoints.PointService;
 41import com.nimbits.client.service.datapoints.PointServiceAsync;
 42import com.nimbits.client.service.diagram.DiagramService;
 43import com.nimbits.client.service.diagram.DiagramServiceAsync;
 44import com.nimbits.client.service.recordedvalues.RecordedValueService;
 45import com.nimbits.client.service.recordedvalues.RecordedValueServiceAsync;
 46import com.nimbits.shared.Utils;
 47import org.vectomatic.dom.svg.*;
 48import org.vectomatic.dom.svg.ui.SVGImage;
 49import org.vectomatic.dom.svg.utils.OMSVGParser;
 50import org.vectomatic.dom.svg.utils.SVGConstants;
 51
 52import java.util.HashMap;
 53import java.util.HashSet;
 54import java.util.Map;
 55import java.util.Set;
 56
 57
 58/**
 59 * Created by bsautner
 60 * User: benjamin
 61 * Date: 4/27/11
 62 * Time: 4:34 PM
 63 */
 64public class DiagramPanel extends NavigationEventProvider {
 65    //1.0
 66
 67
 68    // private static final Icons ICONS = GWT.create(Icons.class);
 69    private SVGImage image;
 70    private OMSVGSVGElement svg;
 71    private final ContentPanel mainPanel = new ContentPanel();
 72    private Map<PointName, Point> points = new HashMap<PointName, Point>();
 73    private Map<DiagramName, Diagram> diagrams = new HashMap<DiagramName, Diagram>();
 74
 75    private final boolean readOnly;
 76    private final RecordedValueServiceAsync recordedValueService = GWT.create(RecordedValueService.class);
 77    private final Set<PointName> pointsInDiagram = new HashSet<PointName>();
 78    private final Set<DiagramName> diagramsInDiagram = new HashSet<DiagramName>();
 79    private final Map<String, String> originalFill = new HashMap<String, String>();
 80
 81    public Diagram getDiagram() {
 82        return diagram;
 83    }
 84
 85    private final Diagram diagram;
 86
 87    private final ClientType clientType;
 88
 89    public DiagramPanel(final Diagram aDiagram, boolean showHeader, final int w, final int h) {
 90        //final ToolBar toolbar = createToolbar(aDiagram);
 91        final FlowPanel imagePanel = new FlowPanel();
 92        final String resourceUrl = Const.PATH_DIAGRAM_SERVICE + "?" + Const.PARAM_BLOB_KEY + "=" + aDiagram.getBlobKey();
 93        this.diagram = aDiagram;
 94        this.readOnly = aDiagram.isReadOnly();
 95        this.clientType = aDiagram.getClientType();
 96
 97        //  mainPanel.setTopComponent(toolbar);
 98
 99        mainPanel.setFrame(true);
100        mainPanel.add(imagePanel);
101
102
103        mainPanel.setHeaderVisible(showHeader);
104        mainPanel.getHeader().addTool(
105                maximizeToolbarButton());
106        mainPanel.getHeader().addTool(
107                closeToolbarButton());
108
109        add(mainPanel);
110
111
112        final RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, resourceUrl);
113        requestBuilder.setCallback(new RequestCallback() {
114
115
116            @Override
117            public void onResponseReceived(final Request request, final Response response) {
118                createSvg(response);
119                createRefreshTimer();
120            }
121
122            private void createSvg(final Response response) {
123                // final int adjH = h - 25;
124                svg = OMSVGParser.parse(response.getText());
125                svg.setAttribute("width", "100%");// String.valueOf(w));
126                svg.setAttribute("height", "100%");//String.valueOf(adjH));
127                image = new SVGImage(svg) {
128                    protected void onAttach() {
129                        super.onAttach();
130
131                        final OMSVGRect viewBox = svg.getViewBox().getBaseVal();
132                        if (viewBox.getWidth() == 0 || viewBox.getHeight() == 0) {
133                            final OMSVGRect bBox = svg.getBBox();
134                            bBox.assignTo(viewBox);
135                        }
136
137                        //  svg.getWidth().getBaseVal().newValueSpecifiedUnits(Style.Unit.PX, w);
138                        //  svg.getHeight().getBaseVal().newValueSpecifiedUnits(Style.Unit.PX, adjH);//- createToolbar.getSize().height);
139                        try {
140                            addHandlers();
141                            refreshDiagramValues();
142
143                        } catch (NimbitsException ignored) {
144
145
146                        }
147                        //refreshDiagramValues();
148
149
150                    }
151                };
152                imagePanel.add(image);
153                doLayout();
154            }
155
156            @Override
157            public void onError(Request request, Throwable throwable) {
158
159            }
160        });
161
162        try {
163            requestBuilder.send();
164        } catch (RequestException ignored) {
165
166        }
167    }
168
169    private ToolButton maximizeToolbarButton() {
170        return new ToolButton("x-tool-maximize",
171                new SelectionListener<IconButtonEvent>() {
172                    boolean isMax;
173
174                    @Override
175                    public void componentSelected(final IconButtonEvent ce) {
176                        final Window window = new Window();
177
178                        final DiagramPanel panel = new DiagramPanel(getDiagram(), false, 800, 800);
179
180
181                        window.add(panel);
182                        window.setWidth(800);
183                        window.setHeight(800);
184
185                        window.show();
186
187
188                    }
189                });
190    }
191
192    private ToolButton closeToolbarButton() {
193        return new ToolButton("x-tool-close",
194                new SelectionListener<IconButtonEvent>() {
195                    boolean isMax;
196
197                    @Override
198                    public void componentSelected(final IconButtonEvent ce) {
199                        notifyDiagramRemovedListener(getDiagram());
200                    }
201                });
202    }
203//    public void setDiagram(final Diagram aDiagram) {
204//
205//    }
206
207    private void createRefreshTimer() {
208        Timer updater = new Timer() {
209            @Override
210            public void run() {
211                try {
212                    refreshDiagramValues();
213                } catch (NimbitsException e) {
214                    GWT.log(e.getMessage());
215                }
216            }
217        };
218
219        updater.scheduleRepeating(Const.DEFAULT_TIMER_UPDATE_SPEED);
220        updater.run();
221    }
222
223//    private ToolBar createToolbar(final Diagram diagram) {
224//        ToolBar toolBar = new ToolBar();
225//        Button refresh = new Button();
226//
227//        refresh.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.refresh2()));
228//
229//        refresh.addListener(Events.OnClick, new Listener<BaseEvent>() {
230//            @Override
231//            public void handleEvent(BaseEvent be) {
232//                try {
233//                    refreshDiagramValues();
234//                } catch (NimbitsException e) {
235//
236//                }
237//
238//            }
239//        });
240//
241//        Button props = new Button();
242//
243//        props.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.edit()));
244//
245//        props.addListener(Events.OnClick, new Listener<BaseEvent>() {
246//            @Override
247//            public void handleEvent(BaseEvent be) {
248//                DiagramPropertyPanel dp = new DiagramPropertyPanel(diagram, readOnly);
249//                final Window w = new Window();
250//                w.setWidth(500);
251//                w.setHeight(400);
252//                w.setHeading(diagram.getName() + " Properties");
253//                w.add(dp);
254//                w.show();
255//                dp.addDiagramDeletedListeners(new DiagramDeletedListener() {
256//
257//
258//                    @Override
259//                    public void onDiagramDeleted(Diagram c, boolean readOnly) throws NimbitsException {
260//                        w.hide();
261//                        notifyDiagramDeletedListener(c, readOnly);
262//                    }
263//                });
264//
265//
266//            }
267//        });
268//
269//        toolBar.add(new SeparatorToolItem());
270//        toolBar.add(refresh);
271//        toolBar.add(props);
272//
273//        return toolBar;
274//
275//    }
276
277    private void addHandler(final OMNode node) {
278
279        if (node instanceof OMSVGTextElement) {
280            processOMSVGTextElement((OMSVGTextElement) node);
281        } else if (node instanceof OMSVGRectElement) {
282            processOMSVGRectElement((OMSVGRectElement) node);
283        } else if (node instanceof OMSVGPathElement) {
284            processOMSVGPathElement((OMSVGPathElement) node);
285        }
286
287
288        if (node.hasChildNodes()) {
289            for (OMNode c : node.getChildNodes()) {
290                addHandler(c);
291            }
292        }
293
294    }
295
296    private void addHandlers() throws NimbitsException {
297
298        for (OMNode node : svg.getChildNodes()) {
299            addHandler(node);
300
301        }
302        getPointsUsedInDiagram();
303
304        getDiagramsUsedInDiagram();
305
306    }
307
308    private void getDiagramsUsedInDiagram() throws NimbitsException {
309        final DiagramServiceAsync diagramServiceAsync = GWT.create(DiagramService.class);
310        diagramServiceAsync.getDiagramsByName(diagram.getUserFk(), diagramsInDiagram, new AsyncCallback<Map<DiagramName, Diagram>>() {
311
312
313            @Override
314            public void onFailure(Throwable throwable) {
315
316            }
317
318            @Override
319            public void onSuccess(Map<DiagramName, Diagram> diagramNameDiagramMap) {
320                diagrams = diagramNameDiagramMap;
321            }
322        });
323    }
324
325    private void getPointsUsedInDiagram() throws NimbitsException {
326        final PointServiceAsync pointServiceAsync = GWT.create(PointService.class);
327
328        pointServiceAsync.getPointsByName(diagram.getUserFk(), pointsInDiagram, new AsyncCallback<Map<PointName, Point>>() {
329
330
331            @Override
332            public void onFailure(Throwable throwable) {
333
334            }
335
336            @Override
337            public void onSuccess(Map<PointName, Point> stringPointMap) {
338                points = stringPointMap;
339
340            }
341        });
342    }
343
344    private void refreshDiagramValues() throws NimbitsException {
345
346        for (final OMNode node : svg.getChildNodes()) {
347            refreshNodeValue(node);
348
349        }
350
351
352    }
353
354    private void refreshNodeValue(final OMNode node) throws NimbitsException {
355        if (node != null) {
356
357            if (node instanceof OMSVGTextElement) {
358                updateTextBoxValue((OMSVGTextElement) node);
359            } else if (node instanceof OMSVGPathElement) {
360                updatePathValue((OMSVGPathElement) node);
361            } else if (node instanceof OMSVGRectElement) {
362                updateRectValue((OMSVGRectElement) node);
363            }
364
365
366            if (node.hasChildNodes()) {
367                try {
368                    for (OMNode c : node.getChildNodes()) {
369                        refreshNodeValue(c);
370                    }
371                } catch (NimbitsException e) {
372                    GWT.log(e.getMessage(), e);
373                }
374            }
375
376        }
377
378    }
379
380    public void resizePanel(final int defaultHeight, final int defaultWidth) {
381        mainPanel.setWidth(defaultWidth);
382        mainPanel.setHeight(defaultHeight);
383        svg.getWidth().getBaseVal().newValueSpecifiedUnits(Style.Unit.PX, defaultWidth);
384        svg.getHeight().getBaseVal().newValueSpecifiedUnits(Style.Unit.PX, defaultHeight - 25);//- createToolbar.getSize().height);
385        //To change body of created methods use File | Settings | File Templates.
386    }
387
388    //Node processing
389
390    //OMSVGTextElement
391
392    private void processOMSVGTextElement(final OMSVGTextElement o) {
393        final String pointNameParam = o.getAttribute(Const.PARAM_POINT);
394        final String action = o.getAttribute(Const.PARAM_ACTION);
395        final String diagramNameParam = o.getAttribute(Const.PARAM_DIAGRAM);
396        final String url = o.getAttribute(Const.PARAM_URL);
397        if (!Utils.isEmptyString(action)) {
398            final String[] actions = action.split(",");
399            if (!Utils.isEmptyString(pointNameParam)) {
400                PointName pointName = CommonFactoryLocator.getInstance().createPointName(pointNameParam);
401                if (!pointsInDiagram.contains(pointName)) {
402
403                    pointsInDiagram.add(pointName);
404                }
405                addTextPointActions(o, pointName, actions);
406            }
407            if (!Utils.isEmptyString(diagramNameParam)) {
408                DiagramName diagramName = CommonFactoryLocator.getInstance().createDiagramName(diagramNameParam);
409                if (!diagramsInDiagram.contains(diagramName)) {
410                    diagramsInDiagram.add(diagramName);
411                }
412                addNestedTextDiagramActions(o, diagramName, actions);
413            }
414            if (!Utils.isEmptyString(url)) {
415                addNestedTextUrlActions(o, url, actions);
416            }
417        }
418
419
420    }
421
422    private void addTextPointActions(final OMSVGTextElement t, final PointName pointName, final String[] actions) {
423        originalFill.put(t.getId(), t.getStyle().getSVGProperty(SVGConstants.CSS_FILL_VALUE));
424        // com.google.gwt.user.client.Window.alert("Adding handler " + t.getId());
425        try {
426            t.addMouseDownHandler(new MouseDownHandler() {
427
428                @Override
429                public void onMouseDown(final MouseDownEvent mouseDownEvent) {
430                    //   com.google.gwt.user.client.Window.alert("1");
431                    if (points.containsKey(pointName)) {
432                        //  com.google.gwt.user.client.Window.alert(pointName);
433                        PointModel p = (PointModel) points.get(pointName);
434                        p.setReadOnly(readOnly);
435                        p.setClientType(clientType);
436                        notifyPointClickedListener(p);
437
438                    }
439                }
440            });
441        } catch (Exception e) {
442            com.google.gwt.user.client.Window.alert(e.getMessage());
443        }
444        t.addMouseOverHandler(new MouseOverHandler() {
445            @Override
446            public void onMouseOver(MouseOverEvent mouseOverEvent) {
447                t.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
448                        SVGConstants.CSS_YELLOW_VALUE);
449
450            }
451        });
452        t.addMouseOutHandler(new MouseOutHandler() {
453            @Override
454            public void onMouseOut(MouseOutEvent mouseOutEvent) {
455
456                t.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
457                        originalFill.get(t.getId()));
458                //To change body of implemented methods use File | Settings | File Templates.
459            }
460        });
461
462    }
463
464    private void addNestedTextDiagramActions(final OMSVGTextElement t, final DiagramName diagramName, final String[] actions) {
465        t.addMouseDownHandler(new MouseDownHandler() {
466            @Override
467            public void onMouseDown(MouseDownEvent mouseDownEvent) {
468                if (diagrams.containsKey(diagramName)) {
469                    DiagramModel diagramModel = (DiagramModel) diagrams.get(diagramName);
470                    diagramModel.setClientType(clientType);
471                    notifyDiagramClickedListener(diagramModel);
472
473                }
474            }
475        });
476
477
478    }
479
480    private void addNestedTextUrlActions(final OMSVGTextElement t, final String url, final String[] actions) {
481        t.addMouseDownHandler(new MouseDownHandler() {
482            @Override
483            public void onMouseDown(MouseDownEvent mouseDownEvent) {
484
485                String target;
486                if (actions.length > 0) {
487                    target = actions[0];
488                } else {
489                    target = Const.PARAM_SELF;
490                }
491
492                notifyUrlClickedListener(url, target);
493
494            }
495
496        });
497
498
499    }
500
501    private void updateTextBoxValue(final OMSVGTextElement o) throws NimbitsException {
502        final String pointNameParam = o.getAttribute(Const.PARAM_POINT);
503        final String action = o.getAttribute(Const.PARAM_ACTION);
504
505        if (!Utils.isEmptyString(action)) {
506            final String[] actions = action.split(",");
507            final PointName pointName = CommonFactoryLocator.getInstance().createPointName(pointNameParam);
508            if (!Utils.isEmptyString(pointNameParam) && points.containsKey(pointName)) {
509                recordedValueService.getCurrentValue(diagram.getUserFk(), pointName, new AsyncCallback<Value>() {
510
511                    @Override
512                    public void onFailure(Throwable throwable) {
513
514                    }
515
516                    @Override
517                    public void onSuccess(Value result) {
518                        applyValueToTextNode(result, pointName, actions, o);
519                    }
520                });
521
522            }
523        }
524    }
525
526    private void applyValueToTextNode(final Value result, final PointName pointName, final String[] actions, final OMSVGTextElement o) {
527        if (points != null) {
528
529            final Point p = points.get(pointName);
530
531            if (result != null) {
532
533
534                for (final String action : actions) {
535                    if (action.equals(Const.ACTION_VALUE)) {
536                        o.getElement().setInnerText(String.valueOf(Utils.roundDouble(result.getValue())));
537                    } else if (action.equals(Const.ACTION_ALERT)) {
538                        if (result.getValue() <= p.getLowAlarm() && p.isLowAlarmOn()) {
539                            o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
540                                    SVGConstants.CSS_BLUE_VALUE);
541                        } else if (result.getValue() >= p.getHighAlarm() && p.isHighAlarmOn()) {
542                            o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
543                                    SVGConstants.CSS_RED_VALUE);
544                        } else {
545                            o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
546                                    originalFill.get(o.getId()));
547                        }
548                    } else if (action.equals(Const.ACTION_IDLE) && p.isIdleAlarmOn()) {
549                        DateWrapper n = new DateWrapper();
550                        long last = result.getTimestamp().getTime();
551                        long current = n.getTime();
552                        long max = p.getIdleSeconds() * 1000;
553                        long elapsed = current - last;
554
555                        if (elapsed > max) {
556                            o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
557                                    SVGConstants.CSS_ORANGE_VALUE);
558                        }
559                    } else if (action.equals(Const.ACTION_ONOFF)) {
560                        if (result.getValue() == 0.0) {
561                            o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
562                                    SVGConstants.CSS_BLACK_VALUE);
563                        } else {
564                            o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
565                                    originalFill.get(o.getId()));
566                        }
567                    }
568
569                }
570            }
571        }
572    }
573
574    //PATH
575
576    private void processOMSVGPathElement(final OMSVGPathElement o) {
577        final String pointNameParam = o.getAttribute(Const.PARAM_POINT);
578        final String action = o.getAttribute(Const.PARAM_ACTION);
579        final String diagramNameParam = o.getAttribute(Const.PARAM_DIAGRAM);
580        final String url = o.getAttribute(Const.PARAM_URL);
581        if (!Utils.isEmptyString(action)) {
582            final String[] actions = action.split(",");
583            if (!Utils.isEmptyString(pointNameParam)) {
584                PointName pointName = CommonFactoryLocator.getInstance().createPointName(pointNameParam);
585                if (!pointsInDiagram.contains(pointName)) {
586                    pointsInDiagram.add(pointName);
587                }
588                addPathPointActions(o, pointName, actions);
589            }
590            if (!Utils.isEmptyString(diagramNameParam)) {
591                DiagramName diagramName = CommonFactoryLocator.getInstance().createDiagramName(diagramNameParam);
592                if (!diagramsInDiagram.contains(diagramName)) {
593                    diagramsInDiagram.add(diagramName);
594                }
595                addNestedPathDiagramActions(o, diagramName, actions);
596            }
597            if (!Utils.isEmptyString(url)) {
598                addPathUrlActions(o, url, actions);
599            }
600        }
601
602
603    }
604
605    private void addPathPointActions(final OMSVGPathElement t, final PointName pointName, final String[] actions) {
606        originalFill.put(t.getId(), t.getStyle().getSVGProperty(SVGConstants.CSS_FILL_VALUE));
607        t.addMouseDownHandler(new MouseDownHandler() {
608
609            @Override
610            public void onMouseDown(MouseDownEvent mouseDownEvent) {
611                if (points.containsKey(pointName)) {
612                    PointModel p = (PointModel) points.get(pointName);
613
614                    notifyPointClickedListener(p);
615                }
616            }
617        });
618        t.addMouseOverHandler(new MouseOverHandler() {
619            @Override
620            public void onMouseOver(MouseOverEvent mouseOverEvent) {
621                t.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
622                        SVGConstants.CSS_YELLOW_VALUE);
623
624            }
625        });
626        t.addMouseOutHandler(new MouseOutHandler() {
627            @Override
628            public void onMouseOut(MouseOutEvent mouseOutEvent) {
629
630                t.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
631                        originalFill.get(t.getId()));
632
633            }
634        });
635
636    }
637
638    private void addNestedPathDiagramActions(final OMSVGPathElement t, final DiagramName diagramName, final String[] actions) {
639        t.addMouseDownHandler(new MouseDownHandler() {
640            @Override
641            public void onMouseDown(final MouseDownEvent mouseDownEvent) {
642                if (diagrams.containsKey(diagramName)) {
643                    DiagramModel diagramModel = (DiagramModel) diagrams.get(diagramName);
644                    diagramModel.setClientType(clientType);
645                    notifyDiagramClickedListener(diagramModel);
646
647                }
648            }
649        });
650
651
652    }
653
654    private void updatePathValue(final OMSVGPathElement o) throws NimbitsException {
655        final String pointNameParam = o.getAttribute(Const.PARAM_POINT);
656        final String action = o.getAttribute(Const.PARAM_ACTION);
657
658        if (!Utils.isEmptyString(action)) {
659            final String[] actions = action.split(",");
660            final PointName pointName = CommonFactoryLocator.getInstance().createPointName(pointNameParam);
661            if (!Utils.isEmptyString(pointNameParam) && points.containsKey(pointName)) {
662
663                recordedValueService.getCurrentValue(diagram.getUserFk(), pointName, new AsyncCallback<Value>() {
664
665                    @Override
666                    public void onFailure(Throwable throwable) {
667                        GWT.log(throwable.getMessage(), throwable);
668                    }
669
670                    @Override
671                    public void onSuccess(Value result) {
672                        applyValueToPathNode(result, pointName, actions, o);
673                    }
674                });
675
676            }
677        }
678    }
679
680    private void applyValueToPathNode(final Value result, final PointName pointName, final String[] actions, final OMSVGPathElement o) {
681
682        if (points != null) {
683            final Point p = points.get(pointName);
684
685            if (result != null) {
686
687
688                for (String action : actions) {
689                    if (action.equals(Const.ACTION_VALUE)) {
690                        o.getElement().setInnerText(String.valueOf(result.getValue()));
691                    } else if (action.equals(Const.ACTION_ALERT)) {
692                        if (result.getValue() <= p.getLowAlarm() && p.isLowAlarmOn()) {
693                            o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
694                                    SVGConstants.CSS_RED_VALUE);
695                        } else if (result.getValue() >= p.getHighAlarm() && p.isHighAlarmOn()) {
696                            o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
697                                    SVGConstants.CSS_RED_VALUE);
698                        } else {
699                            o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
700                                    originalFill.get(o.getId()));
701                        }
702                    } else if (action.equals("idle") && p.isIdleAlarmOn()) {
703                        DateWrapper n = new DateWrapper();
704                        long last = result.getTimestamp().getTime();
705                        long current = n.getTime();
706                        long max = p.getIdleSeconds() * 1000;
707                        long elapsed = current - last;
708
709                        if (elapsed > max) {
710                            o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
711                                    SVGConstants.CSS_GRAY_VALUE);
712                        }
713                    } else if (action.equals(Const.ACTION_ONOFF)) {
714                        if (result.getValue() == 0.0) {
715                            o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
716                                    SVGConstants.CSS_BLACK_VALUE);
717                        } else {
718                            o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
719                                    originalFill.get(o.getId()));
720                        }
721                    }
722
723                }
724            }
725        }
726    }
727
728    private void addPathUrlActions(final OMSVGPathElement t, final String url, final String[] actions) {
729        t.addMouseDownHandler(new MouseDownHandler() {
730            @Override
731            public void onMouseDown(MouseDownEvent mouseDownEvent) {
732
733                String target;
734                if (actions.length > 0) {
735                    target = actions[0];
736                } else {
737                    target = Const.PARAM_SELF;
738                }
739
740                notifyUrlClickedListener(url, target);
741
742            }
743
744        });
745
746
747    }
748
749    //rect
750
751    private void processOMSVGRectElement(OMSVGRectElement o) {
752        final String pointNameParam = o.getAttribute(Const.PARAM_POINT);
753        final String action = o.getAttribute(Const.PARAM_ACTION);
754        final String diagramNameParam = o.getAttribute(Const.PARAM_DIAGRAM);
755        final String url = o.getAttribute(Const.PARAM_URL);
756        if (!Utils.isEmptyString(action)) {
757            final String[] actions = action.split(",");
758            if (!Utils.isEmptyString(pointNameParam)) {
759                final PointName pointName = CommonFactoryLocator.getInstance().createPointName(pointNameParam);
760                if (!pointsInDiagram.contains(pointName)) {
761                    pointsInDiagram.add(pointName);
762                }
763                addRectPointActions(o, pointName, actions);
764            }
765            if (!Utils.isEmptyString(diagramNameParam)) {
766                DiagramName diagramName = CommonFactoryLocator.getInstance().createDiagramName(diagramNameParam);
767                if (!diagramsInDiagram.contains(diagramName)) {
768
769                    diagramsInDiagram.add(diagramName);
770                }
771                addNestedRectDiagramActions(o, diagramName, actions);
772            }
773            if (!Utils.isEmptyString(url)) {
774                addRectUrlActions(o, url, actions);
775            }
776        }
777
778
779    }
780
781    private void addRectPointActions(final OMSVGRectElement t, final PointName pointName, final String[] actions) {
782        originalFill.put(t.getId(), t.getStyle().getSVGProperty(SVGConstants.CSS_FILL_VALUE));
783        t.addMouseDownHandler(new MouseDownHandler() {
784
785            @Override
786            public void onMouseDown(MouseDownEvent mouseDownEvent) {
787                if (points.containsKey(pointName)) {
788                    PointModel p = (PointModel) points.get(pointName);
789                    p.setReadOnly(readOnly);
790                    p.setClientType(clientType);
791                    notifyPointClickedListener(p);
792                }
793            }
794        });
795        t.addMouseOverHandler(new MouseOverHandler() {
796            @Override
797            public void onMouseOver(MouseOverEvent mouseOverEvent) {
798                t.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
799                        SVGConstants.CSS_YELLOW_VALUE);
800
801            }
802        });
803        t.addMouseOutHandler(new MouseOutHandler() {
804            @Override
805            public void onMouseOut(MouseOutEvent mouseOutEvent) {
806
807                t.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
808                        originalFill.get(t.getId()));
809                //To change body of implemented methods use File | Settings | File Templates.
810            }
811        });
812
813    }
814
815    private void addNestedRectDiagramActions(final OMSVGRectElement t, final DiagramName diagramName, final String[] actions) {
816        t.addMouseDownHandler(new MouseDownHandler() {
817            @Override
818            public void onMouseDown(MouseDownEvent mouseDownEvent) {
819                if (diagrams.containsKey(diagramName)) {
820                    DiagramModel diagramModel = (DiagramModel) diagrams.get(diagramName);
821                    diagramModel.setClientType(clientType);
822                    notifyDiagramClickedListener(diagramModel);
823
824                }
825            }
826        });
827
828
829    }
830
831    private void updateRectValue(final OMSVGRectElement o) throws NimbitsException {
832        final String pointNameParam = o.getAttribute(Const.PARAM_POINT);
833        final String action = o.getAttribute(Const.PARAM_ACTION);
834
835        if (!Utils.isEmptyString(action)) {
836            final String[] actions = action.split(",");
837            final PointName pointName = CommonFactoryLocator.getInstance().createPointName(pointNameParam);
838            if (!Utils.isEmptyString(pointNameParam) && points.containsKey(pointName)) {
839                recordedValueService.getCurrentValue(diagram.getUserFk(), pointName, new AsyncCallback<Value>() {
840
841                    @Override
842                    public void onFailure(Throwable throwable) {
843
844                    }
845
846                    @Override
847                    public void onSuccess(Value result) {
848                        applyValueToRectNode(result, pointName, actions, o);
849                    }
850                });
851
852            }
853        }
854    }
855
856    private void applyValueToRectNode(Value result, PointName pointName, String[] actions, OMSVGRectElement o) {
857        final Point p = points.get(pointName);
858
859        if (result != null) {
860
861
862            for (String action : actions) {
863                if (action.equals(Const.ACTION_VALUE)) {
864                    o.getElement().setInnerText(String.valueOf(result.getValue()));
865                } else if (action.equals(Const.ACTION_ALERT)) {
866                    if (result.getValue() <= p.getLowAlarm() && p.isLowAlarmOn()) {
867                        o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
868                                SVGConstants.CSS_RED_VALUE);
869                    } else if (result.getValue() >= p.getHighAlarm() && p.isHighAlarmOn()) {
870                        o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
871                                SVGConstants.CSS_RED_VALUE);
872                    } else {
873                        o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
874                                originalFill.get(o.getId()));
875                    }
876                } else if (action.equals(Const.PARAM_IDLE) && p.isIdleAlarmOn()) {
877                    DateWrapper n = new DateWrapper();
878                    long last = result.getTimestamp().getTime();
879                    long current = n.getTime();
880                    long max = p.getIdleSeconds() * 1000;
881                    long elapsed = current - last;
882
883                    if (elapsed > max) {
884                        o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
885                                SVGConstants.CSS_GRAY_VALUE);
886                    }
887                } else if (action.equals(Const.ACTION_ONOFF)) {
888                    if (result.getValue() == 0.0) {
889                        o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
890                                SVGConstants.CSS_BLACK_VALUE);
891                    } else {
892                        o.getStyle().setSVGProperty(SVGConstants.CSS_FILL_VALUE,
893                                originalFill.get(o.getId()));
894                    }
895                }
896
897            }
898        }
899    }
900
901    private void addRectUrlActions(final OMSVGRectElement t, final String url, final String[] actions) {
902        t.addMouseDownHandler(new MouseDownHandler() {
903            @Override
904            public void onMouseDown(MouseDownEvent mouseDownEvent) {
905
906                String target;
907                if (actions.length > 0) {
908                    target = actions[0];
909                } else {
910                    target = Const.PARAM_SELF;
911                }
912
913                notifyUrlClickedListener(url, target);
914
915            }
916
917        });
918
919
920    }
921
922
923}