source: contrib/psync/src/main/java/br/com/prognus/psync/admin/PIMSyncSourceConfigPanel.java @ 1103

Revision 1103, 12.3 KB checked in by wmerlotto, 15 years ago (diff)

Ticket #554 - Melhorias na sincronizacao de eventos, contatos e adaptacao para Funambol 7.0

Line 
1/**
2 * This class implements the configuration panel for a PIMSyncSource.
3 * @author Diorgenes Felipe Grzesiuk <diorgenes@prognus.com.br>
4 * @copyright Copyright 2007-2008 Prognus
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Foobar; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19package br.com.prognus.psync.admin;
20
21import java.awt.Dimension;
22import java.awt.Rectangle;
23import java.awt.event.ActionEvent;
24import java.awt.event.ActionListener;
25import java.awt.event.ItemEvent;
26import java.awt.event.ItemListener;
27import java.io.Serializable;
28import java.util.ArrayList;
29import java.util.List;
30
31import javax.swing.JButton;
32import javax.swing.JCheckBox;
33import javax.swing.JComboBox;
34import javax.swing.JLabel;
35import javax.swing.JTextField;
36import javax.swing.SwingConstants;
37import javax.swing.border.TitledBorder;
38
39import org.apache.commons.lang.StringUtils;
40
41import br.com.prognus.psync.engine.source.PIMCalendarSyncSource;
42import br.com.prognus.psync.engine.source.PIMContactSyncSource;
43import br.com.prognus.psync.engine.source.PIMSyncSource;
44
45import com.funambol.admin.AdminException;
46import com.funambol.admin.mo.SyncSourceManagementObject;
47import com.funambol.admin.ui.SourceManagementPanel;
48import com.funambol.framework.engine.source.SyncSource;
49
50public abstract class PIMSyncSourceConfigPanel extends SourceManagementPanel
51                implements Serializable {
52
53        // --------------------------------------------------------------- Constants
54
55        protected static final String PANEL_NAME = "Edit PIM SyncSource";
56
57        public static final String NAME_ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_.";
58
59        // All possible PIM SyncSource's
60        public static final Class[] SYNCSOURCE = { PIMContactSyncSource.class,
61                        PIMCalendarSyncSource.class, };
62
63        // ------------------------------------------------------------ Private data
64
65        /** Label for the panel's name */
66        protected JLabel panelName = new JLabel();
67
68        /** Border to pose the panel's title in evidence */
69        protected TitledBorder titledBorder;
70
71        protected JLabel nameLabel = new JLabel();
72
73        protected JTextField nameValue = new JTextField();
74
75        protected JLabel typeLabel = new JLabel();
76
77        protected JComboBox typeCombo = new JComboBox();
78
79        protected JLabel sourceUriLabel = new JLabel();
80
81        protected JTextField sourceUriValue = new JTextField();
82
83        protected JCheckBox encryption = new JCheckBox();
84
85        protected JCheckBox encoding = new JCheckBox();
86
87        protected JButton confirmButton = new JButton();
88
89        // ------------------------------------------------------------ Constructors
90
91        /**
92         * Creates a new instance of PIMSyncSourceConfigPanel.
93         */
94        public PIMSyncSourceConfigPanel() {
95                init();
96        }
97
98        /**
99         * Updates the panel form with values from the linked SyncSource.
100         */
101        public void updateForm() {
102
103                if (!(getSyncSource() instanceof PIMSyncSource)) {
104                        notifyError(new AdminException("This is not a PIMSyncSource! "
105                                        + "Unable to process SyncSource values."));
106                        return;
107                }
108
109                PIMSyncSource syncSource = (PIMSyncSource) getSyncSource();
110
111                if (getState() == STATE_INSERT) {
112                        confirmButton.setText("Add");
113                } else if (getState() == STATE_UPDATE) {
114                        confirmButton.setText("Save");
115                }
116
117                sourceUriValue.setText(syncSource.getSourceURI());
118                nameValue.setText(syncSource.getName());
119
120                if (syncSource.getSourceURI() != null) {
121                        sourceUriValue.setEditable(false);
122                }
123
124                // Preparing to populate the combo box...
125                typeCombo.removeAllItems();
126                List types = getTypes();
127                if (types == null) {
128                        types = new ArrayList();
129                }
130                for (int i = 0; i < types.size(); i++) {
131                        typeCombo.addItem(types.get(i));
132                }
133                String typeToSelect = getTypeToSelect(syncSource);
134                if (typeToSelect != null) {
135                        typeCombo.setSelectedItem(typeToSelect);
136                } else {
137                        typeCombo.setSelectedIndex(0);
138                }
139
140                SyncSourceManagementObject mo = (SyncSourceManagementObject) getManagementObject();
141                String transformationsRequired = mo.getTransformationsRequired();
142
143                if (transformationsRequired == null) {
144                        encryption.setSelected(false);
145                        encoding.setSelected(false);
146                } else {
147                        if ("des;b64".equals(transformationsRequired)) {
148                                encryption.setSelected(true);
149                                encoding.setSelected(true);
150                        } else if ("b64".equals(transformationsRequired)) {
151                                encryption.setSelected(false);
152                                encoding.setSelected(true);
153                        } else {
154                                encryption.setSelected(false);
155                                encoding.setSelected(false);
156                        }
157                }
158
159                // In any case, force the encoding for SIF data
160                if (isSIFSelected()) {
161                        encoding.setSelected(true);
162                        encoding.setEnabled(false);
163                }
164
165        }
166
167        /**
168         * Set preferredSize of the panel
169         *
170         * @return preferredSize of the panel
171         */
172        public Dimension getPreferredSize() {
173                return new Dimension(525, 280);
174        }
175
176        // --------------------------------------------------------- Private methods
177
178        /**
179         * Checks if the values provided by the user are all valid. If they are, the
180         * method ends regularly.
181         *
182         * @throws IllegalArgumentException
183         *             if name, uri, type or directory are empty (null or
184         *             zero-length)
185         */
186        protected void validateValues() throws IllegalArgumentException {
187                String value = null;
188
189                value = nameValue.getText();
190                if (StringUtils.isEmpty(value)) {
191                        throw new IllegalArgumentException("Field 'Name' cannot be empty. "
192                                        + "Please provide a SyncSource name.");
193                }
194
195                if (!StringUtils.containsOnly(value, NAME_ALLOWED_CHARS.toCharArray())) {
196                        throw new IllegalArgumentException(
197                                        "Only the following characters are allowed for field 'Name':"
198                                                        + "\n" + NAME_ALLOWED_CHARS);
199                }
200
201                value = (String) typeCombo.getSelectedItem();
202                if (StringUtils.isEmpty(value)) {
203                        throw new IllegalArgumentException("Field 'Type' cannot be empty. "
204                                        + "Please provide a SyncSource type.");
205                }
206
207                value = sourceUriValue.getText();
208                if (StringUtils.isEmpty(value)) {
209                        throw new IllegalArgumentException(
210                                        "Field 'Source URI' cannot be empty. "
211                                                        + "Please provide a SyncSource URI.");
212                }
213        }
214
215        /**
216         * Sets the SyncSource's properties with the values provided by the user.
217         */
218        protected void getValues() {
219                PIMSyncSource syncSource = (PIMSyncSource) getSyncSource();
220
221                syncSource.setSourceURI(sourceUriValue.getText().trim());
222                syncSource.setName(nameValue.getText().trim());
223
224                String transformationsRequired = null;
225                if (encryption.isSelected()) {
226                        transformationsRequired = "des;b64";
227                } else if (encoding.isSelected()) {
228                        transformationsRequired = "b64";
229                }
230                SyncSourceManagementObject mo = (SyncSourceManagementObject) getManagementObject();
231                mo.setTransformationsRequired(transformationsRequired);
232
233                setSyncSourceInfo(syncSource, (String) typeCombo.getSelectedItem());
234        }
235
236        /**
237         * Checks whether a SIF content type is selected.
238         *
239         * @return true if in the combo box the selected string starts with "SIF"
240         */
241        protected boolean isSIFSelected() {
242
243                if (typeCombo.getItemCount() == 0) {
244                        return false;
245                }
246                return ((String) typeCombo.getSelectedItem()).startsWith("SIF");
247        }
248
249        /**
250         * Adds extra components just above the confirm button.
251         *
252         * @param x
253         *            horizontal position
254         * @param y
255         *            vertical position
256         * @param xGap
257         *            standard horizontal gap
258         * @param yGap
259         *            standard vertical gap
260         * @return the new vertical position
261         */
262        protected int addExtraComponents(int x, int y, int xGap, int yGap) {
263                // Nothing
264
265                return y;
266        }
267
268        /**
269         * Returns the panel name
270         *
271         * @return the panel name
272         */
273        protected String getPanelName() {
274                return PANEL_NAME;
275        }
276
277        /**
278         * Returns the available types
279         *
280         * @return the available types;
281         */
282        protected abstract List getTypes();
283
284        /**
285         * Returns the type to select based on the given syncsource
286         *
287         * @return the type to select based on the given syncsource
288         */
289        protected abstract String getTypeToSelect(SyncSource syncSource);
290
291        /**
292         * Sets the source info of the given syncsource based on the given
293         * selectedType
294         *
295         * @param syncSource
296         *            the source
297         * @param selectedType
298         *            the selected type
299         */
300        protected abstract void setSyncSourceInfo(SyncSource syncSource,
301                        String selectedType);
302
303        // --------------------------------------------------------- Private methods
304
305        /**
306         * Creates the panel's components and layout.
307         *
308         * @todo adjust layout
309         */
310        private void init() {
311                // set layout
312                this.setLayout(null);
313                // set properties of label, position and border
314                // referred to the title of the panel
315                titledBorder = new TitledBorder("");
316                panelName.setFont(titlePanelFont);
317                panelName.setText(getPanelName());
318                panelName.setBounds(new Rectangle(14, 5, 316, 28));
319                panelName.setAlignmentX(SwingConstants.CENTER);
320                panelName.setBorder(titledBorder);
321
322                final int LABEL_X = 14;
323                final int VALUE_X = 170;
324                int y = 60;
325                final int GAP_X = 150;
326                final int GAP_Y = 30;
327
328                sourceUriLabel.setText("Source URI: ");
329                sourceUriLabel.setFont(defaultFont);
330                sourceUriLabel.setBounds(new Rectangle(LABEL_X, y, 150, 18));
331                sourceUriValue.setFont(defaultFont);
332                sourceUriValue.setBounds(new Rectangle(VALUE_X, y, 350, 18));
333
334                y += GAP_Y; // New line
335
336                nameLabel.setText("Name: ");
337                nameLabel.setFont(defaultFont);
338                nameLabel.setBounds(new Rectangle(LABEL_X, y, 150, 18));
339                nameValue.setFont(defaultFont);
340                nameValue.setBounds(new Rectangle(VALUE_X, y, 350, 18));
341                y += GAP_Y; // New line
342
343                typeLabel.setText("Type: ");
344                typeLabel.setFont(defaultFont);
345                typeLabel.setBounds(new Rectangle(LABEL_X, y, 150, 18));
346                typeCombo.setFont(defaultFont);
347                typeCombo.setBounds(new Rectangle(VALUE_X, y, 350, 18));
348
349                // What happens when the Type value is changed?
350                typeCombo.addItemListener(new ItemListener() {
351                        public void itemStateChanged(ItemEvent e) {
352                                if (isSIFSelected()) {
353                                        encoding.setSelected(true); // SIFs always encoded
354                                        encoding.setEnabled(false);
355                                } else {
356                                        encryption.setSelected(false);
357                                        encoding.setSelected(false);
358                                        encoding.setEnabled(true);
359                                }
360                        }
361                });
362
363                y += GAP_Y; // New line
364                int x = LABEL_X;
365
366                y = addExtraComponents(x, y, GAP_X, GAP_Y); // Add other components, if
367                // needed
368
369                encryption.setText("Encrypt data");
370                encryption.setFont(defaultFont);
371                encryption.setSelected(false);
372                encryption.setBounds(x, y, 150, 25);
373
374                // What happens if the encryption is enabled?
375                encryption.addItemListener(new ItemListener() {
376                        public void itemStateChanged(ItemEvent e) {
377                                if (e.getStateChange() == e.SELECTED) {
378                                        encoding.setSelected(true); // Encryption implies encoding
379                                        encoding.setEnabled(false);
380                                }
381                                if (e.getStateChange() == e.DESELECTED) {
382                                        if (!isSIFSelected()) {
383                                                encoding.setEnabled(true);
384                                        }
385                                }
386                        }
387                });
388
389                y += GAP_Y; // New line
390
391                encoding.setText("Encode data in Base 64");
392                encoding.setFont(defaultFont);
393                encoding.setSelected(false);
394                encoding.setBounds(x, y, 150, 25);
395
396                y += GAP_Y; // New line
397                y += GAP_Y; // New line
398
399                confirmButton.setFont(defaultFont);
400                confirmButton.setText("Add");
401                confirmButton.setBounds(VALUE_X, y, 70, 25);
402
403                // What happens when the confirmButton is pressed?
404                confirmButton.addActionListener(new ActionListener() {
405                        public void actionPerformed(ActionEvent event) {
406                                try {
407                                        validateValues();
408                                        getValues();
409                                        if (getState() == STATE_INSERT) {
410                                                PIMSyncSourceConfigPanel.this
411                                                                .actionPerformed(new ActionEvent(
412                                                                                PIMSyncSourceConfigPanel.this,
413                                                                                ACTION_EVENT_INSERT, event
414                                                                                                .getActionCommand()));
415                                        } else {
416                                                PIMSyncSourceConfigPanel.this
417                                                                .actionPerformed(new ActionEvent(
418                                                                                PIMSyncSourceConfigPanel.this,
419                                                                                ACTION_EVENT_UPDATE, event
420                                                                                                .getActionCommand()));
421                                        }
422                                } catch (Exception e) {
423                                        notifyError(new AdminException(e.getMessage(), e));
424                                }
425                        }
426                });
427
428                // Adds all components to the panel
429                this.add(panelName, null);
430                this.add(nameLabel, null);
431                this.add(sourceUriLabel, null);
432                this.add(sourceUriValue, null);
433                this.add(nameValue, null);
434                this.add(typeLabel, null);
435                this.add(typeCombo, null);
436                this.add(encryption, null);
437                this.add(encoding, null);
438                this.add(confirmButton, null);
439        }
440
441}
Note: See TracBrowser for help on using the repository browser.