001    /*
002      Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.ide;
020    
021    import org.objectweb.jac.core.rtti.NamingConventions;
022    import java.io.IOException;
023    import java.io.Writer;
024    import java.util.Iterator;
025    
026    /**
027     * gui.acc generation plugin
028     */
029    
030    public class GuiPlugin extends AbstractPlugin {   
031    
032        public void genConfig(Writer output, Project project) 
033            throws IOException
034        {
035            // Generate enumerated types
036            Iterator enums = Projects.types.getEnumeratedTypes().iterator();
037            while (enums.hasNext()) {
038                EnumeratedType enum = (EnumeratedType)enums.next();
039                output.write("defineEnum "+enum.getName()+" { ");
040                Iterator names = enum.getNames().iterator();
041                while (names.hasNext()) {
042                    String name = (String)names.next();
043                    output.write("\""+name+"\"");
044                    if (names.hasNext())
045                        output.write(", ");
046                }
047                output.write("} "+enum.getStartValue()+" "+enum.getStep()+";\n");
048            }
049            super.genConfig(output,project);
050        }
051    
052        public void genPackageConfig(Writer output, 
053                                     Project project, Package pkg) 
054            throws IOException
055        {
056            // Generate askForParameters
057            output.write("askForParameters "+pkg.getPPath()+".*;\n\n");
058            super.genPackageConfig(output,project,pkg);
059        }
060    
061        public void genClassConfig(Writer output, 
062                                   Project project, Package pkg, Class cl)
063            throws IOException
064        {
065            // Generate default attributes order
066            state.openClass(cl);
067            if (!cl.getName().equals(cl.getGenerationName()))
068                state.write("setLabel \""+NamingConventions.textForName(cl.getName())+"\";\n");
069            state.write("setAttributesOrder {");
070            int count = 0;
071            Iterator fields = cl.getAllFields().iterator();
072            while (fields.hasNext()) {
073                Field field = (Field)fields.next();
074                if(field.isStatic()) continue;
075                if (count>0) {
076                    output.write(",");
077                }
078                output.write(field.getGenerationName());
079                count++;
080            }
081    
082            Iterator roles = cl.getAllNavigableRoles().iterator();
083            while (roles.hasNext()) {
084                RelationRole role = (RelationRole)roles.next();
085                if (count>0) {
086                    output.write(",");
087                }
088                output.write(role.getGenerationName());
089                count++;
090            }
091            output.write("};\n");
092    
093            super.genClassConfig(output,project,pkg,cl);
094          
095        }
096    
097        public void genRoleConfig(Writer output, Project project, 
098                                  Package pkg, Class cl, RelationRole role) 
099            throws IOException 
100        {
101            if (role.isNavigable()) {
102                Class targetClass = (Class)role.getEnd();
103                if (!role.getName().equals(role.getGenerationName())) {
104                    state.openRole(cl,role);
105                    state.write("setLabel \""+
106                                NamingConventions.textForName(role.getRoleName())+"\";\n");
107                }
108                // Generate autoCreate for aggregations
109                if (role.isAggregation()) {
110                    state.openRole(cl,role);
111                    state.write("setAutoCreate;\n");
112                }
113            }
114        }
115    
116        public void genFieldConfig(Writer output, Project project, 
117                                   Package pkg, Class cl, Field field) 
118            throws IOException
119        {
120            if (!field.getName().equals(field.getGenerationName())) {
121                state.openField(cl,field);
122                state.write("setLabel \""+
123                            NamingConventions.textForName(field.getName())+"\";\n");
124            }
125            if (field.getType() instanceof EnumeratedType) {
126                state.openField(cl,field);
127                state.write("setFieldEnum "+
128                            ((EnumeratedType)field.getType()).getName()+";\n");         
129            }
130            state.closeMember();
131        }
132    
133        public void genMethodConfig(Writer output, Project project, 
134                                    Package pkg, Class cl, Method method) 
135            throws IOException 
136        {
137            if (method.getVisibility()!=Visibility.PUBLIC)
138                return;
139            if (!method.getName().equals(method.getGenerationName())) {
140                state.openMethod(cl,method);
141                state.write("setLabel \""+
142                            NamingConventions.textForName(method.getName())+"\";\n");
143            }
144            // Generate method parameters names
145            if (method.getParameters().size()>0) {
146                state.openMethod(cl,method);
147                state.write("setParameterNames {");
148                Iterator it = method.getParameters().iterator();
149                while (it.hasNext()) {
150                    Parameter param = (Parameter)it.next();
151                    output.write("\""+param.getName()+"\"");
152                    if (it.hasNext()) {
153                        output.write(",");
154                    }
155                }
156                output.write("};\n");
157            }
158            state.closeMember();
159        }
160    
161    }