Changeset 4489


Ignore:
Timestamp:
2003-06-05T13:32:17+12:00 (21 years ago)
Author:
kjdon
Message:

changed a lot of stuff to do with importing metadata sets and merging elements with the same name. now there is no separate progress bar cos that was screwy, and we dont really need it anyway. this stuff still isn't working properly, but I'll keep working on it...

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

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/msm/MSMPrompt.java

    r4460 r4489  
    6767    private JButton replace = null;
    6868    private JButton skip = null;
    69     private JDialog off_screen = null;
    7069    private JDialog on_screen = null;
    7170    private JDialog dialog = null;
     
    7978    private Object result = null;
    8079
    81     final static private Dimension MDE_SIZE = new Dimension(500,225);
     80    final static private Dimension MDE_SIZE = new Dimension(500,250);
    8281    final static private Dimension MDS_SIZE = new Dimension(800,425);
    8382    final static private Dimension PROGRESS_SIZE = new Dimension(500,105);
     
    149148    args[0] = mde_new.getAttribute("name");
    150149    args[1] = get(reason, null);
    151     JOptionPane.showMessageDialog(off_screen, get("Add_Failed", args), get("Add_Failed_Title", null), JOptionPane.ERROR_MESSAGE);
     150    JOptionPane.showMessageDialog(Gatherer.g_man, get("Add_Failed", args), get("Add_Failed_Title", null), JOptionPane.ERROR_MESSAGE);
    152151    }
    153152
     
    181180     */
    182181    public void endMerge() {
    183     off_screen.dispose();
    184     off_screen = null;
    185182    }
    186183    /** Method to indicate that yet another element has been succesfully merged, so that progress bar should reflect this.
     
    193190    /** Method to display the metadata element merging prompt, wherein the user determines how the attributes within an element should be merged.
    194191     * @param mde_cur The current <strong>Element</strong> we are merging against.
    195      * @param att_cur The attribute <strong>Element</strong> of the current element in question.
     192     * @param atts_cur The list of matching attribute <strong>Elements</strong> of the current element in question.
    196193     * @param mde_new The <strong>Element</strong> which we have choosen to merge in, and whose attributes are being examined.
    197194     * @param att_new And the new elements attribute <strong>Element</strong>.
    198      * @return An <i>int</i> specifying what further action should be undertaken, if any.
    199      */
    200     public int mDEPrompt(Element mde_cur, Element att_cur, Element mde_new, Element att_new) {
     195     * @return An object, which is either an Integer specifying what further action should be undertaken, if any, or if the action was replace, returns an Element, which is the attribute Element to replace with the new one
     196     */
     197    public Object mDEPrompt(Element mde_cur, Element []atts_cur, Element mde_new, Element att_new) {
    201198    action = Declarations.NO_ACTION;
    202199    // Construction and configuration
     
    208205    JPanel content_pane = (JPanel)dialog.getContentPane();
    209206
    210     JLabel cur_name_label = new JLabel(get("Element_Name"));
    211 
    212     JLabel cur_name = new JLabel(MSMUtils.getFullName(mde_cur));
    213     cur_name.setBackground(Color.white);
    214     cur_name.setOpaque(true);
    215 
    216     JLabel cur_att_label = new JLabel(get("Attribute"));
    217 
    218     JLabel cur_att = new JLabel(att_cur.getAttribute("name") +" = " + MSMUtils.getValue(att_cur));
    219     cur_att.setBackground(Color.white);
    220     cur_att.setOpaque(true);
    221 
    222     JLabel new_name_label = new JLabel(cur_name_label.getText());
    223 
    224     JLabel new_name = new JLabel(MSMUtils.getFullName(mde_new));
    225     new_name.setBackground(Color.white);
    226     new_name.setOpaque(true);
    227 
    228     JLabel new_att_label = new JLabel(cur_att_label.getText());
    229 
    230     JLabel new_att = new JLabel(att_new.getAttribute("name") +" = " + MSMUtils.getValue(att_new));
    231     new_att.setBackground(Color.white);
    232     new_att.setOpaque(true);
    233 
    234     replace.setEnabled(true);
     207    JLabel element_name_label = new JLabel(get("Element_Name"));
     208    JLabel element_name = new JLabel(MSMUtils.getFullName(mde_cur));
     209   
     210    JLabel attribute_name_label = new JLabel(get("Attribute"));
     211    JLabel attribute_name = new JLabel(att_new.getAttribute("name"));
     212
     213    add.setEnabled(true);
     214    replace.setEnabled(false);
    235215    skip.setEnabled(true);
    236216
    237     // Layout
     217    JList cur_values = new JList(atts_cur);
     218    cur_values.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
     219    cur_values.setLayoutOrientation(JList.VERTICAL);
     220    cur_values.setVisibleRowCount(3);
     221    cur_values.setCellRenderer(new AttributeListCellRenderer());
     222    cur_values.addListSelectionListener(new ElementListListener(cur_values, replace));
     223    //Layout
     224    JPanel title_pane = new JPanel();
     225    title_pane.setLayout(new GridLayout(2,2));
     226    title_pane.add(element_name_label);
     227    title_pane.add(element_name);
     228    title_pane.add(attribute_name_label);
     229    title_pane.add(attribute_name);
     230
    238231    JPanel current_pane = new JPanel();
    239     current_pane.setBorder(BorderFactory.createTitledBorder(get("Current_Element")));
    240     current_pane.setLayout(new GridLayout(2,2));
    241     current_pane.add(cur_name_label);
    242     current_pane.add(cur_name);
    243     current_pane.add(cur_att_label);
    244     current_pane.add(cur_att);
     232    current_pane.setLayout(new BorderLayout());
     233    current_pane.setBorder(BorderFactory.createTitledBorder(get("Current_Values")));
     234    JScrollPane scroll = new JScrollPane(cur_values);
     235    current_pane.add(scroll, BorderLayout.CENTER);
    245236
    246237    JPanel new_pane = new JPanel();
    247     new_pane.setBorder(BorderFactory.createTitledBorder(get("New_Element")));
    248     new_pane.setLayout(new GridLayout(2,2));
    249     new_pane.add(new_name_label);
    250     new_pane.add(new_name);
    251     new_pane.add(new_att_label);
    252     new_pane.add(new_att);
    253 
     238    new_pane.setBorder(BorderFactory.createTitledBorder(get("New_Value")));
     239    new_pane.setLayout(new BorderLayout());
     240    JLabel new_att = new JLabel(MSMUtils.getValue(att_new));
     241    new_pane.add(new_att, BorderLayout.WEST);
    254242    JPanel central_pane = new JPanel();
    255243    central_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    256     central_pane.setLayout(new GridLayout(2,1));
     244    central_pane.setLayout(new BoxLayout(central_pane, BoxLayout.Y_AXIS));
     245    central_pane.add(title_pane);
    257246    central_pane.add(current_pane);
    258247    central_pane.add(new_pane);
     
    260249    JPanel button_pane = new JPanel();
    261250    button_pane.setBorder(BorderFactory.createEmptyBorder(0,5,5,5));
    262     button_pane.setLayout(new GridLayout(1,3));
     251    button_pane.setLayout(new GridLayout(1,4));
     252    button_pane.add(add);
    263253    button_pane.add(replace);
    264254    button_pane.add(skip);
     
    270260
    271261    // Display
     262   
    272263    dialog.setLocation((screen_size.width - MDE_SIZE.width) / 2, (screen_size.height - MDE_SIZE.height) / 2);
    273264    on_screen = dialog;
    274     off_screen.setVisible(false);
    275     on_screen.setVisible(true);
    276     off_screen.setVisible(true);
     265    on_screen.setVisible(true); // blocks until hidden
    277266    on_screen.dispose();
    278267
     268    // the return value - will either be an Integer containing the action,
     269    // of the selected Element for a replace
     270    Object result = new Integer(action);
     271    // need to get the value of the list selection if replace was clicked
     272    if (action == Declarations.REPLACE) {
     273        result = cur_values.getSelectedValue();
     274    }
    279275    content_pane = null;
    280     cur_name_label = null;
    281     cur_name = null;
    282     cur_att_label = null;
    283     cur_att = null;
    284     new_name_label = null;
    285     new_name = null;
    286     new_att_label = null;
     276    element_name_label = null;
     277    element_name = null;
     278    attribute_name_label = null;
     279    attribute_name=null;
     280    cur_values = null;
     281    title_pane=null;
     282    current_pane = null;
     283    scroll = null;
     284    new_pane = null;
    287285    new_att = null;
    288     current_pane = null;
    289     new_pane = null;
    290286    central_pane = null;
    291287    button_pane = null;
    292     screen_size = null;
    293288    on_screen = null;
    294289    dialog = null;
    295 
    296     return action;
     290   
     291    return result;
    297292    }
    298293    /** This method displays the metadata data set merging prompt, wherein the user determines how the elements within a set should be merged.
     
    320315        skip.setEnabled(true);
    321316                // Current details.
    322         String str_cur[] = MSMUtils.getStructuralDetails(mde_cur);
     317        String str_cur[] = MSMUtils.getStructuralDetails(mds_cur, mde_cur);
    323318        String opt_cur[] = MSMUtils.getOptionListDetails(mde_cur);
    324319        String ass_cur[] = MSMUtils.getAssignedValuesDetails(mds_cur, mde_cur);
    325         String details_cur = get("Structural");
     320        String details_cur = get("Structural", str_cur);
    326321        if(opt_cur != null) {
    327322        details_cur = details_cur + "\n" + get("OptionList", opt_cur);
     
    346341    }
    347342    // New details.
    348     String str_new[] = MSMUtils.getStructuralDetails(mde_new);
     343    String str_new[] = MSMUtils.getStructuralDetails(mds_new, mde_new);
    349344    String opt_new[] = MSMUtils.getOptionListDetails(mde_new);
    350345    String ass_new[] = MSMUtils.getAssignedValuesDetails(mds_new, mde_new);
     
    402397    dialog.setLocation((screen_size.width - MDS_SIZE.width) / 2, (screen_size.height - MDS_SIZE.height) / 2);
    403398    on_screen = dialog;
    404     off_screen.setVisible(false);
    405399    on_screen.setVisible(true); // Blocks until hidden.
    406     off_screen.setVisible(true);
    407400    on_screen.dispose();
    408401    on_screen = null;
     
    426419    return action;
    427420    }
    428     /** This method creates an initial JDialog which simply shows the MSMs progress towards merging the metadata sets.
     421    /** This method initialises the  progress bar.
    429422     * @param element_count An <i>int</i> specifying the total number of elements to be merged.
    430423     */
    431424    public void startMerge(int element_count) {
    432     action = Declarations.NO_ACTION;
    433     JDialog dialog = new ModalDialog(Gatherer.g_man);
    434     dialog.setModal(false);
    435     dialog.setSize(PROGRESS_SIZE);
    436     dialog.setTitle(get("Merge_Progress"));
    437     //dialog.setJMenuBar(new SimpleMenuBar("0")); ?? do we want help here??
    438 
    439     JPanel content_pane = (JPanel)dialog.getContentPane();
    440425    progress.setMaximum(element_count);
    441426    progress.setMinimum(0);
    442427    progress.setString("0%");
    443428    progress.setValue(0);
    444     // Layout.
    445     content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    446     content_pane.setLayout(new BorderLayout());
    447     content_pane.add(progress_label, BorderLayout.NORTH);
    448     content_pane.add(progress, BorderLayout.CENTER);
    449     // Display.
    450     dialog.setLocation((screen_size.width - PROGRESS_SIZE.width) / 2, (screen_size.height - PROGRESS_SIZE.height) / 2);
    451     off_screen = dialog;
    452     dialog.setVisible(true);
    453     content_pane = null;
    454     dialog = null;
    455     }
     429    }
     430   
    456431    /** This method creates a prompt for renaming an Element.
    457432     * @param mde_new The <strong>Element</strong> we wish to rename.
     
    515490    dialog.setLocation((screen_size.width - RENAME_SIZE.width) / 2, (screen_size.height - RENAME_SIZE.height) / 2);
    516491    on_screen = dialog;
    517     off_screen.setVisible(false);
    518492    on_screen.setVisible(true);
    519     off_screen.setVisible(true);
    520493    on_screen.dispose();
    521494    on_screen = null;         
     
    550523    args[0] = mde_cur.getAttribute("name");
    551524    args[1] = get(reason, null);
    552     JOptionPane.showMessageDialog(off_screen, get("Remove_Failed", args), get("Remove_Failed_Title", null), JOptionPane.ERROR_MESSAGE);
     525    JOptionPane.showMessageDialog(Gatherer.g_man, get("Remove_Failed", args), get("Remove_Failed_Title", null), JOptionPane.ERROR_MESSAGE);
    553526    }
    554527
     
    563536    args[1] = new_name;
    564537    args[2] = get(reason, null);
    565     JOptionPane.showMessageDialog(off_screen, get("Rename_Failed", args), get("Rename_Failed_Title", null), JOptionPane.ERROR_MESSAGE);
     538    JOptionPane.showMessageDialog(Gatherer.g_man, get("Rename_Failed", args), get("Rename_Failed_Title", null), JOptionPane.ERROR_MESSAGE);
    566539    }
    567540
     
    611584    dialog.setLocation((screen_size.width - SELECT_SIZE.width) / 2, (screen_size.height - SELECT_SIZE.height) / 2);
    612585    on_screen = dialog;
    613     off_screen.setVisible(false);
    614586    on_screen.setVisible(true);
    615     off_screen.setVisible(true);
    616587    on_screen.dispose();
    617588    on_screen = null;   
     
    763734    // Display
    764735    dialog.setLocation((screen_size.width - SELECT_ELEMENT_SIZE.width) / 2, (screen_size.height - SELECT_ELEMENT_SIZE.height) / 2);
    765     System.err.println("Showing selection dialog. Block...");
    766736    dialog.setVisible(true);
    767     System.err.println("and release. Returning value.");
    768737    // Deallocate everything because JDK1.4 won't.
    769738    // Why, oh why did I do this?
     
    874843    // Display
    875844    dialog.setLocation((screen_size.width - SELECT_SET_SIZE.width) / 2, (screen_size.height - SELECT_SET_SIZE.height) / 2);
    876     System.err.println("Showing selection dialog. Block...");
    877845    dialog.setVisible(true);
    878     System.err.println("and release. Returning value.");
    879846    Object value = set.getSelectedItem();
    880847    if(value instanceof MetadataSet) {
     
    905872    }
    906873    public void actionPerformed(ActionEvent event) {
    907                 // Get the currently selected metadata set.
     874        // Get the currently selected metadata set.
    908875        MetadataSet mds = (MetadataSet) set.getSelectedItem();
    909876        String n = name;
    910                 // If we've been forced to go this far then any given namespace is complete bollocks.
     877        // If we've been forced to go this far then any given namespace is complete bollocks.
    911878        while(n.indexOf(".") != -1 && !n.equals(".")) {
    912879        n = n.substring(n.indexOf(".") + 1);
    913880        }
    914                 // However, before we attempt to add a new element to the set, we should check that none already exists.
     881        // However, before we attempt to add a new element to the set, we should check that none already exists.
    915882        Element element = mds.getElement(name);
    916883        if(element == null) {
     
    926893    }
    927894    }
     895
     896    /** a ListCellRenderer for a list of Elements - the displayed value of the element is not Element.toString() */
     897    private class AttributeListCellRenderer extends DefaultListCellRenderer {
     898   
     899    public AttributeListCellRenderer() {
     900        super();
     901    }
     902    public Component getListCellRendererComponent(JList list,
     903                              Object value,
     904                              int index,
     905                              boolean isSelected,
     906                              boolean cellHasFocus) {
     907               
     908        setText(MSMUtils.getValue((Element)value));
     909        if (isSelected) {
     910        setBackground(list.getSelectionBackground());
     911        setForeground(list.getSelectionForeground());
     912        }
     913        else {
     914        setBackground(list.getBackground());
     915        setForeground(list.getForeground());
     916        }
     917        setEnabled(list.isEnabled());
     918        setFont(list.getFont());
     919        setOpaque(true);
     920        return this;
     921    }
     922   
     923   
     924    }
     925
    928926    /** This listener listens for selections within the select element to merge prompt, and once one has been made enables the ok button.*/
    929927    private class ElementListListener
     
    986984    }
    987985    public void actionPerformed(ActionEvent event) {
    988                 // Return the currently selected element
     986        // Return the currently selected element
    989987        result = (ElementWrapper)element.getSelectedItem();
    990988        dialog.setVisible(false);
     
    10201018        element.removeAllItems();
    10211019        MetadataSet mds = (MetadataSet) set.getSelectedItem();
    1022         System.err.println("Set selected: " + mds);
     1020        ///ystem.err.println("Set selected: " + mds);
    10231021        if(mds != null) {
    10241022        NodeList elements = mds.getElements();
     
    10671065    } 
    10681066    }
     1067
    10691068}
  • trunk/gli/src/org/greenstone/gatherer/msm/MetadataSetManager.java

    r4481 r4489  
    566566            matched = true;
    567567            MetadataSet mds_cur = (MetadataSet)mds_hashtable.get(key);
    568             //ystem.err.println("Merging " + mds_new + " into " + mds_cur);
    569             MergeTask task = new MergeTask(mds_cur, mds_new);
    570             task.start();
     568            ///ystem.err.println("Merging " + mds_new + " into " + mds_cur);
     569            mergeMDS(mds_cur, mds_new);
    571570        }
    572571        }
     
    576575        }
    577576        // Fire setChanged() message.
    578         //fireSetChanged(mds_new);
     577        fireSetChanged(mds_new);
    579578        return true;
    580579    }
    581580    // else we cancelled for some reason.
    582581    return false;
    583     }
    584 
    585     private class MergeTask
    586     extends Thread {
    587     private MetadataSet mds_cur;
    588     private MetadataSet mds_new;
    589     MergeTask(MetadataSet mds_cur, MetadataSet mds_new) {
    590         this.mds_cur = mds_cur;
    591         this.mds_new = mds_new;
    592     }
    593     public void run() {
    594         mergeMDS(mds_cur, mds_new);
    595         fireSetChanged(mds_new);
    596     }
    597582    }
    598583
     
    652637        boolean cont = false;
    653638        Element mde_new = mds_new.getElement(i);
    654                 // See if the element already exists in the current set
     639        // See if the element already exists in the current set
    655640        Element mde_cur = mds_cur.getElement(mde_new.getAttribute("name"));
    656641        int option = Declarations.NO_ACTION;
     
    765750     */
    766751    public boolean mergeMDE(MetadataSet mds_cur, Element mde_cur, MetadataSet mds_new, Element mde_new) {
    767     while(true) {
    768         for(Node mdn_new = mde_new.getFirstChild(); mdn_new != null; mdn_new = mdn_new.getNextSibling()) {
    769         // Only merge the nodes whose name is 'Attribute'
    770         if(mdn_new.getNodeName().equals("Attribute")) {
    771             Element att_new = (Element) mdn_new;
    772             // Unfortunately some attributes, such as author, can have several occurances, so match each in turn.
    773             Element temp[] = MSMUtils.getAttributeNodesNamed(mde_cur, att_new.getAttribute("name"));
     752    for(Node mdn_new = mde_new.getFirstChild(); mdn_new != null; mdn_new = mdn_new.getNextSibling()) {
     753        // Only merge the nodes whose name is 'Attribute'
     754        if(mdn_new.getNodeName().equals("Attribute")) {
     755        Element att_new = (Element) mdn_new;
     756        int action = Declarations.NO_ACTION;   
     757        Element replace_att = null; // replace this att with the new one
     758        // Unfortunately some attributes, such as author, can have several occurances, so match each in turn.
     759        Element temp[] = MSMUtils.getAttributeNodesNamed(mde_cur, att_new.getAttribute("name"));
     760        if (temp==null) {           
     761            action = Declarations.ADD;
     762        } else {
     763           
     764            // look for an exact match
    774765            for(int i = 0; temp != null && i < temp.length; i++) {
    775766            Element att_cur = temp[i];
    776             boolean cont = false;
    777             int action = Declarations.NO_ACTION;                     
    778             while(!cont) {
    779                 if(att_cur != null) {
    780                 if(!MSMUtils.attributesEqual(att_cur, att_new)) {
    781                     action = prompt.mDEPrompt(mde_cur, att_cur, mde_new, att_new);
    782                 }
    783                 else {
    784                     action = Declarations.SKIP;
    785                 }
    786                 }
    787                 else {
    788                 action = Declarations.ADD;
    789                 }
    790                 switch (action) {
    791                 case Declarations.REPLACE:
    792                 // Out with the old.
    793                 mde_cur.removeChild(att_cur);
    794                 case Declarations.ADD:
    795                 // Simply add the new attribute. No clash is possible as we have already tested for it.
    796                 MSMUtils.add(mde_cur, att_new);
    797                 cont = true;
    798                 break;
    799                 case Declarations.SKIP:
    800                 // Do nothing. Move on to next attribute.
    801                 cont = true;
    802                 break;
    803                 case Declarations.CANCEL:
    804                 return false;
    805                 default:
    806                 cont = false;
    807                 }
     767            if(MSMUtils.attributesEqual(att_cur, att_new)) {
     768                action = Declarations.SKIP;
     769                break;
    808770            }
    809771            att_cur = null;
    810772            }
    811             temp = null;
    812             att_new = null;
    813         }
    814         }
    815         return mergeMDV(mds_cur, mde_cur, mds_new, mde_new);
    816     }
    817     }
     773            if (action == Declarations.NO_ACTION) {
     774            // we didn't find a match, so we have to prompt teh user for what to do
     775            Object result = prompt.mDEPrompt(mde_cur, temp, mde_new, att_new);
     776            if (result instanceof Integer) {
     777                action = ((Integer)result).intValue();
     778            } else {
     779                // we have a replace action, and the returned object is the Attribute to replace
     780                action = Declarations.REPLACE;
     781                replace_att = (Element)result;
     782            }
     783            }
     784        }
     785
     786        // now do the required action
     787        switch (action) {
     788        case Declarations.REPLACE:
     789            // Out with the old.
     790            mde_cur.removeChild(replace_att);
     791        case Declarations.ADD:
     792            // Simply add the new attribute. No clash is possible as we have already tested for it.
     793            MSMUtils.add(mde_cur, att_new);
     794            break;
     795        case Declarations.SKIP:
     796            // Do nothing. Move on to next attribute.
     797            break;
     798        case Declarations.CANCEL:
     799            return false;
     800        default:
     801        }
     802       
     803        att_new = null;
     804        temp = null;
     805        replace_att = null;
     806        } // if node nmae = attribute   
     807    } //for each child node
     808    return mergeMDV(mds_cur, mde_cur, mds_new, mde_new);
     809    }
     810   
    818811    /** Merge two metadata value trees.
    819812     * @param mds_cur The current MetadataSet.
     
    833826    MetadataSet set = (MetadataSet) mds_hashtable.get(namespace);
    834827    if(set != null) {
    835                 // Bugger. Get the old name -before- we remove the element from the set.
     828        // Bugger. Get the old name -before- we remove the element from the set.
    836829        String old_name = element.toString();
    837                 // Remove the element.
     830        // Remove the element.
    838831        set.removeElement(element.getElement());
    839                 // Fire event.
     832        // Fire event.
    840833        fireElementChanged(new MSMEvent(this, null, old_name));
    841834    }
    842835    else {
    843                 ///ystem.err.println("no such set " + namespace);
     836        ///ystem.err.println("no such set " + namespace);
    844837    }
    845838    // No such set. No such element.
Note: See TracChangeset for help on using the changeset viewer.