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

📄 optionizer.java

📁 ErGo是一个很早的Java通用围棋服务器(IGS/NNGS)客户端程序。有全部源码和文档
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      if (f != null)
	return f.getFont();
    }
    catch (Exception e) {
      Debug.backtrace(e);
    }
    //    System.out.println("gFO "+fm.getDefaultFont());
    return FontManager.getDefaultFont();
  }


  // Not sure it makes sense for this to be part of optionizer. (?)  -sigue
  // Err, I thought of it when I was writing the optionizer... :) - AMB
  class Cascader {
    private final int firsty = 0;
    private final int firstx = 0;
    private final int diaginc = 50;
    private final int xinc = 100;
    // These two are the state of the system.
    private int currx = firstx;
    private int lastnum = -1;

    private void setToPos (Point p) {
      p.x = currx + lastnum * diaginc; p.y = firsty + lastnum * diaginc;
    }
    public Point getNext (Dimension d) {
      // No hyper-default supplied for size.
      ++lastnum;
      Point p = new Point();
      setToPos(p);
      if (d.height + p.y > desktopSize.height) { // bounce horiz along
	lastnum = 0;
	currx += xinc;
      }
      setToPos(p);
      if (d.width + p.x > desktopSize.width) { // big horiz back.
	currx = firstx;
        lastnum =0;
      }
      setToPos(p);
      return p;
    }
  }
  Cascader cascader = new Cascader();
  // given window dimensions, get next valid cascade position.
  public Point getNextCascade(Dimension d) {
    return cascader.getNext(d);
  }


  public Dimension getSizeOption(String keyword) {
    Dimension togo = null;
    try {
      togo = (Dimension) getOption(keyword);
    }
    catch (Exception e) {
      Debug.backtrace(e);
    }
    if (togo == null) return null;
    if (togo.width > desktopSize.width) togo.width = desktopSize.width;
    if (togo.height > desktopSize.height) togo.height = desktopSize.height;
    return togo;
  }

  public Point getPointOption(String keyword) {
    Point togo = null;
    try {
      togo = (Point) getOption(keyword);
    }
    catch (Exception e) {
      Debug.backtrace(e);
    }
    if (togo == null) return new Point(0,0);
    return togo;
  }

  public Rectangle getRectangleOption (String keyword) {
    Rectangle togo = null;
    try {
      togo = (Rectangle) getOption(keyword);
    }
    catch (Exception e) {
      Debug.backtrace(e);
    }
    if (togo == null) return null;
    return togo.intersection(desktopRect);
  }

  // handleString differs slightly in that it quietly returns an
  // empty string, in the case storage is null, or nonexistent.
  // current reason is so that passwords are set to blank in the
  // case nothing is in the ini file, but this bears some
  // thinking about.

  public String getStringOption (String keyword) {
    try {
      String s = (String) getOption(keyword);
      if (s == null) return "";
      else return s;
    }
    catch (ErgoException e) {	// quietly ignore.
    }
    catch (Exception e) {
      Debug.backtrace(e);
    }
    return "";
  }


  // Get the multipicity (number of values stored) for this option.
  public int getOptionSize (String keyword) throws ErgoException {
    OptionStorage os = lookup(keyword);
    if (os == null)
      throw new ErgoException("Option "+keyword+" not known in size.");
    return os.size();
  }

  
  // Called when option is first seen. Associates it with its storage.
  private OptionStorage allocateQuietly (String keyword, String externalkeyword) {
    OptionStorage os = new OptionStorage(externalkeyword);
    optionmap.put(keyword.toLowerCase(), os);
    return os;
  }

  /**
   * Three functions: i) informs Optionizer of the type of the option,
   * ii) registers observer (type Optionizable) to listen for changes
   * in option value. Perhaps separate these functions in future.
   * iii) supply default value for option, if it is not in ini file.
   * Note that getXxxxOption functions supply "hyper-defaults" if necessary.
   * getSizeOption is unique in that it may return null.
   *
   * We may pass null as the third parameter, in case we are not interested
   * in being called back. 
   *
   * If we supply inconsistent values for default, latest will be used, 
   * however, no default values are ever written to ini file.
   *
   * If we supply null default value, previous is unchanged.
   */
  public void expressOwnership (String keyword, int type, Optionizable ops,
				Object defaultvalue) {
    //    System.out.println("eO "+keyword+" "+type+" "+defaultvalue);
    OptionStorage storage = lookup(keyword);
    if (storage == null) {
      storage = allocateQuietly(keyword, null);
      storage.applyData(0, defaultvalue);
      storage.type = type;
      //updateOption(keyword, defaultvalue);
    }
    else {  // we had in fact seen the option before. Ignore the supplied value.
      if (storage.type == TYPE_UNKNOWN) { // it has value from the ini file.
	storage.type = type;
	// on the spot, reswizz the string into its Object form.
	for (int i = 0; i < storage.size(); ++i) {
	  if (storage.dataAt(i) != null) {
	    try {
	      storage.sosAt(i).data = 
		handlers[type].handle(keyword, (String)storage.dataAt(i));  
	    }
	    catch (ParseException p) {
	      Debug.backtrace(p);
	    }
	    // Probably do not want to broadcast at this point,
	    // since we will be in the middle of the constructor of some
	    // poor unfortunate fool....
	    //updateOption(keyword, storage.dataAt(i));
	  }			// end if there's some data here.
	}
      }
      else {  // we've seen it, and someone has already grabbed it...!
	Debug.asser(storage.type == type, 
		     "Inconsistent types requested for options " + keyword);
      }
    }				// end if option already known
    // If, after all that, element 0 is still not set, set it to the
    // supplied value.
    if (storage.sosAt(0).data == null) {
      storage.sosAt(0).data = defaultvalue;
    }
    // If we managed to get here, storage must be the legitimate target
    // of ops. Or rather, the other way round.... :)
    if (ops != null) 
      storage.targets.addElement(ops);
  }

  /** Used in "destructor" of observer, to deregister it for broadcast events. */
  public void disclaimOwnership (String keyword, Optionizable opt) {
    OptionStorage os = lookup(keyword);
    os.targets.removeElement(opt);
  }

  // Handler Inner Classes. One for each supported data type.
  // May also provide getXxxxOption access functions also for a type, for convenience.

  class handleInteger extends OptionHandler {
    public Object handle (String keyword, String value) throws ParseException {
      ParsedMessage p = new ParsedMessage(value, " %i");
      Integer i = (Integer)p.matchAt(0);
      return i;
    }
  }

  class handleBoolean extends OptionHandler {
    public Object handle (String keyword, String value) throws ParseException { 
      ParsedMessage p = new ParsedMessage(value, " %s");
      String s = p.stringAt(0);
      Boolean b;
      if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("on") ||
	  s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y"))
	b = T;
      else
	b = F;
      return b;
    }
  }

  // Format: NICKNAME DOMAIN-NAME PORT-NUMBER TYPE
  class handleServer extends OptionHandler {
    public Object handle (String keyword, String value) throws ParseException { 
      ParsedMessage p = new ParsedMessage(value, " %s %s %i %s");
      // Say goodye to your pain in the butt....! :)
      GoServer g = new GoServer(p.stringAt(1), p.intAt(2),
				p.stringAt(0), p.stringAt(3)); 
      return g;
    }
    public String render (Object o) {
      GoServer g = (GoServer) o;
      return g.shortName() + s + g.name + s + g.port + s + g.typeString();
    }
  }

  // Note: Color option will eventually have ColorSpec, analogous to
  // FontSpec, to indicate Desktop colors, and fixed colors, etc.

  class handleColor extends OptionHandler {
    public Object handle (String keyword, String value) throws ParseException {
      ColorSpec c = new ColorSpec(value);
      return c;
    }
    public String render (Object o) {
      ColorSpec c = (ColorSpec) o;
      return c.render();
//      return c.getRed() + s + c.getGreen() + s + c.getBlue();
    }
  }

  class handleFont extends OptionHandler {
    public Object handle (String keyword, String value) throws ParseException { 
      ParsedMessage p = new ParsedMessage(value, " %s %s %s");
      FontSpec f = new FontSpec(p.stringAt(0), p.stringAt(1),
				p.stringAt(2));
      //      System.out.println("Fillin: "+keyword+" "+f);
      if (keyword.equalsIgnoreCase(defaultString))
        FontManager.setDefaultFont(f.getFont());
      return f;
    }
  }

  class handleSize extends OptionHandler {
    public Object handle (String keyword, String value) throws ParseException { 
      ParsedMessage p = new ParsedMessage(value, " %i %i");
      Dimension d = new Dimension(p.intAt(0), p.intAt(1));
      return d;
    }
    public String render(Object o) {
      Dimension d = (Dimension) o;
      return d.width + s + d.height;
    }
  }

  class handlePosition extends OptionHandler {
    public Object handle (String keyword, String value) throws ParseException { 
      ParsedMessage p = new ParsedMessage(value, " %i %i");
      Point point = new Point(p.intAt(0), p.intAt(1));
      return point;
    }
    public String render(Object o) {
      Point p = (Point) o;
      return p.x + s + p.y;
    }
  }

  class handleRectangle extends OptionHandler {
    public Object handle (String keyword, String value) throws ParseException { 
      ParsedMessage p = new ParsedMessage(value, " %i %i %i $i");
      Rectangle r = new Rectangle(p.intAt(0), p.intAt(1), 
                                  p.intAt(2), p.intAt(3));
      return r;
    }
    public String render (Object o) {
      Rectangle r = (Rectangle) o;
      return r.x + s + r.y + s + r.width + r.height;
    }
  }


  class handleString extends OptionHandler {
    public Object handle (String keyword, String value) throws ParseException { 
      ParsedMessage p = new ParsedMessage(value, " %s");
      return p.stringAt(0);
    }
  }

}    // end class Optionizer





// Currently superfluous class to assemble entire collection of system colors.
// Will be used when notebook config dialog is done.
/*
class SystemAssembler {
  Hashtable colorLook = new Hashtable();
  SystemAssembler() {
    SystemColor s = SystemColor.desktop;
    System.out.println(s);
    try {
      Class SysColClas = Class.forName("java.awt.SystemColor");
      Field[] fields = SysColClas.getDeclaredFields();
      for (int i = 0; i < fields.length; ++i) {
        String name = fields[i].getName();
	System.out.println(name);
	for (int j=0; j<name.length(); ++j) {
	  char c = name.charAt(j);
	  if (Character.isLowerCase(c)) {
	    try {
	      colorLook.put(name, fields[i].get(null));
	      Debug.println("got");
	    }
            catch (Exception e) {
	      Debug.backtrace(e);
	    }
	    break;
	  }
	}
      }
    }
    catch (Exception e) {}
  }
  public static void main(String[] argv) {
    SystemAssembler sa = new SystemAssembler();
  }
}
*/

⌨️ 快捷键说明

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