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[] {
48 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
49 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
50 114,120,53,79,96,109,72,108,70,64,76,67,116,74,68,87,
51 111,52,75,119,49,34,82,81,95,65,112,86,118,110,122,105,
52 41,57,83,43,46,102,40,89,38,103,45,50,42,123,91,35,
53 125,55,54,66,124,126,59,47,92,71,115,78,88,107,106,56,
54 36,121,117,104,101,100,69,73,99,63,94,93,39,37,61,48,
55 58,113,32,90,44,98,60,51,33,97,62,77,84,80,85,223,
56 225,216,187,166,229,189,222,188,141,249,148,200,184,136,248,190,
57 199,170,181,204,138,232,218,183,255,234,220,247,213,203,226,193,
58 174,172,228,252,217,201,131,230,197,211,145,238,161,179,160,212,
59 207,221,254,173,202,146,224,151,140,196,205,130,135,133,143,246,
60 192,159,244,239,185,168,215,144,139,165,180,157,147,186,214,176,
61 227,231,219,169,175,156,206,198,129,164,150,210,154,177,134,127,
62 182,128,158,208,162,132,167,209,149,241,153,251,237,236,171,195,
63 243,233,253,240,194,250,191,155,142,137,245,235,163,242,178,152
65 public Configuration() {
66 this(WikiEditorPlugin.HTTP_QUERY); // default type
69 public Configuration(String type) {
73 public String getId() {
77 public String getName() {
81 public String getURL() {
85 public String getPassword() {
92 * @see org.eclipse.monitor.internal.IConfiguration#getLocalPort()
94 public String getUser() {
100 public String getType() {
107 * @see org.eclipse.monitor.internal.IConfiguration#isRunning()
109 public boolean isActive() {
110 return ConfigurationManager.getInstance().isActive(this);
113 public void delete() {
114 ConfigurationManager.getInstance().removeConfiguration(this);
117 public boolean isWorkingCopy() {
121 public IConfigurationWorkingCopy getWorkingCopy() {
122 return new ConfigurationWorkingCopy(this);
125 protected void setInternal(IConfiguration monitor) {
126 fId = monitor.getId();
127 fName = monitor.getName();
128 fUrl = monitor.getURL();
129 fPassword = monitor.getPassword();
130 fUser = monitor.getUser();
131 fType = monitor.getType();
134 protected void save(IMemento memento) {
135 memento.putString(MEMENTO_ID, fId);
136 memento.putString(MEMENTO_NAME, fName);
137 memento.putString(MEMENTO_TYPE_ID, fType);
138 memento.putString(MEMENTO_USER, fUser);
139 memento.putString(MEMENTO_URL, fUrl);
140 String result = 'A'+scramblePassword(fPassword);
141 memento.putString(MEMENTO_PASSWORD, result);
144 protected void load(IMemento memento) {
145 fId = memento.getString(MEMENTO_ID);
149 fName = memento.getString(MEMENTO_NAME);
153 fType = memento.getString(MEMENTO_TYPE_ID);
157 fUser = memento.getString(MEMENTO_USER);
161 fUrl = memento.getString(MEMENTO_URL);
165 String result = memento.getString(MEMENTO_PASSWORD);
167 if (result == null) {
170 fPassword = scramblePassword(result.substring(1));
177 * @see java.lang.Object#toString()
179 public String toString() {
180 StringBuffer buffer = new StringBuffer();
181 buffer.append(fName);
182 buffer.append(" - ");
183 buffer.append(fUser);
184 buffer.append(" - ");
186 buffer.append(" - ");
187 buffer.append(fType);
188 return buffer.toString();
194 * @see java.lang.Comparable#compareTo(java.lang.Object)
196 public int compareTo(Object o) {
197 if (o instanceof IConfiguration) {
198 return fName.compareTo(((IConfiguration) o).getName());
203 private static String scramblePassword(String password) {
204 int length = password.length();
205 char[] out = new char[length];
206 for (int i = 0; i < length; i++) {
207 char value = password.charAt(i);
208 out[i] = SCRAMBLING_TABLE[value];
210 return new String(out);
213 public boolean isUserComplete() {
214 if (fUser==null || fUser.equals("")) {
217 if (fPassword==null || fPassword.equals("")) {
223 * Asks the User to enter a Password. Places the results in the supplied string[]. result[0] must contain the username, result[1]
224 * must contain the fPassword. If the fUser canceled, both values must be zero.
227 * the location to obtain the fPassword for
231 * a message to display to the fUser
233 * whether the fUser can be changed in the dialog
235 * a String array of length two in which to put the result
237 public boolean promptForPassword(final String username, final String message, final boolean userMutable, final String[] result) {
238 if (isUserComplete()) {
240 result[1] = fPassword;
243 Shell shell = Util.findShell();
247 UserValidationDialog dialog = new UserValidationDialog(shell, fUrl, (username == null) ? "" : username, message);//$NON-NLS-1$
248 dialog.setUsernameMutable(userMutable);
250 result[0] = dialog.getUsername();
251 result[1] = dialog.getPassword();
252 if (dialog.getAllowCaching()) {
254 fPassword = result[1];
256 return dialog.getAllowCaching();