📄 htmleditor.java
字号:
public void actionPerformed(ActionEvent e) { doZoom(false); } }; /** * Listener for the edits on the current document. */ protected UndoableEditListener undoHandler = new UndoHandler(); /** UndoManager that we add edits to. */ protected UndoManager undo = new UndoManager(); public UndoAction undoAction = new UndoAction(); public RedoAction redoAction = new RedoAction(); JButton jAlignActionB = new JButton(); public JToolBar editToolbar = new JToolBar(); JButton lAlignActionB = new JButton(); JButton olActionB = new JButton(); JButton linkActionB = new JButton(); JButton italicActionB = new JButton(); JButton propsActionB = new JButton(); JButton imageActionB = new JButton(); public final int T_P = 0; public final int T_H1 = 1; public final int T_H2 = 2; public final int T_H3 = 3; public final int T_H4 = 4; public final int T_H5 = 5; public final int T_H6 = 6; public final int T_PRE = 7; //private final int T_ADDRESS = 8; public final int T_BLOCKQ = 8; //9; String[] elementTypes = { Local.getString("Paragraph"), Local.getString("Header") + " 1", Local.getString("Header") + " 2", Local.getString("Header") + " 3", Local.getString("Header") + " 4", Local.getString("Header") + " 5", Local.getString("Header") + " 6", Local.getString("Preformatted"), //"Address", Local.getString("Blockquote")}; public JComboBox blockCB = new JComboBox(elementTypes); boolean blockCBEventsLock = false; public final int I_NORMAL = 0; public final int I_EM = 1; public final int I_STRONG = 2; public final int I_CODE = 3; public final int I_CITE = 4; public final int I_SUPERSCRIPT = 5; public final int I_SUBSCRIPT = 6; public final int I_CUSTOM = 7; String[] inlineTypes = { Local.getString("Normal"), Local.getString("Emphasis"), Local.getString("Strong"), Local.getString("Code"), Local.getString("Cite"), Local.getString("Superscript"), Local.getString("Subscript"), Local.getString("Custom style") + "..." }; public JComboBox inlineCB = new JComboBox(inlineTypes); boolean inlineCBEventsLock = false; JButton boldActionB = new JButton(); JButton ulActionB = new JButton(); JButton rAlignActionB = new JButton(); JButton tableActionB = new JButton(); JButton cAlignActionB = new JButton(); JButton underActionB = new JButton(); BorderLayout borderLayout1 = new BorderLayout(); JPopupMenu defaultPopupMenu = new JPopupMenu(); //JPopupMenu tablePopupMenu = new JPopupMenu(); JMenuItem jMenuItemUndo = new JMenuItem(undoAction); JMenuItem jMenuItemRedo = new JMenuItem(redoAction); JMenuItem jMenuItemCut = new JMenuItem(cutAction); JMenuItem jMenuItemCopy = new JMenuItem(copyAction); JMenuItem jMenuItemPaste = new JMenuItem(pasteAction); JMenuItem jMenuItemProp = new JMenuItem(propsAction); JMenuItem jMenuItemInsCell = new JMenuItem(insertTableCellAction); JMenuItem jMenuItemInsRow = new JMenuItem(insertTableRowAction); int currentCaret = 0; int currentFontSize = 4; JButton brActionB = new JButton(); JButton hrActionB = new JButton(); JButton insCharActionB = new JButton(); public HTMLEditor() { try { jbInit(); } catch (Exception ex) { ex.printStackTrace(); } } void jbInit() throws Exception { cutAction.putValue( Action.SMALL_ICON, new ImageIcon(cl.getResource("resources/icons/cut.png"))); cutAction.putValue( Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK)); cutAction.putValue(Action.NAME, Local.getString("Cut")); cutAction.putValue(Action.SHORT_DESCRIPTION, Local.getString("Cut")); copyAction.putValue( Action.SMALL_ICON, new ImageIcon(cl.getResource("resources/icons/copy.png"))); copyAction.putValue( Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK)); copyAction.putValue(Action.NAME, Local.getString("Copy")); copyAction.putValue(Action.SHORT_DESCRIPTION, Local.getString("Copy")); pasteAction.putValue( Action.SMALL_ICON, new ImageIcon(cl.getResource("resources/icons/paste.png"))); pasteAction.putValue( Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_MASK)); pasteAction.putValue(Action.NAME, Local.getString("Paste")); pasteAction.putValue( Action.SHORT_DESCRIPTION, Local.getString("Paste")); stylePasteAction.putValue( Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke( KeyEvent.VK_V, KeyEvent.CTRL_MASK + KeyEvent.SHIFT_MASK)); stylePasteAction.putValue( Action.NAME, Local.getString("Paste special")); stylePasteAction.putValue( Action.SHORT_DESCRIPTION, Local.getString("Paste special")); selectAllAction.putValue( Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK)); boldAction.putValue( Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_B, KeyEvent.CTRL_MASK)); italicAction.putValue( Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_I, KeyEvent.CTRL_MASK)); underAction.putValue( Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_U, KeyEvent.CTRL_MASK)); breakAction.putValue( Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_MASK)); breakAction.putValue(Action.NAME, Local.getString("Insert Break")); breakAction.putValue( Action.SHORT_DESCRIPTION, Local.getString("Insert Break")); findAction.putValue( Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_MASK)); document = (HTMLDocument) editorKit.createDefaultDocument(); border1 = BorderFactory.createEtchedBorder( Color.white, new Color(142, 142, 142)); border2 = BorderFactory.createBevelBorder( BevelBorder.LOWERED, Color.white, Color.white, new Color(142, 142, 142), new Color(99, 99, 99)); this.setLayout(borderLayout1); /* * jAlignActionB.setIcon( new * ImageIcon(net.sf.memoranda.ui.htmleditor.HTMLEditor.class.getResource("resources/icons/alignjust.png"))); * jAlignActionB.setMaximumSize(new Dimension(22, 22)); * jAlignActionB.setMinimumSize(new Dimension(22, 22)); * jAlignActionB.setPreferredSize(new Dimension(22, 22)); * jAlignActionB.setFocusable(false); */ editor.addCaretListener(new CaretListener() { public void caretUpdate(CaretEvent e) { editor_caretUpdate(e); } }); editor.setEditorKit(editorKit); editorKit.setDefaultCursor(new Cursor(Cursor.TEXT_CURSOR)); editor.setDocument(document); document.addUndoableEditListener(undoHandler); this.setPreferredSize(new Dimension(520, 57)); editToolbar.setRequestFocusEnabled(false); editToolbar.setToolTipText(""); boldActionB.setAction(boldAction); boldActionB.setBorder(border1); boldActionB.setMaximumSize(new Dimension(22, 22)); boldActionB.setMinimumSize(new Dimension(22, 22)); boldActionB.setPreferredSize(new Dimension(22, 22)); boldActionB.setBorderPainted(false); boldActionB.setFocusable(false); boldActionB.setText(""); italicActionB.setAction(italicAction); italicActionB.setBorder(border1); italicActionB.setMaximumSize(new Dimension(22, 22)); italicActionB.setMinimumSize(new Dimension(22, 22)); italicActionB.setPreferredSize(new Dimension(22, 22)); italicActionB.setBorderPainted(false); italicActionB.setFocusable(false); italicActionB.setText(""); underActionB.setAction(underAction); underActionB.setBorder(border1); underActionB.setMaximumSize(new Dimension(22, 22)); underActionB.setMinimumSize(new Dimension(22, 22)); underActionB.setPreferredSize(new Dimension(22, 22)); underActionB.setBorderPainted(false); underActionB.setFocusable(false); underActionB.setText(""); lAlignActionB.setAction(lAlignAction); lAlignActionB.setMaximumSize(new Dimension(22, 22)); lAlignActionB.setMinimumSize(new Dimension(22, 22)); lAlignActionB.setPreferredSize(new Dimension(22, 22)); lAlignActionB.setBorderPainted(false); lAlignActionB.setFocusable(false); lAlignActionB.setText(""); rAlignActionB.setAction(rAlignAction); rAlignActionB.setFocusable(false); rAlignActionB.setPreferredSize(new Dimension(22, 22)); rAlignActionB.setBorderPainted(false); rAlignActionB.setMinimumSize(new Dimension(22, 22)); rAlignActionB.setMaximumSize(new Dimension(22, 22)); rAlignActionB.setText(""); cAlignActionB.setAction(cAlignAction); cAlignActionB.setMaximumSize(new Dimension(22, 22)); cAlignActionB.setMinimumSize(new Dimension(22, 22)); cAlignActionB.setPreferredSize(new Dimension(22, 22)); cAlignActionB.setBorderPainted(false); cAlignActionB.setFocusable(false); cAlignActionB.setText(""); ulActionB.setAction(ulAction); ulActionB.setMaximumSize(new Dimension(22, 22)); ulActionB.setMinimumSize(new Dimension(22, 22)); ulActionB.setPreferredSize(new Dimension(22, 22)); ulActionB.setBorderPainted(false); ulActionB.setFocusable(false); ulActionB.setText(""); olActionB.setAction(olAction); olActionB.setMaximumSize(new Dimension(22, 22)); olActionB.setMinimumSize(new Dimension(22, 22)); olActionB.setPreferredSize(new Dimension(22, 22)); olActionB.setBorderPainted(false); olActionB.setFocusable(false); olActionB.setText(""); linkActionB.setAction(linkAction); linkActionB.setMaximumSize(new Dimension(22, 22)); linkActionB.setMinimumSize(new Dimension(22, 22)); linkActionB.setPreferredSize(new Dimension(22, 22)); linkActionB.setBorderPainted(false); linkActionB.setFocusable(false); linkActionB.setText(""); propsActionB.setAction(propsAction); propsActionB.setFocusable(false); propsActionB.setPreferredSize(new Dimension(22, 22)); propsActionB.setBorderPainted(false); propsActionB.setMinimumSize(new Dimension(22, 22)); propsActionB.setMaximumSize(new Dimension(22, 22)); propsActionB.setText(""); imageActionB.setAction(imageAction); imageActionB.setMaximumSize(new Dimension(22, 22)); imageActionB.setMinimumSize(new Dimension(22, 22)); imageActionB.setPreferredSize(new Dimension(22, 22)); imageActionB.setBorderPainted(false); imageActionB.setFocusable(false); imageActionB.setText(""); tableActionB.setAction(tableAction); tableActionB.setFocusable(false); tableActionB.setPreferredSize(new Dimension(22, 22)); tableActionB.setBorderPainted(false); tableActionB.setMinimumSize(new Dimension(22, 22)); tableActionB.setMaximumSize(new Dimension(22, 22)); tableActionB.setText(""); brActionB.setAction(breakAction); brActionB.setFocusable(false); brActionB.setBorderPainted(false); brActionB.setPreferredSize(new Dimension(22, 22)); brActionB.setMinimumSize(new Dimension(22, 22)); brActionB.setMaximumSize(new Dimension(22, 22)); brActionB.setText(""); hrActionB.setAction(insertHRAction); hrActionB.setMaximumSize(new Dimension(22, 22)); hrActionB.setMinimumSize(new Dimension(22, 22)); hrActionB.setPreferredSize(new Dimension(22, 22)); hrActionB.setBorderPainted(false); hrActionB.setFocusable(false); hrActionB.setText(""); insCharActionB.setAction(insCharAction); insCharActionB.setBorder(border1); insCharActionB.setMaximumSize(new Dimension(22, 22)); insCharActionB.setMinimumSize(new Dimension(22, 22)); insCharActionB.setPreferredSize(new Dimension(22, 22)); insCharActionB.setBorderPainted(false); insCharActionB.setFocusable(false); insCharActionB.setText(""); blockCB.setBackground(new Color(230, 230, 230)); blockCB.setMaximumRowCount(12); blockCB.setFont(new java.awt.Font("Dialog", 1, 10)); blockCB.setMaximumSize(new Dimension(120, 22)); blockCB.setMinimumSize(new Dimension(60, 22)); blockCB.setPreferredSize(new Dimension(79, 22)); blockCB.setFocusable(false); blockCB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { blockCB_actionPerformed(e); } }); inlineCB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { inlineCB_actionPerformed(e); } }); inlineCB.setFocusable(false); inlineCB.setPreferredSize(new Dimension(79, 22)); inlineCB.setMinimumSize(new Dimension(60, 22)); inlineCB.setMaximumSize(new Dimension(120, 22)); inlineCB.setFont(new java.awt.Font("Dialog", 1, 10)); inlineCB.setMaximumRowCount(12); inlineCB.setBackground(new Color(230, 230, 230)); this.add(jScrollPane1, BorderLayout.CENTER); this.add(editToolbar, BorderLayout.NORTH); editToolbar.add(propsActionB, null); editToolbar.addSeparator(); editToolbar.add(blockCB, null); editToolbar.addSeparator(); editToolbar.add(inlineCB, null); editToolbar.addSeparator(); editToolbar.add(boldActionB, null); editToolbar.add(italicActionB, null); editToolbar.add(underActionB, null); editToolbar.addSeparator(); editToolbar.add(ulActionB, null); editToolbar.add(olActionB, null); editToolbar.addSeparator(); editToolbar.add(lAlignActionB, null); editToolbar.add(cAlignActionB, null); editToolbar.add(rAlignActionB, null); editToolbar.addSeparator(); editToolbar.add(imageActionB, null); editToolbar.add(tableActionB, null); editToolbar.add(linkActionB, null); editToolbar.addSeparator(); editToolbar.add(hrActionB, null); editToolbar.add(brActionB, null); editToolbar.add(insCharActionB, null); jScrollPane1.getViewport().add(editor, null); toolsPanel.setTabPlacement(JTabbedPane.BOTTOM); toolsPanel.setFont(new Font("Dialog", 1, 10)); // editToolbar.add(jAlignActionB, null); /* KEY ACTIONS */ /* * editor.getKeymap().addActionForKeyStroke( * KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_MASK), */ editor.getKeymap().removeKeyStrokeBinding( KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)); editor.getKeymap().addActionForKeyStroke( KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), new ParaBreakAction()); /* * editor.getKeymap().addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_B, * KeyEvent.CTRL_MASK), boldAction); * editor.getKeymap().addActionForKeyStroke( * KeyStroke.getKeyStroke(KeyEvent.VK_I, KeyEvent.CTRL_MASK), * italicAction); editor.getKeymap().addActionForKeyStroke( * KeyStroke.getKeyStroke(KeyEvent.VK_U, KeyEvent.CTRL_MASK), */ /* * editor.getKeymap().addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_Z, * KeyEvent.CTRL_MASK), undoAction); * editor.getKeymap().addActionForKeyStroke( * KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_MASK + * KeyEvent.SHIFT_MASK), redoAction); * editor.getKeymap().addActionForKeyStroke( * KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_MASK + * KeyEvent.SHIFT_MASK), insertTableCellAction);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -