📄 htmleditor.java
字号:
if (menu != null)
menuBar.add(menu);
// Add the alignment menu
menu = buildMenu(Msg.getMsg(Env.getCtx(), "Align"), alignMenu, actions);
if (menu != null)
menuBar.add(menu);
// Add the HTML menu
menu = buildMenu("HTML", htmlMenu, actions);
if (menu != null)
menuBar.add(menu);
// Add to Button Actions
Action targetAction = (Action)actions.get("font-bold");
bBold.addActionListener(targetAction);
targetAction = (Action)actions.get("font-italic");
bItalic.addActionListener(targetAction);
targetAction = (Action)actions.get("font-underline");
bUnderline.addActionListener(targetAction);
} // createMenuBar
/**
* Build Menu
* @param name name
* @param menuActions menu structure
* @param actions lookup
* @return menu
*/
private JMenu buildMenu(String name, HTMLEditor_MenuAction[] menuActions, Hashtable actions)
{
JMenu menu = new JMenu(name);
for (int i = 0; i < menuActions.length; i++)
{
HTMLEditor_MenuAction item = menuActions[i];
if (item.isSubMenu()) // Recurse to handle a sub menu
{
JMenu subMenu = buildMenu(item.getName(), item.getSubMenus(), actions);
if (subMenu != null)
menu.add(subMenu);
}
else if (item.isAction()) // direct action
{
menu.add(item.getAction());
}
else // find it
{
String actionName = item.getActionName();
Action targetAction = (Action)actions.get(actionName);
// Create the menu item
JMenuItem menuItem = menu.add(item.getName());
if (targetAction != null)
menuItem.addActionListener(targetAction);
else // Action not known - disable the menu item
menuItem.setEnabled(false);
}
} // for all actions
// Return null if nothing was added to the menu.
if (menu.getMenuComponentCount() == 0)
menu = null;
return menu;
} // buildMenu
/**
* Action Listener
* @param e event
*/
public void actionPerformed (ActionEvent e)
{
// log.fine("actionPerformed - Text:" + getHtmlText());
//
if (e.getSource() == bImport)
cmd_import();
else if (e.getSource() == bExport)
cmd_export();
//
else if (e.getActionCommand().equals(ConfirmPanel.A_OK))
{
m_text = editorPane.getText();
dispose();
}
else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL))
dispose();
} // actionPerformed
/**
* Import Text from File
*/
private void cmd_import()
{
JFileChooser jc = new JFileChooser();
jc.setDialogTitle(Msg.getMsg(Env.getCtx(), "Import"));
jc.setDialogType(JFileChooser.OPEN_DIALOG);
jc.setFileSelectionMode(JFileChooser.FILES_ONLY);
//
if (jc.showOpenDialog(this) != JFileChooser.APPROVE_OPTION)
return;
StringBuffer sb = new StringBuffer();
try
{
InputStreamReader in = new InputStreamReader (new FileInputStream (jc.getSelectedFile()));
char[] cbuf = new char[1024];
int count;
while ((count = in.read(cbuf)) > 0)
sb.append(cbuf, 0, count);
in.close();
}
catch (Exception e)
{
log.log(Level.SEVERE, "HTMLEditor.import" + e.getMessage());
return;
}
setHtmlText(sb.toString());
} // cmd_import
/**
* Export Text to File
*/
private void cmd_export()
{
JFileChooser jc = new JFileChooser();
jc.setDialogTitle(Msg.getMsg(Env.getCtx(), "Export"));
jc.setDialogType(JFileChooser.SAVE_DIALOG);
jc.setFileSelectionMode(JFileChooser.FILES_ONLY);
//
if (jc.showSaveDialog(this) != JFileChooser.APPROVE_OPTION)
return;
try
{
EditorKit kit = editorPane.getEditorKit();
OutputStreamWriter writer = new OutputStreamWriter
(new FileOutputStream (jc.getSelectedFile()));
editorPane.write(writer);
writer.flush();
writer.close();
}
catch (Exception e)
{
log.log(Level.SEVERE, "HTMLEditor.export" + e.getMessage());
}
} // cmd_export
/*************************************************************************
* Get Html Text
* @return text
*/
public String getHtmlText()
{
return m_text;
} // getHTMLText
/**
* Set Html Text
* @param htmlText
*/
public void setHtmlText (String htmlText)
{
m_text = htmlText;
editorPane.setText(htmlText);
} // setHTMLText
/**************************************************************************
* Test
* @param args ignored
*/
public static void main (String[] args)
{
Compiere.startupEnvironment(true);
JFrame frame = new JFrame("test");
frame.setVisible(true);
String text = "<html><p>this is a line<br>with <b>bold</> info</html>";
int i = 0;
while (true)
{
HTMLEditor ed = new HTMLEditor (frame, "heading " + ++i, text, true);
text = ed.getHtmlText();
}
} // main
} // HTMLEditor
/******************************************************************************
* HTML Editor Menu Action
*/
class HTMLEditor_MenuAction
{
public HTMLEditor_MenuAction(String name, HTMLEditor_MenuAction[] subMenus)
{
m_name = name;
m_subMenus = subMenus;
}
public HTMLEditor_MenuAction(String name, String actionName)
{
m_name = name;
m_actionName = actionName;
}
public HTMLEditor_MenuAction(String name, Action action)
{
m_name = name;
m_action = action;
}
private String m_name;
private String m_actionName;
private Action m_action;
private HTMLEditor_MenuAction[] m_subMenus;
public boolean isSubMenu()
{
return m_subMenus != null;
}
public boolean isAction()
{
return m_action != null;
}
public String getName()
{
return m_name;
}
public HTMLEditor_MenuAction[] getSubMenus()
{
return m_subMenus;
}
public String getActionName()
{
return m_actionName;
}
public Action getAction()
{
return m_action;
}
} // MenuAction
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -