source: contrib/psync/src/main/java/br/com/prognus/psync/admin/PIMCalendarSyncSourceConfigPanel.java @ 1009

Revision 1009, 9.0 KB checked in by wmerlotto, 15 years ago (diff)

Ticket #554 - Commit da versão inicial do psync.

Line 
1/**
2 * This class does something.
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.event.ItemEvent;
23import java.awt.event.ItemListener;
24import java.io.Serializable;
25import java.util.ArrayList;
26import java.util.List;
27
28import javax.swing.JCheckBox;
29
30import br.com.prognus.psync.engine.source.PIMCalendarSyncSource;
31import br.com.prognus.psync.engine.source.PIMSyncSource;
32
33import com.funambol.admin.AdminException;
34import com.funambol.common.pim.calendar.CalendarContent;
35import com.funambol.common.pim.calendar.Event;
36import com.funambol.common.pim.calendar.Task;
37import com.funambol.framework.engine.source.ContentType;
38import com.funambol.framework.engine.source.SyncSource;
39import com.funambol.framework.engine.source.SyncSourceInfo;
40
41public class PIMCalendarSyncSourceConfigPanel extends PIMSyncSourceConfigPanel
42                implements Serializable {
43
44        // --------------------------------------------------------------- Constants
45        protected static final String PANEL_NAME = "Edit PIM Calendar SyncSource";
46
47        protected static final String TYPE_LABEL_SIFE = "SIF-E";
48
49        protected static final String TYPE_LABEL_SIFT = "SIF-T";
50
51        protected static final String TYPE_LABEL_VCAL = "VCal";
52
53        protected static final String TYPE_LABEL_ICAL = "ICal";
54
55        protected static final String TYPE_SIFE = "text/x-s4j-sife";
56
57        protected static final String TYPE_SIFT = "text/x-s4j-sift";
58
59        protected static final String TYPE_VCAL = "text/x-vcalendar";
60
61        protected static final String TYPE_ICAL = "text/calendar";
62
63        protected static final String VERSION_SIFE = "1.0";
64
65        protected static final String VERSION_SIFT = "1.0";
66
67        protected static final String VERSION_VCAL = "1.0";
68
69        protected static final String VERSION_ICAL = "2.0";
70
71        // ---------------------------------------------------------- Protected data
72        protected JCheckBox eventValue;
73
74        protected JCheckBox taskValue;
75
76        // ------------------------------------------------------------ Constructors
77
78        /** Creates a new instance of PIMCalendarSyncSourceConfigPanel. */
79        public PIMCalendarSyncSourceConfigPanel() {
80                super();
81
82                // this.add(eventLabel, null);
83                this.add(eventValue, null);
84                // this.add(taskLabel, null);
85                this.add(taskValue, null);
86
87                typeCombo.addItemListener(new ItemListener() {
88                        public void itemStateChanged(ItemEvent e) {
89                                updateEntityTypeCheckBoxes();
90                        }
91                });
92
93        }
94
95        // ---------------------------------------------------------- Public methods
96
97        /**
98         * Updates the form
99         */
100        public void updateForm() {
101
102                if (!(getSyncSource() instanceof PIMCalendarSyncSource)) {
103                        notifyError(new AdminException(
104                                        "This is not a PIMCalendarSyncSource! "
105                                                        + "Unable to process SyncSource values."));
106                        return;
107                }
108
109                super.updateForm();
110
111                updateEntityTypeCheckBoxes();
112        }
113
114        /**
115         * Set preferredSize of the panel
116         *
117         * @return preferredSize of the panel
118         */
119        public Dimension getPreferredSize() {
120                return new Dimension(525, 310);
121        }
122
123        // ------------------------------------------------------- Protected methods
124
125        /**
126         * Adds extra components just above the confirm button.
127         *
128         * @param x
129         *            horizontal position
130         * @param y
131         *            vertical position
132         * @param xGap
133         *            standard horizontal gap
134         * @param yGap
135         *            standard vertical gap
136         * @return the new vertical position
137         */
138        protected int addExtraComponents(int x, int y, int xGap, int yGap) {
139
140                eventValue = new JCheckBox("Events");
141                taskValue = new JCheckBox("Tasks");
142
143                eventValue.setFont(defaultFont);
144                eventValue.setSelected(true);
145                eventValue.setBounds(170, y, 100, 25);
146                eventValue.setEnabled(true);
147
148                x += xGap; // Shift a bit to the right
149
150                taskValue.setFont(defaultFont);
151                taskValue.setSelected(true);
152                taskValue.setBounds(170 + xGap, y, 100, 25);
153                taskValue.setEnabled(true);
154
155                return y + yGap;
156        }
157
158        /**
159         * Updates entities checkboxes according to the selected type
160         */
161        protected void updateEntityTypeCheckBoxes() {
162                if (isSIFSelected()) {
163                        eventValue.setSelected(areEventsAllowed());
164                        taskValue.setSelected(areTasksAllowed());
165
166                        eventValue.setEnabled(false);
167                        taskValue.setEnabled(false);
168                } else {
169                        PIMCalendarSyncSource syncSource = (PIMCalendarSyncSource) getSyncSource();
170                        boolean events = false;
171                        boolean tasks = false;
172                        if (syncSource.getEntityType() == null
173                                        || syncSource.getEntityType().isAssignableFrom(Event.class)) {
174                                events = true;
175                        }
176                        if (syncSource.getEntityType() == null
177                                        || syncSource.getEntityType().isAssignableFrom(Task.class)) {
178                                tasks = true;
179                        }
180                        eventValue.setSelected(events);
181                        taskValue.setSelected(tasks);
182
183                        eventValue.setEnabled(true);
184                        taskValue.setEnabled(true);
185                }
186        }
187
188        /**
189         * Validates the values selected.
190         *
191         * @throws java.lang.IllegalArgumntException
192         *             if there is an invalid value
193         */
194        protected void validateValues() throws IllegalArgumentException {
195                super.validateValues();
196
197                if (!eventValue.isSelected() && !taskValue.isSelected()) {
198                        throw new IllegalArgumentException(
199                                        "Please check at least one between 'Events' and 'Tasks'.");
200                }
201        }
202
203        /**
204         * Sets the syncsource with the inserted values
205         */
206        protected void getValues() {
207                super.getValues();
208
209                if ((eventValue == null) || (taskValue == null)) {
210                        return;
211                }
212
213                PIMCalendarSyncSource syncSource = (PIMCalendarSyncSource) getSyncSource();
214
215                Class entityType;
216                if (eventValue.isSelected()) {
217                        if (taskValue.isSelected()) {
218                                entityType = CalendarContent.class;
219                        } else {
220                                entityType = Event.class;
221                        }
222                } else {
223                        entityType = Task.class;
224                }
225                syncSource.setEntityType(entityType);
226        }
227
228        /**
229         * Returns the panel name
230         *
231         * @return the panel name
232         */
233        protected String getPanelName() {
234                return PANEL_NAME;
235        }
236
237        /**
238         * Returns the available types
239         *
240         * @return the available types;
241         */
242        protected List getTypes() {
243                List supportedTypes = new ArrayList();
244                supportedTypes.add(TYPE_LABEL_SIFE);
245                supportedTypes.add(TYPE_LABEL_SIFT);
246                supportedTypes.add(TYPE_LABEL_VCAL);
247                supportedTypes.add(TYPE_LABEL_ICAL);
248                return supportedTypes;
249        }
250
251        /**
252         * Returns the type to select based on the given syncsource
253         *
254         * @return the type to select based on the given syncsource
255         */
256        protected String getTypeToSelect(SyncSource syncSource) {
257                String preferredType = null;
258                if (syncSource.getInfo() != null
259                                && syncSource.getInfo().getPreferredType() != null) {
260
261                        preferredType = syncSource.getInfo().getPreferredType().getType();
262                        if (TYPE_ICAL.equals(preferredType)) {
263                                return TYPE_LABEL_ICAL;
264                        }
265                        if (TYPE_VCAL.equals(preferredType)) {
266                                return TYPE_LABEL_VCAL;
267                        }
268                        if (TYPE_SIFE.equals(preferredType)) {
269                                return TYPE_LABEL_SIFE;
270                        }
271                        if (TYPE_SIFT.equals(preferredType)) {
272                                return TYPE_LABEL_SIFT;
273                        }
274                }
275                return null;
276        }
277
278        /**
279         * Sets the source info of the given syncsource based on the given
280         * selectedType
281         *
282         * @param syncSource
283         *            the source
284         * @param selectedType
285         *            the selected type
286         */
287        public void setSyncSourceInfo(SyncSource syncSource, String selectedType) {
288                PIMSyncSource pimSource = (PIMSyncSource) syncSource;
289                ContentType[] contentTypes = null;
290                if (TYPE_LABEL_ICAL.equals(selectedType)) {
291                        contentTypes = new ContentType[2];
292                        contentTypes[0] = new ContentType(TYPE_ICAL, VERSION_ICAL);
293                        contentTypes[1] = new ContentType(TYPE_VCAL, VERSION_VCAL);
294                } else if (TYPE_LABEL_VCAL.equals(selectedType)) {
295                        contentTypes = new ContentType[2];
296                        contentTypes[0] = new ContentType(TYPE_VCAL, VERSION_VCAL);
297                        contentTypes[1] = new ContentType(TYPE_ICAL, VERSION_ICAL);
298                } else if (TYPE_LABEL_SIFE.equals(selectedType)) {
299                        contentTypes = new ContentType[1];
300                        contentTypes[0] = new ContentType(TYPE_SIFE, VERSION_SIFE);
301                } else if (TYPE_LABEL_SIFT.equals(selectedType)) {
302                        contentTypes = new ContentType[1];
303                        contentTypes[0] = new ContentType(TYPE_SIFT, VERSION_SIFT);
304                }
305
306                pimSource.setInfo(new SyncSourceInfo(contentTypes, 0));
307        }
308
309        // --------------------------------------------------------- Private methods
310
311        private boolean areEventsAllowed() {
312                if (typeCombo.getSelectedItem() == null) {
313                        return false;
314                }
315
316                if (TYPE_LABEL_SIFT.equals((String) typeCombo.getSelectedItem())) {
317                        return false;
318                }
319                return true;
320        }
321
322        private boolean areTasksAllowed() {
323                if (typeCombo.getSelectedItem() == null) {
324                        return false;
325                }
326
327                if (TYPE_LABEL_SIFE.equals((String) typeCombo.getSelectedItem())) {
328                        return false;
329                }
330                return true;
331        }
332
333}
Note: See TracBrowser for help on using the repository browser.