/**
   * Set the current entity that we are browsing and whether the user should be warned or not
   *
   * @param entity
   * @param type
   * @param silent
   */
  private void setEntity(ITridas entity, Class<? extends ITridas> type, Boolean silent) {
    if (!silent) {
      if (!warnLosingChanges()) {
        return;
      }
    }

    hasChanged = false;

    currentEntityType = type;
    currentEntity = entity;

    // Swapping entities so disable editing
    this.enableEditing(false);

    // derive a property list
    List<TridasEntityProperty> properties = TridasEntityDeriver.buildDerivationList(type);
    Property[] propArray = properties.toArray(new Property[properties.size()]);

    // set properties and load from entity
    propertiesPanel.setProperties(propArray);
    propertiesTable.expandAllBranches(true);

    // Add data to table from entity
    if (entity != null) {
      propertiesPanel.readFromObject(entity);
      propertiesPanel.setEnabled(true);
      editEntity.setVisible(true);
    } else {
      propertiesPanel.setEnabled(false);
      editEntity.setVisible(false);
    }
  }
  /**
   * Called to enable editing Responsible for loading the duplicate copy into the editor
   *
   * @param enable
   */
  @SuppressWarnings("unchecked")
  protected void enableEditing(boolean enable) {

    propertiesTable.setEditable(enable);

    // show/hide our buttons
    editEntitySave.setEnabled(true);
    editEntityCancel.setEnabled(true);
    editEntitySave.setVisible(enable);
    editEntityCancel.setVisible(enable);

    if (currentEntity == null) {
      editEntityText.setText(null);
    } else {
      editEntityText.setFont(editEntityText.getFont().deriveFont(Font.BOLD));
      editEntityText.setText(
          enable
              ? I18n.getText("metadata.currentlyEditingThis")
                  + " "
                  + TridasTreeViewPanel.getFriendlyClassName(currentEntityType).toLowerCase()
              : I18n.getText("metadata.clickLockToEdit")
                  + " "
                  + TridasTreeViewPanel.getFriendlyClassName(currentEntityType).toLowerCase());
    }
    editEntity.setSelected(enable);

    if (enable) {
      if (currentEntity == null) return;

      if (currentEntity instanceof ITridasSeries)
        temporaryEditingEntity =
            TridasCloner.cloneSeriesRefValues(
                (ITridasSeries) currentEntity,
                (Class<? extends ITridasSeries>) currentEntity.getClass());
      else temporaryEditingEntity = TridasCloner.clone(currentEntity, currentEntity.getClass());

      if (temporaryEditingEntity != null) propertiesPanel.readFromObject(temporaryEditingEntity);
    } else {
      temporaryEditingEntity = null;

      // don't display anything if we have nothingk!
      if (currentEntity != null) {
        propertiesPanel.readFromObject(currentEntity);
      } else {
        return;
      }
    }
  }
  /** Set up the properties panel */
  private void initPropertiesPanel() {

    // Create table and panel to hold it
    propertiesTable = new TellervoPropertySheetTable();
    propertiesPanel = new TellervoPropertySheetPanel(propertiesTable);

    // Set various properties of the properties panel!
    propertiesPanel.setRestoreToggleStates(true);
    propertiesPanel.setToolBarVisible(false);
    propertiesPanel.setDescriptionVisible(true);
    propertiesPanel.setMode(PropertySheet.VIEW_AS_FLAT_LIST);
    propertiesPanel.getTable().setRowHeight(24);
    propertiesPanel.getTable().setRendererFactory(new TridasPropertyRendererFactory());
    propertiesPanel.getTable().setEditorFactory(new TridasPropertyEditorFactory());
    propertiesPanel.getTable().addPropertyChangeListener(this);

    // Set up button bar
    setupButtonBar();
  }
  /** Save changes to the current entity */
  private void doSave() {
    Class<? extends ITridas> type;

    if (temporaryEditingEntity == null) throw new IllegalStateException();

    // if nothing actually changed, just ignore it like a cancel
    if (!hasChanged) {
      editEntityCancel.doClick();
      return;
    }

    propertiesPanel.writeToObject(temporaryEditingEntity);

    // the resource we'll use
    EntityResource<? extends ITridas> resource;

    if (temporaryEditingEntity instanceof TridasObject) {
      resource =
          new EntityResource<TridasObject>(
              temporaryEditingEntity, TellervoRequestType.UPDATE, TridasObject.class);
      type = TridasObject.class;
    } else if (temporaryEditingEntity instanceof TridasElement) {
      resource =
          new EntityResource<TridasElement>(
              temporaryEditingEntity, TellervoRequestType.UPDATE, TridasElement.class);
      type = TridasElement.class;
    } else if (temporaryEditingEntity instanceof TridasSample) {
      resource =
          new EntityResource<TridasSample>(
              temporaryEditingEntity, TellervoRequestType.UPDATE, TridasSample.class);
      type = TridasSample.class;
    } else if (temporaryEditingEntity instanceof TridasRadius) {
      resource =
          new EntityResource<TridasRadius>(
              temporaryEditingEntity, TellervoRequestType.UPDATE, TridasRadius.class);
      type = TridasRadius.class;
    } else if (temporaryEditingEntity instanceof TridasMeasurementSeries) {
      resource =
          new EntityResource<TridasMeasurementSeries>(
              temporaryEditingEntity, TellervoRequestType.UPDATE, TridasMeasurementSeries.class);
      type = TridasMeasurementSeries.class;
    } else {
      log.debug("Can't save entity.  Unsupported entity class.");
      return;
    }

    // set up a dialog...
    Window parentWindow = SwingUtilities.getWindowAncestor(this);
    TellervoResourceAccessDialog dialog =
        TellervoResourceAccessDialog.forWindow(parentWindow, resource);

    // query the resource
    resource.query();
    dialog.setVisible(true);

    // on failure, just return
    if (!dialog.isSuccessful()) {
      JOptionPane.showMessageDialog(
          this,
          I18n.getText("error.savingChanges")
              + "\r\n"
              + I18n.getText("error")
              + ": "
              + dialog.getFailException().getLocalizedMessage(),
          I18n.getText("error"),
          JOptionPane.ERROR_MESSAGE);
      return;
    }

    // replace the saved result
    temporaryEditingEntity = resource.getAssociatedResult();

    // sanity check the result
    if (temporaryEditingEntity == null) {
      new BugDialog(new IllegalStateException("CREATE or UPDATE entity returned null"));
      return;
    }

    setEntity(temporaryEditingEntity, type, true);

    // Inform the tree to update itself
    if (nodeSelected != null) {
      if (nodeSelected.getParent() == null) {

      } else if (nodeSelected.getParent().equals(nodeSelected.getRoot())) {
        ((TridasTreeViewPanel) treepanel).refreshNode(nodeSelected);
      } else {
        ((TridasTreeViewPanel) treepanel)
            .refreshNode((DefaultMutableTreeNode) nodeSelected.getParent());
      }
    }

    hasChanged = false;
  }