View Javadoc

1   /*
2    * Created on 13 avr. 2005
3    * 
4    *   Magic-Project is a turn based strategy simulator
5    *   Copyright (C) 2003-2007 Fabrice Daugan
6    *
7    *   This program is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU General Public License as published by the Free 
9    * Software Foundation; either version 2 of the License, or (at your option) any
10   * later version.
11   *
12   *   This program is distributed in the hope that it will be useful, but WITHOUT 
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
15   * details.
16   *
17   *   You should have received a copy of the GNU General Public License along  
18   * with this program; if not, write to the Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package net.sf.magicproject.ui.component;
22  
23  import java.awt.Color;
24  import java.awt.event.ActionEvent;
25  import java.awt.event.ActionListener;
26  
27  import javax.swing.JTextPane;
28  import javax.swing.JToggleButton;
29  import javax.swing.border.EtchedBorder;
30  import javax.swing.text.BadLocationException;
31  import javax.swing.text.SimpleAttributeSet;
32  import javax.swing.text.StyleConstants;
33  
34  /***
35   * @author benny
36   */
37  public class EditorPane extends JTextPane implements ActionListener {
38  
39  	/***
40  	 */
41  	protected EditorPane() {
42  		style = new SimpleAttributeSet();
43  		setBorder(new EtchedBorder());
44  		setEditable(false);
45  	}
46  
47  	/***
48  	 * Set your name and the contact's name
49  	 * 
50  	 * @param you
51  	 * @param contact
52  	 */
53  	public void setContact(String you, String contact) {
54  		this.you = you;
55  		this.contact = contact;
56  	}
57  
58  	/***
59  	 * Append a new message
60  	 * 
61  	 * @param id
62  	 *          source id
63  	 * @param text
64  	 *          text to append.
65  	 */
66  	public void append(int id, String text) {
67  		StyleConstants.setBold(style, true);
68  		try {
69  			switch (id) {
70  			case 0:
71  				StyleConstants.setForeground(style, Color.GREEN);
72  				getDocument().insertString(getDocument().getLength(),
73  						new StringBuilder("[").append(you).append("] ").toString(), style);
74  				break;
75  			case 1:
76  				StyleConstants.setForeground(style, Color.MAGENTA);
77  				getDocument().insertString(getDocument().getLength(),
78  						new StringBuilder("[").append(contact).append("] ").toString(),
79  						style);
80  				break;
81  			default:
82  				StyleConstants.setForeground(style, Color.GRAY);
83  				getDocument().insertString(getDocument().getLength(), "[syst] ", style);
84  				break;
85  			}
86  		} catch (BadLocationException e) {
87  			e.printStackTrace();
88  		}
89  	}
90  
91  	/*
92  	 * (non-Javadoc)
93  	 * 
94  	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
95  	 */
96  	public void actionPerformed(ActionEvent e) {
97  		String action = e.getActionCommand();
98  		if ("disptime".equals(action)) {
99  			setDispTime(((JToggleButton) e.getSource()).isSelected());
100 		} else if ("clear".equals(action)) {
101 			this.setText("");
102 		} else if ("lock".equals(action)) {
103 			this.setLocked(((JToggleButton) e.getSource()).isSelected());
104 		}
105 	}
106 
107 	/***
108 	 * Is this text form is locked (not auto scroll)
109 	 * 
110 	 * @param locked
111 	 */
112 	public void setLocked(boolean locked) {
113 		this.locked = locked;
114 	}
115 
116 	/***
117 	 * Is this text form is locked (not auto scroll)
118 	 * 
119 	 * @return true if this text form is locked (not auto scroll)
120 	 */
121 	public boolean isLocked() {
122 		return locked;
123 	}
124 
125 	/***
126 	 * Is the time is displayed before each sentence
127 	 * 
128 	 * @param dispTime
129 	 */
130 	public void setDispTime(boolean dispTime) {
131 		this.dispTime = dispTime;
132 	}
133 
134 	/***
135 	 * Is the time is displayed before each sentence
136 	 * 
137 	 * @return true if the time is displayed.
138 	 */
139 	public boolean isDispTime() {
140 		return dispTime;
141 	}
142 
143 	private boolean dispTime;
144 
145 	/***
146 	 * Is this text form is locked (not auto scroll)
147 	 */
148 	protected boolean locked;
149 
150 	/***
151 	 * Your name
152 	 */
153 	protected String you;
154 
155 	/***
156 	 * Contact's name
157 	 */
158 	protected String contact;
159 
160 	/***
161 	 * The preferred display style of this editor
162 	 */
163 	protected SimpleAttributeSet style;
164 
165 }