chatarea.java
来自「开源项目openfire的完整源程序」· Java 代码 · 共 703 行 · 第 1/2 页
JAVA
703 行
/**
* Set the font on the current element.
*
* @param font the font to use with the current element
*/
public void setFont(String font) {
StyleConstants.setFontFamily(styles, font);
try {
setCharacterAttributes(styles, false);
}
catch (Exception fontException) {
Log.error("Error settings font:", fontException);
}
fontFamily = font;
}
/**
* Set the current font size.
*
* @param size the current font size.
*/
public void setFontSize(int size) {
StyleConstants.setFontSize(styles, size);
try {
setCharacterAttributes(styles, false);
}
catch (Exception fontException) {
Log.error("Error settings font:", fontException);
}
fontSize = size;
}
/**
* Inserts the current font.
*
* @param text - the current font( string representation )
*/
private void insertFont(String text) {
int index = text.indexOf("=");
int lastIndexOf = text.lastIndexOf(" ");
String font = text.substring(index + 1, lastIndexOf).replaceAll("_", " ");
index = text.lastIndexOf("=");
int slash = text.indexOf("/");
int fontSize = Integer.parseInt(text.substring(index + 1, slash));
setFontSize(fontSize);
setFont(font);
}
public void mouseClicked(MouseEvent e) {
try {
final int pos = viewToModel(e.getPoint());
final Element element = getStyledDocument().getCharacterElement(pos);
if (element != null) {
final AttributeSet as = element.getAttributes();
final Object o = as.getAttribute("link");
if (o != null) {
try {
final String url = (String)o;
boolean handled = fireLinkInterceptors(e, url);
if (!handled) {
if (Spark.isWindows()) {
SparkManager.getNativeManager().launchBrowser(url);
}
else {
BrowserLauncher.openURL(url);
}
}
}
catch (Exception ioe) {
Log.error("Error launching browser:", ioe);
}
}
}
}
catch (Exception ex) {
Log.error("Visible Error", ex);
}
}
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
handlePopup(e);
return;
}
}
/**
* This launches the <code>BrowserLauncher</code> with the URL
* located in <code>ChatArea</code>. Note that the url will
* automatically be clickable when added to <code>ChatArea</code>
*
* @param e - the MouseReleased event
*/
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
handlePopup(e);
return;
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
}
/**
* Checks to see if the mouse is located over a browseable
* link.
*
* @param e - the current MouseEvent.
*/
public void mouseMoved(MouseEvent e) {
checkForLink(e);
}
/**
* Checks to see if the mouse is located over a browseable
* link.
*
* @param e - the current MouseEvent.
*/
private void checkForLink(MouseEvent e) {
try {
final int pos = viewToModel(e.getPoint());
final Element element = getStyledDocument().getCharacterElement(pos);
if (element != null) {
final AttributeSet as = element.getAttributes();
final Object o = as.getAttribute("link");
if (o != null) {
setCursor(HAND_CURSOR);
}
else {
setCursor(DEFAULT_CURSOR);
}
}
}
catch (Exception ex) {
Log.error("Error in CheckLink:", ex);
}
}
/**
* Examines the chatInput text pane, and returns a string containing the text with any markup
* (jive markup in our case). This will strip any terminating new line from the input.
*
* @return a string of marked up text.
*/
public String getMarkup() {
final StringBuffer buf = new StringBuffer();
final String text = getText();
final StyledDocument doc = getStyledDocument();
final Element rootElem = doc.getDefaultRootElement();
// MAY RETURN THIS BLOCK
if (text.trim().length() <= 0) {
return null;
}
boolean endsInNewline = text.charAt(text.length() - 1) == '\n';
for (int j = 0; j < rootElem.getElementCount(); j++) {
final Element pElem = rootElem.getElement(j);
for (int i = 0; i < pElem.getElementCount(); i++) {
final Element e = pElem.getElement(i);
final AttributeSet as = e.getAttributes();
final boolean bold = StyleConstants.isBold(as);
final boolean italic = StyleConstants.isItalic(as);
final boolean underline = StyleConstants.isUnderline(as);
int end = e.getEndOffset();
if (end > text.length()) {
end = text.length();
}
if (endsInNewline && end >= text.length() - 1) {
end--;
}
// swing text.. :-/
if (j == rootElem.getElementCount() - 1
&& i == pElem.getElementCount() - 1) {
end = text.length();
}
final String current = text.substring(e.getStartOffset(), end);
if (bold) {
buf.append("[b]");
}
if (italic) {
buf.append("[i]");
}
if (underline) {
buf.append("[u]");
}
//buf.append( "[font face=/\"" + fontFamily + "/\" size=/\"" + fontSize + "/\"/]" );
// Iterator over current string to find url tokens
final StringTokenizer tkn = new StringTokenizer(current, " ", true);
while (tkn.hasMoreTokens()) {
final String token = tkn.nextToken();
if (token.startsWith("http://") || token.startsWith("ftp://")
|| token.startsWith("https://")) {
buf.append("[url]").append(token).append("[/url]");
}
else if (token.startsWith("www")) {
buf.append("[url ");
buf.append("http://").append(token);
buf.append("]");
buf.append(token);
buf.append("[/url]");
}
else {
buf.append(token);
}
}
// Always add end tags for markup
if (underline) {
buf.append("[/u]");
}
if (italic) {
buf.append("[/i]");
}
if (bold) {
buf.append("[/b]");
}
// buf.append( "[/font]" );
}
}
return buf.toString();
}
private void handlePopup(MouseEvent e) {
popup = new JPopupMenu();
popup.add(cutMenu);
popup.add(copyMenu);
popup.add(pasteMenu);
fireContextMenuListeners();
popup.addSeparator();
popup.add(selectAll);
// Handle enable
boolean textSelected = ModelUtil.hasLength(getSelectedText());
String clipboard = SparkManager.getClipboard();
cutMenu.setEnabled(textSelected && isEditable());
copyMenu.setEnabled(textSelected);
pasteMenu.setEnabled(ModelUtil.hasLength(clipboard) && isEditable());
popup.show(this, e.getX(), e.getY());
}
/**
* Adds a <code>ContextMenuListener</code> to ChatArea.
*
* @param listener the ContextMenuListener.
*/
public void addContextMenuListener(ContextMenuListener listener) {
contextMenuListener.add(listener);
}
/**
* Remove a <code>ContextMenuListener</code> to ChatArea.
*
* @param listener the ContextMenuListener.
*/
public void removeContextMenuListener(ContextMenuListener listener) {
contextMenuListener.remove(listener);
}
private void fireContextMenuListeners() {
Iterator listeners = new ArrayList(contextMenuListener).iterator();
while (listeners.hasNext()) {
ContextMenuListener listener = (ContextMenuListener)listeners.next();
listener.poppingUp(this, popup);
}
}
public void addLinkInterceptor(LinkInterceptor interceptor) {
interceptors.add(interceptor);
}
public void removeLinkInterceptor(LinkInterceptor interceptor) {
interceptors.remove(interceptor);
}
public boolean fireLinkInterceptors(MouseEvent event, String link) {
final Iterator iter = new ArrayList(interceptors).iterator();
while (iter.hasNext()) {
boolean handled = ((LinkInterceptor)iter.next()).handleLink(event, link);
if (handled) {
return true;
}
}
return false;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == cutMenu) {
cutAction();
}
else if (e.getSource() == copyMenu) {
SparkManager.setClipboard(getSelectedText());
}
else if (e.getSource() == pasteMenu) {
pasteAction();
}
else if (e.getSource() == selectAll) {
requestFocus();
selectAll();
}
}
private void cutAction() {
String selectedText = getSelectedText();
replaceSelection("");
SparkManager.setClipboard(selectedText);
}
private void pasteAction() {
String text = SparkManager.getClipboard();
if (text != null) {
replaceSelection(text);
}
}
protected void releaseResources() {
getActionMap().remove("copy");
getActionMap().remove("cut");
getActionMap().remove("paste");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?