Gave ModInfoArea a style tree that can be overridden
This commit is contained in:
parent
2405b73a28
commit
725aa9d868
1 changed files with 52 additions and 35 deletions
|
@ -5,14 +5,16 @@ import java.awt.Cursor;
|
||||||
import java.awt.Desktop;
|
import java.awt.Desktop;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.HashMap;
|
|
||||||
import javax.swing.JScrollPane;
|
import javax.swing.JScrollPane;
|
||||||
import javax.swing.JTextPane;
|
import javax.swing.JTextPane;
|
||||||
import javax.swing.event.MouseInputAdapter;
|
import javax.swing.event.MouseInputAdapter;
|
||||||
import javax.swing.text.AttributeSet;
|
import javax.swing.text.AttributeSet;
|
||||||
import javax.swing.text.BadLocationException;
|
import javax.swing.text.BadLocationException;
|
||||||
|
import javax.swing.text.DefaultStyledDocument;
|
||||||
import javax.swing.text.SimpleAttributeSet;
|
import javax.swing.text.SimpleAttributeSet;
|
||||||
|
import javax.swing.text.Style;
|
||||||
import javax.swing.text.StyleConstants;
|
import javax.swing.text.StyleConstants;
|
||||||
|
import javax.swing.text.StyleContext;
|
||||||
import javax.swing.text.StyledDocument;
|
import javax.swing.text.StyledDocument;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
@ -23,44 +25,37 @@ public class ModInfoArea extends JScrollPane {
|
||||||
|
|
||||||
private static final Logger log = LogManager.getLogger(ModInfoArea.class);
|
private static final Logger log = LogManager.getLogger(ModInfoArea.class);
|
||||||
|
|
||||||
private static final String HYPERLINK_TARGET = "hyperlink-target";
|
private static final String STYLE_REGULAR = "regular";
|
||||||
|
private static final String STYLE_HYPERLINK = "hyperlink";
|
||||||
|
private static final String STYLE_TITLE = "title";
|
||||||
|
private static final String ATTR_HYPERLINK_TARGET = "hyperlink-target";
|
||||||
|
|
||||||
|
public static Color COLOR_HYPER = Color.BLUE;
|
||||||
|
public static final StyleContext DEFAULT_STYLES = ModInfoArea.getDefaultStyleContext();
|
||||||
|
|
||||||
private JTextPane textPane;
|
private JTextPane textPane;
|
||||||
private StyledDocument doc;
|
private StyledDocument doc;
|
||||||
private HashMap<String,SimpleAttributeSet> attrMap = new HashMap<String,SimpleAttributeSet>();
|
|
||||||
|
|
||||||
|
|
||||||
public ModInfoArea() {
|
public ModInfoArea() {
|
||||||
|
this( DEFAULT_STYLES );
|
||||||
|
}
|
||||||
|
|
||||||
|
public ModInfoArea( StyleContext styleContext ) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
textPane = new JTextPane();
|
textPane = new JTextPane();
|
||||||
textPane.setEditable( false );
|
textPane.setEditable( false );
|
||||||
|
|
||||||
doc = textPane.getStyledDocument();
|
doc = new DefaultStyledDocument(styleContext);
|
||||||
|
textPane.setStyledDocument(doc);
|
||||||
|
|
||||||
SimpleAttributeSet tmpAttr = new SimpleAttributeSet();
|
|
||||||
StyleConstants.setFontFamily( tmpAttr, "Monospaced" );
|
|
||||||
StyleConstants.setFontSize( tmpAttr, 12 );
|
|
||||||
attrMap.put( "regular", tmpAttr );
|
|
||||||
|
|
||||||
tmpAttr = new SimpleAttributeSet();
|
|
||||||
StyleConstants.setFontFamily( tmpAttr, "SansSerif" );
|
|
||||||
StyleConstants.setFontSize( tmpAttr, 24 );
|
|
||||||
StyleConstants.setBold( tmpAttr, true );
|
|
||||||
attrMap.put( "title", tmpAttr );
|
|
||||||
|
|
||||||
tmpAttr = new SimpleAttributeSet();
|
|
||||||
StyleConstants.setFontFamily( tmpAttr, "Monospaced" );
|
|
||||||
StyleConstants.setFontSize( tmpAttr, 12 );
|
|
||||||
StyleConstants.setForeground( tmpAttr, Color.BLUE );
|
|
||||||
StyleConstants.setUnderline( tmpAttr, true );
|
|
||||||
attrMap.put( "hyperlink", tmpAttr );
|
|
||||||
|
|
||||||
MouseInputAdapter hyperlinkListener = new MouseInputAdapter() {
|
MouseInputAdapter hyperlinkListener = new MouseInputAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked( MouseEvent e ) {
|
public void mouseClicked( MouseEvent e ) {
|
||||||
AttributeSet tmpAttr = doc.getCharacterElement( textPane.viewToModel(e.getPoint()) ).getAttributes();
|
AttributeSet tmpAttr = doc.getCharacterElement( textPane.viewToModel(e.getPoint()) ).getAttributes();
|
||||||
Object targetObj = tmpAttr.getAttribute( HYPERLINK_TARGET );
|
Object targetObj = tmpAttr.getAttribute( ATTR_HYPERLINK_TARGET );
|
||||||
if ( targetObj != null ) {
|
if ( targetObj != null ) {
|
||||||
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
|
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
|
||||||
if ( desktop != null && desktop.isSupported(Desktop.Action.BROWSE) ) {
|
if ( desktop != null && desktop.isSupported(Desktop.Action.BROWSE) ) {
|
||||||
|
@ -77,7 +72,7 @@ public class ModInfoArea extends JScrollPane {
|
||||||
@Override
|
@Override
|
||||||
public void mouseMoved( MouseEvent e ) {
|
public void mouseMoved( MouseEvent e ) {
|
||||||
AttributeSet tmpAttr = doc.getCharacterElement( textPane.viewToModel(e.getPoint()) ).getAttributes();
|
AttributeSet tmpAttr = doc.getCharacterElement( textPane.viewToModel(e.getPoint()) ).getAttributes();
|
||||||
if ( tmpAttr.getAttribute( HYPERLINK_TARGET ) != null ) {
|
if ( tmpAttr.getAttribute( ATTR_HYPERLINK_TARGET ) != null ) {
|
||||||
textPane.setCursor( new Cursor(Cursor.HAND_CURSOR) );
|
textPane.setCursor( new Cursor(Cursor.HAND_CURSOR) );
|
||||||
} else {
|
} else {
|
||||||
textPane.setCursor( new Cursor(Cursor.DEFAULT_CURSOR) );
|
textPane.setCursor( new Cursor(Cursor.DEFAULT_CURSOR) );
|
||||||
|
@ -98,26 +93,27 @@ public class ModInfoArea extends JScrollPane {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDescription( String title, String author, String version, String url, String body ) {
|
public void setDescription( String title, String author, String version, String url, String body ) {
|
||||||
|
Style regularStyle = doc.getStyle( STYLE_REGULAR );
|
||||||
try {
|
try {
|
||||||
doc.remove( 0, doc.getLength() );
|
doc.remove( 0, doc.getLength() );
|
||||||
doc.insertString( doc.getLength(), title +"\n", attrMap.get("title") );
|
doc.insertString( doc.getLength(), title +"\n", doc.getStyle( STYLE_TITLE ) );
|
||||||
|
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
if ( author != null ) {
|
if ( author != null ) {
|
||||||
doc.insertString( doc.getLength(), String.format("%sby %s", (first ? "" : " "), author), attrMap.get("regular") );
|
doc.insertString( doc.getLength(), String.format("%sby %s", (first ? "" : " "), author), regularStyle );
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
if ( version != null ) {
|
if ( version != null ) {
|
||||||
doc.insertString( doc.getLength(), String.format("%s(version %s)", (first ? "" : " "), version), attrMap.get("regular") );
|
doc.insertString( doc.getLength(), String.format("%s(version %s)", (first ? "" : " "), version), regularStyle );
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
if ( !first ) {
|
if ( !first ) {
|
||||||
doc.insertString( doc.getLength(), "\n", attrMap.get("regular") );
|
doc.insertString( doc.getLength(), "\n", regularStyle );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( url != null ) {
|
if ( url != null ) {
|
||||||
SimpleAttributeSet tmpAttr;
|
SimpleAttributeSet tmpAttr;
|
||||||
doc.insertString( doc.getLength(), "Website: ", attrMap.get("regular") );
|
doc.insertString( doc.getLength(), "Website: ", regularStyle );
|
||||||
|
|
||||||
boolean browseWorks = false;
|
boolean browseWorks = false;
|
||||||
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
|
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
|
||||||
|
@ -126,21 +122,20 @@ public class ModInfoArea extends JScrollPane {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( browseWorks && url.matches("^(?:https?|ftp)://.*") ) {
|
if ( browseWorks && url.matches("^(?:https?|ftp)://.*") ) {
|
||||||
tmpAttr = new SimpleAttributeSet( attrMap.get("hyperlink") );
|
tmpAttr = new SimpleAttributeSet( doc.getStyle( STYLE_HYPERLINK ) );
|
||||||
tmpAttr.addAttribute( HYPERLINK_TARGET, url );
|
tmpAttr.addAttribute( ATTR_HYPERLINK_TARGET, url );
|
||||||
doc.insertString( doc.getLength(), "Link", tmpAttr );
|
doc.insertString( doc.getLength(), "Link", tmpAttr );
|
||||||
} else {
|
} else {
|
||||||
tmpAttr = new SimpleAttributeSet( attrMap.get("regular") );
|
doc.insertString( doc.getLength(), url, regularStyle );
|
||||||
doc.insertString( doc.getLength(), url, tmpAttr );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
doc.insertString( doc.getLength(), "\n", attrMap.get("regular") );
|
doc.insertString( doc.getLength(), "\n", regularStyle );
|
||||||
}
|
}
|
||||||
|
|
||||||
doc.insertString( doc.getLength(), "\n", attrMap.get("regular") );
|
doc.insertString( doc.getLength(), "\n", regularStyle );
|
||||||
|
|
||||||
if ( body != null ) {
|
if ( body != null ) {
|
||||||
doc.insertString( doc.getLength(), body, attrMap.get("regular") );
|
doc.insertString( doc.getLength(), body, regularStyle );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch ( BadLocationException e) {
|
catch ( BadLocationException e) {
|
||||||
|
@ -149,4 +144,26 @@ public class ModInfoArea extends JScrollPane {
|
||||||
|
|
||||||
textPane.setCaretPosition(0);
|
textPane.setCaretPosition(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static StyleContext getDefaultStyleContext() {
|
||||||
|
StyleContext result = new StyleContext();
|
||||||
|
Style defaultStyle = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE );
|
||||||
|
Style baseStyle = result.addStyle("base", defaultStyle);
|
||||||
|
|
||||||
|
Style regularStyle = result.addStyle( STYLE_REGULAR, baseStyle );
|
||||||
|
StyleConstants.setFontFamily( regularStyle, "Monospaced" );
|
||||||
|
StyleConstants.setFontSize( regularStyle, 12 );
|
||||||
|
|
||||||
|
Style hyperStyle = result.addStyle( STYLE_HYPERLINK, regularStyle );
|
||||||
|
StyleConstants.setForeground( hyperStyle, COLOR_HYPER );
|
||||||
|
StyleConstants.setUnderline( hyperStyle, true );
|
||||||
|
|
||||||
|
Style titleStyle = result.addStyle( STYLE_TITLE, baseStyle );
|
||||||
|
StyleConstants.setFontFamily( titleStyle, "SansSerif" );
|
||||||
|
StyleConstants.setFontSize( titleStyle, 24 );
|
||||||
|
StyleConstants.setBold( titleStyle, true );
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue