001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2007-2010 ADEQUATE SYSTEMS SARL
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.builder.ui;
022    
023    import java.io.IOException;
024    
025    import org.eclipse.core.resources.IProject;
026    import org.eclipse.core.runtime.CoreException;
027    import org.eclipse.jface.resource.ImageDescriptor;
028    import org.eclipse.jface.wizard.Wizard;
029    import org.eclipse.jface.wizard.WizardDialog;
030    import org.eclipse.jface.wizard.WizardPage;
031    import org.eclipse.swt.widgets.Composite;
032    import org.eclipse.swt.widgets.Display;
033    import org.eclipse.swt.widgets.Shell;
034    import org.granite.builder.GraniteBuilderContext;
035    import org.granite.builder.GraniteNature;
036    import org.granite.builder.ToggleNatureAction;
037    import org.granite.builder.properties.Gas3Transformer;
038    import org.granite.builder.properties.GraniteProperties;
039    import org.granite.builder.properties.GranitePropertiesLoader;
040    import org.granite.builder.util.ProjectUtil;
041    import org.granite.builder.util.SWTUtil;
042    
043    /**
044     * @author Franck WOLFF
045     */
046    public class AddNatureWizard extends Wizard {
047    
048            private static final String SOURCES_PANEL = "sourcesPanel";
049            private static final String PROJECTS_PANEL = "projectsPanel";
050            private static final String CLASSPATHS_PANEL = "classpathsPanel";
051            private static final String TEMPLATES_PANEL = "templatesPanel";
052            private static final String OPTIONS_PANEL = "optionsPanel";
053            
054            private final GraniteBuilderContext context;
055            
056            private WizardDialog dialog = null;
057            
058            public AddNatureWizard(IProject project) throws CoreException {
059                    this.context = new GraniteBuilderContext(project);
060            }
061    
062            @Override
063            public void addPages() {
064                    setWindowTitle("Add Granite Nature Wizard");
065                    setDefaultPageImageDescriptor(ImageDescriptor.createFromImage(
066                            SWTUtil.getImage(getShell().getDisplay(), "icons/gdswiz.gif"))
067                    );
068                    
069                    addPage(new WizardPage(SOURCES_PANEL) {
070                            public void createControl(Composite parent) {
071                                    try {
072                                            setControl(new SourcesPanel(parent, context));
073                                    } catch (Exception e) {
074                                            throw new RuntimeException(e);
075                                    }
076                                    setTitle("Source Folder Configuration");
077                                    setDescription("Step 1: select Java source folders, included/excluded patterns and output folders...");
078                            }
079                    });
080                    
081                    addPage(new WizardPage(PROJECTS_PANEL) {
082                            public void createControl(Composite parent) {
083                                    try {
084                                            setControl(new ProjectsPanel(parent, context));
085                                    } catch (Exception e) {
086                                            throw new RuntimeException(e);
087                                    }
088                                    setTitle("Dependent Projects Configuration");
089                                    setDescription("Step 2: select dependent granite projects...");
090                            }
091                    });
092                    
093                    addPage(new WizardPage(CLASSPATHS_PANEL) {
094                            public void createControl(Composite parent) {
095                                    try {
096                                            setControl(new ClasspathsPanel(parent, context));
097                                    } catch (Exception e) {
098                                            throw new RuntimeException(e);
099                                    }
100                                    setTitle("Classpath Configuration");
101                                    setDescription("Step 3: select jars or class folders used as classpath...");
102                            }
103                    });
104                    
105                    addPage(new WizardPage(TEMPLATES_PANEL) {
106                            public void createControl(Composite parent) {
107                                    try {
108                                            setControl(new TemplatesPanel(parent, context));
109                                    } catch (Exception e) {
110                                            throw new RuntimeException(e);
111                                    }
112                                    setTitle("Templates Configuration");
113                                    setDescription("Step 4: select templates that will be used for generation...");
114                            }
115                    });
116                    
117                    addPage(new WizardPage(OPTIONS_PANEL) {
118                            public void createControl(Composite parent) {
119                                    try {
120                                            setControl(new OptionsPanel(parent, context));
121                                    } catch (Exception e) {
122                                            throw new RuntimeException(e);
123                                    }
124                                    setTitle("Miscanellous Options");
125                                    setDescription("Step 5: modify various options that control file generation...");
126                            }
127                    });
128            }
129    
130            @Override
131            public boolean performFinish() {
132            GraniteProperties properties = GraniteProperties.getDefaultProperties();
133            
134            SourcesPanel sourcesPanel = (SourcesPanel)getPage(SOURCES_PANEL).getControl();
135            properties.getGas3().getSources().addAll(sourcesPanel.getSources());
136    
137            ProjectsPanel projectsPanel = (ProjectsPanel)getPage(PROJECTS_PANEL).getControl();
138            properties.getGas3().getProjects().addAll(projectsPanel.getProjects());
139            
140            ClasspathsPanel classpathsPanel = (ClasspathsPanel)getPage(CLASSPATHS_PANEL).getControl();
141            properties.getGas3().getClasspaths().addAll(classpathsPanel.getClasspaths());
142            
143            TemplatesPanel templatesPanel = (TemplatesPanel)getPage(TEMPLATES_PANEL).getControl();
144            properties.getGas3().getTemplates().clear();
145            properties.getGas3().getTemplates().addAll(templatesPanel.getTemplates());
146            
147            OptionsPanel optionsPanel = (OptionsPanel)getPage(OPTIONS_PANEL).getControl();
148            properties.getGas3().setUid(optionsPanel.getUid());
149            properties.getGas3().setAs3TypeFactory(optionsPanel.getAs3TypeFactory());
150            properties.getGas3().setEntityFactory(optionsPanel.getEntityFactory());
151            properties.getGas3().setRemoteDestinationFactory(optionsPanel.getRemoteDestinationFactory());
152            properties.getGas3().getTransformers().clear();
153            properties.getGas3().getTransformers().add(new Gas3Transformer(optionsPanel.getTransformer()));
154            properties.getGas3().getTranslators().clear();
155            properties.getGas3().getTranslators().addAll(optionsPanel.getTranslators());
156            properties.getGas3().setDebugEnabled(optionsPanel.isDebugEnabled());
157            properties.getGas3().setFlexConfig(optionsPanel.isFlexConfig());
158            properties.getGas3().setExternalizeLong(optionsPanel.isExternalizeLong());
159            properties.getGas3().setExternalizeBigInteger(optionsPanel.isExternalizeBigInteger());
160            properties.getGas3().setExternalizeBigDecimal(optionsPanel.isExternalizeBigDecimal());
161            
162            try {
163                GranitePropertiesLoader.save(context.getJavaProject().getProject(), properties);
164            } catch (IOException e) {
165                dialog.setErrorMessage("Could not save Granite properties: " + e.toString());
166                return false;
167            }
168                    
169                    return true;
170            }
171            
172            @Override
173            public boolean performCancel() {
174                    try {
175                            IProject project = context.getJavaProject().getProject();
176                            if (project.getDescription().hasNature(GraniteNature.NATURE_ID))
177                                    ToggleNatureAction.toggleNature(project);
178                    } catch (CoreException e) {
179                    }
180                    return true;
181            }
182    
183            public static void run(final IProject project) throws CoreException {
184                    final Display display = (Display.getCurrent() != null ? Display.getCurrent() : Display.getDefault());
185                    try {
186                            display.syncExec(new Runnable() {
187                                    public void run() {
188                                            try {
189                                                    Shell shell = new Shell(display);
190                                            AddNatureWizard wizard = new AddNatureWizard(project);
191                                            final WizardDialog dialog = new WizardDialog(shell, wizard);
192                                            wizard.dialog = dialog;
193                                            dialog.setPageSize(640, 589);
194                                            dialog.setHelpAvailable(false);
195                                            dialog.create();
196                                                    dialog.open();
197                                            } catch (Exception e) {
198                                                    throw new RuntimeException(e);
199                                            }
200                                    }
201                            });
202                    } catch (Exception e) {
203                            if (e.getCause() instanceof CoreException)
204                                    throw (CoreException)e.getCause();
205                            throw new CoreException(ProjectUtil.createErrorStatus("Could not run wizard: " + e.toString(), null));
206                    }
207            }
208    }