Ejemplo n.º 1
0
    // When we get the trigger event, if some rows are selected, show the menu
    private void show(MouseEvent e) {
      if (e.isPopupTrigger()) { // Only do something if this is the correct event

        // Disable all the menu items, we'll enable those that can work next
        cutItem.setEnabled(false);
        copyItem.setEnabled(false);
        pasteItem.setEnabled(false);
        deleteItem.setEnabled(false);
        selectAllItem.setEnabled(false);

        // Find out if our text component is editable or read-only, and if it has some selected text
        boolean editable = component.isEditable();
        boolean selection = Text.hasText(component.getSelectedText());

        // Enable Cut and Delete if the text component is editable and has selected text
        if (editable && selection) {
          cutItem.setEnabled(true);
          deleteItem.setEnabled(true);
        }

        // Enable Copy if the text component has selected text
        if (selection) copyItem.setEnabled(true);

        // Enable Paste if the text component is editable and there's text on the clipboard
        if (editable && Clipboard.hasText()) pasteItem.setEnabled(true);

        // Enable Select All of the text component has text
        if (Text.hasText(component.getText())) selectAllItem.setEnabled(true);

        // Show the menu to the user
        menu.show(e.getComponent(), e.getX(), e.getY());
      }
    }
Ejemplo n.º 2
0
 /** Insert commas in the given String, turn "1234" into "1,234". */
 public static String commas(String s) {
   String done = "";
   while (s.length() > 3) { // Loop, chopping groups of 3 characters off the end of s
     done = "," + Text.end(s, 3) + done;
     s = Text.chop(s, 3);
   }
   return s + done;
 }
Ejemplo n.º 3
0
 /**
  * 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"
 }
Ejemplo n.º 4
0
    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);
      }
    }