KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > view > ViewTextDialog


1 package SnowMailClient.view;
2
3 import SnowMailClient.SnowMailClientApp;
4 import snow.utils.gui.*;
5 import snow.text.*;
6 import SnowMailClient.Language.Language;
7 import java.util.*;
8 import java.awt.*;
9 import java.awt.event.*;
10 import javax.swing.*;
11 import javax.swing.event.*;
12 import javax.swing.text.*;
13
14 /** A simple dialog with a text pane and search function to view a text
15     Used for the full mail content view or other debug views.
16     This dialog is NOT turn to visible. Please do it yourself.
17 */

18 public class ViewTextDialog extends JDialog
19 {
20   final int fontSize = UIManager.getFont("Label.font").getSize();
21   JTextPane textpane = new JTextPane();
22   //SnowMailClientApp snowMail;
23
private final JTextField searchTF = new JTextField(7);
24
25   private DefaultStyledDocument doc = null;
26   private final String JavaDoc propertiesKeyword;
27   final JLabel lineLabel = new JLabel();
28
29   public ViewTextDialog( String JavaDoc propertiesKeyword, String JavaDoc dialogTitle, boolean modal)
30   {
31     super(SnowMailClientApp.getInstance(), dialogTitle, modal);
32     this.propertiesKeyword = propertiesKeyword;
33
34     // define the stale for the search
35
doc = (DefaultStyledDocument) textpane.getDocument();
36     Style defaultStyle = textpane.getStyle("default"); //StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
37

38     Style searchHitStyle = doc.addStyle("SearchHit", defaultStyle);
39     StyleConstants.setBackground(searchHitStyle, Color.yellow);
40     StyleConstants.setBold(searchHitStyle, true);
41
42     this.setContentPane(new SnowBackgroundPanel(new BorderLayout()));
43
44     // Center
45
//
46
JScrollPane jsp = new JScrollPane(textpane);
47
48     this.getContentPane().add(jsp, BorderLayout.CENTER);
49     textpane.setEditable(false);
50     jsp.setOpaque(false);
51     jsp.getViewport().setOpaque(false);
52     textpane.setOpaque(false);
53
54     // North
55
//
56
JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
57     this.getContentPane().add(controlPanel, BorderLayout.NORTH);
58     controlPanel.add(new JContrastLabel(Language.translate("Search")+": "));
59     controlPanel.add(searchTF);
60     searchTF.addKeyListener(new KeyAdapter()
61      {
62         @Override JavaDoc public void keyReleased(KeyEvent e)
63         {
64            if(e.getKeyCode()==KeyEvent.VK_ENTER)
65            {
66               searchText(searchTF.getText(), true); // search next
67
}
68            else
69            {
70               searchText(searchTF.getText(), false); // initiate a new search
71
}
72         }
73      });
74     controlPanel.add(Box.createHorizontalGlue());
75     controlPanel.add(lineLabel);
76
77
78     searchTF.addFocusListener(new FocusListener()
79     {
80        public void focusGained(FocusEvent e)
81        {
82           searchTF.selectAll();
83        }
84        public void focusLost(FocusEvent e)
85        {
86        }
87     });
88
89
90     // South
91
//
92
CloseControlPanel closePanel = new CloseControlPanel(this, false, true, Language.translate("Close"));
93     //closePanel.setOkButtonText(Language.translate("Close"));
94
//closePanel.add
95

96
97
98     this.getContentPane().add(closePanel, BorderLayout.SOUTH);
99
100
101     SnowMailClientApp.getInstance().getProperties().setComponentSizeFromINIFile(
102       this, propertiesKeyword, fontSize*40,fontSize*50, 300,150);
103
104     this.addComponentListener(new ComponentListener()
105     {
106        public void componentShown(ComponentEvent ce)
107        {
108        }
109
110        public void componentMoved(ComponentEvent ce)
111        {
112        }
113
114        public void componentResized(ComponentEvent ce)
115        {
116           saveSizeAndLocation();
117        }
118
119        public void componentHidden(ComponentEvent ce)
120        {
121        }
122     });
123
124     textpane.addCaretListener(new CaretListener()
125     {
126        public void caretUpdate(javax.swing.event.CaretEvent JavaDoc ce)
127        {
128           int cp = textpane.getCaretPosition();
129           try
130           {
131             DefaultStyledDocument sd = (DefaultStyledDocument) textpane.getDocument();
132             Element elt = sd.getParagraphElement(cp);
133             lineLabel.setText(Language.translate("Line")+" "+(TextUtils.getPositionInParent(elt)+1));
134           }
135           catch(Exception JavaDoc e)
136           {
137             e.printStackTrace();
138           }
139        }
140     });
141
142   } // Constructor
143

144   public void saveSizeAndLocation()
145   {
146      //System.out.println("Save location and size of "+propertiesKeyword);
147
SnowMailClientApp.getInstance().getProperties().saveComponentLocationInINIFile(
148        this, propertiesKeyword);
149   }
150
151
152
153   public void setText(String JavaDoc cont)
154   {
155      textpane.setText(cont);
156      textpane.setCaretPosition(0); // scrolls...
157
}
158
159   int lastFoundPosition = -1;
160
161
162   public void searchText(String JavaDoc text, boolean searchNext)
163   {
164      // important, don't use the textpane getText method() !!
165
String JavaDoc textUp = "";
166      try
167      {
168        textUp = doc.getText(0, doc.getLength()).toUpperCase();
169      }
170      catch(Exception JavaDoc e)
171      {
172        e.printStackTrace();
173      }
174
175      Style defaultStyle = StyleContext.getDefaultStyleContext().
176                                      getStyle(StyleContext.DEFAULT_STYLE);
177
178      // remove all old style settings
179
doc.setCharacterAttributes(0, textUp.length(), defaultStyle, true);
180
181      if(text.length()==0)
182      {
183        // nothing to find !!
184
return;
185      }
186
187      lastFoundPosition = textUp.indexOf(text.toUpperCase(), lastFoundPosition+1);
188
189      // search from beginning
190
if(lastFoundPosition==-1 && searchNext)
191      {
192         lastFoundPosition = textUp.indexOf(text.toUpperCase(), 0);
193      }
194
195
196      if(lastFoundPosition!=-1)
197      {
198         try
199         {
200           doc.setCharacterAttributes(lastFoundPosition, text.length(), doc.getStyle("SearchHit"), true);
201           textpane.setCaretPosition(lastFoundPosition);
202         }
203         catch(Exception JavaDoc e)
204         {
205           e.printStackTrace();
206         }
207      }
208
209   }
210
211
212 } // ViewTextDialog
Popular Tags