📄 webpad.java
字号:
menuItem = menu.add(aboutAction); menuItem.addMouseListener(mouseHandler); return menu; } // Panel which allows for the enabling and disabling of all the actions. private JPanel createPanel() { textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); textArea.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if(e.isPopupTrigger()) { popup.show(textArea, e.getX(), e.getY()); } } public void mouseReleased(MouseEvent e) { if(e.isPopupTrigger()) { popup.show(textArea, e.getX(), e.getY()); } } }); JPanel panel = new JPanel(new BorderLayout()); panel.setPreferredSize(new Dimension(450, 200)); panel.add(scrollPane, BorderLayout.CENTER); return panel; } // Creates the status bar. private JLabel createStatusBar() { status = new JLabel("Ready..."); status.setBorder(BorderFactory.createEtchedBorder()); return status; } /* * This method acts as the Action handler delegate for all the actions. * The Cut, Copy and Paste Actions operate on the JTextArea. */ public void actionPerformed(ActionEvent evt) { String command = evt.getActionCommand(); // Compare the action command to the known actions. if (command.equals(aboutAction.getActionCommand())) { // The about action was invoked JOptionPane.showMessageDialog(this, aboutAction.getLongDescription(), aboutAction.getShortDescription(), JOptionPane.INFORMATION_MESSAGE); } else if (command.equals(cutAction.getActionCommand())) { ClipboardHandler.toClipboard(textArea.getSelectedText()); textArea.replaceSelection(""); } else if (command.equals(copyAction.getActionCommand())) { ClipboardHandler.toClipboard(textArea.getSelectedText()); } else if (command.equals(pasteAction.getActionCommand())) { String txt = ClipboardHandler.fromClipboard(); textArea.replaceSelection(txt); } else if (command.equals(helpAction.getActionCommand())) { HelpHandler.showHelp(evt); } else if (command.equals(exitAction.getActionCommand())) { saveScratch(textArea.getText()); System.exit(0); } else if (command.equals(openAction.getActionCommand())) { String t = FileHandler.open(); if (t != null) textArea.setText(t); } else if (command.equals(saveAction.getActionCommand())) { FileHandler.save(textArea.getText()); } else if (command.equals(saveAsAction.getActionCommand())) { FileHandler.saveAs(textArea.getText()); } else if (command.equals(printAction.getActionCommand())) { FileHandler.print(textArea); } else if (command.equals(publishAction.getActionCommand())) { WebHandler.publish(textArea.getText()); } else if (command.equals(showAction.getActionCommand())) { WebHandler.show(); } }/* * This method will check to see if there is anything stored in * PersistenceStorage. If so, it will read it. If not, we will * just create a new Persistence Storage. This is implemented * mainly for the Webpad app to remember/recall what was * displayed in the TextArea before the App was exited */ private void initPersistence() { long maxSize = 8192; long fileSize = 0; boolean persExists = true; URL url = null; BufferedReader br = null; String fName = "README"; try { ps = (PersistenceService)ServiceManager.lookup("javax.jnlp.PersistenceService"); bs = (BasicService)ServiceManager.lookup("javax.jnlp.BasicService"); } catch (UnavailableServiceException e) { ps = null; bs = null; } if (ps != null && bs != null) { /* ** When the app is executed for the first time, there will be no ** Persistence Storage existing, so an Exception will be thrown. **/ try { URL codebase = bs.getCodeBase(); url = new URL(codebase.toString() + "perstest"); fc = ps.get(url); maxSize = fc.getMaxLength(); fileSize = fc.getLength(); } catch (Exception ee) { System.out.println("Exception reading existing Persistence Storage " + url.toString() +": " + ee); /* ** There will no persistence storage when the app is executed for ** for the first time on the client machine. */ persExists = false; } try { /* If there is no persistence storage, open the default README file. ** If persistence exists, but there was nothing in the scratch pad ** still open the default README file. Deliberately checking for ** length > 1, because in some cases it is writing one byte to the ** file even though it is a null character. Maybe due to some bug. ** If we had some valid contents in scratch pad, then retrieve ** it from Persistence storage and display it. */ if (persExists = true && fileSize > 1) { br = new BufferedReader(new InputStreamReader(fc.getInputStream())); } else { System.out.println("Opening default README file"); br = new BufferedReader(new InputStreamReader(cl.getResource(fName).openStream())); fileSize = 4096; } StringBuffer sb = new StringBuffer((int)fileSize); String line = br.readLine(); while(line != null) { sb.append(line); sb.append("\n"); line = br.readLine(); } textArea.setText(sb.toString()); br.close(); ps.delete(url); } catch (Exception e) { /* ** This exception will be raised when the app is executed ** for the first time in ps.delete. */ System.out.println("Exception thrown when deleting non-existing ps"); } try { ps.create(url, maxSize); fc = ps.get(url); } catch (Exception e) { e.printStackTrace(); } } } /* ** This method will be called upon exit to store whatever is in the ** scratch pad into the Persistence Storage. Whenever the app is started ** next time, the same content will be displayed again. **/ private void saveScratch(String txt) { try { int sizeNeeded = txt.length() * 2; if (sizeNeeded > fc.getMaxLength()) fc.setMaxLength(sizeNeeded); BufferedWriter os = new BufferedWriter(new OutputStreamWriter(fc.getOutputStream(false))); os.write(txt); os.close(); } catch(Exception e) { e.printStackTrace(); } } /** * This adapter is constructed to handle mouse over component events. */ private class MouseHandler extends MouseAdapter { private JLabel label; private String oldMsg; /** * ctor for the adapter. * @param label the JLabel which will recieve value of the * Action.LONG_DESCRIPTION key. */ public MouseHandler(JLabel label) { setLabel(label); oldMsg = label.getText(); } public void setLabel(JLabel label) { this.label = label; } public void mouseEntered(MouseEvent evt) { if (evt.getSource() instanceof AbstractButton) { AbstractButton button = (AbstractButton)evt.getSource(); Action action = button.getAction(); // getAction is new in JDK 1.3 if (action != null) { oldMsg = label.getText(); String message = (String)action.getValue(Action.LONG_DESCRIPTION); label.setText(message); } } } public void mouseExited(MouseEvent evt) { label.setText(oldMsg); } } /** * Main method */ public static void main(String[] args) { WebPad demo = new WebPad(); demo.pack(); demo.setVisible(true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -