Convert files to Unix line endings Change-Id: Ia06031385c7a8cfdb675020a4cbe9c1b49675955
diff --git a/org.eclipse.example.calc.test/tst/org/eclipse/example/calc/internal/CalculatorTest.java b/org.eclipse.example.calc.test/tst/org/eclipse/example/calc/internal/CalculatorTest.java index 031d49c..318a4b4 100644 --- a/org.eclipse.example.calc.test/tst/org/eclipse/example/calc/internal/CalculatorTest.java +++ b/org.eclipse.example.calc.test/tst/org/eclipse/example/calc/internal/CalculatorTest.java
@@ -1,93 +1,93 @@ -/******************************************************************************* - * Copyright (C) 2010, Matthias Sohn <matthias.sohn@sap.com> - * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com> - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - *******************************************************************************/ -package org.eclipse.example.calc.internal; - -import static org.junit.Assert.assertEquals; - -import org.eclipse.example.calc.Operations; -import org.junit.After; -import org.junit.Test; - -public class CalculatorTest { - - @After - public void tearDown() throws Exception { - Operations.INSTANCE.reset(); - } - - @Test - public void testPlus() { - TestTextProvider textProvider = new TestTextProvider(); - Calculator calculator = new Calculator(textProvider); - - calculator.handleButtonClick("1"); - calculator.handleButtonClick("+"); - calculator.handleButtonClick("2"); - calculator.handleButtonClick("="); - - assertEquals("3.0", textProvider.getDisplayText()); - } - - @Test - public void testTwoTimesPlus() { - TestTextProvider textProvider = new TestTextProvider(); - Calculator calculator = new Calculator(textProvider); - - calculator.handleButtonClick("1"); - calculator.handleButtonClick("+"); - calculator.handleButtonClick("2"); - calculator.handleButtonClick("+"); - calculator.handleButtonClick("3"); - calculator.handleButtonClick("="); - - assertEquals("6.0", textProvider.getDisplayText()); - } - - @Test - public void testPlusWithFraction() { - TestTextProvider textProvider = new TestTextProvider(); - Calculator calculator = new Calculator(textProvider); - - calculator.handleButtonClick("1"); - calculator.handleButtonClick("+"); - calculator.handleButtonClick("2"); - calculator.handleButtonClick("."); - calculator.handleButtonClick("1"); - calculator.handleButtonClick("="); - - assertEquals("3.1", textProvider.getDisplayText()); - } - - @Test - public void testSquare() { - TestTextProvider textProvider = new TestTextProvider(); - Calculator calculator = new Calculator(textProvider); - - calculator.handleButtonClick("2"); - calculator.handleButtonClick("x²"); - - assertEquals("4.0", textProvider.getDisplayText()); - } - - private static final class TestTextProvider implements TextProvider { - - private String text = "0"; - - public void setDisplayText(String text) { - this.text = text; - - } - - public String getDisplayText() { - return text; - } - - } -} +/******************************************************************************* + * Copyright (C) 2010, Matthias Sohn <matthias.sohn@sap.com> + * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com> + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.eclipse.example.calc.internal; + +import static org.junit.Assert.assertEquals; + +import org.eclipse.example.calc.Operations; +import org.junit.After; +import org.junit.Test; + +public class CalculatorTest { + + @After + public void tearDown() throws Exception { + Operations.INSTANCE.reset(); + } + + @Test + public void testPlus() { + TestTextProvider textProvider = new TestTextProvider(); + Calculator calculator = new Calculator(textProvider); + + calculator.handleButtonClick("1"); + calculator.handleButtonClick("+"); + calculator.handleButtonClick("2"); + calculator.handleButtonClick("="); + + assertEquals("3.0", textProvider.getDisplayText()); + } + + @Test + public void testTwoTimesPlus() { + TestTextProvider textProvider = new TestTextProvider(); + Calculator calculator = new Calculator(textProvider); + + calculator.handleButtonClick("1"); + calculator.handleButtonClick("+"); + calculator.handleButtonClick("2"); + calculator.handleButtonClick("+"); + calculator.handleButtonClick("3"); + calculator.handleButtonClick("="); + + assertEquals("6.0", textProvider.getDisplayText()); + } + + @Test + public void testPlusWithFraction() { + TestTextProvider textProvider = new TestTextProvider(); + Calculator calculator = new Calculator(textProvider); + + calculator.handleButtonClick("1"); + calculator.handleButtonClick("+"); + calculator.handleButtonClick("2"); + calculator.handleButtonClick("."); + calculator.handleButtonClick("1"); + calculator.handleButtonClick("="); + + assertEquals("3.1", textProvider.getDisplayText()); + } + + @Test + public void testSquare() { + TestTextProvider textProvider = new TestTextProvider(); + Calculator calculator = new Calculator(textProvider); + + calculator.handleButtonClick("2"); + calculator.handleButtonClick("x²"); + + assertEquals("4.0", textProvider.getDisplayText()); + } + + private static final class TestTextProvider implements TextProvider { + + private String text = "0"; + + public void setDisplayText(String text) { + this.text = text; + + } + + public String getDisplayText() { + return text; + } + + } +}
diff --git a/org.eclipse.example.calc/src/org/eclipse/example/calc/internal/Calculator.java b/org.eclipse.example.calc/src/org/eclipse/example/calc/internal/Calculator.java index 8d1725b..fe825b0 100644 --- a/org.eclipse.example.calc/src/org/eclipse/example/calc/internal/Calculator.java +++ b/org.eclipse.example.calc/src/org/eclipse/example/calc/internal/Calculator.java
@@ -1,113 +1,113 @@ -/******************************************************************************* - * Copyright (C) 2010, Matthias Sohn <matthias.sohn@sap.com> - * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com> - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - *******************************************************************************/ -package org.eclipse.example.calc.internal; - -import org.eclipse.example.calc.BinaryOperation; -import org.eclipse.example.calc.Operation; -import org.eclipse.example.calc.Operations; -import org.eclipse.example.calc.UnaryOperation; -import org.eclipse.example.calc.internal.operations.Equals; -import org.eclipse.example.calc.internal.operations.Minus; -import org.eclipse.example.calc.internal.operations.Plus; -import org.eclipse.example.calc.internal.operations.Square; - -public class Calculator { - - private TextProvider textProvider; - - private String cmd; - - private boolean clearText; - - private float value; - - public static String NAME = "Simple Calculator"; - - public Calculator(TextProvider textProvider) { - this.textProvider = textProvider; - setupDefaultOperations(); - } - - private void setupDefaultOperations() { - new Equals(); - new Minus(); - new Plus(); - new Square(); - } - - private void calculate(String cmdName) { - float curValue; - float newValue = 0; - - // get current value of display - curValue = Float.parseFloat(textProvider.getDisplayText()); - - Operation currentOp = Operations.INSTANCE.getOperation(cmdName); - if ((currentOp instanceof BinaryOperation) && (cmd == null)) { - // if last clicked operation was binary and there is no saved - // operation, store it - cmd = cmdName; - setClearText(true); - } else { - // if saved command is binary perform it - Operation savedOp = Operations.INSTANCE.getOperation(cmd); - if (savedOp instanceof BinaryOperation) { - BinaryOperation bop = (BinaryOperation) savedOp; - newValue = bop.perform(value, curValue); - } // if current operation is unary perform it - else if (currentOp instanceof UnaryOperation) { - UnaryOperation uop = (UnaryOperation) currentOp; - newValue = uop.perform(curValue); - } - - // display the result and prepare clear on next button - textProvider.setDisplayText("" + newValue); - setClearText(true); - if (currentOp instanceof Equals) { - // do not save "=" command - cmd = null; - } else if (currentOp instanceof BinaryOperation) { - // save binary commands as they are executed on next operation - cmd = cmdName; - } else { - // clear saved command - cmd = null; - } - } - - } - - private boolean isCommand(String name) { - return (Operations.INSTANCE.getOperation(name) != null); - } - - public void handleButtonClick(String str) { - if (isCommand(str)) { - calculate(str); - } else { - char digit = (str.toCharArray())[0]; - if (Character.isDigit(digit) || digit == '.') { - if (clearText) { - // save current value and clear the display - value = Float.parseFloat(textProvider.getDisplayText()); - textProvider.setDisplayText(""); - setClearText(false); - } - - // add new digit to display - textProvider.setDisplayText(textProvider.getDisplayText() + digit); - } - } - } - - public void setClearText(boolean clearText) { - this.clearText = clearText; - } -} +/******************************************************************************* + * Copyright (C) 2010, Matthias Sohn <matthias.sohn@sap.com> + * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com> + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.eclipse.example.calc.internal; + +import org.eclipse.example.calc.BinaryOperation; +import org.eclipse.example.calc.Operation; +import org.eclipse.example.calc.Operations; +import org.eclipse.example.calc.UnaryOperation; +import org.eclipse.example.calc.internal.operations.Equals; +import org.eclipse.example.calc.internal.operations.Minus; +import org.eclipse.example.calc.internal.operations.Plus; +import org.eclipse.example.calc.internal.operations.Square; + +public class Calculator { + + private TextProvider textProvider; + + private String cmd; + + private boolean clearText; + + private float value; + + public static String NAME = "Simple Calculator"; + + public Calculator(TextProvider textProvider) { + this.textProvider = textProvider; + setupDefaultOperations(); + } + + private void setupDefaultOperations() { + new Equals(); + new Minus(); + new Plus(); + new Square(); + } + + private void calculate(String cmdName) { + float curValue; + float newValue = 0; + + // get current value of display + curValue = Float.parseFloat(textProvider.getDisplayText()); + + Operation currentOp = Operations.INSTANCE.getOperation(cmdName); + if ((currentOp instanceof BinaryOperation) && (cmd == null)) { + // if last clicked operation was binary and there is no saved + // operation, store it + cmd = cmdName; + setClearText(true); + } else { + // if saved command is binary perform it + Operation savedOp = Operations.INSTANCE.getOperation(cmd); + if (savedOp instanceof BinaryOperation) { + BinaryOperation bop = (BinaryOperation) savedOp; + newValue = bop.perform(value, curValue); + } // if current operation is unary perform it + else if (currentOp instanceof UnaryOperation) { + UnaryOperation uop = (UnaryOperation) currentOp; + newValue = uop.perform(curValue); + } + + // display the result and prepare clear on next button + textProvider.setDisplayText("" + newValue); + setClearText(true); + if (currentOp instanceof Equals) { + // do not save "=" command + cmd = null; + } else if (currentOp instanceof BinaryOperation) { + // save binary commands as they are executed on next operation + cmd = cmdName; + } else { + // clear saved command + cmd = null; + } + } + + } + + private boolean isCommand(String name) { + return (Operations.INSTANCE.getOperation(name) != null); + } + + public void handleButtonClick(String str) { + if (isCommand(str)) { + calculate(str); + } else { + char digit = (str.toCharArray())[0]; + if (Character.isDigit(digit) || digit == '.') { + if (clearText) { + // save current value and clear the display + value = Float.parseFloat(textProvider.getDisplayText()); + textProvider.setDisplayText(""); + setClearText(false); + } + + // add new digit to display + textProvider.setDisplayText(textProvider.getDisplayText() + digit); + } + } + } + + public void setClearText(boolean clearText) { + this.clearText = clearText; + } +}
diff --git a/org.eclipse.example.calc/src/org/eclipse/example/calc/internal/TextProvider.java b/org.eclipse.example.calc/src/org/eclipse/example/calc/internal/TextProvider.java index 4a1993d..db2edb5 100644 --- a/org.eclipse.example.calc/src/org/eclipse/example/calc/internal/TextProvider.java +++ b/org.eclipse.example.calc/src/org/eclipse/example/calc/internal/TextProvider.java
@@ -1,26 +1,26 @@ -/******************************************************************************* - * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com> - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - *******************************************************************************/ -package org.eclipse.example.calc.internal; - -/** - * Display abstraction for {@link Calculator} - */ -public interface TextProvider { - - /** - * @param text the text to display on the {@link Calculator} - */ - public void setDisplayText(String text); - - /** - * @return the text on the display of the {@link Calculator} - */ - public String getDisplayText(); - -} +/******************************************************************************* + * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com> + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.eclipse.example.calc.internal; + +/** + * Display abstraction for {@link Calculator} + */ +public interface TextProvider { + + /** + * @param text the text to display on the {@link Calculator} + */ + public void setDisplayText(String text); + + /** + * @return the text on the display of the {@link Calculator} + */ + public String getDisplayText(); + +}
diff --git a/org.eclipse.example.calc/src/org/eclipse/example/calc/internal/ui/swt/CalculatorUI.java b/org.eclipse.example.calc/src/org/eclipse/example/calc/internal/ui/swt/CalculatorUI.java index 2961499..a082ef3 100644 --- a/org.eclipse.example.calc/src/org/eclipse/example/calc/internal/ui/swt/CalculatorUI.java +++ b/org.eclipse.example.calc/src/org/eclipse/example/calc/internal/ui/swt/CalculatorUI.java
@@ -1,147 +1,147 @@ -/******************************************************************************* - * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com> - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - *******************************************************************************/ -package org.eclipse.example.calc.internal.ui.swt; - -import org.eclipse.example.calc.Operations; -import org.eclipse.example.calc.internal.Calculator; -import org.eclipse.example.calc.internal.TextProvider; -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.SelectionEvent; -import org.eclipse.swt.events.SelectionListener; -import org.eclipse.swt.layout.GridData; -import org.eclipse.swt.layout.GridLayout; -import org.eclipse.swt.widgets.Button; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Display; -import org.eclipse.swt.widgets.Group; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.swt.widgets.Text; - -/* - * A simple calculator featuring a SWT - * UI. - */ -public class CalculatorUI implements TextProvider, SelectionListener { - - private static final long serialVersionUID = 1L; - - private Calculator calculator; - - private Shell shell; - - private Text display; - - private Button[] numberButtons; - - private Button[] cmdButtons; - - public static void main(String[] args) { - Display display = new Display(); - Shell shell = new CalculatorUI().open(display); - while (!shell.isDisposed()) { - if (!display.readAndDispatch()) - display.sleep(); - } - display.dispose(); - } - - private Shell open(Display display) { - shell = new Shell(display); - setupGUI(); - - shell.pack(); - shell.open(); - return shell; - } - - public CalculatorUI() { - calculator = new Calculator(this); - } - - private void setupGUI() { - shell.setText(Calculator.NAME); - GridLayout gridLayout = new GridLayout(); - gridLayout.numColumns = 1; - gridLayout.marginHeight = gridLayout.marginWidth = 0; - shell.setLayout(gridLayout); - - setupDisplay(); - setupNumberButtons(); - setupCommandButtons(); - - } - - private void setupDisplay() { - GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); - display = new Text(shell, SWT.BORDER_SOLID | SWT.RIGHT); - display.setLayoutData(gridData); - display.setText("0"); - calculator.setClearText(true); - } - - private void setupNumberButtons() { - Composite numberButtonsPanel = new Composite(shell, SWT.NONE); - numberButtonsPanel.setLayout(new GridLayout(4, true)); - numberButtons = new Button[11]; - - for (int i = 0; i < numberButtons.length - 1; i++) { - addNumberButton(numberButtonsPanel, i, Integer.valueOf(i) - .toString()); - } - addNumberButton(numberButtonsPanel, 10, "."); - } - - private void addNumberButton(Composite parent, int i, String name) { - numberButtons[i] = new Button(parent, SWT.PUSH); - numberButtons[i].setText(name); - numberButtons[i].addSelectionListener(this); - } - - private void setupCommandButtons() { - // command buttons - Group cmdButtonsPanel = new Group(shell, SWT.NONE); - cmdButtonsPanel.setText("Operations"); - cmdButtonsPanel.setLayout(new GridLayout(4, true)); - - cmdButtons = new Button[Operations.INSTANCE.size()]; - - // make the buttons, set ActionListener and add to panel - for (int i = 0; i < cmdButtons.length; i++) { - addCommandButton(cmdButtonsPanel, i); - } - } - - private void addCommandButton(Composite parent, int i) { - cmdButtons[i] = new Button(parent, SWT.NONE); - cmdButtons[i].setText(Operations.INSTANCE.getOperationName(i)); - cmdButtons[i].addSelectionListener(this); - } - - @Override - public void widgetDefaultSelected(SelectionEvent arg0) { - // empty - } - - @Override - public void widgetSelected(SelectionEvent event) { - String str = ((Button) event.getSource()).getText(); - calculator.handleButtonClick(str); - } - - @Override - public void setDisplayText(String text) { - display.setText(text); - } - - @Override - public String getDisplayText() { - return display.getText(); - } - -} +/******************************************************************************* + * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com> + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.eclipse.example.calc.internal.ui.swt; + +import org.eclipse.example.calc.Operations; +import org.eclipse.example.calc.internal.Calculator; +import org.eclipse.example.calc.internal.TextProvider; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Group; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; + +/* + * A simple calculator featuring a SWT + * UI. + */ +public class CalculatorUI implements TextProvider, SelectionListener { + + private static final long serialVersionUID = 1L; + + private Calculator calculator; + + private Shell shell; + + private Text display; + + private Button[] numberButtons; + + private Button[] cmdButtons; + + public static void main(String[] args) { + Display display = new Display(); + Shell shell = new CalculatorUI().open(display); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); + } + + private Shell open(Display display) { + shell = new Shell(display); + setupGUI(); + + shell.pack(); + shell.open(); + return shell; + } + + public CalculatorUI() { + calculator = new Calculator(this); + } + + private void setupGUI() { + shell.setText(Calculator.NAME); + GridLayout gridLayout = new GridLayout(); + gridLayout.numColumns = 1; + gridLayout.marginHeight = gridLayout.marginWidth = 0; + shell.setLayout(gridLayout); + + setupDisplay(); + setupNumberButtons(); + setupCommandButtons(); + + } + + private void setupDisplay() { + GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); + display = new Text(shell, SWT.BORDER_SOLID | SWT.RIGHT); + display.setLayoutData(gridData); + display.setText("0"); + calculator.setClearText(true); + } + + private void setupNumberButtons() { + Composite numberButtonsPanel = new Composite(shell, SWT.NONE); + numberButtonsPanel.setLayout(new GridLayout(4, true)); + numberButtons = new Button[11]; + + for (int i = 0; i < numberButtons.length - 1; i++) { + addNumberButton(numberButtonsPanel, i, Integer.valueOf(i) + .toString()); + } + addNumberButton(numberButtonsPanel, 10, "."); + } + + private void addNumberButton(Composite parent, int i, String name) { + numberButtons[i] = new Button(parent, SWT.PUSH); + numberButtons[i].setText(name); + numberButtons[i].addSelectionListener(this); + } + + private void setupCommandButtons() { + // command buttons + Group cmdButtonsPanel = new Group(shell, SWT.NONE); + cmdButtonsPanel.setText("Operations"); + cmdButtonsPanel.setLayout(new GridLayout(4, true)); + + cmdButtons = new Button[Operations.INSTANCE.size()]; + + // make the buttons, set ActionListener and add to panel + for (int i = 0; i < cmdButtons.length; i++) { + addCommandButton(cmdButtonsPanel, i); + } + } + + private void addCommandButton(Composite parent, int i) { + cmdButtons[i] = new Button(parent, SWT.NONE); + cmdButtons[i].setText(Operations.INSTANCE.getOperationName(i)); + cmdButtons[i].addSelectionListener(this); + } + + @Override + public void widgetDefaultSelected(SelectionEvent arg0) { + // empty + } + + @Override + public void widgetSelected(SelectionEvent event) { + String str = ((Button) event.getSource()).getText(); + calculator.handleButtonClick(str); + } + + @Override + public void setDisplayText(String text) { + display.setText(text); + } + + @Override + public String getDisplayText() { + return display.getText(); + } + +}