@Override
 protected void getControls(DialogAccess dlg) {
   if (dlg.getCheckBoxStateAsBoolean("UseCustom" + getSuffix())) {
     saveFile(sCustomFileName, dlg.getTextFieldText("Custom" + getSuffix()));
     killFile(sCustomFileName + ".bak");
   } else {
     saveFile(sCustomFileName + ".bak", dlg.getTextFieldText("Custom" + getSuffix()));
     killFile(sCustomFileName);
   }
 }
    private void loadDefaultsClick(DialogAccess dlg) {
      updateStyleMaps(dlg);

      // Count styles that we will overwrite
      Config clean = ConverterFactory.createConverter(getMIMEType()).getConfig();
      clean.readDefaultConfig(getDefaultConfigName());

      int nCount = 0;
      int nFamilyCount = sFamilyNames.length;
      for (int i = 0; i < nFamilyCount; i++) {
        ComplexOption cleanMap = clean.getComplexOption(sFamilyNames[i] + "-map");
        Map<String, String> displayNames = styleNameProvider.getDisplayNames(sOOoFamilyNames[i]);
        for (String sName : cleanMap.keySet()) {
          String sDisplayName =
              (displayNames != null && displayNames.containsKey(sName))
                  ? displayNames.get(sName)
                  : "";
          if (styleMap[i].containsKey(sDisplayName)) {
            nCount++;
          }
        }
      }

      // Display confirmation dialog
      boolean bConfirm = false;
      XDialog xDialog = getDialog(getDialogLibraryName() + ".LoadDefaults");
      if (xDialog != null) {
        DialogAccess ldlg = new DialogAccess(xDialog);
        if (nCount > 0) {
          String sLabel = ldlg.getLabelText("OverwriteLabel");
          sLabel = sLabel.replaceAll("%s", Integer.toString(nCount));
          ldlg.setLabelText("OverwriteLabel", sLabel);
        } else {
          ldlg.setLabelText("OverwriteLabel", "");
        }
        bConfirm = xDialog.execute() == ExecutableDialogResults.OK;
        xDialog.endExecute();
      }

      // Do the replacement
      if (bConfirm) {
        for (int i = 0; i < nFamilyCount; i++) {
          ComplexOption cleanMap = clean.getComplexOption(sFamilyNames[i] + "-map");
          Map<String, String> displayNames = styleNameProvider.getDisplayNames(sOOoFamilyNames[i]);
          copyStyles(cleanMap, styleMap[i], displayNames);
        }
      }

      // Force update of the user interface
      nCurrentFamily = -1;
      sCurrentStyleName = null;
      styleFamilyChange(dlg);
    }
 // Method to display delete dialog
 protected boolean deleteItem(String sName) {
   XDialog xDialog = getDialog(getDialogLibraryName() + ".DeleteDialog");
   if (xDialog != null) {
     DialogAccess ddlg = new DialogAccess(xDialog);
     String sLabel = ddlg.getLabelText("DeleteLabel");
     sLabel = sLabel.replaceAll("%s", sName);
     ddlg.setLabelText("DeleteLabel", sLabel);
     boolean bDelete = xDialog.execute() == ExecutableDialogResults.OK;
     xDialog.endExecute();
     return bDelete;
   }
   return false;
 }
 private void styleNameChange(DialogAccess dlg) {
   if (nCurrentFamily > -1) {
     updateStyleMaps(dlg);
     short nStyleNameItem = dlg.getListBoxSelectedItem("StyleName");
     if (nStyleNameItem >= 0) {
       sCurrentStyleName = dlg.getListBoxStringItemList("StyleName")[nStyleNameItem];
       setControls(dlg, styleMap[nCurrentFamily].get(sCurrentStyleName));
     } else {
       sCurrentStyleName = null;
       clearControls(dlg);
     }
   }
 }
 protected void listBoxFromConfig(
     DialogAccess dlg,
     String sListBoxName,
     String sConfigName,
     String[] sConfigValues,
     short nDefault) {
   String sCurrentValue = config.getOption(sConfigName);
   int nCount = sConfigValues.length;
   for (short i = 0; i < nCount; i++) {
     if (sConfigValues[i].equals(sCurrentValue)) {
       dlg.setListBoxSelectedItem(sListBoxName, i);
       return;
     }
   }
   dlg.setListBoxSelectedItem(sListBoxName, nDefault);
 }
 private String newItem(Set<String> suggestions) {
   XDialog xDialog = getDialog(getDialogLibraryName() + ".NewDialog");
   if (xDialog != null) {
     String[] sItems = Misc.sortStringSet(suggestions);
     DialogAccess ndlg = new DialogAccess(xDialog);
     ndlg.setListBoxStringItemList("Name", sItems);
     String sResult = null;
     if (xDialog.execute() == ExecutableDialogResults.OK) {
       DialogAccess dlg = new DialogAccess(xDialog);
       sResult = dlg.getTextFieldText("Name");
     }
     xDialog.endExecute();
     return sResult;
   }
   return null;
 }
 private void formattingAttributeChange(DialogAccess dlg) {
   updateAttributeMaps(dlg);
   short nNewAttribute = dlg.getListBoxSelectedItem("FormattingAttribute");
   if (nNewAttribute > -1 && nNewAttribute != nCurrentAttribute) {
     nCurrentAttribute = nNewAttribute;
     String sName = sAttributeNames[nCurrentAttribute];
     if (!attributeMap.containsKey(sName)) {
       attributeMap.put(sName, new HashMap<String, String>());
       attributeMap.get(sName).put("deleted", "true");
     }
     Map<String, String> attr = attributeMap.get(sName);
     dlg.setCheckBoxStateAsBoolean(
         "CustomAttribute", !attr.containsKey("deleted") || attr.get("deleted").equals("false"));
     customAttributeChange(dlg);
     setControls(dlg, attr);
   }
 }
 private void loadCustomClick(DialogAccess dlg) {
   String sFileName = filePicker.getPath();
   if (sFileName != null) {
     String sText = loadFile(sFileName);
     if (sText != null) {
       dlg.setTextFieldText("Custom" + getSuffix(), sText);
     }
   }
 }
 // Methods to handle user controlled lists
 protected boolean deleteCurrentItem(DialogAccess dlg, String sListName) {
   String[] sItems = dlg.getListBoxStringItemList(sListName);
   short nSelected = dlg.getListBoxSelectedItem(sListName);
   if (nSelected >= 0 && deleteItem(sItems[nSelected])) {
     int nOldLen = sItems.length;
     String[] sNewItems = new String[nOldLen - 1];
     if (nSelected > 0) {
       System.arraycopy(sItems, 0, sNewItems, 0, nSelected);
     }
     if (nSelected < nOldLen - 1) {
       System.arraycopy(sItems, nSelected + 1, sNewItems, nSelected, nOldLen - 1 - nSelected);
     }
     dlg.setListBoxStringItemList(sListName, sNewItems);
     short nNewSelected = nSelected < nOldLen - 1 ? nSelected : (short) (nSelected - 1);
     dlg.setListBoxSelectedItem(sListName, nNewSelected);
     return true;
   }
   return false;
 }
    // Implement abstract methods from our super
    protected void setControls(DialogAccess dlg) {
      // Load attribute maps from config
      attributeMap.clear();
      attributeMap.copyAll(config.getComplexOption("text-attribute-map"));

      // Update the dialog
      nCurrentAttribute = -1;
      dlg.setListBoxSelectedItem("FormattingAttribute", (short) 0);
      formattingAttributeChange(dlg);
    }
    private void styleFamilyChange(DialogAccess dlg) {
      short nNewFamily = dlg.getListBoxSelectedItem("StyleFamily");
      if (nNewFamily > -1 && nNewFamily != nCurrentFamily) {
        // The user has changed the family; load and display the corresponding style names
        updateStyleMaps(dlg);
        nCurrentFamily = nNewFamily;
        sCurrentStyleName = null;

        String[] sStyleNames = Misc.sortStringSet(styleMap[nNewFamily].keySet());
        dlg.setListBoxStringItemList("StyleName", sStyleNames);
        if (sStyleNames.length > 0) {
          dlg.setListBoxSelectedItem("StyleName", (short) 0);
        } else {
          dlg.setListBoxSelectedItem("StyleName", (short) -1);
        }

        updateStyleControls(dlg);
        styleNameChange(dlg);
      }
    }
 protected String appendItem(DialogAccess dlg, String sListName, Set<String> suggestions) {
   String[] sItems = dlg.getListBoxStringItemList(sListName);
   String sNewItem = newItem(suggestions);
   if (sNewItem != null) {
     int nOldLen = sItems.length;
     for (short i = 0; i < nOldLen; i++) {
       if (sNewItem.equals(sItems[i])) {
         // Item already exists, select the existing one
         dlg.setListBoxSelectedItem(sListName, i);
         return null;
       }
     }
     String[] sNewItems = new String[nOldLen + 1];
     System.arraycopy(sItems, 0, sNewItems, 0, nOldLen);
     sNewItems[nOldLen] = sNewItem;
     dlg.setListBoxStringItemList(sListName, sNewItems);
     dlg.setListBoxSelectedItem(sListName, (short) nOldLen);
   }
   return sNewItem;
 }
    @Override
    protected void setControls(DialogAccess dlg) {
      String sText = "";
      boolean bUseCustom = false;
      if (fileExists(sCustomFileName)) {
        sText = loadFile(sCustomFileName);
        bUseCustom = true;
      } else if (fileExists(sCustomFileName + ".bak")) {
        sText = loadFile(sCustomFileName + ".bak");
      }
      // Currently ignore failure to load the file
      if (sText == null) {
        sText = "";
      }

      dlg.setCheckBoxStateAsBoolean("UseCustom" + getSuffix(), bUseCustom);
      dlg.setTextFieldText("Custom" + getSuffix(), sText);

      useCustomChange(dlg);
    }
 // Internal methods
 private void updateAttributeMaps(DialogAccess dlg) {
   // Save the current attribute map, if any
   if (nCurrentAttribute > -1) {
     String sName = sAttributeNames[nCurrentAttribute];
     if (!attributeMap.containsKey(sName)) {
       attributeMap.put(sName, new HashMap<String, String>());
     }
     Map<String, String> attr = attributeMap.get(sName);
     attr.put("deleted", Boolean.toString(!dlg.getCheckBoxStateAsBoolean("CustomAttribute")));
     getControls(dlg, attr);
     attributeMap.put(sName, attr);
   }
 }
    // Implement abstract methods from super
    protected void setControls(DialogAccess dlg) {
      // Load style maps from config (translating keys to display names)
      int nCount = sFamilyNames.length;
      for (int i = 0; i < nCount; i++) {
        ComplexOption configMap = config.getComplexOption(sFamilyNames[i] + "-map");
        styleMap[i].clear();
        Map<String, String> displayNames = styleNameProvider.getDisplayNames(sOOoFamilyNames[i]);
        copyStyles(configMap, styleMap[i], displayNames);
      }

      // Display paragraph maps first
      nCurrentFamily = -1;
      sCurrentStyleName = null;
      dlg.setListBoxSelectedItem("StyleFamily", (short) 1);
      styleFamilyChange(dlg);
    }
 private void updateStyleControls(DialogAccess dlg) {
   boolean bHasMappings = dlg.getListBoxStringItemList("StyleName").length > 0;
   dlg.setControlEnabled("DeleteStyleButton", bHasMappings);
   prepareControls(dlg, bHasMappings);
 }
 protected void textFieldToConfig(DialogAccess dlg, String sTextBoxName, String sConfigName) {
   config.setOption(sConfigName, dlg.getTextFieldText(sTextBoxName));
 }
 protected void listBoxToConfig(
     DialogAccess dlg, String sListBoxName, String sConfigName, String[] sConfigValues) {
   config.setOption(sConfigName, sConfigValues[dlg.getListBoxSelectedItem(sListBoxName)]);
 }
 protected void checkBoxToConfig(DialogAccess dlg, String sCheckBoxName, String sConfigName) {
   config.setOption(sConfigName, Boolean.toString(dlg.getCheckBoxStateAsBoolean(sCheckBoxName)));
 }
 private void useCustomChange(DialogAccess dlg) {
   boolean bUseCustom = dlg.getCheckBoxStateAsBoolean("UseCustom" + getSuffix());
   dlg.setControlEnabled("Custom" + getSuffix(), bUseCustom);
   dlg.setControlEnabled("Load" + getSuffix() + "Button", bUseCustom);
   useCustomInner(dlg, bUseCustom);
 }
 private void customAttributeChange(DialogAccess dlg) {
   prepareControls(dlg, dlg.getCheckBoxStateAsBoolean("CustomAttribute"));
 }
 // Methods to set and get controls based on config
 protected void checkBoxFromConfig(DialogAccess dlg, String sCheckBoxName, String sConfigName) {
   dlg.setCheckBoxStateAsBoolean(sCheckBoxName, "true".equals(config.getOption(sConfigName)));
 }