Search in sources :

Example 6 with Timer

use of javax.swing.Timer in project jgnash by ccavanaugh.

the class RecurringPanel method startTimer.

private void startTimer() {
    if (timer == null) {
        Preferences p = Preferences.userNodeForPackage(getClass());
        int snooze = p.getInt(SNOOZE, DEFAULT_SNOOZE);
        timer = new Timer(snooze, this);
        timer.setInitialDelay(START_UP_DELAY);
        // Don't start until the UI has caught up
        EventQueue.invokeLater(() -> {
            timer.start();
            Logger.getLogger(RecurringPanel.class.getName()).info("Recurring timer started");
        });
    }
}
Also used : Timer(javax.swing.Timer) Preferences(java.util.prefs.Preferences)

Example 7 with Timer

use of javax.swing.Timer in project jdk8u_jdk by JetBrains.

the class AquaScrollBarUI method installListeners.

protected void installListeners() {
    fTrackListener = createTrackListener();
    fModelListener = createModelListener();
    fPropertyChangeListener = createPropertyChangeListener();
    fScrollBar.addMouseListener(fTrackListener);
    fScrollBar.addMouseMotionListener(fTrackListener);
    fScrollBar.getModel().addChangeListener(fModelListener);
    fScrollBar.addPropertyChangeListener(fPropertyChangeListener);
    fScrollListener = createScrollListener();
    fScrollTimer = new Timer(kNormalDelay, fScrollListener);
    // default InitialDelay?
    fScrollTimer.setInitialDelay(kInitialDelay);
}
Also used : Timer(javax.swing.Timer)

Example 8 with Timer

use of javax.swing.Timer in project JMRI by JMRI.

the class DiagnosticFrame method runOutputTest.

/**
     * Local Method to run an Output Test
     */
@SuppressFBWarnings(value = "SBSC_USE_STRINGBUFFER_CONCATENATION")
protected // though it would be good to fix it if you're working in this area
void runOutputTest() {
    // Set up timer to update output pattern periodically
    outTimer = new Timer(obsDelay, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evnt) {
            if (testRunning && outTest) {
                short[] outBitPattern = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
                String[] portID = { "A", "B", "C", "D" };
                // set new pattern
                outBytes[curOutByte] = (byte) outBitPattern[curOutBit];
                // send new pattern
                SerialMessage m = createOutPacket();
                m.setTimeout(50);
                _memo.getTrafficController().sendSerialMessage(m, curFrame);
                // update status panel to show bit that is on
                statusText1.setText("Port " + portID[curOutByte - begOutByte] + " Bit " + Integer.toString(curOutBit) + " is on - Compare LED's with the pattern below");
                statusText1.setVisible(true);
                StringBuilder st = new StringBuilder();
                for (int i = begOutByte; i <= endOutByte; i++) {
                    st.append("  ");
                    for (int j = 0; j < 8; j++) {
                        if ((i == curOutByte) && (j == curOutBit)) {
                            st.append("X ");
                        } else {
                            st.append("O ");
                        }
                    }
                }
                statusText2.setText(new String(st));
                statusText2.setVisible(true);
                // update bit pattern for next entry
                curOutBit++;
                if (curOutBit > 7) {
                    // Move to the next byte
                    curOutBit = 0;
                    outBytes[curOutByte] = 0;
                    curOutByte++;
                    if (curOutByte > endOutByte) {
                        // Pattern complete, recycle to first byte
                        curOutByte = begOutByte;
                        numIterations++;
                    }
                }
            }
        }
    });
    // start timer        
    outTimer.start();
}
Also used : Timer(javax.swing.Timer) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) SerialMessage(jmri.jmrix.cmri.serial.SerialMessage) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 9 with Timer

use of javax.swing.Timer in project JMRI by JMRI.

the class ClientActions method buttonForList.

JButton buttonForList(final java.util.ArrayList<JButton> list, String label) {
    JButton b = new JButton(label);
    b.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            //milliseconds
            int delay = 0;
            for (final JButton b : list) {
                ActionListener taskPerformer = new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        target.doClick();
                    }

                    JButton target = b;
                };
                Timer t = new Timer(delay, taskPerformer);
                t.setRepeats(false);
                t.start();
                delay = delay + 150;
            }
        }
    });
    return b;
}
Also used : ActionListener(java.awt.event.ActionListener) ActionListener(java.awt.event.ActionListener) Timer(javax.swing.Timer) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) ActionEvent(java.awt.event.ActionEvent)

Example 10 with Timer

use of javax.swing.Timer in project JMRI by JMRI.

the class LightControl method activateLightControl.

/**
     * Activates a Light Control by control type. This method tests the control
     * type, and set up a control mechanism, appropriate for the control type.
     */
public void activateLightControl() {
    // skip if Light Control is already active
    if (!_active) {
        // activate according to control type
        switch(_controlType) {
            case Light.SENSOR_CONTROL:
                _namedControlSensor = null;
                if (_controlSensorName.length() > 0) {
                    Sensor sen = InstanceManager.sensorManagerInstance().provideSensor(_controlSensorName);
                    _namedControlSensor = nbhm.getNamedBeanHandle(_controlSensorName, sen);
                }
                if (_namedControlSensor != null) {
                    // if sensor state is currently known, set light accordingly
                    int kState = _namedControlSensor.getBean().getKnownState();
                    if (kState == Sensor.ACTIVE) {
                        if (_controlSensorSense == Sensor.ACTIVE) {
                            // Turn light on
                            _parentLight.setState(Light.ON);
                        } else {
                            // Turn light off
                            _parentLight.setState(Light.OFF);
                        }
                    } else if (kState == Sensor.INACTIVE) {
                        if (_controlSensorSense == Sensor.INACTIVE) {
                            // Turn light on
                            _parentLight.setState(Light.ON);
                        } else {
                            // Turn light off
                            _parentLight.setState(Light.OFF);
                        }
                    }
                    // listen for change in sensor state
                    _namedControlSensor.getBean().addPropertyChangeListener(_sensorListener = new java.beans.PropertyChangeListener() {

                        @Override
                        public void propertyChange(java.beans.PropertyChangeEvent e) {
                            if (!_parentLight.getEnabled()) {
                                // ignore property change if user disabled Light
                                return;
                            }
                            if (e.getPropertyName().equals("KnownState")) {
                                int now = _namedControlSensor.getBean().getKnownState();
                                if (now == Sensor.ACTIVE) {
                                    if (_controlSensorSense == Sensor.ACTIVE) {
                                        // Turn light on
                                        _parentLight.setState(Light.ON);
                                    } else {
                                        // Turn light off
                                        _parentLight.setState(Light.OFF);
                                    }
                                } else if (now == Sensor.INACTIVE) {
                                    if (_controlSensorSense == Sensor.INACTIVE) {
                                        // Turn light on
                                        _parentLight.setState(Light.ON);
                                    } else {
                                        // Turn light off
                                        _parentLight.setState(Light.OFF);
                                    }
                                }
                            }
                        }
                    }, _controlSensorName, "Light Control " + _parentLight.getDisplayName());
                    _active = true;
                } else {
                    // control sensor does not exist
                    log.error("Light " + _parentLight.getSystemName() + " is linked to a Sensor that does not exist: " + _controlSensorName);
                    return;
                }
                break;
            case Light.FAST_CLOCK_CONTROL:
                if (_clock == null) {
                    _clock = InstanceManager.getDefault(jmri.Timebase.class);
                }
                // set up time as minutes in a day
                _timeOn = _fastClockOnHour * 60 + _fastClockOnMin;
                _timeOff = _fastClockOffHour * 60 + _fastClockOffMin;
                // initialize light based on current fast time
                updateClockControlLight();
                // set up to listen for time changes on a minute basis
                _clock.addMinuteChangeListener(_timebaseListener = new java.beans.PropertyChangeListener() {

                    @Override
                    public void propertyChange(java.beans.PropertyChangeEvent e) {
                        if (_parentLight.getEnabled()) {
                            // don't change light if not enabled
                            // update control if light is enabled
                            updateClockControlLight();
                        }
                    }
                });
                _active = true;
                break;
            case Light.TURNOUT_STATUS_CONTROL:
                try {
                    _controlTurnout = InstanceManager.turnoutManagerInstance().provideTurnout(_controlTurnoutName);
                } catch (IllegalArgumentException e) {
                    // control turnout does not exist
                    log.error("Light " + _parentLight.getSystemName() + " is linked to a Turnout that does not exist: " + _controlSensorName);
                    return;
                }
                // set light based on current turnout state if known
                int tState = _controlTurnout.getKnownState();
                if (tState == Turnout.CLOSED) {
                    if (_turnoutState == Turnout.CLOSED) {
                        // Turn light on
                        _parentLight.setState(Light.ON);
                    } else {
                        // Turn light off
                        _parentLight.setState(Light.OFF);
                    }
                } else if (tState == Turnout.THROWN) {
                    if (_turnoutState == Turnout.THROWN) {
                        // Turn light on
                        _parentLight.setState(Light.ON);
                    } else {
                        // Turn light off
                        _parentLight.setState(Light.OFF);
                    }
                }
                // listen for change in turnout state
                _controlTurnout.addPropertyChangeListener(_turnoutListener = new java.beans.PropertyChangeListener() {

                    @Override
                    public void propertyChange(java.beans.PropertyChangeEvent e) {
                        if (!_parentLight.getEnabled()) {
                            // ignore property change if user disabled light
                            return;
                        }
                        if (e.getPropertyName().equals("KnownState")) {
                            int now = _controlTurnout.getKnownState();
                            if (now == Turnout.CLOSED) {
                                if (_turnoutState == Turnout.CLOSED) {
                                    // Turn light on
                                    _parentLight.setState(Light.ON);
                                } else {
                                    // Turn light off
                                    _parentLight.setState(Light.OFF);
                                }
                            } else if (now == Turnout.THROWN) {
                                if (_turnoutState == Turnout.THROWN) {
                                    // Turn light on
                                    _parentLight.setState(Light.ON);
                                } else {
                                    // Turn light off
                                    _parentLight.setState(Light.OFF);
                                }
                            }
                        }
                    }
                });
                _active = true;
                break;
            case Light.TIMED_ON_CONTROL:
                if (_timedSensorName.length() > 0) {
                    Sensor sen = InstanceManager.sensorManagerInstance().provideSensor(_timedSensorName);
                    _namedTimedControlSensor = nbhm.getNamedBeanHandle(_timedSensorName, sen);
                }
                if (_namedTimedControlSensor != null) {
                    // set initial state off
                    _parentLight.setState(Light.OFF);
                    // listen for change in timed control sensor state
                    _namedTimedControlSensor.getBean().addPropertyChangeListener(_timedSensorListener = new java.beans.PropertyChangeListener() {

                        @Override
                        public void propertyChange(java.beans.PropertyChangeEvent e) {
                            if (!_parentLight.getEnabled()) {
                                // ignore property change if user disabled light
                                return;
                            }
                            if (e.getPropertyName().equals("KnownState")) {
                                int now = _namedTimedControlSensor.getBean().getKnownState();
                                if (!_lightOnTimerActive) {
                                    if (now == Sensor.ACTIVE) {
                                        // Turn light on
                                        _parentLight.setState(Light.ON);
                                        // Create a timer if one does not exist
                                        if (_timedControlTimer == null) {
                                            _timedControlListener = new TimeLight();
                                            _timedControlTimer = new Timer(_timeOnDuration, _timedControlListener);
                                        }
                                        // Start the Timer to turn the light OFF
                                        _lightOnTimerActive = true;
                                        _timedControlTimer.start();
                                    }
                                }
                            }
                        }
                    }, _timedSensorName, "Light Control " + _parentLight.getDisplayName());
                    _active = true;
                } else {
                    // timed control sensor does not exist
                    log.error("Light " + _parentLight.getSystemName() + " is linked to a Sensor that does not exist: " + _timedSensorName);
                    return;
                }
                break;
            case Light.TWO_SENSOR_CONTROL:
                _namedControlSensor = null;
                _namedControlSensor2 = null;
                if (_controlSensorName.length() > 0) {
                    Sensor sen = InstanceManager.sensorManagerInstance().provideSensor(_controlSensorName);
                    _namedControlSensor = nbhm.getNamedBeanHandle(_controlSensorName, sen);
                }
                if (_controlSensor2Name.length() > 0) {
                    Sensor sen = InstanceManager.sensorManagerInstance().provideSensor(_controlSensor2Name);
                    _namedControlSensor2 = nbhm.getNamedBeanHandle(_controlSensor2Name, sen);
                }
                if ((_namedControlSensor != null) && (_namedControlSensor2 != null)) {
                    // if sensor state is currently known, set light accordingly
                    int kState = _namedControlSensor.getBean().getKnownState();
                    int kState2 = _namedControlSensor2.getBean().getKnownState();
                    if (_controlSensorSense == Sensor.ACTIVE) {
                        if ((kState == Sensor.ACTIVE) || (kState2 == Sensor.ACTIVE)) {
                            // Turn light on
                            _parentLight.setState(Light.ON);
                        } else {
                            // Turn light off
                            _parentLight.setState(Light.OFF);
                        }
                    } else if (_controlSensorSense == Sensor.INACTIVE) {
                        if ((kState == Sensor.INACTIVE) || (kState2 == Sensor.INACTIVE)) {
                            // Turn light on
                            _parentLight.setState(Light.ON);
                        } else {
                            // Turn light off
                            _parentLight.setState(Light.OFF);
                        }
                    }
                    // listen for change in sensor states
                    _namedControlSensor.getBean().addPropertyChangeListener(_sensorListener = new java.beans.PropertyChangeListener() {

                        @Override
                        public void propertyChange(java.beans.PropertyChangeEvent e) {
                            twoSensorChanged(e);
                        }
                    }, _controlSensorName, "Light Control " + _parentLight.getDisplayName());
                    _namedControlSensor2.getBean().addPropertyChangeListener(_sensor2Listener = new java.beans.PropertyChangeListener() {

                        @Override
                        public void propertyChange(java.beans.PropertyChangeEvent e) {
                            twoSensorChanged(e);
                        }
                    }, _controlSensor2Name, "Light Control " + _parentLight.getDisplayName());
                    _active = true;
                } else {
                    // at least one control sensor does not exist
                    log.error("Light " + _parentLight.getSystemName() + " is linked to a Sensor that does not exist: ");
                    return;
                }
                break;
            default:
                log.warn("Unexpected control type when activating Light: " + _parentLight.getSystemName());
        }
    }
}
Also used : Timebase(jmri.Timebase) Timer(javax.swing.Timer) Sensor(jmri.Sensor)

Aggregations

Timer (javax.swing.Timer)130 ActionEvent (java.awt.event.ActionEvent)66 ActionListener (java.awt.event.ActionListener)62 IOException (java.io.IOException)12 JPanel (javax.swing.JPanel)12 JLabel (javax.swing.JLabel)11 BorderLayout (java.awt.BorderLayout)9 Dimension (java.awt.Dimension)9 Point (java.awt.Point)9 Color (java.awt.Color)8 JCheckBox (javax.swing.JCheckBox)8 MouseEvent (java.awt.event.MouseEvent)7 JFrame (javax.swing.JFrame)7 MouseAdapter (java.awt.event.MouseAdapter)6 Window (java.awt.Window)5 JButton (javax.swing.JButton)5 JDialog (javax.swing.JDialog)5 JScrollPane (javax.swing.JScrollPane)5 File (java.io.File)4 Date (java.util.Date)4