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

http://nimbits-server.googlecode.com/ · Java · 320 lines · 232 code · 59 blank · 29 comment · 21 complexity · c1ef83f76970ebd8ce24223565b5a7d9 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;
  15. import com.extjs.gxt.ui.client.event.BaseEvent;
  16. import com.extjs.gxt.ui.client.event.Events;
  17. import com.extjs.gxt.ui.client.event.Listener;
  18. import com.extjs.gxt.ui.client.util.Margins;
  19. import com.extjs.gxt.ui.client.widget.ContentPanel;
  20. import com.extjs.gxt.ui.client.widget.Info;
  21. import com.extjs.gxt.ui.client.widget.button.Button;
  22. import com.extjs.gxt.ui.client.widget.layout.FlowData;
  23. import com.extjs.gxt.ui.client.widget.layout.RowData;
  24. import com.extjs.gxt.ui.client.widget.layout.RowLayout;
  25. import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
  26. import com.google.gwt.user.client.Element;
  27. import com.google.gwt.user.client.ui.AbstractImagePrototype;
  28. import com.nimbits.client.exception.NimbitsException;
  29. import com.nimbits.client.icons.Icons;
  30. import com.nimbits.client.model.Const;
  31. import com.nimbits.client.model.diagram.Diagram;
  32. import com.nimbits.client.model.point.Point;
  33. import com.nimbits.client.model.point.PointName;
  34. import com.nimbits.client.model.value.Value;
  35. import java.util.HashMap;
  36. import java.util.List;
  37. import java.util.Map;
  38. /**
  39. * Created by bsautner
  40. * User: benjamin
  41. * Date: 7/23/11
  42. * Time: 10:37 AM
  43. */
  44. class CenterPanel extends NavigationEventProvider {
  45. final private Map<PointName, Point> points = new HashMap<PointName, Point>();
  46. final private PointGridPanel grid = new PointGridPanel();
  47. private final Map<String, AnnotatedTimeLinePanel> lines = new HashMap<String, AnnotatedTimeLinePanel>();
  48. private ContentPanel bottom;
  49. protected void onRender(final Element target, final int index) {
  50. super.onRender(target, index);
  51. grid.addPointClickedListeners(new NavigationEventProvider.PointClickedListener() {
  52. @Override
  53. public void onPointClicked(final Point c) {
  54. addPoint(c);
  55. }
  56. });
  57. grid.addValueEnteredListeners(new ValueEnteredListener() {
  58. @Override
  59. public void onValueEntered(final Point point, final Value value) {
  60. for (AnnotatedTimeLinePanel line : lines.values()) {
  61. if (line.containsPoint(point)) {
  62. line.addValue(point, value);
  63. }
  64. }
  65. }
  66. });
  67. loadLayout();
  68. }
  69. private ToolBar toolbar() {
  70. ToolBar toolBar = new ToolBar();
  71. Button addChartButton = new Button("Add Chart");
  72. addChartButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.chart24()));
  73. addChartButton.setToolTip("Add another chart");
  74. addChartButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
  75. @Override
  76. public void handleEvent(BaseEvent baseEvent) {
  77. final String name = (lines.size() == 0) ? Const.DEFAULT_CHART_NAME : "line" + lines.size() + 1;
  78. lines.put(name, createLine(name));
  79. addLinesToBottom();
  80. }
  81. });
  82. toolBar.add(addChartButton);
  83. Button removeButton = new Button("Hide Point");
  84. removeButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.delete()));
  85. removeButton.setToolTip("Remove Point from display");
  86. removeButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
  87. @Override
  88. public void handleEvent(BaseEvent baseEvent) {
  89. List<Point> selectedPoints = grid.getSelectedPoints();
  90. for (Point px : selectedPoints) {
  91. removePoint(px);
  92. }
  93. addLinesToBottom();
  94. }
  95. });
  96. toolBar.add(removeButton);
  97. Button saveButton = new Button("Save");
  98. saveButton.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.SaveAll()));
  99. saveButton.setToolTip("Save checked rows");
  100. saveButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
  101. @Override
  102. public void handleEvent(BaseEvent baseEvent) {
  103. try {
  104. grid.saveSelectedPoints();
  105. } catch (NimbitsException e) {
  106. Info.display("Error Saving", e.getMessage());
  107. }
  108. }
  109. });
  110. toolBar.add(saveButton);
  111. return toolBar;
  112. }
  113. private AnnotatedTimeLinePanel createLine(final String name) {
  114. final AnnotatedTimeLinePanel line = new AnnotatedTimeLinePanel(true, name);
  115. line.setSelected(true);
  116. line.addChartRemovedClickedListeners(new ChartRemovedListener() {
  117. @Override
  118. public void onChartRemovedClicked(String chartName) {
  119. lines.remove(chartName);
  120. addLinesToBottom();
  121. }
  122. });
  123. line.addListener(Events.OnClick, new Listener<BaseEvent>() {
  124. @Override
  125. public void handleEvent(BaseEvent baseEvent) {
  126. for (AnnotatedTimeLinePanel l : lines.values()) {
  127. l.setSelected(false);
  128. }
  129. line.setSelected(true);
  130. }
  131. });
  132. line.isSelected();
  133. for (AnnotatedTimeLinePanel l : lines.values()) {
  134. l.setSelected(false);
  135. }
  136. return line;
  137. }
  138. private boolean isOneLineSelected() {
  139. for (AnnotatedTimeLinePanel l : lines.values()) {
  140. if (l.isSelected()) {
  141. return true;
  142. }
  143. }
  144. return false;
  145. }
  146. private void addLinesToBottom() {
  147. initBottomPanel();
  148. assert (lines.size() > 0);
  149. if (!isOneLineSelected() && lines.values().iterator().hasNext()) {
  150. lines.values().iterator().next().setSelected(true);
  151. }
  152. for (final AnnotatedTimeLinePanel l : lines.values()) {
  153. if (lines.size() == 1) {
  154. l.setSelected(true);
  155. }
  156. double w = 1.0 / (double) lines.size();
  157. l.initChart();
  158. // l.refreshChart();
  159. bottom.add(l, new RowData(w, 1, new Margins(4)));
  160. }
  161. add(bottom, new FlowData(0));
  162. doLayout(true);
  163. }
  164. private void initBottomPanel() {
  165. if (bottom != null && getItems().contains(bottom)) {
  166. remove(bottom);
  167. }
  168. bottom = bottomPanel();
  169. }
  170. public void removePoint(final Point p) {
  171. for (AnnotatedTimeLinePanel line : lines.values()) {
  172. line.removePoint(p);
  173. points.remove(p.getName());
  174. }
  175. grid.removePoint(p);
  176. }
  177. private void loadLayout() {
  178. final ContentPanel panel = new ContentPanel();
  179. panel.setHeading("Data Channels");
  180. panel.setLayout(new RowLayout(Style.Orientation.VERTICAL));
  181. // panel.setSize(400, 300);
  182. panel.setFrame(true);
  183. panel.setCollapsible(true);
  184. panel.setHeight("100%");
  185. panel.add(grid, new RowData(1, 1, new Margins(4)));
  186. panel.setTopComponent(toolbar());
  187. add(panel, new FlowData(0));
  188. // final private AnnotatedTimeLinePanel line1 = new AnnotatedTimeLinePanel(true);
  189. addBlankLineToBottom();
  190. // add(bottom, new FlowData(0));
  191. layout(true);
  192. }
  193. private void addBlankLineToBottom() {
  194. final AnnotatedTimeLinePanel line = createLine(Const.DEFAULT_CHART_NAME);
  195. lines.put(Const.DEFAULT_CHART_NAME, line);
  196. addLinesToBottom();
  197. }
  198. private ContentPanel bottomPanel() {
  199. final ContentPanel bottom = new ContentPanel();
  200. bottom.setLayout(new RowLayout(Style.Orientation.HORIZONTAL));
  201. bottom.setFrame(false);
  202. bottom.setCollapsible(true);
  203. bottom.setHeaderVisible(false);
  204. return bottom;
  205. }
  206. public void addPoint(final Point point) {
  207. if (!points.containsKey(point.getName())) {
  208. points.put(point.getName(), point);
  209. grid.addPoint(point);
  210. }
  211. for (final AnnotatedTimeLinePanel line : lines.values()) {
  212. if (!line.containsPoint(point) && line.isSelected()) {
  213. line.addPoint(point);
  214. }
  215. }
  216. //
  217. // if (! line1.containsPoint(point)) {
  218. // line1.addPoint(point);
  219. // }
  220. // else if (! line2.containsPoint(point)) {
  221. // line2.addPoint(point);
  222. // }
  223. }
  224. public void addDiagram(final Diagram d) {
  225. final int w = (bottom.getWidth() / 2);
  226. final AnnotatedTimeLinePanel line = createLine(Const.DEFAULT_CHART_NAME);
  227. final DiagramPanel diagramPanel = new DiagramPanel(d, true, w, bottom.getHeight());
  228. diagramPanel.addPointClickedListeners(new NavigationEventProvider.PointClickedListener() {
  229. @Override
  230. public void onPointClicked(final Point p) {
  231. addPoint(p);
  232. }
  233. });
  234. diagramPanel.addDiagramClickedListeners(new NavigationEventProvider.DiagramClickedListener() {
  235. @Override
  236. public void onDiagramClicked(Diagram d) {
  237. bottom.remove(diagramPanel);
  238. addDiagram(d);
  239. }
  240. });
  241. diagramPanel.addDiagramRemovedClickedListeners(new DiagramRemovedListener() {
  242. @Override
  243. public void onDiagramRemovedClicked(Diagram diagram) {
  244. bottom.remove(diagramPanel);
  245. line.resize(bottom.getHeight(), bottom.getWidth());
  246. line.setWidth(bottom.getWidth());
  247. line.setHeight(bottom.getHeight());
  248. bottom.remove(line);
  249. bottom.add(line, new RowData(1, 1, new Margins(4)));
  250. layout(true);
  251. }
  252. });
  253. bottom.removeAll();
  254. lines.clear();
  255. line.setSelected(true);
  256. lines.put(Const.DEFAULT_CHART_NAME, line);
  257. bottom.add(diagramPanel, new RowData(w, 1, new Margins(4)));
  258. bottom.add(line, new RowData(w, 1, new Margins(4)));
  259. layout(true);
  260. }
  261. }