Commits

Anonymous committed 81aeb5b

add counter model

Comments (0)

Files changed (6)

 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
 	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="src" path="test"/>
 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
 	<classpathentry kind="output" path="bin"/>
 </classpath>

.settings/org.eclipse.core.resources.prefs

+#Sat May 29 14:38:24 JST 2010
+eclipse.preferences.version=1
+encoding/<project>=UTF-8

.settings/org.eclipse.core.runtime.prefs

+#Sat May 29 14:38:24 JST 2010
+eclipse.preferences.version=1
+line.separator=\n

src/tutorial/AppMain.java

 
 import java.awt.EventQueue;
 
+import tutorial.model.Counter;
 import tutorial.view.AppFrame;
 
 public class AppMain {
+	private volatile static AppMain INSTANCE = new AppMain();
+
+	public static AppMain get() {
+		return INSTANCE;
+	}
 
 	public static void main(String[] args) {
+		INSTANCE.initalize(args);
+		INSTANCE.start();
+	}
+
+	private final Counter counter;
+
+	AppMain() {
+		counter = new Counter();
+	}
+	
+	private void initalize(String[] args) {
+	}
+
+	public Counter getCounter() {
+		return counter;
+	}
+
+	public void start() {
 		EventQueue.invokeLater(new Runnable() {
 
 			@Override

src/tutorial/model/Counter.java

+package tutorial.model;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class Counter {
+
+	private final AtomicInteger count = new AtomicInteger(1);
+
+	public Counter() {
+	}
+
+	public void increment() {
+		count.incrementAndGet();
+	}
+
+	public void decrement() {
+		count.decrementAndGet();
+	}
+
+	public int get() {
+		return count.get();
+	}
+}

src/tutorial/view/AppPanel.java

 import javax.swing.JLabel;
 import javax.swing.JPanel;
 
+import tutorial.AppMain;
+
 public class AppPanel extends JPanel {
 
 	private static final long serialVersionUID = 1L;
-	private int counter = 0;
 	private JLabel label;
-	
+
 	public AppPanel() {
 		initComponents();
 	}
 
 	private void initComponents() {
-		label = new JLabel(Integer.toString(counter));
+		label = new JLabel(Integer.toString(AppMain.get().getCounter().get()));
 		JButton upButton = new JButton("Up");
 		upButton.addActionListener(new ActionListener() {
 
 	}
 
 	protected void onUpButtuon() {
-		counter++;
+		AppMain.get().getCounter().increment();
 		updateCounterLabel();
 	}
 
 	protected void onDownButton() {
-		counter--;
+		AppMain.get().getCounter().decrement();
 		updateCounterLabel();
 	}
 
 	private void updateCounterLabel() {
-		label.setText(Integer.toString(counter));
+		label.setText(Integer.toString(AppMain.get().getCounter().get()));
 	}
 }