]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/examples/org/simantics/databoard/example/StringVariantBindingExample.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / examples / org / simantics / databoard / example / StringVariantBindingExample.java
1 /*******************************************************************************
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     VTT Technical Research Centre of Finland - initial API and implementation
10  *******************************************************************************/
11 package org.simantics.databoard.example;
12
13 import java.awt.geom.Rectangle2D;
14 import java.util.Arrays;
15
16 import org.simantics.databoard.Bindings;
17 import org.simantics.databoard.binding.Binding;
18 import org.simantics.databoard.binding.mutable.MutableVariant;
19
20 public class StringVariantBindingExample {
21
22         public static void main(String[] args) throws Exception {
23
24                 //
25                 // You can escape any string to a string that is filename and URL compatible,
26                 // using StringVariantBinding
27                 //              
28                 String str = "Hello World";
29                 String escapedString = (String) Bindings.STR_VARIANT.create(Bindings.STRING, str);
30                 String unescapedString = (String) Bindings.STR_VARIANT.getContent(escapedString, Bindings.STRING);
31                 System.out.println("Unescaped String: "+unescapedString);
32                 System.out.println("Escaped String: "+escapedString);
33                 System.out.println();
34                 
35                 str = "http://www.simantics.org/path?query=value";
36                 escapedString = (String) Bindings.STR_VARIANT.create(Bindings.STRING, str);
37                 unescapedString = (String) Bindings.STR_VARIANT.getContent(escapedString, Bindings.STRING);
38                 System.out.println("Unescaped String: "+unescapedString);
39                 System.out.println("Escaped String: "+escapedString);
40                 System.out.println();
41                 
42                 // 
43                 // The escaped string is actually a string representation of a variant.
44                 // It contains information of the data type as well.
45                 // Strings are prefixed with "S", Integers with "I", Longs "L",
46                 // Other types are Base64 encoded binary which are not human readable.
47                 //
48                 // 
49                 // This means you can convert almost any value Integer, Long, Array, int[], 
50                 // Map, Set, Bean-like Class, Immutable-like class to a string and back
51                 // to an object.
52                 //
53                 Object value = 5;
54                 escapedString = (String) Bindings.STR_VARIANT.create(Bindings.INTEGER, value);
55                 Object unescapedObject = (Integer) Bindings.STR_VARIANT.getContent(escapedString, Bindings.INTEGER);
56                 System.out.println("Unescaped Object: "+unescapedObject);
57                 System.out.println("Escaped String: "+escapedString);
58                 System.out.println();
59
60                 value = new int[] {1, 2, 3, 4, 5, 6};
61                 escapedString = (String) Bindings.STR_VARIANT.create(Bindings.INTEGER, value);
62                 unescapedObject = (int[]) Bindings.STR_VARIANT.getContent(escapedString, Bindings.INTEGER);             
63                 System.out.println("Unescaped Object: "+Arrays.toString((int[]) unescapedObject));
64                 System.out.println("Escaped String: "+escapedString);
65                 System.out.println();
66
67                 //
68                 // You can change the class to a compatible class with another binding
69                 // int[] -> Integer[]
70                 //
71                 Binding binding1 = Bindings.INT_ARRAY;
72                 Binding binding2 = Bindings.getBinding(Integer[].class);
73                 value = new int[] {1, 2, 3, 4, 5, 6};
74                 escapedString = (String) Bindings.STR_VARIANT.create(binding1, value);
75                 unescapedObject = (Integer[]) Bindings.STR_VARIANT.getContent(escapedString,  binding2);                
76                 System.out.println("Unescaped Object: "+Arrays.toString((Integer[]) unescapedObject));
77                 System.out.println("Escaped String: "+escapedString);
78                 System.out.println();
79
80                 //
81                 // Binding.getBinding creates a binding for many classes by inspecting the class using a Reflection
82                 //
83                 Binding rectangleBinding = Bindings.getBinding(Rectangle2D.Double.class);
84                 value = new Rectangle2D.Double(10, 10, 200, 200);
85                 escapedString = (String) Bindings.STR_VARIANT.create(rectangleBinding, value);
86                 unescapedObject = (Rectangle2D) Bindings.STR_VARIANT.getContent(escapedString, rectangleBinding);               
87                 System.out.println("Unescaped Object: "+unescapedObject);
88                 System.out.println("Escaped String: "+escapedString);
89                 System.out.println();
90
91                 //
92                 // ObjectVariantBinding binds many java classes to java.lang.Object
93                 // You can adapt Objects, which are bound as variants, to strings, also bound as variants 
94                 //
95                 value = "Some object";
96                 String stringVariant = (String) Bindings.adapt(value, Bindings.OBJECT, Bindings.STR_VARIANT);
97                 Object objectVariant = Bindings.adapt(stringVariant, Bindings.STR_VARIANT, Bindings.OBJECT);
98                 MutableVariant variantVariant = (MutableVariant) Bindings.adapt(stringVariant, Bindings.STR_VARIANT, Bindings.MUTABLE_VARIANT);         
99                 System.out.println("Object Variant: "+objectVariant+" ("+objectVariant.getClass().getName()+")");
100                 System.out.println("String Variant: "+stringVariant);
101                 System.out.println("Variant Variant: "+variantVariant);
102                 System.out.println();
103                 
104         }
105         
106 }
107