private void lookupUsersAndGroups() {
    PermissionsEntityType pEntityType;

    if (theentity instanceof TridasObject) {
      pEntityType = PermissionsEntityType.OBJECT;
    } else if (theentity instanceof TridasElement) {
      pEntityType = PermissionsEntityType.ELEMENT;
    } else if (theentity instanceof TridasMeasurementSeries) {
      pEntityType = PermissionsEntityType.MEASUREMENT_SERIES;
    } else if (theentity instanceof TridasDerivedSeries) {
      pEntityType = PermissionsEntityType.DERIVED_SERIES;
    } else {
      Alert.error(
          "Error", "Permissions information is only available for objects, elements and series");
      return;
    }

    PermissionsResource resource = new PermissionsResource();

    for (WSISecurityUser user :
        (ArrayList<WSISecurityUser>)
            Dictionary.getDictionaryAsArrayList("securityUserDictionary")) {
      if (!user.isIsActive()) continue;

      resource.addPermission(pEntityType, theentity.getIdentifier().getValue(), user);
    }

    for (WSISecurityGroup grp :
        (ArrayList<WSISecurityGroup>)
            Dictionary.getDictionaryAsArrayList("securityGroupDictionary")) {
      resource.addPermission(pEntityType, theentity.getIdentifier().getValue(), grp);
    }

    // Query db
    TellervoResourceAccessDialog dialog = new TellervoResourceAccessDialog(resource);
    resource.query();
    dialog.setVisible(true);

    if (!dialog.isSuccessful()) {
      log.error("Error getting permissions info");
      Alert.error("Error", "Error getting permissions info");
      return;
    }

    permsList = resource.getAssociatedResult();

    if (tblGroupPerms != null) tblGroupPerms.repaint();
    if (tblUserPerms != null) tblUserPerms.repaint();

    if (permsList.size() == 0) {
      Alert.error("Error", "No records found");
      return;
    }
  }
  /**
   * 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;
      }
    }
  }
  public void setEntity(ITridas entity) {
    theentity = entity;
    lookupUsersAndGroups();

    userTableModel = new UsersWithPermissionsTableModel(permsList);
    groupTableModel = new GroupsWithPermissionsTableModel(permsList);
    groupTableModel.addTableModelListener(
        new TableModelListener() {

          @Override
          public void tableChanged(TableModelEvent e) {
            updateUsersTable();
          }
        });
    setLayout(new MigLayout("", "[][grow]", "[86px][28.00][328px,grow]"));

    lblPermissionsInfoFor = new JLabel("Permissions info for:");
    add(lblPermissionsInfoFor, "cell 0 1,alignx trailing");

    // TODO : make this code pretty...
    txtLabCode = new JTextField();
    txtLabCode.setText(entity.getTitle());
    txtLabCode.setFocusable(false);

    txtLabCode.setEditable(false);
    add(txtLabCode, "cell 1 1,growx");
    txtLabCode.setColumns(10);

    tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    add(tabbedPane, "cell 0 2 2 1,grow");

    panelGroups = new JPanel();
    tabbedPane.addTab(
        "Group permissions", Builder.getIcon("edit_group.png", 22), panelGroups, null);
    panelGroups.setLayout(new MigLayout("", "[648px,grow]", "[381px,grow][]"));

    JScrollPane scrollGroup = new JScrollPane();
    panelGroups.add(scrollGroup, "cell 0 0,grow");

    tblGroupPerms = new JXTable(groupTableModel);
    tblGroupPerms.setSortable(true);

    searchableGroup = new TableSearchable(tblGroupPerms);
    tblGroupPerms.setSearchable(searchableGroup);

    scrollGroup.setViewportView(tblGroupPerms);

    btnEditGroup = new JButton("View / Edit Group");
    btnEditGroup.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent arg0) {
            editSelectedGroup();
          }
        });
    panelGroups.add(btnEditGroup, "flowx,cell 0 1");

    btnRevertToDefault = new JButton("Revert to database defaults");
    btnRevertToDefault.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent e) {
            resetPermissionsForCurrentGroup();
          }
        });
    panelGroups.add(btnRevertToDefault, "cell 0 1");

    btnGroupRefresh = new JButton("Refresh");
    btnGroupRefresh.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent arg0) {
            updateGroupsTable();
          }
        });
    panelGroups.add(btnGroupRefresh, "cell 0 1");

    tblGroupPerms.getColumnModel().getColumn(0).setPreferredWidth(15);
    tblGroupPerms.getColumnModel().getColumn(1).setPreferredWidth(100);
    tblGroupPerms.getColumnModel().getColumn(2).setPreferredWidth(45);
    tblGroupPerms.getColumnModel().getColumn(3).setPreferredWidth(45);
    tblGroupPerms.getColumnModel().getColumn(4).setPreferredWidth(45);
    tblGroupPerms.getColumnModel().getColumn(5).setPreferredWidth(45);
    tblGroupPerms.getColumnModel().getColumn(6).setPreferredWidth(45);
    tblGroupPerms.getColumnModel().getColumn(7).setPreferredWidth(300);

    panelUsers = new JPanel();
    tabbedPane.addTab("Users with access", Builder.getIcon("edit_user.png", 22), panelUsers, null);
    panelUsers.setLayout(new MigLayout("", "[648px,grow]", "[381px,grow][]"));

    JScrollPane scrollUser = new JScrollPane();
    panelUsers.add(scrollUser, "cell 0 0,grow");

    tblUserPerms = new JXTable(userTableModel);

    searchableUser = new TableSearchable(tblUserPerms);
    tblUserPerms.setSearchable(searchableUser);

    tblUserPerms.setSortable(true);
    scrollUser.setViewportView(tblUserPerms);

    btnEditUser = new JButton("View / Edit User");
    btnEditUser.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent arg0) {
            editSelectedUser();
          }
        });
    panelUsers.add(btnEditUser, "flowx,cell 0 1");

    btnUserRefresh = new JButton("Refresh");
    btnUserRefresh.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent arg0) {

            updateUsersTable();
          }
        });
    panelUsers.add(btnUserRefresh, "cell 0 1");

    tblUserPerms.getColumnModel().getColumn(0).setPreferredWidth(15);
    tblUserPerms.getColumnModel().getColumn(1).setPreferredWidth(100);
    tblUserPerms.getColumnModel().getColumn(2).setPreferredWidth(45);
    tblUserPerms.getColumnModel().getColumn(3).setPreferredWidth(45);
    tblUserPerms.getColumnModel().getColumn(4).setPreferredWidth(45);
    tblUserPerms.getColumnModel().getColumn(5).setPreferredWidth(45);
    tblUserPerms.getColumnModel().getColumn(6).setPreferredWidth(45);
    tblUserPerms.getColumnModel().getColumn(7).setPreferredWidth(300);

    tblGroupPerms.addMouseListener(this);
    tblUserPerms.addMouseListener(this);

    panelTitle = new JPanel();
    add(panelTitle, "cell 0 0 2 1,growx,aligny top");
    panelTitle.setBackground(Color.WHITE);
    panelTitle.setLayout(new MigLayout("", "[411.00,grow][]", "[][grow]"));

    lblGroupPermissions = new JLabel("Access Permissions");
    lblGroupPermissions.setFont(new Font("Dialog", Font.BOLD, 14));
    panelTitle.add(lblGroupPermissions, "flowy,cell 0 0");

    panelIcon = new JPanel();
    panelIcon.setBackground(Color.WHITE);
    panelTitle.add(panelIcon, "cell 1 0 1 2,grow");

    lblIcon = new JLabel();
    lblIcon.setIcon(Builder.getIcon("trafficlight.png", 64));
    panelIcon.add(lblIcon);

    txtDescription = new JTextArea();
    txtDescription.setBorder(null);
    txtDescription.setEditable(false);
    txtDescription.setFocusable(false);
    txtDescription.setFont(new Font("Dialog", Font.PLAIN, 10));
    txtDescription.setLineWrap(true);
    txtDescription.setWrapStyleWord(true);
    txtDescription.setText(
        "Permissions are set on groups, not users.  You can set: create; read; "
            + "update; and delete permissions separately.  Permissions are inherited from the default "
            + "database permissions given to the group (editable in the main group dialog). If you "
            + "change permissions here you will override the access for the current entity and any "
            + "child entities in the database. "
            + "The users tab shows the current list of users who have permission to access this "
            + "entity in some way.");
    panelTitle.add(txtDescription, "cell 0 1,grow,wmin 10");
  }