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
021package org.granite.builder.ui;
022
023import java.beans.PropertyChangeEvent;
024import java.beans.PropertyChangeListener;
025import java.util.HashSet;
026import java.util.Set;
027
028import org.eclipse.core.runtime.CoreException;
029import org.eclipse.swt.SWT;
030import org.eclipse.swt.events.SelectionAdapter;
031import org.eclipse.swt.events.SelectionEvent;
032import org.eclipse.swt.graphics.Rectangle;
033import org.eclipse.swt.layout.FillLayout;
034import org.eclipse.swt.layout.GridData;
035import org.eclipse.swt.layout.GridLayout;
036import org.eclipse.swt.widgets.Button;
037import org.eclipse.swt.widgets.Composite;
038import org.eclipse.swt.widgets.Label;
039import org.eclipse.swt.widgets.List;
040import org.eclipse.swt.widgets.Text;
041import org.granite.builder.GraniteBuilderContext;
042import org.granite.builder.properties.Gas3;
043import org.granite.builder.properties.Gas3Translator;
044import org.granite.builder.properties.GraniteProperties;
045import org.granite.builder.util.SWTUtil;
046import org.granite.builder.util.StringUtil;
047
048/**
049 * @author Franck WOLFF
050 */
051public class OptionsPanel extends Composite implements PropertyChangeListener {
052
053        private static final String TRANSLATOR_SEPARATOR = " -> ";
054        
055        private final GraniteProperties properties;
056        
057        private Text uid = null;
058        private Text as3TypeFactory = null;
059        private Text entityFactory = null;
060        private Text remoteDestinationFactory = null;
061        private Text transformer = null;
062        private List translators = null;
063        private Button debugEnabled = null;
064        private Button flexConfig = null;
065        private Button externalizeLong = null;
066        private Button externalizeBigInteger = null;
067        private Button externalizeBigDecimal = null;
068        
069        private boolean initialized = false;
070        
071        public OptionsPanel(Composite parent, GraniteBuilderContext context) throws CoreException {
072        super(parent, SWT.NONE);
073        this.properties = context.getProperties();
074        initializeComponents();
075        }
076        
077        public String getUid() {
078                if (!initialized)
079                        return properties.getGas3().getUid();
080                return uid.getText();
081        }
082
083        public String getAs3TypeFactory() {
084                if (!initialized)
085                        return properties.getGas3().getAs3TypeFactory();
086                return as3TypeFactory.getText();
087        }
088
089        public String getEntityFactory() {
090                if (!initialized)
091                        return properties.getGas3().getEntityFactory();
092                return entityFactory.getText();
093        }
094
095        public String getRemoteDestinationFactory() {
096                if (!initialized)
097                        return properties.getGas3().getRemoteDestinationFactory();
098                return remoteDestinationFactory.getText();
099        }
100        
101        public String getTransformer() {
102                if (!initialized) {
103                        if (properties.getGas3().getTransformers().isEmpty())
104                                return "";
105                        return properties.getGas3().getTransformers().get(0).getType();
106                }
107                return transformer.getText();
108        }
109        
110        public Set<Gas3Translator> getTranslators() {
111                if (!initialized)
112                        return properties.getGas3().getTranslators();
113                
114                Set<Gas3Translator> translatorsSet = new HashSet<Gas3Translator>();
115                for (String translator : translators.getItems()) {
116                        String[] values = StringUtil.split(translator, TRANSLATOR_SEPARATOR);
117                        if (values.length == 2)
118                                translatorsSet.add(new Gas3Translator(values[0], values[1]));
119                }
120                return translatorsSet;
121        }
122        
123        public boolean isDebugEnabled() {
124                return debugEnabled.getSelection();
125        }
126        
127        public boolean isFlexConfig() {
128                return flexConfig.getSelection();
129        }
130
131        public boolean isExternalizeLong() {
132                return externalizeLong.getSelection();
133        }
134
135        public boolean isExternalizeBigInteger() {
136                return externalizeBigInteger.getSelection();
137        }
138
139        public boolean isExternalizeBigDecimal() {
140                return externalizeBigDecimal.getSelection();
141        }
142
143        @Override
144        public Rectangle getClientArea() {
145                initializeContent();
146                return super.getClientArea();
147        }
148        
149        private void initializeContent() {
150                if (!initialized) {
151                if (properties.getGas3().getUid() != null)
152                        uid.setText(properties.getGas3().getUid());
153
154                if (properties.getGas3().getAs3TypeFactory() != null)
155                        as3TypeFactory.setText(properties.getGas3().getAs3TypeFactory());
156
157                if (properties.getGas3().getEntityFactory() != null)
158                        entityFactory.setText(properties.getGas3().getEntityFactory());
159
160                if (properties.getGas3().getRemoteDestinationFactory() != null)
161                        remoteDestinationFactory.setText(properties.getGas3().getRemoteDestinationFactory());
162                
163                if (!properties.getGas3().getTransformers().isEmpty())
164                        transformer.setText(properties.getGas3().getTransformers().get(0).getType());
165                
166                for (Gas3Translator translator : properties.getGas3().getTranslators())
167                        translators.add(translator.getJava() + TRANSLATOR_SEPARATOR + translator.getAs3());
168                
169                debugEnabled.setSelection(properties.getGas3().isDebugEnabled());
170                flexConfig.setSelection(properties.getGas3().isFlexConfig());
171                externalizeLong.setSelection(properties.getGas3().isExternalizeLong());
172                externalizeBigInteger.setSelection(properties.getGas3().isExternalizeBigInteger());
173                externalizeBigDecimal.setSelection(properties.getGas3().isExternalizeBigDecimal());
174                
175                        initialized = true;
176                
177                this.properties.getGas3().removePropertyChangeListener(this);
178                this.properties.getGas3().addPropertyChangeListener(this);
179                }
180        }
181    
182        @Override
183        public void propertyChange(PropertyChangeEvent evt) {
184                initialized = false;
185                initializeContent();
186        }
187
188        private void initializeComponents() {
189        setLayout(new GridLayout());
190
191        Label label = new Label(this, SWT.NONE);
192        label.setText("UID property name (leave empty if you don't want this feature):");
193        
194        uid = new Text(this, SWT.BORDER);
195        uid.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
196        
197        label = new Label(this, SWT.NONE);
198        label.setText("As3TypeFactory class:");
199        
200        as3TypeFactory = new Text(this, SWT.BORDER);
201        as3TypeFactory.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
202
203        label = new Label(this, SWT.NONE);
204        label.setText("EntityFactory class:");
205        
206        entityFactory = new Text(this, SWT.BORDER);
207        entityFactory.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
208
209        label = new Label(this, SWT.NONE);
210        label.setText("RemoteDestinationFactory class:");
211        
212        remoteDestinationFactory = new Text(this, SWT.BORDER);
213        remoteDestinationFactory.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
214        
215        label = new Label(this, SWT.NONE);
216        label.setText("Transformer class:");
217        
218        transformer = new Text(this, SWT.BORDER);
219        transformer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
220        
221        label = new Label(this, SWT.NONE);
222        label.setText("Package translators:");
223        
224        Composite translatorsComposite = new Composite(this, SWT.BORDER);
225        translatorsComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
226        translatorsComposite.setLayout(new GridLayout(2, false));
227        
228        translators = new List(translatorsComposite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI);
229        translators.setLayoutData(new GridData(GridData.FILL_BOTH));
230        
231                Composite translatorsCompositeButtons = new Composite(translatorsComposite, SWT.NONE);
232                translatorsCompositeButtons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
233                translatorsCompositeButtons.setLayout(new FillLayout(SWT.VERTICAL));
234
235        SWTUtil.newButton(translatorsCompositeButtons, "Add Translator...", true, new SelectionAdapter() {
236                        @Override
237                        public void widgetSelected(SelectionEvent e) {
238                                String[] values = Dialogs.addPackageTranslator(getDisplay().getActiveShell(), "Add Translator");
239                                if (values != null && values.length == 2)
240                                        translators.add(StringUtil.join(values, TRANSLATOR_SEPARATOR));
241                        }
242                });
243
244        final Button editTranslatorButton = SWTUtil.newButton(translatorsCompositeButtons, "Edit Translator...", false, new SelectionAdapter() {
245                        @Override
246                        public void widgetSelected(SelectionEvent e) {
247                                int selectedIndex = translators.getSelectionIndex();
248                                String selected = translators.getItem(selectedIndex);
249                                String[] values = StringUtil.split(selected, TRANSLATOR_SEPARATOR);
250                                
251                                values = Dialogs.editPackageTranslator(
252                                        getDisplay().getActiveShell(),
253                                        "Edit Translator",
254                                        (values.length > 0 ? values[0] : null),
255                                        (values.length > 1 ? values[1] : null)
256                                );
257                                
258                                if (values != null && values.length == 2)
259                                        translators.setItem(selectedIndex, StringUtil.join(values, TRANSLATOR_SEPARATOR));
260                        }
261                });
262
263        final Button removeTranslatorButton = SWTUtil.newButton(translatorsCompositeButtons, "Remove Translators", false, new SelectionAdapter() {
264                        @Override
265                        public void widgetSelected(SelectionEvent e) {
266                                if (translators.getSelectionCount() > 0)
267                                        translators.remove(translators.getSelectionIndices());
268                        }
269                });
270        
271        translators.addSelectionListener(new SelectionAdapter() {
272                        @Override
273                        public void widgetSelected(SelectionEvent e) {
274                                editTranslatorButton.setEnabled(translators.getSelectionCount() == 1);
275                                removeTranslatorButton.setEnabled(translators.getSelectionCount() > 0);
276                        }
277                });
278        
279        debugEnabled = new Button(this, SWT.CHECK);
280        debugEnabled.setText("Show debug information in console");
281        
282        flexConfig = new Button(this, SWT.CHECK);
283        flexConfig.setText("Generate a Flex Builder configuration file");
284        
285        externalizeLong = new Button(this, SWT.CHECK);
286        externalizeLong.setText("Use org.granite.math.Long");
287        
288        externalizeBigInteger = new Button(this, SWT.CHECK);
289        externalizeBigInteger.setText("Use org.granite.math.BigInteger");
290        
291        externalizeBigDecimal = new Button(this, SWT.CHECK);
292        externalizeBigDecimal.setText("Use org.granite.math.BigDecimal");
293        
294        SWTUtil.newButton(this, "Reset to default values", true, new SelectionAdapter() {
295                        @Override
296                        public void widgetSelected(SelectionEvent e) {
297                                Gas3 defaultGas3 = GraniteProperties.getDefaultProperties().getGas3();
298                                uid.setText(defaultGas3.getUid());
299                                as3TypeFactory.setText(defaultGas3.getAs3TypeFactory());
300                                entityFactory.setText(defaultGas3.getEntityFactory());
301                                remoteDestinationFactory.setText(defaultGas3.getRemoteDestinationFactory());
302                                transformer.setText(defaultGas3.getTransformers().get(0).getType());
303                                debugEnabled.setSelection(false);
304                                flexConfig.setSelection(false);
305                                externalizeLong.setSelection(false);
306                                externalizeBigInteger.setSelection(false);
307                                externalizeBigDecimal.setSelection(false);
308                        }
309                });
310        }
311}