/** * Add item to the model. * * <p>If the item has no color, this will define its color based on the model's next available * color. * * <p>If the model is already 'running', the item will be 'start'ed. * * @param item {@link ModelItem} to add * @throws RuntimeException if item is already in model * @throws Exception on error trying to start a PV Item that's added to a running model */ public void addItem(final ModelItem item) throws Exception { // A new item with the same PV name are allowed to be added in the // model. This way Data Browser can show the trend of the same PV // in different axes or with different waveform indexes. For example, // one may want to show the first element of epics://aaa:bbb in axis 1 // while showing the third element of the same PV in axis 2 to compare // their trends in one chart. // // if (getItem(item.getName()) != null) // throw new RuntimeException("Item " + item.getName() + " already in Model"); // But, if exactly the same instance of the given ModelItem already exists in this // model, it will not be added. if (items.indexOf(item) != -1) throw new RuntimeException("Item " + item.getName() + " already in Model"); // Assign default color if (item.getColor() == null) item.setColor(getNextItemColor()); // Force item to be on an axis if (item.getAxis() == null) { if (axes.size() == 0) addAxis(item.getDisplayName()); item.setAxis(axes.get(0)); } // Check item axis if (!axes.contains(item.getAxis())) throw new Exception("Item " + item.getName() + " added with invalid axis " + item.getAxis()); // Add to model items.add(item); item.setModel(this); if (is_running && item instanceof PVItem) ((PVItem) item).start(scanner); // Notify listeners of new item for (ModelListener listener : listeners) listener.itemAdded(item); }
/** * Remove item from the model. * * <p>If the model and thus item are 'running', the item will be 'stopped'. * * @param item * @throws RuntimeException if item is already in model */ public void removeItem(final ModelItem item) { if (is_running && item instanceof PVItem) { final PVItem pv = (PVItem) item; pv.stop(); // Delete its samples: // For one, so save memory. // Also, in case item is later added back in, its old samples // will have gaps because the item was stopped pv.getSamples().clear(); } if (!items.remove(item)) throw new RuntimeException("Unknown item " + item.getName()); // Detach item from model item.setModel(null); // Notify listeners of removed item for (ModelListener listener : listeners) listener.itemRemoved(item); // Remove axis if unused AxisConfig axis = item.getAxis(); item.setAxis(null); if (countActiveItemsOnAxis(axis) == 0) { removeAxis(axis); fireAxisChangedEvent(null); } }
/** * @param axis Axis to remove * @throws Error when axis not in model, or axis in use by model item */ public void removeAxis(final AxisConfig axis) { if (!axes.contains(axis)) throw new Error("Unknown AxisConfig"); for (ModelItem item : items) if (item.getAxis() == axis) throw new Error("Cannot removed AxisConfig while in use"); axis.setModel(null); axes.remove(axis); fireAxisChangedEvent(null); }
/** * @param axis Axis to test * @return ModelItem linked to this axis count */ public int countActiveItemsOnAxis(final AxisConfig axis) { int count = 0; for (ModelItem item : items) if (item.getAxis() == axis && item.isVisible()) count++; return count; }
/** * @param axis Axis to test * @return First ModelItem that uses the axis, <code>null</code> if axis is empty */ public ModelItem getFirstItemOnAxis(final AxisConfig axis) { for (ModelItem item : items) if (item.getAxis() == axis) return item; return null; }