1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| // new JTextpane
m_editor = new JTextPane();
m_kit = new HTMLEditorKit();
m_editor.setEditorKit(m_kit);
m_editor.setAutoscrolls(true);
m_editor.setCursor(new Cursor(Cursor.TEXT_CURSOR));
// Bind some standard actions
InputMap inputMap = m_editor.getInputMap();
// Set the Bold action
Action a = new HTMLEditorKit.BoldAction() ;
m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_B, Event.CTRL_MASK), "Bold");
m_editor.getActionMap().put("Bold", a);
// Set the Italic action
a = new HTMLEditorKit.ItalicAction() ;
m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_I, Event.CTRL_MASK), "Italic");
m_editor.getActionMap().put("Italic", a);
// Set the Underline action
a = new HTMLEditorKit.UnderlineAction() ;
m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_U, Event.CTRL_MASK), "Underline");
m_editor.getActionMap().put("Underline", a);
// Set the increase font size action
a = new HTMLEditorKit.FontSizeAction("10", 10) ;
m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_1, Event.CTRL_MASK), "Inc10");
m_editor.getActionMap().put("Inc10", a);
// Set the increase font size action
a = new HTMLEditorKit.FontSizeAction("12", 12) ;
m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_2, Event.CTRL_MASK), "Inc12");
m_editor.getActionMap().put("Inc12", a);
// Set the increase font size action
a = new HTMLEditorKit.FontSizeAction("14", 14) ;
m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_3, Event.CTRL_MASK), "Inc14");
m_editor.getActionMap().put("Inc14", a);
// Set the increase font size action
a = new HTMLEditorKit.FontSizeAction("20", 20) ;
m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_4, Event.CTRL_MASK), "Inc20");
m_editor.getActionMap().put("Inc20", a);
// Set the increase font size action
a = new HTMLEditorKit.FontSizeAction("24",24) ;
m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_5, Event.CTRL_MASK), "Inc24");
m_editor.getActionMap().put("Inc24", a);
// Set the increase font size action
a = new HTMLEditorKit.FontSizeAction("30",30) ;
m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_6, Event.CTRL_MASK), "Inc30");
m_editor.getActionMap().put("Inc30", a);
// Set the SansSerif font action
a = new HTMLEditorKit.FontFamilyAction("SansSerif","SansSerif") ;
m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK), "SansSerif");
m_editor.getActionMap().put("SansSerif", a);
// Set the Copy action
a = new HTMLEditorKit.CopyAction() ;
m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK), "Copy");
m_editor.getActionMap().put("Copy", a);
// Set the Paste action
a = new HTMLEditorKit.PasteAction() ;
m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK), "Paste");
m_editor.getActionMap().put("Paste", a);
/*
// Set the Upper
a = new HTMLEditorKit.PasteAction() ;
m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_M, Event.CTRL_MASK), "Upper");
m_editor.getActionMap().put("Upper", a);
*/
Action actionListener = new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
JTextPane jtp = (JTextPane) actionEvent.getSource();
iStartSelection = jtp.getSelectionStart() ;
iEndSelection = jtp.getSelectionEnd() ;
String sText = "" ;
System.out.println("Sel="+iStartSelection+':'+iEndSelection);
try{
sText = jtp.getDocument().getText(0,jtp.getDocument().getLength());
}
catch (Exception e) { }
String sSel = jtp.getSelectedText().toUpperCase() ;
String s = sText.substring(0,iStartSelection)
+ sText.substring(iEndSelection);
// Insert content
StyledDocument document = new DefaultStyledDocument();
try {
Style style = (Style) document.getStyle(StyleContext.DEFAULT_STYLE);
jtp.getDocument().remove(iStartSelection,iEndSelection-iStartSelection);
jtp.getDocument().insertString(iStartSelection, sSel, style);
try{
sText = jtp.getDocument().getText(0,jtp.getDocument().getLength());
System.out.println("NewText="+sText);
}
catch (Exception e) { }
} catch (BadLocationException badLocationException) {
System.err.println("Oops");
}
System.out.println("New="+s);
}
};
m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_M, Event.CTRL_MASK), "Upper");
m_editor.getActionMap().put("Upper", actionListener);
JScrollPane ps = new JScrollPane(m_editor);
ps.setHorizontalScrollBarPolicy(ps.HORIZONTAL_SCROLLBAR_AS_NEEDED);
ps.setVerticalScrollBarPolicy(ps.VERTICAL_SCROLLBAR_AS_NEEDED);
ps.setVisible(true);
add(ps); |
Partager