1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.element.handler.impl;
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;
21 public class TextEditorImpl implements TextEditor {
23 private static final long serialVersionUID = 7183657432363119779L;
25 public static final TextEditor INSTANCE = new TextEditorImpl();
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");
34 public boolean isActive(IElement e) {
35 return ElementUtils.getHintOrDefault(e, ACTIVE, Boolean.FALSE);
39 public void setActive(IElement e, boolean value) {
40 ElementUtils.setOrRemoveHint(e, ACTIVE, value ? Boolean.TRUE : null);
42 e.removeHint(CARET_POS);
47 public String getText(IElement e) {
48 return e.getHint(TEXT);
52 public void setText(IElement e, String text) {
53 ElementUtils.setOrRemoveHint(e, TEXT, text);
57 public Modifier getModifier(IElement e) {
58 return e.getHint(MODIFIER);
62 public void setModifier(IElement e, org.simantics.g2d.element.handler.TextEditor.Modifier modifier) {
63 ElementUtils.setOrRemoveHint(e, MODIFIER, modifier);
67 public Integer getCaretPosition(IElement e) {
68 Integer pos = e.getHint(CARET_POS);
73 public TextSegment getSelection(IElement e) {
74 return e.getHint(TEXT_SELECTION);
77 private static boolean isValidCaretPosition(String s, int pos) {
79 return pos >= 0 && pos <= l;
82 private static void assertValidCaretPosition(String s, int pos) {
83 boolean valid = isValidCaretPosition(s, pos);
85 throw new IllegalArgumentException("caret position " + pos + " is out of bounds in text '" + s + "'");
89 public void setCaretPosition(IElement e, Integer position) {
90 if (position == null) {
91 e.removeHint(CARET_POS);
93 String text = getText(e);
94 Integer old = e.getHint(CARET_POS);
95 if (position.equals(old))
97 assertValidCaretPosition(text, position);
98 e.setHint(CARET_POS, position);
103 public void setSelection(IElement e, TextSegment segment) {
104 if (segment == null) {
105 e.removeHint(TEXT_SELECTION);
107 String text = getText(e);
108 TextSegment old = e.getHint(TEXT_SELECTION);
109 if (segment.equals(old))
111 if (!segment.existsIn(text))
112 throw new IllegalArgumentException("segment " + segment + " does not exist in text '" + text + "'");
113 e.setHint(TEXT_SELECTION, segment);