/v3.2/nimbits-tds/src/com/nimbits/client/panels/PointGridPanel.java
http://nimbits-server.googlecode.com/ · Java · 567 lines · 416 code · 128 blank · 23 comment · 18 complexity · d4b720d2e92dbb2674de9453fa89e9de MD5 · raw file
- /*
- * Copyright (c) 2010 Tonic Solutions LLC.
- *
- * http://www.nimbits.com
- *
- *
- * 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
- *
- * http://www.gnu.org/licenses/gpl.html
- *
- * 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.
- */
-
- package com.nimbits.client.panels;
-
- import com.extjs.gxt.ui.client.Style;
- import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
- import com.extjs.gxt.ui.client.data.ModelData;
- import com.extjs.gxt.ui.client.event.*;
- import com.extjs.gxt.ui.client.store.ListStore;
- import com.extjs.gxt.ui.client.widget.ContentPanel;
- import com.extjs.gxt.ui.client.widget.Info;
- import com.extjs.gxt.ui.client.widget.button.Button;
- import com.extjs.gxt.ui.client.widget.form.CheckBox;
- import com.extjs.gxt.ui.client.widget.form.DateField;
- import com.extjs.gxt.ui.client.widget.form.NumberField;
- import com.extjs.gxt.ui.client.widget.form.TextField;
- import com.extjs.gxt.ui.client.widget.grid.*;
- import com.extjs.gxt.ui.client.widget.layout.FillLayout;
- import com.extjs.gxt.ui.client.widget.layout.FitLayout;
- import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
- import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
- import com.google.gwt.core.client.GWT;
- import com.google.gwt.i18n.client.DateTimeFormat;
- import com.google.gwt.i18n.client.NumberFormat;
- import com.google.gwt.user.client.Element;
- import com.google.gwt.user.client.Timer;
- import com.google.gwt.user.client.rpc.AsyncCallback;
- import com.google.gwt.user.client.ui.AbstractImagePrototype;
- import com.nimbits.client.exception.NimbitsException;
- import com.nimbits.client.icons.Icons;
- import com.nimbits.client.model.Const;
- import com.nimbits.client.model.GxtPointModel;
- import com.nimbits.client.model.point.Point;
- import com.nimbits.client.model.point.PointName;
- import com.nimbits.client.model.value.Value;
- import com.nimbits.client.model.value.ValueModelFactory;
- import com.nimbits.client.service.recordedvalues.RecordedValueService;
- import com.nimbits.client.service.recordedvalues.RecordedValueServiceAsync;
-
- import java.util.*;
-
- class PointGridPanel extends NavigationEventProvider {
-
-
- private final ListStore<GxtPointModel> store = new ListStore<GxtPointModel>();
- private final EditorGrid<GxtPointModel> grid;
- private final Map<PointName, Point> points = new HashMap<PointName, Point>();
- private final CheckBox saveToNowCheckBox = new CheckBox();
- private final CheckBox autoSaveCheckBox = new CheckBox();
- private final CheckBoxSelectionModel<GxtPointModel> sm = new CheckBoxSelectionModel<GxtPointModel>();
- private Timer updater;
- private final static int valueColumnIndex = 4;
-
-
- @Override
- protected void onRender(final Element parent, final int index) {
- super.onRender(parent, index);
-
- setLayout(new FillLayout());
- getAriaSupport().setPresentation(true);
-
-
- grid.addListener(Events.AfterEdit, new Listener<GridEvent>() {
-
- @Override
- public void handleEvent(final GridEvent be) {
- updater.cancel();
- final GxtPointModel model = (GxtPointModel) be.getModel();
- if (!model.isReadOnly()) {
- model.setDirty(true);
-
-
- if (be.getColIndex() == valueColumnIndex && autoSaveCheckBox.getValue()) { //only save when the value is updated
-
- final Point point = points.get(model.getName());
- final Date timestamp = saveToNowCheckBox.getValue() ? new Date() : (Date) model.get(Const.PARAM_TIMESTAMP);
- final Double v = model.get(Const.PARAM_VALUE);
- final String note = model.get(Const.PARAM_NOTE);
- final Value value = ValueModelFactory.createValueModel(0.0, 0.0, v, timestamp, model.getId(), note);
-
- GWT.log(value.getNote());
- GWT.log(String.valueOf(value.getValue()));
- RecordedValueServiceAsync service = GWT.create(RecordedValueService.class);
- try {
- service.recordValue(point, value, new AsyncCallback<Value>() {
- @Override
- public void onFailure(final Throwable throwable) {
- be.getRecord().reject(false);
- }
-
- @Override
- public void onSuccess(final Value value) {
- be.getRecord().commit(false);
- model.setDirty(false);
- notifyValueEnteredListener(point, value);
- }
- });
- } catch (NimbitsException e) {
- GWT.log(e.getMessage(), e);
- }
- }
-
-
- }
- updater.scheduleRepeating(Const.DEFAULT_TIMER_UPDATE_SPEED);
- updater.run();
-
- }
- });
-
- final ContentPanel mainPanel = new ContentPanel();
- mainPanel.setTopComponent(gridToolBar());
- mainPanel.setHeaderVisible(false);
- mainPanel.setBorders(false);
- mainPanel.setBodyBorder(false);
- mainPanel.setLayout(new FitLayout());
- mainPanel.setScrollMode(Style.Scroll.AUTO);
- // grid.setHeight(400);
- mainPanel.add(grid);
-
- add(mainPanel);
-
-
- }
-
- public PointGridPanel() {
- grid = new EditorGrid<GxtPointModel>(store, new ColumnModel(gridConfig()));
-
- grid.setHeight("100%");
- grid.setBorders(true);
- grid.setSelectionModel(sm);
- grid.addPlugin(sm);
-
- }
-
- public List<Point> getSelectedPoints() {
- final List<GxtPointModel> models = grid.getSelectionModel().getSelectedItems();
- final List<Point> retObj = new ArrayList<Point>();
-
- for (GxtPointModel model : models) {
- retObj.add(points.get(model.getName()));
- }
- return retObj;
-
- }
-
- private List<ColumnConfig> gridConfig() {
- final List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
- sm.setSelectionMode(Style.SelectionMode.SIMPLE);
-
- configs.add(sm.getColumn());
- addPropertyColumn(configs);
- addAlertColumn(configs);
- addPointNameColumn(configs);
- addCurrentValueColumn(configs);
- addNoteColumn(configs);
- addTimestampColumn(configs);
-
- return configs;
- }
-
- private void addTimestampColumn(final List<ColumnConfig> configs) {
- final DateField dateField = new DateField();
- dateField.getPropertyEditor().setFormat(
- DateTimeFormat.getFormat(Const.FORMAT_DATE_TIME));
-
- final ColumnConfig columnTime = new ColumnConfig();
- columnTime.setId(Const.PARAM_TIMESTAMP);
- columnTime.setHeader(Const.WORD_TIMESTAMP);
- columnTime.setAlignment(HorizontalAlignment.LEFT);
- columnTime.setWidth(200);
- columnTime.setDateTimeFormat(DateTimeFormat
- .getFormat(Const.FORMAT_DATE_TIME));
- columnTime.setEditor(new CellEditor(dateField));
- // column.setDateTimeFormat(DateTimeFormat.getShortDateTimeFormat());
- configs.add(columnTime);
- }
-
- private void addNoteColumn(final List<ColumnConfig> configs) {
- final ColumnConfig columnNote = new ColumnConfig();
- columnNote.setId(Const.PARAM_NOTE);
- columnNote.setHeader(Const.WORD_ANNOTATION);
- columnNote.setWidth(250);
-
- final TextField<String> noteText = new TextField<String>();
- noteText.setAllowBlank(true);
- columnNote.setEditor(new CellEditor(noteText));
- columnNote.setAlignment(HorizontalAlignment.LEFT);
- configs.add(columnNote);
- }
-
- private void addCurrentValueColumn(final List<ColumnConfig> configs) {
- final NumberField n = new NumberField();
- n.getPropertyEditor().setFormat(NumberFormat.getDecimalFormat());
- n.setSelectOnFocus(true);
- n.setEditable(true);
-
- final ColumnConfig columnValue = new ColumnConfig();
- columnValue.setId(Const.PARAM_VALUE);
- columnValue.setHeader(Const.WORD_VALUE);
- columnValue.setAlignment(HorizontalAlignment.CENTER);
- columnValue.setWidth(50);
- columnValue.setNumberFormat(NumberFormat.getDecimalFormat());
- CellEditor ce = new CellEditor(n);
- columnValue.setEditor(ce);
-
- configs.add(columnValue);
- }
-
- private void addPointNameColumn(final List<ColumnConfig> configs) {
- final ColumnConfig nameColumn = new ColumnConfig();
- nameColumn.setId(Const.PARAM_NAME);
- nameColumn.setHeader(Const.MESSAGE_DATA_POINT);
- nameColumn.setAlignment(HorizontalAlignment.LEFT);
-
- nameColumn.setWidth(250);
-
- TextField<String> nameText = new TextField<String>();
- nameText.setAllowBlank(false);
- nameText.setSelectOnFocus(true);
- configs.add(nameColumn);
- }
-
- private void addPropertyColumn(final List<ColumnConfig> configs) {
- final GridCellRenderer<GxtPointModel> propertyButtonRenderer = new GridCellRenderer<GxtPointModel>() {
-
-
- public Object render(final GxtPointModel model, final String property, final ColumnData config, final int rowIndex,
- final int colIndex, final ListStore<GxtPointModel> store, final Grid<GxtPointModel> grid) {
-
-
- final Button b = new Button((String) model.get(property), new SelectionListener<ButtonEvent>() {
- @Override
- public void componentSelected(ButtonEvent ce) {
- Point p = points.get(model.getName());
- // notifyPointClickedListener(p);
- try {
- createPointPropertyWindow(p);
- } catch (NimbitsException e) {
- GWT.log(e.getMessage());
- }
-
- }
- });
-
- b.setWidth(20);
- b.setToolTip(Const.MESSAGE_CONFIGURE_POINT);
- b.setEnabled(!model.isReadOnly());
- b.setBorders(false);
- // b.setStyleName("gwt-button");
- b.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.edit()));
- return b;
- }
- };
-
-
- final ColumnConfig propertyColumn = new ColumnConfig();
- propertyColumn.setId(Const.PARAM_PROPERTY);
- propertyColumn.setHeader("edit");
- propertyColumn.setWidth(35);
- propertyColumn.setAlignment(HorizontalAlignment.LEFT);
- propertyColumn.setRenderer(propertyButtonRenderer);
- configs.add(propertyColumn);
- }
-
- private void addAlertColumn(final List<ColumnConfig> configs) {
- final GridCellRenderer<GxtPointModel> propertyButtonRenderer = new GridCellRenderer<GxtPointModel>() {
-
- public Object render(final GxtPointModel model, final String property, final ColumnData config, final int rowIndex,
- final int colIndex, final ListStore<GxtPointModel> store, final Grid<GxtPointModel> grid) {
-
- final Button b = new Button((String) model.get(property), new SelectionListener<ButtonEvent>() {
- @Override
- public void componentSelected(final ButtonEvent ce) {
- final Point p = points.get(model.getName());
- notifyPointClickedListener(p);
- /// createPointPropertyWindow(p);
-
- }
- });
-
- b.setWidth(25);
- b.setToolTip(Const.MESSAGE_CLICK_TO_TREND);
- b.setEnabled(!model.isReadOnly());
-
- b.setBorders(false);
- switch (model.getAlertState()) {
- case IdleAlert:
- b.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.point_idle()));
- break;
- case HighAlert:
- b.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.point_high()));
- break;
- case LowAlert:
- b.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.point_low()));
- break;
- default:
- b.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.point_ok()));
-
- }
-
- return b;
- }
- };
-
-
- final ColumnConfig propertyColumn = new ColumnConfig();
- propertyColumn.setId(Const.PARAM_STATE);
- propertyColumn.setHeader("state");
- propertyColumn.setWidth(35);
- propertyColumn.setAlignment(HorizontalAlignment.LEFT);
- propertyColumn.setRenderer(propertyButtonRenderer);
- configs.add(propertyColumn);
- }
-
-
- @Override
- protected void onAttach() {
- updater = new Timer() {
- @Override
- public void run() {
- try {
- updateValues();
- } catch (NimbitsException e) {
- GWT.log(e.getMessage(), e);
- }
- }
- };
- updater.scheduleRepeating(Const.DEFAULT_TIMER_UPDATE_SPEED);
- updater.run();
- super.onAttach();
- }
-
- @Override
- protected void onDetach() {
-
- updater.cancel();
-
- // updater.cancel();
- super.onDetach();
- }
-
- public void addPoint(final Point point) {
- if (!points.containsKey(point.getName())) {
- points.put(point.getName(), point);
- store.add(new GxtPointModel(point));
- }
-
- }
-
- private void createPointPropertyWindow(final Point p) throws NimbitsException {
- final com.extjs.gxt.ui.client.widget.Window window = new com.extjs.gxt.ui.client.widget.Window();
- final PointPanel pp = new PointPanel(p);
-
- pp.addPointUpdatedListeners(new PointPanel.PointUpdatedListener() {
- @Override
- public void onPointUpdated(final Point p) {
-
- points.remove(p.getName());
- points.put(p.getName(), p);
-
- // mp.setTopComponent(mainToolBar( points));
- }
- });
-
- pp.addPointDeletedListeners(new PointPanel.PointDeletedListener() {
- @Override
- public void onPointDeleted(Point p) {
- points.remove(p.getName());
-
-
- }
- });
-
- window.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.connect()));
- window.setSize(466, 520);
- window.setPlain(false);
- window.setModal(true);
- window.setBlinkModal(true);
- window.setHeading(p.getName().getValue() + " Properties");
- window.setHeaderVisible(true);
- window.setBodyBorder(true);
- // window.setLayout(new FitLayout());
- window.add(pp);
- window.show();
- }
-
-
- private void updateValues() throws NimbitsException {
- store.commitChanges();
- final RecordedValueServiceAsync dataService = GWT.create(RecordedValueService.class);
- if (!grid.isEditing()) {
- for (final GxtPointModel gxtPointModel : store.getModels()) {
- if (!gxtPointModel.isDirty()) {
- final Point point = points.get(gxtPointModel.getName());
-
- dataService.getCurrentValue(point,
- new AsyncCallback<Value>() {
-
- @Override
- public void onFailure(final Throwable caught) {
- GWT.log(caught.getMessage(), caught);
- }
-
- @Override
- public void onSuccess(final Value result) {
-
-
- if (!(result == null)) {
-
- gxtPointModel.set(Const.PARAM_VALUE, result.getValue());
- gxtPointModel.set(Const.PARAM_TIMESTAMP, result.getTimestamp());
- gxtPointModel.set(Const.PARAM_NOTE, result.getNote());
- gxtPointModel.setAlertState(result.getAlertState());
- store.update(gxtPointModel);
-
- } else {
- gxtPointModel.set(Const.PARAM_VALUE, 0);
-
- gxtPointModel.set(Const.PARAM_TIMESTAMP, new Date());
- gxtPointModel.set(Const.PARAM_NOTE, "");
- store.update(gxtPointModel);
- }
- }
-
- });
-
- }
-
-
- }
- }
-
- }
-
- private ToolBar gridToolBar() {
- final ToolBar t = new ToolBar();
-
- final Button refresh = new Button();
- refresh.setText("");
-
- refresh.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.refresh2()));
- refresh.addSelectionListener(new SelectionListener<ButtonEvent>() {
-
- @Override
- public void componentSelected(ButtonEvent ce) {
-
-
- try {
- updateValues();
- } catch (NimbitsException ignored) {
-
- }
- }
-
- });
-
-
- final Button pause = new Button();
- pause.setText("");
-
- pause.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.Pause()));
- pause.addSelectionListener(new SelectionListener<ButtonEvent>() {
-
- @Override
- public void componentSelected(ButtonEvent ce) {
-
- updater.cancel();
- }
-
- });
- Button play = new Button();
- play.setText("");
-
- play.setIcon(AbstractImagePrototype.create(Icons.INSTANCE.Play()));
- play.addSelectionListener(new SelectionListener<ButtonEvent>() {
-
- @Override
- public void componentSelected(ButtonEvent ce) {
-
- updater.scheduleRepeating(Const.DEFAULT_TIMER_UPDATE_SPEED);
- updater.run();
- }
- });
-
- saveToNowCheckBox.setBoxLabel("Save with Current Time");
- saveToNowCheckBox.setValue(true);
-
- autoSaveCheckBox.setBoxLabel("Auto-Save on new value entry");
- autoSaveCheckBox.setValue(true);
-
- // t.add(save);
- t.add(refresh);
-
- t.add(pause);
- t.add(play);
-
- t.add(new SeparatorToolItem());
-
- t.add(saveToNowCheckBox);
- t.add(autoSaveCheckBox);
-
- return t;
-
- }
-
- public void removePoint(Point p) {
-
- for (final ModelData m : store.getModels()) {
-
- final GxtPointModel model = (GxtPointModel) m;
- points.remove(((GxtPointModel) m).getName());
- if (model.getName().getValue().equals(p.getName().getValue())) {
- store.remove(model);
- break;
- }
- }
-
-
- //To change body of created methods use File | Settings | File Templates.
- }
-
-
- public List<Point> saveSelectedPoints() throws NimbitsException {
- final List<GxtPointModel> models = grid.getSelectionModel().getSelectedItems();
- final List<Point> retObj = new ArrayList<Point>();
- RecordedValueServiceAsync service = GWT.create(RecordedValueService.class);
- for (final GxtPointModel model : models) {
- if (model.isDirty()) {
-
- final Date timestamp = saveToNowCheckBox.getValue() ? new Date() : (Date) model.get(Const.PARAM_TIMESTAMP);
- final double v = model.get(Const.PARAM_VALUE) == null ? 0.0 : Double.valueOf(model.get(Const.PARAM_VALUE).toString());
- final String note = model.get(Const.PARAM_NOTE);
- final Value value = ValueModelFactory.createValueModel(0.0, 0.0, v, timestamp, model.getId(), note);
- service.recordValue(points.get(model.getName()), value, new AsyncCallback<Value>() {
- @Override
- public void onFailure(final Throwable throwable) {
- Info.display("Error Saving", throwable.getMessage());
- }
-
- @Override
- public void onSuccess(final Value value) {
-
- //be.getRecord().commit(false);
- model.setDirty(false);
- notifyValueEnteredListener(points.get(model.getName()), value);
- }
- });
- model.setDirty(false);
- }
- retObj.add(points.get(model.getName()));
- }
- return retObj;
-
- }
- }