001    /*
002      Copyright (C) 2002-2003 Renaud Pawlak <renaud@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 java.io.IOException;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.Vector;
025    import org.objectweb.jac.aspects.gui.swing.SHEditorConfig;
026    import org.objectweb.jac.aspects.export.ExportAC;
027    import org.objectweb.jac.aspects.export.Importer;
028    import org.objectweb.jac.aspects.timestamp.Timestamps;
029    import org.objectweb.jac.core.ACManager;
030    import org.objectweb.jac.core.Wrappee;
031    import org.objectweb.jac.core.rtti.FieldItem;
032    import org.objectweb.jac.util.File;
033    import org.xml.sax.SAXException;
034    
035    public class Projects {
036        public static transient Projects root;
037        public static transient TypeRepository types;
038        public static transient Plurals plurals;
039        public static transient Timestamps stamps;
040        public static transient Preferences prefs;
041    
042        public static Boolean notPrimitiveType(Wrappee substance,
043                                               FieldItem field,
044                                               Object value,
045                                               Object[] values)
046        {
047            Iterator it = Projects.types.getPrimitiveTypes().iterator();
048            while(it.hasNext()) {
049                Type type = (Type)it.next();
050                if (((String)value).equals(type.getName())) {
051                    return Boolean.FALSE;
052                }
053            }
054            return Boolean.TRUE;
055        }
056    
057        /**
058         * Handle upgrading from class with no package to a class with
059         * package, for compatibility with 0.10 versions
060         * @param name name the class to add
061         * @param pkg package name of the class to add
062         */
063        private static Type initExternalClass(String name, String pkg) {
064            Type type = types.resolveType(name, "");
065            if (type!=null) {
066                type.setPackagePath(pkg);
067            } else {
068                type = types.resolveType(name,pkg);
069                if (type==null) {
070                    type = new Type(name,pkg);
071                    types.addExternalClass(type);
072                }
073            }
074            return type;
075        }
076    
077        private static ExtendedType initExtendedType(String name, Type realType) {
078            Type type = types.resolveType(name, "");
079            if (type!=null) {
080                if (type instanceof ExtendedType)
081                    ((ExtendedType)type).setRealType(realType);
082                else {
083                    System.err.println("Warning: there's alreayd a type named "+
084                                       name+", but it's not an extended type");
085                    return null;
086                }
087            } else {
088                type = new ExtendedType(name,realType);
089                types.addExtendedType((ExtendedType)type);
090            }
091            return (ExtendedType)type;
092        }
093    
094        private static Type initPrimitiveType(String name) {
095            Type type = types.resolveType(name, "");
096            if (type==null) {
097                type = new Type(name,"");
098                types.addPrimitiveType(type);
099            }
100            return type;
101        }
102    
103        public static void main(String[] args) {
104            root = new Projects();
105            types = new TypeRepository();
106            plurals = new Plurals();
107            stamps = new Timestamps();
108            prefs = new Preferences();
109            prefs.setEditorPrefs(new SHEditorConfig());
110    
111            // create the primitive types if needed
112    
113            initPrimitiveType("void");
114            initPrimitiveType("boolean");
115            initPrimitiveType("int");
116            initPrimitiveType("long");
117            initPrimitiveType("float");
118            initPrimitiveType("double");
119    
120            // create some useful external classes
121            initExternalClass("Object","java.lang");
122          
123            Type type = initExternalClass("String","java.lang");
124            initExtendedType("text",type);
125            initExtendedType("email",type);
126            initExtendedType("password",type);
127            initExtendedType("javaCode",type);
128            initExtendedType("accCode",type);
129          
130            type = initExternalClass("float","");
131            initExtendedType("percentage",type);
132    
133            type = initExternalClass("File","java.io");
134            initExtendedType("directory",type);
135          
136            type = initExternalClass("URL","java.net");
137            initExtendedType("directoryURL",type);
138            initExtendedType("imageURL",type);
139          
140            type = initExternalClass("Date","java.util");
141            initExtendedType("dateHour",type);
142          
143            type = initExternalClass("Vector","java.util");
144            type = initExternalClass("List","java.util");
145            type = initExternalClass("Map","java.util");
146            type = initExternalClass("HashMap","java.util");
147            type = initExternalClass("Set","java.util");
148            type = initExternalClass("HashSet","java.util");
149            type = initExternalClass("Collection","java.util");
150            type = initExternalClass("Reader","java.io");
151            type = initExternalClass("Writer","java.io");
152            type = initExternalClass("InputStream","java.io");
153            type = initExternalClass("OutputStream","java.io");
154        }
155    
156        Vector projects = new Vector();
157       
158        /**
159         * Get the value of projects.
160         * @return value of projects.
161         */
162        public List getProjects() {
163            return projects;
164        }
165       
166        public void addProject(Project p) {
167            projects.add(p);
168        }
169       
170        public void removeProject(Project p) {
171            projects.remove(p);
172        }
173    
174        public String toString() {
175            return "projects";
176        }
177    
178        Application currentApplication = null;
179        public void setCurrentApplication(Application application) {
180            currentApplication = application;
181        }
182        public Application getCurrentApplication() {
183            return currentApplication;
184        }
185    
186        /**
187         * Starts the current application
188         */
189        public void startCurrentApplication() throws IOException {
190            if (currentApplication!=null) {
191                currentApplication.start();
192            }
193        }
194    
195        /**
196         * Stops the current application
197         */
198        public void stopCurrentApplication() {
199            if (currentApplication!=null) {
200                currentApplication.stop();
201            }
202        }
203    
204        public boolean isNotStarted() {
205            return currentApplication!=null && currentApplication.isNotStarted();
206        }
207    
208        public boolean isStarted() {
209            return currentApplication!=null && currentApplication.isStarted();      
210        }
211    
212        /**
213         * Exports projects to an XML file
214         * @param f the file to export to
215         */
216        public static void export(File f) throws IOException, Exception {
217            ExportAC exportAC = (ExportAC)ACManager.getACM().getACFromFullName("ide.export");
218            if (exportAC==null) {
219                throw new Exception("No export aspect found");
220            } else {
221                exportAC.export(f);
222            }
223        }
224    
225        public static void importObjects(File f) throws SAXException, IOException {
226            Importer importer = new Importer();
227            importer.importObjects(f);
228        }
229    }