/** Builds the Language panel. */
 protected JPanel buildLanguagePanel() {
   JPanel p = new JPanel();
   p.setBorder(BorderFactory.createEmptyBorder(16, 16, 16, 16));
   languagePanel = new LanguageDialog.Panel();
   languagePanel.setBorder(BorderFactory.createEmptyBorder());
   Color c = UIManager.getColor("Window.background");
   languagePanel.getComponent(0).setBackground(c);
   languagePanel.getComponent(1).setBackground(c);
   p.add(languagePanel);
   return p;
 }
 /** Stores the current settings in the PreferenceManager model. */
 protected void savePreferences() {
   model.setString(PREFERENCE_KEY_LANGUAGES, languagePanel.getLanguages());
   model.setString(PREFERENCE_KEY_USER_STYLESHEET, userStylesheet.getText());
   model.setBoolean(PREFERENCE_KEY_USER_STYLESHEET_ENABLED, userStylesheetEnabled.isSelected());
   model.setBoolean(PREFERENCE_KEY_SHOW_RENDERING, showRendering.isSelected());
   model.setBoolean(PREFERENCE_KEY_AUTO_ADJUST_WINDOW, autoAdjustWindow.isSelected());
   model.setBoolean(PREFERENCE_KEY_ENABLE_DOUBLE_BUFFERING, enableDoubleBuffering.isSelected());
   model.setBoolean(PREFERENCE_KEY_SHOW_DEBUG_TRACE, showDebugTrace.isSelected());
   model.setBoolean(PREFERENCE_KEY_SELECTION_XOR_MODE, selectionXorMode.isSelected());
   model.setBoolean(PREFERENCE_KEY_IS_XML_PARSER_VALIDATING, isXMLParserValidating.isSelected());
   model.setBoolean(PREFERENCE_KEY_ENFORCE_SECURE_SCRIPTING, enforceSecureScripting.isSelected());
   model.setBoolean(PREFERENCE_KEY_GRANT_SCRIPT_FILE_ACCESS, grantScriptFileAccess.isSelected());
   model.setBoolean(
       PREFERENCE_KEY_GRANT_SCRIPT_NETWORK_ACCESS, grantScriptNetworkAccess.isSelected());
   model.setBoolean(PREFERENCE_KEY_LOAD_JAVA, loadJava.isSelected());
   model.setBoolean(PREFERENCE_KEY_LOAD_ECMASCRIPT, loadEcmascript.isSelected());
   int i;
   switch (allowedScriptOrigin.getSelectedIndex()) {
     case 0:
       i = ResourceOrigin.ANY;
       break;
     case 1:
       i = ResourceOrigin.DOCUMENT;
       break;
     case 2:
       i = ResourceOrigin.EMBEDED;
       break;
       // case 3:
     default:
       i = ResourceOrigin.NONE;
       break;
   }
   model.setInteger(PREFERENCE_KEY_ALLOWED_SCRIPT_ORIGIN, i);
   switch (allowedResourceOrigin.getSelectedIndex()) {
     case 0:
       i = ResourceOrigin.ANY;
       break;
     case 1:
       i = ResourceOrigin.DOCUMENT;
       break;
     case 2:
       i = ResourceOrigin.EMBEDED;
       break;
       // case 3:
     default:
       i = ResourceOrigin.NONE;
       break;
   }
   model.setInteger(PREFERENCE_KEY_ALLOWED_EXTERNAL_RESOURCE_ORIGIN, i);
   i = 1;
   if (animationLimitFPS.isSelected()) {
     i = 2;
   } else if (animationLimitUnlimited.isSelected()) {
     i = 0;
   }
   model.setInteger(PREFERENCE_KEY_ANIMATION_RATE_LIMITING_MODE, i);
   float f;
   try {
     f = Float.parseFloat(animationLimitCPUAmount.getText()) / 100;
     if (f <= 0f || f >= 1.0f) {
       f = 0.85f;
     }
   } catch (NumberFormatException e) {
     f = 0.85f;
   }
   model.setFloat(PREFERENCE_KEY_ANIMATION_RATE_LIMITING_CPU, f);
   try {
     f = Float.parseFloat(animationLimitFPSAmount.getText());
     if (f <= 0) {
       f = 15f;
     }
   } catch (NumberFormatException e) {
     f = 15f;
   }
   model.setFloat(PREFERENCE_KEY_ANIMATION_RATE_LIMITING_FPS, f);
   model.setString(PREFERENCE_KEY_PROXY_HOST, host.getText());
   model.setString(PREFERENCE_KEY_PROXY_PORT, port.getText());
   StringBuffer sb = new StringBuffer();
   Enumeration e = mediaListModel.elements();
   while (e.hasMoreElements()) {
     sb.append((String) e.nextElement());
     sb.append(' ');
   }
   model.setString(PREFERENCE_KEY_CSS_MEDIA, sb.toString());
 }
  /** Initializes the GUI components with the values from the model. */
  protected void initializeGUI() {
    boolean b;
    float f;
    int i;
    String s;

    // General options
    enableDoubleBuffering.setSelected(model.getBoolean(PREFERENCE_KEY_ENABLE_DOUBLE_BUFFERING));
    showRendering.setSelected(model.getBoolean(PREFERENCE_KEY_SHOW_RENDERING));
    autoAdjustWindow.setSelected(model.getBoolean(PREFERENCE_KEY_AUTO_ADJUST_WINDOW));
    selectionXorMode.setSelected(model.getBoolean(PREFERENCE_KEY_SELECTION_XOR_MODE));

    switch (model.getInteger(PREFERENCE_KEY_ANIMATION_RATE_LIMITING_MODE)) {
      case 0: // unlimited
        animationLimitUnlimited.setSelected(true);
        break;
      case 2: // fps
        animationLimitFPS.setSelected(true);
        break;
        // case 1: // %cpu
      default:
        animationLimitCPU.setSelected(true);
        break;
    }
    f = model.getFloat(PREFERENCE_KEY_ANIMATION_RATE_LIMITING_CPU);
    if (f <= 0f || f > 100f) {
      f = 85f;
    } else {
      f *= 100;
    }
    if (((int) f) == f) {
      animationLimitCPUAmount.setText(Integer.toString((int) f));
    } else {
      animationLimitCPUAmount.setText(Float.toString(f));
    }
    f = model.getFloat(PREFERENCE_KEY_ANIMATION_RATE_LIMITING_FPS);
    if (f <= 0f) {
      f = 10f;
    }
    if (((int) f) == f) {
      animationLimitFPSAmount.setText(Integer.toString((int) f));
    } else {
      animationLimitFPSAmount.setText(Float.toString(f));
    }

    showDebugTrace.setSelected(model.getBoolean(PREFERENCE_KEY_SHOW_DEBUG_TRACE));
    isXMLParserValidating.setSelected(model.getBoolean(PREFERENCE_KEY_IS_XML_PARSER_VALIDATING));

    // Security options
    enforceSecureScripting.setSelected(model.getBoolean(PREFERENCE_KEY_ENFORCE_SECURE_SCRIPTING));
    grantScriptFileAccess.setSelected(model.getBoolean(PREFERENCE_KEY_GRANT_SCRIPT_FILE_ACCESS));
    grantScriptNetworkAccess.setSelected(
        model.getBoolean(PREFERENCE_KEY_GRANT_SCRIPT_NETWORK_ACCESS));
    loadJava.setSelected(model.getBoolean(PREFERENCE_KEY_LOAD_JAVA));
    loadEcmascript.setSelected(model.getBoolean(PREFERENCE_KEY_LOAD_ECMASCRIPT));

    i = model.getInteger(PREFERENCE_KEY_ALLOWED_SCRIPT_ORIGIN);
    switch (i) {
      case ResourceOrigin.ANY:
        allowedScriptOrigin.setSelectedIndex(0);
        break;
      case ResourceOrigin.DOCUMENT:
        allowedScriptOrigin.setSelectedIndex(1);
        break;
      case ResourceOrigin.EMBEDED:
        allowedScriptOrigin.setSelectedIndex(2);
        break;
      default:
        allowedScriptOrigin.setSelectedIndex(3);
        break;
    }

    i = model.getInteger(PREFERENCE_KEY_ALLOWED_EXTERNAL_RESOURCE_ORIGIN);
    switch (i) {
      case ResourceOrigin.ANY:
        allowedResourceOrigin.setSelectedIndex(0);
        break;
      case ResourceOrigin.DOCUMENT:
        allowedResourceOrigin.setSelectedIndex(1);
        break;
      case ResourceOrigin.EMBEDED:
        allowedResourceOrigin.setSelectedIndex(2);
        break;
      default:
        allowedResourceOrigin.setSelectedIndex(3);
        break;
    }

    // Language options
    languagePanel.setLanguages(model.getString(PREFERENCE_KEY_LANGUAGES));

    // Stylesheet options
    s = model.getString(PREFERENCE_KEY_CSS_MEDIA);
    mediaListModel.removeAllElements();
    StringTokenizer st = new StringTokenizer(s, " ");
    while (st.hasMoreTokens()) {
      mediaListModel.addElement(st.nextToken());
    }

    userStylesheet.setText(model.getString(PREFERENCE_KEY_USER_STYLESHEET));
    b = model.getBoolean(PREFERENCE_KEY_USER_STYLESHEET_ENABLED);
    userStylesheetEnabled.setSelected(b);

    // Network options
    host.setText(model.getString(PREFERENCE_KEY_PROXY_HOST));
    port.setText(model.getString(PREFERENCE_KEY_PROXY_PORT));

    // Set some components disabled initially
    b = enableDoubleBuffering.isSelected();
    showRendering.setEnabled(b);

    b = animationLimitCPU.isSelected();
    animationLimitCPUAmount.setEnabled(b);
    animationLimitCPULabel.setEnabled(b);

    b = animationLimitFPS.isSelected();
    animationLimitFPSAmount.setEnabled(b);
    animationLimitFPSLabel.setEnabled(b);

    b = enforceSecureScripting.isSelected();
    grantScriptFileAccess.setEnabled(b);
    grantScriptNetworkAccess.setEnabled(b);

    b = userStylesheetEnabled.isSelected();
    userStylesheetLabel.setEnabled(b);
    userStylesheet.setEnabled(b);
    userStylesheetBrowse.setEnabled(b);

    mediaListRemoveButton.setEnabled(!mediaList.isSelectionEmpty());
    mediaListClearButton.setEnabled(!mediaListModel.isEmpty());
  }