< prev index next >

src/demo/share/java2d/J2DBench/src/j2dbench/Option.java

Print this page


   1 /*
   2  * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
   3  *
   4  * Redistribution and use in source and binary forms, with or without
   5  * modification, are permitted provided that the following conditions
   6  * are met:
   7  *
   8  *   - Redistributions of source code must retain the above copyright
   9  *     notice, this list of conditions and the following disclaimer.
  10  *
  11  *   - Redistributions in binary form must reproduce the above copyright
  12  *     notice, this list of conditions and the following disclaimer in the
  13  *     documentation and/or other materials provided with the distribution.
  14  *
  15  *   - Neither the name of Oracle nor the names of its
  16  *     contributors may be used to endorse or promote products derived
  17  *     from this software without specific prior written permission.
  18  *
  19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR


 106         return setValueFromString(value);
 107     }
 108 
 109     public abstract String setValueFromString(String value);
 110 
 111     public void write(PrintWriter pw) {
 112         //if (!isDefault()) {
 113             pw.println(getOptionString());
 114         //}
 115     }
 116 
 117     public String toString() {
 118         return "Option("+getOptionString()+")";
 119     }
 120 
 121     public static class Toggle extends Option {
 122         public static final int Off = 0;
 123         public static final int On = 1;
 124         public static final int Both = 2;
 125 
 126         private static final String valnames[] = {"Off", "On", "Both"};
 127         private static final Boolean valuelist[][] = {
 128             BooleanIterator.FalseList,
 129             BooleanIterator.TrueList,
 130             BooleanIterator.FalseTrueList,
 131         };
 132 
 133         int defaultvalue;
 134         int value;
 135         JPanel jp;
 136         JComboBox jcb;
 137 
 138         public Toggle(Group parent, String nodeName, String description,
 139                       int defaultvalue)
 140         {
 141             super(parent, nodeName, description);
 142             if (defaultvalue != Off &&
 143                 defaultvalue != On &&
 144                 defaultvalue != Both)
 145             {
 146                 throw new IllegalArgumentException("bad default");
 147             }


 403         public String setValueFromString(String value) {
 404             int val;
 405             try {
 406                 val = Integer.parseInt(value);
 407             } catch (NumberFormatException e) {
 408                 return "Value not an integer ("+value+")";
 409             }
 410             if (val < minvalue || val > maxvalue) {
 411                 return "Value out of range";
 412             }
 413             if (this.value != val) {
 414                 this.value = val;
 415                 updateGUI();
 416             }
 417             return null;
 418         }
 419     }
 420 
 421     public static class ObjectList extends Option {
 422         int size;
 423         String optionnames[];
 424         Object optionvalues[];
 425         String abbrevnames[];
 426         String descnames[];
 427         int defaultenabled;
 428         int enabled;
 429         JPanel jp;
 430         JList jlist;
 431         int numrows;
 432 
 433         public ObjectList(Group parent, String nodeName, String description,
 434                           String optionnames[],
 435                           Object optionvalues[],
 436                           String abbrevnames[],
 437                           String descnames[],
 438                           int defaultenabled)
 439         {
 440             this(parent, nodeName, description,
 441                  Math.min(Math.min(optionnames.length,
 442                                    optionvalues.length),
 443                           Math.min(abbrevnames.length,
 444                                    descnames.length)),
 445                  optionnames, optionvalues,
 446                  abbrevnames, descnames, defaultenabled);
 447         }
 448 
 449         public ObjectList(Group parent, String nodeName, String description,
 450                           int size,
 451                           String optionnames[],
 452                           Object optionvalues[],
 453                           String abbrevnames[],
 454                           String descnames[],
 455                           int defaultenabled)
 456         {
 457             super(parent, nodeName, description);
 458             this.size = size;
 459             this.optionnames = trim(optionnames, size);
 460             this.optionvalues = trim(optionvalues, size);
 461             this.abbrevnames = trim(abbrevnames, size);
 462             this.descnames = trim(descnames, size);
 463             this.enabled = this.defaultenabled = defaultenabled;
 464         }
 465 
 466         private static String[] trim(String list[], int size) {
 467             if (list.length == size) {
 468                 return list;
 469             }
 470             String newlist[] = new String[size];
 471             System.arraycopy(list, 0, newlist, 0, size);
 472             return newlist;
 473         }
 474 
 475         private static Object[] trim(Object list[], int size) {
 476             if (list.length == size) {
 477                 return list;
 478             }
 479             Object newlist[] = new Object[size];
 480             System.arraycopy(list, 0, newlist, 0, size);
 481             return newlist;
 482         }
 483 
 484         public void restoreDefault() {
 485             if (enabled != defaultenabled) {
 486                 enabled = defaultenabled;
 487                 updateGUI();
 488             }
 489         }
 490 
 491         public void updateGUI() {
 492             if (jlist != null) {
 493                 int enabled = this.enabled;
 494                 jlist.clearSelection();
 495                 for (int curindex = 0; curindex < size; curindex++) {
 496                     if ((enabled & (1 << curindex)) != 0) {
 497                         jlist.addSelectionInterval(curindex, curindex);
 498                     }
 499                 }


 587                         if (optionnames[i].equals(s)) {
 588                             enabled |= (1 << i);
 589                             s = null;
 590                             break;
 591                         }
 592                     }
 593                 } catch (NumberFormatException e) {
 594                 }
 595                 if (s != null) {
 596                     return "Bad value in list ("+s+")";
 597                 }
 598             }
 599             this.enabled = enabled;
 600             updateGUI();
 601             return null;
 602         }
 603     }
 604 
 605     public static class IntList extends ObjectList {
 606         public IntList(Group parent, String nodeName, String description,
 607                        int values[], String abbrevnames[], String descnames[],
 608                        int defaultenabled)
 609         {
 610             super(parent, nodeName, description,
 611                   makeNames(values), makeValues(values),
 612                   abbrevnames, descnames, defaultenabled);
 613         }
 614 
 615         private static String[] makeNames(int intvalues[]) {
 616             String names[] = new String[intvalues.length];
 617             for (int i = 0; i < intvalues.length; i++) {
 618                 names[i] = Integer.toString(intvalues[i]);
 619             }
 620             return names;
 621         }
 622 
 623         private static Object[] makeValues(int intvalues[]) {
 624             Object values[] = new Object[intvalues.length];
 625             for (int i = 0; i < intvalues.length; i++) {
 626                 values[i] = new Integer(intvalues[i]);
 627             }
 628             return values;
 629         }
 630     }
 631 
 632     public static class ObjectChoice extends Option {
 633          int size;
 634          String optionnames[];
 635          Object optionvalues[];
 636          String abbrevnames[];
 637          String descnames[];
 638          int defaultselected;
 639          int selected;
 640          JPanel jp;
 641          JComboBox jcombo;
 642 
 643          public ObjectChoice(Group parent, String nodeName, String description,
 644                              String optionnames[],
 645                              Object optionvalues[],
 646                              String abbrevnames[],
 647                              String descnames[],
 648                              int defaultselected)
 649          {
 650              this(parent, nodeName, description,
 651                   Math.min(Math.min(optionnames.length,
 652                                     optionvalues.length),
 653                            Math.min(abbrevnames.length,
 654                                     descnames.length)),
 655                   optionnames, optionvalues,
 656                   abbrevnames, descnames, defaultselected);
 657          }
 658 
 659          public ObjectChoice(Group parent, String nodeName, String description,
 660                              int size,
 661                              String optionnames[],
 662                              Object optionvalues[],
 663                              String abbrevnames[],
 664                              String descnames[],
 665                              int defaultselected)
 666          {
 667              super(parent, nodeName, description);
 668              this.size = size;
 669              this.optionnames = trim(optionnames, size);
 670              this.optionvalues = trim(optionvalues, size);
 671              this.abbrevnames = trim(abbrevnames, size);
 672              this.descnames = trim(descnames, size);
 673              this.selected = this.defaultselected = defaultselected;
 674          }
 675 
 676          private static String[] trim(String list[], int size) {
 677              if (list.length == size) {
 678                  return list;
 679              }
 680              String newlist[] = new String[size];
 681              System.arraycopy(list, 0, newlist, 0, size);
 682              return newlist;
 683          }
 684 
 685          private static Object[] trim(Object list[], int size) {
 686              if (list.length == size) {
 687                  return list;
 688              }
 689              Object newlist[] = new Object[size];
 690              System.arraycopy(list, 0, newlist, 0, size);
 691              return newlist;
 692          }
 693 
 694          public void restoreDefault() {
 695              if (selected != defaultselected) {
 696                  selected = defaultselected;
 697                  updateGUI();
 698              }
 699          }
 700 
 701          public void updateGUI() {
 702              if (jcombo != null) {
 703                  jcombo.setSelectedIndex(this.selected);
 704              }
 705          }
 706 
 707          public boolean isDefault() {
 708              return (selected == defaultselected);
 709          }


 782                      updateGUI();
 783                      return null;
 784                  }
 785              }
 786              return "Bad value";
 787          }
 788 
 789          public String setValueFromString(String value) {
 790              for (int i = 0; i < size; i++) {
 791                  if (optionnames[i].equals(value)) {
 792                      this.selected = i;
 793                      updateGUI();
 794                      return null;
 795                  }
 796              }
 797              return "Bad value";
 798          }
 799     }
 800 
 801     public static class BooleanIterator implements Modifier.Iterator {
 802         private Boolean list[];
 803         private int index;
 804 
 805         public static final Boolean FalseList[] = { Boolean.FALSE };
 806         public static final Boolean TrueList[] = { Boolean.TRUE };
 807         public static final Boolean
 808             FalseTrueList[] = { Boolean.FALSE, Boolean.TRUE };
 809         public static final Boolean
 810             TrueFalseList[] = { Boolean.TRUE, Boolean.FALSE };
 811 
 812         public BooleanIterator(boolean v) {
 813             this(v ? TrueList : FalseList);
 814         }
 815 
 816         public BooleanIterator(Boolean list[]) {
 817             this.list = list;
 818         }
 819 
 820         public boolean hasNext() {
 821             return (index < list.length);
 822         }
 823 
 824         public Object next() {
 825             if (index >= list.length) {
 826                 throw new NoSuchElementException();
 827             }
 828             return list[index++];
 829         }
 830 
 831         public void remove() {
 832             throw new UnsupportedOperationException();
 833         }
 834     }
 835 
 836     public static class SwitchIterator implements Modifier.Iterator {
 837         private Object list[];
 838         private int enabled;
 839         private int index;
 840 
 841         public SwitchIterator(Object[] list, int enabled) {
 842             this.list = list;
 843             this.enabled = enabled;
 844         }
 845 
 846         public boolean hasNext() {
 847             return ((1 << index) <= enabled);
 848         }
 849 
 850         public Object next() {
 851             while ((enabled & (1 << index)) == 0) {
 852                 index++;
 853                 if (index >= list.length) {
 854                     throw new NoSuchElementException();
 855                 }
 856             }
 857             return list[index++];
   1 /*
   2  * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
   3  *
   4  * Redistribution and use in source and binary forms, with or without
   5  * modification, are permitted provided that the following conditions
   6  * are met:
   7  *
   8  *   - Redistributions of source code must retain the above copyright
   9  *     notice, this list of conditions and the following disclaimer.
  10  *
  11  *   - Redistributions in binary form must reproduce the above copyright
  12  *     notice, this list of conditions and the following disclaimer in the
  13  *     documentation and/or other materials provided with the distribution.
  14  *
  15  *   - Neither the name of Oracle nor the names of its
  16  *     contributors may be used to endorse or promote products derived
  17  *     from this software without specific prior written permission.
  18  *
  19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR


 106         return setValueFromString(value);
 107     }
 108 
 109     public abstract String setValueFromString(String value);
 110 
 111     public void write(PrintWriter pw) {
 112         //if (!isDefault()) {
 113             pw.println(getOptionString());
 114         //}
 115     }
 116 
 117     public String toString() {
 118         return "Option("+getOptionString()+")";
 119     }
 120 
 121     public static class Toggle extends Option {
 122         public static final int Off = 0;
 123         public static final int On = 1;
 124         public static final int Both = 2;
 125 
 126         private static final String[] valnames = {"Off", "On", "Both"};
 127         private static final Boolean[][] valuelist = {
 128             BooleanIterator.FalseList,
 129             BooleanIterator.TrueList,
 130             BooleanIterator.FalseTrueList,
 131         };
 132 
 133         int defaultvalue;
 134         int value;
 135         JPanel jp;
 136         JComboBox jcb;
 137 
 138         public Toggle(Group parent, String nodeName, String description,
 139                       int defaultvalue)
 140         {
 141             super(parent, nodeName, description);
 142             if (defaultvalue != Off &&
 143                 defaultvalue != On &&
 144                 defaultvalue != Both)
 145             {
 146                 throw new IllegalArgumentException("bad default");
 147             }


 403         public String setValueFromString(String value) {
 404             int val;
 405             try {
 406                 val = Integer.parseInt(value);
 407             } catch (NumberFormatException e) {
 408                 return "Value not an integer ("+value+")";
 409             }
 410             if (val < minvalue || val > maxvalue) {
 411                 return "Value out of range";
 412             }
 413             if (this.value != val) {
 414                 this.value = val;
 415                 updateGUI();
 416             }
 417             return null;
 418         }
 419     }
 420 
 421     public static class ObjectList extends Option {
 422         int size;
 423         String[] optionnames;
 424         Object[] optionvalues;
 425         String[] abbrevnames;
 426         String[] descnames;
 427         int defaultenabled;
 428         int enabled;
 429         JPanel jp;
 430         JList jlist;
 431         int numrows;
 432 
 433         public ObjectList(Group parent, String nodeName, String description,
 434                           String[] optionnames,
 435                           Object[] optionvalues,
 436                           String[] abbrevnames,
 437                           String[] descnames,
 438                           int defaultenabled)
 439         {
 440             this(parent, nodeName, description,
 441                  Math.min(Math.min(optionnames.length,
 442                                    optionvalues.length),
 443                           Math.min(abbrevnames.length,
 444                                    descnames.length)),
 445                  optionnames, optionvalues,
 446                  abbrevnames, descnames, defaultenabled);
 447         }
 448 
 449         public ObjectList(Group parent, String nodeName, String description,
 450                           int size,
 451                           String[] optionnames,
 452                           Object[] optionvalues,
 453                           String[] abbrevnames,
 454                           String[] descnames,
 455                           int defaultenabled)
 456         {
 457             super(parent, nodeName, description);
 458             this.size = size;
 459             this.optionnames = trim(optionnames, size);
 460             this.optionvalues = trim(optionvalues, size);
 461             this.abbrevnames = trim(abbrevnames, size);
 462             this.descnames = trim(descnames, size);
 463             this.enabled = this.defaultenabled = defaultenabled;
 464         }
 465 
 466         private static String[] trim(String[] list, int size) {
 467             if (list.length == size) {
 468                 return list;
 469             }
 470             String[] newlist = new String[size];
 471             System.arraycopy(list, 0, newlist, 0, size);
 472             return newlist;
 473         }
 474 
 475         private static Object[] trim(Object[] list, int size) {
 476             if (list.length == size) {
 477                 return list;
 478             }
 479             Object[] newlist = new Object[size];
 480             System.arraycopy(list, 0, newlist, 0, size);
 481             return newlist;
 482         }
 483 
 484         public void restoreDefault() {
 485             if (enabled != defaultenabled) {
 486                 enabled = defaultenabled;
 487                 updateGUI();
 488             }
 489         }
 490 
 491         public void updateGUI() {
 492             if (jlist != null) {
 493                 int enabled = this.enabled;
 494                 jlist.clearSelection();
 495                 for (int curindex = 0; curindex < size; curindex++) {
 496                     if ((enabled & (1 << curindex)) != 0) {
 497                         jlist.addSelectionInterval(curindex, curindex);
 498                     }
 499                 }


 587                         if (optionnames[i].equals(s)) {
 588                             enabled |= (1 << i);
 589                             s = null;
 590                             break;
 591                         }
 592                     }
 593                 } catch (NumberFormatException e) {
 594                 }
 595                 if (s != null) {
 596                     return "Bad value in list ("+s+")";
 597                 }
 598             }
 599             this.enabled = enabled;
 600             updateGUI();
 601             return null;
 602         }
 603     }
 604 
 605     public static class IntList extends ObjectList {
 606         public IntList(Group parent, String nodeName, String description,
 607                        int[] values, String[] abbrevnames, String[] descnames,
 608                        int defaultenabled)
 609         {
 610             super(parent, nodeName, description,
 611                   makeNames(values), makeValues(values),
 612                   abbrevnames, descnames, defaultenabled);
 613         }
 614 
 615         private static String[] makeNames(int[] intvalues) {
 616             String[] names = new String[intvalues.length];
 617             for (int i = 0; i < intvalues.length; i++) {
 618                 names[i] = Integer.toString(intvalues[i]);
 619             }
 620             return names;
 621         }
 622 
 623         private static Object[] makeValues(int[] intvalues) {
 624             Object[] values = new Object[intvalues.length];
 625             for (int i = 0; i < intvalues.length; i++) {
 626                 values[i] = new Integer(intvalues[i]);
 627             }
 628             return values;
 629         }
 630     }
 631 
 632     public static class ObjectChoice extends Option {
 633          int size;
 634          String[] optionnames;
 635          Object[] optionvalues;
 636          String[] abbrevnames;
 637          String[] descnames;
 638          int defaultselected;
 639          int selected;
 640          JPanel jp;
 641          JComboBox jcombo;
 642 
 643          public ObjectChoice(Group parent, String nodeName, String description,
 644                              String[] optionnames,
 645                              Object[] optionvalues,
 646                              String[] abbrevnames,
 647                              String[] descnames,
 648                              int defaultselected)
 649          {
 650              this(parent, nodeName, description,
 651                   Math.min(Math.min(optionnames.length,
 652                                     optionvalues.length),
 653                            Math.min(abbrevnames.length,
 654                                     descnames.length)),
 655                   optionnames, optionvalues,
 656                   abbrevnames, descnames, defaultselected);
 657          }
 658 
 659          public ObjectChoice(Group parent, String nodeName, String description,
 660                              int size,
 661                              String[] optionnames,
 662                              Object[] optionvalues,
 663                              String[] abbrevnames,
 664                              String[] descnames,
 665                              int defaultselected)
 666          {
 667              super(parent, nodeName, description);
 668              this.size = size;
 669              this.optionnames = trim(optionnames, size);
 670              this.optionvalues = trim(optionvalues, size);
 671              this.abbrevnames = trim(abbrevnames, size);
 672              this.descnames = trim(descnames, size);
 673              this.selected = this.defaultselected = defaultselected;
 674          }
 675 
 676          private static String[] trim(String[] list, int size) {
 677              if (list.length == size) {
 678                  return list;
 679              }
 680              String[] newlist = new String[size];
 681              System.arraycopy(list, 0, newlist, 0, size);
 682              return newlist;
 683          }
 684 
 685          private static Object[] trim(Object[] list, int size) {
 686              if (list.length == size) {
 687                  return list;
 688              }
 689              Object[] newlist = new Object[size];
 690              System.arraycopy(list, 0, newlist, 0, size);
 691              return newlist;
 692          }
 693 
 694          public void restoreDefault() {
 695              if (selected != defaultselected) {
 696                  selected = defaultselected;
 697                  updateGUI();
 698              }
 699          }
 700 
 701          public void updateGUI() {
 702              if (jcombo != null) {
 703                  jcombo.setSelectedIndex(this.selected);
 704              }
 705          }
 706 
 707          public boolean isDefault() {
 708              return (selected == defaultselected);
 709          }


 782                      updateGUI();
 783                      return null;
 784                  }
 785              }
 786              return "Bad value";
 787          }
 788 
 789          public String setValueFromString(String value) {
 790              for (int i = 0; i < size; i++) {
 791                  if (optionnames[i].equals(value)) {
 792                      this.selected = i;
 793                      updateGUI();
 794                      return null;
 795                  }
 796              }
 797              return "Bad value";
 798          }
 799     }
 800 
 801     public static class BooleanIterator implements Modifier.Iterator {
 802         private Boolean[] list;
 803         private int index;
 804 
 805         public static final Boolean[] FalseList = { Boolean.FALSE };
 806         public static final Boolean[] TrueList = { Boolean.TRUE };
 807         public static final Boolean[]
 808             FalseTrueList = { Boolean.FALSE, Boolean.TRUE };
 809         public static final Boolean[]
 810             TrueFalseList = { Boolean.TRUE, Boolean.FALSE };
 811 
 812         public BooleanIterator(boolean v) {
 813             this(v ? TrueList : FalseList);
 814         }
 815 
 816         public BooleanIterator(Boolean[] list) {
 817             this.list = list;
 818         }
 819 
 820         public boolean hasNext() {
 821             return (index < list.length);
 822         }
 823 
 824         public Object next() {
 825             if (index >= list.length) {
 826                 throw new NoSuchElementException();
 827             }
 828             return list[index++];
 829         }
 830 
 831         public void remove() {
 832             throw new UnsupportedOperationException();
 833         }
 834     }
 835 
 836     public static class SwitchIterator implements Modifier.Iterator {
 837         private Object[] list;
 838         private int enabled;
 839         private int index;
 840 
 841         public SwitchIterator(Object[] list, int enabled) {
 842             this.list = list;
 843             this.enabled = enabled;
 844         }
 845 
 846         public boolean hasNext() {
 847             return ((1 << index) <= enabled);
 848         }
 849 
 850         public Object next() {
 851             while ((enabled & (1 << index)) == 0) {
 852                 index++;
 853                 if (index >= list.length) {
 854                     throw new NoSuchElementException();
 855                 }
 856             }
 857             return list[index++];
< prev index next >