KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > MessageDisplayPanel


1 package net.suberic.pooka.gui;
2 import net.suberic.pooka.*;
3 import net.suberic.util.gui.*;
4 import javax.mail.*;
5 import javax.mail.internet.*;
6 import java.awt.*;
7 import java.awt.event.*;
8 import javax.swing.*;
9 import javax.swing.text.TextAction JavaDoc;
10 import java.util.*;
11 import java.util.regex.*;
12 import javax.swing.text.JTextComponent JavaDoc;
13 import javax.swing.event.*;
14 import java.io.File JavaDoc;
15
16 /**
17  * This is a JPanel which displays the content of a message.
18  *
19  * This component should be usable either as part of a window (internal
20  * or full frame), and as part of a preview pane.
21  */

22 public abstract class MessageDisplayPanel extends JPanel {
23   protected MessageUI msgUI;
24   protected JSplitPane splitPane = null;
25   protected AttachmentPane attachmentPanel = null;
26   protected JTextPane editorPane = null;
27   protected JScrollPane editorScrollPane = null;
28   protected boolean hasAttachment = false;
29   protected ConfigurableKeyBinding keyBindings;
30   
31   protected HashMap mFontSetMap = new HashMap();
32
33   protected Matcher currentMatcher = null;
34
35   JPanel attachmentDisplayPanel;
36
37   /**
38    * Creates an empty MessageDisplayPanel.
39    */

40   public MessageDisplayPanel() {
41     this.setLayout(new CardLayout());
42   }
43   
44   /**
45    * Creates a MessageDisplayPanel for the given MessageUI.
46    */

47   public MessageDisplayPanel(MessageUI newMsgUI) {
48     msgUI = newMsgUI;
49     
50     this.setLayout(new BorderLayout());
51     
52   }
53   
54   /**
55    * This method is expected to do all the implementation-specific
56    * duties, like setting the editorPane, etc.
57    */

58   
59   public abstract void configureMessageDisplay() throws MessagingException;
60
61   /**
62    * Shows the current display of the encryption status.
63    */

64   public abstract net.suberic.pooka.gui.crypto.CryptoStatusDisplay getCryptoStatusDisplay();
65
66
67   /**
68    * Shows a dialog that asks for a String (ok, actually a regexp) to
69    * search for, then searches for it.
70    */

71   public void searchMessage() {
72     String JavaDoc searchString = getMessageUI().showInputDialog(Pooka.getProperty("message.search", "Find what"), Pooka.getProperty("message.search.title", "Find"));
73     if (searchString != null && searchString.length() > 0)
74       findRegexp(searchString);
75   }
76
77   /**
78    * Searches for the last-sought string. If no search has been done,
79    * calls searchMessage().
80    */

81   public void searchAgain() {
82     if (currentMatcher != null)
83       findNext();
84     else
85       searchMessage();
86   }
87
88   /**
89    * This finds the given regular expression in the displayed page.
90    */

91   protected void findRegexp(String JavaDoc regString) {
92     boolean matchSucceeded = doFindRegexp(regString);
93     if (matchSucceeded) {
94       int start = currentMatcher.start();
95       int end = currentMatcher.end();
96       JTextPane currentPane = getCurrentEditorPane();
97       currentPane.setCaretPosition(start);
98       currentPane.moveCaretPosition(end);
99     }
100   }
101
102   /**
103    * This finds the given regular expression in the displayed page.
104    */

105   protected void findNext() {
106     boolean matchSucceeded = doFindNext();
107     if (matchSucceeded) {
108       int start = currentMatcher.start();
109       int end = currentMatcher.end();
110       JTextPane currentPane = getCurrentEditorPane();
111       currentPane.setCaretPosition(start);
112       currentPane.moveCaretPosition(end);
113     }
114   }
115
116   /**
117    * Does the work of finding the next match.
118    */

119   protected boolean doFindRegexp(String JavaDoc regString) {
120     if (currentMatcher != null) {
121       String JavaDoc origString = currentMatcher.pattern().pattern();
122       if (origString.equals(regString)) {
123     return doFindNext();
124       }
125     }
126
127     // otherwise make a new pattern/matcher.
128

129     Pattern p = Pattern.compile(regString);
130     javax.swing.text.Document JavaDoc currentDoc = getCurrentEditorPane().getDocument();
131
132     try {
133       currentMatcher = p.matcher(currentDoc.getText(0, currentDoc.getLength()));
134       return currentMatcher.find();
135     } catch (Exception JavaDoc e) {
136
137     }
138     
139     return false;
140
141   }
142
143   /**
144    * Matches the next value.
145    */

146   protected boolean doFindNext() {
147     if (currentMatcher != null) {
148       return currentMatcher.find();
149     } else {
150       return false;
151     }
152   }
153
154   /**
155    * This calculates the default size for the EditorPane.
156    *
157    * Here, we use the MessageWindow.editorPane.* properties to determine
158    * the size. Specifically, we check for the hsizeByCharLength
159    * property. If this is set to true, then we dynamically determine
160    * the appropriate width using the current font of the editorPane
161    * along with the charLength property.
162    *
163    * If hsizeByCharLength is set to false, or if for whatever reason we
164    * find that we're unable to determine an appropriate size, then we just
165    * use the vsize and hsize properties.
166    */

167   public Dimension getDefaultEditorPaneSize() {
168     int hsize = 500;
169     int vsize = 500;
170     
171     try {
172       vsize = Integer.parseInt(Pooka.getProperty("MessageWindow.editorPane.vsize", "500"));
173     } catch (NumberFormatException JavaDoc nfe) {
174       vsize=500;
175     }
176     
177     try {
178       if (Pooka.getProperty("MessageWindow.editorPane.hsizeByCharLength", "false").equalsIgnoreCase("true") && editorPane != null) {
179     int charLength = Integer.parseInt(Pooka.getProperty("MessageWindow.editorPane.charLength", "80"));
180     Font currentFont = editorPane.getFont();
181     if (currentFont != null) {
182       FontMetrics fm = this.getFontMetrics(currentFont);
183       Insets margin = editorPane.getMargin();
184       int scrollBarWidth = 0;
185       if (editorScrollPane != null && editorScrollPane.getVerticalScrollBar() != null) {
186         scrollBarWidth = editorScrollPane.getVerticalScrollBar().getPreferredSize().width;
187       }
188       hsize = ((int)(charLength * fm.getStringBounds("Remember when you were young? You shone like the sun. Shine on you crazy diamo", editorPane.getGraphics()).getWidth() / 80)) + margin.left + margin.right + scrollBarWidth;
189     }
190       } else {
191     hsize = Integer.parseInt(Pooka.getProperty("MessageWindow.editorPane.hsize", "500"));
192       }
193     } catch (NumberFormatException JavaDoc nfe) {
194       hsize=500;
195     }
196     
197     Dimension retval = new Dimension(hsize, vsize);
198     return retval;
199   }
200
201
202   /**
203    * This sets the default font for the editorPane to a font determined
204    * by the MessageWindow.editorPane.font property.
205    *
206    * I believe that if the font cannot be found or instantiated,
207    * nothing should happen, but i'm not sure. :)
208    */

209   public void setDefaultFont() {
210     setDefaultFont(getEditorPane());
211   }
212
213   /**
214    * This sets the default font for the editorPane to a font determined
215    * by the MessageWindow.editorPane.font property.
216    *
217    * I believe that if the font cannot be found or instantiated,
218    * nothing should happen, but i'm not sure. :)
219    */

220   protected void setDefaultFont(JEditorPane jep) {
221     Font f = null;
222     try {
223       net.suberic.util.swing.ThemeSupporter ts = (net.suberic.util.swing.ThemeSupporter)getMessageUI();
224       net.suberic.util.swing.ConfigurableMetalTheme cmt = (net.suberic.util.swing.ConfigurableMetalTheme) ts.getTheme(Pooka.getUIFactory().getPookaThemeManager());
225       if (cmt != null) {
226     f = cmt.getMonospacedFont();
227       }
228     } catch (Exception JavaDoc e) {
229       // if we get an exception, just ignore it and use the default.
230
}
231     
232     if (f == null && mFontSetMap.get(jep) == null) {
233       String JavaDoc fontString = Pooka.getProperty("MessageWindow.editorPane.font", "Monospaced-Plain-11");
234       
235       f = Font.decode(fontString);
236     }
237     
238     if (f != null) {
239       jep.setFont(f);
240       mFontSetMap.put(jep, new Boolean JavaDoc(true));
241     }
242   }
243
244   /**
245    * This sets the size of the MessageDisplayPanel to a reasonable
246    * default value.
247    *
248    * This method should be implemented by subclasses.
249    */

250   public abstract void sizeToDefault();
251   
252   public UserProfile getDefaultProfile() {
253     if (getMessageProxy() != null)
254       return getMessageProxy().getDefaultProfile();
255     else
256       return null;
257   }
258   
259   public JTextPane getEditorPane() {
260     return editorPane;
261   }
262   
263
264   /**
265    * Returns the current EditorPane being used.
266    */

267   public JTextPane getCurrentEditorPane() {
268     return editorPane;
269   }
270   
271   public MessageProxy getMessageProxy() {
272     if (msgUI != null)
273       return msgUI.getMessageProxy();
274     else
275       return null;
276   }
277   
278   public MessageUI getMessageUI() {
279     return msgUI;
280   }
281   
282   public void setMessageUI(MessageUI newValue) {
283     msgUI = newValue;
284   }
285   
286   public String JavaDoc getMessageText() {
287     return getEditorPane().getText();
288   }
289   
290   public String JavaDoc getMessageContentType() {
291     return getEditorPane().getContentType();
292   }
293   
294   public AttachmentPane getAttachmentPanel() {
295     return attachmentPanel;
296   }
297   
298   public Action JavaDoc[] getActions() {
299     return null;
300   }
301   
302   public JSplitPane getSplitPane() {
303     return splitPane;
304   }
305 }
306
Popular Tags