2013-07-24 21 views
2

我的問題如下:如何爲改變文本部分的HTML編輯器的背景顏色與一個HTMLEditorKit內置提供Functionalty

我想使我的小HTML編輯器的用戶對 不同的背景顏色之間進行切換正在輸入文字。我第一次嘗試 爲此目的使用CSS樣式。不同的風格定義了 不同的背景顏色,並且通過JComboBox用戶可以在這些風格之間切換 。一旦在HTMLDocument中選擇 各自位置的樣式,將會輸入 <span class="style">類型的新HTML元素。 不幸的是我無法完成這項工作。量程元素 根本就沒有創建(see my question regarding this problem)。

在之間我看看類StyledEditorKit.ForegroundAction 瞭解如何運作。執行後,它只是修改StyledEditorKit的 輸入屬性,使用前景顏色設置新的 。之後輸入的文字將以 顯示新的前景色。當將HTML代碼寫入文件時,該文本自動包含在HTML元素中。而且所有這些都可以運行在選定的文本上,這些文本可能會在多個段落上運行 。在這種情況下,顯然受影響的 文本在所有受影響的段落中都包含在<font ...> HTML標記中。

我想完成同樣的功能,用於在任意文本塊上設置背景顏色 。但出乎意料的是這似乎並不 那麼容易:-(

我沒有找到一個預定義的動作類類似於Java 7的JDK的 StyledEditorKit.foregroundAction這一目的。創建 這樣一類似乎沒有是複雜的,它幾乎一樣 ForegroundActionactionPerformed方法改爲設置 背景而不是前景屬性的

但是如何建立有效的HTML代碼,設置一個特定的背景 顏色所包含的部分。文字? 直到現在我不知道哪個部分HTMLEditorKit執行 創建所有<font>元素的HTMLDocument中的文本 具有設置的前景屬性。我認爲,從現有的代碼 創建<font>元素應該不會太難得到的 實現創建<span style="background-color:..."> 元素,而不是用於設置背景顏色 文本的任意區域<font>元素。或者,這一切已經可用,我只有沒有注意到?任何幫助,將不勝感激!

在我之間,我向前邁出了重要的一步,並感謝找到的一段代碼here 我設法創建了有效的<span>元素。在span元素中,我使用class屬性來分配預定義樣式。

這裏是我的代碼:

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.util.Enumeration; 
import java.util.Vector; 

import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.DefaultComboBoxModel; 
import javax.swing.JComboBox; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JOptionPane; 
import javax.swing.JTextPane; 
import javax.swing.JToolBar; 
import javax.swing.text.AttributeSet; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.MutableAttributeSet; 
import javax.swing.text.SimpleAttributeSet; 
import javax.swing.text.Style; 
import javax.swing.text.StyleContext; 
import javax.swing.text.html.HTML; 
import javax.swing.text.html.HTMLDocument; 
import javax.swing.text.html.HTMLEditorKit; 
import javax.swing.text.html.StyleSheet; 

public class SimpleEditor extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private final JTextPane textPane; 
    private final HTMLEditorKit edtKit; 
    private final HTMLDocument doc; 
    private final StyleSheet predefStyles; 

    public static void main(String[] args) throws BadLocationException, IOException { 
     final SimpleEditor editor = new SimpleEditor(); 
     editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     editor.setVisible(true); 
    } 

    public SimpleEditor() throws BadLocationException, IOException { 
     super("Very Simple HTML Editor"); 
     textPane = new JTextPane(); 
     edtKit = new HTMLEditorKit(); 
     textPane.setEditorKit(edtKit); 
     predefStyles = new StyleSheet(); 
     predefStyles.addRule(".MyStyle1 { color:#cc0000; background-color:silver }\n" + 
          ".MyStyle2 { color:#0000cc; background-color:aqua }"); 
     doc = new HTMLDocument(predefStyles); 
     textPane.setDocument(doc); 

     final Container content = getContentPane(); 
     content.add(textPane, BorderLayout.CENTER); 
     content.add(createToolBar(), BorderLayout.NORTH); 
     setJMenuBar(createMenuBar()); 
     setSize(500, 240); 
    } 

    private JToolBar createToolBar() { 
     final Vector<String> styleNames = new Vector<String>(); 
     final Enumeration<?> names = predefStyles.getStyleNames(); 
     while (names.hasMoreElements()) { 
      styleNames.add((String) names.nextElement()); 
     } 
     final DefaultComboBoxModel<String> stylesModel = 
       new DefaultComboBoxModel<String>(styleNames); 
     final JComboBox<String> cbStyleSel = new JComboBox<String>(stylesModel); 
     final JToolBar bar = new JToolBar(); 
     Action dumpAction = null; 
     for (final Action act : edtKit.getActions()) { 
      if (act.getValue(Action.NAME).equals("dump-model")) { 
       dumpAction = act; 
       break; 
      } 
     } 
     bar.add(dumpAction); 
     cbStyleSel.setEditable(false); 
     cbStyleSel.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       e.getSource(); 
       @SuppressWarnings("unchecked") 
       final JComboBox<CondStyle> cboStyleSel = (JComboBox<CondStyle>) e.getSource(); 
       final String selItem = (String) cboStyleSel.getSelectedItem(); 
       final MutableAttributeSet divAttributes = new SimpleAttributeSet(); 
       if (selItem.equals("default")) { 
        // This does not work! 
        final Style defStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); 
        divAttributes.addAttribute(HTML.Tag.CONTENT, defStyle); 
        textPane.setCharacterAttributes(divAttributes, true); 
       } else { 
        divAttributes.addAttribute(HTML.Attribute.CLASS, selItem.substring(1)); 
        final MutableAttributeSet tagAttributes = new SimpleAttributeSet(); 
        tagAttributes.addAttribute(HTML.Tag.SPAN, divAttributes); 
        textPane.setCharacterAttributes(tagAttributes, false); 
       } 
       textPane.requestFocusInWindow(); 
      } 
     }); 
     bar.add(cbStyleSel); 
     return bar; 
    } 

    /** 
    * Extracts the style attributes except the style's name 
    * @param aStyle The style to be processed 
    * @return The visual attributes extracted from the style 
    */ 
    AttributeSet extractStyleAttribs(Style aStyle) { 
     final MutableAttributeSet attribs = new SimpleAttributeSet(); 
     final Enumeration<?> attribNames = aStyle.getAttributeNames(); 
     while (attribNames.hasMoreElements()) { 
      final Object attribName = attribNames.nextElement(); 
      if (attribName == Style.NameAttribute) { 
       continue; 
      } 
      attribs.addAttribute(attribName, aStyle.getAttribute(attribName)); 
     } 
     return attribs; 
    } 

    private JMenuBar createMenuBar() { 
     final JMenuBar menubar = new JMenuBar(); 
     final JMenu mnuFile = new JMenu("File"); 
     menubar.add(mnuFile); 
     final SaveAction actSave = new SaveAction(); 
     mnuFile.add(actSave); 
     return menubar; 
    } 

    class SaveAction extends AbstractAction { 
     private static final long serialVersionUID = 1L; 
     public SaveAction() { 
      super("Save"); 
     } 
     @Override 
     public void actionPerformed(ActionEvent ev) { 
      final JFileChooser chooser = new JFileChooser(); 
      if (chooser.showSaveDialog(SimpleEditor.this) != JFileChooser.APPROVE_OPTION) 
       return; 
      final File file = chooser.getSelectedFile(); 
      if (file == null) 
       return; 
      FileWriter writer = null; 
      try { 
       writer = new FileWriter(file); 
       textPane.write(writer); 
      } catch (final IOException ex) { 
       JOptionPane.showMessageDialog(SimpleEditor.this, 
               "File Not Saved", "ERROR", 
               JOptionPane.ERROR_MESSAGE); 
      } finally { 
       if (writer != null) { 
        try { 
         writer.close(); 
        } catch (final IOException x) { 
        } 
       } 
      } 
     } 
    } 
} 

到目前爲止好!這個解決方案唯一的問題是,我無法實現從包含在<span>元素中的文本切換回「正常」文本,即文本不在<span>元素內。 這應該沒什麼大不了的,但不幸的是我無法弄清楚我該如何實現這一點。任何想法都會非常受歡迎!

回答

0

我明白了!它很簡單;-) 要使用<span>元素從樣式文本條目切換回來,我只需要從當前文本的輸入屬性中刪除HTML.Tag.SPAN屬性。

edtKit.getInputAttributes().removeAttribute(HTML.Tag.SPAN); 

所以在示例代碼我在(更新)的問題張貼現在的ActionListener代碼如下所示:這是如下完成

 cbStyleSel.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      e.getSource(); 
      @SuppressWarnings("unchecked") 
      final JComboBox<CondStyle> cboStyleSel = (JComboBox<CondStyle>) e.getSource(); 
      final String selItem = (String) cboStyleSel.getSelectedItem(); 
      if (selItem.equals("default")) { 
       edtKit.getInputAttributes().removeAttribute(HTML.Tag.SPAN); 
      } else { 
       final MutableAttributeSet divAttributes = new SimpleAttributeSet(); 
       divAttributes.addAttribute(HTML.Attribute.CLASS, selItem.substring(1)); 
       final MutableAttributeSet tagAttributes = new SimpleAttributeSet(); 
       tagAttributes.addAttribute(HTML.Tag.SPAN, divAttributes); 
       textPane.setCharacterAttributes(tagAttributes, false); 
      } 
      textPane.requestFocusInWindow(); 
     } 
    }); 

我貼過關於多個問題我實現該功能的問題(請參閱herehere),但沒有得到答案。也許這個問題太瑣碎了;-)

相關問題