Changeset 14039


Ignore:
Timestamp:
2007-05-03T08:54:51+12:00 (17 years ago)
Author:
xiao
Message:

Change made in the method getElementAt() not to get element from the 'cache' but directly from the 'children' list because when an element is deleted from the internal DOM tree, 'children' is up to date but 'cache' is not.

Location:
trunk/gli/src/org/greenstone/gatherer/cdm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/cdm/DOMProxyListModel.java

    r12641 r14039  
    3131import org.greenstone.gatherer.DebugStream;
    3232import org.greenstone.gatherer.util.StaticStrings;
     33import org.greenstone.gatherer.util.XMLTools;
    3334import org.w3c.dom.*;
    3435
     
    3839 */
    3940public class DOMProxyListModel
    40     extends AbstractListModel {
     41extends AbstractListModel {
    4142    protected Element root;
    4243    private DOMProxyListEntry class_type;
    43     private HashMap cache = new HashMap();
     44    private HashMap cache = new HashMap ();
    4445    private NodeList children = null;
    4546    private String tag_name;
    46 
     47   
    4748    /** Constructor.
    4849     * @param root the Element at the root of the subtree to be searched for appropriate child elements
     
    5051     * @param class_type the type of object to wrap the elements returned in, as a DOMProxyListEntry
    5152     */
    52     public DOMProxyListModel(Element root, String tag_name, DOMProxyListEntry class_type) {
    53     this.class_type = class_type;
    54     this.root = root;
    55     this.tag_name = tag_name;
    56     this.children = this.root.getElementsByTagName(this.tag_name);
    57     }
    58 
     53    public DOMProxyListModel (Element root, String tag_name, DOMProxyListEntry class_type) {
     54        this.class_type = class_type;
     55        this.root = root;
     56        this.tag_name = tag_name;
     57        this.children = this.root.getElementsByTagName (this.tag_name);
     58    }
     59   
    5960    /** Used to add an element into the underlying dom, and fire the appropriate repaint events. This version always adds the new element at the very head of the DOM. */
    60     public synchronized void add(DOMProxyListEntry entry) {
    61     Element element = entry.getElement();
    62     if(root.hasChildNodes()) {
    63         Node sibling = root.getFirstChild();
    64         root.insertBefore(element, sibling);
    65         sibling = null;
    66     }
    67     else {
    68         root.appendChild(element);
    69     }
    70     element = null;
    71     // Regardless fire update event
    72     cache.clear();
    73     fireIntervalAdded(this, 0, 0);
    74     }
    75 
     61    public synchronized void add (DOMProxyListEntry entry) {
     62        Element element = entry.getElement ();
     63        if(root.hasChildNodes ()) {
     64            Node sibling = root.getFirstChild ();
     65            root.insertBefore (element, sibling);
     66            sibling = null;
     67        }
     68        else {
     69            root.appendChild (element);
     70        }
     71        element = null;
     72        // Regardless fire update event
     73        cache.clear ();
     74        fireIntervalAdded (this, 0, 0);
     75    }
     76   
    7677    /** Used to add an element into the underlying dom, and fire the appropriate repaint events.
    7778     * @param index the index where the element should be inserted (relative of the other elements in this proxy list)
    7879     * @param entry the <strong>DOMProxyListEntry</strong> to be inserted
    7980     */
    80     public synchronized void add(int index, DOMProxyListEntry entry) {
    81     ///atherer.println("Add entry at " + index + " where size = " + getSize());
    82     Element element = entry.getElement();
    83     // retrieve the node where we want to insert
    84     if(index < children.getLength()) {
    85         Node sibling = children.item(index);
    86         // Find the parent node
    87         Node parent_node = sibling.getParentNode();
    88         parent_node.insertBefore(element, sibling);
    89         sibling = null;
    90     }
    91     // If the index is too large, we are adding to the end of our list of entries. However you have to remember that this list is only a viewport on the entire DOM so there might be entries following this group that we actually want to insert before (not append at the very end!)
    92     else {
    93         // Retrieve the currently last entry
    94         index = children.getLength() - 1;
    95         Node sibling = null;
    96         Node parent_node = null;
    97         if(index >= 0) {
    98         sibling = children.item(index);
    99         parent_node = sibling.getParentNode();
    100         sibling = sibling.getNextSibling();
    101         }
    102         if(sibling != null && parent_node != null) {
    103         parent_node.insertBefore(element, sibling);
    104         }
    105         // Add to the root node
    106         else {
    107         index = 0;
    108         root.appendChild(element);
    109         }
    110     }
    111     // Regardless fire update event
    112     cache.clear();
    113     fireIntervalAdded(this, index, index);
    114     }
    115 
     81    public synchronized void add (int index, DOMProxyListEntry entry) {
     82        ///atherer.println("Add entry at " + index + " where size = " + getSize());
     83        Element element = entry.getElement ();
     84        // retrieve the node where we want to insert
     85        if(index < children.getLength ()) {
     86            Node sibling = children.item (index);
     87            // Find the parent node
     88            Node parent_node = sibling.getParentNode ();
     89            parent_node.insertBefore (element, sibling);
     90            sibling = null;
     91        }
     92        // If the index is too large, we are adding to the end of our list of entries. However you have to remember that this list is only a viewport on the entire DOM so there might be entries following this group that we actually want to insert before (not append at the very end!)
     93        else {
     94            // Retrieve the currently last entry
     95            index = children.getLength () - 1;
     96            Node sibling = null;
     97            Node parent_node = null;
     98            if(index >= 0) {
     99                sibling = children.item (index);
     100                parent_node = sibling.getParentNode ();
     101                sibling = sibling.getNextSibling ();
     102            }
     103            if(sibling != null && parent_node != null) {
     104                parent_node.insertBefore (element, sibling);
     105            }
     106            // Add to the root node
     107            else {
     108                index = 0;
     109                root.appendChild (element);
     110            }
     111        }
     112        // Regardless fire update event
     113        cache.clear ();
     114        fireIntervalAdded (this, index, index);
     115    }
     116   
    116117    /** Used to add an element into the underlying dom, and fire the appropriate repaint events. This version inserts the new entry immediately -after- the given entry in the DOM.
    117118     * @param entry the DOMProxyListEntry to be inserted
    118119     * @param preceeding_entry the DOMProxyListEntry immediately before where we want the new entry
    119120     */
    120     public synchronized void addAfter(DOMProxyListEntry entry, DOMProxyListEntry preceeding_entry) {
    121     Element element = entry.getElement();
    122     Element preceeding_sibling = preceeding_entry.getElement();
    123     Node parent_node = preceeding_sibling.getParentNode();
    124     Node following_sibling = preceeding_sibling.getNextSibling();
    125     if(following_sibling != null) {
    126         parent_node.insertBefore(element, following_sibling);
    127     }
    128     else {
    129         parent_node.appendChild(element);
    130     }
    131     // Regardless fire update event
    132     cache.clear();
    133     int index = indexOf(entry);
    134     fireIntervalAdded(this, index, index);
    135     }
    136 
     121    public synchronized void addAfter (DOMProxyListEntry entry, DOMProxyListEntry preceeding_entry) {
     122        Element element = entry.getElement ();
     123        Element preceeding_sibling = preceeding_entry.getElement ();
     124        Node parent_node = preceeding_sibling.getParentNode ();
     125        Node following_sibling = preceeding_sibling.getNextSibling ();
     126        if(following_sibling != null) {
     127            parent_node.insertBefore (element, following_sibling);
     128        }
     129        else {
     130            parent_node.appendChild (element);
     131        }
     132        // Regardless fire update event
     133        cache.clear ();
     134        int index = indexOf (entry);
     135        fireIntervalAdded (this, index, index);
     136    }
     137   
    137138    /** Used to add an element into the underlying dom, and fire the appropriate repaint events. This version inserts the new entry immediately -before- the given entry in the DOM.
    138139     * @param entry the DOMProxyListEntry to be inserted
    139140     * @param following_entry the DOMProxyListEntry immediately after where we want the new entry
    140141     */
    141     public synchronized void addBefore(DOMProxyListEntry entry, DOMProxyListEntry following_entry) {
    142     Element element = entry.getElement();
    143     Element following_sibling = following_entry.getElement();
    144     Node parent_node = following_sibling.getParentNode();
    145     parent_node.insertBefore(element, following_sibling);
    146     // Regardless fire update event
    147     cache.clear();
    148     int index = indexOf(entry);
    149     fireIntervalAdded(this, index, index);
     142    public synchronized void addBefore (DOMProxyListEntry entry, DOMProxyListEntry following_entry) {
     143        Element element = entry.getElement ();
     144        Element following_sibling = following_entry.getElement ();
     145        Node parent_node = following_sibling.getParentNode ();
     146        parent_node.insertBefore (element, following_sibling);
     147        // Regardless fire update event
     148        cache.clear ();
     149        int index = indexOf (entry);
     150        fireIntervalAdded (this, index, index);
     151    }
     152   
     153    public synchronized void add (Node parent, DOMProxyListEntry entry, Node sibling) {
     154        Element child = entry.getElement ();
     155        if(sibling != null) {
     156            parent.insertBefore (child, sibling);
     157        }
     158        else {
     159            parent.appendChild (child);
     160        }
     161        cache.clear ();
     162        int index = indexOf (entry);
     163       
     164       
     165        fireIntervalAdded (this, index, index);
     166    }
     167   
     168    /** Used to add an element into the underlying dom, and fire the appropriate repaint events. This version always adds the new element at the end of the children. */
     169    public synchronized void append (DOMProxyListEntry entry) {
     170        Element element = entry.getElement ();
     171        root.appendChild (element);
     172        element = null;
     173        // Regardless fire update event
     174        cache.clear ();
     175        fireIntervalAdded (this, 0, 0);
     176    }
     177   
     178    public synchronized ArrayList children () {
     179        ArrayList child_list = new ArrayList ();
     180        int child_count = children.getLength ();
     181        for(int i = 0; i < child_count; i++) {
     182            child_list.add (getElementAt (i));
     183        }
     184        return child_list;
     185    }
     186   
     187    public synchronized boolean contains (Object entry) {
     188        boolean found = false;
     189        int size = getSize ();
     190       
     191        for(int i = 0; !found && i < size; i++) {
     192            DOMProxyListEntry sibling = (DOMProxyListEntry) getElementAt (i);
     193            if(sibling.equals (entry)) {
     194               
     195                found = true;
     196            }
     197        }
     198        return found;
     199    }
     200   
     201    public synchronized Object getElementAt (int index) {
     202        /** There are times when the length of the 'cache' is not as same as the length of the 'children', etc. not up to date.
     203            eg, when a classifier has been deleted. So we rather not have this efficiency for Format4gs3.java.*/       
     204        if (class_type instanceof Format4gs3) {
     205            Element element = (Element) children.item (index);
     206            // Now wrap it in the object of the users choice
     207            Object object = class_type.create (element);
     208            return object;
     209        }
     210       
     211        Object object = cache.get (new Integer (index));
     212        if (object != null) {
     213            return object;
     214        }
     215       
     216        // Retrieve the required element
     217        Element element = (Element) children.item (index);
     218        DebugStream.println ("Element at index " + index + " not in cache: " + element);
     219       
     220        // Now wrap it in the object of the users choice
     221        object = class_type.create (element);
     222        cache.put (new Integer (index), object);
     223        return object;
     224    }
     225   
     226    public synchronized int indexOf (DOMProxyListEntry entry) {
     227        Element element = entry.getElement ();
     228        int children_length = children.getLength ();
     229        for(int i = 0; i < children_length; i++) {
     230            Node node = children.item (i);
     231            if(element == node) {
     232                return i;
     233            }
     234        }
     235        return -1;
     236    }
     237   
     238    public synchronized int getSize () {
     239       
     240        if(children == null) {
     241            children = root.getElementsByTagName (tag_name);
     242        }
     243        return children.getLength ();
     244    }
     245   
     246    public synchronized void refresh () {
     247        fireContentsChanged (this, 0, getSize ());
     248    }
     249   
     250    public synchronized void refresh (DOMProxyListEntry entry) {
     251        int index = indexOf (entry);
     252        fireContentsChanged (this, index, index);
     253    }
     254   
     255    public synchronized void remove (DOMProxyListEntry entry) {
     256        remove (indexOf (entry));
     257    }
     258   
     259    public synchronized void remove (int index) {
     260        // Retrieve the required element
     261        Node node = children.item (index);
     262        // Find its parent
     263        Node parent_node = node.getParentNode ();
     264        // Now remove it
     265        parent_node.removeChild (node);
     266        // Refresh model
     267        cache.clear ();
     268           
     269        fireIntervalRemoved (this, index, index);
     270    }
     271   
     272   
     273    /** Changes the 'root' element that this list sources its information from.
     274     * @param  root the new root Element
     275     */
     276    public synchronized void setRoot (Element root) {
     277        this.children = null;
     278        cache.clear ();
     279        this.root = root;
     280        this.children = this.root.getElementsByTagName (this.tag_name);
     281        fireContentsChanged (this, 0, getSize ());
     282    }
     283   
     284    public synchronized void setAssigned (boolean assigned) {
     285        if (assigned) {
     286            root.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
     287        }
     288        else {
     289            root.setAttribute (StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
     290        }
     291    }
     292   
     293    public boolean isAssigned () {
     294        return (root.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals ("") || root.getAttribute (StaticStrings.ASSIGNED_ATTRIBUTE).equals (StaticStrings.TRUE_STR));
    150295    }
    151296
    152     public synchronized void add(Node parent, DOMProxyListEntry entry, Node sibling) {
    153     Element child = entry.getElement();
    154     if(sibling != null) {
    155         parent.insertBefore(child, sibling);
    156     }
    157     else {
    158         parent.appendChild(child);
    159     }
    160     cache.clear();
    161     int index = indexOf(entry);
    162      
    163          
    164     fireIntervalAdded(this, index, index);
    165     }
    166 
    167     /** Used to add an element into the underlying dom, and fire the appropriate repaint events. This version always adds the new element at the end of the children. */
    168     public synchronized void append(DOMProxyListEntry entry) {
    169     Element element = entry.getElement();
    170     root.appendChild(element);
    171     element = null;
    172     // Regardless fire update event
    173     cache.clear();
    174     fireIntervalAdded(this, 0, 0);
    175     }
    176    
    177     public synchronized ArrayList children() {
    178     ArrayList child_list = new ArrayList();
    179     int child_count = children.getLength();
    180     for(int i = 0; i < child_count; i++) {
    181         child_list.add(getElementAt(i));
    182     }
    183     return child_list;
    184     }
    185 
    186     public synchronized boolean contains(Object entry) {
    187     boolean found = false;
    188     int size = getSize();
    189      
    190     for(int i = 0; !found && i < size; i++) {
    191         DOMProxyListEntry sibling = (DOMProxyListEntry) getElementAt(i);
    192             if(sibling.equals(entry)) {
    193                
    194         found = true;
    195         }
    196     }
    197     return found;
    198     }
    199 
    200    
    201     public synchronized Object getElementAt(int index)
    202     {
    203     Object object = cache.get(new Integer(index));
    204     if (object != null) {
    205         return object;
    206     }
    207 
    208     // Retrieve the required element
    209     Element element = (Element) children.item(index);
    210     DebugStream.println("Element at index " + index + " not in cache: " + element);
    211 
    212     // Now wrap it in the object of the users choice
    213     object = class_type.create(element);
    214     cache.put(new Integer(index), object);
    215     return object;
    216     }
    217 
    218     public synchronized int indexOf(DOMProxyListEntry entry) {
    219     Element element = entry.getElement();
    220     int children_length = children.getLength();
    221     for(int i = 0; i < children_length; i++) {
    222         Node node = children.item(i);
    223         if(element == node) {
    224         return i;
    225         }
    226     }
    227     return -1;
    228     }
    229 
    230     public synchronized int getSize() {
    231     if(children == null) {
    232         children = root.getElementsByTagName(tag_name);
    233     }
    234     return children.getLength();
    235     }
    236 
    237     public synchronized void refresh() {
    238     fireContentsChanged(this, 0, getSize());
    239     }
    240 
    241     public synchronized void refresh(DOMProxyListEntry entry) {
    242     int index = indexOf(entry);
    243     fireContentsChanged(this, index, index);
    244     }
    245 
    246     public synchronized void remove(DOMProxyListEntry entry) {
    247     remove(indexOf(entry));
    248     }
    249 
    250     public synchronized void remove(int index) {
    251     // Retrieve the required element
    252     Node node = children.item(index);
    253     // Find its parent
    254     Node parent_node = node.getParentNode();
    255     // Now remove it
    256     parent_node.removeChild(node);
    257     // Refresh model
    258     cache.clear();
    259     fireIntervalRemoved(this, index, index);
    260     }
    261 
    262  
    263     /** Changes the 'root' element that this list sources its information from.
    264      * @param  root the new root Element
    265      */
    266     public synchronized void setRoot(Element root) {
    267     this.children = null;
    268     cache.clear();
    269     this.root = root;
    270     this.children = this.root.getElementsByTagName(this.tag_name);
    271     fireContentsChanged(this, 0, getSize());     
    272     }
    273 
    274     public synchronized void setAssigned(boolean assigned) {
    275     if (assigned) {
    276         root.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
    277     }
    278     else {
    279         root.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
    280     }
    281     }
    282 
    283     public boolean isAssigned() {
    284     return (root.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals("") || root.getAttribute(StaticStrings.ASSIGNED_ATTRIBUTE).equals(StaticStrings.TRUE_STR));
    285     }
    286297}
  • trunk/gli/src/org/greenstone/gatherer/cdm/FormatManager.java

    r13195 r14039  
    5050 */
    5151public class FormatManager
    52     extends DOMProxyListModel {
     52    extends DOMProxyListModel implements SharedByTwoFormatManager {
    5353
    5454    static final private String BLANK = "blank";
Note: See TracChangeset for help on using the changeset viewer.