public void runCalculatorDialog(MouseEvent e) {
    if (calculatorDialog == null) {
      JFrame frame = null;
      Container container = getTopLevelAncestor();

      if (container instanceof JFrame) {
        frame = (JFrame) container;
      }

      calculatorDialog = new JDialog(frame, "Rechner", true);
      calculator = new Calculator();
      calculator.getOKButton().addActionListener(this);
      calculatorDialog.getContentPane().add(calculator);
      calculatorDialog.pack();
    }

    // Set calculator's init value

    Money money = getValue();

    if (money != null) {
      calculator.setValue(money.getValue());
    } else {
      calculator.setValue(0.0);
    }

    // calculate POP (point of presentation)

    Point point = ((JComponent) e.getSource()).getLocationOnScreen();
    int x = point.x + e.getX();
    int y = point.y + e.getY();

    // ensure that it does not exceed the screen limits

    Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension dim = calculatorDialog.getPreferredSize();
    if (x + dim.width >= screenDim.width) {
      x = screenDim.width - dim.width - 1;
    }
    if (y + dim.height >= screenDim.height) {
      y = screenDim.height - dim.height - 1;
    }

    // make it visible at wanted location

    calculatorDialog.setLocation(x, y);
    calculatorDialog.show();
  }
  /** Overload hasFocus. */
  public boolean hasFocus() {
    if (moneyField.hasFocus() || (calculator != null && calculator.hasFocus())) {
      return true;
    }

    return false;
  }
Esempio n. 3
0
  /**
   * This method generates the time series for the methods and puts it in an HTML string. The
   * methods are sorted according to the greatest average time for each method.
   *
   * @param dataBase the database to aggregate the statistics for
   * @return the html representing the series of timings for the method
   */
  @SuppressWarnings({"unchecked", "rawtypes"})
  protected String methodSeries(final IDataBase dataBase) {
    Comparator<Method> comparator =
        new Comparator<Method>() {
          public int compare(Method o1, Method o2) {
            Double o1Average = calculator.averageMethodTime(o1);
            Double o2Average = calculator.averageMethodTime(o2);
            // We want a descending table, i.e. the most expensive at the top
            return o2Average.compareTo(o1Average);
          }
        };
    Set<Method> sortedMethods = new TreeSet<>(comparator);
    List<Method> methods = dataBase.find(Method.class);
    sortedMethods.addAll(methods);

    List<Snapshot<?, ?>> snapshots =
        methods.size() > 0 ? methods.get(0).getSnapshots() : new ArrayList<Snapshot<?, ?>>();
    Element tableElement = tableElement(snapshots);

    for (Method method : sortedMethods) {
      Class<?, ?> klass = (Class<?, ?>) method.getParent();
      String className = klass.getName();
      String methodName = method.getName();

      Element rowElement = addElement(tableElement, "tr", null);
      addElement(rowElement, "td", className);
      addElement(rowElement, "td", methodName);
      addElement(rowElement, "td", Double.toString(calculator.averageMethodTime(method)));
      addElement(rowElement, "td", Double.toString(calculator.averageMethodNetTime(method)));
      addElement(rowElement, "td", Double.toString(calculator.totalMethodTime(method)));
      addElement(rowElement, "td", Double.toString(calculator.totalNetMethodTime(method)));
      addElement(rowElement, "td", Integer.toString(method.getInvocations()));

      Element dataElement = addElement(rowElement, "td", null);
      Element imageElement = addElement(dataElement, "img", null);
      // Add the method series graph for the average and total time for the method
      List<Double> methodSeries = calculator.methodSeries(method);
      String url = buildGraph(IConstants.METHOD_SERIES, method, methodSeries);
      addAttributes(imageElement, new String[] {"src"}, new String[] {url});

      dataElement = addElement(rowElement, "td", null);
      imageElement = addElement(dataElement, "img", null);
      // Add the method change graph, i.e. the change in the average time for the method
      List<Double> methodChangeSeries = calculator.methodChangeSeries(method);
      url = buildGraph(IConstants.METHOD_CHANGE_SERIES, method, methodChangeSeries);
      addAttributes(imageElement, new String[] {"src"}, new String[] {url});
    }

    Document document = tableElement.getDocument();
    return prettyPrint(document);
  }
  public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();

    if (calculator != null && source == calculator.getOKButton()) {
      calculatorDialog.setVisible(false);
      setValue(new Money(calculator.convertOutput(), moneyField.getValue().getCurrency()));
    } else if (source == copyItem) {
      Clipboard clipboard = getToolkit().getSystemClipboard();
      Money value = getValue();

      if (value == null) {
        getToolkit().beep();
        return;
      }

      StringSelection contents = new StringSelection(value.toString());
      clipboard.setContents(contents, defaultClipboardOwner);
    } else if (source == pasteItem) {
      Clipboard clipboard = getToolkit().getSystemClipboard();
      Transferable content = clipboard.getContents(this);

      if (content == null) {
        setValue(null);
        getToolkit().beep();
        return;
      }

      try {
        String moneyData = (String) (content.getTransferData(DataFlavor.stringFlavor));
        int index = moneyData.indexOf(' ');

        if (index <= 0) {
          getToolkit().beep();
          return;
        }

        String value = moneyData.substring(0, index);
        String currencyAbbreviation = moneyData.substring(index + 1);

        value = value.replace(',', '.');
        Money money = new Money(value, Money.getCurrencyValueFor(currencyAbbreviation));
        setValue(money);
      } catch (Exception ex) {
        getToolkit().beep();
      }
    } else if (source == calculatorItem) {
      Dimension size = this.moneyField.getSize();

      runCalculatorDialog(
          new MouseEvent(
              this,
              0, // id
              System.currentTimeMillis(), // when
              0, // modifiers
              size.width, // x
              size.height, // y
              1, // clickCount
              true // popuptrigger
              ));
    } else if (source instanceof JMenuItem) {
      JMenuItem menuItem = (JMenuItem) source;

      // Perform 'changed currency' - action

      String actionText = menuItem.getActionCommand();

      // Cut off additional information from actionCommand

      int currencyDelimPos = actionText.indexOf(" ");

      if (currencyDelimPos > 0) {
        actionText = actionText.substring(0, currencyDelimPos);
      }

      // lastCurrencyItem will show any currency except the default ones

      boolean isNotDefault = true;

      for (int i = 0; i < defaultCurrencies.length; i++) {
        if (defaultCurrencies[i].equals(actionText)) {
          isNotDefault = false;
          break;
        }
      }

      // @todo ... check if lastCurrencyItem is needed or not [aj] seems to be old stuff
      if (isNotDefault && lastCurrencyItem != null) {
        lastCurrencyItem.setText(actionText);
      }

      // Change the currency

      changeCurrency(actionText, false);
    } else if ("popup".equals(e.getActionCommand())) // key: SHIFT+F10 <=> popup
    {
      Point point = getLocation();
      moneyPopup.doPopup(0, getHeight());
    }
  }