p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor.prototype2 / src / eu / etaxonomy / taxeditor / prototype2 / view / nameviewersupport / NamePartitioner__.java
1 package eu.etaxonomy.taxeditor.prototype2.view.nameviewersupport;
2
3 import java.beans.PropertyDescriptor;
4 import java.lang.reflect.Field;
5
6 import org.eclipse.core.databinding.beans.BeansObservables;
7 import org.eclipse.core.databinding.observable.value.IObservableValue;
8 import org.eclipse.core.runtime.Assert;
9 import org.eclipse.jface.text.BadLocationException;
10 import org.eclipse.jface.text.BadPositionCategoryException;
11 import org.eclipse.jface.text.DocumentEvent;
12 import org.eclipse.jface.text.IDocument;
13 import org.eclipse.jface.text.IDocumentPartitioner;
14 import org.eclipse.jface.text.ITypedRegion;
15 import org.eclipse.jface.text.TypedPosition;
16 import org.eclipse.jface.text.rules.FastPartitioner;
17 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
18
19
20 public class NamePartitioner__ implements IDocumentPartitioner {
21
22 private static final String NAME_CONTENT_TYPES_CATEGORY = "__name_content_types_category";
23 /**
24 * The position category this partitioner uses to store the document's partitioning information.
25 */
26 private final String fPositionCategory;
27 private NameDocument fDocument;
28 private String[] contentTypes = {"genus","specificEpithet"};
29
30 public NamePartitioner__() {
31 fPositionCategory = NAME_CONTENT_TYPES_CATEGORY + hashCode();
32 }
33
34 public ITypedRegion[] computePartitioning(int offset, int length) {
35 // TODO Auto-generated method stub
36 return null;
37 }
38
39 public void connect(IDocument document) {
40 Assert.isTrue(document instanceof NameDocument,
41 "NamePartitioner__ requires a NameDocument");
42 this.fDocument = (NameDocument) document;
43 this.fDocument.addPositionCategory(fPositionCategory);
44
45 TaxonName tn = fDocument.getTaxonName();
46
47 if (tn == null) {
48 // find matching pattern to init TaxonName
49
50 document.get();
51 } else {
52 // construct partitions from TaxonName
53
54 try {
55 TypedPosition[] positions = new TypedPosition[contentTypes.length];
56 int offset = 0;
57 String text = "";
58
59 for (int i = 0; i < contentTypes.length; i++) {
60 String field = contentTypes[i];
61
62 IObservableValue observeValue = BeansObservables.observeValue(tn, field);
63
64 String value = (String) observeValue.getValue();
65 int length = value.length();
66
67 TypedPosition p = new TypedPosition(offset, length, field);
68 positions[i] = p;
69
70 offset += length + 1;
71 text += value + " ";
72 }
73 fDocument.set(text);
74 for (int i = 0; i < positions.length; i++)
75 fDocument.addPosition(fPositionCategory, positions[i]);
76 // } catch (BadLocationException e) {
77 // // TODO Auto-generated catch block
78 // e.printStackTrace();
79 // } catch (SecurityException e) {
80 // // TODO Auto-generated catch block
81 // e.printStackTrace();
82 // } catch (NoSuchFieldException e) {
83 // // TODO Auto-generated catch block
84 // e.printStackTrace();
85 // } catch (IllegalArgumentException e) {
86 // // TODO Auto-generated catch block
87 // e.printStackTrace();
88 // } catch (IllegalAccessException e) {
89 // // TODO Auto-generated catch block
90 // e.printStackTrace();
91 // } catch (BadPositionCategoryException e) {
92 // // TODO Auto-generated catch block
93 // e.printStackTrace();
94 } catch (Exception e) {
95 e.printStackTrace();
96 System.out.println(e.getMessage());
97 }
98 }
99 }
100
101 public void disconnect() {
102 // TODO Auto-generated method stub
103
104 }
105
106 public void documentAboutToBeChanged(DocumentEvent event) {
107 System.out.println("You are here: " + getContentType(event.fOffset));
108 // System.out.println("You are here: " + event.fOffset);
109 }
110
111 public boolean documentChanged(DocumentEvent event) {
112 // TODO Auto-generated method stub
113 return false;
114 }
115
116 public String getContentType(int offset) {
117 ITypedRegion region = null;
118 try {
119 region = fDocument.getPartition(offset);
120 } catch (BadLocationException e) {
121 // TODO Auto-generated catch block
122 e.printStackTrace();
123 }
124 return region.getType();
125 }
126
127 public String[] getLegalContentTypes() {
128 return contentTypes;
129 }
130
131 public ITypedRegion getPartition(int offset) {
132 ITypedRegion region = null;
133 try {
134 region = fDocument.getPartition(offset);
135 } catch (BadLocationException e) {
136 // TODO Auto-generated catch block
137 e.printStackTrace();
138 }
139 return region;
140 }
141 }