RotaryButton.java
     1: //========================================================================================
     2: //  RotaryButton.java
     3: //    en:Rotary button
     4: //    ja:回転式ボタン
     5: //  Copyright (C) 2003-2025 Makoto Kamada
     6: //
     7: //  This file is part of the XEiJ (X68000 Emulator in Java).
     8: //  You can use, modify and redistribute the XEiJ if the conditions are met.
     9: //  Read the XEiJ License for more details.
    10: //  https://stdkmd.net/xeij/
    11: //========================================================================================
    12: 
    13: package xeij;
    14: 
    15: import java.awt.event.*;
    16: import java.util.*;
    17: import javax.swing.*;
    18: 
    19: //class RotaryButton
    20: //  回転式ボタン
    21: //  押す度にテキストまたはアイコンが入れ替わるボタン
    22: @SuppressWarnings ("this-escape")
    23: public class RotaryButton extends JButton implements ActionListener {
    24: 
    25:   protected int index;  //インデックス。現在のテキストまたはアイコンの番号
    26:   protected String[] texts;  //テキスト
    27:   protected Icon[] icons;  //アイコン
    28:   protected ArrayList<ActionListener> listeners;  //ユーザーのアクションリスナーのリスト
    29: 
    30:   //button = new RotaryButton (index, text, ...)
    31:   //  テキストが入れ替わる回転式ボタンのコンストラクタ
    32:   //  サイズを調整しない。必要ならば最小サイズを設定すること
    33:   public RotaryButton (int index, String... texts) {
    34:     super (texts[index]);
    35:     this.texts = texts;
    36:     this.index = index;
    37:     listeners = new ArrayList<ActionListener> ();
    38:     super.addActionListener (this);
    39:   }  //new RotaryButton
    40: 
    41:   //button = new RotaryButton (index, icon, ...)
    42:   //  アイコンが入れ替わる回転式ボタンのコンストラクタ
    43:   public RotaryButton (int index, Icon... icons) {
    44:     super (icons[index]);
    45:     this.icons = icons;
    46:     this.index = index;
    47:     listeners = new ArrayList<ActionListener> ();
    48:     super.addActionListener (this);
    49:   }  //new RotaryButton
    50: 
    51:   //index = getIndex ()
    52:   //  インデックスを取得する
    53:   //  ユーザーのアクションリスナーから呼び出したとき更新後のインデックスが返る
    54:   public int getIndex () {
    55:     return index;
    56:   }  //getIndex
    57: 
    58:   //setIndex (index)
    59:   //  インデックスを設定する
    60:   //  アクションイベントは発火しない
    61:   public void setIndex (int index) {
    62:     if (this.index != index) {
    63:       this.index = index;
    64:       if (texts != null) {
    65:         setText (texts[index]);
    66:       } else if (icons != null) {
    67:         setIcon (icons[index]);
    68:       }
    69:     }
    70:   }  //setIndex
    71: 
    72:   //actionPerformed (ae)
    73:   //  自分のアクションリスナー
    74:   //  インデックスを更新した後にユーザーのアクションリスナーを呼び出す
    75:   @Override public void actionPerformed (ActionEvent ae) {
    76:     if (texts != null) {
    77:       index = index + 1 < texts.length ? index + 1 : 0;
    78:       setText (texts[index]);
    79:     } else if (icons != null) {
    80:       index = index + 1 < icons.length ? index + 1 : 0;
    81:       setIcon (icons[index]);
    82:     }
    83:     for (ActionListener listener : listeners) {
    84:       listener.actionPerformed (ae);
    85:     }
    86:   }  //actionPerformed
    87: 
    88:   //addActionListener (listener)
    89:   //  ユーザーのアクションリスナーを追加する
    90:   @Override public void addActionListener (ActionListener listener) {
    91:     if (listener != null) {
    92:       listeners.add (listener);
    93:     }
    94:   }  //addActionListener
    95: 
    96:   //listeners = getActionListeners ()
    97:   //  ユーザーのアクションリスナーの配列を返す
    98:   @Override public ActionListener[] getActionListeners () {
    99:     return listeners.toArray (new ActionListener[listeners.size ()]);
   100:   }  //getActionListeners
   101: 
   102:   //removeActionListener (listener)
   103:   //  ユーザーのアクションリスナーを削除する
   104:   @Override public void removeActionListener (ActionListener listener) {
   105:     listeners.remove (listener);
   106:   }  //removeActionListener
   107: 
   108: }  //class RotaryButton