[2082] | 1 | /* |
---|
| 2 | * Funambol is a mobile platform developed by Funambol, Inc. |
---|
| 3 | * Copyright (C) 2008 Funambol, Inc. |
---|
| 4 | * |
---|
| 5 | * This program is free software; you can redistribute it and/or modify it under |
---|
| 6 | * the terms of the GNU Affero General Public License version 3 as published by |
---|
| 7 | * the Free Software Foundation with the addition of the following permission |
---|
| 8 | * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED |
---|
| 9 | * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE |
---|
| 10 | * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, but WITHOUT |
---|
| 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 14 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 15 | * details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU Affero General Public License |
---|
| 18 | * along with this program; if not, see http://www.gnu.org/licenses or write to |
---|
| 19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
---|
| 20 | * MA 02110-1301 USA. |
---|
| 21 | * |
---|
| 22 | * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite |
---|
| 23 | * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com. |
---|
| 24 | * |
---|
| 25 | * The interactive user interfaces in modified source and object code versions |
---|
| 26 | * of this program must display Appropriate Legal Notices, as required under |
---|
| 27 | * Section 5 of the GNU Affero General Public License version 3. |
---|
| 28 | * |
---|
| 29 | * In accordance with Section 7(b) of the GNU Affero General Public License |
---|
| 30 | * version 3, these Appropriate Legal Notices must retain the display of the |
---|
| 31 | * "Powered by Funambol" logo. If the display of the logo is not reasonably |
---|
| 32 | * feasible for technical reasons, the Appropriate Legal Notices must display |
---|
| 33 | * the words "Powered by Funambol". |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | package br.com.prognus.psync; |
---|
| 37 | |
---|
| 38 | import java.io.Serializable; |
---|
| 39 | |
---|
| 40 | import java.awt.Rectangle; |
---|
| 41 | import java.awt.event.ActionEvent; |
---|
| 42 | import java.awt.event.ActionListener; |
---|
| 43 | |
---|
| 44 | import javax.swing.JButton; |
---|
| 45 | import javax.swing.JLabel; |
---|
| 46 | import javax.swing.JTextField; |
---|
| 47 | import javax.swing.SwingConstants; |
---|
| 48 | import javax.swing.border.TitledBorder; |
---|
| 49 | |
---|
| 50 | import org.apache.commons.lang.StringUtils; |
---|
| 51 | |
---|
| 52 | import com.funambol.framework.engine.source.ContentType; |
---|
| 53 | import com.funambol.framework.engine.source.SyncSourceInfo; |
---|
| 54 | |
---|
| 55 | import com.funambol.admin.AdminException; |
---|
| 56 | import com.funambol.admin.ui.SourceManagementPanel; |
---|
| 57 | import java.util.HashMap; |
---|
| 58 | import java.util.Iterator; |
---|
| 59 | import java.util.Map; |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * This class implements the configuration panel for MySyncSource |
---|
| 64 | * |
---|
| 65 | */ |
---|
| 66 | public class MySyncSourceAdminPanel |
---|
| 67 | extends SourceManagementPanel |
---|
| 68 | implements Serializable { |
---|
| 69 | |
---|
| 70 | // --------------------------------------------------------------- Constants |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * Allowed characters for name and uri |
---|
| 74 | */ |
---|
| 75 | public static final String NAME_ALLOWED_CHARS |
---|
| 76 | = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_."; |
---|
| 77 | |
---|
| 78 | // ------------------------------------------------------------ Private data |
---|
| 79 | /** label for the panel's name */ |
---|
| 80 | private JLabel panelName = new JLabel(); |
---|
| 81 | |
---|
| 82 | /** border to evidence the title of the panel */ |
---|
| 83 | private TitledBorder titledBorder1; |
---|
| 84 | |
---|
| 85 | private JLabel nameLabel = new JLabel() ; |
---|
| 86 | private JTextField nameValue = new JTextField() ; |
---|
| 87 | private JLabel typeLabel = new JLabel() ; |
---|
| 88 | private JTextField typeValue = new JTextField() ; |
---|
| 89 | private JLabel versionLabel = new JLabel() ; |
---|
| 90 | private JTextField versionValue = new JTextField() ; |
---|
| 91 | |
---|
| 92 | private JLabel sourceUriLabel = new JLabel() ; |
---|
| 93 | private JTextField sourceUriValue = new JTextField() ; |
---|
| 94 | |
---|
| 95 | private JLabel myStringLabel = new JLabel() ; |
---|
| 96 | private JTextField myStringValue = new JTextField() ; |
---|
| 97 | |
---|
| 98 | private JLabel myIntLabel = new JLabel() ; |
---|
| 99 | private JTextField myIntValue = new JTextField() ; |
---|
| 100 | |
---|
| 101 | private JLabel myMapLabel = new JLabel() ; |
---|
| 102 | private JTextField myMapEntry = new JTextField() ; |
---|
| 103 | private JTextField myMapValue = new JTextField() ; |
---|
| 104 | |
---|
| 105 | private JButton confirmButton = new JButton() ; |
---|
| 106 | |
---|
| 107 | private MySyncSource syncSource = null ; |
---|
| 108 | |
---|
| 109 | // ------------------------------------------------------------ Constructors |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * Creates a new MySyncSourceAdminPanel instance |
---|
| 113 | */ |
---|
| 114 | public MySyncSourceAdminPanel() { |
---|
| 115 | init(); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | // ----------------------------------------------------------- Private methods |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * Create the panel |
---|
| 122 | * @throws Exception if error occures during creation of the panel |
---|
| 123 | */ |
---|
| 124 | private void init(){ |
---|
| 125 | // set layout |
---|
| 126 | this.setLayout(null); |
---|
| 127 | |
---|
| 128 | // set properties of label, position and border |
---|
| 129 | // referred to the title of the panel |
---|
| 130 | titledBorder1 = new TitledBorder(""); |
---|
| 131 | |
---|
| 132 | panelName.setFont(titlePanelFont); |
---|
| 133 | panelName.setText("Edit My SyncSource"); |
---|
| 134 | panelName.setBounds(new Rectangle(14, 5, 316, 28)); |
---|
| 135 | panelName.setAlignmentX(SwingConstants.CENTER); |
---|
| 136 | panelName.setBorder(titledBorder1); |
---|
| 137 | |
---|
| 138 | int y = 60; |
---|
| 139 | int dy = 30; |
---|
| 140 | |
---|
| 141 | sourceUriLabel.setText("Source URI: "); |
---|
| 142 | sourceUriLabel.setFont(defaultFont); |
---|
| 143 | sourceUriLabel.setBounds(new Rectangle(14, y, 150, 18)); |
---|
| 144 | sourceUriValue.setFont(defaultFont); |
---|
| 145 | sourceUriValue.setBounds(new Rectangle(170, y, 350, 18)); |
---|
| 146 | |
---|
| 147 | y += dy; |
---|
| 148 | |
---|
| 149 | nameLabel.setText("Name: "); |
---|
| 150 | nameLabel.setFont(defaultFont); |
---|
| 151 | nameLabel.setBounds(new Rectangle(14, y, 150, 18)); |
---|
| 152 | nameValue.setFont(defaultFont); |
---|
| 153 | nameValue.setBounds(new Rectangle(170, y, 350, 18)); |
---|
| 154 | |
---|
| 155 | y += dy; |
---|
| 156 | |
---|
| 157 | typeLabel.setText("Supported type: "); |
---|
| 158 | typeLabel.setFont(defaultFont); |
---|
| 159 | typeLabel.setBounds(new Rectangle(14, y, 150, 18)); |
---|
| 160 | typeValue.setFont(defaultFont); |
---|
| 161 | typeValue.setBounds(new Rectangle(170, y, 350, 18)); |
---|
| 162 | |
---|
| 163 | y += dy; |
---|
| 164 | |
---|
| 165 | versionLabel.setText("Supported version: "); |
---|
| 166 | versionLabel.setFont(defaultFont); |
---|
| 167 | versionLabel.setBounds(new Rectangle(14, y, 150, 18)); |
---|
| 168 | versionValue.setFont(defaultFont); |
---|
| 169 | versionValue.setBounds(new Rectangle(170, y, 350, 18)); |
---|
| 170 | |
---|
| 171 | y += dy; |
---|
| 172 | |
---|
| 173 | myStringLabel.setText("MyString: "); |
---|
| 174 | myStringLabel.setFont(defaultFont); |
---|
| 175 | myStringLabel.setBounds(new Rectangle(14, y, 150, 18)); |
---|
| 176 | myStringValue.setFont(defaultFont); |
---|
| 177 | myStringValue.setBounds(new Rectangle(170, y, 350, 18)); |
---|
| 178 | |
---|
| 179 | y += dy; |
---|
| 180 | |
---|
| 181 | myIntLabel.setText("MyInt: "); |
---|
| 182 | myIntLabel.setFont(defaultFont); |
---|
| 183 | myIntLabel.setBounds(new Rectangle(14, y, 150, 18)); |
---|
| 184 | myIntValue.setFont(defaultFont); |
---|
| 185 | myIntValue.setBounds(new Rectangle(170, y, 35, 18)); |
---|
| 186 | |
---|
| 187 | y += dy; |
---|
| 188 | |
---|
| 189 | myMapLabel.setText("MyMap entry: "); |
---|
| 190 | myMapLabel.setFont(defaultFont); |
---|
| 191 | myMapLabel.setBounds(new Rectangle(14, y, 150, 18)); |
---|
| 192 | myMapEntry.setFont(defaultFont); |
---|
| 193 | myMapEntry.setBounds(new Rectangle(170, y, 35, 18)); |
---|
| 194 | myMapValue.setFont(defaultFont); |
---|
| 195 | myMapValue.setBounds(new Rectangle(210, y, 35, 18)); |
---|
| 196 | |
---|
| 197 | y += dy; |
---|
| 198 | y += dy; |
---|
| 199 | |
---|
| 200 | confirmButton.setFont(defaultFont); |
---|
| 201 | confirmButton.setText("Add"); |
---|
| 202 | confirmButton.setBounds(170, y, 70, 25); |
---|
| 203 | |
---|
| 204 | confirmButton.addActionListener(new ActionListener() { |
---|
| 205 | public void actionPerformed(ActionEvent event ) { |
---|
| 206 | try { |
---|
| 207 | validateValues(); |
---|
| 208 | getValues(); |
---|
| 209 | if (getState() == STATE_INSERT) { |
---|
| 210 | MySyncSourceAdminPanel.this.actionPerformed( |
---|
| 211 | new ActionEvent(MySyncSourceAdminPanel.this, |
---|
| 212 | ACTION_EVENT_INSERT, |
---|
| 213 | event.getActionCommand())); |
---|
| 214 | } else { |
---|
| 215 | MySyncSourceAdminPanel.this.actionPerformed( |
---|
| 216 | new ActionEvent(MySyncSourceAdminPanel.this, |
---|
| 217 | ACTION_EVENT_UPDATE, |
---|
| 218 | event.getActionCommand())); |
---|
| 219 | } |
---|
| 220 | } catch (Exception e) { |
---|
| 221 | notifyError(new AdminException(e.getMessage())); |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | }); |
---|
| 225 | |
---|
| 226 | // add all components to the panel |
---|
| 227 | this.add(panelName , null); |
---|
| 228 | this.add(nameLabel , null); |
---|
| 229 | this.add(nameValue , null); |
---|
| 230 | this.add(typeLabel , null); |
---|
| 231 | this.add(typeValue , null); |
---|
| 232 | this.add(versionLabel , null); |
---|
| 233 | this.add(versionValue , null); |
---|
| 234 | this.add(sourceUriLabel , null); |
---|
| 235 | this.add(sourceUriValue , null); |
---|
| 236 | this.add(myStringLabel , null); |
---|
| 237 | this.add(myStringValue , null); |
---|
| 238 | this.add(myIntLabel , null); |
---|
| 239 | this.add(myIntValue , null); |
---|
| 240 | this.add(myMapLabel , null); |
---|
| 241 | this.add(myMapEntry , null); |
---|
| 242 | this.add(myMapValue , null); |
---|
| 243 | this.add(confirmButton , null); |
---|
| 244 | |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | /** |
---|
| 248 | * Loads the given syncSource showing the name, uri and type in the panel's |
---|
| 249 | * fields. |
---|
| 250 | * |
---|
| 251 | * @param syncSource the SyncSource instance |
---|
| 252 | */ |
---|
| 253 | public void updateForm() { |
---|
| 254 | if (!(getSyncSource() instanceof MySyncSource)) { |
---|
| 255 | notifyError( |
---|
| 256 | new AdminException( |
---|
| 257 | "This is not an MySyncSource! Unable to process SyncSource values." |
---|
| 258 | ) |
---|
| 259 | ); |
---|
| 260 | return; |
---|
| 261 | } |
---|
| 262 | if (getState() == STATE_INSERT) { |
---|
| 263 | confirmButton.setText("Add"); |
---|
| 264 | } else if (getState() == STATE_UPDATE) { |
---|
| 265 | confirmButton.setText("Save"); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | this.syncSource = (MySyncSource) getSyncSource(); |
---|
| 269 | |
---|
| 270 | sourceUriValue.setText(syncSource.getSourceURI() ); |
---|
| 271 | nameValue.setText (syncSource.getName() ); |
---|
| 272 | |
---|
| 273 | SyncSourceInfo info = syncSource.getInfo(); |
---|
| 274 | if (info != null) { |
---|
| 275 | ContentType[] types = info.getSupportedTypes(); |
---|
| 276 | |
---|
| 277 | StringBuffer typesList = new StringBuffer(), versionsList = new StringBuffer(); |
---|
| 278 | for (int i=0; ((types != null) && (i<types.length)); ++i) { |
---|
| 279 | typesList.append(types[i].getType()); |
---|
| 280 | versionsList.append(types[i].getVersion()); |
---|
| 281 | // |
---|
| 282 | // Also if the SyncSourceInfo contains more contentTypes, we handle |
---|
| 283 | // just the first one |
---|
| 284 | // |
---|
| 285 | break; |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | typeValue.setText(typesList.toString()); |
---|
| 289 | versionValue.setText(versionsList.toString()); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | if (this.syncSource.getSourceURI() != null) { |
---|
| 293 | sourceUriValue.setEditable(false); |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | if (syncSource.getMyString() != null) { |
---|
| 297 | myStringValue.setText(syncSource.getMyString()); |
---|
| 298 | } else { |
---|
| 299 | myStringValue.setText(""); |
---|
| 300 | } |
---|
| 301 | myIntValue.setText(String.valueOf(syncSource.getMyInt())); |
---|
| 302 | |
---|
| 303 | Map map = syncSource.getMyMap(); |
---|
| 304 | if (map != null) { |
---|
| 305 | Iterator i = map.keySet().iterator(); |
---|
| 306 | if (i.hasNext()) { |
---|
| 307 | String key = (String)i.next(); |
---|
| 308 | myMapEntry.setText(key); |
---|
| 309 | Object value = map.get(key); |
---|
| 310 | myMapValue.setText(((value != null) ? (String)value : "")); |
---|
| 311 | } |
---|
| 312 | } |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | // ----------------------------------------------------------- Private methods |
---|
| 316 | /** |
---|
| 317 | * Checks if the values provided by the user are all valid. In caso of errors, |
---|
| 318 | * a IllegalArgumentException is thrown. |
---|
| 319 | * |
---|
| 320 | * @throws IllegalArgumentException if: |
---|
| 321 | * <ul> |
---|
| 322 | * <li>name, uri, type or directory are empty (null or zero-length) |
---|
| 323 | * <li>the types list length does not match the versions list length |
---|
| 324 | * </ul> |
---|
| 325 | */ |
---|
| 326 | private void validateValues() throws IllegalArgumentException { |
---|
| 327 | String value = null; |
---|
| 328 | |
---|
| 329 | value = nameValue.getText().trim(); |
---|
| 330 | if (StringUtils.isEmpty(value)) { |
---|
| 331 | throw new |
---|
| 332 | IllegalArgumentException( |
---|
| 333 | "Field 'Name' cannot be empty. Please provide a SyncSource name."); |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | if (!StringUtils.containsOnly(value, NAME_ALLOWED_CHARS.toCharArray())) { |
---|
| 337 | throw new |
---|
| 338 | IllegalArgumentException( |
---|
| 339 | "Only the following characters are allowed for field 'Name': \n" + NAME_ALLOWED_CHARS); |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | value = typeValue.getText().trim(); |
---|
| 343 | if (StringUtils.isEmpty(value)) { |
---|
| 344 | throw new |
---|
| 345 | IllegalArgumentException( |
---|
| 346 | "Field 'Supported Type' cannot be empty. Please provide a type."); |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | value = versionValue.getText().trim(); |
---|
| 350 | if (StringUtils.isEmpty(value)) { |
---|
| 351 | throw new |
---|
| 352 | IllegalArgumentException( |
---|
| 353 | "Field 'Supported Version' cannot be empty. Please provide a version."); |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | value = sourceUriValue.getText().trim(); |
---|
| 357 | if (StringUtils.isEmpty(value)) { |
---|
| 358 | throw new |
---|
| 359 | IllegalArgumentException( |
---|
| 360 | "Field 'Source URI' cannot be empty. Please provide a SyncSource URI."); |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | value = myIntValue.getText().trim(); |
---|
| 364 | try { |
---|
| 365 | Integer.parseInt(value); |
---|
| 366 | } catch (NumberFormatException e) { |
---|
| 367 | throw new |
---|
| 368 | IllegalArgumentException( |
---|
| 369 | "Field 'myInt' must be an integer number."); |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | value = myMapValue.getText().trim(); |
---|
| 373 | if (!StringUtils.isEmpty(value) && StringUtils.isEmpty(myMapEntry.getText())) { |
---|
| 374 | throw new |
---|
| 375 | IllegalArgumentException( |
---|
| 376 | "Missing key for 'myMap'."); |
---|
| 377 | } |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | /** |
---|
| 381 | * Set syncSource properties with the values provided by the user. |
---|
| 382 | */ |
---|
| 383 | private void getValues() { |
---|
| 384 | syncSource.setSourceURI(sourceUriValue.getText().trim()); |
---|
| 385 | syncSource.setName (nameValue.getText().trim() ); |
---|
| 386 | |
---|
| 387 | ContentType[] contentTypes = new ContentType[] { |
---|
| 388 | new ContentType(typeValue.getText(), versionValue.getText()) |
---|
| 389 | }; |
---|
| 390 | |
---|
| 391 | syncSource.setInfo(new SyncSourceInfo(contentTypes, 0)); |
---|
| 392 | |
---|
| 393 | syncSource.setMyString(myStringValue.getText().trim()); |
---|
| 394 | syncSource.setMyInt(Integer.parseInt(myIntValue.getText().trim())); |
---|
| 395 | |
---|
| 396 | HashMap map = new HashMap(); |
---|
| 397 | map.put(myMapEntry.getText().trim(), myMapValue.getText().trim()); |
---|
| 398 | syncSource.setMyMap(map); |
---|
| 399 | } |
---|
| 400 | } |
---|