17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright (c) 1999-2000 by Sun Microsystems, Inc. 247c478bd9Sstevel@tonic-gate * All rights reserved. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate import java.awt.*; 287c478bd9Sstevel@tonic-gate import java.awt.event.*; 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /** 317c478bd9Sstevel@tonic-gate * Creates a panel with two buttons (+ and - side by side on it). The 327c478bd9Sstevel@tonic-gate * panel registers a DCListener with it that gets notified whenever 337c478bd9Sstevel@tonic-gate * these butons are clicked. <bold>The buttons may also be kept continously 347c478bd9Sstevel@tonic-gate * pressed for faster increments/decrements.</bold> 357c478bd9Sstevel@tonic-gate * <para> 367c478bd9Sstevel@tonic-gate * On a single click of the button, the listener is notified to 377c478bd9Sstevel@tonic-gate * increment/decrement itself by a small amount. When the button is kept 387c478bd9Sstevel@tonic-gate * pressed the following notifications are sent out for larger 397c478bd9Sstevel@tonic-gate * increments/decrements. (It is up to the listener to decide the 407c478bd9Sstevel@tonic-gate * increment/decrement corresponding to large/small.) Moreover, these 417c478bd9Sstevel@tonic-gate * notifications will be sent out much faster if the button is kept 427c478bd9Sstevel@tonic-gate * pressed. 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate // The panel waits for a period of BIG_SLEEP_TIME before the faster 467c478bd9Sstevel@tonic-gate // increments are sent out. They, in turn, are sent out after 477c478bd9Sstevel@tonic-gate // intervals of SMALL_SLEEP_TIME. Therfore, an instance of this class 487c478bd9Sstevel@tonic-gate // is associated with 2 timers - a longer one that starts off and then 497c478bd9Sstevel@tonic-gate // schedules the shorter one. The shorter one keeps scheduling itself 507c478bd9Sstevel@tonic-gate // every time it wakes up. 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate public class DCPanel extends Panel { 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate private Button plusButton; 557c478bd9Sstevel@tonic-gate private Button minusButton; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate private DCListener listener = null; 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate private Timer bigTimer; 607c478bd9Sstevel@tonic-gate private Timer smallTimer; 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate private static int BIG_SLEEP_TIME = 1000; 637c478bd9Sstevel@tonic-gate private static int SMALL_SLEEP_TIME = 100; 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate private boolean incrementFlag; 667c478bd9Sstevel@tonic-gate DCPanel()677c478bd9Sstevel@tonic-gate public DCPanel() { 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate setLayout(new GridLayout(1, 2)); 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate bigTimer = new BigTimer(); 727c478bd9Sstevel@tonic-gate smallTimer = new SmallTimer(); 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate bigTimer.start(); 757c478bd9Sstevel@tonic-gate smallTimer.start(); 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate plusButton = new DCButton("+"); 787c478bd9Sstevel@tonic-gate minusButton = new DCButton("-"); 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate add(plusButton); 817c478bd9Sstevel@tonic-gate add(minusButton); 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate } 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate /** 867c478bd9Sstevel@tonic-gate * Ensures that this component is not brought into focus by 877c478bd9Sstevel@tonic-gate * tabbing. This prevents the tab focus from moving in here instead 887c478bd9Sstevel@tonic-gate * of going to a text field. 897c478bd9Sstevel@tonic-gate * @return false always. 907c478bd9Sstevel@tonic-gate */ isFocusable()91*a89721aeSRichard PALO public boolean isFocusable() { 927c478bd9Sstevel@tonic-gate return false; 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate /** 967c478bd9Sstevel@tonic-gate * Sets the listener for this tab. 977c478bd9Sstevel@tonic-gate * @param listener the DCListener that needs to be notified when the 987c478bd9Sstevel@tonic-gate * buttons on this panel are pressed. 997c478bd9Sstevel@tonic-gate * @return the old listener 1007c478bd9Sstevel@tonic-gate */ setListener(DCListener listener)1017c478bd9Sstevel@tonic-gate public DCListener setListener(DCListener listener) { 1027c478bd9Sstevel@tonic-gate DCListener oldListener = this.listener; 1037c478bd9Sstevel@tonic-gate this.listener = listener; 1047c478bd9Sstevel@tonic-gate return oldListener; 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /** 1087c478bd9Sstevel@tonic-gate * Removes the listener when it no longer need to be notified. 1097c478bd9Sstevel@tonic-gate * @return the old listener 1107c478bd9Sstevel@tonic-gate */ removeListener()1117c478bd9Sstevel@tonic-gate public DCListener removeListener() { 1127c478bd9Sstevel@tonic-gate return setListener(null); 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate /** 1167c478bd9Sstevel@tonic-gate * Kicks the times into action. Is called when a button is pressed. 1177c478bd9Sstevel@tonic-gate */ startAction()1187c478bd9Sstevel@tonic-gate private void startAction() { 1197c478bd9Sstevel@tonic-gate bigTimer.request(); 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate /** 1237c478bd9Sstevel@tonic-gate * Stops the timers. Is called when a button is released. 1247c478bd9Sstevel@tonic-gate */ stopAction()1257c478bd9Sstevel@tonic-gate private void stopAction() { 1267c478bd9Sstevel@tonic-gate smallTimer.cancel(); 1277c478bd9Sstevel@tonic-gate bigTimer.cancel(); 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate /** 1317c478bd9Sstevel@tonic-gate * Notifies the listener about whether to increment or decrement and 1327c478bd9Sstevel@tonic-gate * by how much. 1337c478bd9Sstevel@tonic-gate * @param bigFlag true if the listener needs to increment/decrement 1347c478bd9Sstevel@tonic-gate * by a large amount, false otherwise. 1357c478bd9Sstevel@tonic-gate */ informListener(boolean bigFlag)1367c478bd9Sstevel@tonic-gate private void informListener(boolean bigFlag) { 1377c478bd9Sstevel@tonic-gate // System.out.println("DCPanel.informListener: " + bigFlag); 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate if (listener != null) { 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate if (bigFlag) { 1427c478bd9Sstevel@tonic-gate // request a big change 1437c478bd9Sstevel@tonic-gate if (incrementFlag) 1447c478bd9Sstevel@tonic-gate listener.bigIncrement(); 1457c478bd9Sstevel@tonic-gate else 1467c478bd9Sstevel@tonic-gate listener.bigDecrement(); 1477c478bd9Sstevel@tonic-gate } else { 1487c478bd9Sstevel@tonic-gate // request a small change 1497c478bd9Sstevel@tonic-gate if (incrementFlag) 1507c478bd9Sstevel@tonic-gate listener.increment(); 1517c478bd9Sstevel@tonic-gate else 1527c478bd9Sstevel@tonic-gate listener.decrement(); 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate } // informListener 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate // *********************************************** 1617c478bd9Sstevel@tonic-gate // I N N E R C L A S S E S F O L L O W 1627c478bd9Sstevel@tonic-gate // *********************************************** 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate /** 1657c478bd9Sstevel@tonic-gate * A timer class since java does not have one. 1667c478bd9Sstevel@tonic-gate */ 1677c478bd9Sstevel@tonic-gate private abstract class Timer extends Thread { 1687c478bd9Sstevel@tonic-gate private boolean running = false; 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate /** 1717c478bd9Sstevel@tonic-gate * Sleeps till the timer's services are requested using wait() and 1727c478bd9Sstevel@tonic-gate * notify(). Then it does its task and goes back to sleep. And 1737c478bd9Sstevel@tonic-gate * loops forever like this. 1747c478bd9Sstevel@tonic-gate */ run()1757c478bd9Sstevel@tonic-gate public void run() { 1767c478bd9Sstevel@tonic-gate while (true) { 1777c478bd9Sstevel@tonic-gate try { 1787c478bd9Sstevel@tonic-gate synchronized (this) { 1797c478bd9Sstevel@tonic-gate running = false; 1807c478bd9Sstevel@tonic-gate // Wait till the timer is required 1817c478bd9Sstevel@tonic-gate wait(); 1827c478bd9Sstevel@tonic-gate running = true; 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate doTask(); 1857c478bd9Sstevel@tonic-gate } catch (InterruptedException e) {} 1867c478bd9Sstevel@tonic-gate } // while loop 1877c478bd9Sstevel@tonic-gate } // run method 1887c478bd9Sstevel@tonic-gate doTask()1897c478bd9Sstevel@tonic-gate protected void doTask() {} // bug in java workshop 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate /** 1927c478bd9Sstevel@tonic-gate * Wakes up the timer. 1937c478bd9Sstevel@tonic-gate */ request()1947c478bd9Sstevel@tonic-gate public synchronized void request() { 1957c478bd9Sstevel@tonic-gate notify(); 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate /** 1997c478bd9Sstevel@tonic-gate * Cancels the timer if it is running. 2007c478bd9Sstevel@tonic-gate */ cancel()2017c478bd9Sstevel@tonic-gate public void cancel() { 2027c478bd9Sstevel@tonic-gate if (running) { 2037c478bd9Sstevel@tonic-gate interrupt(); 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate }// class Timer 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate /** 2107c478bd9Sstevel@tonic-gate * The first stage of timer - is a longer timer. Wait to see if the 2117c478bd9Sstevel@tonic-gate * user really wants to amek the increments/decrements go by fast. 2127c478bd9Sstevel@tonic-gate */ 2137c478bd9Sstevel@tonic-gate private class BigTimer extends Timer { 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate /** 2167c478bd9Sstevel@tonic-gate * Sleep for the long amount of time. Then inform the listener 2177c478bd9Sstevel@tonic-gate * to have a bigIncrement/bigDecrement. After that, your job is 2187c478bd9Sstevel@tonic-gate * done, schedule the smaller (faster) timer from this point on. 2197c478bd9Sstevel@tonic-gate */ doTask()2207c478bd9Sstevel@tonic-gate protected void doTask() { 2217c478bd9Sstevel@tonic-gate try { 2227c478bd9Sstevel@tonic-gate sleep(BIG_SLEEP_TIME); 2237c478bd9Sstevel@tonic-gate informListener(true); 2247c478bd9Sstevel@tonic-gate smallTimer.request(); 2257c478bd9Sstevel@tonic-gate } catch (InterruptedException e) { 2267c478bd9Sstevel@tonic-gate informListener(false); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate } // class BigTimer 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate /** 2347c478bd9Sstevel@tonic-gate * The second stage of timers. This timer keeps rescheduling itself 2357c478bd9Sstevel@tonic-gate * everytime it wakes up. In between this, it sends a notification 2367c478bd9Sstevel@tonic-gate * to the listener to do a big Increment/Decrement. 2377c478bd9Sstevel@tonic-gate */ 2387c478bd9Sstevel@tonic-gate private class SmallTimer extends Timer { 2397c478bd9Sstevel@tonic-gate doTask()2407c478bd9Sstevel@tonic-gate protected void doTask() { 2417c478bd9Sstevel@tonic-gate try { 2427c478bd9Sstevel@tonic-gate // loop forever and keep rescheduling yourself 2437c478bd9Sstevel@tonic-gate while (true) { 2447c478bd9Sstevel@tonic-gate sleep(SMALL_SLEEP_TIME); 2457c478bd9Sstevel@tonic-gate informListener(true); 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate } catch (InterruptedException e) {} 2487c478bd9Sstevel@tonic-gate } // doTask method 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate } // class SmallTimer 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate /** 2537c478bd9Sstevel@tonic-gate * A mouse listener to detect when a button has been 2547c478bd9Sstevel@tonic-gate * pressed/released. One instance of this is bound to the plus 2557c478bd9Sstevel@tonic-gate * button and the other instance to the minus button. 2567c478bd9Sstevel@tonic-gate */ 2577c478bd9Sstevel@tonic-gate private class DCMouseListener extends MouseAdapter { 2587c478bd9Sstevel@tonic-gate private boolean plusOrMinus; 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate /** 2617c478bd9Sstevel@tonic-gate * Constructor for DCMouseListener. 2627c478bd9Sstevel@tonic-gate * @param plusOrMinus true if this is a listener for the plus 2637c478bd9Sstevel@tonic-gate * button, false if it is for the minus button. 2647c478bd9Sstevel@tonic-gate */ DCMouseListener(boolean plusOrMinus)2657c478bd9Sstevel@tonic-gate public DCMouseListener(boolean plusOrMinus) { 2667c478bd9Sstevel@tonic-gate this.plusOrMinus = plusOrMinus; 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate /** 2707c478bd9Sstevel@tonic-gate * Kicks in when the mouse is pressed. 2717c478bd9Sstevel@tonic-gate */ mousePressed(MouseEvent e)2727c478bd9Sstevel@tonic-gate public void mousePressed(MouseEvent e) { 2737c478bd9Sstevel@tonic-gate incrementFlag = plusOrMinus; 2747c478bd9Sstevel@tonic-gate DCPanel.this.startAction(); 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate /** 2787c478bd9Sstevel@tonic-gate * Kicks in when the mouse is released. 2797c478bd9Sstevel@tonic-gate */ mouseReleased(MouseEvent e)2807c478bd9Sstevel@tonic-gate public void mouseReleased(MouseEvent e) { 2817c478bd9Sstevel@tonic-gate incrementFlag = plusOrMinus; 2827c478bd9Sstevel@tonic-gate DCPanel.this.stopAction(); 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate /** 2877c478bd9Sstevel@tonic-gate * The button used by this DCPanel. 2887c478bd9Sstevel@tonic-gate */ 2897c478bd9Sstevel@tonic-gate private class DCButton extends Button { DCButton(String text)2907c478bd9Sstevel@tonic-gate public DCButton(String text) { 2917c478bd9Sstevel@tonic-gate super(text); 2927c478bd9Sstevel@tonic-gate if (text.equals("+")) 2937c478bd9Sstevel@tonic-gate addMouseListener(new DCMouseListener(true)); 2947c478bd9Sstevel@tonic-gate else 2957c478bd9Sstevel@tonic-gate addMouseListener(new DCMouseListener(false)); 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate /** 2997c478bd9Sstevel@tonic-gate * Make the button non-focus traversable so that it cannot be 3007c478bd9Sstevel@tonic-gate * tabbed in to. 3017c478bd9Sstevel@tonic-gate */ isFocusable()302*a89721aeSRichard PALO public boolean isFocusable() { 3037c478bd9Sstevel@tonic-gate return false; 3047c478bd9Sstevel@tonic-gate } 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate } // DCButton 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate /** 3107c478bd9Sstevel@tonic-gate * Test method for DCPanel class to see appearance. 3117c478bd9Sstevel@tonic-gate */ main(String args[])3127c478bd9Sstevel@tonic-gate public static void main(String args[]) { 3137c478bd9Sstevel@tonic-gate Frame f = new Frame("Testing DCPanel"); 3147c478bd9Sstevel@tonic-gate f.add(new DCPanel()); 3157c478bd9Sstevel@tonic-gate f.setBounds(new Rectangle(100, 100, 100, 100)); 3167c478bd9Sstevel@tonic-gate f.setVisible(true); 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate } 320