Revision 309

tmp/org.txm.core/src/java/org/txm/core/preferences/TXMPreferences.java (revision 309)
1 1
package org.txm.core.preferences;
2 2

  
3
import java.lang.reflect.Field;
3 4
import java.util.ArrayList;
4 5
import java.util.Arrays;
5 6

  
......
9 10
import org.eclipse.core.runtime.preferences.IScopeContext;
10 11
import org.eclipse.core.runtime.preferences.InstanceScope;
11 12
import org.osgi.service.prefs.BackingStoreException;
13
import org.txm.core.results.ITXMResult;
12 14

  
13 15

  
14 16

  
......
20 22
 * - command default value (DefaultScope)
21 23
 * - alternative specified global value
22 24
 * - alternative specified default value
23
 * TODO: methods that puts a value in the local node does not check if the value is the the default value of the parent, it should be removed from the local node if it's the default value
25
 * TODO: methods that puts a value in the local node does not check if the value is the default value of the parent, it should be removed from the local node if it's the default value
24 26
 * of command scope or alternative scope so the preference will not be saved in the local scope .prefs file if its a default value. 
25 27
 * TODO: when we'll use IProject for managing Corporas, the local node should be stored in the ProjectScope instead of in the InstanceScope.
26 28
 * TODO: need to determine if we'll use this class to persistence of TXM results. At this moment, we persist result by calling flush() that creates a file named by the result node qualifier,
......
51 53
	 * @param key
52 54
	 * @param value
53 55
	 */
54
	public static void putLocalBoolean(Object result, String key, boolean value)	{
56
	public static void putLocalBoolean(ITXMResult result, String key, boolean value)	{
55 57
		scope.getNode(getId(result)).putBoolean(key, value);
56 58
	}
57 59
	
......
68 70
	 * @param key
69 71
	 * @return
70 72
	 */
71
	public static boolean getBoolean(String nodeQualifier, Object result, String key)	{
73
	public static boolean getBoolean(String nodeQualifier, ITXMResult result, String key)	{
72 74

  
73 75
		boolean defaultValue = true;
74
		boolean globalValue = defaultValue;
75
		boolean keyExists = (result != null && resultScopeKeyExists(result, key));
76

  
77
		// get the result scope preference
78
		if(keyExists)	{
79
			nodeQualifier = getId(result);
80
		}
81
		// look in the global alternative preferences nodes
82
		else if(!keyExists(nodeQualifier, key))	{
83
			for(int i = 0; i < alternativeNodesQualifiers.size(); i++) {
84
				if(keyExists(alternativeNodesQualifiers.get(i), key))	{
85
					nodeQualifier = alternativeNodesQualifiers.get(i);
86
					break;
87
				}
88
			}
89
		}
76
		boolean value = defaultValue;
77
		
78
		nodeQualifier = findNodeQualifier(nodeQualifier, result, key);
79
		
90 80
		defaultValue = DefaultScope.INSTANCE.getNode(nodeQualifier).getBoolean(key, defaultValue);
91
		globalValue = scope.getNode(nodeQualifier).getBoolean(key, defaultValue);
92
		return globalValue;
81
		value = scope.getNode(nodeQualifier).getBoolean(key, defaultValue);
82
		
83
		return value;
93 84
	}
94 85
	
95
	
96 86
	/**
97 87
	 * 
98 88
	 * @param nodeQualifier
......
109 99
	 * @param key
110 100
	 * @return
111 101
	 */
112
	public static boolean getBoolean(Object result, String key)	{
102
	public static boolean getBoolean(ITXMResult result, String key)	{
113 103
		return getBoolean(null, result, key);
114 104
	}
115 105
	
......
119 109
	 * @param key
120 110
	 * @param value
121 111
	 */
122
	public static void putLocalString(Object result, String key, String value)	{
112
	public static void putLocalString(ITXMResult result, String key, String value)	{
123 113
		scope.getNode(getId(result)).put(key, value);
124 114
	}
125 115
	
......
135 125
	 * @param key
136 126
	 * @return
137 127
	 */
138
	public static String getString(String nodeQualifier, Object result, String key)	{
128
	public static String getString(String nodeQualifier, ITXMResult result, String key)	{
139 129

  
140 130
		String defaultValue = "";
141
		String globalValue = defaultValue;
142
		boolean keyExists = (result != null && resultScopeKeyExists(result, key));
143

  
144
		// get the result scope preference
145
		if(keyExists)	{
146
			nodeQualifier = getId(result);
147
		}
148
		// look in the global alternative preferences nodes
149
		else if(!keyExists(nodeQualifier, key))	{
150
			for(int i = 0; i < alternativeNodesQualifiers.size(); i++) {
151
				if(keyExists(alternativeNodesQualifiers.get(i), key))	{
152
					nodeQualifier = alternativeNodesQualifiers.get(i);
153
					break;
154
				}
155
			}
156
		}
131
		String value = defaultValue;
132
		
133
		nodeQualifier = findNodeQualifier(nodeQualifier, result, key);
134
		
157 135
		defaultValue = DefaultScope.INSTANCE.getNode(nodeQualifier).get(key, defaultValue);
158
		globalValue = scope.getNode(nodeQualifier).get(key, defaultValue);
159
		return globalValue;
136
		value = scope.getNode(nodeQualifier).get(key, defaultValue);
137
		
138
		return value;
160 139
	}
161 140
	
162 141
	/**
......
176 155
	 * @param key
177 156
	 * @return
178 157
	 */
179
	public static String getString(Object result, String key)	{
158
	public static String getString(ITXMResult result, String key)	{
180 159
		return getString(null, result, key);
181 160
	}
182 161

  
......
186 165
	 * @param key
187 166
	 * @param value
188 167
	 */
189
	public static void putLocalInt(Object result, String key, int value)	{
168
	public static void putLocalInt(ITXMResult result, String key, int value)	{
190 169
		scope.getNode(getId(result)).putInt(key, value);
191 170
	}
192 171
	
......
201 180
	 * @param key
202 181
	 * @return
203 182
	 */
204
	public static int getInt(String nodeQualifier, Object result, String key)	{
183
	public static int getInt(String nodeQualifier, ITXMResult result, String key)	{
205 184

  
206 185
		int defaultValue = -1;
207
		int globalValue = defaultValue;
208
		boolean keyExists = (result != null && resultScopeKeyExists(result, key));
209

  
210
		// get the result scope preference
211
		if(keyExists)	{
212
			nodeQualifier = getId(result);
213
		}
214
		// look in the global alternative preferences nodes
215
		else if(!keyExists(nodeQualifier, key))	{
216
			for(int i = 0; i < alternativeNodesQualifiers.size(); i++) {
217
				if(keyExists(alternativeNodesQualifiers.get(i), key))	{
218
					nodeQualifier = alternativeNodesQualifiers.get(i);
219
					break;
220
				}
221
			}
222
		}
186
		int value = defaultValue;
187
		
188
		nodeQualifier = findNodeQualifier(nodeQualifier, result, key);
189
		
223 190
		defaultValue = DefaultScope.INSTANCE.getNode(nodeQualifier).getInt(key, defaultValue);
224
		globalValue = scope.getNode(nodeQualifier).getInt(key, defaultValue);
225
		return globalValue;
191
		value = scope.getNode(nodeQualifier).getInt(key, defaultValue);
192
		
193
		return value;
226 194
	}
227 195

  
228 196
	
......
243 211
	 * @param key
244 212
	 * @return
245 213
	 */
246
	public static int getInt(Object result, String key)	{
214
	public static int getInt(ITXMResult result, String key)	{
247 215
		return getInt(null, result, key);
248 216
	}
249 217

  
......
254 222
	 * @param key
255 223
	 * @param value
256 224
	 */
257
	public static void putLocalDouble(Object result, String key, double value)	{
225
	public static void putLocalDouble(ITXMResult result, String key, double value)	{
258 226
		scope.getNode(getId(result)).putDouble(key, value);
259 227
	}
260 228
	
......
269 237
	 * @param key
270 238
	 * @return
271 239
	 */
272
	public static double getDouble(String nodeQualifier, Object result, String key)	{
240
	public static double getDouble(String nodeQualifier, ITXMResult result, String key)	{
273 241

  
274 242
		double defaultValue = -1;
275
		double globalValue = defaultValue;
276
		boolean keyExists = (result != null && resultScopeKeyExists(result, key));
243
		double value = defaultValue;
277 244

  
278
		// get the result scope preference
279
		if(keyExists)	{
280
			nodeQualifier = getId(result);
281
		}
282
		// look in the global alternative preferences nodes
283
		else if(!keyExists(nodeQualifier, key))	{
284
			for(int i = 0; i < alternativeNodesQualifiers.size(); i++) {
285
				if(keyExists(alternativeNodesQualifiers.get(i), key))	{
286
					nodeQualifier = alternativeNodesQualifiers.get(i);
287
					break;
288
				}
289
			}
290
		}
245
		nodeQualifier = findNodeQualifier(nodeQualifier, result, key);
246
		
291 247
		defaultValue = DefaultScope.INSTANCE.getNode(nodeQualifier).getDouble(key, defaultValue);
292
		globalValue = scope.getNode(nodeQualifier).getDouble(key, defaultValue);
293
		return globalValue;
248
		value = scope.getNode(nodeQualifier).getDouble(key, defaultValue);
249
		
250
		return value;
294 251
	}
295 252
	
296 253
	
......
311 268
	 * @param key
312 269
	 * @return
313 270
	 */
314
	public static double getDouble(Object result, String key)	{
271
	public static double getDouble(ITXMResult result, String key)	{
315 272
		return getDouble(null, result, key);
316 273
	}
317 274

  
......
322 279
	 * @param key
323 280
	 * @param value
324 281
	 */
325
	public static void putLocalFloat(Object result, String key, float value)	{
282
	public static void putLocalFloat(ITXMResult result, String key, float value)	{
326 283
		scope.getNode(getId(result)).putFloat(key, value);
327 284
	}
328 285
	
......
337 294
	 * @param key
338 295
	 * @return
339 296
	 */
340
	public static float getFloat(String nodeQualifier, Object result, String key)	{
297
	public static float getFloat(String nodeQualifier, ITXMResult result, String key)	{
341 298

  
342 299
		float defaultValue = -1;
343
		float globalValue = defaultValue;
344
		boolean keyExists = (result != null && resultScopeKeyExists(result, key));
300
		float value = defaultValue;
345 301

  
346
		// get the result scope preference
347
		if(keyExists)	{
348
			nodeQualifier = getId(result);
349
		}
350
		// look in the global alternative preferences nodes
351
		else if(!keyExists(nodeQualifier, key))	{
352
			for(int i = 0; i < alternativeNodesQualifiers.size(); i++) {
353
				if(keyExists(alternativeNodesQualifiers.get(i), key))	{
354
					nodeQualifier = alternativeNodesQualifiers.get(i);
355
					break;
356
				}
357
			}
358
		}
302
		nodeQualifier = findNodeQualifier(nodeQualifier, result, key);
303
		
359 304
		defaultValue = DefaultScope.INSTANCE.getNode(nodeQualifier).getFloat(key, defaultValue);
360
		globalValue = scope.getNode(nodeQualifier).getFloat(key, defaultValue);
361
		return globalValue;
305
		value = scope.getNode(nodeQualifier).getFloat(key, defaultValue);
306
		return value;
362 307
	}
363 308

  
364 309
	
......
379 324
	 * @param key
380 325
	 * @return
381 326
	 */
382
	public static float getFloat(Object result, String key)	{
327
	public static float getFloat(ITXMResult result, String key)	{
383 328
		return getFloat(null, result, key);
384 329
	}
385 330

  
......
389 334
	 * @param result
390 335
	 * @return <code>true</code> if the node exists, otherwise <code>false</code>
391 336
	 */
392
	public static boolean resultScopeNodeExists(Object result)	{
337
	public static boolean resultScopeNodeExists(ITXMResult result)	{
393 338
		if(result == null)	{
394 339
			return false;
395 340
		}
......
402 347
	 * @param key
403 348
	 * @return <code>true</code> if the node and the key exist, otherwise <code>false</code>
404 349
	 */
405
	public static boolean resultScopeKeyExists(Object result, String key)	{
350
	public static boolean resultScopeKeyExists(ITXMResult result, String key)	{
406 351
		return keyExists(getId(result), key);
407 352
	}
408 353

  
......
475 420
	 * @param nodeQualifier
476 421
	 * @param result
477 422
	 */
478
	public static void dump(String nodeQualifier, Object result)	{
423
	public static void dump(String nodeQualifier, ITXMResult result)	{
479 424
		
480 425
		// result scope node
481 426
		if(result != null)	{
......
508 453
	 * Save the local preferences to file.
509 454
	 * @param result
510 455
	 */
511
	public static void flush(Object result)	{
456
	public static void flush(ITXMResult result)	{
512 457
		try {
513 458
			System.err.println("TXMPreferences.flush(): Local preferences for object " + getId(result) + " saved to file.");
514 459
			// FIXME: useless?
......
525 470
	 * Gets a unique Id for the object.
526 471
	 * @param object
527 472
	 */
528
	public static String getId(Object object)	{
473
	public static String getId(ITXMResult object)	{
529 474
		return object.getClass().getName() + '@' + Integer.toHexString(object.hashCode());
530 475
	}
531 476

  
477
	/**
478
	 * Internal method to find the node qualifier of a result otherwise look in instance, default scopes and in alternatives nodes qualifiers.
479
	 * @param result
480
	 * @param key
481
	 */
482
	public static String findNodeQualifier(String nodeQualifier, ITXMResult result, String key)	{
483
		
484
		boolean keyExists = (result != null && resultScopeKeyExists(result, key));
485

  
486
		// get the result scope preference
487
		if(keyExists)	{
488
			nodeQualifier = getId(result);
489
		}
490
		// look in the global alternative preferences nodes
491
		else if(!keyExists(nodeQualifier, key))	{
492
			for(int i = 0; i < alternativeNodesQualifiers.size(); i++) {
493
				if(keyExists(alternativeNodesQualifiers.get(i), key))	{
494
					nodeQualifier = alternativeNodesQualifiers.get(i);
495
					break;
496
				}
497
			}
498
		}
499
		
500
		return nodeQualifier;
501
	}
532 502
	
533 503
	
504
	public static void put(String nodeQualifier, String key, String value) {
505
		scope.getNode(nodeQualifier).put(key, value);
506
	}
507

  
508
	public static void putInt(String nodeQualifier, String key, int value) {
509
		scope.getNode(nodeQualifier).putInt(key, value);
510
	}
511

  
512
	public static void putFloat(String nodeQualifier, String key, float value) {
513
		scope.getNode(nodeQualifier).putFloat(key, value);
514
	}
515

  
516
	public static void putDouble(String nodeQualifier, String key, double value) {
517
		scope.getNode(nodeQualifier).putDouble(key, value);
518
	}
519

  
520
	public static void putByteArray(String nodeQualifier, String key, byte[] value) {
521
		scope.getNode(nodeQualifier).putByteArray(key, value);
522
	}
523

  
524
	public static void putBoolean(String nodeQualifier, String key, boolean value) {
525
		scope.getNode(nodeQualifier).putBoolean(key, value);
526
	}
527

  
528
	public static void putLong(String nodeQualifier, String key, long value) {
529
		scope.getNode(nodeQualifier).putLong(key, value);
530
	}
531

  
532

  
533
	
534 534
	//FIXME: this code is dedicated to dynamically retrieve the static field PREFERENCES_NODE of the runtime subclass and use it super class to avoid to pass it to all methods.
535 535
	// eg. to use ProgressionPreferences.getBoolean("test") instead of TXMPreferences.getBoolean(ProgressionPreferences.PREFERENCES_NODE, "test")
536 536
	// but it doesn"t work, need to continue the tests...
......
555 555
//	 * Convenience method to avoid to pass it to all methods. 
556 556
//	 * @return
557 557
//	 */
558
//	private static String getNodeQualifier()	{
558
//	public static String getNodeQualifier()	{
559 559
//        String runtimeSubClassNodeQualifier  = null;
560 560
//        try {
561
//        	
562
//        	//System.err.println("TXMPreferences.getNodeQualifier() class name using thread stack = " + Thread.currentThread().getStackTrace()[1].getClassName());
563
//        	System.err.println("TXMPreferences.getNodeQualifier() class name using encolsing class = " + new Object() { }.getClass().getEnclosingClass());
564
//        	
565
//        	
561 566
//        	Class c = new CurrentClassGetter().getCurrentClass();
562 567
//        	System.out.println("TXMPreferences.getNodeQualifier(): class = " + c);
563 568
//            Field f = c.getDeclaredField("PREFERENCES_NODE");
......
582 587
//            // TODO Auto-generated catch block
583 588
//            e.printStackTrace();
584 589
//        }
590
//        
591
//		//System.out.println("TXMPreferences.getNodeQualifier() runtime class: " + );
592
////    	// FIXME: new tests with bundles context class loading		
593
////		HashSet<ClassLoader> loaders = new HashSet<ClassLoader>();
594
////		BundleContext bundleContext = InternalPlatform.getDefault().getBundleContext();
595
////		
596
////		for (Bundle b : bundleContext.getBundles()) {
597
////			BundleWiring bundleWiring = b.adapt(BundleWiring.class);
598
////			loaders.add(bundleWiring.getClassLoader());
599
////			
600
////			//FIXME: debug
601
////			System.out.println("GSERunner.createGSERunner(): class loader added: " + bundleWiring.getClassLoader());
602
////		}
603
////        
604
//        
585 605
//        return runtimeSubClassNodeQualifier;
586 606
//	}
587 607
//	
588 608
//	
589
	// FIXME: new tests with bundles context class loading
590
//	private static String getNodeQualifier()	{
591
//
592
//		//System.out.println("TXMPreferences.getNodeQualifier() runtime class: " + );
593
//		
594
//		HashSet<ClassLoader> loaders = new HashSet<ClassLoader>();
595
//		BundleContext bundleContext = InternalPlatform.getDefault().getBundleContext();
596
//		
597
//		for (Bundle b : bundleContext.getBundles()) {
598
//			BundleWiring bundleWiring = b.adapt(BundleWiring.class);
599
//			loaders.add(bundleWiring.getClassLoader());
600
//			
601
//			//FIXME: debug
602
//			System.out.println("GSERunner.createGSERunner(): class loader added: " + bundleWiring.getClassLoader());
603
//		}
604
//		
605
//		
606
//	}
607
	
608
	
609
//	/**
610
//	 * 
611
//	 * @param key
612
//	 * @return
613
//	 */
614 609
//	public static String getString(String key)	{
610
////		Class c = new CurrentClassGetter().getCurrentClass();
611
////		System.out.println("TXMPreferences.getString(): class = " + c);
615 612
//		return getString(getNodeQualifier(), key);
616 613
//	}
617
//
618
//
619
//	/**
620
//	 * 
621
//	 * @param key
622
//	 * @return
623
//	 */
624
//	public static boolean getBoolean(String key)	{
625
//    	Class c = new CurrentClassGetter().getCurrentClass();
626
//    	System.out.println("TXMPreferences.getBoolean(): class = " + c);
627
//		return getBoolean(getNodeQualifier(), key);
628
//	}
629 614
//	
630
//	
631
//	/**
632
//	 * 
633
//	 * @param key
634
//	 * @return
635
//	 */
636
//	public static int getInt(String key)	{
637
//		return getInt(getNodeQualifier(), key);
638
//	}
639
//	
640
//	/**
641
//	 * 
642
//	 * @param key
643
//	 * @return
644
//	 */
645
//	public static double getDouble(String key)	{
646
//		return getDouble(getNodeQualifier(), key);
647
//	}
648
//	
649
//	
650
//	/**
651
//	 * 
652
//	 * @param key
653
//	 * @return
654
//	 */
655
//	public static float getFloat(String key)	{
656
//		return getFloat(getNodeQualifier(), key);
657
//	}
615
////	/**
616
////	 * 
617
////	 * @param key
618
////	 * @return
619
////	 */
620
////	public static String getString(String key)	{
621
////		return getString(getNodeQualifier(), key);
622
////	}
623
////
658 624
	//end of fixme
659 625
	
660 626
}

Also available in: Unified diff