下面列出了javax.swing.text.html.HTML.Tag#javax.swing.text.MutableAttributeSet 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Select previous occurrence of text.
*/
public void selectPreviousOccurrence() {
StyledDocument doc = textPane.getStyledDocument();
int lastStart = Integer.MIN_VALUE;
for (int pos = textPane.getSelectionStart(); pos > 0; pos = lastStart) {
Element run = doc.getCharacterElement(pos - 1);
lastStart = run.getStartOffset();
MutableAttributeSet attr = (MutableAttributeSet) run.getAttributes();
if ((attr != null) &&
(attr.getAttribute(MWPaneFormatter.ATTRIBUTE_TYPE) != null) &&
(attr.getAttribute(MWPaneFormatter.ATTRIBUTE_OCCURRENCE) != Boolean.FALSE)) {
select(run);
return;
}
}
selectLastOccurrence();
}
private AttributeSet setStyle(
String fontFamilyName,
int size,
Color color,
boolean bold,
boolean italic,
boolean underline
) {
MutableAttributeSet attr = new SimpleAttributeSet();
if (color != null) {
StyleConstants.setForeground(attr, color);
}
if (fontFamilyName != null) {
StyleConstants.setFontFamily(attr, fontFamilyName);
}
if (size != -1) {
StyleConstants.setFontSize(attr, size);
}
StyleConstants.setBold(attr, bold);
StyleConstants.setItalic(attr, italic);
StyleConstants.setUnderline(attr, underline);
setStyle(attr);
return getStyle();
}
/**
* Do bold face by spans.
*/
private void doBoldFaceBySpans() {
if (this.boldFaceSpans == null || this.boldFaceSpans.length == 0) {
return;
}
int docLength = this.cas.getDocumentText().length();
int spanLength = this.boldFaceSpans.length - (this.boldFaceSpans.length % 2);
int i = 0;
while (i < spanLength) {
int begin = this.boldFaceSpans[i];
int end = this.boldFaceSpans[i + 1];
if (begin >= 0 && begin <= docLength && end >= 0 && end <= docLength && begin < end) {
MutableAttributeSet attrs = new SimpleAttributeSet();
StyleConstants.setBold(attrs, true);
StyledDocument doc = (StyledDocument) this.textPane.getDocument();
doc.setCharacterAttributes(begin, end - begin, attrs, false);
}
i += 2;
}
}
public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
if (t == HTML.Tag.IMG) {
// get a src
String src = (String) a.getAttribute(HTML.Attribute.SRC);
if (src == null) {
return;
}
if (Pattern.matches(regex, src)) {
imgs.add(src);
}
}
}
public boolean set(MutableAttributeSet target, int parameter)
{
/* See above note in the case that parameter==1 */
Boolean value = ( parameter != 0 ? True : False );
target.addAttribute(swingName, value);
return true; /* true indicates we were successful */
}
public boolean set(MutableAttributeSet target)
{
/* TODO: There's some ambiguity about whether this should
*set* or *toggle* the attribute. */
target.addAttribute(swingName, True);
return true; /* true indicates we were successful */
}
public boolean set(MutableAttributeSet target, int parameter)
{
Number swingValue;
if (scale == 1f)
swingValue = Integer.valueOf(parameter);
else
swingValue = new Float(parameter / scale);
target.addAttribute(swingName, swingValue);
return true;
}
public void write(String text, MutableAttributeSet attrs) {
try {
insertString(getLength(), text, attrs);
limit = getLength();
caret.setDot(limit);
} catch (BadLocationException e) {
}
}
public boolean set(MutableAttributeSet target)
{
if (swingValue == null)
target.removeAttribute(swingName);
else
target.addAttribute(swingName, swingValue);
return true;
}
public
void setFontSize(int size) {
Enumeration e = attributes.elements();
while (e.hasMoreElements()) {
StyleConstants.setFontSize((MutableAttributeSet)e.nextElement(),size);
}
return;
}
public boolean set(MutableAttributeSet target, int parameter)
{
Number swingValue;
if (scale == 1f)
swingValue = Integer.valueOf(parameter);
else
swingValue = new Float(parameter / scale);
target.addAttribute(swingName, swingValue);
return true;
}
public boolean set(MutableAttributeSet target)
{
/* TODO: There's some ambiguity about whether this should
*set* or *toggle* the attribute. */
target.addAttribute(swingName, True);
return true; /* true indicates we were successful */
}
@Override
public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
if (stringBuilder.length() != 0
&& t.isBlock()
&& !stringBuilder.toString().endsWith(lineBreak)) {
stringBuilder.append(lineBreak);
}
}
public @Override void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
if (t == HTML.Tag.BODY) {
try {
this.in.close ();
} catch (IOException ioe) {/*Ignore it*/}
}
}
public boolean set(MutableAttributeSet target, int parameter)
{
/* See above note in the case that parameter==1 */
Boolean value = ( parameter != 0 ? True : False );
target.addAttribute(swingName, value);
return true; /* true indicates we were successful */
}
public boolean setDefault(MutableAttributeSet target)
{
if (swingDefault != rtfDefault ||
( target.getAttribute(swingName) != null ) )
target.addAttribute(swingName, Boolean.valueOf(rtfDefault));
return true;
}
public boolean set(MutableAttributeSet target)
{
/* TODO: There's some ambiguity about whether this should
*set* or *toggle* the attribute. */
target.addAttribute(swingName, True);
return true; /* true indicates we were successful */
}
public boolean set(MutableAttributeSet target)
{
if (swingValue == null)
target.removeAttribute(swingName);
else
target.addAttribute(swingName, swingValue);
return true;
}
public boolean set(MutableAttributeSet target, int parameter)
{
Number swingValue;
if (scale == 1f)
swingValue = Integer.valueOf(parameter);
else
swingValue = new Float(parameter / scale);
target.addAttribute(swingName, swingValue);
return true;
}
public boolean setDefault(MutableAttributeSet target)
{
if (swingDefault != rtfDefault ||
( target.getAttribute(swingName) != null ) )
target.addAttribute(swingName, Boolean.valueOf(rtfDefault));
return true;
}
public boolean set(MutableAttributeSet target, int parameter)
{
/* See above note in the case that parameter==1 */
Boolean value = ( parameter != 0 ? True : False );
target.addAttribute(swingName, value);
return true; /* true indicates we were successful */
}
private static void toggleStyleInSelection(JTextPane textPane, Object style) {
MutableAttributeSet attrSet = new SimpleAttributeSet();
// Add or remove the style on the entire selection
attrSet.addAttribute(style, !hasStyleInSelection(textPane, style));
textPane.getStyledDocument().setCharacterAttributes(textPane.getSelectionStart(),
textPane.getSelectionEnd() - textPane.getSelectionStart(),
attrSet, false);
}
public boolean set(MutableAttributeSet target, int parameter)
{
Number swingValue;
if (scale == 1f)
swingValue = Integer.valueOf(parameter);
else
swingValue = new Float(parameter / scale);
target.addAttribute(swingName, swingValue);
return true;
}
public boolean set(MutableAttributeSet target, int parameter)
{
Number swingValue;
if (scale == 1f)
swingValue = Integer.valueOf(parameter);
else
swingValue = new Float(parameter / scale);
target.addAttribute(swingName, swingValue);
return true;
}
public boolean setDefault(MutableAttributeSet target)
{
Number old = (Number)target.getAttribute(swingName);
if (old == null)
old = swingDefault;
if (old != null && (
(scale == 1f && old.intValue() == rtfDefault) ||
(Math.round(old.floatValue() * scale) == rtfDefault)
))
return true;
set(target, rtfDefault);
return true;
}
public void addImage(ImageNode image) {
try {
MutableAttributeSet inputAttributes = getInputAttributes();
inputAttributes.removeAttributes(inputAttributes);
StyleConstants.setIcon(inputAttributes, new ImageIcon(image.getImagePath()));
inputAttributes.addAttribute(PGTUtil.IMAGE_ID_ATTRIBUTE, image.getId());
imageReplaceSelection(" ");
inputAttributes.removeAttributes(inputAttributes);
} catch (IOException e) {
IOHandler.writeErrorLog(e);
InfoBox.error("Image Insertion Error", "Unable to insert image: "
+ e.getLocalizedMessage(), core.getRootWindow());
}
}
public boolean set(MutableAttributeSet target, int parameter)
{
Number swingValue;
if (scale == 1f)
swingValue = Integer.valueOf(parameter);
else
swingValue = new Float(parameter / scale);
target.addAttribute(swingName, swingValue);
return true;
}
public boolean setDefault(MutableAttributeSet target)
{
if (swingDefault != rtfDefault ||
( target.getAttribute(swingName) != null ) )
target.addAttribute(swingName, Boolean.valueOf(rtfDefault));
return true;
}
public boolean set(MutableAttributeSet target, int parameter)
{
Number swingValue;
if (scale == 1f)
swingValue = Integer.valueOf(parameter);
else
swingValue = new Float(parameter / scale);
target.addAttribute(swingName, swingValue);
return true;
}
public boolean set(MutableAttributeSet target, int parameter)
{
/* See above note in the case that parameter==1 */
Boolean value = ( parameter != 0 ? True : False );
target.addAttribute(swingName, value);
return true; /* true indicates we were successful */
}