HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Conditional logic to determine if popup menu should be visible

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
visiblelogicconditionalpopupmenudetermineshould

Problem

I have this small piece of code written in a class. I am looking for a nicer way to write this, preferably avoiding nested if statements like what I currently have:

@Override
  public void popupMenuWillBecomeVisible(PopupMenuEvent e)
  {
    TheatreListDataWrapper[] selectedOperations = getSelectedDatas();
    boolean isOperationFromMulitpleSoc = TheatreListDataWrapper.operationsOfMultiplePatients(selectedOperations);
    if (!isOperationFromMulitpleSoc)
    {
      //passing boolean literal value 'false' instead of variable 'isOperationFromMultipleSoc' since that,
      //at this point it is certain that the operations selected are from a single patient
      CambioAction attentionSignalAction = AttentionSignalDelegator.getAttentionSignalAction(false,selectedOperations);
      if (attentionSignalAction != null)
      {
        if (Framework.getInstance().getActiveSubjectOfCare().id == null
            || !getAcuteListTable().getTableDataByRow(getAcuteListTable().getSelectedRow()).socId.equals(Framework
                .getInstance().getActiveSubjectOfCare().id))
        {
          attentionSignalAction.setEnabled(false);
        }
        else
        {
          attentionSignalAction.setEnabled(true);
        }
      }
    }

    if(selectedOperations != null && selectedOperations.length > 1){
        listRegistraionOpenMenuItem.setEnabled(false);
    }else{
        listRegistraionOpenMenuItem.setEnabled(true);
    }
  }


For me, using that last if checking seems like, I could do it in a better way, using a switch or some other control structure. Is there a way to avoid the null checks?

Solution

You may not need another if else to setEnabled. The expression within if returns a boolean. Just set the inverse of it to setEnabled. You can pull it into a variable in case of better readability.

Updated:

if (attentionSignalAction != null) {
  bool isActiveRowSelected = (Framework.getInstance().getActiveSubjectOfCare().id == null||!getAcuteListTable().getTableDataByRow(getAcuteListTable().getSelectedRow()).socId.equals(Framework.getInstance().getActiveSubjectOfCare().id));
  attentionSignalAction.setEnabled(!isActiveRowSelected);
}


Update 2

if(selectedOperations != null && selectedOperations.length > 1){
    listRegistraionOpenMenuItem.setEnabled(false);
}else{
    listRegistraionOpenMenuItem.setEnabled(true);
}


can also be reduced to

bool operationsSelected = (selectedOperations != null && selectedOperations.length > 1);
listRegistraionOpenMenuItem.setEnabled(!operationsSelected);

Code Snippets

if (attentionSignalAction != null) {
  bool isActiveRowSelected = (Framework.getInstance().getActiveSubjectOfCare().id == null||!getAcuteListTable().getTableDataByRow(getAcuteListTable().getSelectedRow()).socId.equals(Framework.getInstance().getActiveSubjectOfCare().id));
  attentionSignalAction.setEnabled(!isActiveRowSelected);
}
if(selectedOperations != null && selectedOperations.length > 1){
    listRegistraionOpenMenuItem.setEnabled(false);
}else{
    listRegistraionOpenMenuItem.setEnabled(true);
}
bool operationsSelected = (selectedOperations != null && selectedOperations.length > 1);
listRegistraionOpenMenuItem.setEnabled(!operationsSelected);

Context

StackExchange Code Review Q#117908, answer score: 4

Revisions (0)

No revisions yet.