Today I've been working on a removeRule and addRule method that will show and 
hide content.

What I have is a checkbox named 0_name which is for the 0-(1st layer) and the 
attribute is named name

Based on the checkbox being clicked I either want to display or not display the 
labels.
There has to be an easier way then how i'm doing this :-)
If so please let me know.

One of my issues is that I can get a Label for the attribute name to display 
but I cannot get it to disappear even though I see the rule - symbolizer being 
removed from the rule stack.

Any input on how to stream line my solution would be great and if possible 
letting me know why my labels are not disappearing after the checkbox is 
deselected.

Below is my method for the Checkbox creation in the toolbar - map display and 
action event
and 2 other methods: removeRule and addRule

public static void displayMap() throws Exception {

    mapFrame = new JMapFrame(map);
    mapFrame.enableToolBar(true);
    JToolBar toolBar = mapFrame.getToolBar();
    
    JCheckBox cbox = new JCheckBox("Name");
    cbox.setName("0_name");
    toolBar.addSeparator();
    toolBar.add(cbox);
    
    cbox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) 
actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            
            if(selected){
                //add rule
                //checkbox: 0_name
                String split[] = abstractButton.getName().split("_");
                int layerIndex = Integer.parseInt(split[0]);
                String labelField = split[1];
                pointStyle = map.getLayer(layerIndex).getStyle();
                
mapFrame.getMapContext().getLayer(layerIndex).setStyle(addRule(pointStyle,labelField));
            } else {
                //remove rule
                //checkbox: 0_name
                String split[] = abstractButton.getName().split("_");
                int layerIndex = Integer.parseInt(split[0]);
                String labelField = split[1];
                pointStyle = map.getLayer(layerIndex).getStyle();
                
mapFrame.getMapContext().getLayer(layerIndex).setStyle(removeRule(pointStyle,labelField));
            }
            
            mapFrame.repaint();
        }
    });
    
    mapFrame.setSize(600, 600);
    mapFrame.setVisible(true);
}

    public static Style removeRule(Style style, String labelField){

    ArrayList ftsList = (ArrayList)style.featureTypeStyles();
    ListIterator i = ftsList.listIterator();
    while(i.hasNext()){
        FeatureTypeStyle ftStyle = (FeatureTypeStyle)i.next();
        Rule rule = styleFactory.createRule();
        ArrayList rList = (ArrayList)ftStyle.rules();
        ListIterator r = rList.listIterator();
        while(r.hasNext()){
            Rule ruleo = (Rule)r.next();
            ArrayList symList = (ArrayList)ruleo.symbolizers();
            ListIterator s = symList.listIterator();
            while(s.hasNext()){
                Symbolizer ts=(Symbolizer)s.next();
                if(ts.getName().compareTo(labelField)!=0){
                    rule.symbolizers().add(ts);
                }
            }
        } //end rule rList loop
        FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle(new 
Rule[]{rule});
        style = styleFactory.createStyle();
        style.featureTypeStyles().add(fts);
        
    } //end featureTypeStyle ftsList loop
    return style;
}


    
public static Style addRule(Style style, String labelField){
    ArrayList ftsList = (ArrayList)style.featureTypeStyles();
    ListIterator i = ftsList.listIterator();
    while(i.hasNext()){
        FeatureTypeStyle ftStyle = (FeatureTypeStyle)i.next();
        
        Rule rule = styleFactory.createRule();
        ArrayList rList = (ArrayList)ftStyle.rules();
        
        ListIterator j = rList.listIterator();
        while(j.hasNext()){
            Rule ruleo = (Rule)j.next();
            ArrayList symList = (ArrayList)ruleo.symbolizers();
            ListIterator s = symList.listIterator();
            while(s.hasNext()){
                Symbolizer ts=(Symbolizer)s.next();
                rule.symbolizers().add(ts);
            }
        }
        
        
        
        Fill labelFill = 
styleFactory.createFill(filterFactory.literal(Color.PINK));
        
        AnchorPoint anchor = 
styleFactory.createAnchorPoint(filterFactory.literal(-0.5), 
filterFactory.literal(0));
        Displacement disp = 
styleFactory.createDisplacement(filterFactory.literal(0), 
filterFactory.literal(-10));
        LabelPlacement placement = styleFactory.createPointPlacement(anchor, 
disp, filterFactory.literal(0));
        
        Font font = styleFactory.createFont(filterFactory.literal("Arial"), 
filterFactory.literal("italic"), filterFactory.literal(8), 
filterFactory.literal(8));
        
        
        TextSymbolizer textSym = styleFactory.createTextSymbolizer(
                labelFill, new Font[]{font}, null, 
filterFactory.property(labelField),
        placement, null);
        textSym.setName(labelField);
        
        
        rule.symbolizers().add(textSym);
        
        FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle(new 
Rule[]{rule});
        style = styleFactory.createStyle();
        style.featureTypeStyles().add(fts);
        
    } //end featureTypeStyle ftsList loop
    return style;
}



------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Geotools-gt2-users mailing list
Geotools-gt2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to