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.swt.SWT;
028    import org.eclipse.swt.layout.FillLayout;
029    import org.eclipse.swt.widgets.Composite;
030    import org.eclipse.swt.widgets.Control;
031    import org.eclipse.swt.widgets.TabFolder;
032    import org.eclipse.swt.widgets.TabItem;
033    import org.eclipse.swt.widgets.Text;
034    import org.eclipse.ui.dialogs.PropertyPage;
035    import org.granite.builder.GraniteBuilderContext;
036    import org.granite.builder.GraniteRebuildJob;
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.SWTUtil;
041    import org.granite.builder.util.StringUtil;
042    
043    /**
044     * @author Franck WOLFF
045     */
046    public class GranitePropertyPage extends PropertyPage {
047            
048            private GraniteBuilderContext context = null;
049            
050            private SourcesPanel sourcesPanel = null;
051            private ProjectsPanel projectsPanel = null;
052            private ClasspathsPanel classpathsPanel = null;
053            private TemplatesPanel templatesPanel = null;
054            private OptionsPanel optionsPanel = null;
055    
056        public GranitePropertyPage() {
057            super();
058        }
059    
060        @Override
061        protected Control createContents(Composite parent) {
062            Composite composite = new Composite(parent, SWT.NONE);
063            composite.setLayout(new FillLayout());
064    
065            try {
066                    this.context = new GraniteBuilderContext((IProject)getElement());
067                    
068                    TabFolder tabFolder = new TabFolder(composite, SWT.FILL);
069                    
070                    addTabItem1(tabFolder);
071                    addTabItem2(tabFolder);
072                    addTabItem3(tabFolder);
073                    addTabItem4(tabFolder);
074                    addTabItem5(tabFolder);
075                    
076                    noDefaultAndApplyButton();
077                    
078                    return composite;
079            } catch (Exception e) {
080                Text text = new Text(composite, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY);
081                text.setText(StringUtil.tokenize("Granite properties load failed: " + e.toString(), 80, Text.DELIMITER));
082                return composite;
083            }
084        }
085        
086        private void addTabItem1(TabFolder tabFolder) throws CoreException {
087            TabItem item = new TabItem(tabFolder, SWT.NONE);
088            item.setText("Java Sources");
089            item.setImage(SWTUtil.getImage(item.getDisplay(), SWTUtil.IMG_PKG_FOLDER));
090            sourcesPanel = new SourcesPanel(tabFolder, context);
091            item.setControl(sourcesPanel);
092        }
093        
094        private void addTabItem2(TabFolder tabFolder) throws CoreException {
095            TabItem item = new TabItem(tabFolder, SWT.NONE);
096            item.setText("Projects");
097            item.setImage(SWTUtil.getImage(item.getDisplay(), SWTUtil.IMG_PROJECTS));
098            projectsPanel = new ProjectsPanel(tabFolder, context);
099            item.setControl(projectsPanel);
100        }
101        
102        private void addTabItem3(TabFolder tabFolder) throws CoreException {
103            TabItem item = new TabItem(tabFolder, SWT.NONE);
104            item.setText("Libraries");
105            item.setImage(SWTUtil.getImage(item.getDisplay(), SWTUtil.IMG_LIBRARY));
106            classpathsPanel = new ClasspathsPanel(tabFolder, context);
107            item.setControl(classpathsPanel);
108        }
109        
110        private void addTabItem4(TabFolder tabFolder) throws CoreException {
111            TabItem item = new TabItem(tabFolder, SWT.NONE);
112            item.setText("Templates");
113            item.setImage(SWTUtil.getImage(item.getDisplay(), SWTUtil.IMG_TEMPLATE));
114            templatesPanel = new TemplatesPanel(tabFolder, context);
115            item.setControl(templatesPanel);
116        }
117        
118        private void addTabItem5(TabFolder tabFolder) throws CoreException {
119            TabItem item = new TabItem(tabFolder, SWT.NONE);
120            item.setText("Options");
121            item.setImage(SWTUtil.getImage(item.getDisplay(), SWTUtil.IMG_SETTINGS));
122            optionsPanel = new OptionsPanel(tabFolder, context);
123            item.setControl(optionsPanel);
124        }
125    
126            @Override
127        public boolean performOk() {
128            GraniteProperties properties = GraniteProperties.getDefaultProperties();
129            
130            properties.getGas3().getSources().addAll(sourcesPanel.getSources());
131    
132            properties.getGas3().getProjects().addAll(projectsPanel.getProjects());
133            
134            properties.getGas3().getClasspaths().addAll(classpathsPanel.getClasspaths());
135            
136            properties.getGas3().getTemplates().clear();
137            properties.getGas3().getTemplates().addAll(templatesPanel.getTemplates());
138            
139            properties.getGas3().setUid(optionsPanel.getUid());
140            properties.getGas3().setAs3TypeFactory(optionsPanel.getAs3TypeFactory());
141            properties.getGas3().setEntityFactory(optionsPanel.getEntityFactory());
142            properties.getGas3().setRemoteDestinationFactory(optionsPanel.getRemoteDestinationFactory());
143            properties.getGas3().getTransformers().clear();
144            properties.getGas3().getTransformers().add(new Gas3Transformer(optionsPanel.getTransformer()));
145            properties.getGas3().getTranslators().clear();
146            properties.getGas3().getTranslators().addAll(optionsPanel.getTranslators());
147            properties.getGas3().setDebugEnabled(optionsPanel.isDebugEnabled());
148            properties.getGas3().setFlexConfig(optionsPanel.isFlexConfig());
149            properties.getGas3().setExternalizeLong(optionsPanel.isExternalizeLong());
150            properties.getGas3().setExternalizeBigInteger(optionsPanel.isExternalizeBigInteger());
151            properties.getGas3().setExternalizeBigDecimal(optionsPanel.isExternalizeBigDecimal());
152            
153            try {
154                GranitePropertiesLoader.save((IProject)getElement(), properties);
155            } catch (IOException e) {
156                setErrorMessage("Could not save Granite properties: " + e.toString());
157                return false;
158            }
159            
160            GraniteRebuildJob rebuild = new GraniteRebuildJob((IProject)getElement(), true);
161            rebuild.schedule();
162            
163            return true;
164        }
165    }