DecimalSpinner.java
     1: //========================================================================================
     2: //  DecimalSpinner.java
     3: //    en:Decimal spinner
     4: //    ja:10進数スピナー
     5: //  Copyright (C) 2003-2024 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.*;  //BasicStroke,BorderLayout,BoxLayout,Color,Component,Container,Cursor,Desktop,Dimension,Font,Frame,Graphics,Graphics2D,GraphicsDevice,GraphicsEnvironment,GridLayout,Image,Insets,Paint,Point,Rectangle,RenderingHints,Robot,Shape,Stroke,TexturePaint,Toolkit
    16: import java.lang.*;  //Boolean,Character,Class,Comparable,Double,Exception,Float,IllegalArgumentException,Integer,Long,Math,Number,Object,Runnable,SecurityException,String,StringBuilder,System
    17: import javax.swing.*;  //AbstractSpinnerModel,Box,ButtonGroup,DefaultListModel,ImageIcon,JApplet,JButton,JCheckBox,JCheckBoxMenuItem,JDialog,JFileChooser,JFrame,JLabel,JList,JMenu,JMenuBar,JMenuItem,JPanel,JRadioButton,JScrollPane,JSpinner,JTextArea,JTextField,JTextPane,JViewport,ScrollPaneConstants,SpinnerListModel,SpinnerNumberModel,SwingConstants,SwingUtilities,UIManager,UIDefaults,UnsupportedLookAndFeelException
    18: import javax.swing.border.*;  //Border,CompoundBorder,EmptyBorder,EtchedBorder,LineBorder,TitledBorder
    19: 
    20: public class DecimalSpinner extends NumberSpinner {
    21: 
    22:   protected int minimum;  //最小値。model.getMinimum()はintではない
    23:   protected int maximum;  //最大値
    24:   protected int option;  //オプション。多数のスピナーをリスナーの中で効率よく見分けたいときに番号などを入れる
    25: 
    26:   //DecimalSpinner (value, minimum, maximum, stepSize)
    27:   //DecimalSpinner (value, minimum, maximum, stepSize, option)
    28:   //  コンストラクタ
    29:   public DecimalSpinner (int value, int minimum, int maximum, int stepSize) {
    30:     this (value, minimum, maximum, stepSize, 0);
    31:   }  //new DecimalSpinner(int,int,int,int)
    32:   @SuppressWarnings ("this-escape") public DecimalSpinner (int value, int minimum, int maximum, int stepSize, int option) {
    33:     super (new SpinnerNumberModel (value, minimum, maximum, stepSize));  //minimum<=value<=maximumでなければここでIllegalArgumentExceptionが発生する
    34:     this.minimum = minimum;
    35:     this.maximum = maximum;
    36:     this.option = option;
    37:     this.setBorder (new LineBorder (new Color (LnF.lnfRGB[10]), 1));  //[this-escape]
    38:     int digits = String.valueOf (maximum).length ();  //maximumの桁数
    39:     ComponentFactory.setFixedSize (this, 24 + (LnF.lnfFontSize * 2 / 3) * digits, LnF.lnfFontSize + 4);
    40:     JSpinner.NumberEditor editor = (JSpinner.NumberEditor) this.getEditor ();
    41:     editor.getFormat ().setGroupingUsed (false);  //3桁毎にグループ化しない
    42:     JTextField textField = editor.getTextField ();
    43:     textField.setText (String.valueOf (value));  //初回は3桁毎にグループ化されているので上書きする
    44:     textField.setHorizontalAlignment (JTextField.CENTER);  //中央寄せ
    45:     //textField.setFont (LnF.lnfMonospacedFont);
    46:   }  //new DecimalSpinner(int,int,int,int,int)
    47: 
    48:   //value = spinner.getIntValue ()
    49:   //  値を読み出す
    50:   //  範囲外の値が返ることはない
    51:   public int getIntValue () {
    52:     return this.model.getNumber ().intValue ();
    53:   }  //getIntValue()
    54: 
    55:   //spinner.setIntValue (value)
    56:   //  値を書き込む
    57:   //  範囲外の値は設定できない
    58:   //  チェンジリスナーが呼び出される
    59:   public void setIntValue (int value) {
    60:     if (value < minimum || maximum < value) {
    61:       throw new IllegalArgumentException ();
    62:     }
    63:     this.model.setValue (Integer.valueOf (value));
    64:   }  //spinner.setIntValue(int)
    65: 
    66:   //option = spinner.getOption ()
    67:   //  オプションを読み出す
    68:   public int getOption () {
    69:     return this.option;
    70:   }  //spinner.getOption()
    71: 
    72:   //spinner.setOption(option)
    73:   //  オプションを書き込む
    74:   public void setOption (int option) {
    75:     this.option = option;
    76:   }  //spinner.setOption(int)
    77: 
    78: }  //class DecimalSpinner
    79: 
    80: 
    81: