source: other-projects/trunk/realistic-books/packages/AntInstaller/src/org/tp23/gui/widget/FollowingJTextArea.java@ 19253

Last change on this file since 19253 was 19253, checked in by davidb, 15 years ago

Establishing a source code repository for Veronica's Realistic Book's software

File size: 3.9 KB
Line 
1/*
2 * Copyright 2005 Paul Hinds
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.tp23.gui.widget;
17
18import java.awt.event.ActionEvent;
19import java.awt.event.ActionListener;
20import java.awt.event.MouseAdapter;
21import java.awt.event.MouseEvent;
22import java.io.FileNotFoundException;
23import java.io.FileWriter;
24import java.io.IOException;
25
26import javax.swing.JMenuItem;
27import javax.swing.JPopupMenu;
28import javax.swing.JTextArea;
29
30public class FollowingJTextArea extends JTextArea{
31
32 private boolean follow = true;
33
34 public FollowingJTextArea() {
35 jInit();
36 }
37
38
39 private void jInit(){
40 final JPopupMenu popUp = getPopupMenu();
41 this.add(popUp);
42 this.addMouseListener(new MouseAdapter(){
43 public void mouseClicked(MouseEvent e) {
44 if (e.getButton() == e.BUTTON3) {
45 popUp.show(FollowingJTextArea.this,e.getX(),e.getY());
46 }
47 }
48 });
49 }
50
51 public boolean isFollow() {
52 return follow;
53 }
54 public void setFollow(boolean follow) {
55 this.follow = follow;
56 }
57
58 private void scrollToEnd(){
59 setCaretPosition(getDocument().getLength());
60 }
61 private void toggleFollow(){
62 setFollow(!isFollow());
63 }
64
65 /**
66 * Appends the given text to the end of the document.
67 *
68 * @param str the text to insert
69 * @todo Implement this javax.swing.JTextArea method
70 */
71 public void append(String str) {
72 super.append(str);
73 if(follow)scrollToEnd();
74 }
75 private JPopupMenu getPopupMenu() {
76 JPopupMenu contextMenu = new JPopupMenu("Options");
77 JMenuItem saveMenu = new JMenuItem("Save Text");
78 saveMenu.addActionListener(new ActionListener() {
79 public void actionPerformed(ActionEvent e) {
80 SelectFileAction action = new SelectFileAction("Save Output", null, null);
81 try {
82 action.actionPerformed(new ActionEvent(this, 0, "Save Output"));
83 if (action.selectedFile != null) {
84 FileWriter fos = new FileWriter(action.selectedFile);
85 fos.write(getText());
86 fos.close();
87 }
88
89 }
90 catch (FileNotFoundException ex) {
91 System.err.println("FileNotFoundException");
92 }
93 catch (IOException ex) {
94 System.err.println("IOException");
95 }
96 }
97 });
98 contextMenu.add(saveMenu);
99
100 JMenuItem toggleFollowMenu = new JMenuItem("Toggle Follow");
101 toggleFollowMenu.addActionListener(new ActionListener() {
102 public void actionPerformed(ActionEvent e) {
103 toggleFollow();
104 }
105 });
106 contextMenu.add(toggleFollowMenu);
107
108 JMenuItem jumpToEndMenu = new JMenuItem("Jump To End");
109 jumpToEndMenu.addActionListener(new ActionListener() {
110 public void actionPerformed(ActionEvent e) {
111 setCaretPosition(getDocument().getLength());
112 }
113 });
114 contextMenu.add(toggleFollowMenu);
115
116 JMenuItem clearTextMenu = new JMenuItem("Clear Text");
117 clearTextMenu.addActionListener(new ActionListener() {
118 public void actionPerformed(ActionEvent e) {
119 setText("");
120 }
121 });
122 contextMenu.add(clearTextMenu);
123 return contextMenu;
124 }
125}
Note: See TracBrowser for help on using the repository browser.