Changeset 4436


Ignore:
Timestamp:
2003-05-30T14:53:48+12:00 (21 years ago)
Author:
jmt12
Message:

2030094 & 2030095: Fixed two places where NPEs were being thrown, mostly because of accessing non-existing columns of a table, or indeed the wrong table.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/mem/MetadataEditorManager.java

    r4420 r4436  
    7777    /** A card layout is used to switch between the two differing detail views. */
    7878    private CardLayout card_layout = null;
     79    /** A card layout is used to switch between a value tree or an empty placeholder (if no value tree available). */
     80    private CardLayout element_values_layout = null;
    7981    /** Um, the size of the screen I'd guess. */
    8082    private Dimension screen_size = null;
     
    8486    private MetadataEditorManager self = null;
    8587    private int current_attribute = -1;
     88    private int current_attribute_type = -1;
    8689    private JButton add_attribute = null;
    8790    private JButton add_element = null;
     
    104107    private JScrollPane set_attributes_scroll = null;
    105108    private JPanel details_pane = null;
     109    private JPanel element_values_pane = null;
    106110    private KeepTreeRootExpandedListener keep_root_expanded_listener = null;
    107111    private MEMModel model = null;
     
    129133    static final private String PROFILE = "profile";
    130134    static final private String SET = "set";
     135    static final private String VALUES = "values";
    131136    /** Constructor. */
    132137    public MetadataEditorManager() {
     
    225230    element_attributes.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    226231
    227     JPanel element_values_pane = new JPanel();
     232    element_values_layout = new CardLayout();
     233    element_values_pane = new JPanel();
    228234    element_values_pane.setOpaque(false);
    229235    element_values = new SmarterTree();
     
    233239    element_values.setSelectionColor(Gatherer.config.getColor("coloring.collection_selection_background", false));
    234240    element_values.setSelectedTextColor(Gatherer.config.getColor("coloring.collection_selection_foreground", false));
     241
     242    JPanel element_novalues_pane = new JPanel();
    235243       
    236244    keep_root_expanded_listener = new KeepTreeRootExpandedListener();
     
    371379         
    372380    element_values_pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(get("Values")), BorderFactory.createEmptyBorder(2,2,2,2)));
    373     element_values_pane.setLayout(new BorderLayout());
    374     element_values_pane.add(new JScrollPane(element_values), BorderLayout.CENTER);
     381    //element_values_pane.setLayout(new BorderLayout());
     382    //element_values_pane.add(new JScrollPane(element_values), BorderLayout.CENTER);
     383    element_values_pane.setLayout(element_values_layout);
     384    element_values_pane.add(element_novalues_pane, BLANK);
     385    element_values_pane.add(new JScrollPane(element_values), VALUES);
    375386         
    376387    element_inner_pane.setLayout(new GridLayout(2,1));
     
    531542        setSize(ADD_OR_EDIT_ATTRIBUTE_SIZE);
    532543        name_to_values = new HashMap();
    533                 // Creation
     544        // Creation
    534545        JPanel content_pane = (JPanel) getContentPane();
    535546        content_pane.setBackground(Gatherer.config.getColor("coloring.collection_heading_background", false));
     
    625636        if(source == ok_button) {
    626637        boolean cont = true;
     638        AttributeTableModel model = null;
     639        switch(current_attribute_type) {
     640        case MEMNode.COLLECTION:
     641            model = (AttributeTableModel) profile_attributes.getModel();
     642            break;
     643        case MEMNode.ELEMENT:
     644            model = (AttributeTableModel) element_attributes.getModel();
     645            break;
     646        case MEMNode.SET:
     647            model = (AttributeTableModel) set_attributes.getModel();
     648            break;
     649        }
    627650        // Now we add the new or improved values, removing old values if this is an edit.
    628651        // Depends on what we are adding the attribute to.
    629         if(current_collection_file != null) {
    630             String name_str = name.getSelectedItem().toString();
    631             String value_str = value.getText();
    632             AttributeTableModel model = (AttributeTableModel) profile_attributes.getModel();
    633             // Remove the existing one if this is an edit.
    634             if(!add_type && current_attribute != -1) {
    635             String old_source = (String) profile_attributes.getValueAt(current_attribute, 0);
    636             Gatherer.c_man.msm.profiler.removeAction(current_collection_file, old_source);
    637             old_source = null;
    638             model.removeRow(current_attribute);
    639             }
    640             // First thing we check from profiles is that there isn't already an entry for this name.
    641             if(!model.contains(name_str, 0)) {
    642             // Add profile
    643             Gatherer.c_man.msm.profiler.addAction(current_collection_file, name_str, value_str);
    644             // Update attribute table.
    645             model.add(new Attribute(name_str, value_str));
    646             }
    647             else {
    648             // Show an error message and do not proceed.
    649             cont = false;
    650             JOptionPane.showMessageDialog(self, get("Attribute_Already_Exists"), get("General.Error"), JOptionPane.ERROR_MESSAGE);
     652        if(model != null) {
     653            if(current_collection_file != null) {
     654            String name_str = name.getSelectedItem().toString();
     655            String value_str = value.getText();
     656            // Remove the existing one if this is an edit.
     657            if(!add_type && current_attribute != -1) {
     658                String old_source = (String) model.getValueAt(current_attribute, 0);
     659                Gatherer.c_man.msm.profiler.removeAction(current_collection_file, old_source);
     660                old_source = null;
     661                model.removeRow(current_attribute);
     662            }
     663            // First thing we check from profiles is that there isn't already an entry for this name.
     664            if(!model.contains(name_str, 0)) {
     665                // Add profile
     666                Gatherer.c_man.msm.profiler.addAction(current_collection_file, name_str, value_str);
     667                // Update attribute table.
     668                model.add(new Attribute(name_str, value_str));
     669            }
     670            else {
     671                // Show an error message and do not proceed.
     672                cont = false;
     673                JOptionPane.showMessageDialog(self, get("Attribute_Already_Exists"), get("General.Error"), JOptionPane.ERROR_MESSAGE);
     674            }
     675            value_str = null;
     676            name_str = null;
     677            }
     678            else if(current_element != null) {
     679            // Add the attribute, even if one of the same name already exists. Maybe one day someone would like to enforce the occurance rules but not me and not today.
     680            String name_str = name.getSelectedItem().toString();
     681            String language_code = ((Language)language_box.getSelectedItem()).getCode();
     682            String value_str = value.getText();
     683            // Remove the existing one if this is an edit.
     684            if(!add_type && current_attribute != -1) {
     685                model.removeRow(current_attribute);
     686            }
     687            // Add attribute
     688            current_element.addAttribute(name_str, language_code, value_str);
     689            // Update the attribute table
     690            model.add(new Attribute(name_str, language_code, value_str));
     691            value_str = null;
     692            language_code = null;
     693            name_str = null;
     694            }
     695            else if(current_set != null) {
     696            String name_str = name.getSelectedItem().toString();
     697            String value_str = value.getText();                     
     698            // Remove the existing one if this is an edit.
     699            if(!add_type && current_attribute != -1) {
     700                model.removeRow(current_attribute);
     701            }
     702            // First thing we check from profiles is that there isn't already an entry for this name.
     703            if(!model.contains(name_str, 0)) {
     704                // Add profile
     705                current_set.addAttribute(name_str, value_str);
     706                // Update attribute table.
     707                model.add(new Attribute(name_str, value_str));
     708            }
     709            // Otherwise provide an error message and do not proceed.
     710            else {
     711                cont = false;
     712                JOptionPane.showMessageDialog(self, get("Attribute_Already_Exists"), get("General.Error"), JOptionPane.ERROR_MESSAGE);
     713            }
     714            value_str = null;
     715            name_str = null;
     716            }
     717            // Hide dialog if we are allowed to continue.
     718            if(cont) {
     719            setVisible(false);
    651720            }
    652721            model = null;
    653             value_str = null;
    654             name_str = null;
    655         }
    656         else if(current_element != null) {
    657             // Add the attribute, even if one of the same name already exists. Maybe one day someone would like to enforce the occurance rules but not me and not today.
    658             String name_str = name.getSelectedItem().toString();
    659             String language_code = ((Language)language_box.getSelectedItem()).getCode();
    660             String value_str = value.getText();
    661             AttributeTableModel model = (AttributeTableModel) element_attributes.getModel();
    662             // Remove the existing one if this is an edit.
    663             if(!add_type && current_attribute != -1) {
    664             model.removeRow(current_attribute);
    665             }
    666             // Add attribute
    667             current_element.addAttribute(name_str, language_code, value_str);
    668             // Update the attribute table
    669             model.add(new Attribute(name_str, language_code, value_str));
    670             model = null;
    671             value_str = null;
    672             language_code = null;
    673             name_str = null;
    674         }
    675         else if(current_set != null) {
    676             String name_str = name.getSelectedItem().toString();
    677             String value_str = value.getText();                     
    678             // First thing we check from profiles is that there isn't already an entry for this name.
    679             AttributeTableModel model = (AttributeTableModel) set_attributes.getModel();
    680             // Remove the existing one if this is an edit.
    681             if(!add_type && current_attribute != -1) {
    682             model.removeRow(current_attribute);
    683             }
    684             // First thing we check from profiles is that there isn't already an entry for this name.
    685             if(!model.contains(name_str, 0)) {
    686             // Add profile
    687             current_set.addAttribute(name_str, value_str);
    688             // Update attribute table.
    689             model.add(new Attribute(name_str, value_str));
    690             }
    691             // Otherwise provide an error message and do not proceed.
    692             else {
    693             cont = false;
    694             JOptionPane.showMessageDialog(self, get("Attribute_Already_Exists"), get("General.Error"), JOptionPane.ERROR_MESSAGE);
    695             }
    696             model = null;
    697             value_str = null;
    698             name_str = null;
    699         }
    700         // Hide dialog if we are allowed to continue.
    701         if(cont) {
    702             setVisible(false);
    703722        }
    704723        }
     
    825844        }
    826845        else if(current_attribute != -1) {
     846            AttributeTableModel model = null;
     847            switch(current_attribute_type) {
     848            case MEMNode.COLLECTION:
     849            model = (AttributeTableModel) profile_attributes.getModel();
     850            break;
     851            case MEMNode.ELEMENT:
     852            model = (AttributeTableModel) element_attributes.getModel();
     853            break;
     854            case MEMNode.SET:
     855            model = (AttributeTableModel) set_attributes.getModel();
     856            break;
     857            }
    827858            add_type = false;
    828859            setTitle(get("EditAttribute"));
    829             String name_str = (String) profile_attributes.getValueAt(current_attribute, 0);
    830             String value_str = (String) profile_attributes.getValueAt(current_attribute, 1);
     860            String name_str = (String) model.getValueAt(current_attribute, 0);
     861            String value_str = (String) model.getValueAt(current_attribute, model.getColumnCount() - 1);
     862            model = null;
    831863            // Retrieve the appropriate value model
    832864            java.util.List values = (java.util.List) name_to_values.get(name_str);
     
    838870            name_str = null;
    839871            for(int i = 0; i < values.size(); i++) {
    840             value.add(new NameElementWrapperEntry(values.get(i)));
     872            Object temp_value = values.get(i);
     873            if(temp_value instanceof ElementWrapper) {
     874                value.add(new NameElementWrapperEntry(temp_value));
     875            }
     876            else {
     877                value.add(temp_value);
     878            }
    841879            }
    842880            values = null;
     
    16781716    }
    16791717    public void valueChanged(ListSelectionEvent event) {
    1680         if((current_attribute = table.getSelectedRow()) != -1) {
    1681         edit_attribute.setEnabled(true);
    1682         remove_attribute.setEnabled(true);
    1683         }
    1684         else {
    1685         current_attribute = -1;
    1686         edit_attribute.setEnabled(false);
    1687         remove_attribute.setEnabled(false);
     1718        if(!event.getValueIsAdjusting()) {
     1719        if((current_attribute = table.getSelectedRow()) != -1) {
     1720            edit_attribute.setEnabled(true);
     1721            remove_attribute.setEnabled(true);
     1722        }
     1723        else {
     1724            current_attribute = -1;
     1725            edit_attribute.setEnabled(false);
     1726            remove_attribute.setEnabled(false);
     1727        }
    16881728        }
    16891729    }
     
    17221762        AttributeTableModel atm = null;
    17231763        TreeSet attributes = null;
    1724         switch(current_node.getType()) {
     1764        current_attribute_type = current_node.getType();
     1765        switch(current_attribute_type) {
    17251766        case MEMNode.COLLECTION:
    17261767            current_collection_file = current_node.toString();
     
    17601801            // Be careful not to add the keep_root_expanded_listener over and over!
    17611802            GValueModel value_model = Gatherer.c_man.msm.getValueTree(current_element);
    1762             value_model.removeTreeModelListener(keep_root_expanded_listener); // Remove it if its already there
    1763             value_model.addTreeModelListener(keep_root_expanded_listener); // Then add again.
    1764             element_values.setModel(value_model);
     1803            if(value_model != null) {
     1804            value_model.removeTreeModelListener(keep_root_expanded_listener); // Remove it if its already there
     1805            value_model.addTreeModelListener(keep_root_expanded_listener); // Then add again.
     1806            element_values.setModel(value_model);
     1807            element_values_layout.show(element_values_pane, VALUES);
     1808            // Meanwhile disable/enable controls depending on this Element selection.
     1809            setControls(true, false, false, false, false, true, true, false, false, true, false, false);
     1810            }
     1811            else {
     1812            element_values_layout.show(element_values_pane, BLANK);
     1813            // Meanwhile disable/enable controls depending on this Element selection.
     1814            setControls(true, false, false, false, false, false, true, false, false, false, false, false);
     1815            }
    17651816            card_layout.show(details_pane, ELEMENT);
    1766             // Meanwhile disable/enable controls depending on this Element selection.
    1767             setControls(true, false, false, false, false, true, true, false, false, true, false, false);
    17681817            break;
    17691818        case MEMNode.SET:
Note: See TracChangeset for help on using the changeset viewer.