-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathComment.java
More file actions
158 lines (145 loc) · 5.38 KB
/
Copy pathComment.java
File metadata and controls
158 lines (145 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package openblocks.renderable;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JTextArea;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import openblocks.workspace.Workspace;
import openblocks.workspace.WorkspaceEvent;
import openblocks.yacodeblocks.Escapers;
import openblocks.codeblockutil.CGraphite;
/**
* Comment stores and displays user-generated text that
* can be edited by the user.
*
* Comments are associated with a parent source of type JComponent.
* It should "tag" along with that component. Note, however, that
* this feature should be ensured by the parent source. The
* parent source can guarantee this by invoking the methods
* setPosition, translatePosition, and setParent when
* appropriate.
*/
public class Comment extends BlockNote {
private static int INITIAL_WIDTH = 200;
private static int INITIAL_HEIGHT = 100;
private static int MAX_WIDTH = 300;
private UndoManager undoManager;
/**
* Constructs a Comment
* with belonging to source, with text of initText, and initial zoom
* The comment's borders will have the color borderColor.
*
* Note that initializing a comment only constructs
* all of the necessary structures. To graphically display a comment,
* the implementor must then add the comment using the proper
* Swing methods OR through the convenience method Comment.setParent()
*
* @param source block that the comment is linked to.
*/
public Comment(RenderableBlock source){
this(new JTextArea(), source);
}
private Comment(JTextArea jText, RenderableBlock source) {
super(jText, source, "?", CGraphite.yellow, true,
MAX_WIDTH, INITIAL_HEIGHT);
textContent = jText;
textContent.setCaretColor(Color.BLACK);
textContent.setLineWrap(true);
textContent.setWrapStyleWord(true);
fontMetrics = getFontMetrics(jText.getFont());
textPadding = fontMetrics.stringWidth("M") * TEXT_PADDING_EMS;
undoManager = new UndoManager();
undoManager.setLimit(1000);
textContent.getDocument().addUndoableEditListener(undoManager);
textContent.addKeyListener(new KeyAdapter(){
@Override
public void keyPressed(KeyEvent e){
Workspace.getInstance().notifyListeners(new WorkspaceEvent(
getRenderableBlock().getParentWidget(), WorkspaceEvent.BLOCK_NOTE_CHANGED));
if(e.isControlDown() || ((e.getModifiers() &
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0)){
if(e.getKeyCode() == KeyEvent.VK_Z){
try{
undoManager.undo();
}catch(CannotUndoException exception){}
}else if(e.getKeyCode() == KeyEvent.VK_Y){
try{
undoManager.redo();
}catch (CannotRedoException exception){}
}
}
}
});
textContent.setEditable(true);
setBlockNoteSize(new Dimension(INITIAL_WIDTH, INITIAL_HEIGHT));
source.chooseLocationForBalloon(this, INITIAL_WIDTH, INITIAL_HEIGHT);
}
/**
* @return the save String for this comment
*/
@Override
public String getSaveString() {
StringBuffer saveString = new StringBuffer();
saveString.append("<Comment>\n");
saveString.append("<Text>");
String textValue = getText().replaceAll("`", "'");
// Encode comments to preserve international characters.
textValue = Escapers.encodeInternationalCharacters(textValue);
saveString.append(Escapers.escapeForXml(textValue));
saveString.append("</Text>");
saveString.append(super.getSaveString());
saveString.append("</Comment>\n");
return saveString.toString();
}
/**
* Loads the comment from a NodeList of comment parts
* @param commentChildren
* @param rb
* @return the comment from a NodeList of comment parts
*/
public static Comment loadComment(NodeList commentChildren, RenderableBlock rb) {
Comment comment = new Comment(rb);
for (int j = 0; j < commentChildren.getLength(); j++) {
Node commentChild = commentChildren.item(j);
if (commentChild.getNodeName().equals("Text")) {
String textValue = commentChild.getTextContent();
// Decode comments to preserve international characters.
textValue = Escapers.decodeInternationalCharacters(textValue);
comment.setText(textValue);
break;
}
}
loadBlockNote(commentChildren, rb, comment);
return comment;
}
/**
* @return The comment's text
*/
public String getText() {
return textContent.getText();
}
// /**
// * @modifies textContent
// * @param text
// */
// public void setText(String text) {
// textArea.setText(text);
// }
/**
* String representation of this
*/
@Override
public String toString() {
return "Comment ID: " + " at " + getLocation() + " with text: \"" + getText() + "\"";
}
}