PageRenderTime 49ms CodeModel.GetById 27ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

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