Enable word wrapping with preference key editor.wrap.words (false by default)
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / internal / Configuration.java
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
7  �*
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpeclipse.wiki.internal;
12
13 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
14 import net.sourceforge.phpeclipse.wiki.preferences.UserValidationDialog;
15 import net.sourceforge.phpeclipse.wiki.preferences.Util;
16
17 import org.eclipse.swt.widgets.Shell;
18
19 /**
20  *  
21  */
22 public class Configuration implements IConfiguration, Comparable {
23   private static final String MEMENTO_ID = "id";
24
25   private static final String MEMENTO_NAME = "name";
26
27   private static final String MEMENTO_USER = "user";
28
29   private static final String MEMENTO_URL = "url";
30
31   private static final String MEMENTO_PASSWORD = "password";
32
33   private static final String MEMENTO_TYPE_ID = "type-id";
34
35   protected String fId = "";
36
37   protected String fName = "";
38
39   protected String fUrl = "";
40
41   protected String fPassword = "";
42
43   protected String fUser = "";
44
45   protected String fType = "";
46
47   private static final char[] SCRAMBLING_TABLE = new char[] {
48       0,
49       1,
50       2,
51       3,
52       4,
53       5,
54       6,
55       7,
56       8,
57       9,
58       10,
59       11,
60       12,
61       13,
62       14,
63       15,
64       16,
65       17,
66       18,
67       19,
68       20,
69       21,
70       22,
71       23,
72       24,
73       25,
74       26,
75       27,
76       28,
77       29,
78       30,
79       31,
80       114,
81       120,
82       53,
83       79,
84       96,
85       109,
86       72,
87       108,
88       70,
89       64,
90       76,
91       67,
92       116,
93       74,
94       68,
95       87,
96       111,
97       52,
98       75,
99       119,
100       49,
101       34,
102       82,
103       81,
104       95,
105       65,
106       112,
107       86,
108       118,
109       110,
110       122,
111       105,
112       41,
113       57,
114       83,
115       43,
116       46,
117       102,
118       40,
119       89,
120       38,
121       103,
122       45,
123       50,
124       42,
125       123,
126       91,
127       35,
128       125,
129       55,
130       54,
131       66,
132       124,
133       126,
134       59,
135       47,
136       92,
137       71,
138       115,
139       78,
140       88,
141       107,
142       106,
143       56,
144       36,
145       121,
146       117,
147       104,
148       101,
149       100,
150       69,
151       73,
152       99,
153       63,
154       94,
155       93,
156       39,
157       37,
158       61,
159       48,
160       58,
161       113,
162       32,
163       90,
164       44,
165       98,
166       60,
167       51,
168       33,
169       97,
170       62,
171       77,
172       84,
173       80,
174       85,
175       223,
176       225,
177       216,
178       187,
179       166,
180       229,
181       189,
182       222,
183       188,
184       141,
185       249,
186       148,
187       200,
188       184,
189       136,
190       248,
191       190,
192       199,
193       170,
194       181,
195       204,
196       138,
197       232,
198       218,
199       183,
200       255,
201       234,
202       220,
203       247,
204       213,
205       203,
206       226,
207       193,
208       174,
209       172,
210       228,
211       252,
212       217,
213       201,
214       131,
215       230,
216       197,
217       211,
218       145,
219       238,
220       161,
221       179,
222       160,
223       212,
224       207,
225       221,
226       254,
227       173,
228       202,
229       146,
230       224,
231       151,
232       140,
233       196,
234       205,
235       130,
236       135,
237       133,
238       143,
239       246,
240       192,
241       159,
242       244,
243       239,
244       185,
245       168,
246       215,
247       144,
248       139,
249       165,
250       180,
251       157,
252       147,
253       186,
254       214,
255       176,
256       227,
257       231,
258       219,
259       169,
260       175,
261       156,
262       206,
263       198,
264       129,
265       164,
266       150,
267       210,
268       154,
269       177,
270       134,
271       127,
272       182,
273       128,
274       158,
275       208,
276       162,
277       132,
278       167,
279       209,
280       149,
281       241,
282       153,
283       251,
284       237,
285       236,
286       171,
287       195,
288       243,
289       233,
290       253,
291       240,
292       194,
293       250,
294       191,
295       155,
296       142,
297       137,
298       245,
299       235,
300       163,
301       242,
302       178,
303       152 };
304
305   public Configuration() {
306     this(WikiEditorPlugin.HTTP_QUERY); // default type
307   }
308
309   public Configuration(String type) {
310     this.fType = type;
311   }
312
313   public String getId() {
314     return fId;
315   }
316
317   public String getName() {
318     return fName;
319   }
320
321   public String getURL() {
322     return fUrl;
323   }
324
325   public String getPassword() {
326     return fPassword;
327   }
328
329   /*
330    * (non-Javadoc)
331    * 
332    * @see org.eclipse.monitor.internal.IConfiguration#getLocalPort()
333    */
334   public String getUser() {
335     return fUser;
336   }
337
338   /**
339    */
340   public String getType() {
341     return fType;
342   }
343
344   /*
345    * (non-Javadoc)
346    * 
347    * @see org.eclipse.monitor.internal.IConfiguration#isRunning()
348    */
349   public boolean isActive() {
350     return ConfigurationManager.getInstance().isActive(this);
351   }
352
353   public void delete() {
354     ConfigurationManager.getInstance().removeConfiguration(this);
355   }
356
357   public boolean isWorkingCopy() {
358     return false;
359   }
360
361   public IConfigurationWorkingCopy getWorkingCopy() {
362     return new ConfigurationWorkingCopy(this);
363   }
364
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();
372   }
373
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);
382   }
383
384   protected void load(IMemento memento) {
385     fId = memento.getString(MEMENTO_ID);
386     if (fId == null) {
387       fId = "";
388     }
389     fName = memento.getString(MEMENTO_NAME);
390     if (fName == null) {
391       fName = "";
392     }
393     fType = memento.getString(MEMENTO_TYPE_ID);
394     if (fType == null) {
395       fType = "";
396     }
397     fUser = memento.getString(MEMENTO_USER);
398     if (fUser == null) {
399       fUser = "";
400     }
401     fUrl = memento.getString(MEMENTO_URL);
402     if (fUrl == null) {
403       fUrl = "";
404     }
405     String result = memento.getString(MEMENTO_PASSWORD);
406
407     if (result == null) {
408       fPassword = "";
409     } else {
410       fPassword = scramblePassword(result.substring(1));
411     }
412   }
413
414   /*
415    * (non-Javadoc)
416    * 
417    * @see java.lang.Object#toString()
418    */
419   public String toString() {
420     StringBuffer buffer = new StringBuffer();
421     buffer.append(fName);
422     buffer.append(" - ");
423     buffer.append(fUser);
424     buffer.append(" - ");
425     buffer.append(fUrl);
426     buffer.append(" - ");
427     buffer.append(fType);
428     return buffer.toString();
429   }
430
431   /*
432    * (non-Javadoc)
433    * 
434    * @see java.lang.Comparable#compareTo(java.lang.Object)
435    */
436   public int compareTo(Object o) {
437     if (o instanceof IConfiguration) {
438       return fName.compareTo(((IConfiguration) o).getName());
439     }
440     return 1;
441   }
442
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];
449     }
450     return new String(out);
451   }
452
453   public boolean isUserComplete() {
454     if (fUser == null || fUser.equals("")) {
455       return false;
456     }
457     if (fPassword == null || fPassword.equals("")) {
458       return false;
459     }
460     return true;
461   }
462
463   /**
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.
466    * 
467    * @param location
468    *          the location to obtain the fPassword for
469    * @param category
470    *          the category
471    * @param message
472    *          a message to display to the fUser
473    * @param userMutable
474    *          whether the fUser can be changed in the dialog
475    * @param result
476    *          a String array of length two in which to put the result
477    */
478   public boolean promptForPassword(final String username, final String message, final boolean userMutable, final String[] result) {
479     if (isUserComplete()) {
480       result[0] = fUser;
481       result[1] = fPassword;
482       return true;
483     }
484     result[0] = null;
485     result[1] = null;
486     Shell shell = Util.findShell();
487     if (shell == null) {
488       return false;
489     }
490     UserValidationDialog dialog = new UserValidationDialog(shell, fUrl, (username == null) ? "" : username, message);//$NON-NLS-1$
491     dialog.setUsernameMutable(userMutable);
492     dialog.open();
493     result[0] = dialog.getUsername();
494     result[1] = dialog.getPassword();
495     if (dialog.getAllowCaching()) {
496       fUser = result[0];
497       fPassword = result[1];
498     }
499     return dialog.getAllowCaching();
500   }
501
502   /*
503    * (non-Javadoc)
504    * 
505    * @see java.lang.Object#equals(java.lang.Object)
506    */
507   public boolean equals(Object obj) {
508     if (obj instanceof Configuration) {
509       if (fName == null || ((Configuration) obj).fName == null) {
510         return false;
511       }
512       return fName.equals(((Configuration) obj).fName);
513     }
514     return false;
515   }
516 }