FileDocCategorySizeDatePackage
SimpleEditor.javaAPI DocExample7353Mon Nov 09 12:45:52 GMT 1998None

SimpleEditor

public class SimpleEditor extends JFrame

Fields Summary
private Action
openAction
private Action
saveAction
private JTextComponent
textComp
private Hashtable
actionHash
Constructors Summary
public SimpleEditor()

    super("Swing Editor");
    textComp = createTextComponent();
    hashDefaultActions();
    makeActionsPretty();
    updateKeymap();

    Container content = getContentPane();
    content.add(textComp, BorderLayout.CENTER);
    content.add(createToolBar(), BorderLayout.NORTH);

    setJMenuBar(createMenuBar());

    setSize(320, 240);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent ev) { System.exit(0); }
    });
  
Methods Summary
protected javax.swing.JMenuBarcreateMenuBar()

    JMenuBar menubar = new JMenuBar();
    JMenu file = new JMenu("File");
    JMenu edit = new JMenu("Edit");
    menubar.add(file);
    menubar.add(edit);

    file.add(getOpenAction());
    file.add(getSaveAction());
    file.add(new ExitAction());
    edit.add(getHashedAction(DefaultEditorKit.cutAction));
    edit.add(getHashedAction(DefaultEditorKit.copyAction));
    edit.add(getHashedAction(DefaultEditorKit.pasteAction));
    edit.add(getHashedAction(DefaultEditorKit.selectAllAction));
    return menubar;
  
protected javax.swing.text.JTextComponentcreateTextComponent()

    JTextArea ta = new JTextArea();
    ta.setLineWrap(true);
    return ta;
  
protected javax.swing.JToolBarcreateToolBar()

    JToolBar bar = new JToolBar();

    // Add simple actions for opening & saving
    bar.add(getOpenAction()).setText("");
    bar.add(getSaveAction()).setText("");
    bar.addSeparator();

    // Add cut/copy/paste buttons
    bar.add(getHashedAction(DefaultEditorKit.cutAction)).setText("");
    bar.add(getHashedAction(DefaultEditorKit.copyAction)).setText("");
    bar.add(getHashedAction(DefaultEditorKit.pasteAction)).setText("");
    return bar;
  
protected javax.swing.ActiongetHashedAction(java.lang.String name)

    return (Action)actionHash.get(name);
  
protected javax.swing.ActiongetOpenAction()

 return openAction; 
protected javax.swing.ActiongetSaveAction()

 return saveAction; 
protected javax.swing.text.JTextComponentgetTextComponent()

 return textComp; 
protected voidhashDefaultActions()

    Action[] actions = textComp.getActions();
    for (int i=0; i<actions.length; i++) {
      String name = (String)actions[i].getValue(Action.NAME);
      actionHash.put(name, actions[i]);
    }
  
public static voidmain(java.lang.String[] args)

    SimpleEditor editor = new SimpleEditor();
    editor.addWindowListener(new BasicWindowMonitor());
    editor.setVisible(true);
  
protected voidmakeActionsPretty()

    Action a;
    a = getHashedAction(DefaultEditorKit.cutAction);
    a.putValue(Action.SMALL_ICON, new ImageIcon("icons/cut.gif"));
    a.putValue(Action.NAME, "Cut");

    a = getHashedAction(DefaultEditorKit.copyAction);
    a.putValue(Action.SMALL_ICON, new ImageIcon("icons/copy.gif"));
    a.putValue(Action.NAME, "Copy");

    a = getHashedAction(DefaultEditorKit.pasteAction);
    a.putValue(Action.SMALL_ICON, new ImageIcon("icons/paste.gif"));
    a.putValue(Action.NAME, "Paste");

    a = getHashedAction(DefaultEditorKit.selectAllAction);
    a.putValue(Action.NAME, "Select All");
  
protected voidupdateKeymap()


    // Create a new child Keymap
    Keymap map = JTextComponent.addKeymap("NextPrevMap",
      textComp.getKeymap());

    // Define the keystrokes to be added
    KeyStroke next = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,
      InputEvent.CTRL_MASK, false);
    KeyStroke prev = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,
      InputEvent.CTRL_MASK, false);
    KeyStroke selNext = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,
      InputEvent.CTRL_MASK|InputEvent.SHIFT_MASK, false);
    KeyStroke selPrev = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,
      InputEvent.CTRL_MASK|InputEvent.SHIFT_MASK, false);

    // Add the new mappings used DefaultEditorKit actions
    map.addActionForKeyStroke(next, getHashedAction(
      DefaultEditorKit.nextWordAction));
    map.addActionForKeyStroke(prev, getHashedAction(
      DefaultEditorKit.previousWordAction));
    map.addActionForKeyStroke(selNext, getHashedAction(
      DefaultEditorKit.selectionNextWordAction));
    map.addActionForKeyStroke(selPrev, getHashedAction(
      DefaultEditorKit.selectionPreviousWordAction));

    // Set the Keymap for the text component
    textComp.setKeymap(map);