Java Code Examples for javax.swing.text.StyledDocument#getStyle()

The following examples show how to use javax.swing.text.StyledDocument#getStyle() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SeaGlassTextPaneUI.java    From seaglass with Apache License 2.0 7 votes vote down vote up
/**
 * Update the font in the default style of the document.
 *
 * @param font the new font to use or null to remove the font attribute
 *             from the document's style
 */
private void updateFont(Font font) {
    StyledDocument doc = (StyledDocument)getComponent().getDocument();
    Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);

    if (style == null) {
        return;
    }

    if (font == null) {
        style.removeAttribute(StyleConstants.FontFamily);
        style.removeAttribute(StyleConstants.FontSize);
        style.removeAttribute(StyleConstants.Bold);
        style.removeAttribute(StyleConstants.Italic);
    } else {
        StyleConstants.setFontFamily(style, font.getName());
        StyleConstants.setFontSize(style, font.getSize());
        StyleConstants.setBold(style, font.isBold());
        StyleConstants.setItalic(style, font.isItalic());
    }
}
 
Example 2
Source File: VCSHyperlinkSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void insertString(StyledDocument sd, Style style) throws BadLocationException {
    if(style == null) {
        style = authorStyle;
    }
    sd.insertString(sd.getLength(), author, style);

    String iconStyleName = AUTHOR_ICON_STYLE + author;
    Style iconStyle = sd.getStyle(iconStyleName);
    if(iconStyle == null) {
        iconStyle = sd.addStyle(iconStyleName, null);
        StyleConstants.setIcon(iconStyle, kenaiUser.getIcon());
    }
    sd.insertString(sd.getLength(), " ", style);
    sd.insertString(sd.getLength(), " ", iconStyle);
}
 
Example 3
Source File: MWPaneFormatter.java    From wpcleaner with Apache License 2.0 6 votes vote down vote up
/**
 * Format elements in a MediaWikiPane highlighting non wiki text.
 * 
 * @param doc Document to be formatted.
 * @param analysis Page analysis.
 */
private void formatWikiText(
    StyledDocument doc,
    PageAnalysis analysis) {
  if ((doc == null) || (analysis == null)) {
    return;
  }
  Style style = doc.getStyle(ConfigurationValueStyle.COMMENTS.getName());
  List<Area> areas = analysis.getAreas().getAreas();
  if (areas != null) {
    for (Area area : areas) {
      int beginIndex = area.getBeginIndex();
      int endIndex = area.getEndIndex();
      doc.setCharacterAttributes(
          beginIndex, endIndex - beginIndex,
          style, true);
    }
  }
}
 
Example 4
Source File: GuiUtil.java    From FancyBing with GNU General Public License v3.0 5 votes vote down vote up
public static void setStyle(JTextPane textPane, int start, int length,
                            String name)
{
    StyledDocument doc = textPane.getStyledDocument();
    Style style;
    if (name == null)
    {
        StyleContext context = StyleContext.getDefaultStyleContext();
        style = context.getStyle(StyleContext.DEFAULT_STYLE);
    }
    else
        style = doc.getStyle(name);
    doc.setCharacterAttributes(start, length, style, true);
}
 
Example 5
Source File: SeaGlassTextPaneUI.java    From seaglass with Apache License 2.0 5 votes vote down vote up
/**
 * Update the color in the default style of the document.
 *
 * @param color the new color to use or null to remove the color attribute
 *              from the document's style
 */
private void updateForeground(Color color) {
    StyledDocument doc = (StyledDocument)getComponent().getDocument();
    Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);

    if (style == null) {
        return;
    }

    if (color == null) {
        style.removeAttribute(StyleConstants.Foreground);
    } else {
        StyleConstants.setForeground(style, color);
    }
}
 
Example 6
Source File: MWPaneFormatter.java    From wpcleaner with Apache License 2.0 5 votes vote down vote up
/**
 * @param doc Document.
 * @param analysis Page analysis.
 * @param link Link.
 * @return Style depending on the status of the link.
 */
private Style getInternalLinkStyle(
    StyledDocument doc,
    PageAnalysis analysis,
    PageElementInternalLink link) {
  Style style = null;

  if ((analysis != null) &&
      (analysis.getPage() != null) &&
      (analysis.getPage().getLinks() != null)) {

    // Find link target
    Page target = null;
    for (Page tmpPage : analysis.getPage().getLinks()) {
      if (Page.areSameTitle(tmpPage.getTitle(), link.getLink())) {
        target = tmpPage;
      }
    }

    // Specific styles
    if (target != null) {
      if (target.getRedirects().isRedirect()) {
        style = doc.getStyle(ConfigurationValueStyle.INTERNAL_LINK_DEFAULT_REDIRECT.getName());
      } else if (Boolean.FALSE.equals(target.isExisting())) {
        style = doc.getStyle(ConfigurationValueStyle.INTERNAL_LINK_DEFAULT_MISSING.getName());
      }
    }
  }

  // Apply default style for internal links
  if (style == null) {
    style = doc.getStyle(ConfigurationValueStyle.INTERNAL_LINK.getName());
  }

  return style;
}
 
Example 7
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout(5, 5));
  JButton ok = new JButton("Test");
  ok.addActionListener(e -> append("Test test test test", true));

  JButton err = new JButton("Error");
  err.addActionListener(e -> append("Error error error error", false));

  JButton clr = new JButton("Clear");
  clr.addActionListener(e -> jtp.setText(""));

  Box box = Box.createHorizontalBox();
  box.add(Box.createHorizontalGlue());
  box.add(ok);
  box.add(err);
  box.add(Box.createHorizontalStrut(5));
  box.add(clr);

  jtp.setEditable(false);
  StyledDocument doc = jtp.getStyledDocument();
  // Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
  Style def = doc.getStyle(StyleContext.DEFAULT_STYLE);

  // Style regular = doc.addStyle("regular", def);
  // StyleConstants.setForeground(def, Color.BLACK);

  Style error = doc.addStyle("error", def);
  StyleConstants.setForeground(error, Color.RED);

  JScrollPane scroll = new JScrollPane(jtp);
  scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  scroll.getVerticalScrollBar().setUnitIncrement(25);

  add(scroll);
  add(box, BorderLayout.SOUTH);
  setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  setPreferredSize(new Dimension(320, 240));
}
 
Example 8
Source File: ParticleDataTrackFunctionPanel.java    From tracker with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void refreshInstructions(FunctionEditor source, boolean editing, int selectedColumn) {
  StyledDocument doc = instructions.getStyledDocument();
  Style style = doc.getStyle("blue");                                                //$NON-NLS-1$
  String s = TrackerRes.getString("ParticleDataTrackFunctionPanel.Instructions.General"); //$NON-NLS-1$
  if(!editing && hasInvalidExpressions()) {                            // error condition
    s = ToolsRes.getString("FunctionPanel.Instructions.BadCell");           //$NON-NLS-1$
    style = doc.getStyle("red");                                            //$NON-NLS-1$
  }
  instructions.setText(s);
  int len = instructions.getText().length();
  doc.setCharacterAttributes(0, len, style, false);
  revalidate();
}
 
Example 9
Source File: FunctionPanel.java    From osp with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Refreshes the instructions based on selected cell.
 *
 * @param source the function editor (may be null)
 * @param editing true if the table is editing
 * @param selectedColumn the selected table column, or -1 if none
 */
protected void refreshInstructions(FunctionEditor source, boolean editing, int selectedColumn) {
  StyledDocument doc = instructions.getStyledDocument();
  Style style = doc.getStyle("blue");                                                //$NON-NLS-1$
  String s = isEmpty() ? ToolsRes.getString("FunctionPanel.Instructions.GetStarted") //$NON-NLS-1$
                       : ToolsRes.getString("FunctionPanel.Instructions.General")   //$NON-NLS-1$
                         +"  "+ToolsRes.getString("FunctionPanel.Instructions.EditDescription"); //$NON-NLS-1$ //$NON-NLS-2$
  if(!editing&&hasCircularErrors()) {                                       // error condition
    s = ToolsRes.getString("FunctionPanel.Instructions.CircularErrors");    //$NON-NLS-1$
    style = doc.getStyle("red");                                            //$NON-NLS-1$
  } else if(!editing&&hasInvalidExpressions()) {                            // error condition
    s = ToolsRes.getString("FunctionPanel.Instructions.BadCell");           //$NON-NLS-1$
    style = doc.getStyle("red");                                            //$NON-NLS-1$
  } else if(source!=null) {
    if((selectedColumn==0)&&editing) {                                      // editing name
      s = ToolsRes.getString("FunctionPanel.Instructions.NameCell");        //$NON-NLS-1$
    } else if((selectedColumn==1)&&editing) {                               // editing expression
      s = source.getVariablesString(": "); //$NON-NLS-1$
    } else if(selectedColumn>-1) {
      s = ToolsRes.getString("FunctionPanel.Instructions.EditCell");        //$NON-NLS-1$
      if(selectedColumn==0) {
        s += "  "+ToolsRes.getString("FunctionPanel.Instructions.NameCell"); //$NON-NLS-1$//$NON-NLS-2$
        s += "\n"+ToolsRes.getString("FunctionPanel.Instructions.EditDescription"); //$NON-NLS-1$//$NON-NLS-2$
      } else {
        s += " "+ToolsRes.getString("FunctionPanel.Instructions.Help");     //$NON-NLS-1$//$NON-NLS-2$
      }
    }
  }
  instructions.setText(s);
  int len = instructions.getText().length();
  doc.setCharacterAttributes(0, len, style, false);
  revalidate();
}
 
Example 10
Source File: FunctionEditor.java    From osp with GNU General Public License v3.0 4 votes vote down vote up
public Component getTableCellEditorComponent(JTable atable, Object value, boolean isSelected, int row, int column) {
  table.rowToSelect = row;
  table.columnToSelect = column;
  if (usePopupEditor) {
  	undoEditsEnabled = false;
  	JDialog popup = getPopupEditor();
    if (functionPanel.functionTool!=null) {
    	// set font level of popup editor
   	int level = functionPanel.functionTool.getFontLevel();
   	FontSizer.setFonts(popup, level);
    }      	
    dragLabel.setText(ToolsRes.getString("FunctionEditor.DragLabel.Text")); //$NON-NLS-1$

   prevObject = objects.get(row);
   if (prevObject!=null) {
   	prevName = getName(prevObject);
   	prevExpression = getExpression(prevObject);
   }

    String val = value.toString();
    if (prevObject!=null && column>0) {
    	if (val.endsWith(DEGREES)) {
    		val = val.substring(0, val.length()-1);
    	}
    	else {
    		val = prevExpression;
    	}
    }
    
    popupField.setText(val);
   popupField.requestFocusInWindow();
	try {
	String s = popupField.getText();
	setInitialValue(s);
} catch (NumberFormatException ex) {
}

   popupField.selectAll();
   popupField.setBackground(Color.WHITE);
   if (column==1) {
    variablesPane.setText(getVariablesString(":\n")); //$NON-NLS-1$
     StyledDocument doc = variablesPane.getStyledDocument();
     Style blue = doc.getStyle("blue"); //$NON-NLS-1$
     doc.setCharacterAttributes(0, variablesPane.getText().length(), blue, false);
     popup.getContentPane().add(variablesPane, BorderLayout.CENTER);
   }
   else {
     popup.getContentPane().remove(variablesPane);
   }
   Rectangle cell = table.getCellRect(row, column, true);
   minPopupWidth = cell.width+2;
   Dimension dim = resizePopupEditor();
   Point p = table.getLocationOnScreen();
   popup.setLocation(p.x+cell.x+cell.width/2-dim.width/2, 
   		p.y+cell.y+cell.height/2-dim.height/2);
   popup.setVisible(true);
  }
  else {
  	field.setText(value.toString());
    functionPanel.refreshInstructions(FunctionEditor.this, true, column);
    functionPanel.tableEditorField = field;
  }
  return panel;
}