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