📄 notepad.java
字号:
String[] menuKeys = tokenize(getResourceString("menubar")); for (int i = 0; i < menuKeys.length; i++) { JMenu m = createMenu(menuKeys[i]); if (m != null) { mb.add(m); } } return mb; } /** * Create a menu for the app. By default this pulls the * definition of the menu from the associated resource file. */ protected JMenu createMenu(String key) { String[] itemKeys = tokenize(getResourceString(key)); JMenu menu = new JMenu(getResourceString(key + "Label")); for (int i = 0; i < itemKeys.length; i++) { if (itemKeys[i].equals("-")) { menu.addSeparator(); } else { JMenuItem mi = createMenuItem(itemKeys[i]); menu.add(mi); } } return menu; } // Yarked from JMenu, ideally this would be public. protected PropertyChangeListener createActionChangeListener(JMenuItem b) { return new ActionChangedListener(b); } // Yarked from JMenu, ideally this would be public. private class ActionChangedListener implements PropertyChangeListener { JMenuItem menuItem; ActionChangedListener(JMenuItem mi) { super(); this.menuItem = mi; } public void propertyChange(PropertyChangeEvent e) { String propertyName = e.getPropertyName(); if (e.getPropertyName().equals(Action.NAME)) { String text = (String) e.getNewValue(); menuItem.setText(text); } else if (propertyName.equals("enabled")) { Boolean enabledState = (Boolean) e.getNewValue(); menuItem.setEnabled(enabledState.booleanValue()); } } } private JTextComponent editor; private Hashtable commands; private Hashtable menuItems; private JMenuBar menubar; private JToolBar toolbar; private JComponent status; private JFrame elementTreeFrame; protected ElementTreePanel elementTreePanel; protected FileDialog fileDialog; /** * Listener for the edits on the current document. */ protected UndoableEditListener undoHandler = new UndoHandler(); /** UndoManager that we add edits to. */ protected UndoManager undo = new UndoManager(); /** * Suffix applied to the key used in resource file * lookups for an image. */ public static final String imageSuffix = "Image"; /** * Suffix applied to the key used in resource file * lookups for a label. */ public static final String labelSuffix = "Label"; /** * Suffix applied to the key used in resource file * lookups for an action. */ public static final String actionSuffix = "Action"; /** * Suffix applied to the key used in resource file * lookups for tooltip text. */ public static final String tipSuffix = "Tooltip"; public static final String openAction = "open"; public static final String newAction = "new"; public static final String saveAction = "save"; public static final String exitAction = "exit"; public static final String showElementTreeAction = "showElementTree"; class UndoHandler implements UndoableEditListener { /** * Messaged when the Document has created an edit, the edit is * added to <code>undo</code>, an instance of UndoManager. */ public void undoableEditHappened(UndoableEditEvent e) { undo.addEdit(e.getEdit()); undoAction.update(); redoAction.update(); } } /** * FIXME - I'm not very useful yet */ class StatusBar extends JComponent { public StatusBar() { super(); setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); } public void paint(Graphics g) { super.paint(g); } } // --- action implementations ----------------------------------- private UndoAction undoAction = new UndoAction(); private RedoAction redoAction = new RedoAction(); /** * Actions defined by the Notepad class */ private Action[] defaultActions = { new NewAction(), new OpenAction(), new ExitAction(), new ShowElementTreeAction(), undoAction, redoAction }; class UndoAction extends AbstractAction { public UndoAction() { super("Undo"); setEnabled(false); } public void actionPerformed(ActionEvent e) { try { undo.undo(); } catch (CannotUndoException ex) { System.out.println("Unable to undo: " + ex); ex.printStackTrace(); } update(); redoAction.update(); } protected void update() { if(undo.canUndo()) { setEnabled(true); putValue(Action.NAME, undo.getUndoPresentationName()); } else { setEnabled(false); putValue(Action.NAME, "Undo"); } } } class RedoAction extends AbstractAction { public RedoAction() { super("Redo"); setEnabled(false); } public void actionPerformed(ActionEvent e) { try { undo.redo(); } catch (CannotRedoException ex) { System.out.println("Unable to redo: " + ex); ex.printStackTrace(); } update(); undoAction.update(); } protected void update() { if(undo.canRedo()) { setEnabled(true); putValue(Action.NAME, undo.getRedoPresentationName()); } else { setEnabled(false); putValue(Action.NAME, "Redo"); } } } class OpenAction extends NewAction { OpenAction() { super(openAction); } public void actionPerformed(ActionEvent e) { Frame frame = getFrame(); if (fileDialog == null) { fileDialog = new FileDialog(frame); } fileDialog.setMode(FileDialog.LOAD); fileDialog.show(); String file = fileDialog.getFile(); if (file == null) { return; } String directory = fileDialog.getDirectory(); File f = new File(directory, file); if (f.exists()) { Document oldDoc = getEditor().getDocument(); if(oldDoc != null) oldDoc.removeUndoableEditListener(undoHandler); if (elementTreePanel != null) { elementTreePanel.setEditor(null); } getEditor().setDocument(new PlainDocument()); frame.setTitle(file); Thread loader = new FileLoader(f, editor.getDocument()); loader.start(); } } } class NewAction extends AbstractAction { NewAction() { super(newAction); } NewAction(String nm) { super(nm); } public void actionPerformed(ActionEvent e) { Document oldDoc = getEditor().getDocument(); if(oldDoc != null) oldDoc.removeUndoableEditListener(undoHandler); getEditor().setDocument(new PlainDocument()); getEditor().getDocument().addUndoableEditListener(undoHandler); resetUndoManager(); revalidate(); } } /** * Really lame implementation of an exit command */ class ExitAction extends AbstractAction { ExitAction() { super(exitAction); } public void actionPerformed(ActionEvent e) { System.exit(0); } } /** * Action that brings up a JFrame with a JTree showing the structure * of the document. */ class ShowElementTreeAction extends AbstractAction { ShowElementTreeAction() { super(showElementTreeAction); } ShowElementTreeAction(String nm) { super(nm); } public void actionPerformed(ActionEvent e) { if(elementTreeFrame == null) { // Create a frame containing an instance of // ElementTreePanel. try { String title = resources.getString ("ElementTreeFrameTitle"); elementTreeFrame = new JFrame(title); } catch (MissingResourceException mre) { elementTreeFrame = new JFrame(); } elementTreeFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent weeee) { elementTreeFrame.setVisible(false); } }); Container fContentPane = elementTreeFrame.getContentPane(); fContentPane.setLayout(new BorderLayout()); elementTreePanel = new ElementTreePanel(getEditor()); fContentPane.add(elementTreePanel); elementTreeFrame.pack(); } elementTreeFrame.show(); } } /** * Thread to load a file into the text storage model */ class FileLoader extends Thread { FileLoader(File f, Document doc) { setPriority(4); this.f = f; this.doc = doc; } public void run() { try { // initialize the statusbar status.removeAll(); JProgressBar progress = new JProgressBar(); progress.setMinimum(0); progress.setMaximum((int) f.length()); status.add(progress); status.revalidate(); // try to start reading Reader in = new FileReader(f); char[] buff = new char[4096]; int nch; while ((nch = in.read(buff, 0, buff.length)) != -1) { doc.insertString(doc.getLength(), new String(buff, 0, nch), null); progress.setValue(progress.getValue() + nch); } // we are done... get rid of progressbar doc.addUndoableEditListener(undoHandler); status.removeAll(); status.revalidate(); resetUndoManager(); } catch (IOException e) { System.err.println(e.toString()); } catch (BadLocationException e) { System.err.println(e.getMessage()); } if (elementTreePanel != null) { SwingUtilities.invokeLater(new Runnable() { public void run() { elementTreePanel.setEditor(getEditor()); } }); } } Document doc; File f; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -