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

http://nimbits-server.googlecode.com/ · Java · 988 lines · 826 code · 131 blank · 31 comment · 117 complexity · da1b10c22b0839d0f958cb23a542ac77 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.Style.Scroll;
  15. import com.extjs.gxt.ui.client.data.ModelData;
  16. import com.extjs.gxt.ui.client.data.ModelIconProvider;
  17. import com.extjs.gxt.ui.client.dnd.DND.Feedback;
  18. import com.extjs.gxt.ui.client.dnd.TreePanelDragSource;
  19. import com.extjs.gxt.ui.client.dnd.TreePanelDropTarget;
  20. import com.extjs.gxt.ui.client.event.*;
  21. import com.extjs.gxt.ui.client.store.TreeStore;
  22. import com.extjs.gxt.ui.client.util.Format;
  23. import com.extjs.gxt.ui.client.util.Params;
  24. import com.extjs.gxt.ui.client.widget.ContentPanel;
  25. import com.extjs.gxt.ui.client.widget.Info;
  26. import com.extjs.gxt.ui.client.widget.MessageBox;
  27. import com.extjs.gxt.ui.client.widget.Window;
  28. import com.extjs.gxt.ui.client.widget.button.Button;
  29. import com.extjs.gxt.ui.client.widget.menu.Menu;
  30. import com.extjs.gxt.ui.client.widget.menu.MenuItem;
  31. import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
  32. import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
  33. import com.google.gwt.core.client.GWT;
  34. import com.google.gwt.user.client.Timer;
  35. import com.google.gwt.user.client.rpc.AsyncCallback;
  36. import com.google.gwt.user.client.ui.AbstractImagePrototype;
  37. import com.nimbits.client.exception.NimbitsException;
  38. import com.nimbits.client.exceptions.PointExistsException;
  39. import com.nimbits.client.icons.Icons;
  40. import com.nimbits.client.model.*;
  41. import com.nimbits.client.model.category.Category;
  42. import com.nimbits.client.model.category.CategoryName;
  43. import com.nimbits.client.model.common.CommonFactoryLocator;
  44. import com.nimbits.client.model.diagram.Diagram;
  45. import com.nimbits.client.model.diagram.DiagramName;
  46. import com.nimbits.client.model.email.EmailAddress;
  47. import com.nimbits.client.model.point.Point;
  48. import com.nimbits.client.model.point.PointName;
  49. import com.nimbits.client.service.category.CategoryService;
  50. import com.nimbits.client.service.category.CategoryServiceAsync;
  51. import com.nimbits.client.service.datapoints.PointService;
  52. import com.nimbits.client.service.datapoints.PointServiceAsync;
  53. import com.nimbits.client.service.diagram.DiagramService;
  54. import com.nimbits.client.service.diagram.DiagramServiceAsync;
  55. import com.nimbits.shared.Utils;
  56. import java.util.HashMap;
  57. import java.util.List;
  58. import java.util.Map;
  59. class NavigationPanel extends NavigationEventProvider {
  60. private final ContentPanel mainPanel;
  61. private GxtDiagramModel diagramModelToDelete;
  62. private GxtPointCategoryModel categoryModelToDelete;
  63. private GxtPointModel pointModelToDelete;
  64. private final Map<PointName, Point> pointMap = new HashMap<PointName, Point>();
  65. private final Map<CategoryName, Category> categoryMap = new HashMap<CategoryName, Category>();
  66. private final Map<DiagramName, Diagram> diagramMap = new HashMap<DiagramName, Diagram>();
  67. private Point pointToBeCopied;
  68. private TreePanel<ModelData> tree;
  69. private TreeStore<ModelData> store;
  70. private final boolean isConnectionPanel;
  71. private final EmailAddress email;
  72. public NavigationPanel(final EmailAddress anEmailAddress, final boolean isConnection) {
  73. mainPanel = new ContentPanel();
  74. mainPanel.setHeaderVisible(false);
  75. mainPanel.setFrame(false);
  76. mainPanel.setBodyBorder(false);
  77. mainPanel.setHeight("100%");
  78. mainPanel.setScrollMode(Scroll.AUTOY);
  79. this.isConnectionPanel = isConnection;
  80. this.email = anEmailAddress;
  81. try {
  82. if (isConnectionPanel) {
  83. mainPanel.setHeight(600);
  84. loadConnectionTree();
  85. } else {
  86. mainPanel.setTopComponent(treeToolBar());
  87. loadAuthTree();
  88. }
  89. } catch (NimbitsException e) {
  90. GWT.log(e.getMessage(), e);
  91. }
  92. }
  93. private ToolBar treeToolBar() {
  94. final ToolBar toolBar = new ToolBar();
  95. toolBar.add(addNewCategoryButton());
  96. toolBar.add(addNewPointButton());
  97. toolBar.add(addNewDiagramButton());
  98. return toolBar;
  99. }
  100. private Button addNewCategoryButton() {
  101. final Button newCategory = new Button();
  102. newCategory.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.category()));
  103. newCategory.setToolTip(Const.MESSAGE_ADD_CATEGORY);
  104. newCategory.addListener(Events.OnClick, new Listener<BaseEvent>() {
  105. @Override
  106. public void handleEvent(final BaseEvent be) {
  107. final MessageBox box = MessageBox.prompt(Const.MESSAGE_NEW_CATEGORY,
  108. Const.MESSAGE_NEW_CATEGORY_PROMPT);
  109. box.addCallback(new Listener<MessageBoxEvent>() {
  110. @Override
  111. public void handleEvent(final MessageBoxEvent be) {
  112. final String newCategoryName = be.getValue();
  113. final CategoryName categoryName = CommonFactoryLocator.getInstance().createCategoryName(newCategoryName);
  114. final CategoryServiceAsync categoryService = GWT.create(CategoryService.class);
  115. try {
  116. categoryService.addCategory(categoryName,
  117. new AsyncCallback<Category>() {
  118. @Override
  119. public void onFailure(Throwable caught) {
  120. Info.display(Const.WORD_ERROR,
  121. caught.getMessage());
  122. }
  123. @Override
  124. public void onSuccess(final Category c) {
  125. final GxtPointCategoryModel m = new GxtPointCategoryModel(c);// PointCategoryModelFactory.createPointCategoryModel(c);
  126. categoryMap.put(c.getName(), c);
  127. if (store != null) {
  128. store = tree.getStore();
  129. store.add(m, true);
  130. tree.setExpanded(m, true);
  131. final String v = Format.ellipse(
  132. newCategoryName, 80);
  133. Info.display(Const.WORD_SUCCESS,
  134. "New Category added: '{0}'",
  135. new Params(v));
  136. layout(true);
  137. try {
  138. if (!isConnectionPanel) {
  139. loadAuthTree();
  140. } else {
  141. loadConnectionTree();
  142. }
  143. } catch (NimbitsException ignored) {
  144. }
  145. } else {
  146. try {
  147. loadAuthTree();
  148. } catch (NimbitsException e) {
  149. Info.display(Const.WORD_ERROR, e.getMessage());
  150. }
  151. }
  152. }
  153. });
  154. } catch (NimbitsException e) {
  155. Info.display(Const.WORD_ERROR, e.getMessage());
  156. }
  157. }
  158. });
  159. }
  160. });
  161. return newCategory;
  162. }
  163. private void loadConnectionTree() throws NimbitsException {
  164. removeAll();
  165. //add(statusImage());
  166. final CategoryServiceAsync categoryService = GWT.create(CategoryService.class);
  167. categoryService.getConnectionCategories(true, true, email,
  168. new AsyncCallback<List<Category>>() {
  169. @Override
  170. public void onFailure(Throwable caught) {
  171. GWT.log(caught.getMessage(), caught);
  172. }
  173. @Override
  174. public void onSuccess(List<Category> result) {
  175. decideWhatViewToLoad(result);
  176. }
  177. });
  178. }
  179. private void decideWhatViewToLoad(final List<Category> result) {
  180. if (result.size() == 1) {
  181. if (result.get(0).getDiagrams().size() == 0 && result.get(0).getPoints().size() == 0) {
  182. showEmptyView();
  183. } else {
  184. createTree(result);
  185. }
  186. } else if (result.size() == 0) {
  187. showEmptyView();
  188. } else {
  189. createTree(result);
  190. }
  191. setVisible(true);
  192. add(mainPanel);
  193. doLayout();
  194. }
  195. private void showEmptyView() {
  196. if (this.isConnectionPanel) {
  197. mainPanel.addText(Const.RESPONSE_NO_POINTS);
  198. } else {
  199. mainPanel.setUrl(Const.PATH_WELCOME_URL);
  200. }
  201. store = null;
  202. }
  203. private void createTree(final List<Category> result) {
  204. store = new TreeStore<ModelData>();
  205. tree = new TreePanel<ModelData>(store) {
  206. @Override
  207. protected boolean hasChildren(ModelData model) {
  208. return model instanceof GxtPointCategoryModel || !(model instanceof GxtPointModel) && super.hasChildren(model);
  209. }
  210. };
  211. treePropertyBuilder();
  212. treeStoreBuilder(result);
  213. setBorders(false);
  214. setScrollMode(Scroll.AUTOY);
  215. treeDNDBuilder();
  216. final TreePanelDropTarget target = new TreePanelDropTarget(tree);
  217. target.setAllowSelfAsSource(!isConnectionPanel);
  218. target.setFeedback(Feedback.BOTH);
  219. mainPanel.removeAll();
  220. if (this.email != null && isConnectionPanel) {
  221. mainPanel.addText(email.getValue());
  222. }
  223. mainPanel.add(tree);
  224. doLayout(true);
  225. }
  226. private void treePropertyBuilder() {
  227. if (!isConnectionPanel) {
  228. tree.setContextMenu(createContextMenu());
  229. }
  230. tree.setStateful(true);
  231. tree.setDisplayProperty(Const.PARAM_NAME);
  232. tree.setTrackMouseOver(true);
  233. tree.setHeight("100%");
  234. tree.setIconProvider(new ModelIconProvider<ModelData>() {
  235. @Override
  236. public AbstractImagePrototype getIcon(ModelData model) {
  237. // if (model.getInstance("icon") != null) {
  238. if (model instanceof GxtPointCategoryModel) {
  239. return AbstractImagePrototype.create(Icons.INSTANCE.category());
  240. } else if (model instanceof GxtPointModel) {
  241. switch (((GxtPointModel) model).getAlertState()) {
  242. case IdleAlert:
  243. return AbstractImagePrototype.create(Icons.INSTANCE.point_idle());
  244. case HighAlert:
  245. return AbstractImagePrototype.create(Icons.INSTANCE.point_high());
  246. case LowAlert:
  247. return AbstractImagePrototype.create(Icons.INSTANCE.point_low());
  248. default:
  249. return AbstractImagePrototype.create(Icons.INSTANCE.point_ok());
  250. }
  251. } else if (model instanceof GxtDiagramModel) {
  252. return AbstractImagePrototype.create(Icons.INSTANCE.diagram());
  253. } else {
  254. return null;
  255. }
  256. // return IconHelper.createStyle((String)
  257. // model.getInstance("icon"));
  258. // } else {
  259. //// return null;
  260. // }
  261. }
  262. });
  263. tree.addListener(Events.OnDoubleClick,
  264. new Listener<TreePanelEvent<ModelData>>() {
  265. @Override
  266. public void handleEvent(TreePanelEvent<ModelData> be) {
  267. ModelData selectedFolder = tree.getSelectionModel()
  268. .getSelectedItem();
  269. if (selectedFolder != null) {
  270. // String icon = selectedFolder.getInstance("icon");
  271. if (selectedFolder instanceof GxtPointCategoryModel) {
  272. try {
  273. Category category = categoryMap.get(((GxtPointCategoryModel) selectedFolder).getName());
  274. notifyCategoryClickedListener(category, isConnectionPanel);
  275. } catch (NimbitsException e) {
  276. GWT.log(e.getMessage(), e);
  277. }
  278. } else if (selectedFolder instanceof GxtPointModel) {
  279. Point point = pointMap.get(((GxtPointModel) selectedFolder).getName());
  280. point.setReadOnly(isConnectionPanel);
  281. point.setClientType(ClientType.other);
  282. notifyPointClickedListener(point);
  283. } else if (selectedFolder instanceof GxtDiagramModel) {
  284. Diagram diagram = diagramMap.get(((GxtDiagramModel) selectedFolder).getName());
  285. diagram.setClientType(ClientType.other);
  286. diagram.setReadOnly(isConnectionPanel);
  287. notifyDiagramClickedListener(diagram);
  288. }
  289. } else {
  290. try {
  291. loadAuthTree();
  292. } catch (NimbitsException ignored) {
  293. }
  294. }
  295. }
  296. });
  297. }
  298. private Menu createContextMenu() {
  299. Menu contextMenu = new Menu();
  300. contextMenu.add(propertyContext());
  301. contextMenu.add(copyContext());
  302. contextMenu.add(deleteContext());
  303. return contextMenu;
  304. }
  305. private MenuItem copyContext() {
  306. MenuItem retObj = new MenuItem();
  307. retObj.setText("Copy");
  308. retObj.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.album()));
  309. retObj.addSelectionListener(new SelectionListener<MenuEvent>() {
  310. public void componentSelected(MenuEvent ce) {
  311. ModelData selectedModel = tree.getSelectionModel().getSelectedItem();
  312. if (selectedModel.get(Const.PARAM_ICON).equals(Const.PARAM_POINT)) {
  313. GxtPointModel model = (GxtPointModel) selectedModel;
  314. pointToBeCopied = pointMap.get(model.getName());
  315. final MessageBox box = MessageBox.prompt(
  316. Const.MESSAGE_NEW_POINT,
  317. Const.MESSAGE_NEW_POINT_PROMPT);
  318. box.addCallback(copyPointListener());
  319. }
  320. }
  321. });
  322. return retObj;
  323. }
  324. private Listener<MessageBoxEvent> copyPointListener() {
  325. return new Listener<MessageBoxEvent>() {
  326. private String newPointName;
  327. @Override
  328. public void handleEvent(MessageBoxEvent be) {
  329. newPointName = be.getValue();
  330. if (!Utils.isEmptyString(newPointName)) {
  331. final MessageBox box = MessageBox.wait("Progress",
  332. "Creating your data point channel into the cloud", "Creating: " + newPointName);
  333. box.show();
  334. PointServiceAsync pointService = GWT.create(PointService.class);
  335. PointName pointName = CommonFactoryLocator.getInstance().createPointName(newPointName);
  336. pointService.copyPoint(pointToBeCopied, pointName,
  337. new AsyncCallback<Point>() {
  338. @Override
  339. public void onFailure(Throwable caught) {
  340. Info.display("Could not create "
  341. + newPointName,
  342. caught.getMessage());
  343. box.close();
  344. }
  345. @Override
  346. public void onSuccess(Point result) {
  347. try {
  348. reloadTree();
  349. } catch (NimbitsException e) {
  350. GWT.log(e.getMessage(), e);
  351. }
  352. box.close();
  353. }
  354. });
  355. }
  356. }
  357. };
  358. }
  359. private void reloadTree() throws NimbitsException {
  360. if (isConnectionPanel) {
  361. try {
  362. loadConnectionTree();
  363. } catch (NimbitsException ignored) {
  364. }
  365. } else {
  366. loadAuthTree();
  367. }
  368. }
  369. private MenuItem deleteContext() {
  370. MenuItem retObj = new MenuItem();
  371. retObj.setText("Delete");
  372. retObj.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.delete()));
  373. retObj.addSelectionListener(new SelectionListener<MenuEvent>() {
  374. public void componentSelected(MenuEvent ce) {
  375. ModelData selectedModel = tree.getSelectionModel().getSelectedItem();
  376. if (selectedModel.get(Const.PARAM_ICON).equals(Const.PARAM_CATEGORY)) {
  377. categoryModelToDelete = (GxtPointCategoryModel) selectedModel;
  378. MessageBox.confirm("Confirm", "Are you sure you want delete this category? Doing so will permanently delete all of the points in this category along with their date"
  379. , deleteCategoryListener);
  380. } else if (selectedModel.get(Const.PARAM_ICON).equals(Const.PARAM_POINT)) {
  381. pointModelToDelete = (GxtPointModel) selectedModel;
  382. MessageBox.confirm("Confirm", "Are you sure you want delete this Point? Doing so will permanently delete all of its historical data"
  383. , deletePointListener);
  384. } else if (selectedModel.get(Const.PARAM_ICON).equals(Const.PARAM_DIAGRAM)) {
  385. diagramModelToDelete = (GxtDiagramModel) selectedModel;
  386. MessageBox.confirm("Confirm", "Are you sure you want delete this Diagram?"
  387. , deleteDiagramListener);
  388. }
  389. }
  390. });
  391. return retObj;
  392. }
  393. private MenuItem propertyContext() {
  394. MenuItem retObj = new MenuItem();
  395. retObj.setText(Const.WORD_PROPERTIES);
  396. retObj.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.edit()));
  397. retObj.addSelectionListener(new SelectionListener<MenuEvent>() {
  398. public void componentSelected(MenuEvent ce) {
  399. ModelData selectedModel = tree.getSelectionModel().getSelectedItem();
  400. if (selectedModel.get(Const.PARAM_ICON).equals(Const.PARAM_CATEGORY)) {
  401. } else if (selectedModel.get(Const.PARAM_ICON).equals(Const.PARAM_POINT)) {
  402. GxtPointModel model = ((GxtPointModel) selectedModel);
  403. Point p = pointMap.get(model.getName());
  404. try {
  405. createPointPropertyWindow(p);
  406. } catch (NimbitsException e) {
  407. GWT.log(e.getMessage());
  408. }
  409. } else if (selectedModel.get(Const.PARAM_ICON).equals(Const.PARAM_DIAGRAM)) {
  410. GxtDiagramModel diagramModel = (GxtDiagramModel) selectedModel;
  411. Diagram diagram = diagramMap.get(diagramModel.getName());
  412. DiagramPropertyPanel dp = new DiagramPropertyPanel(diagram, diagram.isReadOnly());
  413. final Window w = new Window();
  414. w.setWidth(500);
  415. w.setHeight(400);
  416. w.setHeading(diagram.getName() + " " + Const.WORD_PROPERTIES);
  417. w.add(dp);
  418. w.show();
  419. }
  420. }
  421. });
  422. return retObj;
  423. }
  424. private void createPointPropertyWindow(Point p) throws NimbitsException {
  425. final com.extjs.gxt.ui.client.widget.Window window = new com.extjs.gxt.ui.client.widget.Window();
  426. final PointPanel pp = new PointPanel(p);
  427. pp.addPointUpdatedListeners(new PointPanel.PointUpdatedListener() {
  428. @Override
  429. public void onPointUpdated(Point p) {
  430. pointMap.remove(p.getName());
  431. pointMap.put(p.getName(), p);
  432. // points.remove(p.getName());
  433. // points.put(p.getName(), p) ;
  434. // mp.setTopComponent(mainToolBar( points));
  435. }
  436. });
  437. pp.addPointDeletedListeners(new PointPanel.PointDeletedListener() {
  438. @Override
  439. public void onPointDeleted(Point p) {
  440. pointMap.remove(p.getName());
  441. }
  442. });
  443. window.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.connect()));
  444. window.setSize(466, 520);
  445. window.setPlain(false);
  446. window.setModal(true);
  447. window.setBlinkModal(true);
  448. window.setHeading(p.getName().getValue() + " Properties");
  449. window.setHeaderVisible(true);
  450. window.setBodyBorder(true);
  451. // window.setLayout(new FitLayout());
  452. window.add(pp);
  453. window.show();
  454. }
  455. private void treeStoreBuilder(final List<Category> result) {
  456. pointMap.clear();
  457. diagramMap.clear();
  458. categoryMap.clear();
  459. for (Category c : result) {
  460. GxtPointCategoryModel gxtPointCategoryModel = new GxtPointCategoryModel(c);// PointCategoryModelFactory.createPointCategoryModel(c);
  461. if (!categoryMap.containsKey(c.getName())) {
  462. categoryMap.put(c.getName(), c);
  463. }
  464. if (!c.getName().getValue().equals(Const.CONST_HIDDEN_CATEGORY)) {
  465. if (!(c.getPoints() == null)) {
  466. for (Point p : c.getPoints()) {
  467. if (!pointMap.containsKey(p.getName())) {
  468. pointMap.put(p.getName(), p);
  469. }
  470. p.setCatID(c.getId());
  471. gxtPointCategoryModel.add(new GxtPointModel(p));
  472. }
  473. }
  474. if (!(c.getDiagrams() == null)) {
  475. for (Diagram d : c.getDiagrams()) {
  476. if (!diagramMap.containsKey(d.getName())) {
  477. diagramMap.put(d.getName(), d);
  478. }
  479. d.setCategoryFk(c.getId());
  480. gxtPointCategoryModel.add(new GxtDiagramModel(d));
  481. }
  482. }
  483. store.add(gxtPointCategoryModel, true);
  484. }
  485. }
  486. for (Category c : result) {
  487. if (c.getName().getValue().equals(Const.CONST_HIDDEN_CATEGORY) && (c.getPoints() != null)) {
  488. for (Point p : c.getPoints()) {
  489. if (!pointMap.containsKey(p.getName())) {
  490. pointMap.put(p.getName(), p);
  491. p.setCatID(c.getId());
  492. store.add(new GxtPointModel(p), false);
  493. }
  494. // break;
  495. }
  496. if (c.getName().getValue().equals(Const.CONST_HIDDEN_CATEGORY) && (c.getDiagrams() != null)) {
  497. for (Diagram d : c.getDiagrams()) {
  498. if (!diagramMap.containsKey(d.getName())) {
  499. diagramMap.put(d.getName(), d);
  500. }
  501. d.setCategoryFk(c.getId());
  502. store.add(new GxtDiagramModel(d), false);
  503. }
  504. // break;
  505. }
  506. }
  507. }
  508. }
  509. private void treeDNDBuilder() {
  510. TreePanelDragSource source = new TreePanelDragSource(tree);
  511. source.addDNDListener(new DNDListener() {
  512. ModelData selectedModel;
  513. @Override
  514. public void dragStart(DNDEvent e) {
  515. super.dragStart(e);
  516. selectedModel = tree.getSelectionModel().getSelectedItem();
  517. if (selectedModel != null && selectedModel == tree.getStore().getRootItems().get(0)) {
  518. e.setCancelled(true);
  519. e.getStatus().setStatus(false);
  520. } else if (selectedModel != null) {
  521. if (selectedModel.get(Const.PARAM_ICON).equals(Const.PARAM_CATEGORY)) {
  522. e.setCancelled(true);
  523. e.getStatus().setStatus(false);
  524. }
  525. }
  526. }
  527. @Override
  528. public void dragDrop(final DNDEvent e) {
  529. super.dragDrop(e);
  530. if (!(e.getTarget().getInnerHTML().equals("&nbsp;")) && !isConnectionPanel) {
  531. if (selectedModel instanceof GxtPointModel) {
  532. final GxtPointModel gxtPointModel = (GxtPointModel) selectedModel;
  533. selectedModel.set(Const.PARAM_NAME, gxtPointModel.getName().getValue());
  534. PointServiceAsync pointService = GWT.create(PointService.class);
  535. try {
  536. CategoryName categoryName = CommonFactoryLocator.getInstance().createCategoryName(e.getTarget()
  537. .getInnerText());
  538. pointService.movePoint(gxtPointModel.getName(), categoryName, new AsyncCallback<Point>() {
  539. @Override
  540. public void onFailure(Throwable caught) {
  541. Info.display(Const.WORD_ERROR, caught.getMessage());
  542. }
  543. @Override
  544. public void onSuccess(Point result) {
  545. // System.out.println();
  546. Info.display("Point moved ", gxtPointModel.getName().getValue());
  547. }
  548. });
  549. } catch (NimbitsException e1) {
  550. Info.display(Const.WORD_ERROR, e1.getMessage());
  551. }
  552. } else if (selectedModel instanceof GxtDiagramModel) {
  553. final GxtDiagramModel gxtDiagramModel = (GxtDiagramModel) selectedModel;
  554. selectedModel.set(Const.PARAM_NAME, gxtDiagramModel.getName());
  555. DiagramServiceAsync diagramService = GWT.create(DiagramService.class);
  556. try {
  557. CategoryName categoryName = CommonFactoryLocator.getInstance().createCategoryName(e.getTarget().getInnerText());
  558. diagramService.moveDiagram(gxtDiagramModel.getName(), categoryName, new AsyncCallback<Void>() {
  559. @Override
  560. public void onFailure(Throwable throwable) {
  561. }
  562. @Override
  563. public void onSuccess(Void aVoid) {
  564. try {
  565. loadAuthTree();
  566. } catch (NimbitsException e1) {
  567. GWT.log(e1.getMessage(), e1);
  568. }
  569. }
  570. });
  571. } catch (NimbitsException caught) {
  572. Info.display(Const.WORD_ERROR, caught.getMessage());
  573. }
  574. }
  575. } else {
  576. // final GxtPointModel gxtPointModel = (GxtPointModel) selectedModel;
  577. // store.add(gxtPointModel, true);
  578. try {
  579. if (!isConnectionPanel) {
  580. loadAuthTree();
  581. } else {
  582. loadConnectionTree();
  583. }
  584. } catch (NimbitsException ignored) {
  585. }
  586. }
  587. }
  588. });
  589. }
  590. private void loadAuthTree() throws NimbitsException {
  591. removeAll();
  592. // add(statusImage());
  593. final CategoryServiceAsync categoryService = GWT.create(CategoryService.class);
  594. categoryService.getCategories(true, true, true,
  595. new AsyncCallback<List<Category>>() {
  596. @Override
  597. public void onFailure(Throwable caught) {
  598. GWT.log(caught.getMessage(), caught);
  599. }
  600. @Override
  601. public void onSuccess(List<Category> result) {
  602. decideWhatViewToLoad(result);
  603. }
  604. });
  605. }
  606. private Button addNewPointButton() {
  607. Button newPoint = new Button("");
  608. newPoint.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.point_ok()));
  609. newPoint.setToolTip(Const.MESSAGE_NEW_POINT);
  610. newPoint.addListener(Events.OnClick, new Listener<BaseEvent>() {
  611. @Override
  612. public void handleEvent(BaseEvent be) {
  613. final MessageBox box = MessageBox.prompt(
  614. Const.MESSAGE_NEW_POINT,
  615. Const.MESSAGE_NEW_POINT_PROMPT);
  616. box.addCallback(createNewPointListener());
  617. }
  618. private Listener<MessageBoxEvent> createNewPointListener() {
  619. return new Listener<MessageBoxEvent>() {
  620. private String newPointName;
  621. @Override
  622. public void handleEvent(MessageBoxEvent be) {
  623. newPointName = be.getValue();
  624. if (!Utils.isEmptyString(newPointName)) {
  625. final MessageBox box = MessageBox.wait("Progress",
  626. "Creating your data point channel into the cloud", "Creating: " + newPointName);
  627. box.show();
  628. PointServiceAsync pointService = GWT.create(PointService.class);
  629. PointName pointName = CommonFactoryLocator.getInstance().createPointName(newPointName);
  630. try {
  631. pointService.addPoint(pointName, new AsyncCallback<Point>() {
  632. @Override
  633. public void onFailure(Throwable caught) {
  634. Info.display("Could not create "
  635. + newPointName,
  636. caught.getMessage());
  637. box.close();
  638. }
  639. @Override
  640. public void onSuccess(Point result) {
  641. addNewlyCreatedPointToTree(result);
  642. box.close();
  643. }
  644. });
  645. } catch (NimbitsException e) {
  646. box.close();
  647. GWT.log(e.getMessage());
  648. } catch (PointExistsException e) {
  649. box.close();
  650. Info.display("Could not create "
  651. + newPointName,
  652. e.getMessage());
  653. }
  654. }
  655. }
  656. };
  657. }
  658. });
  659. return newPoint;
  660. }
  661. private void addNewlyCreatedPointToTree(final Point result) {
  662. pointMap.put(result.getName(), result);
  663. if (tree != null && tree.getStore() != null) {
  664. GxtPointModel model = new GxtPointModel(result);
  665. store = tree.getStore();
  666. store.add(model, true);
  667. tree.setExpanded(model, true);
  668. } else {
  669. try {
  670. loadAuthTree();
  671. } catch (NimbitsException e) {
  672. Info.display(Const.WORD_ERROR, e.getMessage());
  673. }
  674. }
  675. }
  676. private Button addNewDiagramButton() {
  677. Button newDiagram = new Button();
  678. newDiagram.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.diagram()));
  679. newDiagram.setToolTip("Add a new process diagram");
  680. newDiagram.addListener(Events.OnClick, addDiagramListener());
  681. return newDiagram;
  682. }
  683. private Listener<BaseEvent> addDiagramListener() {
  684. return new Listener<BaseEvent>() {
  685. @Override
  686. public void handleEvent(final BaseEvent be) {
  687. final Window w = new Window();
  688. w.setAutoWidth(true);
  689. w.setHeading(Const.MESSAGE_UPLOAD_SVG);
  690. DiagramUploadPanel p = new DiagramUploadPanel(DiagramUploadPanel.UploadType.newFile);
  691. p.addDiagramAddedListeners(new DiagramUploadPanel.DiagramAddedListener() {
  692. @Override
  693. public void onDiagramAdded() throws NimbitsException {
  694. w.hide();
  695. reloadTree();
  696. }
  697. });
  698. w.add(p);
  699. w.show();
  700. }
  701. };
  702. }
  703. @Override
  704. protected void afterRender() {
  705. super.afterRender();
  706. layout(true);
  707. }
  708. @Override
  709. protected void onAttach() {
  710. final Timer updater = new Timer() {
  711. @Override
  712. public void run() {
  713. try {
  714. updateValues();
  715. } catch (NimbitsException e) {
  716. GWT.log(e.getMessage(), e);
  717. }
  718. }
  719. };
  720. // updater.schedule(1000);
  721. updater.scheduleRepeating(Const.DEFAULT_TIMER_UPDATE_SPEED);
  722. updater.run();
  723. super.onAttach();
  724. }
  725. private void updateValues() throws NimbitsException {
  726. if (tree != null) {
  727. final CategoryServiceAsync service = GWT.create(CategoryService.class);
  728. service.getCategories(true, false, true, new AsyncCallback<List<Category>>() {
  729. @Override
  730. public void onFailure(final Throwable throwable) {
  731. GWT.log(throwable.getMessage(), throwable);
  732. }
  733. @Override
  734. public void onSuccess(final List<Category> categories) {
  735. final TreeStore<ModelData> models = tree.getStore();
  736. final HashMap<PointName, Point> pointHashMap = new HashMap<PointName, Point>();
  737. for (final Category c : categories) {
  738. if (c.getPoints() != null) {
  739. for (final Point p : c.getPoints()) {
  740. if (p.isHighAlarmOn() || p.isLowAlarmOn() || p.isIdleAlarmOn()) {
  741. pointHashMap.put(p.getName(), p);
  742. }
  743. }
  744. }
  745. }
  746. for (final ModelData m : models.getAllItems()) {
  747. if (m instanceof GxtPointModel) {
  748. final GxtPointModel gxtPointModel = (GxtPointModel) m;
  749. if (pointHashMap.containsKey(((GxtPointModel) m).getName())) {
  750. gxtPointModel.setAlertState(pointHashMap.get(gxtPointModel.getName()).getAlertState());
  751. }
  752. models.update(m);
  753. }
  754. }
  755. }
  756. });
  757. }
  758. }
  759. public void addPoint(final Point p, final Category c) {
  760. store = tree.getStore();
  761. c.getPoints().add(p);
  762. GxtPointCategoryModel model = new GxtPointCategoryModel(c);
  763. GxtPointModel pModel = new GxtPointModel(p);
  764. store.remove(model);
  765. model.add(pModel);
  766. store.add(model, true);
  767. if (!pointMap.containsKey(p.getName())) {
  768. pointMap.put(p.getName(), p);
  769. }
  770. if (!categoryMap.containsKey(c.getName())) {
  771. categoryMap.put(c.getName(), c);
  772. }
  773. tree.setExpanded(model, true);
  774. }
  775. private final Listener<MessageBoxEvent> deleteCategoryListener = new Listener<MessageBoxEvent>() {
  776. public void handleEvent(MessageBoxEvent ce) {
  777. Button btn = ce.getButtonClicked();
  778. final CategoryServiceAsync categoryService = GWT.create(CategoryService.class);
  779. if (btn.getText().equals(Const.WORD_YES)) {
  780. Category categoryToDelete = categoryMap.get(categoryModelToDelete.getName());
  781. categoryService.deleteCategory(categoryToDelete, new AsyncCallback<Void>() {
  782. @Override
  783. public void onFailure(Throwable e) {
  784. GWT.log(e.getMessage(), e);
  785. }
  786. @Override
  787. public void onSuccess(Void aVoid) {
  788. for (ModelData modelData : categoryModelToDelete.getChildren()) {
  789. GxtPointModel pointModel = (GxtPointModel) modelData;
  790. Point point = pointMap.get(pointModel.getName());
  791. try {
  792. notifyPointDeletedListener(point);
  793. } catch (NimbitsException e) {
  794. GWT.log(e.getMessage(), e);
  795. }
  796. }
  797. tree.getStore().remove(categoryModelToDelete);
  798. }
  799. });
  800. }
  801. }
  802. };
  803. private final Listener<MessageBoxEvent> deletePointListener = new Listener<MessageBoxEvent>() {
  804. public void handleEvent(MessageBoxEvent ce) {
  805. Button btn = ce.getButtonClicked();
  806. final PointServiceAsync service = GWT.create(PointService.class);
  807. if (btn.getText().equals(Const.WORD_YES)) {
  808. final Point pointToDelete = pointMap.get(pointModelToDelete.getName());
  809. try {
  810. service.deletePoint(pointToDelete, new AsyncCallback<Void>() {
  811. @Override
  812. public void onFailure(Throwable e) {
  813. GWT.log(e.getMessage(), e);
  814. }
  815. @Override
  816. public void onSuccess(Void aVoid) {
  817. try {
  818. notifyPointDeletedListener(pointToDelete);
  819. GWT.log("Deleted " + pointToDelete.getName().getValue());
  820. } catch (NimbitsException e) {
  821. GWT.log(e.getMessage(), e);
  822. }
  823. tree.getStore().remove(pointModelToDelete);
  824. }
  825. });
  826. } catch (NimbitsException e) {
  827. GWT.log(e.getMessage(), e);
  828. }
  829. }
  830. }
  831. };
  832. private final Listener<MessageBoxEvent> deleteDiagramListener = new Listener<MessageBoxEvent>() {
  833. public void handleEvent(MessageBoxEvent ce) {
  834. Button btn = ce.getButtonClicked();
  835. final DiagramServiceAsync service = GWT.create(DiagramService.class);
  836. if (btn.getText().equals(Const.WORD_YES)) {
  837. final Diagram diagramToDelete = diagramMap.get(diagramModelToDelete.getName());
  838. service.deleteDiagram(diagramToDelete, new AsyncCallback<Void>() {
  839. @Override
  840. public void onFailure(Throwable e) {
  841. GWT.log(e.getMessage(), e);
  842. }
  843. @Override
  844. public void onSuccess(Void aVoid) {
  845. try {
  846. notifyDiagramDeletedListener(diagramToDelete, false);
  847. GWT.log("Deleted " + diagramToDelete.getName());
  848. } catch (NimbitsException e) {
  849. GWT.log(e.getMessage(), e);
  850. }
  851. tree.getStore().remove(diagramModelToDelete);
  852. }
  853. });
  854. }
  855. }
  856. };
  857. }