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