// determine which menuItem was invoked and process it
JMenuItem source = (JMenuItem)e.getSource();
if ( applyStyleMenu.isMenuComponent(source) ) {
// apply an existing style to the paragraph at the caret position
String styleName = source.getActionCommand();
Style style = textPane.getStyle(styleName);
textPane.setLogicalStyle(style);
}
if ( source == createItem ) {
// define a new Style and add it to the menus
styleBox.clear();
int response = JOptionPane.showConfirmDialog(this, styleBox,
"Style Editor", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (response == JOptionPane.OK_OPTION &&
styleBox.getStyleName().length() > 0) {
String styleName = styleBox.getStyleName();
Style style = textPane.addStyle(styleName, null);
styleBox.fillStyle(style);
createMenuItems(styleName); // add new Style to the menus
}
}
if ( modifyStyleMenu.isMenuComponent(source) ) {
// redefine a Style (will automatically redraw paragraphs using Style)
String styleName = source.getActionCommand();
Style style = textPane.getStyle(styleName);
styleBox.loadFromStyle(style);
int response = JOptionPane.showConfirmDialog(this, styleBox,
"Style Editor", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (response == JOptionPane.OK_OPTION) styleBox.fillStyle(style);
}