1 7 8 import javax.swing.*; 9 import java.util.*; 10 import java.beans.*; 11 import java.net.URL ; 12 import javax.swing.text.*; 13 import javax.swing.event.*; 14 import java.awt.datatransfer.*; 15 import javax.swing.text.html.*; 16 import java.awt.Color ; 17 18 33 public class MyTextPane extends JTextPane{ 34 35 static final private String signature = "!:)==($)"; 36 41 public MyTextPane(){ 42 super(); 43 try { 44 jbInit(); 45 }catch( Exception e ) { 46 e.printStackTrace(); 47 } 48 pasteDeletesSelection = true; 49 } 50 54 public void cut(){ 55 copy( true ); 56 } 57 58 public void copyExt(){ 59 super.copy(); 60 } 61 65 public void copy(){ 66 copy( false ); 67 setSelectionStart( getSelectionEnd()); 68 } 69 80 public void paste(){ 81 if( myClipboard == null ) 82 return; 83 if( pasteDeletesSelection ){ 84 int selectionStart = getSelectionStart(); 85 int selectionEnd = getSelectionEnd(); 86 if( selectionStart < selectionEnd ){ 87 try{ 88 getStyledDocument().remove( selectionStart, selectionEnd - selectionStart ); 89 }catch( BadLocationException e ){ 90 e.printStackTrace(); 91 } 92 } 93 } 94 try{ 95 String clip = ( String )( myClipboard.getContents( DataFlavor.stringFlavor ).getTransferData( DataFlavor.stringFlavor )); 96 if(( clip.length()> signature.length())&&( clip.substring( 0, signature.length()).equals( signature ))) 98 takeIn( clip ); 99 else 100 super.paste(); 101 }catch( Exception e ){ 102 e.printStackTrace(); 103 } 104 } 105 111 public boolean getPasteDeletesSelection(){ 112 return this.pasteDeletesSelection; 113 } 114 120 public void setPasteDeletesSelection( boolean pasteDeletesSelection ){ 121 this.pasteDeletesSelection = pasteDeletesSelection; 122 } 123 130 private java.awt.datatransfer.Clipboard myClipboard; 131 134 private boolean pasteDeletesSelection; 135 140 private void jbInit() throws Exception { 141 pickClipboardToUse(); 142 } 143 147 private void copy( boolean deleteToo ){ 148 int selectionStart = getSelectionStart(); 149 int selectionEnd = getSelectionEnd(); 150 if( selectionStart >= selectionEnd ) 151 return; 152 StringSelection stringCopied = new StringSelection( returnCopy( selectionStart, selectionEnd )); 153 myClipboard.setContents( stringCopied, stringCopied ); 154 if( deleteToo ){ 155 try{ 156 getStyledDocument().remove( selectionStart, selectionEnd - selectionStart ); 157 }catch( BadLocationException e ){ 158 e.printStackTrace(); 159 } 160 } 161 } 162 167 public void pickClipboardToUse(){ 169 if( myClipboard != null ) 170 return; 171 try{ 172 System.getSecurityManager().checkSystemClipboardAccess(); 173 myClipboard = getToolkit().getSystemClipboard(); 174 System.out.println("Using system clipboard."); 175 }catch( SecurityException se ){ 176 myClipboard = new Clipboard("TextEditor"); 177 System.out.println("Using local clipboard, because system isn't available."); 178 } 179 } 180 193 private void takeIn( String myOwnClipboard ){ 194 StyledDocument theDocument = getStyledDocument(); 195 SimpleAttributeSet characterAttributes = new SimpleAttributeSet( theDocument.getCharacterElement( getSelectionStart()).getAttributes()); 196 for( int i = signature.length(); i < myOwnClipboard.length(); ++i ){ 197 String currentChar = myOwnClipboard.substring( i, 1 + i ); 198 int insertHere = getSelectionStart(); 199 if( currentChar.equals("\\")){ 200 int nextBackSlash = myOwnClipboard.indexOf('\\', 1 + i ); 201 if(( myOwnClipboard.charAt( i + 1 )=='\\')){ 202 try{ 203 theDocument.insertString( insertHere, currentChar, characterAttributes ); 204 i++; 205 continue; 206 }catch( BadLocationException e ){ 207 e.printStackTrace(); 208 } 209 } 210 currentChar = myOwnClipboard.substring( i, myOwnClipboard.indexOf("\\", 1 + i )); 211 switch( currentChar.charAt( 2 )){ 212 case 'l': 213 StyleConstants.setAlignment( characterAttributes,( new Integer ( currentChar.substring( 6 ))).intValue()); 214 try{ 215 theDocument.insertString( insertHere,"\n", characterAttributes ); 216 insertHere = getSelectionStart(); 217 theDocument.setParagraphAttributes( insertHere, 1, characterAttributes, true ); 218 }catch( BadLocationException e ){ 219 e.printStackTrace(); 220 } 221 break; 222 case 't': 223 if( currentChar.substring( 6 ).equals("Bold")) 224 StyleConstants.setBold( characterAttributes, true ); 225 else if( currentChar.substring( 6 ).equals("Italic")) 226 StyleConstants.setItalic( characterAttributes, true ); 227 else 228 StyleConstants.setUnderline( characterAttributes, true ); 229 break; 230 case 'a': 231 StyleConstants.setFontFamily( characterAttributes, currentChar.substring( 7 )); 232 StyleConstants.setBold( characterAttributes, false ); 233 StyleConstants.setItalic( characterAttributes, false ); 234 StyleConstants.setUnderline( characterAttributes, false ); 235 break; 236 case 'o': 237 StyleConstants.setForeground( characterAttributes, new Color ( new Integer ( currentChar.substring( 6 )).intValue())); 238 break; 239 case 'i': 240 StyleConstants.setFontSize( characterAttributes,( new Integer ( currentChar.substring( 5 ))).intValue()); 241 break; 242 } 243 i += currentChar.length(); 244 }else{ 245 try{ 246 theDocument.insertString( insertHere, currentChar, characterAttributes ); 247 }catch( BadLocationException e ){ 248 e.printStackTrace(); 249 } 250 } 251 } 252 } 253 258 private String returnCopy( int selectionStart, int selectionEnd ) { 259 StyledDocument theDocument = getStyledDocument( ); 260 AttributeSet characterAttributes = theDocument.getCharacterElement( selectionStart ).getAttributes( ); 261 String result = signature + shotOut( characterAttributes ); 262 while(selectionStart < selectionEnd) { 263 AttributeSet currentAttributes = theDocument.getCharacterElement( selectionStart ).getAttributes( ); 264 if(! characterAttributes.isEqual( currentAttributes )) { 265 result += shotOut( currentAttributes ); 266 characterAttributes = currentAttributes.copyAttributes( ); 267 } 268 try{ 269 String theChar = theDocument.getText( selectionStart++, 1 ); 270 if(theChar.equals( "\n" )) { 271 result += "\\align"+ StyleConstants.getAlignment( theDocument.getParagraphElement( selectionStart ).getAttributes())+"\\"; 272 }else{ 273 result += theChar; 274 if(theChar.equals("\\")) 275 result += theChar; 276 } 277 }catch( BadLocationException e ) { 278 e.printStackTrace(); 279 } 280 } 281 return( result ); 282 } 283 287 private String shotOut( AttributeSet attributes ){ 288 String result = "\\family" + StyleConstants.getFontFamily( attributes )+"\\"; 289 result += "\\color" + StyleConstants.getForeground( attributes ).getRGB()+"\\"; 290 if( StyleConstants.isBold( attributes )){ 291 result += "\\styleBold\\"; 292 } 293 if( StyleConstants.isItalic( attributes )){ 294 result += "\\styleItalic\\"; 295 } 296 if( StyleConstants.isUnderline( attributes )){ 297 result += "\\styleUnderline\\"; 298 } 299 return( result +"\\size"+ StyleConstants.getFontSize( attributes )+"\\" ); 300 } 301 } 302 | Popular Tags |