]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/TextEditorImpl.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / handler / impl / TextEditorImpl.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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.g2d.element.handler.impl;
13
14 import org.simantics.g2d.element.ElementUtils;
15 import org.simantics.g2d.element.IElement;
16 import org.simantics.g2d.element.handler.TextEditor;
17 import org.simantics.g2d.utils.TextSegment;
18 import org.simantics.utils.datastructures.hints.IHintContext.Key;
19 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
20
21 public class TextEditorImpl implements TextEditor {
22
23     private static final long      serialVersionUID = 7183657432363119779L;
24
25     public static final TextEditor INSTANCE         = new TextEditorImpl();
26
27     public static final Key        ACTIVE           = new KeyOf(Boolean.class, "TEXT_EDITOR_ACTIVE");
28     public static final Key        TEXT             = new KeyOf(String.class, "TEXT_EDIT_TEXT");
29     public static final Key        MODIFIER         = new KeyOf(Modifier.class, "TEXT_MODIFIER");
30     public static final Key        TEXT_SELECTION   = new KeyOf(TextSegment.class, "TEXT_SELECTION");
31     public static final Key        CARET_POS        = new KeyOf(Integer.class, "CARET_POS");
32
33     @Override
34     public boolean isActive(IElement e) {
35         return ElementUtils.getHintOrDefault(e, ACTIVE, Boolean.FALSE);
36     }
37
38     @Override
39     public void setActive(IElement e, boolean value) {
40         ElementUtils.setOrRemoveHint(e, ACTIVE, value ? Boolean.TRUE : null);
41         if (!value) {
42             e.removeHint(CARET_POS);
43         }
44     }
45
46     @Override
47     public String getText(IElement e) {
48         return e.getHint(TEXT);
49     }
50
51     @Override
52     public void setText(IElement e, String text) {
53         ElementUtils.setOrRemoveHint(e, TEXT, text);
54     }
55
56     @Override
57     public Modifier getModifier(IElement e) {
58         return e.getHint(MODIFIER);
59     }
60
61     @Override
62     public void setModifier(IElement e, org.simantics.g2d.element.handler.TextEditor.Modifier modifier) {
63         ElementUtils.setOrRemoveHint(e, MODIFIER, modifier);
64     }
65
66     @Override
67     public Integer getCaretPosition(IElement e) {
68         Integer pos = e.getHint(CARET_POS);
69         return pos;
70     }
71
72     @Override
73     public TextSegment getSelection(IElement e) {
74         return e.getHint(TEXT_SELECTION);
75     }
76
77     private static boolean isValidCaretPosition(String s, int pos) {
78         int l = s.length();
79         return pos >= 0 && pos <= l; 
80     }
81
82     private static void assertValidCaretPosition(String s, int pos) {
83         boolean valid = isValidCaretPosition(s, pos);
84         if (!valid)
85             throw new IllegalArgumentException("caret position " + pos + " is out of bounds in text '" + s + "'");
86     }
87
88     @Override
89     public void setCaretPosition(IElement e, Integer position) {
90         if (position == null) {
91             e.removeHint(CARET_POS);
92         } else {
93             String text = getText(e);
94             Integer old = e.getHint(CARET_POS);
95             if (position.equals(old))
96                 return;
97             assertValidCaretPosition(text, position);
98             e.setHint(CARET_POS, position);
99         }
100     }
101
102     @Override
103     public void setSelection(IElement e, TextSegment segment) {
104         if (segment == null) {
105             e.removeHint(TEXT_SELECTION);
106         } else {
107             String text = getText(e);
108             TextSegment old = e.getHint(TEXT_SELECTION);
109             if (segment.equals(old))
110                 return;
111             if (!segment.existsIn(text))
112                 throw new IllegalArgumentException("segment " + segment + " does not exist in text '" + text + "'");
113             e.setHint(TEXT_SELECTION, segment);
114         }
115     }
116
117 }