FileDocCategorySizeDatePackage
StyleFrame.javaAPI DocExample4160Mon Nov 09 12:45:52 GMT 1998None

StyleFrame

public class StyleFrame extends JFrame

Fields Summary
private StyleBox
styleBox
private JTextPane
textPane
private StylishDocument
doc
private JMenu
styleMenu
private JMenu
setStyleMenu
private JMenu
modifyStyleMenu
private Hashtable
styleHash
Constructors Summary
public StyleFrame()

    doc = new StylishDocument();

    textPane = new JTextPane(doc);
    setContentPane(new JScrollPane(textPane));

    JMenuBar menuBar = createMenuBar();
    setJMenuBar(menuBar);

    setSize(400, 300);
  
Methods Summary
protected voidaddStyleToMenus(javax.swing.text.Style newStyle)

    String styleName = newStyle.getName();

    // Add a menu item to the "Set Style" menu
    JMenuItem newItem = new JMenuItem(styleName);
    setStyleMenu.add(newItem);

    newItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        textPane.setLogicalStyle(newStyle);
      }
    });

    // Add a menu item to the "Modify Style" menu
    newItem = new JMenuItem(styleName);
    modifyStyleMenu.add(newItem);

    newItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) {
        styleBox.loadFromStyle(newStyle);
        showStyleDialog();
      }
    });
  
protected javax.swing.JMenuBarcreateMenuBar()

    JMenuBar bar = new JMenuBar();

    // Add a file/exit menu
    JMenu fileMenu = new JMenu("File");
    JMenuItem exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) { System.exit(0); }
    });
    fileMenu.add(exitItem);
    bar.add(fileMenu);

    // Add a style menu-the "Create Style" option will call our showStyleDialog()
    // method (as will "Modify Style" menu items)
    styleMenu = new JMenu("Style");
    JMenuItem createItem = new JMenuItem("Create Style");
    createItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ev) { showStyleDialog(); }
    });

    setStyleMenu = new JMenu("Set Style");
    modifyStyleMenu = new JMenu("Modify Style");
    styleMenu.add(createItem);
    styleMenu.add(setStyleMenu);
    styleMenu.add(modifyStyleMenu);
    bar.add(styleMenu);

    // Add the default style to our menus
    addStyleToMenus(doc.getStyle(StyleContext.DEFAULT_STYLE));
    return bar;
  
public static voidmain(java.lang.String[] args)

    JFrame frame = new StyleFrame();
    frame.addWindowListener(new BasicWindowMonitor());
    frame.setVisible(true);
  
protected voidshowStyleDialog()

    String[] options = {"OK", "Cancel"};

    int opt = JOptionPane.showOptionDialog(this, styleBox,
      "Style Editor", JOptionPane.DEFAULT_OPTION,
      JOptionPane.PLAIN_MESSAGE, null, options, options[0]);

    if (opt == 0) { // "OK" pressed

      String name = styleBox.getStyleName();
      // Only do anything if the user named the style
      if (name != null) {
        // See if the style already exists, in which case this
        // is a change, not a creation
        Style oldStyle = doc.getStyle(name);
        if (oldStyle != null) {

          // Redefine the Style and tell our document to
          // repaint all Elements using this Style
          styleBox.fillStyle(oldStyle);
          doc.styleUpdated(oldStyle);
        }
        else {
          // A new style has been specified. We'll add it to the document . . .
          Style newStyle = doc.addStyle(name, null);

          // Now fill the style with the values entered in the dialog
          styleBox.fillStyle(newStyle);

          // Add this Style so we can use it
          addStyleToMenus(newStyle);
        }
      }
    }
    styleBox.clear();