Ignore:
Timestamp:
2003-08-21T11:08:09+12:00 (21 years ago)
Author:
jmt12
Message:

Fix 203B017. Completely rewrote format manager view so that it used the consistant add/replace/remove buttons rather than the confusing live idea. Also added code to release/reload collections within the Greenstone local library server - thus changes to the format commands are viewable without having to restart GLI.

File:
1 edited

Legend:

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

    r5164 r5232  
    4646import org.greenstone.gatherer.cdm.Format;
    4747import org.greenstone.gatherer.msm.ElementWrapper;
     48import org.greenstone.gatherer.util.GSDLSiteConfig;
    4849import org.greenstone.gatherer.util.Utility;
    4950import org.w3c.dom.*;
     
    5556    extends DOMProxyListModel {
    5657
     58    static final private String BLANK = "blank";
     59    static final private String FLAG  = "flag";
     60    static final private String VALUE = "value";
     61
     62    /** This flag is set if some change has occured to the format commands. When a collection has been built for previewing, and the greenstone local library server is used, then we have to send commands to remove then add the new collection. */
     63    private boolean formats_changed = false;
    5764    /** The controls used to edit the format commands. */
    5865    private Control controls = null;
     
    8188        add(root, format, target_node);
    8289        Gatherer.c_man.configurationChanged();
     90        formats_changed = true;
    8391    }
    8492    }
     
    101109    return result;
    102110    }
     111
     112    public Format getFormat(String name) {
     113    int model_size = getSize();
     114    for(int index = 0; index < model_size; index++) {
     115        Format format = (Format) getElementAt(index);
     116        if(format.getName().equals(name)) {
     117        return format;
     118        }
     119    }
     120    return null;
     121    }
    103122   
    104123    /** Method to retrieve this managers controls.
     
    118137    remove(format);
    119138    Gatherer.c_man.configurationChanged();
     139    formats_changed = true;
     140    }
     141
     142    private HashMap buildDefaultMappings(ArrayList features_model, ArrayList parts_model) {
     143    System.err.println("buildDefaultMappings(): replace me with something that reads in a data xml file.");
     144    return new HashMap();
     145    }
     146
     147    private ArrayList buildFeatureModel() {
     148    // Rebuild feature model.
     149    ArrayList feature_model = new ArrayList();
     150    // Add the set options
     151    for(int i = 0; i < Format.DEFAULT_FEATURES.length; i++) {
     152        feature_model.add(new Entry(Format.DEFAULT_FEATURES[i]));
     153    }
     154    // Now the classifiers.
     155    for(int j = 0; j < CollectionDesignManager.classifier_manager.getSize(); j++) {
     156        feature_model.add(new Entry(CollectionDesignManager.classifier_manager.getClassifier(j)));
     157    }
     158    Collections.sort(feature_model);
     159    return feature_model;
     160    }
     161
     162    private ArrayList buildPartModel() {
     163    System.err.println("buildPartModel(): replace me with something that reads in a data xml file.");
     164    ArrayList part_model = new ArrayList();
     165    part_model.add("");
     166    part_model.add("DateList");
     167    part_model.add("HList");
     168    part_model.add("Invisible");
     169    part_model.add("VList");
     170    return part_model;
     171    }
     172
     173    private ArrayList buildVariableModel() {
     174    System.err.println("buildVariableModel(): replace me with something that reads in a data xml file.");
     175    ArrayList variable_model = new ArrayList();
     176    variable_model.add("[Text]");
     177    variable_model.add("[link]");
     178    variable_model.add("[/link]");
     179    variable_model.add("[icon]");
     180    variable_model.add("[num]");
     181    variable_model.add("[parent():_]");
     182    variable_model.add("[parent(Top):_]");
     183    variable_model.add("[parent(All'_'):_]");
     184    Vector elements = Gatherer.c_man.getCollection().msm.getAssignedElements();
     185    for(int i = 0; i < elements.size(); i++) {
     186        variable_model.add("[" + ((ElementWrapper)elements.get(i)).getName() + "]");
     187    }
     188    Collections.sort(variable_model);
     189    return variable_model;
    120190    }
    121191
     
    146216    extends JPanel
    147217    implements Control {
    148     /** Do we ignore selection changing events (mainly because we're generating them!) */
    149     private boolean ignore = false;
    150     private boolean new_entry = true;
    151     private boolean ready = false;
    152     private ButtonGroup button_group = null;
    153     private CardLayout card_layout = null;
    154     private Dimension LABEL_SIZE = new Dimension(175,25);
    155     private Format current_format = null;
    156     private String view_type = "custom";
    157     private JButton add = null;
    158     private JButton insert = null;
    159     private JButton preview = null;
    160     private JButton remove = null;
    161     private JComboBox feature = null;
    162     private JComboBox part = null;
    163     private JComboBox special = null;
    164     private JLabel editor_label = null;
    165     private JLabel feature_label = null;
    166     private JLabel format_list_label = null;
    167     private JLabel part_label = null;
    168     private JLabel special_label = null;
    169     private JLabel title = null;
    170     private JLabel value_label = null;
    171     private JList format_list = null;
    172     private JPanel blank_pane = null;
    173     private JPanel control_pane = null;
    174     private JPanel editor_pane = null;
    175     private JPanel feature_pane = null;
    176     private JPanel format_list_pane = null;
    177     private JPanel header_pane = null;
    178     private JPanel inner_button_pane = null;
    179     private JPanel inner_state_pane = null;
    180     private JPanel inner_value_pane = null;
    181     private JPanel options_pane = null;
    182     private JPanel outer_button_pane = null;
    183     private JPanel part_pane = null;
    184     private JPanel special_pane = null;
    185     private JPanel state_pane = null;
    186     private JPanel value_pane = null;
    187     private JPanel view_pane = null;
    188     private JTextArea editor = null;
    189     private JTextArea instructions = null;
    190     private JTextField value = null;
    191     private JToggleButton off = null;
    192     private JToggleButton on = null;
    193     private String BLANK = "blank";
    194     private String CUSTOM = "custom";
    195     private String FLAG = "flag";
    196     private String PARAM = "param";
    197     private Vector part_model = null;
    198     private Vector special_model = null;
     218   
     219    private ArrayList feature_model;
     220    private ArrayList part_model;
     221    private ArrayList variable_model;
     222    private boolean ignore_event = false;
     223    private boolean ready = false; // Are these controls available to be refreshed
     224    private CardLayout card_layout;
     225    private HashMap default_mappings;
     226    private JButton add_button;
     227    private JButton insert_button;
     228    private JButton remove_button;
     229    private JButton replace_button;
     230    private JButton reset_button;
     231    private JCheckBox enabled_checkbox;
     232    private JComboBox feature_combobox;
     233    private JComboBox part_combobox;
     234    private JComboBox variable_combobox;
     235    private JList format_list;
     236    private JTextArea instructions_textarea;
     237    private JTextArea editor_textarea;
     238    private JPanel control_pane;
     239    private JPanel part_pane;
     240    private JPanel selection_pane;
     241    private String view_type;
     242
    199243    public FormatControl() {
    200         ArrayList feature_model = buildFeatureModel();
    201         part_model = new Vector();
    202         part_model.add("");
    203         part_model.add("DateList");
    204         part_model.add("HList");
    205         part_model.add("Invisible");
    206         part_model.add("VList");
    207         special_model = new Vector();
    208         special_model.add("[Text]");
    209         special_model.add("[link]");
    210         special_model.add("[/link]");
    211         special_model.add("[icon]");
    212         special_model.add("[num]");
    213         special_model.add("[parent():_]");
    214         special_model.add("[parent(Top):_]");
    215         special_model.add("[parent(All'_'):_]");
    216         Vector elements = Gatherer.c_man.getCollection().msm.getAssignedElements();
    217         for(int i = 0; i < elements.size(); i++) {
    218         special_model.add("[" + ((ElementWrapper)elements.get(i)).getName() + "]");
    219         }
    220         Collections.sort(special_model);
     244        feature_model = buildFeatureModel();
     245        part_model = buildPartModel();
     246        variable_model = buildVariableModel();
     247        default_mappings = buildDefaultMappings(feature_model, part_model);
     248
    221249        // Create
    222         add = new JButton(get("Add"));
    223         add.setEnabled(false);
    224         blank_pane = new JPanel();
    225         button_group = new ButtonGroup();
     250        JPanel instructions_pane = new JPanel();
     251        JLabel title_label = new JLabel(get("Title"));
     252        title_label.setHorizontalAlignment(JLabel.CENTER);
     253        title_label.setOpaque(true);
     254        instructions_textarea = new JTextArea(get("Instructions"));
     255        instructions_textarea.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
     256        instructions_textarea.setEditable(false);
     257        instructions_textarea.setLineWrap(true);
     258        instructions_textarea.setRows(6);
     259        instructions_textarea.setWrapStyleWord(true);
     260
     261        JLabel format_label = new JLabel(get("Assigned_Formats"));
     262        format_list = new JList(model);
     263
     264        selection_pane = new JPanel();
     265        JPanel feature_pane = new JPanel();
     266        JLabel feature_label = new JLabel(get("Feature"));
     267        feature_label.setPreferredSize(Utility.LABEL_SIZE);
     268        feature_combobox = new JComboBox(feature_model.toArray());
     269        feature_combobox.setEditable(false);
     270        part_pane = new JPanel();
     271        JLabel part_label = new JLabel(get("Part"));
     272        part_label.setPreferredSize(Utility.LABEL_SIZE);
     273        part_combobox = new JComboBox(part_model.toArray());
     274        part_combobox.setEditable(false);
     275
     276        JPanel center_pane = new JPanel();
     277
    226278        card_layout = new CardLayout();
    227279        control_pane = new JPanel();
    228         editor = new JTextArea();
    229         editor.setBackground(Gatherer.config.getColor("coloring.editable_background", false));
    230         editor.setCaretPosition(0);
    231         editor.setLineWrap(true);
    232         editor.setWrapStyleWord(false);
    233         editor_label = new JLabel(get("Editor"));
    234         editor_label.setHorizontalAlignment(JLabel.CENTER);
    235         editor_pane = new JPanel();
    236         feature = new JComboBox(feature_model.toArray());
    237         feature.setEditable(false);
    238         feature_label = new JLabel(get("Feature"));
    239         feature_label.setPreferredSize(LABEL_SIZE);
    240         format_list = new JList(model);
    241         format_list_label = new JLabel(get("Assigned_Formats"));
    242         format_list_pane = new JPanel();
    243         feature_pane = new JPanel();
    244         header_pane = new JPanel();
    245         inner_button_pane = new JPanel();
    246         inner_state_pane = new JPanel();
    247         inner_value_pane = new JPanel();
    248         insert = new JButton(get("Insert"));
    249         instructions = new JTextArea(get("Instructions"));
    250         instructions.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
    251         instructions.setEditable(false);
    252         instructions.setLineWrap(true);
    253         instructions.setRows(6);
    254         instructions.setWrapStyleWord(true);
    255         off = new JToggleButton(get("Off"));
    256         off.setSelected(false);
    257         on = new JToggleButton(get("On"));
    258         on.setSelected(true);
    259         options_pane = new JPanel();
    260         outer_button_pane = new JPanel();
    261         part = new JComboBox(part_model);
    262         part.setEditable(false);
    263         part_label = new JLabel(get("Part"));
    264         part_label.setPreferredSize(LABEL_SIZE);
    265         part_pane = new JPanel();
    266         preview = new JButton(get("Preview"));
    267         preview.setEnabled(false);
    268         remove = new JButton(get("Remove"));
    269         remove.setEnabled(false);
    270         special = new JComboBox(special_model);
    271         special_label = new JLabel(get("Special"));
    272         special_label.setHorizontalAlignment(JLabel.CENTER);
    273         special_pane = new JPanel();
    274         state_pane = new JPanel();
    275         title = new JLabel(get("Title"));
    276         title.setHorizontalAlignment(JLabel.CENTER);
    277         title.setOpaque(true);
    278         value = new JTextField();
    279         value_label = new JLabel(get("Value"));
    280         value_pane = new JPanel();
    281         view_pane = new JPanel();
     280
     281        JPanel blank_pane = new JPanel();
     282
     283        JPanel editor_pane = new JPanel();
     284        JPanel editor_header_pane = new JPanel();
     285        JLabel editor_label = new JLabel(get("Editor"));
     286        reset_button = new JButton(get("Reset"));
     287        reset_button.setMnemonic(KeyEvent.VK_T);
     288        editor_textarea = new JTextArea();
     289        editor_textarea.setBackground(Gatherer.config.getColor("coloring.editable_background", false));
     290        editor_textarea.setCaretPosition(0);
     291        editor_textarea.setLineWrap(true);
     292        editor_textarea.setRows(6);
     293        editor_textarea.setWrapStyleWord(false);
     294
     295        JPanel variable_pane = new JPanel();
     296        JLabel variable_label = new JLabel(get("Variable"));
     297        variable_combobox = new JComboBox(variable_model.toArray());
     298        insert_button = new JButton(get("Insert"));
     299        insert_button.setMnemonic(KeyEvent.VK_I);
     300
     301        JPanel flag_pane = new JPanel();
     302        enabled_checkbox = new JCheckBox(get("Enabled"));
     303
     304        JPanel button_pane = new JPanel();
     305        add_button = new JButton(get("Add"));
     306        add_button.setEnabled(false);
     307        add_button.setMnemonic(KeyEvent.VK_A);
     308        replace_button = new JButton(get("Replace"));
     309        replace_button.setEnabled(false);
     310        replace_button.setMnemonic(KeyEvent.VK_C);
     311        remove_button = new JButton(get("Remove"));
     312        remove_button.setEnabled(false);
     313        remove_button.setMnemonic(KeyEvent.VK_R);
     314
    282315        // Connect
    283         add.addActionListener(new AddListener());
    284         button_group.add(on);
    285         button_group.add(off);
    286         editor.getDocument().addDocumentListener(new EditorListener());
    287         feature.addActionListener(new FeatureListener());
     316        add_button.addActionListener(new AddListener());
     317        insert_button.addActionListener(new InsertListener());
     318        remove_button.addActionListener(new RemoveListener());
     319        replace_button.addActionListener(new ReplaceListener());
     320        reset_button.addActionListener(new ResetListener());
     321        enabled_checkbox.addActionListener(new EnabledListener());
     322        feature_combobox.addActionListener(new FeatureListener());
     323        part_combobox.addActionListener(new PartListener());
     324        editor_textarea.getDocument().addDocumentListener(new EditorListener());
    288325        format_list.addListSelectionListener(new FormatListListener());
    289         insert.addActionListener(new InsertListener());
    290         off.addActionListener(new StateListener());
    291         on.addActionListener(new StateListener());
    292         part.addActionListener(new PartListener());
    293         preview.addActionListener(new PreviewListener());
    294         remove.addActionListener(new RemoveListener());
    295         value.addKeyListener(new ValueListener());
    296                 // Layout
    297         instructions.setBorder(BorderFactory.createEmptyBorder(2,5,2,5));
    298 
    299         header_pane.setLayout(new BorderLayout());
    300         header_pane.add(title, BorderLayout.NORTH);
    301         header_pane.add(new JScrollPane(instructions), BorderLayout.CENTER);
    302 
    303         format_list_label.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
    304 
    305         format_list_pane.setLayout(new BorderLayout());
    306         format_list_pane.add(format_list_label, BorderLayout.NORTH);
    307         format_list_pane.add(new JScrollPane(format_list), BorderLayout.CENTER);
    308 
    309         feature_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
    310 
    311         feature_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
     326
     327        // Layout
     328        instructions_pane.setLayout(new BorderLayout());
     329        instructions_pane.add(title_label, BorderLayout.NORTH);
     330        instructions_pane.add(new JScrollPane(instructions_textarea), BorderLayout.CENTER);
     331        instructions_pane.add(format_label, BorderLayout.SOUTH);
     332
    312333        feature_pane.setLayout(new BorderLayout());
    313334        feature_pane.add(feature_label, BorderLayout.WEST);
    314         feature_pane.add(feature, BorderLayout.CENTER);
    315 
    316         part_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
    317 
    318         part_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
     335        feature_pane.add(feature_combobox, BorderLayout.CENTER);
     336
    319337        part_pane.setLayout(new BorderLayout());
    320338        part_pane.add(part_label, BorderLayout.WEST);
    321         part_pane.add(part, BorderLayout.CENTER);
    322 
    323         options_pane.setLayout(new GridLayout(2,1));
    324         options_pane.add(feature_pane);
    325         options_pane.add(part_pane);
    326 
    327         inner_state_pane = new JPanel(new GridLayout(1,2));
    328         inner_state_pane.add(on);
    329         inner_state_pane.add(off);
    330 
    331         state_pane.setLayout(new BorderLayout());
    332         state_pane.add(inner_state_pane, BorderLayout.NORTH);
    333         state_pane.add(new JPanel(), BorderLayout.CENTER);
    334 
    335         value_label.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
    336 
    337         inner_value_pane.setLayout(new BorderLayout());
    338         inner_value_pane.add(value_label, BorderLayout.WEST);
    339         inner_value_pane.add(value, BorderLayout.CENTER);
    340 
    341         value_pane.setLayout(new BorderLayout());
    342         value_pane.add(inner_value_pane, BorderLayout.NORTH);
    343         value_pane.add(new JPanel(), BorderLayout.CENTER);
    344 
    345         special_pane.setLayout(new GridLayout(3,1));
    346         special_pane.add(special_label);
    347         special_pane.add(special);
    348         special_pane.add(insert);
     339        part_pane.add(part_combobox, BorderLayout.CENTER);
     340
     341        selection_pane.setLayout(new GridLayout(2,1,0,2));
     342        selection_pane.add(feature_pane);
     343        selection_pane.add(part_pane);
     344
     345        flag_pane.add(enabled_checkbox);
     346
     347        editor_header_pane.setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
     348        editor_header_pane.setLayout(new GridLayout(1,3));
     349        editor_header_pane.add(editor_label);
     350        editor_header_pane.add(new JPanel());
     351        editor_header_pane.add(reset_button);
     352
     353        variable_pane.setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
     354        variable_pane.setLayout(new GridLayout(1,3));
     355        variable_pane.add(variable_label);
     356        variable_pane.add(variable_combobox);
     357        variable_pane.add(insert_button);
    349358
    350359        editor_pane.setLayout(new BorderLayout());
    351         editor_pane.add(editor_label, BorderLayout.NORTH);
    352         editor_pane.add(new JScrollPane(editor), BorderLayout.CENTER);
    353         editor_pane.add(special_pane, BorderLayout.EAST);
    354 
    355                 // Magic for view_pane card layout.
    356         view_pane.setLayout(card_layout);
    357         view_pane.add(editor_pane, CUSTOM);
    358         view_pane.add(state_pane, FLAG);
    359                 //view_pane.add(value_pane, PARAM);
    360 
    361         inner_button_pane.setLayout(new GridLayout(1,2));
    362         inner_button_pane.add(add);
    363         inner_button_pane.add(remove);
    364 
    365         outer_button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
    366         outer_button_pane.setLayout(new GridLayout(1,1));
    367         outer_button_pane.add(inner_button_pane);
    368                 //outer_button_pane.add(preview);
    369 
    370         control_pane.setLayout(new BorderLayout());
    371         control_pane.add(options_pane, BorderLayout.NORTH);
    372         control_pane.add(view_pane, BorderLayout.CENTER);
    373         control_pane.add(outer_button_pane, BorderLayout.SOUTH);
     360        editor_pane.add(editor_header_pane, BorderLayout.NORTH);
     361        editor_pane.add(new JScrollPane(editor_textarea), BorderLayout.CENTER);
     362        editor_pane.add(variable_pane, BorderLayout.SOUTH);
     363
     364        control_pane.setLayout(card_layout);
     365        control_pane.add(flag_pane, FLAG);
     366        control_pane.add(editor_pane, VALUE);
     367        control_pane.add(blank_pane, BLANK);
     368
     369        button_pane.setLayout(new GridLayout(1,3));
     370        button_pane.add(add_button);
     371        button_pane.add(replace_button);
     372        button_pane.add(remove_button);
     373
     374        center_pane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(get("Editing_Controls")), BorderFactory.createEmptyBorder(2,2,2,2)));
     375        center_pane.setLayout(new BorderLayout());
     376        center_pane.add(selection_pane, BorderLayout.NORTH);
     377        center_pane.add(control_pane, BorderLayout.CENTER);
     378        center_pane.add(button_pane, BorderLayout.SOUTH);
    374379
    375380        setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    376381        setLayout(new BorderLayout());
    377         add(header_pane, BorderLayout.NORTH);
    378         add(format_list_pane, BorderLayout.CENTER);
    379         add(control_pane, BorderLayout.SOUTH);
     382        add(instructions_pane, BorderLayout.NORTH);
     383        add(new JScrollPane(format_list), BorderLayout.CENTER);
     384        add(center_pane, BorderLayout.SOUTH);
    380385        ready = true;
    381386    }
     
    389394        // This is only necessary if the components have been realized
    390395        if(ready) {
     396        formats_changed = false;
    391397        model.refresh();
    392         ArrayList feature_model = buildFeatureModel();
     398        feature_model = buildFeatureModel();
    393399        // Remember the current selection
    394         Object selected_object = feature.getSelectedItem();
    395         feature.setModel(new DefaultComboBoxModel(feature_model.toArray()));
    396         // Now resotre the selected object as best as possible
    397         feature.setSelectedItem(selected_object);
    398         if(instructions != null) {
    399             instructions.setCaretPosition(0);
     400        Object selected_object = feature_combobox.getSelectedItem();
     401        feature_combobox.setModel(new DefaultComboBoxModel(feature_model.toArray()));
     402        // Now restore the selected object as best as possible
     403        feature_combobox.setSelectedItem(selected_object);
     404        selected_object = null;
     405        if(instructions_textarea != null) {
     406            instructions_textarea.setCaretPosition(0);
    400407        }
    401408        }   
     
    408415        Format format = (Format) model.getElementAt(i);
    409416        format.update();
    410         }
    411     }
    412 
    413     private ArrayList buildFeatureModel() {
    414         // Rebuild feature model.
    415         ArrayList feature_model = new ArrayList();
    416         // Add the set options
    417         for(int i = 0; i < Format.DEFAULT_FEATURES.length; i++) {
    418         feature_model.add(new Entry(Format.DEFAULT_FEATURES[i]));
    419         }
    420         // Now the classifiers.
    421         for(int j = 0; j < CollectionDesignManager.classifier_manager.getSize(); j++) {
    422         feature_model.add(new Entry(CollectionDesignManager.classifier_manager.getClassifier(j)));
    423         }
    424         Collections.sort(feature_model);
    425         return feature_model;
    426     }
    427 
    428     /** Formats the formatting string so that it contains safe characters and isn't enclosed in speech marks etc. (Ironic eh?)
    429      * @see java.lang.StringBuffer
    430      */
    431     private String format(String raw) {
    432         String safe = null;
    433         if(raw != null && raw.length() > 0) {
    434         StringBuffer temp = new StringBuffer(raw);
    435         // Remove quotes at start and end. Look at my wiggly save three lines of code skills.
    436         char start = ' ';
    437         while(temp.length() > 0 && (start = temp.charAt(0)) == '\"' || start == '\'') {
    438             temp.delete(0, 1);
    439         }
    440         int length = 0;
    441         char end = ' ';
    442         while((length = temp.length()) > 0 && (end = temp.charAt(length - 1)) == '\"' || end == '\'') {
    443             temp.delete(length - 1, length);
    444         }
    445         // Now escape quotes within the format string
    446         int quote_index = -1;
    447         while((quote_index = temp.indexOf("\"", quote_index + 1)) != -1) {
    448             temp.replace(quote_index, quote_index + 1, "\\\"");
    449             quote_index = quote_index + 1;
    450         }
    451         // Done.
    452         safe = temp.toString();
    453         }
    454         return safe;
    455     }
    456     /** Remove safe characters from string replacing them with unsafe ones.
    457             * @see java.lang.StringBuffer
    458             */
    459     private String unformat(String safe) {
    460         String raw = null;
    461         if(safe != null && safe.length() > 0) {
    462         StringBuffer temp = new StringBuffer(safe);
    463         int quote_index = -1;
    464         while((quote_index = temp.indexOf("\\\"")) != -1) {
    465             temp.replace(quote_index, quote_index + 2, "\"");
    466         }
    467         raw = temp.toString();
    468         }
    469         return raw;
    470     }
    471 
    472     /** Listens for clicks on the add button, and if the relevant details are provided adds a new format. */
     417        format = null;
     418        }
     419
     420        // Now if the formats have changed, and the greenstone local library is being used and the collection has been built for previewing, then remove the add the collection to Greenstone to ensure the formatting updates correctly.
     421        if(formats_changed && Gatherer.c_man.built() && Gatherer.config.exec_file != null) {
     422        // Release the collection
     423        Gatherer.g_man.preview_pane.configServer(GSDLSiteConfig.RELEASE_COMMAND + Gatherer.c_man.getCollection().getName());
     424        // Then re-add it to force format commands to be processed
     425        Gatherer.g_man.preview_pane.configServer(GSDLSiteConfig.ADD_COMMAND + Gatherer.c_man.getCollection().getName());
     426        }
     427        formats_changed = false;
     428    }
     429
     430    /** Listens for clicks on the add button, and if the relevant details are provided adds a new format. Note that formats are responsible for codecing the values into something that can be a) stored in a DOM and b) written to file */
    473431    private class AddListener
    474432        implements ActionListener {
    475433        public void actionPerformed(ActionEvent event) {
    476         //ignore = true;
    477         Entry entry = (Entry)feature.getSelectedItem();
     434        ignore_event = true; // Prevent format_list excetera propagating events
     435        Entry entry = (Entry)feature_combobox.getSelectedItem();
    478436        Object f = entry.getFeature();
    479         String p = (String)part.getSelectedItem();
     437        String p = (String)part_combobox.getSelectedItem();
     438        Format format = null;
    480439        if(view_type.equals(FLAG)) {
    481             current_format = new Format(f, p, on.isSelected());
     440            format = new Format(f, p, enabled_checkbox.isSelected());
    482441        }
    483442        else {
    484             current_format = new Format(f, p, format(Utility.stripNL(editor.getText())));
    485         }
    486         addFormat(current_format);
    487         add.setEnabled(false);
    488         remove.setEnabled(true);
     443            format = new Format(f, p, editor_textarea.getText());
     444        }
     445        addFormat(format);
     446        add_button.setEnabled(false);
     447        replace_button.setEnabled(false);
     448        remove_button.setEnabled(true);
    489449        // Update list selection
    490         format_list.setSelectedValue(current_format, true);
    491         new_entry = false;
    492         //ignore = false;
     450        format_list.setSelectedValue(format, true);
     451        format = null;
     452        p = null;
     453        f = null;
     454        entry = null;
     455        ignore_event = false;
    493456        }
    494457    }
     
    510473
    511474        public void update() {
    512         String safe = format(editor.getText());
    513         if(!ignore && current_format != null) {
    514             // We have just performed an edit. Immediately update the format and model.
    515             if(safe != null) {
    516             current_format.setValue(safe);
    517             }
    518             else {
    519             current_format.setValue("");
    520             }
    521             Gatherer.c_man.configurationChanged();
    522             model.refresh(current_format);
    523         }
    524         Entry entry = (Entry) feature.getSelectedItem();
    525         String name = entry.toString();
    526         if(!(name.length() == 0 && ((String)part.getSelectedItem()).length() == 0) && safe != null && safe.length() != 0 && new_entry) {
    527             add.setEnabled(true);
     475        // Determine if replace should be enabled
     476        if(!format_list.isSelectionEmpty()) {
     477            Format format = (Format)format_list.getSelectedValue();
     478            replace_button.setEnabled(!format.isParamType() && editor_textarea.getText() != format.getValue());
    528479        }
    529480        else {
    530             add.setEnabled(false);
    531         }
    532         }
    533     }
    534 
    535     /** This object provides a wrapping around an entry in the format target control, which is tranparent as to whether it is backed by a String or a Classifier. */
    536     private class Entry
    537         implements Comparable {
    538         private Classifier classifier = null;
    539         private String text = null;
    540         public Entry(Object object) {
    541         if(object instanceof Classifier) {
    542             classifier = (Classifier)object;
    543         }
    544         else if(object instanceof String) {
    545             text = (String)object;
    546         }
    547         else {
    548             text = "";
    549         }
    550         }
    551         public Entry(String text) {
    552         this.text = text;
    553         }
    554         public int compareTo(Object object) {
    555         if(object == null) {
    556             return 1;
    557         }
    558         if(toString() == null) {
    559             return -1;
    560         }
    561         else {
    562             String object_str = object.toString();
    563             if(object_str == null) {
    564             return 1;
    565             }
    566             return toString().compareTo(object_str);
    567         }
    568         }
    569         public boolean equals(Object object) {
    570         if(compareTo(object) == 0) {
    571             return true;
    572         }
    573         return false;
    574         }
    575         public Classifier getClassifier() {
    576         return classifier;
    577         }
    578         public Object getFeature() {
    579         if(classifier != null) {
    580             return classifier;
    581         }
    582         return text;
    583         }
    584         public String toString() {
    585         if(classifier != null) {
    586             String name = classifier.toString();
    587             return name.substring(9);
    588         }
    589         return text;
     481            replace_button.setEnabled(false);
     482        }
     483        }
     484    }
     485
     486    private class EnabledListener
     487        implements ActionListener {
     488       
     489        public void actionPerformed(ActionEvent event) {
     490        // If there is a current format selected, and the value of enable_checkbox is now different than to value in it, then enable to replace button.
     491        if(!format_list.isSelectionEmpty()) {
     492            Format format = (Format)format_list.getSelectedValue();
     493            replace_button.setEnabled(format.isParamType() && enabled_checkbox.isSelected() != format.getState());
     494        }
     495        // Thats it. Add would have been enabled upon feature/part selection depending if no existing format, um, existed.
    590496        }
    591497    }
     
    594500        implements ActionListener {
    595501        public void actionPerformed(ActionEvent event) {
    596         if(!ignore) {
    597             current_format = null;
    598             Entry entry = (Entry) feature.getSelectedItem();
     502        if(!ignore_event) {
     503            ignore_event = true;
     504            // Step one: reset part
     505            part_combobox.setSelectedIndex(0);
     506            // Step two: the rest
     507            Entry entry = (Entry) feature_combobox.getSelectedItem();
    599508            String name = entry.toString();
    600509            if(Format.isParamType(name)) {
    601510            // Flags first.
    602             part.setEnabled(false);
    603             part_pane.remove(part);
    604             card_layout.show(view_pane, FLAG);
     511            selection_pane.remove(part_pane);
     512            card_layout.show(control_pane, FLAG);
    605513            view_type = FLAG;
    606             add.setEnabled(true); // One of the options must be selected.
    607514            }
    608515            else {
    609             part.setEnabled(true);
    610             part_pane.add(part, BorderLayout.CENTER);
    611             card_layout.show(view_pane, CUSTOM);
    612             view_type = CUSTOM;
    613             if(!(name.length() == 0 && ((String)part.getSelectedItem()).length() == 0) && editor.getText().length() != 0) {
    614                 add.setEnabled(true);
     516            selection_pane.add(part_pane);
     517            card_layout.show(control_pane, VALUE);
     518            view_type = VALUE;
     519            }
     520            control_pane.updateUI();
     521            // Add is only enabled if there isn't already a format for the choosen feature and part. Create a dummy format and test if itsa already in the model
     522            Object f = entry.getFeature();
     523            String p = (String)part_combobox.getSelectedItem();
     524            // You can never add anything to blank-blank
     525            if(f.toString().length() == 0 && p.length() == 0) {
     526            add_button.setEnabled(false);
     527            replace_button.setEnabled(false);
     528            remove_button.setEnabled(false);
     529            }
     530            else {
     531            name = Format.generateName(f, p);
     532            Format format = getFormat(name);
     533            // If there is an existing feature, select it.
     534            if(format != null) {
     535                format_list.setSelectedValue(format, true);
     536                // Now use type to determine what controls are visible, and what have initial values.
     537                if(format.isParamType()) {
     538                // Flags first.
     539                selection_pane.remove(part_pane);
     540                card_layout.show(control_pane, FLAG);
     541                view_type = FLAG;
     542                // Initial value
     543                enabled_checkbox.setSelected(format.getState());
     544                }
     545                else {
     546                selection_pane.add(part_pane);
     547                card_layout.show(control_pane, VALUE);
     548                view_type = VALUE;
     549                // Initial value
     550                editor_textarea.setText(format.getValue());
     551                }
     552                control_pane.updateUI();
     553            remove_button.setEnabled(true);
    615554            }
    616555            else {
    617                 add.setEnabled(false);
     556                format_list.clearSelection();
     557                if(Format.isParamType(name)) {
     558                // Flags first.
     559                selection_pane.remove(part_pane);
     560                card_layout.show(control_pane, FLAG);
     561                view_type = FLAG;
     562                // Initial value
     563                enabled_checkbox.setSelected(false);
     564                }
     565                else {
     566                selection_pane.add(part_pane);
     567                card_layout.show(control_pane, VALUE);
     568                view_type = VALUE;
     569                // Initial value
     570                editor_textarea.setText("");
     571                }
     572                add_button.setEnabled(true);
    618573            }
     574            format = null;
     575            name = null;
    619576            }
    620             control_pane.updateUI();
    621             new_entry = true;
     577            p = null;
     578            f = null;
     579            replace_button.setEnabled(false);
     580            name = null;
     581            entry = null;
     582            ignore_event = false;
    622583        }
    623584        }
     
    627588        implements ListSelectionListener {
    628589        public void valueChanged(ListSelectionEvent event) {
    629         if(!ignore) {
     590        if(!ignore_event) {
    630591            if(!format_list.isSelectionEmpty()) {
    631             ignore = true;
    632             current_format = (Format)format_list.getSelectedValue();
     592            ignore_event = true;
     593            Format format = (Format)format_list.getSelectedValue();
    633594            // Try to match the target, remembering the entries within are Entry's
    634             Entry an_entry = new Entry(current_format.getFeature());
    635             feature.setSelectedItem(an_entry);
     595            Entry an_entry = new Entry(format.getFeature());
     596            feature_combobox.setSelectedItem(an_entry);
    636597            // If we didn't match anything, add it and select it again
    637             Entry result_entry = (Entry) feature.getSelectedItem();
     598            Entry result_entry = (Entry) feature_combobox.getSelectedItem();
    638599            if(!an_entry.equals(result_entry)) {
    639                 feature.insertItemAt(an_entry, feature.getItemCount());
    640                 feature.setSelectedItem(an_entry);
     600                feature_combobox.insertItemAt(an_entry, feature_combobox.getItemCount());
     601                feature_combobox.setSelectedItem(an_entry);
    641602            }
    642603           
    643             if(current_format.canHavePart()) {
    644                 part.setEnabled(true);
     604            if(format.canHavePart()) {
     605                part_combobox.setEnabled(true);
    645606                // Try to match the part.
    646                 String part_entry = current_format.getPart();
    647                 part.setSelectedItem(part_entry);
     607                String part_entry = format.getPart();
     608                part_combobox.setSelectedItem(part_entry);
    648609                // If we didn't match anything, add it and select it again
    649                 String selected_part = (String)part.getSelectedItem();
     610                String selected_part = (String)part_combobox.getSelectedItem();
    650611                if(!part_entry.equals(selected_part)) {
    651                 part.insertItemAt(part_entry, part.getItemCount());
    652                 feature.setSelectedItem(part_entry);
     612                part_combobox.insertItemAt(part_entry, part_combobox.getItemCount());
     613                part_combobox.setSelectedItem(part_entry);
    653614                }
    654615            }
    655616            else {
    656                 part.setEnabled(false);
     617                part_combobox.setEnabled(false);
    657618            }
    658619            // Now use type to determine what controls are visible, and what have initial values.
    659             if(current_format.isParamType()) {
     620            if(format.isParamType()) {
    660621                // Flags first.
    661                 part.setEnabled(false);
    662                 part_pane.remove(part);
    663                 card_layout.show(view_pane, FLAG);
     622                selection_pane.remove(part_pane);
     623                card_layout.show(control_pane, FLAG);
    664624                view_type = FLAG;
    665625                // Initial value
    666                 on.setSelected(current_format.getState());
    667                 off.setSelected(!current_format.getState());
    668                 add.setEnabled(false); // Can only update
     626                enabled_checkbox.setSelected(format.getState());
    669627            }
    670628            else {
    671                 part.setEnabled(true);
    672                 part_pane.add(part, BorderLayout.CENTER);
    673                 card_layout.show(view_pane, CUSTOM);
    674                 view_type = CUSTOM;
     629                selection_pane.add(part_pane);
     630                card_layout.show(control_pane, VALUE);
     631                view_type = VALUE;
    675632                // Initial value
    676                 editor.setText(unformat(current_format.getValue()));
    677                 add.setEnabled(false);
     633                editor_textarea.setText(format.getValue());
    678634            }
    679635            control_pane.updateUI();
    680             ignore = false;
    681             preview.setEnabled(true);
    682             remove.setEnabled(true);
    683             new_entry = false;
     636            remove_button.setEnabled(true);
     637            ignore_event = false;
    684638            }
    685639            else {
    686             preview.setEnabled(false);
    687             remove.setEnabled(false);
     640            remove_button.setEnabled(false);
    688641            }
    689         }
    690         }
    691     }
     642            add_button.setEnabled(false);
     643            replace_button.setEnabled(false);
     644        }
     645        }
     646    }
     647
    692648    private class InsertListener
    693649        implements ActionListener {
    694650        public void actionPerformed(ActionEvent event) {
    695         editor.insert((String)special.getSelectedItem(), editor.getCaretPosition());
    696         }
    697     }
     651        editor_textarea.insert((String)variable_combobox.getSelectedItem(), editor_textarea.getCaretPosition());
     652        }
     653    }
     654
    698655    private class PartListener
    699656        implements ActionListener {
    700657        public void actionPerformed(ActionEvent event) {
    701         if(!ignore) {
    702             current_format = null;
    703             Entry entry = (Entry) feature.getSelectedItem();
    704             String name = entry.toString();
    705             if(!(name.length() == 0 && ((String)part.getSelectedItem()).length() == 0) && editor.getText().length() != 0) {
    706             add.setEnabled(true);
     658        if(!ignore_event) {
     659            // Add is only enabled if there isn't already a format for the choosen feature and part. Create a dummy format and test if itsa already in the model
     660            Entry entry = (Entry) feature_combobox.getSelectedItem();
     661            Object f = entry.getFeature();
     662            String p = (String)part_combobox.getSelectedItem();
     663            // You can never add anything to blank-blank
     664            if(f.toString().length() == 0 && p.length() == 0) {
     665            add_button.setEnabled(false);
     666            replace_button.setEnabled(false);
     667            remove_button.setEnabled(false);
    707668            }
    708669            else {
    709             add.setEnabled(false);
     670            String name = Format.generateName(f, p);
     671            Format format = getFormat(name);
     672            // If there is an existing feature, select it.
     673            if(format != null) {
     674                format_list.setSelectedValue(format, true);
     675                // Now use type to determine what controls are visible, and what have initial values.
     676                if(format.isParamType()) {
     677                // Flags first.
     678                selection_pane.remove(part_pane);
     679                card_layout.show(control_pane, FLAG);
     680                view_type = FLAG;
     681                // Initial value
     682                enabled_checkbox.setSelected(format.getState());
     683                }
     684                else {
     685                selection_pane.add(part_pane);
     686                card_layout.show(control_pane, VALUE);
     687                view_type = VALUE;
     688                // Initial value
     689                editor_textarea.setText(format.getValue());
     690                }
     691                control_pane.updateUI();
     692                remove_button.setEnabled(true);
     693            }
     694            else {
     695                format_list.clearSelection();
     696                if(Format.isParamType(name)) {
     697                // Flags first.
     698                selection_pane.remove(part_pane);
     699                card_layout.show(control_pane, FLAG);
     700                view_type = FLAG;
     701                // Initial value
     702                enabled_checkbox.setSelected(false);
     703                }
     704                else {
     705                selection_pane.add(part_pane);
     706                card_layout.show(control_pane, VALUE);
     707                view_type = VALUE;
     708                // Initial value
     709                editor_textarea.setText("");
     710                }
     711                add_button.setEnabled(true);
     712            }
     713            format = null;
     714            name = null;
    710715            }
    711             new_entry = true;
    712         }
    713         }
    714     }
    715     private class PreviewListener
    716         implements ActionListener {
    717         public void actionPerformed(ActionEvent event) {
    718         }
    719     }
     716            p = null;
     717            f = null;
     718            entry = null;
     719            replace_button.setEnabled(false);
     720        }
     721        }
     722    }
     723
    720724    private class RemoveListener
    721725        implements ActionListener {
     
    724728            removeFormat((Format)format_list.getSelectedValue());
    725729            // Change buttons
    726             add.setEnabled(true);
    727             remove.setEnabled(false);
    728         }
    729         }
    730     }
    731     private class StateListener
     730            add_button.setEnabled(true);
     731            replace_button.setEnabled(false);
     732        }
     733        remove_button.setEnabled(false);
     734        }
     735    }
     736
     737    private class ReplaceListener
    732738        implements ActionListener {
     739
    733740        public void actionPerformed(ActionEvent event) {
    734         if(!ignore && current_format != null) {
    735             // We have just performed an edit. Immediately update the format and model.
    736             current_format.setState(on.isSelected());
    737             model.refresh(current_format);
    738         }
    739         }
    740     }
    741     private class ValueListener
    742         extends KeyAdapter {
    743         public void keyReleased(KeyEvent event) {
    744         if(!ignore && current_format != null) {
    745             // We have just performed an edit. Immediately update the format and model.
    746             current_format.setValue(value.getText());
    747             model.refresh(current_format);
    748         }
    749         }
     741        /** @todo **/
     742        }
     743    }
     744
     745    private class ResetListener
     746        implements ActionListener {
     747
     748        public void actionPerformed(ActionEvent event) {
     749        /** @todo **/
     750        }
     751    }
     752    }
     753
     754    /** This object provides a wrapping around an entry in the format target control, which is tranparent as to whether it is backed by a String or a Classifier. */
     755    private class Entry
     756    implements Comparable {
     757    private Classifier classifier = null;
     758    private String text = null;
     759   
     760    public Entry(Object object) {
     761        if(object instanceof Classifier) {
     762        classifier = (Classifier)object;
     763        }
     764        else if(object instanceof String) {
     765        text = (String)object;
     766        }
     767        else {
     768        text = "";
     769        }
     770    }
     771   
     772    public Entry(String text) {
     773        this.text = text;
     774    }
     775   
     776    public int compareTo(Object object) {
     777        if(object == null) {
     778        return 1;
     779        }
     780        if(toString() == null) {
     781        return -1;
     782        }
     783        else {
     784        String object_str = object.toString();
     785        if(object_str == null) {
     786            return 1;
     787        }
     788        return toString().compareTo(object_str);
     789        }
     790    }
     791   
     792    public boolean equals(Object object) {
     793        if(compareTo(object) == 0) {
     794        return true;
     795        }
     796        return false;
     797    }
     798   
     799    public Classifier getClassifier() {
     800        return classifier;
     801    }
     802   
     803    public Object getFeature() {
     804        if(classifier != null) {
     805        return classifier;
     806        }
     807        return text;
     808    }
     809   
     810    public String toString() {
     811        if(classifier != null) {
     812        // Return the classifier - less the 'classify ' prefix
     813        return classifier.toString().substring(9);
     814        }
     815        return text;
    750816    }
    751817    }
Note: See TracChangeset for help on using the changeset viewer.