private void setPointButtonAction() { JButton button = calculator.getButtonPoint(); Action action = new AbstractAction("colonAction") { @Override public void actionPerformed(ActionEvent e) { appendTextInCalculatorDisplay("."); } }; // Shortcut al punto normal action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 0)); button.getActionMap().put("colonAction", action); button .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "colonAction"); // Shortcut al punto del numpad action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_DECIMAL, 0)); button.getActionMap().put("colonAction", action); button .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "colonAction"); button.addActionListener(action); }
private void setMinusButtonAction() { JButton button = calculator.getButtonMinus(); Action action = new AbstractAction("buttonMinusAction") { @Override public void actionPerformed(ActionEvent e) { appendTextInCalculatorDisplay("-"); } }; // Shortcut al menos del numpad action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, 0)); button.getActionMap().put("buttonMinusAction", action); button .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "buttonMinusAction"); // Shortcut al guion alto action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, 0)); button.getActionMap().put("buttonMinusAction", action); button .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "buttonMinusAction"); button.addActionListener(action); }
private void setHexModeButtonAction() { ActionListener hexModeAction = new ActionListener() { @Override public void actionPerformed(ActionEvent event) { calculator.setHexMode(); if (calculator.getPreviousMode() == CalculatorModes.DEC_MODE) { // Convierto de decimal a hexadecimal decToHex(); } } }; calculator.getHexModeButton().addActionListener(hexModeAction); }
private void setFpModeButtonAction() { ActionListener fpModeAction = new ActionListener() { @Override public void actionPerformed(ActionEvent event) { calculator.setFpMode(); if (calculator.getPreviousMode() == CalculatorModes.DEC_MODE) { // Convierto de decimal a punto flotante decToFp(); } } }; calculator.getFpModeButton().addActionListener(fpModeAction); }
private void setDecModeButtonAction() { ActionListener decModeAction = new ActionListener() { @Override public void actionPerformed(ActionEvent event) { calculator.setDecMode(); if (calculator.getPreviousMode() == CalculatorModes.HEX_MODE) { // Convierto de hexadecimal a decimal hexToDec(); } else if (calculator.getPreviousMode() == CalculatorModes.FP_MODE) { fpToDec(); } } }; calculator.getDecModeButton().addActionListener(decModeAction); }
private void fpToDec() { if (calculator.getDisplayText().length() == 0) return; try { Integer displayNumber = Integer.valueOf(calculator.getDisplayText(), 16); Double converted = Utils.fpToDec(displayNumber); if ((displayNumber > 944.44444) || (displayNumber < -944.44444)) { throw (new IllegalArgumentException()); } calculator.setDisplayText(converted.toString()); } catch (NumberFormatException e) { calculator.setDisplayText("Formato de numero invalido"); calculator.setErrorMode(); } catch (IllegalArgumentException e) { calculator.setDisplayText("Numero fuera de rango"); calculator.setErrorMode(); } }
// Sirve para numeros entre -127 y 128 private void hexToDec() { if (calculator.getDisplayText().length() == 0) return; try { Long aux = Long.valueOf(calculator.getDisplayText(), 16); aux = (aux > 127) ? (long) (aux - 256) : aux; if ((aux > 127) || (aux < -128)) { throw (new IllegalArgumentException()); } calculator.setDisplayText(aux.toString()); } catch (NumberFormatException e) { calculator.setDisplayText("Formato de numero invalido"); calculator.setErrorMode(); } catch (IllegalArgumentException e) { calculator.setDisplayText("Numero fuera de rango"); calculator.setErrorMode(); } }
private void setButtonCopyAction() { JButton button = calculator.getButtonCopy(); Action action = new AbstractAction("buttonCopyAction") { @Override public void actionPerformed(ActionEvent e) { calculator.getDisplay().selectAll(); calculator.getDisplay().copy(); } }; // Shortcut a control+c action.putValue( Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_DOWN_MASK)); button.getActionMap().put("buttonCopyAction", action); button .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "buttonCopyAction"); button.addActionListener(action); }
private void setButtonClearAction() { JButton button = calculator.getButtonClear(); Action action = new AbstractAction("buttonClearAction") { @Override public void actionPerformed(ActionEvent e) { calculator.setDisplayText(""); if (calculator.getActualMode() == CalculatorModes.ERROR_MODE) { calculator.reset(); calculator.setDecMode(); } } }; // Shorcut al space action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0)); button.getActionMap().put("buttonClearAction", action); button .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "buttonClearAction"); button.addActionListener(action); }
// Sirve para numeros entre -127 y 128 private void decToHex() { if (calculator.getDisplayText().length() == 0) return; try { Long displayNumber = Long.parseLong(calculator.getDisplayText()); Long number; number = Math.abs(displayNumber); if ((displayNumber > 127) || (displayNumber < -128)) { throw (new IllegalArgumentException()); } if (displayNumber < 0) { // Paso el numero a binario y completo con 0 hasta 8 bits String binary = Long.toBinaryString(number); while (binary.length() < 8) { binary = '0' + binary; } // Invertir bits char[] inverted = binary.toCharArray(); for (Integer i = 0; i < inverted.length; i++) { if (inverted[i] == '0') inverted[i] = '1'; else inverted[i] = '0'; } String invertedString = new String(inverted); number = Long.parseLong(invertedString, 2) + 1; } calculator.setDisplayText(Long.toHexString(number)); } catch (NumberFormatException e) { calculator.setDisplayText("Formato de numero invalido"); calculator.setErrorMode(); } catch (IllegalArgumentException Exception) { calculator.setDisplayText("Numero fuera de rango"); calculator.setErrorMode(); } }
private void decToFp() { if (calculator.getDisplayText().length() == 0) return; String result = ""; try { Double displayNumber = Double.parseDouble(calculator.getDisplayText()); if ((displayNumber > 944.44444) || (displayNumber < -944.44444)) { throw (new IllegalArgumentException()); } result = Utils.hexString(Utils.decToFp(displayNumber)); } catch (NumberFormatException e) { result = "Formato de numero inválido"; calculator.setErrorMode(); } catch (IllegalArgumentException Exception) { result = "Número fuera de rango"; calculator.setErrorMode(); } catch (LostPrecisionException e) { result = Utils.hexString(e.getNumber()); calculator.showPrecisionLostDialog(); } finally { calculator.setDisplayText(result); } }
private void appendTextInCalculatorDisplay(String text) { calculator.getDisplay().append(text); }
@Override public void control() { setDecModeButtonAction(); setHexModeButtonAction(); setFpModeButtonAction(); setButtonAction(calculator.getButton0(), "0"); setButtonAction(calculator.getButton1(), "1"); setButtonAction(calculator.getButton2(), "2"); setButtonAction(calculator.getButton3(), "3"); setButtonAction(calculator.getButton4(), "4"); setButtonAction(calculator.getButton5(), "5"); setButtonAction(calculator.getButton6(), "6"); setButtonAction(calculator.getButton7(), "7"); setButtonAction(calculator.getButton8(), "8"); setButtonAction(calculator.getButton9(), "9"); setButtonAction(calculator.getButtonA(), "A"); setButtonAction(calculator.getButtonB(), "B"); setButtonAction(calculator.getButtonC(), "C"); setButtonAction(calculator.getButtonD(), "D"); setButtonAction(calculator.getButtonE(), "E"); setButtonAction(calculator.getButtonF(), "F"); setMinusButtonAction(); setPointButtonAction(); setButtonClearAction(); setButtonCopyAction(); }