/** @see org.openquark.gems.client.valueentry.ValueEditor#setInitialValue() */
  @Override
  public void setInitialValue() {

    IntellicutAdapter adapter = new IntellicutAdapter();

    adapter.setNamingPolicy(
        new UnqualifiedUnlessAmbiguous(
            valueEditorManager.getPerspective().getWorkingModuleTypeInfo()));

    intellicutPanel = new IntellicutPanel(this, adapter, null, IntellicutMode.NOTHING);

    intellicutPanel.loadListModel();
    intellicutPanel.setBorder(null);
    intellicutPanel.setMoveable(false);

    // Select the correct item in the list.
    GemEntity gemEntity = (GemEntity) getValueNode().getValue();
    IntellicutListEntry listEntry = adapter.getListEntryForData(gemEntity);

    if (listEntry != null) {
      intellicutPanel.getIntellicutList().setSelectedValue(listEntry, true);
    } else {
      intellicutPanel.getIntellicutList().clearSelection();
    }

    add(intellicutPanel, BorderLayout.CENTER);
    setSize(getPreferredSize());

    // Add a dummy MouseListener so that Swing doesn't forward mouse events intended for
    // us to the component below us. Swing does that if a component has no mouse listeners at all.
    addMouseListener(new MouseAdapter() {});
  }
  /** {@inheritDoc} */
  @Override
  public void setInitialValue() {

    setLayout(new BorderLayout());
    setResizable(true);

    IntellicutAdapter adapter = new IntellicutAdapter();
    adapter.setNamingPolicy(
        new UnqualifiedUnlessAmbiguous(
            valueEditorManager.getPerspective().getWorkingModuleTypeInfo()));

    intellicutPanel = new IntellicutPanel(this, adapter, null, IntellicutMode.NOTHING);
    intellicutPanel.loadListModel();
    intellicutPanel.setBorder(null);
    intellicutPanel.setMoveable(false);
    intellicutPanel
        .getIntellicutList()
        .setCellRenderer(new ParametricValueListRenderer(IntellicutMode.NOTHING));

    // By default there is no item selected.
    intellicutPanel.getIntellicutList().clearSelection();

    add(intellicutPanel, BorderLayout.CENTER);

    resetSize();
  }
  @Override
  public void commitValue() {

    IntellicutListEntry listEntry =
        (IntellicutListEntry) intellicutPanel.getIntellicutList().getSelectedValue();

    if (listEntry != null) {

      GemEntity gemEntity = (GemEntity) listEntry.getData();
      ModuleTypeInfo currentModuleInfo =
          valueEditorManager.getPerspective().getWorkingModuleTypeInfo();
      TypeExpr valueNodeType = getValueNode().getTypeExpr();
      TypeExpr functionType = gemEntity.getTypeExpr();
      TypeExpr unifiedType;

      try {
        unifiedType = TypeExpr.unify(valueNodeType, functionType, currentModuleInfo);
      } catch (TypeException e) {
        throw new IllegalStateException(e.getMessage());
      }

      GemEntityValueNode newValueNode = new GemEntityValueNode(gemEntity, unifiedType);
      replaceValueNode(newValueNode, true);
    }

    super.commitValue();
  }
  /** Resets the current size and minimum resize dimension of the editor. */
  protected final void resetSize() {

    int size = intellicutPanel.getIntellicutListModel().getSize();

    if (size > 0) {

      // If the list is displayed, then setup some sizes.

      // Set the minimum resize dimension to a height of 2 item.
      intellicutPanel.setPreferredVisibleRows(2);
      setMinResizeDimension(getPreferredSize());
      setMaxResizeDimension(new Dimension(getPreferredSize().width, 2048));
      setResizable(true);

      // Set the preferred list height to max 10 rows
      intellicutPanel.setPreferredVisibleRows(size >= 10 ? 10 : size + 1);

    } else {
      setResizable(false);
    }

    setSize(getPreferredSize());
  }
  /** {@inheritDoc} */
  @Override
  protected void commitValue() {

    IntellicutListEntry listEntry =
        (IntellicutListEntry) intellicutPanel.getIntellicutList().getSelectedValue();

    if (listEntry != null) {

      // Note: For TypeExpr which contains uninstantiated TypeVars, we must 'duplicate' them
      // somehow.
      TypeExpr instanceTypeExpr = ((TypeExpr) listEntry.getData()).copyTypeExpr();
      ValueNode replacement =
          getOwnerValueNode()
              .transmuteValueNode(
                  valueEditorManager.getValueNodeBuilderHelper(),
                  valueEditorManager.getValueNodeTransformer(),
                  instanceTypeExpr);

      // Get the TypeVar that we are instantiating.
      replaceValueNode(replacement, true);

      notifyValueCommitted();
    }
  }
 /** @see org.openquark.gems.client.valueentry.ValueEditor#getDefaultFocusComponent() */
 @Override
 public Component getDefaultFocusComponent() {
   return intellicutPanel.getIntellicutList();
 }