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 18 public class ViewTextDialog extends JDialog 19 { 20 final int fontSize = UIManager.getFont("Label.font").getSize(); 21 JTextPane textpane = new JTextPane(); 22 private final JTextField searchTF = new JTextField(7); 24 25 private DefaultStyledDocument doc = null; 26 private final String propertiesKeyword; 27 final JLabel lineLabel = new JLabel(); 28 29 public ViewTextDialog( String propertiesKeyword, String dialogTitle, boolean modal) 30 { 31 super(SnowMailClientApp.getInstance(), dialogTitle, modal); 32 this.propertiesKeyword = propertiesKeyword; 33 34 doc = (DefaultStyledDocument) textpane.getDocument(); 36 Style defaultStyle = textpane.getStyle("default"); 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 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 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 public void keyReleased(KeyEvent e) 63 { 64 if(e.getKeyCode()==KeyEvent.VK_ENTER) 65 { 66 searchText(searchTF.getText(), true); } 68 else 69 { 70 searchText(searchTF.getText(), false); } 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 CloseControlPanel closePanel = new CloseControlPanel(this, false, true, Language.translate("Close")); 93 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 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 e) 136 { 137 e.printStackTrace(); 138 } 139 } 140 }); 141 142 } 144 public void saveSizeAndLocation() 145 { 146 SnowMailClientApp.getInstance().getProperties().saveComponentLocationInINIFile( 148 this, propertiesKeyword); 149 } 150 151 152 153 public void setText(String cont) 154 { 155 textpane.setText(cont); 156 textpane.setCaretPosition(0); } 158 159 int lastFoundPosition = -1; 160 161 162 public void searchText(String text, boolean searchNext) 163 { 164 String textUp = ""; 166 try 167 { 168 textUp = doc.getText(0, doc.getLength()).toUpperCase(); 169 } 170 catch(Exception e) 171 { 172 e.printStackTrace(); 173 } 174 175 Style defaultStyle = StyleContext.getDefaultStyleContext(). 176 getStyle(StyleContext.DEFAULT_STYLE); 177 178 doc.setCharacterAttributes(0, textUp.length(), defaultStyle, true); 180 181 if(text.length()==0) 182 { 183 return; 185 } 186 187 lastFoundPosition = textUp.indexOf(text.toUpperCase(), lastFoundPosition+1); 188 189 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 e) 204 { 205 e.printStackTrace(); 206 } 207 } 208 209 } 210 211 212 } | Popular Tags |