]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document/src/org/simantics/document/function/WikiDocumentNodeImpl.java
8987298289c882e7bec74f2c257678fe13dfe039
[simantics/platform.git] / bundles / org.simantics.document / src / org / simantics / document / function / WikiDocumentNodeImpl.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.document.function;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Hashtable;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.simantics.document.WikiDocumentNode;
21 import org.simantics.scenegraph.INode;
22 import org.simantics.scenegraph.ParentNode;
23 import org.simantics.scenegraph.ScenegraphUtils;
24 import org.simantics.scl.runtime.function.Function1;
25 import org.simantics.scl.runtime.function.Function2;
26
27 abstract public class WikiDocumentNodeImpl extends ParentNode<WikiDocumentNode> implements WikiDocumentNode {
28
29         public Boolean printInPDF = false;
30         public String editText = null;
31         
32         private static final long serialVersionUID = 3394059912639648935L;
33
34         class M implements Map<String, WikiDocumentNode> {
35                 
36                 ArrayList<WikiDocumentNode> list = new ArrayList<WikiDocumentNode>();
37                 Hashtable<String, WikiDocumentNode> table = new Hashtable<String, WikiDocumentNode>();
38                 
39                 @Override
40                 public void clear() {
41                         table.clear();
42                         list.clear();
43                 }
44                 @Override
45                 public boolean containsKey(Object arg0) {
46                         return table.containsKey(arg0);
47                 }
48                 @Override
49                 public boolean containsValue(Object arg0) {
50                         return table.containsValue(arg0);
51                 }
52                 @Override
53                 public Set<java.util.Map.Entry<String, WikiDocumentNode>> entrySet() {
54                         return table.entrySet();
55                 }
56                 @Override
57                 public WikiDocumentNode get(Object arg0) {
58                         return table.get(arg0);
59                 }
60                 @Override
61                 public boolean isEmpty() {
62                         return table.isEmpty();
63                 }
64                 @Override
65                 public Set<String> keySet() {
66                         return table.keySet();
67                 }
68                 @Override
69                 public WikiDocumentNode put(String arg0, WikiDocumentNode arg1) {
70                         WikiDocumentNode exist = table.put(arg0, arg1);
71                         if(exist != null) list.remove(exist);
72                         list.add(arg1);
73                         return exist;
74                 }
75                 @Override
76                 public void putAll(Map<? extends String, ? extends WikiDocumentNode> arg0) {
77                         for(Map.Entry<? extends String, ? extends WikiDocumentNode> entry : arg0.entrySet())
78                                 put(entry.getKey(), entry.getValue());
79                 }
80                 @Override
81                 public WikiDocumentNode remove(Object arg0) {
82                         WikiDocumentNode node = table.remove(arg0);
83                         if(node != null) list.remove(node);
84                         return node;
85                 }
86                 @Override
87                 public int size() {
88                         return table.size();
89                 }
90                 @Override
91                 public Collection<WikiDocumentNode> values() {
92                         return list;
93                 }
94                 
95         }
96         
97         @Override
98         final public void asyncRemoveNode(INode node) {
99         throw new Error();
100         }
101         
102         @Override
103         protected Map<String, WikiDocumentNode> createChildMap() {
104                 return new M();
105         }
106         
107         @Override
108         public Function1<Object, Boolean> getPropertyFunction(String propertyName) {
109                 return ScenegraphUtils.getMethodPropertyFunction(null, this, propertyName);
110         }
111         
112         @Override
113         public <T> T getProperty(String propertyName) {
114                 return null;
115         }
116         
117         @Override
118         public void setPropertyCallback(Function2<String, Object, Boolean> callback) {
119         }
120
121         protected void createChildren(StringBuilder b, boolean isPDF) {
122                 for(WikiDocumentNode node : children.values()) node.create(b, isPDF);
123         }
124         
125         public void synchronizePrintInPDF(Boolean value) {
126                 this.printInPDF = value;
127         }
128         
129         protected String getName() {
130                 for(String id : parent.getNodeIds()) {
131                         if(parent.getNode(id) == this) return id;
132                 }
133                 return "err";
134         }
135         
136         public String getPath() {
137                 if(parent instanceof WikiDocumentNodeImpl) {
138                         // Do not include root
139                         if(parent.getParent() == null) return "";
140                         else return ((WikiDocumentNodeImpl)parent).getPath() + "/" + getName(); 
141                 } else {
142                         return "";
143                 }
144         }
145         
146         public void synchronizeEditText(String editText) {
147                 this.editText = editText;
148         }
149         
150 }