1 18 19 package sync4j.exchange.admin; 20 21 import java.awt.Rectangle ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 25 import java.io.Serializable ; 26 27 import javax.swing.ButtonGroup ; 28 import javax.swing.JButton ; 29 import javax.swing.JCheckBox ; 30 import javax.swing.JComboBox ; 31 import javax.swing.JLabel ; 32 import javax.swing.JOptionPane ; 33 import javax.swing.JRadioButton ; 34 import javax.swing.JTextField ; 35 36 import javax.swing.SwingConstants ; 37 import javax.swing.border.TitledBorder ; 38 39 import java.util.StringTokenizer ; 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 63 public class ExchangeSyncSourceConfigPanel 64 extends SourceManagementPanel 65 implements Serializable { 66 67 69 public static final String NAME_ALLOWED_CHARS = 70 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_." ; 71 public static final String PORT_ALLOWED_CHARS = 72 "0123456789" ; 73 74 public static final String VCARD_TYPE = "text/x-vcard"; 75 public static final String VCARD_TYPES = "text/x-vcard,text/vcard" ; 76 public static final String VCARD_VERSIONS = "2.1, 3.0" ; 77 public static final String ICAL_TYPE = "text/x-vcalendar" ; 78 public static final String ICAL_TYPES = "text/x-vcalendar" ; 79 public static final String ICAL_VERSIONS = "1.0" ; 80 public static final String SIFC_TYPE = "text/x-s4j-sifc" ; 81 public static final String SIFC_TYPES = "text/x-s4j-sifc" ; 82 public static final String SIFC_VERSIONS = "1.0" ; 83 public static final String SIFE_TYPE = "text/x-s4j-sife" ; 84 public static final String SIFE_TYPES = "text/x-s4j-sife" ; 85 public static final String SIFE_VERSIONS = "1.0"; 86 public static final String SIFN_TYPE = "text/x-s4j-sifn" ; 87 public static final String SIFN_TYPES = "text/x-s4j-sifn" ; 88 public static final String SIFN_VERSIONS = "1.0"; 89 public static final String SIFT_TYPE = "text/x-s4j-sift" ; 90 public static final String SIFT_TYPES = "text/x-s4j-sift" ; 91 public static final String SIFT_VERSIONS = "1.0" ; 92 93 95 private JLabel panelName = new JLabel (); 96 97 98 private TitledBorder titledBorder1; 99 100 private JLabel nameLabel = new JLabel () ; 101 private JTextField nameValue = new JTextField () ; 102 103 private JLabel sourceUriLabel = new JLabel () ; 104 private JTextField sourceUriValue = new JTextField () ; 105 106 private JLabel exchangeFolderLabel = new JLabel () ; 107 private JTextField exchangeFolderValue = new JTextField () ; 108 109 private JLabel hostLabel = new JLabel () ; 110 private JTextField hostValue = new JTextField () ; 111 112 private JLabel portLabel = new JLabel () ; 113 private JTextField portValue = new JTextField () ; 114 115 private JLabel conversionTypeLabel = new JLabel () ; 116 117 private JComboBox conversion = new JComboBox () ; 118 119 private JButton confirmButton = new JButton () ; 120 121 123 public ExchangeSyncSourceConfigPanel() { 124 init(); 125 } 126 127 132 133 private void init(){ 134 this.setLayout(null); 136 137 titledBorder1 = new TitledBorder (""); 140 141 panelName.setFont(titlePanelFont); 142 panelName.setText("Edit Exchange SyncSource"); 143 panelName.setBounds(new Rectangle (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 (14, 60, 150, 18) ); 150 sourceUriValue.setFont(new java.awt.Font ("Arial", 0, 12) ); 151 sourceUriValue.setBounds(new Rectangle (170, 60, 350, 18) ); 152 153 nameLabel.setText("Name: "); 154 nameLabel.setFont(defaultFont); 155 nameLabel.setBounds(new Rectangle (14, 90, 150, 18) ); 156 nameValue.setFont(new java.awt.Font ("Arial", 0, 12) ); 157 nameValue.setBounds(new Rectangle (170, 90, 350, 18) ); 158 159 hostLabel.setText("Exchange Server: "); 160 hostLabel.setFont(defaultFont); 161 hostLabel.setBounds(new Rectangle (14, 120, 150, 18) ); 162 hostValue.setFont(new java.awt.Font ("Arial", 0, 12) ); 163 hostValue.setBounds(new Rectangle (170, 120, 350, 18) ); 164 165 portLabel.setText("Exchange Port: "); 166 portLabel.setFont(defaultFont); 167 portLabel.setBounds(new Rectangle (14, 150, 150, 18) ); 168 portValue.setFont(new java.awt.Font ("Arial", 0, 12) ); 169 portValue.setBounds(new Rectangle (170, 150, 350, 18) ); 170 171 exchangeFolderLabel.setText("Exchange Folder: "); 172 exchangeFolderLabel.setFont(defaultFont); 173 exchangeFolderLabel.setBounds(new Rectangle (14, 180, 150, 18) ); 174 exchangeFolderValue.setFont(new java.awt.Font ("Arial", 0, 12) ); 175 exchangeFolderValue.setBounds(new Rectangle (170, 180, 350, 18) ); 176 177 conversionTypeLabel.setText("Type: "); 178 conversionTypeLabel.setFont(defaultFont); 179 conversionTypeLabel.setBounds(new Rectangle (14, 210, 150, 18) ); 180 conversion.setFont(new java.awt.Font ("Arial", 0, 12) ); 181 conversion.setBounds(new Rectangle (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 () { 188 public void actionPerformed(ActionEvent event ) { 189 try { 190 validateValues(); 191 getValues(); 192 if (getState() == STATE_INSERT) { 193 ExchangeSyncSourceConfigPanel.this.actionPerformed(new ActionEvent (ExchangeSyncSourceConfigPanel.this, ACTION_EVENT_INSERT, event.getActionCommand())); 194 } else { 195 ExchangeSyncSourceConfigPanel.this.actionPerformed(new ActionEvent (ExchangeSyncSourceConfigPanel.this, ACTION_EVENT_UPDATE, event.getActionCommand())); 196 } 197 } catch (Exception e) { 198 notifyError(new AdminException(e.getMessage())); 199 } 200 } 201 }); 202 203 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 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 280 290 private void validateValues() throws IllegalArgumentException { 291 String value = null; 292 293 value = nameValue.getText(); 294 if (StringUtils.isEmpty(value)) { 295 throw new 296 IllegalArgumentException ( 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 ( 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 ( 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 ( 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 ( 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 ( 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 ( 345 "Field 'Source URI' cannot be empty. " + 346 "Please provide a SyncSource URI." ); 347 } 348 349 } 350 351 354 private void getValues() { 355 356 ExchangeSyncSource syncSource = (ExchangeSyncSource)getSyncSource(); 357 358 StringTokenizer types = null ; 359 StringTokenizer 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 (SIFE_TYPES , "," ) ; 378 versions = new StringTokenizer (SIFE_VERSIONS , "," ) ; 379 } else if (syncSource instanceof ExchangeContactSyncSource ) { 380 syncSource.setType(SIFC_TYPE); 381 types = new StringTokenizer (SIFC_TYPES , "," ) ; 382 versions = new StringTokenizer (SIFC_VERSIONS , "," ) ; 383 } else if (syncSource instanceof ExchangeTaskSyncSource ) { 384 syncSource.setType(SIFT_TYPE); 385 types = new StringTokenizer (SIFT_TYPES , "," ) ; 386 versions = new StringTokenizer (SIFT_VERSIONS , "," ) ; 387 } else if (syncSource instanceof ExchangeNoteSyncSource ) { 388 syncSource.setType(SIFN_TYPE); 389 types = new StringTokenizer (SIFN_TYPES , "," ) ; 390 versions = new StringTokenizer (SIFN_VERSIONS , "," ) ; 391 } 392 393 } else if (syncSource instanceof ExchangeCalendarSyncSource) { 394 syncSource.setEncode (false) ; 395 syncSource.setType(ICAL_TYPE); 396 types = new StringTokenizer (ICAL_TYPES , "," ) ; 397 versions = new StringTokenizer (ICAL_VERSIONS , "," ) ; 398 } else if (syncSource instanceof ExchangeContactSyncSource ) { 399 syncSource.setEncode (false ) ; 400 syncSource.setType(VCARD_TYPE); 401 types = new StringTokenizer (VCARD_TYPES , "," ) ; 402 versions = new StringTokenizer (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 |