/**
   * 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;
      }
    }
  }
  /** 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;
  }