/**
  * This method invokes the correct edit dialog according to the supplied action type.
  *
  * @param action The action to edit
  */
 private void showActionEditDialog(KeywordAction action) {
   BaseActionDialog dialog;
   switch (action.getType()) {
     case FORWARD:
       dialog = new ForwardActionDialog(ui, this);
       break;
     case JOIN:
       dialog = new JoinGroupActionDialog(ui, this);
       break;
     case LEAVE:
       dialog = new JoinGroupActionDialog(ui, this);
       break;
     case REPLY:
       dialog = new ReplyActionDialog(ui, this);
       break;
     case EXTERNAL_CMD:
       dialog = new ExternalCommandActionDialog(ui, this);
       break;
     case EMAIL:
       dialog = new EmailActionDialog(ui, this);
       break;
     default:
       throw new IllegalStateException();
   }
   dialog.init(action);
   dialog.show();
 }
  public void showSelectedKeyword() {
    Object selected = ui.getSelectedItem(keywordListComponent);

    Object divider = find(COMPONENT_KEYWORDS_DIVIDER);
    if (ui.getItems(divider).length >= 2) {
      ui.remove(ui.getItems(divider)[ui.getItems(divider).length - 1]);
    }

    // If selected is null, then we are here because a keyword has been unselected
    if (selected == null) {
      enableKeywordFields(ui.find(COMPONENT_KEY_PANEL));
      return;
    }

    // An existent keyword is selected, let's check if it is simple or advanced.
    Keyword keyword = ui.getAttachedObject(selected, Keyword.class);
    Collection<KeywordAction> actions = this.keywordActionDao.getActions(keyword);
    boolean simple = actions.size() <= 3;
    if (simple) {
      KeywordAction.Type previousType = null;
      for (KeywordAction action : actions) {
        KeywordAction.Type type = action.getType();
        if (type != KeywordAction.Type.REPLY
            && type != KeywordAction.Type.JOIN
            && type != KeywordAction.Type.LEAVE) {
          simple = false;
          break;
        }

        if (action.getEndDate() != DEFAULT_END_DATE) {
          simple = false;
          break;
        }

        if (type == previousType) {
          simple = false;
          break;
        }

        previousType = type;
      }
    }

    String keywordDescription = getDisplayableDescription(keyword);
    if (simple) {
      Object panel = ui.loadComponentFromFile(UI_FILE_KEYWORDS_SIMPLE_VIEW, this);
      ui.add(divider, panel);

      // Fill every field
      Object tfKeyword = ui.find(panel, COMPONENT_TF_KEYWORD);
      Object lbKeywordDescription = ui.find(panel, COMPONENT_LB_KEYWORD_DESCRIPTION);
      ui.setEnabled(tfKeyword, false);
      ui.setText(tfKeyword, getDisplayableKeyword(keyword));

      // We display the keyword description in the panel
      if (keywordDescription != null && keywordDescription.length() > 0) {
        ui.setText(lbKeywordDescription, keywordDescription);
      } else {
        ui.remove(lbKeywordDescription);
      }

      // We have to set the text in case there is no join/leave keyword actions
      setJoinGroupDisplay(null);
      setLeaveGroupDisplay(null);

      for (KeywordAction action : actions) {
        KeywordAction.Type type = action.getType();
        if (type == KeywordAction.Type.REPLY) {
          Object cbReply = ui.find(panel, COMPONENT_CB_AUTO_REPLY);
          Object tfReply = ui.find(panel, COMPONENT_TF_AUTO_REPLY);
          ui.setSelected(cbReply, true);
          ui.setText(tfReply, action.getUnformattedReplyText());
        } else if (type == KeywordAction.Type.JOIN) {
          setJoinGroupDisplay(action.getGroup());
        } else if (type == KeywordAction.Type.LEAVE) {
          setLeaveGroupDisplay(action.getGroup());
        }
      }
    } else {
      Object panel = ui.loadComponentFromFile(UI_FILE_KEYWORDS_ADVANCED_VIEW, this);
      Object table = ui.find(panel, COMPONENT_ACTION_LIST);
      Object lbKeywordDescription = ui.find(panel, COMPONENT_LB_KEYWORD_DESCRIPTION);

      // We display the keyword description in the panel
      if (keywordDescription != null && keywordDescription.length() > 0) {
        ui.setText(lbKeywordDescription, keywordDescription);
      } else {
        ui.remove(lbKeywordDescription);
      }
      ui.setText(
          panel,
          InternationalisationUtils.getI18nString(
              COMMON_KEYWORD_ACTIONS_OF, getDisplayableKeyword(keyword)));
      // Fill every field
      for (KeywordAction action : actions) {
        ui.add(table, ui.getRow(action));
      }
      ui.add(divider, panel);
      enableKeywordActionFields(table, ui.find(panel, COMPONENT_KEY_ACT_PANEL));
    }
    enableKeywordFields(ui.find(COMPONENT_KEY_PANEL));
  }