001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.gui.widget;
017:
018: import java.awt.event.ActionEvent;
019: import java.awt.event.ActionListener;
020: import java.awt.event.MouseAdapter;
021: import java.awt.event.MouseEvent;
022: import java.io.FileNotFoundException;
023: import java.io.FileWriter;
024: import java.io.IOException;
025:
026: import javax.swing.JMenuItem;
027: import javax.swing.JPopupMenu;
028: import javax.swing.JTextArea;
029:
030: public class FollowingJTextArea extends JTextArea {
031:
032: private boolean follow = true;
033:
034: public FollowingJTextArea() {
035: jInit();
036: }
037:
038: private void jInit() {
039: final JPopupMenu popUp = getPopupMenu();
040: this .add(popUp);
041: this .addMouseListener(new MouseAdapter() {
042: public void mouseClicked(MouseEvent e) {
043: if (e.getButton() == e.BUTTON3) {
044: popUp.show(FollowingJTextArea.this , e.getX(), e
045: .getY());
046: }
047: }
048: });
049: }
050:
051: public boolean isFollow() {
052: return follow;
053: }
054:
055: public void setFollow(boolean follow) {
056: this .follow = follow;
057: }
058:
059: private void scrollToEnd() {
060: setCaretPosition(getDocument().getLength());
061: }
062:
063: private void toggleFollow() {
064: setFollow(!isFollow());
065: }
066:
067: /**
068: * Appends the given text to the end of the document.
069: *
070: * @param str the text to insert
071: * @todo Implement this javax.swing.JTextArea method
072: */
073: public void append(String str) {
074: super .append(str);
075: if (follow)
076: scrollToEnd();
077: }
078:
079: private JPopupMenu getPopupMenu() {
080: JPopupMenu contextMenu = new JPopupMenu("Options");
081: JMenuItem saveMenu = new JMenuItem("Save Text");
082: saveMenu.addActionListener(new ActionListener() {
083: public void actionPerformed(ActionEvent e) {
084: SelectFileAction action = new SelectFileAction(
085: "Save Output", null, null);
086: try {
087: action.actionPerformed(new ActionEvent(this , 0,
088: "Save Output"));
089: if (action.selectedFile != null) {
090: FileWriter fos = new FileWriter(
091: action.selectedFile);
092: fos.write(getText());
093: fos.close();
094: }
095:
096: } catch (FileNotFoundException ex) {
097: System.err.println("FileNotFoundException");
098: } catch (IOException ex) {
099: System.err.println("IOException");
100: }
101: }
102: });
103: contextMenu.add(saveMenu);
104:
105: JMenuItem toggleFollowMenu = new JMenuItem("Toggle Follow");
106: toggleFollowMenu.addActionListener(new ActionListener() {
107: public void actionPerformed(ActionEvent e) {
108: toggleFollow();
109: }
110: });
111: contextMenu.add(toggleFollowMenu);
112:
113: JMenuItem jumpToEndMenu = new JMenuItem("Jump To End");
114: jumpToEndMenu.addActionListener(new ActionListener() {
115: public void actionPerformed(ActionEvent e) {
116: setCaretPosition(getDocument().getLength());
117: }
118: });
119: contextMenu.add(toggleFollowMenu);
120:
121: JMenuItem clearTextMenu = new JMenuItem("Clear Text");
122: clearTextMenu.addActionListener(new ActionListener() {
123: public void actionPerformed(ActionEvent e) {
124: setText("");
125: }
126: });
127: contextMenu.add(clearTextMenu);
128: return contextMenu;
129: }
130: }
|