/** * Reset the List of external file types after user customization. * * @param types The new List of external file types. This is the complete list, not just new * entries. */ public void setExternalFileTypes(List<ExternalFileType> types) { // First find a list of the default types: List<ExternalFileType> defTypes = getDefaultExternalFileTypes(); // Make a list of types that are unchanged: List<ExternalFileType> unchanged = new ArrayList<ExternalFileType>(); externalFileTypes.clear(); for (Iterator<ExternalFileType> iterator = types.iterator(); iterator.hasNext(); ) { ExternalFileType type = iterator.next(); externalFileTypes.add(type); // See if we can find a type with matching name in the default type list: ExternalFileType found = null; for (ExternalFileType defType : defTypes) { if (defType.getName().equals(type.getName())) { found = defType; break; } } if (found != null) { // Found it! Check if it is an exact match, or if it has been customized: if (found.equals(type)) unchanged.add(type); else { // It was modified. Remove its entry from the defaults list, since // the type hasn't been removed: defTypes.remove(found); } } } // Go through unchanged types. Remove them from the ones that should be stored, // and from the list of defaults, since we don't need to mention these in prefs: for (ExternalFileType type : unchanged) { defTypes.remove(type); types.remove(type); } // Now set up the array to write to prefs, containing all new types, all modified // types, and a flag denoting each default type that has been removed: String[][] array = new String[types.size() + defTypes.size()][]; int i = 0; for (ExternalFileType type : types) { array[i] = type.getStringArrayRepresentation(); i++; } for (ExternalFileType type : defTypes) { array[i] = new String[] {type.getName(), FILE_TYPE_REMOVED_FLAG}; i++; } // System.out.println("Encoded: '"+Util.encodeStringArray(array)+"'"); put("externalFileTypes", Util.encodeStringArray(array)); }
/** * Set up the list of external file types, either from default values, or from values recorded in * Preferences. */ public void updateExternalFileTypes() { // First get a list of the default file types as a starting point: List<ExternalFileType> types = getDefaultExternalFileTypes(); // If no changes have been stored, simply use the defaults: if (prefs.get("externalFileTypes", null) == null) { externalFileTypes.clear(); externalFileTypes.addAll(types); return; } // Read the prefs information for file types: String[][] vals = Util.decodeStringDoubleArray(prefs.get("externalFileTypes", "")); for (int i = 0; i < vals.length; i++) { if ((vals[i].length == 2) && (vals[i][1].equals(FILE_TYPE_REMOVED_FLAG))) { // This entry indicates that a default entry type should be removed: ExternalFileType toRemove = null; for (ExternalFileType type : types) { if (type.getName().equals(vals[i][0])) { toRemove = type; break; } } // If we found it, remove it from the type list: if (toRemove != null) types.remove(toRemove); } else { // A new or modified entry type. Construct it from the string array: ExternalFileType type = new ExternalFileType(vals[i]); // Check if there is a default type with the same name. If so, this is a // modification of that type, so remove the default one: ExternalFileType toRemove = null; for (ExternalFileType defType : types) { if (type.getName().equals(defType.getName())) { toRemove = defType; break; } } // If we found it, remove it from the type list: if (toRemove != null) { types.remove(toRemove); } // Then add the new one: types.add(type); } } // Finally, build the list of types based on the modified defaults list: for (ExternalFileType type : types) { externalFileTypes.add(type); } }
/** * Look up the external file type registered with this name, if any. * * @param name The file type name. * @return The ExternalFileType registered, or null if none. */ public ExternalFileType getExternalFileTypeByName(String name) { for (Iterator<ExternalFileType> iterator = externalFileTypes.iterator(); iterator.hasNext(); ) { ExternalFileType type = iterator.next(); if (type.getName().equals(name)) return type; } // Return an instance that signifies an unknown file type: return new UnknownExternalFileType(name); }