1 /**********************************************************************
2 * Copyright (c) 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM - Initial API and implementation
10 **********************************************************************/
11 package net.sourceforge.phpeclipse.wiki.internal;
13 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
14 import net.sourceforge.phpeclipse.wiki.preferences.UserValidationDialog;
15 import net.sourceforge.phpeclipse.wiki.preferences.Util;
17 import org.eclipse.swt.widgets.Shell;
22 public class Configuration implements IConfiguration, Comparable {
23 private static final String MEMENTO_ID = "id";
25 private static final String MEMENTO_NAME = "name";
27 private static final String MEMENTO_USER = "user";
29 private static final String MEMENTO_URL = "url";
31 private static final String MEMENTO_PASSWORD = "password";
33 private static final String MEMENTO_TYPE_ID = "type-id";
35 protected String fId = "";
37 protected String fName = "";
39 protected String fUrl = "";
41 protected String fPassword = "";
43 protected String fUser = "";
45 protected String fType = "";
47 private static final char[] SCRAMBLING_TABLE = new char[] {
305 public Configuration() {
306 this(WikiEditorPlugin.HTTP_QUERY); // default type
309 public Configuration(String type) {
313 public String getId() {
317 public String getName() {
321 public String getURL() {
325 public String getPassword() {
332 * @see org.eclipse.monitor.internal.IConfiguration#getLocalPort()
334 public String getUser() {
340 public String getType() {
347 * @see org.eclipse.monitor.internal.IConfiguration#isRunning()
349 public boolean isActive() {
350 return ConfigurationManager.getInstance().isActive(this);
353 public void delete() {
354 ConfigurationManager.getInstance().removeConfiguration(this);
357 public boolean isWorkingCopy() {
361 public IConfigurationWorkingCopy getWorkingCopy() {
362 return new ConfigurationWorkingCopy(this);
365 protected void setInternal(IConfiguration monitor) {
366 fId = monitor.getId();
367 fName = monitor.getName();
368 fUrl = monitor.getURL();
369 fPassword = monitor.getPassword();
370 fUser = monitor.getUser();
371 fType = monitor.getType();
374 protected void save(IMemento memento) {
375 memento.putString(MEMENTO_ID, fId);
376 memento.putString(MEMENTO_NAME, fName);
377 memento.putString(MEMENTO_TYPE_ID, fType);
378 memento.putString(MEMENTO_USER, fUser);
379 memento.putString(MEMENTO_URL, fUrl);
380 String result = 'A' + scramblePassword(fPassword);
381 memento.putString(MEMENTO_PASSWORD, result);
384 protected void load(IMemento memento) {
385 fId = memento.getString(MEMENTO_ID);
389 fName = memento.getString(MEMENTO_NAME);
393 fType = memento.getString(MEMENTO_TYPE_ID);
397 fUser = memento.getString(MEMENTO_USER);
401 fUrl = memento.getString(MEMENTO_URL);
405 String result = memento.getString(MEMENTO_PASSWORD);
407 if (result == null) {
410 fPassword = scramblePassword(result.substring(1));
417 * @see java.lang.Object#toString()
419 public String toString() {
420 StringBuffer buffer = new StringBuffer();
421 buffer.append(fName);
422 buffer.append(" - ");
423 buffer.append(fUser);
424 buffer.append(" - ");
426 buffer.append(" - ");
427 buffer.append(fType);
428 return buffer.toString();
434 * @see java.lang.Comparable#compareTo(java.lang.Object)
436 public int compareTo(Object o) {
437 if (o instanceof IConfiguration) {
438 return fName.compareTo(((IConfiguration) o).getName());
443 private static String scramblePassword(String password) {
444 int length = password.length();
445 char[] out = new char[length];
446 for (int i = 0; i < length; i++) {
447 char value = password.charAt(i);
448 out[i] = SCRAMBLING_TABLE[value];
450 return new String(out);
453 public boolean isUserComplete() {
454 if (fUser == null || fUser.equals("")) {
457 if (fPassword == null || fPassword.equals("")) {
464 * Asks the user to enter a password. Places the results in the supplied string[]. result[0] must contain the category, result[1]
465 * must contain the fPassword. If the user canceled, both values must be zero.
468 * the location to obtain the fPassword for
472 * a message to display to the fUser
474 * whether the fUser can be changed in the dialog
476 * a String array of length two in which to put the result
478 public boolean promptForPassword(final String username, final String message, final boolean userMutable, final String[] result) {
479 if (isUserComplete()) {
481 result[1] = fPassword;
486 Shell shell = Util.findShell();
490 UserValidationDialog dialog = new UserValidationDialog(shell, fUrl, (username == null) ? "" : username, message);//$NON-NLS-1$
491 dialog.setUsernameMutable(userMutable);
493 result[0] = dialog.getUsername();
494 result[1] = dialog.getPassword();
495 if (dialog.getAllowCaching()) {
497 fPassword = result[1];
499 return dialog.getAllowCaching();
505 * @see java.lang.Object#equals(java.lang.Object)
507 public boolean equals(Object obj) {
508 if (obj instanceof Configuration) {
509 if (fName == null || ((Configuration) obj).fName == null) {
512 return fName.equals(((Configuration) obj).fName);