Ignore:
Timestamp:
2004-01-28T14:37:50+13:00 (20 years ago)
Author:
jmt12
Message:

File associations are now stored in the user specific config. Also, to make everyones life a little easier, the config has a version number which if no match is found, causes the current config to be backed-up, and a new default config created.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/file/FileAssociationManager.java

    r6622 r6630  
    4141    extends AbstractTableModel {
    4242    static final public String FILENAME_ARG = "%1";
    43     static final private String DATA_FILENAME = "associations.xml";
    44     static final private String ENTRY_ELEMENT = "Entry";
    4543    static final private String ESCAPE = "\\\\"; // '\'
    4644    static final private String ESCAPED_ESCAPE = "\\\\\\\\"; // '\\'
    47     static final private String EXTENSION_ATTRIBUTE = "extension";
    48     private Document document;
     45    private Element associations_element;
    4946    private File data_file;   
    5047
    5148    public FileAssociationManager() {
    52     // If a associations.xml is available in the GLI install directory load it.
    53     data_file = new File(Utility.BASE_DIR + DATA_FILENAME);
    54     if(data_file.exists()) {
    55         document = Utility.parse(data_file, true);
    56     }
    57     // Load the default associations xml data file. This can be done using the classloader.
    58     else {
    59         document = Utility.parse(Utility.XML_DIRECTORY + DATA_FILENAME, true);
    60     }
     49    // Retrieve the associations_element from the config
     50    associations_element = Gatherer.config.getFileAssociations();
    6151    // Initialize the associations. This involves looking through all current associations searching for those with a command of "".
    62     if(document != null) {
    63         NodeList entries = (document.getDocumentElement()).getElementsByTagName(ENTRY_ELEMENT);
     52    if(associations_element != null) {
     53        NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
    6454        for(int i = 0; i < entries.getLength(); i++) {
    6555        Element entry = (Element) entries.item(i);
    66         String extension = entry.getAttribute(EXTENSION_ATTRIBUTE);
     56        String extension = entry.getAttribute(StaticStrings.EXTENSION_ATTRIBUTE);
    6757        String command = MSMUtils.getValue(entry);
    6858        // If we encounter a command of ""...
     
    139129    // pretend we are trying to open an html file
    140130    // Mmmoooo. - jmt12
    141     /*
    142     String extension = "html";
    143     Element entry = getCommand(extension);
    144     if (entry != null) {
    145         command = MSMUtils.getValue(entry);
    146     }
    147    
    148     if(command == null || command.length() == 0) {
    149         FileAssociationDialog dialog = new FileAssociationDialog(this);
    150         command = dialog.display(extension);
    151         dialog = null;
    152     }
    153     if(command != null) {
    154         // If no previous entry existed create one.
    155         if(entry == null) {
    156         entry = document.createElement(ENTRY_ELEMENT);
    157         entry.setAttribute(EXTENSION_ATTRIBUTE, extension);
    158         document.getDocumentElement().appendChild(entry);
    159         }
    160         // Replace the text in this node. Remember to replace the dummy filename with %1
    161         MSMUtils.setValue(entry, command.replaceAll("temp.html", FILENAME_ARG));
    162     }
    163     */
     131
    164132    // if we haven't got a command by now, we'll never get one
    165133    if (command == null) {
     
    234202            // If no previous entry existed create one.
    235203            if(entry == null) {
    236             entry = document.createElement(ENTRY_ELEMENT);
    237             entry.setAttribute(EXTENSION_ATTRIBUTE, extension);
    238             document.getDocumentElement().appendChild(entry);
     204            entry = associations_element.getOwnerDocument().createElement(StaticStrings.ENTRY_ELEMENT);
     205            entry.setAttribute(StaticStrings.EXTENSION_ATTRIBUTE, extension);
     206            associations_element.appendChild(entry);
    239207            }
    240208            // Replace the text in this node. Remember to replace the dummy filename with %1 - I dont think the filename will ever be in the comand now
     
    258226
    259227    public Element getCommand(String target_extension) {
    260     NodeList entries = document.getDocumentElement().getElementsByTagName(ENTRY_ELEMENT);
     228    NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
    261229    for(int i = 0; i < entries.getLength(); i++) {
    262230        Element entry = (Element) entries.item(i);
    263         String extension = entry.getAttribute(EXTENSION_ATTRIBUTE);
     231        String extension = entry.getAttribute(StaticStrings.EXTENSION_ATTRIBUTE);
    264232        if(extension.equalsIgnoreCase(target_extension)) {
    265233        entries = null;
     
    283251
    284252    public String getExtension(int index) {
    285     NodeList entries = document.getDocumentElement().getElementsByTagName(ENTRY_ELEMENT);
     253    NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
    286254    if(0 <= index && index < entries.getLength()) {
    287255        Element entry = (Element) entries.item(index);
    288         return entry.getAttribute(EXTENSION_ATTRIBUTE);
     256        return entry.getAttribute(StaticStrings.EXTENSION_ATTRIBUTE);
    289257    }
    290258    return "";
     
    292260
    293261    public int getRowCount() {
    294     NodeList entries = document.getDocumentElement().getElementsByTagName(ENTRY_ELEMENT);
    295     return entries.getLength();
     262    return size();
    296263    }
    297264
     
    307274
    308275    public void save() {
    309     Utility.export(document, data_file);
    310276    }
    311277
     
    316282    // If no previous entry existed create one.
    317283    if(entry == null && command != null) {
    318         entry = document.createElement(ENTRY_ELEMENT);
    319         entry.setAttribute(EXTENSION_ATTRIBUTE, extension);
    320         document.getDocumentElement().appendChild(entry);
     284        entry = associations_element.getOwnerDocument().createElement(StaticStrings.ENTRY_ELEMENT);
     285        entry.setAttribute(StaticStrings.EXTENSION_ATTRIBUTE, extension);
     286        associations_element.appendChild(entry);
    321287    }
    322288
     
    327293    else {
    328294        // Remove the entry
    329         document.getDocumentElement().removeChild(entry);
     295        associations_element.removeChild(entry);
    330296    }
    331297    entry = null;
     
    334300
    335301    public int size() {
    336     NodeList entries = document.getDocumentElement().getElementsByTagName(ENTRY_ELEMENT);
     302    NodeList entries = associations_element.getElementsByTagName(StaticStrings.ENTRY_ELEMENT);
    337303    return entries.getLength();
    338304    }
Note: See TracChangeset for help on using the changeset viewer.