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