⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 optionwidget.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:
/**
 * The contents of this file are subject to the OAA  Community Research
 * License Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License
 * at http://www.ai.sri.com/~oaa/.  Software distributed under the License
 * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * rights and limitations under the License.  Portions of the software are
 * Copyright (c) SRI International, 1999-2003.  All rights reserved.
 * "OAA" is a registered trademark, and "Open Agent Architecture" is a
 * trademark, of SRI International, a California nonprofit public benefit
 * corporation.
*/

package com.sri.oaa2.agt.startit;

import java.util.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class OptionWidget {

  private final JComponent leftSide;
  private final OptionValueWidget rightSide;

  private final String OVERRIDE = "OVERRIDE";
  private final String DEFAULT = "DEFAULT";

  private OptionInfo info;

  public OptionWidget(OptionInfo inf) {
    info = inf;
    // Create the RHS widget
    switch(info.type) {
    case OptionInfo.TOGGLE:
      rightSide = new OptionSwapper(new LabelWidget(info.falseLabel,
						    info.falseValue),
				    new LabelWidget(info.trueLabel,
						    info.trueValue),
				    info.set);
      break;
    case OptionInfo.TOGGLE_TEXT:
      rightSide = new OptionSwapper(new LabelWidget(info.falseLabel,
						    info.falseValue),
				    new TextWidget(info.value, info.size),
				    info.set);
      break;
    case OptionInfo.TOGGLE_TEXT_MENU:
      rightSide = new OptionSwapper(new LabelWidget(info.falseLabel,
						    info.falseValue),
				    new TextMenuWidget(info.options,
						       info.value),
				    info.set);
      
      break;
    case OptionInfo.TEXT_MENU:
      rightSide = new TextMenuWidget(info.options, info.value);
      break;
    case OptionInfo.MENU:
      rightSide = new MenuWidget(info.options, info.defaultOption);
      break;
    case OptionInfo.TEXT:
      rightSide = new TextWidget(info.value, info.size);
      break;
    default:
      rightSide = new LabelWidget("RHS", "RHS");
    }

    // Create the LHS widget
    switch(info.type) {
    case OptionInfo.TOGGLE:
    case OptionInfo.TOGGLE_TEXT:
    case OptionInfo.TOGGLE_TEXT_MENU:
      final JCheckBox toggle = new JCheckBox(info.label, info.set);
      toggle.setSelected(info.set);
      toggle.addActionListener(new ActionListener() {
	  public void actionPerformed(ActionEvent e) {
	    ((OptionSwapper)rightSide).showDefault(!toggle.isSelected());
	  }
	});
      leftSide = toggle;
      break;
    default:
      leftSide = new JLabel(info.label);
    }
  }

  public JComponent getLeft() {
    return (JComponent) leftSide;
  }
  public JComponent getRight() {
    return (JComponent) rightSide;
  }
  public void updateInfo() {
    // 1: any kind of TOGGLE* has to update "set"
    if (leftSide instanceof JCheckBox) {
      info.set = ((JCheckBox) leftSide).isSelected();
    }
    // 2: any kind of *TEXT* has to update "value"
    if (info.type == OptionInfo.TOGGLE_TEXT ||
	info.type == OptionInfo.TOGGLE_TEXT_MENU) {
      info.value = ((OptionSwapper) rightSide).overrideW.getOptionValue();
    }
    if (info.type == OptionInfo.TEXT ||
	info.type == OptionInfo.TEXT_MENU) {
      info.value = rightSide.getOptionValue();
    }
    // 3: MENU's have to update the currentOption
    if (info.type == OptionInfo.MENU) {
      info.currentOption = (OptionInfo.MenuOption) ((JComboBox) rightSide).getSelectedItem();
    }
  }

  private interface OptionValueWidget {
    public String getOptionValue();
  }

  private class OptionSwapper extends JPanel implements OptionValueWidget {
    OptionValueWidget onTop;
    OptionValueWidget defaultW, overrideW;
    CardLayout layout = new CardLayout();

    public OptionSwapper(OptionValueWidget dw, OptionValueWidget ow, boolean set) {
      defaultW = dw;
      overrideW = ow;
      setLayout(layout);
      add((JComponent)defaultW, DEFAULT);
      add((JComponent)overrideW, OVERRIDE);
      showDefault(!set);
    }
    public void showDefault(boolean de) {
      layout.show(this, de ? DEFAULT : OVERRIDE);
      onTop = (de ? defaultW : overrideW);
    }
    public String getOptionValue() {
      return (onTop.getOptionValue());
    }
  }
  protected static class LabelWidget extends JLabel implements OptionValueWidget {
    String value;
    public LabelWidget(String label, String v) {
      super(label);
      value = v;
    }
    public String getOptionValue() { return value; }
  }
  protected static class MenuWidget extends JComboBox implements OptionValueWidget {
    public MenuWidget(Vector v, Object selected) {
      super(v);
      if (selected != null) setSelectedItem(selected);
    }
    public String getOptionValue() { return getSelectedItem().toString(); }
  }
  protected static class TextMenuWidget extends JComboBox implements OptionValueWidget {
    public TextMenuWidget(Vector v) {
      this(v, null);
    }
    public TextMenuWidget(Vector v, Object contents) {
      super(v);
      setFont(getFont().deriveFont(Font.PLAIN));
      setEditable(true);
      setSelectedItem(null);
      if (contents != null) setOptionValue(contents.toString());
    }
    public String getOptionValue() { return getEditor().getItem().toString(); }
    public void   setOptionValue(String v) { getEditor().setItem(v); }
  }
  protected static class TextWidget extends JTextField implements OptionValueWidget {
    public TextWidget(String text, int size) {
      super(text);
      if (size > 0) setColumns(size);
    }
    public String getOptionValue() { return getText(); }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -