001: package org.columba.mail.gui.composer;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Font;
006: import java.awt.event.ActionEvent;
007: import java.awt.event.ActionListener;
008: import java.awt.event.ItemEvent;
009: import java.awt.event.ItemListener;
010: import java.awt.event.MouseEvent;
011: import java.awt.event.MouseListener;
012: import java.io.BufferedReader;
013: import java.io.File;
014: import java.io.FileReader;
015: import java.io.IOException;
016: import java.util.Observable;
017: import java.util.Observer;
018:
019: import javax.swing.BorderFactory;
020: import javax.swing.JButton;
021: import javax.swing.JPanel;
022: import javax.swing.JTextArea;
023: import javax.swing.UIManager;
024: import javax.swing.text.JTextComponent;
025:
026: import org.columba.core.config.Config;
027: import org.columba.core.gui.base.RoundedBorder;
028: import org.columba.core.gui.util.FontProperties;
029: import org.columba.core.xml.XmlElement;
030: import org.columba.mail.config.AccountItem;
031: import org.columba.mail.gui.config.account.EditSignatureAction;
032:
033: public class SignatureView extends JPanel implements MouseListener,
034: ItemListener, Observer {
035:
036: private ComposerController controller;
037:
038: private AccountItem item;
039:
040: private JTextComponent textPane;
041:
042: private JButton button;
043:
044: public SignatureView(ComposerController controller) {
045: super ();
046:
047: this .controller = controller;
048:
049: setLayout(new BorderLayout());
050:
051: setBackground(UIManager.getColor("TextArea.background"));
052:
053: setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
054:
055: JPanel centerPanel = new JPanel();
056: centerPanel.setBackground(UIManager
057: .getColor("TextArea.background"));
058:
059: centerPanel.setBorder(BorderFactory.createCompoundBorder(
060: new RoundedBorder(new Color(220, 220, 220)),
061: BorderFactory.createEmptyBorder(8, 8, 8, 8)));
062: centerPanel.setLayout(new BorderLayout());
063:
064: add(centerPanel, BorderLayout.CENTER);
065:
066: // setBorder(BorderFactory.createCompoundBorder(BorderFactory
067: // .createEmptyBorder(5, 5, 5, 5), BorderFactory
068: // .createCompoundBorder(new RoundedBorder(
069: // new Color(220, 220, 220)), BorderFactory
070: // .createEmptyBorder(5, 5, 5, 5))));
071:
072: button = new JButton("Edit Signature...");
073: button.addActionListener(new ActionListener() {
074: public void actionPerformed(ActionEvent event) {
075: new EditSignatureAction(SignatureView.this .controller,
076: item).actionPerformed(null);
077: }
078: });
079:
080: JPanel topPanel = new JPanel();
081:
082: topPanel.setBackground(UIManager
083: .getColor("TextArea.background"));
084: topPanel.setLayout(new BorderLayout());
085: topPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 8, 0));
086:
087: topPanel.add(button, BorderLayout.WEST);
088:
089: centerPanel.add(topPanel, BorderLayout.NORTH);
090:
091: textPane = new JTextArea();
092:
093: centerPanel.add(textPane, BorderLayout.CENTER);
094:
095: textPane.setEditable(false);
096: textPane.setBackground(UIManager
097: .getColor("TextArea.background"));
098: // textPane.setEnabled(false);
099: textPane.addMouseListener(this );
100:
101: Font font = FontProperties.getTextFont();
102: setFont(font);
103:
104: XmlElement options = Config.getInstance().get("options")
105: .getElement("/options");
106: XmlElement gui = options.getElement("gui");
107: XmlElement fonts = gui.getElement("fonts");
108:
109: if (fonts == null) {
110: fonts = gui.addSubElement("fonts");
111: }
112:
113: // register interest on configuration changes
114: fonts.addObserver(this );
115:
116: item = (AccountItem) controller.getAccountController()
117: .getView().getSelectedItem();
118:
119: if (item.getIdentity().getSignature() != null)
120: readSignature(item.getIdentity().getSignature());
121:
122: // if account selection changes, reload signature file
123: controller.getAccountController().getView().addItemListener(
124: this );
125:
126: // add listener to changes
127: item.getIdentity().addObserver(this );
128:
129: }
130:
131: public void itemStateChanged(ItemEvent e) {
132: if (e.getStateChange() == ItemEvent.SELECTED) {
133:
134: // remove listener from old account selection
135: item.getIdentity().removeObserver(this );
136:
137: item = (AccountItem) controller.getAccountController()
138: .getView().getSelectedItem();
139: if (item.getIdentity().getSignature() != null) {
140: readSignature(item.getIdentity().getSignature());
141: } else {
142: textPane.setText("");
143: }
144:
145: // add listener to changes
146: item.getIdentity().addObserver(this );
147:
148: }
149:
150: }
151:
152: /**
153: * Read signature from file.
154: *
155: * @param signatureFile
156: * signature file
157: */
158: private void readSignature(File signatureFile) {
159: StringBuffer strbuf = new StringBuffer();
160:
161: try {
162: BufferedReader in = new BufferedReader(new FileReader(
163: signatureFile));
164: String str;
165:
166: while ((str = in.readLine()) != null) {
167: strbuf.append(str + "\n");
168: }
169:
170: in.close();
171:
172: textPane.setText(strbuf.toString());
173:
174: } catch (IOException ex) {
175: textPane.setText("");
176: }
177: }
178:
179: public void mouseClicked(MouseEvent event) {
180: if ((event.getModifiers() & MouseEvent.BUTTON1_MASK) != 0
181: && item != null) {
182: if (event.getClickCount() >= 2) {
183: new EditSignatureAction(controller, item)
184: .actionPerformed(null);
185: }
186: }
187: }
188:
189: public void mouseEntered(MouseEvent e) {
190: // TODO Auto-generated method stub
191:
192: }
193:
194: public void mouseExited(MouseEvent e) {
195: // TODO Auto-generated method stub
196:
197: }
198:
199: public void mousePressed(MouseEvent e) {
200: // TODO Auto-generated method stub
201:
202: }
203:
204: public void mouseReleased(MouseEvent e) {
205: // TODO Auto-generated method stub
206:
207: }
208:
209: /**
210: *
211: * @see org.columba.mail.gui.config.general.MailOptionsDialog
212: *
213: * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
214: */
215: public void update(Observable arg0, Object arg1) {
216: Font font = FontProperties.getTextFont();
217: setFont(font);
218:
219: readSignature(item.getIdentity().getSignature());
220: }
221:
222: }
|