001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
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                            @Override
071                            public void createControl(Composite parent) {
072                                    try {
073                                            setControl(new SourcesPanel(parent, context));
074                                    } catch (Exception e) {
075                                            throw new RuntimeException(e);
076                                    }
077                                    setTitle("Source Folder Configuration");
078                                    setDescription("Step 1: select Java source folders, included/excluded patterns and output folders...");
079                            }
080                    });
081                    
082                    addPage(new WizardPage(PROJECTS_PANEL) {
083                            @Override
084                            public void createControl(Composite parent) {
085                                    try {
086                                            setControl(new ProjectsPanel(parent, context));
087                                    } catch (Exception e) {
088                                            throw new RuntimeException(e);
089                                    }
090                                    setTitle("Dependent Projects Configuration");
091                                    setDescription("Step 2: select dependent granite projects...");
092                            }
093                    });
094                    
095                    addPage(new WizardPage(CLASSPATHS_PANEL) {
096                            @Override
097                            public void createControl(Composite parent) {
098                                    try {
099                                            setControl(new ClasspathsPanel(parent, context));
100                                    } catch (Exception e) {
101                                            throw new RuntimeException(e);
102                                    }
103                                    setTitle("Classpath Configuration");
104                                    setDescription("Step 3: select jars or class folders used as classpath...");
105                            }
106                    });
107                    
108                    addPage(new WizardPage(TEMPLATES_PANEL) {
109                            @Override
110                            public void createControl(Composite parent) {
111                                    try {
112                                            setControl(new TemplatesPanel(parent, context));
113                                    } catch (Exception e) {
114                                            throw new RuntimeException(e);
115                                    }
116                                    setTitle("Templates Configuration");
117                                    setDescription("Step 4: select templates that will be used for generation...");
118                            }
119                    });
120                    
121                    addPage(new WizardPage(OPTIONS_PANEL) {
122                            @Override
123                            public void createControl(Composite parent) {
124                                    try {
125                                            setControl(new OptionsPanel(parent, context));
126                                    } catch (Exception e) {
127                                            throw new RuntimeException(e);
128                                    }
129                                    setTitle("Miscanellous Options");
130                                    setDescription("Step 5: modify various options that control file generation...");
131                            }
132                    });
133            }
134    
135            @Override
136            public boolean performFinish() {
137            GraniteProperties properties = GraniteProperties.getDefaultProperties();
138            
139            SourcesPanel sourcesPanel = (SourcesPanel)getPage(SOURCES_PANEL).getControl();
140            properties.getGas3().getSources().addAll(sourcesPanel.getSources());
141    
142            ProjectsPanel projectsPanel = (ProjectsPanel)getPage(PROJECTS_PANEL).getControl();
143            properties.getGas3().getProjects().addAll(projectsPanel.getProjects());
144            
145            ClasspathsPanel classpathsPanel = (ClasspathsPanel)getPage(CLASSPATHS_PANEL).getControl();
146            properties.getGas3().getClasspaths().addAll(classpathsPanel.getClasspaths());
147            
148            TemplatesPanel templatesPanel = (TemplatesPanel)getPage(TEMPLATES_PANEL).getControl();
149            properties.getGas3().getTemplates().clear();
150            properties.getGas3().getTemplates().addAll(templatesPanel.getTemplates());
151            
152            OptionsPanel optionsPanel = (OptionsPanel)getPage(OPTIONS_PANEL).getControl();
153            properties.getGas3().setUid(optionsPanel.getUid());
154            properties.getGas3().setAs3TypeFactory(optionsPanel.getAs3TypeFactory());
155            properties.getGas3().setEntityFactory(optionsPanel.getEntityFactory());
156            properties.getGas3().setRemoteDestinationFactory(optionsPanel.getRemoteDestinationFactory());
157            properties.getGas3().getTransformers().clear();
158            properties.getGas3().getTransformers().add(new Gas3Transformer(optionsPanel.getTransformer()));
159            properties.getGas3().getTranslators().clear();
160            properties.getGas3().getTranslators().addAll(optionsPanel.getTranslators());
161            properties.getGas3().setDebugEnabled(optionsPanel.isDebugEnabled());
162            properties.getGas3().setFlexConfig(optionsPanel.isFlexConfig());
163            properties.getGas3().setExternalizeLong(optionsPanel.isExternalizeLong());
164            properties.getGas3().setExternalizeBigInteger(optionsPanel.isExternalizeBigInteger());
165            properties.getGas3().setExternalizeBigDecimal(optionsPanel.isExternalizeBigDecimal());
166            
167            try {
168                GranitePropertiesLoader.save(context.getJavaProject().getProject(), properties);
169            } catch (IOException e) {
170                dialog.setErrorMessage("Could not save Granite properties: " + e.toString());
171                return false;
172            }
173                    
174                    return true;
175            }
176            
177            @Override
178            public boolean performCancel() {
179                    try {
180                            IProject project = context.getJavaProject().getProject();
181                            if (project.getDescription().hasNature(GraniteNature.NATURE_ID))
182                                    ToggleNatureAction.toggleNature(project);
183                    } catch (CoreException e) {
184                    }
185                    return true;
186            }
187    
188            public static void run(final IProject project) throws CoreException {
189                    final Display display = (Display.getCurrent() != null ? Display.getCurrent() : Display.getDefault());
190                    try {
191                            display.syncExec(new Runnable() {
192                                    @Override
193                                    public void run() {
194                                            try {
195                                                    Shell shell = new Shell(display);
196                                            AddNatureWizard wizard = new AddNatureWizard(project);
197                                            final WizardDialog dialog = new WizardDialog(shell, wizard);
198                                            wizard.dialog = dialog;
199                                            dialog.setPageSize(640, 589);
200                                            dialog.setHelpAvailable(false);
201                                            dialog.create();
202                                                    dialog.open();
203                                            } catch (Exception e) {
204                                                    throw new RuntimeException(e);
205                                            }
206                                    }
207                            });
208                    } catch (Exception e) {
209                            if (e.getCause() instanceof CoreException)
210                                    throw (CoreException)e.getCause();
211                            throw new CoreException(ProjectUtil.createErrorStatus("Could not run wizard: " + e.toString(), null));
212                    }
213            }
214    }