KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > exchange > admin > ExchangeSyncSourceConfigPanel


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.exchange.admin;
20
21 import java.awt.Rectangle JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24
25 import java.io.Serializable JavaDoc;
26
27 import javax.swing.ButtonGroup JavaDoc;
28 import javax.swing.JButton JavaDoc;
29 import javax.swing.JCheckBox JavaDoc;
30 import javax.swing.JComboBox JavaDoc;
31 import javax.swing.JLabel JavaDoc;
32 import javax.swing.JOptionPane JavaDoc;
33 import javax.swing.JRadioButton JavaDoc;
34 import javax.swing.JTextField JavaDoc;
35
36 import javax.swing.SwingConstants JavaDoc;
37 import javax.swing.border.TitledBorder JavaDoc;
38
39 import java.util.StringTokenizer JavaDoc;
40
41 import sync4j.framework.engine.source.ContentType;
42 import sync4j.framework.engine.source.SyncSource;
43 import sync4j.framework.engine.source.SyncSourceInfo;
44
45 import sync4j.syncadmin.AdminException;
46 import sync4j.syncadmin.ui.SourceManagementPanel;
47
48 import sync4j.exchange.engine.source.ExchangeCalendarSyncSource;
49 import sync4j.exchange.engine.source.ExchangeContactSyncSource;
50 import sync4j.exchange.engine.source.ExchangeNoteSyncSource;
51 import sync4j.exchange.engine.source.ExchangeTaskSyncSource;
52 import sync4j.exchange.engine.source.ExchangeSyncSource;
53
54 import org.apache.commons.lang.StringUtils;
55
56 /**
57  * This class implements the configuration panel for an ExchangeSyncSource
58  *
59  * @author Luigia Fassina
60  *
61  * @version $Id: ExchangeSyncSourceConfigPanel.java,v 1.11 2005/06/03 13:45:14 stefano_fornari Exp $
62  */

63 public class ExchangeSyncSourceConfigPanel
64 extends SourceManagementPanel
65 implements Serializable JavaDoc {
66
67   // ----------------------------------------------------------------- Constants
68

69   public static final String JavaDoc NAME_ALLOWED_CHARS =
70     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_." ;
71   public static final String JavaDoc PORT_ALLOWED_CHARS =
72     "0123456789" ;
73
74   public static final String JavaDoc VCARD_TYPE = "text/x-vcard";
75   public static final String JavaDoc VCARD_TYPES = "text/x-vcard,text/vcard" ;
76   public static final String JavaDoc VCARD_VERSIONS = "2.1, 3.0" ;
77   public static final String JavaDoc ICAL_TYPE = "text/x-vcalendar" ;
78   public static final String JavaDoc ICAL_TYPES = "text/x-vcalendar" ;
79   public static final String JavaDoc ICAL_VERSIONS = "1.0" ;
80   public static final String JavaDoc SIFC_TYPE = "text/x-s4j-sifc" ;
81   public static final String JavaDoc SIFC_TYPES = "text/x-s4j-sifc" ;
82   public static final String JavaDoc SIFC_VERSIONS = "1.0" ;
83   public static final String JavaDoc SIFE_TYPE = "text/x-s4j-sife" ;
84   public static final String JavaDoc SIFE_TYPES = "text/x-s4j-sife" ;
85   public static final String JavaDoc SIFE_VERSIONS = "1.0";
86   public static final String JavaDoc SIFN_TYPE = "text/x-s4j-sifn" ;
87   public static final String JavaDoc SIFN_TYPES = "text/x-s4j-sifn" ;
88   public static final String JavaDoc SIFN_VERSIONS = "1.0";
89   public static final String JavaDoc SIFT_TYPE = "text/x-s4j-sift" ;
90   public static final String JavaDoc SIFT_TYPES = "text/x-s4j-sift" ;
91   public static final String JavaDoc SIFT_VERSIONS = "1.0" ;
92
93   // -------------------------------------------------------------- Private data
94
/** label for the panel's name */
95   private JLabel JavaDoc panelName = new JLabel JavaDoc();
96
97   /** border to evidence the title of the panel */
98   private TitledBorder JavaDoc titledBorder1;
99
100   private JLabel JavaDoc nameLabel = new JLabel JavaDoc () ;
101   private JTextField JavaDoc nameValue = new JTextField JavaDoc () ;
102
103   private JLabel JavaDoc sourceUriLabel = new JLabel JavaDoc () ;
104   private JTextField JavaDoc sourceUriValue = new JTextField JavaDoc () ;
105
106   private JLabel JavaDoc exchangeFolderLabel = new JLabel JavaDoc () ;
107   private JTextField JavaDoc exchangeFolderValue = new JTextField JavaDoc () ;
108
109   private JLabel JavaDoc hostLabel = new JLabel JavaDoc () ;
110   private JTextField JavaDoc hostValue = new JTextField JavaDoc () ;
111
112   private JLabel JavaDoc portLabel = new JLabel JavaDoc () ;
113   private JTextField JavaDoc portValue = new JTextField JavaDoc () ;
114
115   private JLabel JavaDoc conversionTypeLabel = new JLabel JavaDoc () ;
116
117   private JComboBox JavaDoc conversion = new JComboBox JavaDoc () ;
118
119   private JButton JavaDoc confirmButton = new JButton JavaDoc () ;
120
121   // ----------------------------------------------------------- Constructors
122

123   public ExchangeSyncSourceConfigPanel() {
124       init();
125   }
126
127   // ----------------------------------------------------------- Private methods
128
/**
129    * Create the panel
130    * @throws Exception if error occures during creation of the panel
131    */

132
133   private void init(){
134     // set layout
135
this.setLayout(null);
136
137     // set properties of label, position and border
138
// referred to the title of the panel
139
titledBorder1 = new TitledBorder JavaDoc("");
140
141     panelName.setFont(titlePanelFont);
142     panelName.setText("Edit Exchange SyncSource");
143     panelName.setBounds(new Rectangle JavaDoc(14, 5, 316, 28));
144     panelName.setAlignmentX(SwingConstants.CENTER);
145     panelName.setBorder(titledBorder1);
146
147     sourceUriLabel.setText("Source URI: ");
148     sourceUriLabel.setFont(defaultFont);
149     sourceUriLabel.setBounds(new Rectangle JavaDoc(14, 60, 150, 18) );
150     sourceUriValue.setFont(new java.awt.Font JavaDoc("Arial", 0, 12) );
151     sourceUriValue.setBounds(new Rectangle JavaDoc(170, 60, 350, 18) );
152
153     nameLabel.setText("Name: ");
154     nameLabel.setFont(defaultFont);
155     nameLabel.setBounds(new Rectangle JavaDoc(14, 90, 150, 18) );
156     nameValue.setFont(new java.awt.Font JavaDoc("Arial", 0, 12) );
157     nameValue.setBounds(new Rectangle JavaDoc(170, 90, 350, 18) );
158
159     hostLabel.setText("Exchange Server: ");
160     hostLabel.setFont(defaultFont);
161     hostLabel.setBounds(new Rectangle JavaDoc(14, 120, 150, 18) );
162     hostValue.setFont(new java.awt.Font JavaDoc("Arial", 0, 12) );
163     hostValue.setBounds(new Rectangle JavaDoc(170, 120, 350, 18) );
164
165     portLabel.setText("Exchange Port: ");
166     portLabel.setFont(defaultFont);
167     portLabel.setBounds(new Rectangle JavaDoc(14, 150, 150, 18) );
168     portValue.setFont(new java.awt.Font JavaDoc("Arial", 0, 12) );
169     portValue.setBounds(new Rectangle JavaDoc(170, 150, 350, 18) );
170
171     exchangeFolderLabel.setText("Exchange Folder: ");
172     exchangeFolderLabel.setFont(defaultFont);
173     exchangeFolderLabel.setBounds(new Rectangle JavaDoc(14, 180, 150, 18) );
174     exchangeFolderValue.setFont(new java.awt.Font JavaDoc("Arial", 0, 12) );
175     exchangeFolderValue.setBounds(new Rectangle JavaDoc(170, 180, 350, 18) );
176
177     conversionTypeLabel.setText("Type: ");
178     conversionTypeLabel.setFont(defaultFont);
179     conversionTypeLabel.setBounds(new Rectangle JavaDoc(14, 210, 150, 18) );
180     conversion.setFont(new java.awt.Font JavaDoc("Arial", 0, 12) );
181     conversion.setBounds(new Rectangle JavaDoc(170, 210, 80, 18) );
182
183     confirmButton.setFont(defaultFont);
184     confirmButton.setText("Add");
185     confirmButton.setBounds(170, 270, 70, 25);
186
187     confirmButton.addActionListener(new ActionListener JavaDoc() {
188         public void actionPerformed(ActionEvent JavaDoc event ) {
189             try {
190                 validateValues();
191                 getValues();
192                 if (getState() == STATE_INSERT) {
193                     ExchangeSyncSourceConfigPanel.this.actionPerformed(new ActionEvent JavaDoc(ExchangeSyncSourceConfigPanel.this, ACTION_EVENT_INSERT, event.getActionCommand()));
194                 } else {
195                     ExchangeSyncSourceConfigPanel.this.actionPerformed(new ActionEvent JavaDoc(ExchangeSyncSourceConfigPanel.this, ACTION_EVENT_UPDATE, event.getActionCommand()));
196                 }
197             } catch (Exception JavaDoc e) {
198                 notifyError(new AdminException(e.getMessage()));
199             }
200         }
201     });
202
203     // add all components to the panel
204
this.add(panelName , null ) ;
205     this.add(sourceUriLabel , null ) ;
206     this.add(sourceUriValue , null ) ;
207     this.add(nameLabel , null ) ;
208     this.add(nameValue , null ) ;
209     this.add(hostLabel , null ) ;
210     this.add(hostValue , null ) ;
211     this.add(portLabel , null ) ;
212     this.add(portValue , null ) ;
213     this.add(exchangeFolderLabel , null ) ;
214     this.add(exchangeFolderValue , null ) ;
215     this.add(conversionTypeLabel , null ) ;
216     this.add(conversion , null ) ;
217     this.add(confirmButton , null ) ;
218
219   }
220
221   public void updateForm() {
222       if (!(getSyncSource() instanceof ExchangeSyncSource)) {
223           notifyError(
224               new AdminException(
225                   "This is not an ExchangeSyncSource! Unable to process SyncSource values."
226               )
227           );
228           return;
229       }
230       
231       ExchangeSyncSource syncSource =
232           (ExchangeSyncSource)getSyncSource();
233
234       if (getState() == STATE_INSERT) {
235           confirmButton.setText("Add");
236       } else if (getState() == STATE_UPDATE) {
237           confirmButton.setText("Save");
238       }
239
240       sourceUriValue.setText (syncSource.getSourceURI() );
241       exchangeFolderValue.setText (syncSource.getExchangeFolder() );
242       nameValue.setText (syncSource.getName() );
243       hostValue.setText (syncSource.getHost() );
244       portValue.setText (String.valueOf(syncSource.getPort()));
245
246       conversion.removeAllItems();
247
248       if (syncSource instanceof ExchangeCalendarSyncSource) {
249         conversion.addItem ("SIF-E" );
250         conversion.addItem ("iCal" );
251       } else if (syncSource instanceof ExchangeContactSyncSource) {
252         conversion.addItem ("SIF-C" );
253         conversion.addItem ("vCard" );
254       } else if (syncSource instanceof ExchangeNoteSyncSource) {
255         conversion.addItem ("SIF-N" );
256       } else if (syncSource instanceof ExchangeTaskSyncSource) {
257         conversion.addItem ("SIF-T" );
258       }
259
260       if ((syncSource instanceof ExchangeCalendarSyncSource) ||
261           (syncSource instanceof ExchangeContactSyncSource)) {
262
263           String JavaDoc cType = ((ExchangeSyncSource) syncSource).getType();
264
265           if (VCARD_TYPE.equals(cType) || ICAL_TYPE.equals(cType)) {
266               conversion.setSelectedIndex(1);
267           } else {
268               conversion.setSelectedIndex(0);
269           }
270       }
271
272       if (syncSource.getSourceURI() != null) {
273           sourceUriValue.setEditable(false);
274       }
275   }
276
277
278   // ----------------------------------------------------------- Private methods
279

280   /**
281    * Checks if the values provided by the user are all valid. In caso of errors,
282    * a IllegalArgumentException is thrown.
283    *
284    * @throws IllegalArgumentException if:
285    * <ul>
286    * <li>name, uri, type or directory are empty (null or zero-length)
287    * <li>the types list length does not match the versions list length
288    * </ul>
289    */

290   private void validateValues() throws IllegalArgumentException JavaDoc {
291       String JavaDoc value = null;
292
293       value = nameValue.getText();
294       if (StringUtils.isEmpty(value)) {
295           throw new
296             IllegalArgumentException JavaDoc(
297                 "Field 'Name' cannot be empty. " +
298                 "Please provide a SyncSource name." );
299       }
300
301       if (!StringUtils.containsOnly(value, NAME_ALLOWED_CHARS.toCharArray())) {
302           throw new
303             IllegalArgumentException JavaDoc(
304                 "Only the following characters are " +
305                 "allowed for field 'Name': \n" +
306                 NAME_ALLOWED_CHARS );
307       }
308
309       value = exchangeFolderValue.getText();
310       if (StringUtils.isEmpty(value)) {
311         throw new
312             IllegalArgumentException JavaDoc(
313                 "Field 'Exchange folder' cannot be empty." +
314                 "Please provide an Exchange folder." );
315       }
316
317       value = hostValue.getText();
318       if (StringUtils.isEmpty(value)) {
319           throw new
320           IllegalArgumentException JavaDoc(
321               "Field 'Host' cannot be empty. " +
322               "Please provide a SyncSource host." );
323       }
324
325       value = portValue.getText();
326       if (StringUtils.isEmpty(value)) {
327           throw new
328             IllegalArgumentException JavaDoc(
329                 "Field 'Port' cannot be empty. " +
330                 "Please provide a SyncSource port." );
331       }
332
333       if (!StringUtils.containsOnly(value, PORT_ALLOWED_CHARS.toCharArray())) {
334           throw new
335             IllegalArgumentException JavaDoc(
336                 "Only the following characters are " +
337                 "allowed for field 'Port': \n" +
338                 PORT_ALLOWED_CHARS );
339       }
340
341       value = sourceUriValue.getText();
342       if (StringUtils.isEmpty(value)) {
343           throw new
344             IllegalArgumentException JavaDoc(
345                 "Field 'Source URI' cannot be empty. " +
346                 "Please provide a SyncSource URI." );
347       }
348
349   }
350
351   /**
352    * Set syncSource properties with the values provided by the user.
353    */

354   private void getValues() {
355       
356         ExchangeSyncSource syncSource = (ExchangeSyncSource)getSyncSource();
357
358         StringTokenizer JavaDoc types = null ;
359         StringTokenizer JavaDoc versions = null ;
360
361         syncSource.setSourceURI
362             (sourceUriValue.getText ().trim() ) ;
363         syncSource.setExchangeFolder
364             (exchangeFolderValue.getText ().trim() ) ;
365         syncSource.setName
366             (nameValue.getText ().trim() ) ;
367         syncSource.setHost
368             (hostValue.getText ().trim() ) ;
369         syncSource.setPort
370             (Integer.parseInt(portValue.getText ().trim())) ;
371
372         if (conversion.getSelectedIndex() == 0) {
373              syncSource.setEncode (true) ;
374
375             if (syncSource instanceof ExchangeCalendarSyncSource) {
376                 syncSource.setType(SIFE_TYPE);
377                 types = new StringTokenizer JavaDoc(SIFE_TYPES , "," ) ;
378                 versions = new StringTokenizer JavaDoc(SIFE_VERSIONS , "," ) ;
379             } else if (syncSource instanceof ExchangeContactSyncSource ) {
380                 syncSource.setType(SIFC_TYPE);
381                 types = new StringTokenizer JavaDoc(SIFC_TYPES , "," ) ;
382                 versions = new StringTokenizer JavaDoc(SIFC_VERSIONS , "," ) ;
383             } else if (syncSource instanceof ExchangeTaskSyncSource ) {
384                 syncSource.setType(SIFT_TYPE);
385                 types = new StringTokenizer JavaDoc(SIFT_TYPES , "," ) ;
386                 versions = new StringTokenizer JavaDoc(SIFT_VERSIONS , "," ) ;
387             } else if (syncSource instanceof ExchangeNoteSyncSource ) {
388                 syncSource.setType(SIFN_TYPE);
389                 types = new StringTokenizer JavaDoc(SIFN_TYPES , "," ) ;
390                 versions = new StringTokenizer JavaDoc(SIFN_VERSIONS , "," ) ;
391             }
392
393         } else if (syncSource instanceof ExchangeCalendarSyncSource) {
394             syncSource.setEncode (false) ;
395             syncSource.setType(ICAL_TYPE);
396             types = new StringTokenizer JavaDoc(ICAL_TYPES , "," ) ;
397             versions = new StringTokenizer JavaDoc(ICAL_VERSIONS , "," ) ;
398         } else if (syncSource instanceof ExchangeContactSyncSource ) {
399             syncSource.setEncode (false ) ;
400             syncSource.setType(VCARD_TYPE);
401             types = new StringTokenizer JavaDoc(VCARD_TYPES , "," ) ;
402             versions = new StringTokenizer JavaDoc(VCARD_VERSIONS , "," ) ;
403         }
404
405         ContentType[] contentTypes= new ContentType[types.countTokens()];
406
407         for(int i = 0, l = contentTypes.length; i < l; ++i) {
408           contentTypes[i] = new ContentType(types.nextToken().trim() ,
409                                             versions.nextToken().trim() );
410         }
411
412         syncSource.setInfo(new SyncSourceInfo(contentTypes, 0));
413   }
414 }
Popular Tags