001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.scxml2.env;
018
019import java.awt.BorderLayout;
020import java.awt.Graphics;
021import java.awt.Image;
022import java.awt.Toolkit;
023import java.awt.event.ActionEvent;
024import java.awt.event.ActionListener;
025import java.net.URL;
026import java.util.Timer;
027import java.util.TimerTask;
028
029import javax.swing.JButton;
030import javax.swing.JFrame;
031import javax.swing.JLabel;
032import javax.swing.JPanel;
033
034import org.apache.commons.scxml2.model.ModelException;
035
036
037/**
038 * Quick GUI to demonstrate the SCXML driven stopwatch.
039 *
040 * Separation of UI (this class) from behavior (StopWatch class).
041 * UI serves merely as a front to relay user initiated events to StopWatch
042 * object, which encapsulates all the behavior of a stopwatch.
043 * Using SCXML makes the StopWatch class simplistic, and provides a direct
044 * route from the UML model to the runtime.
045 *
046 * @see StopWatch
047 */
048public class StopWatchDisplay extends JFrame
049        implements ActionListener {
050
051    private static final long serialVersionUID = 1L;
052    private StopWatch stopWatch;
053    private Image watchImage, watchIcon;
054
055    public StopWatchDisplay() throws ModelException {
056        super("SCXML stopwatch");
057        stopWatch = new StopWatch();
058        setupUI();
059    }
060
061    public void actionPerformed(ActionEvent e) {
062        String command = e.getActionCommand();
063        if (command.equals("START")) {
064            if (start.getText().equals("Start")) {
065                stopWatch.fireEvent(StopWatch.EVENT_START);
066                start.setText("Stop");
067                split.setEnabled(true);
068            } else if (start.getText().equals("Stop")) {
069                stopWatch.fireEvent(StopWatch.EVENT_STOP);
070                start.setText("Reset");
071                split.setEnabled(false);
072            } else {
073                stopWatch.fireEvent(StopWatch.EVENT_RESET);
074                start.setText("Start");
075                split.setText("Split");
076            }
077        } else if (command.equals("SPLIT")) {
078            if (split.getText().equals("Split")) {
079                stopWatch.fireEvent(StopWatch.EVENT_SPLIT);
080                split.setText("Unsplit");
081            } else {
082                stopWatch.fireEvent(StopWatch.EVENT_UNSPLIT);
083                split.setText("Split");
084            }
085        }
086    }
087
088    private void setupUI() {
089        URL imageURL = this.getClass().getClassLoader().
090            getResource("org/apache/commons/scxml2/env/stopwatch.gif");
091        URL iconURL = this.getClass().getClassLoader().
092            getResource("org/apache/commons/scxml2/env/stopwatchicon.gif");
093        Toolkit kit = Toolkit.getDefaultToolkit();
094        watchImage = kit.createImage(imageURL);
095        watchIcon = kit.createImage(iconURL);
096        WatchPanel panel = new WatchPanel();
097        panel.setLayout(new BorderLayout());
098        setContentPane(panel);
099        display = new JLabel(stopWatch.getDisplay());
100        panel.add(display, BorderLayout.PAGE_START);
101        start = makeButton("START", "start, stop, reset", "Start");
102        panel.add(start, BorderLayout.LINE_START);
103        state = new JLabel();
104        panel.add(state, BorderLayout.CENTER);
105        split = makeButton("SPLIT", "split, unsplit", "Split");
106        split.setEnabled(false);
107        panel.add(split, BorderLayout.LINE_END);
108        pack();
109        setLocation(200,200);
110        setIconImage(watchIcon);
111        setResizable(false);
112        setSize(300,125);
113        setVisible(true);
114        setDefaultCloseOperation(EXIT_ON_CLOSE);
115        Timer displayTimer = new Timer();
116        displayTimer.scheduleAtFixedRate(new TimerTask() {
117            @Override
118            public void run() {
119                display.setText(DISPLAY_PREFIX + stopWatch.getDisplay()
120                    + DISPLAY_SUFFIX);
121                state.setText(STATE_PREFIX + stopWatch.getCurrentState()
122                    + STATE_SUFFIX);
123            }
124        }, 100, 100);
125    }
126
127    private JButton makeButton(final String actionCommand,
128            final String toolTipText, final String altText) {
129        JButton button = new JButton(altText);
130        button.setActionCommand(actionCommand);
131        button.setToolTipText(toolTipText);
132        button.addActionListener(this);
133        button.setOpaque(false);
134        return button;
135    }
136
137    class WatchPanel extends JPanel {
138        private static final long serialVersionUID = 1L;
139
140        @Override
141        public void paintComponent(Graphics g) {
142            if(watchImage != null) {
143                g.drawImage(watchImage,0,0,this.getWidth(),this.getHeight(),this);
144            }
145        }
146    }
147
148    public static void main(String[] args) throws Exception {
149        new StopWatchDisplay();
150    }
151
152    private JLabel display, state;
153    private JButton start, split;
154    // spaces :: GridBagConstraints ;-)
155    private static final String
156        DISPLAY_PREFIX = "<html><font face=\"Courier\" color=\"maroon\"" +
157            " size=\"10\"><b>&nbsp;&nbsp;&nbsp;",
158        DISPLAY_SUFFIX = "</b></font></html>",
159        STATE_PREFIX = "<html><font color=\"blue\" size=\"4\"" +
160            ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",
161        STATE_SUFFIX = "</font></html>";
162
163}
164