< prev index next >

src/com/sun/javatest/tool/UIFactory.java

Print this page
rev 145 : 7902237: Fixing raw use of parameterized class
Reviewed-by: jjg


 395         Color c = Colors.getColorByPreferencesName(cs);
 396         b.setBackground(c);
 397         b.setSize(14, 14);
 398         b.setText(" ");
 399         b.setName(cs);
 400         if(l != null)
 401             b.addActionListener(l);
 402         if(label != null)
 403             label.setLabelFor(b);
 404         return b;
 405     }
 406 
 407     /**
 408      * Create a UIFactory object for a specific class.
 409      * The class is used to determine the resource bundle
 410      * for i18n strings; the bundle is named i18n.properties
 411      * in the same package as the specified class.
 412      * @param c the class used to determine the i18n properties
 413      * @param helpBroker the help broker to be used when creating help buttons
 414      */
 415     public UIFactory(Class c, HelpBroker helpBroker) {
 416         this(c, null, helpBroker);
 417     }
 418 
 419     /**
 420      * Create a UIFactory object for a specific component.
 421      * The component's class is used to determine the resource bundle
 422      * for i18n strings; the bundle is named i18n.properties
 423      * in the same package as the specified class.
 424      * @param c the component used to determine the i18n properties
 425      * @param helpBroker the help broker to be used when creating help buttons
 426      */
 427     public UIFactory(Component c, HelpBroker helpBroker) {
 428         this(c.getClass(), c, helpBroker);
 429     }
 430 
 431     /**
 432      * Create a UIFactory object for a specific class.
 433      * The class is used to determine the resource bundle
 434      * for i18n strings; the bundle is named i18n.properties
 435      * in the same package as the specified class.
 436      * @param c the class used to determine the i18n properties
 437      * @param p the parent component to be used for any dialogs that are created
 438      * @param helpBroker the help broker to be used when creating help buttons
 439      */
 440     public UIFactory(Class c, Component p, HelpBroker helpBroker) {
 441         this.helpBroker = helpBroker;
 442         clientClass = c;
 443         parent = p;
 444         i18n = I18NResourceBundle.getBundleForClass(c);
 445     }
 446 
 447     /**
 448      * Set the parent component to be used for dialogs created by this factory.
 449      * This setting cannot be changed after it is set.
 450      *
 451      * @param p The parent component, should not be null.
 452      */
 453     public void setDialogParent(Component p) {
 454         if (parent != null && parent != p)
 455             throw new IllegalStateException();
 456         parent = p;
 457     }
 458 
 459     /**
 460      * Get the screen resolution, in dots per inch, as provided


1144      * Create a choice item, using resources to specify the choices and the
1145      * tool tip. <br>
1146      * The resources used are:
1147      * <table>
1148      * <tr><td><i>uiKey</i>.<i>choiceKeys<sub>i</sub></i>.chc  <td>the choice to appear in the item, for 0 &lt;= i &lt; choiceKeys.length
1149      * <tr><td><i>uiKey</i>.name <td>the accessible name for the selector
1150      * <tr><td><i>uiKey</i>.tip  <td>the tool tip for the choice item
1151      * </table>
1152      * In addition, the name of the choice is set to <i>uiKey</i>.
1153      * Note: the choice item is created with the choices set to the names
1154      * of the resources used -- not the values. This means that the client can
1155      * examine and manipulate the choices, including the selected choice,
1156      * as location-independent resource names. A custom renderer is used to
1157      * ensure that the correctly localized value is displayed to the user.
1158      * @param uiKey the base name of the resources to be used for the menu
1159      * @param choiceKeys an array of strings used to construct the resource
1160      * names for the choices.
1161      * @return the choice item that was created
1162      * @see #createLiteralChoice
1163      */
1164     public JComboBox createChoice(final String uiKey, final String[] choiceKeys) {
1165         return createChoice(uiKey, choiceKeys, false);
1166     }
1167 
1168     /**
1169      * Same as the two parameter <code>createChoice</code>, except you can
1170      * make this an mutable choice component (freeform editing of the
1171      * response).  If the component is to be editable, an additional
1172      * <i>uiKey</i>.ed resource is needed to set the component name of the
1173      * editable field which will be onscreen.
1174      * @param uiKey the base name of the resources to be used for the menu
1175      * @param choiceKeys an array of strings used to construct the resource
1176      * names for the choices.
1177      * @param editable True if the choice component should allow freeform
1178      * editing of the response.
1179      * @return a choice box with the attributes indicated by the parameters
1180      * @see #createChoice(String,String[])
1181      */
1182     public JComboBox<String> createChoice(final String uiKey, final String[] choiceKeys, boolean editable) {
1183         // create a cache of the presentation string, for use when
1184         // rendering, but otherwise, let the JComboBox work in terms of the


1189 
1190         JComboBox<String> choice = new JComboBox<>(choiceKeys);
1191         choice.setName(uiKey);
1192         setToolTip(choice, uiKey);
1193         setAccessibleName(choice, uiKey);
1194 
1195         choice.setEditable(editable);
1196         if (editable) {
1197             Component editComp = choice.getEditor().getEditorComponent();
1198             if (editComp instanceof Accessible) {
1199                 if (editComp.getName() == null)
1200                     editComp.setName(uiKey + ".ed");
1201                 AccessibleContext ac = choice.getAccessibleContext();
1202                 AccessibleContext ed_ac = editComp.getAccessibleContext();
1203                 ed_ac.setAccessibleName(ac.getAccessibleName());
1204                 ed_ac.setAccessibleDescription(ac.getAccessibleDescription());
1205             }
1206         }
1207 
1208         choice.setRenderer(new DefaultListCellRenderer() {
1209             public Component getListCellRendererComponent(JList list, Object o, int index,
1210                                 boolean isSelected, boolean cellHasFocus) {
1211                 Object c = o;
1212                 for (int i = 0; i < choiceKeys.length; i++) {
1213                     if (choiceKeys[i] == o) {
1214                         c = choices[i];
1215                         break;
1216                     }
1217                 }
1218                 return super.getListCellRendererComponent(list, c, index, isSelected, cellHasFocus);
1219             }
1220         });
1221 
1222         return choice;
1223     }
1224 
1225     /**
1226      * Create an empty choice item, using a resource to specify the tool tip. <br>
1227      * The resource used is:
1228      * <table>
1229      * <tr><td><i>uiKey</i>.tip  <td>the tool tip for the choice item
1230      * </table>
1231      * In addition, the name of the choice is set to <i>uiKey</i>.
1232      * @param uiKey the base name of the resources to be used for the menu
1233      * @return the choice component that was created
1234      */
1235     public JComboBox createChoice(String uiKey) {
1236         return createChoice(uiKey, false);
1237     }
1238 
1239     /**
1240      * Same as single parameter version, except you can select a
1241      * component that allows freeform editing of the user's response.
1242      * @param uiKey the base name of the resources to be used for the menu
1243      * @param editable True if the user should be allowed to edit the
1244      * response.
1245      * @return the choice component that was created
1246      * @see #createChoice(String)
1247      */
1248     public JComboBox createChoice(String uiKey, boolean editable) {
1249         return createChoice(uiKey, editable, (JLabel) null);
1250     }
1251 
1252     /**
1253      * Same as the one parameter version, except a label can be
1254      * associated with this component.  This is to support accessibility.
1255      * @param uiKey the base name of the resources to be used for the menu
1256      * @param label Label to associate with this component
1257      * @return the choice component that was created
1258      * @see #createChoice(String)
1259      * @see javax.swing.JLabel#setLabelFor
1260      */
1261     public <E> JComboBox<E> createChoice(String uiKey, JLabel label) {
1262         return createChoice(uiKey, false, label);
1263     }
1264 
1265     /**
1266      * Combination of the two parameter methods, allowing you to select
1267      * a mutable response and associate a label.
1268      * @param uiKey the base name of the resources to be used for the menu
1269      * @param editable True if the user should be allowed to edit the


3535         clientClass = null;
3536         parent = null;
3537     }
3538 
3539     //----------------------------------------------------------------------------
3540 
3541     private static Font baseFont = new JLabel().getFont();
3542 
3543     private static final ActionListener closeListener = new ActionListener() {
3544             public void actionPerformed(ActionEvent e) {
3545                 Component src = (Component) (e.getSource());
3546                 for (Container p = src.getParent(); p != null; p = p.getParent()) {
3547                     if (p instanceof JInternalFrame || p instanceof Window) {
3548                         p.setVisible(false);
3549                         return;
3550                     }
3551                 }
3552             }
3553         };
3554 
3555     private Class clientClass;
3556     private Component parent;
3557     private I18NResourceBundle i18n;
3558     private HelpBroker helpBroker;
3559 
3560     private static I18NResourceBundle local_i18n = I18NResourceBundle.getBundleForClass(UIFactory.class);
3561     private static final int DOTS_PER_INCH = Toolkit.getDefaultToolkit().getScreenResolution();
3562 
3563     /**
3564      * Extension to the UIFactory that allows to use more than one resource
3565      * bundle. All methods accessing the resource bundle are overridden to
3566      * search for a resource in the alternative bundle first, and, if not found,
3567      * look up it in the original one.
3568      * <b>
3569      * This class might be helpful, when a component extends another components
3570      * from a different package.
3571      */
3572     public static class UIFactoryExt extends UIFactory {
3573         private I18NResourceBundle i18n_alt;
3574         private Class altClass;
3575 
3576         public UIFactoryExt(UIFactory uif, Class altClass) {
3577             super(uif.clientClass, uif.parent, uif.helpBroker);
3578             i18n_alt = I18NResourceBundle.getBundleForClass(altClass);
3579             this.altClass = altClass;
3580         }
3581 
3582         @Override
3583         public Color getI18NColor(String key) {
3584             if (!hasKey(i18n_alt,key)) {
3585                 return super.getI18NColor(key);
3586             }
3587             String value = i18n_alt.getString(key + ".clr");
3588             try {
3589                 if (value != null)
3590                     return Color.decode(value);
3591             }
3592             catch (Exception e) {
3593                 // ignore
3594             }
3595             return Color.BLACK;
3596 




 395         Color c = Colors.getColorByPreferencesName(cs);
 396         b.setBackground(c);
 397         b.setSize(14, 14);
 398         b.setText(" ");
 399         b.setName(cs);
 400         if(l != null)
 401             b.addActionListener(l);
 402         if(label != null)
 403             label.setLabelFor(b);
 404         return b;
 405     }
 406 
 407     /**
 408      * Create a UIFactory object for a specific class.
 409      * The class is used to determine the resource bundle
 410      * for i18n strings; the bundle is named i18n.properties
 411      * in the same package as the specified class.
 412      * @param c the class used to determine the i18n properties
 413      * @param helpBroker the help broker to be used when creating help buttons
 414      */
 415     public UIFactory(Class<?> c, HelpBroker helpBroker) {
 416         this(c, null, helpBroker);
 417     }
 418 
 419     /**
 420      * Create a UIFactory object for a specific component.
 421      * The component's class is used to determine the resource bundle
 422      * for i18n strings; the bundle is named i18n.properties
 423      * in the same package as the specified class.
 424      * @param c the component used to determine the i18n properties
 425      * @param helpBroker the help broker to be used when creating help buttons
 426      */
 427     public UIFactory(Component c, HelpBroker helpBroker) {
 428         this(c.getClass(), c, helpBroker);
 429     }
 430 
 431     /**
 432      * Create a UIFactory object for a specific class.
 433      * The class is used to determine the resource bundle
 434      * for i18n strings; the bundle is named i18n.properties
 435      * in the same package as the specified class.
 436      * @param c the class used to determine the i18n properties
 437      * @param p the parent component to be used for any dialogs that are created
 438      * @param helpBroker the help broker to be used when creating help buttons
 439      */
 440     public UIFactory(Class<?> c, Component p, HelpBroker helpBroker) {
 441         this.helpBroker = helpBroker;
 442         clientClass = c;
 443         parent = p;
 444         i18n = I18NResourceBundle.getBundleForClass(c);
 445     }
 446 
 447     /**
 448      * Set the parent component to be used for dialogs created by this factory.
 449      * This setting cannot be changed after it is set.
 450      *
 451      * @param p The parent component, should not be null.
 452      */
 453     public void setDialogParent(Component p) {
 454         if (parent != null && parent != p)
 455             throw new IllegalStateException();
 456         parent = p;
 457     }
 458 
 459     /**
 460      * Get the screen resolution, in dots per inch, as provided


1144      * Create a choice item, using resources to specify the choices and the
1145      * tool tip. <br>
1146      * The resources used are:
1147      * <table>
1148      * <tr><td><i>uiKey</i>.<i>choiceKeys<sub>i</sub></i>.chc  <td>the choice to appear in the item, for 0 &lt;= i &lt; choiceKeys.length
1149      * <tr><td><i>uiKey</i>.name <td>the accessible name for the selector
1150      * <tr><td><i>uiKey</i>.tip  <td>the tool tip for the choice item
1151      * </table>
1152      * In addition, the name of the choice is set to <i>uiKey</i>.
1153      * Note: the choice item is created with the choices set to the names
1154      * of the resources used -- not the values. This means that the client can
1155      * examine and manipulate the choices, including the selected choice,
1156      * as location-independent resource names. A custom renderer is used to
1157      * ensure that the correctly localized value is displayed to the user.
1158      * @param uiKey the base name of the resources to be used for the menu
1159      * @param choiceKeys an array of strings used to construct the resource
1160      * names for the choices.
1161      * @return the choice item that was created
1162      * @see #createLiteralChoice
1163      */
1164     public JComboBox<String> createChoice(final String uiKey, final String[] choiceKeys) {
1165         return createChoice(uiKey, choiceKeys, false);
1166     }
1167 
1168     /**
1169      * Same as the two parameter <code>createChoice</code>, except you can
1170      * make this an mutable choice component (freeform editing of the
1171      * response).  If the component is to be editable, an additional
1172      * <i>uiKey</i>.ed resource is needed to set the component name of the
1173      * editable field which will be onscreen.
1174      * @param uiKey the base name of the resources to be used for the menu
1175      * @param choiceKeys an array of strings used to construct the resource
1176      * names for the choices.
1177      * @param editable True if the choice component should allow freeform
1178      * editing of the response.
1179      * @return a choice box with the attributes indicated by the parameters
1180      * @see #createChoice(String,String[])
1181      */
1182     public JComboBox<String> createChoice(final String uiKey, final String[] choiceKeys, boolean editable) {
1183         // create a cache of the presentation string, for use when
1184         // rendering, but otherwise, let the JComboBox work in terms of the


1189 
1190         JComboBox<String> choice = new JComboBox<>(choiceKeys);
1191         choice.setName(uiKey);
1192         setToolTip(choice, uiKey);
1193         setAccessibleName(choice, uiKey);
1194 
1195         choice.setEditable(editable);
1196         if (editable) {
1197             Component editComp = choice.getEditor().getEditorComponent();
1198             if (editComp instanceof Accessible) {
1199                 if (editComp.getName() == null)
1200                     editComp.setName(uiKey + ".ed");
1201                 AccessibleContext ac = choice.getAccessibleContext();
1202                 AccessibleContext ed_ac = editComp.getAccessibleContext();
1203                 ed_ac.setAccessibleName(ac.getAccessibleName());
1204                 ed_ac.setAccessibleDescription(ac.getAccessibleDescription());
1205             }
1206         }
1207 
1208         choice.setRenderer(new DefaultListCellRenderer() {
1209             public Component getListCellRendererComponent(JList<?> list, Object o, int index,
1210                                 boolean isSelected, boolean cellHasFocus) {
1211                 Object c = o;
1212                 for (int i = 0; i < choiceKeys.length; i++) {
1213                     if (choiceKeys[i] == o) {
1214                         c = choices[i];
1215                         break;
1216                     }
1217                 }
1218                 return super.getListCellRendererComponent(list, c, index, isSelected, cellHasFocus);
1219             }
1220         });
1221 
1222         return choice;
1223     }
1224 
1225     /**
1226      * Create an empty choice item, using a resource to specify the tool tip. <br>
1227      * The resource used is:
1228      * <table>
1229      * <tr><td><i>uiKey</i>.tip  <td>the tool tip for the choice item
1230      * </table>
1231      * In addition, the name of the choice is set to <i>uiKey</i>.
1232      * @param uiKey the base name of the resources to be used for the menu
1233      * @return the choice component that was created
1234      */
1235     public <E> JComboBox<E> createChoice(String uiKey) {
1236         return createChoice(uiKey, false);
1237     }
1238 
1239     /**
1240      * Same as single parameter version, except you can select a
1241      * component that allows freeform editing of the user's response.
1242      * @param uiKey the base name of the resources to be used for the menu
1243      * @param editable True if the user should be allowed to edit the
1244      * response.
1245      * @return the choice component that was created
1246      * @see #createChoice(String)
1247      */
1248     public <E> JComboBox<E> createChoice(String uiKey, boolean editable) {
1249         return createChoice(uiKey, editable, null);
1250     }
1251 
1252     /**
1253      * Same as the one parameter version, except a label can be
1254      * associated with this component.  This is to support accessibility.
1255      * @param uiKey the base name of the resources to be used for the menu
1256      * @param label Label to associate with this component
1257      * @return the choice component that was created
1258      * @see #createChoice(String)
1259      * @see javax.swing.JLabel#setLabelFor
1260      */
1261     public <E> JComboBox<E> createChoice(String uiKey, JLabel label) {
1262         return createChoice(uiKey, false, label);
1263     }
1264 
1265     /**
1266      * Combination of the two parameter methods, allowing you to select
1267      * a mutable response and associate a label.
1268      * @param uiKey the base name of the resources to be used for the menu
1269      * @param editable True if the user should be allowed to edit the


3535         clientClass = null;
3536         parent = null;
3537     }
3538 
3539     //----------------------------------------------------------------------------
3540 
3541     private static Font baseFont = new JLabel().getFont();
3542 
3543     private static final ActionListener closeListener = new ActionListener() {
3544             public void actionPerformed(ActionEvent e) {
3545                 Component src = (Component) (e.getSource());
3546                 for (Container p = src.getParent(); p != null; p = p.getParent()) {
3547                     if (p instanceof JInternalFrame || p instanceof Window) {
3548                         p.setVisible(false);
3549                         return;
3550                     }
3551                 }
3552             }
3553         };
3554 
3555     private Class<?> clientClass;
3556     private Component parent;
3557     private I18NResourceBundle i18n;
3558     private HelpBroker helpBroker;
3559 
3560     private static I18NResourceBundle local_i18n = I18NResourceBundle.getBundleForClass(UIFactory.class);
3561     private static final int DOTS_PER_INCH = Toolkit.getDefaultToolkit().getScreenResolution();
3562 
3563     /**
3564      * Extension to the UIFactory that allows to use more than one resource
3565      * bundle. All methods accessing the resource bundle are overridden to
3566      * search for a resource in the alternative bundle first, and, if not found,
3567      * look up it in the original one.
3568      * <b>
3569      * This class might be helpful, when a component extends another components
3570      * from a different package.
3571      */
3572     public static class UIFactoryExt extends UIFactory {
3573         private I18NResourceBundle i18n_alt;
3574         private Class<?> altClass;
3575 
3576         public UIFactoryExt(UIFactory uif, Class<?> altClass) {
3577             super(uif.clientClass, uif.parent, uif.helpBroker);
3578             i18n_alt = I18NResourceBundle.getBundleForClass(altClass);
3579             this.altClass = altClass;
3580         }
3581 
3582         @Override
3583         public Color getI18NColor(String key) {
3584             if (!hasKey(i18n_alt,key)) {
3585                 return super.getI18NColor(key);
3586             }
3587             String value = i18n_alt.getString(key + ".clr");
3588             try {
3589                 if (value != null)
3590                     return Color.decode(value);
3591             }
3592             catch (Exception e) {
3593                 // ignore
3594             }
3595             return Color.BLACK;
3596 


< prev index next >