/** * Retrieves a single style from the StyleServer and compares it to * the previously saved style. * * @param name * @return true if the style has changed, false otherwise */ private boolean loadStyle(String name) { MutableAttributeSet newStyle = styleServer.getStyle(name); AttributeSet oldStyle = rawStyles.get(name); if (oldStyle != null && oldStyle.isEqual(newStyle)) { // Nothing in the style has changed, so nothing further to do return false; } // Save immutable copy of new style for next comparison rawStyles.put(name, newStyle.copyAttributes()); // Add type attribute to the style, so it can be recognized when // refreshing the styles in the document newStyle.addAttribute(TYPE, name); styles.put(name, newStyle); return true; }
/** * Inserts a link into the current document. * * @param link - the link to insert( ex. http://www.javasoft.com ) * @throws BadLocationException if the location is not available for insertion. */ public void insertLink( Document doc, String link ) throws BadLocationException { // Create a new style, based on the style used for generic text, for the link. final MutableAttributeSet linkStyle = new SimpleAttributeSet( getMessageStyle().copyAttributes() ); StyleConstants.setForeground( linkStyle, (Color) UIManager.get( "Link.foreground" ) ); StyleConstants.setUnderline( linkStyle, true ); linkStyle.addAttribute( "link", link ); doc.insertString( doc.getLength(), link, linkStyle ); }
/** * Inserts a network address into the current document. * * @param address - the address to insert( ex. \superpc\etc\file\ OR http://localhost/ ) * @throws BadLocationException if the location is not available for insertion. */ public void insertAddress( Document doc, String address ) throws BadLocationException { // Create a new style, based on the style used for generic text, for the address. final MutableAttributeSet addressStyle = new SimpleAttributeSet( getMessageStyle().copyAttributes() ); StyleConstants.setForeground( addressStyle, (Color) UIManager.get( "Address.foreground" ) ); StyleConstants.setUnderline( addressStyle, true ); addressStyle.addAttribute( "link", address ); doc.insertString( doc.getLength(), address, addressStyle ); }
public void appendToInput(String message, boolean preserveCaretPosition) { try { document.insertString(document.getLength(), message, textPane.getInputAttributes().copyAttributes()); } catch (BadLocationException e) { e.printStackTrace(); } catch (Exception e) { logger.error("Cannot insert input text", e); } if(!preserveCaretPosition) { textPane.setCaretPosition(document.getLength()); } }
public MutableAttributeSet applyMessageStyle( char directive, MutableAttributeSet messageStyle ) { final MutableAttributeSet style = new SimpleAttributeSet( messageStyle.copyAttributes() ); switch ( directive ) { case '*': StyleConstants.setBold( style, true ); break; case '_': StyleConstants.setItalic( style, true ); break; case '~': StyleConstants.setStrikeThrough( style, true ); break; case '`': StyleConstants.setFontFamily( style, "Monospaced" ); break; case 'K': // Keyword StyleConstants.setForeground( style, ((Color) messageStyle.getAttribute( Foreground )).brighter().brighter().brighter() ); break; default: Log.warning( "Cannot apply message style for unrecognized directive: " + directive ); } return style; }
private OffsetAttrs fixCaretPosition(int offset, AttributeSet attrs) { if (!isEditAllowedAt(offset) && (readMode || fakeReadMode)) { textPane.setCaretPosition(document.getLength()); return new OffsetAttrs(document.getLength(), textPane.getInputAttributes().copyAttributes()); } return new OffsetAttrs(offset, attrs); }
public void replaceInput(String message, boolean preserveCaretPosition) { int oldCaretPosition = textPane.getCaretPosition(); try { document.remove(startReadLen, document.getLength() - startReadLen); document.insertString(document.getLength(), message, textPane.getInputAttributes().copyAttributes()); } catch (BadLocationException e) { e.printStackTrace(); } catch (Exception e) { logger.error("Cannot insert input text", e); } int newCaretPosition = (preserveCaretPosition && oldCaretPosition <= document.getLength()) ? oldCaretPosition : document.getLength(); textPane.setCaretPosition(newCaretPosition); }