/** * Given a number of bytes transferred in a second, describe the speed in kilobytes per second * like "2.24 KB/s". */ public static String speed(int bytesPerSecond) { int i = (bytesPerSecond * 100) / 1024; // Compute the number of hundreadth kilobytes per second if (i == 0) return ""; // Return "" instead of "0.00 KB/s" else if (i < 10) return ("0.0" + i + " KB/s"); // 1 digit "0.09 KB/s" else if (i < 100) return ("0." + i + " KB/s"); // 2 digits "0.99 KB/s" else if (i < 1000) return (Text.start(Number.toString(i), 1) + "." + Text.clip(Number.toString(i), 1, 2) + " KB/s"); // 3 digits "9.99 KB/s" else if (i < 10000) return (Text.start(Number.toString(i), 2) + "." + Text.clip(Number.toString(i), 2, 1) + " KB/s"); // 4 digits "99.9 KB/s" else return commas(Text.chop(Number.toString(i), 2)) + " KB/s"; // 5 or more "999 KB/s" or "1,234 KB/s" }
public void actionPerformed(ActionEvent a) { try { // Get the selected text, and the text before and after it String s = component.getText(); int i = component.getSelectionStart(); // The index where the selection starts int size = component.getSelectionEnd() - i; // The number of selected characters String before = Text.start(s, i); String selected = Text.clip(s, i, size); String after = Text.after(s, i + size); // The user clicked "Cut" on the menu if (a.getActionCommand().equals("Cut")) { Clipboard.copy(selected); component.setText(before + after); // Remove the selected text component.setCaretPosition(i); // The user clicked "Copy" on the menu } else if (a.getActionCommand().equals("Copy")) { Clipboard.copy(selected); // The user clicked "Paste" on the menu } else if (a.getActionCommand().equals("Paste")) { String clipboard = Clipboard.paste(); // Insert text from the clipboard component.setText(before + clipboard + after); component.setCaretPosition(i + clipboard.length()); // The user clicked "Delete" on the menu } else if (a.getActionCommand().equals("Delete")) { component.setText(before + after); // Remove the selected text component.setCaretPosition(i); // The user clicked "Select All" on the menu } else if (a.getActionCommand().equals("Select All")) { component.selectAll(); } } catch (Exception e) { Mistake.grab(e); } }