AbstractOpenDialog.java
     1: //========================================================================================
     2: //  AbstractOpenDialog.java
     3: //    en:Dialog to open image files
     4: //    ja:イメージファイルを開くダイアログ
     5: //  Copyright (C) 2003-2023 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.*;  //Frame
    16: import java.awt.event.*;  //ActionListener
    17: import java.io.*;  //File
    18: import java.util.*;  //ArrayList
    19: import javax.swing.*;  //JDialog
    20: import javax.swing.filechooser.*;  //FileFilter
    21: 
    22: public abstract class AbstractOpenDialog extends JDialog implements ActionListener {
    23: 
    24:   //ファイルチューザー
    25:   protected JFileChooser2 fileChooser;
    26: 
    27:   //コンポーネント
    28:   protected JCheckBox readOnlyCheckBox;
    29:   protected JRadioButton rebootRadioButton;
    30:   protected JRadioButton openRadioButton;
    31: 
    32:   //フラグ
    33:   protected boolean directory;  //true=ディレクトリを開く,false=ファイルを開く
    34:   protected boolean readOnly;  //true=書き込み禁止,false=書き込み許可
    35:   protected boolean reboot;  //true=ここから再起動,false=開く
    36: 
    37:   //super (owner, title, jaTitle, directory, fileFilter)
    38:   //  コンストラクタ
    39:   @SuppressWarnings ("this-escape") public AbstractOpenDialog (
    40:     Frame owner, String title, String jaTitle, boolean directory,
    41:     javax.swing.filechooser.FileFilter fileFilter) {
    42:     super (owner, title, true);  //モーダル
    43:     this.setUndecorated (true);  //[this-escape]ウインドウの枠を消す
    44:     this.getRootPane ().setWindowDecorationStyle (JRootPane.FRAME);  //飾り枠を描く
    45:     this.setAlwaysOnTop (true);  //常に手前に表示
    46:     this.setLocationByPlatform (true);
    47:     this.setDefaultCloseOperation (WindowConstants.HIDE_ON_CLOSE);
    48: 
    49:     Multilingual.mlnTitle (this, "ja", jaTitle);
    50:     this.directory = directory;
    51: 
    52:     //ファイルチューザー
    53:     fileChooser = new JFileChooser2 ();
    54:     fileChooser.setMultiSelectionEnabled (true);  //複数選択可能
    55:     fileChooser.setControlButtonsAreShown (false);  //デフォルトのボタンを消す
    56:     fileChooser.setFileFilter (fileFilter);
    57:     fileChooser.addActionListener (this);
    58: 
    59:     //コンポーネント
    60:     ButtonGroup approveGroup = new ButtonGroup ();
    61:     JComponent component =
    62:       ComponentFactory.createBorderPanel (
    63:         0, 0,
    64:         ComponentFactory.createVerticalBox (
    65:           fileChooser,
    66:           ComponentFactory.createHorizontalBox (
    67:             Box.createHorizontalStrut (12),
    68:             Box.createHorizontalGlue (),
    69:             readOnlyCheckBox =
    70:             Multilingual.mlnText (
    71:               ComponentFactory.createCheckBox (
    72:                 readOnly, "Read-only", this),
    73:               "ja", "書き込み禁止"),
    74:             Box.createHorizontalGlue (),
    75:             rebootRadioButton =
    76:             ComponentFactory.setFixedSize (
    77:               ComponentFactory.setText (
    78:                 ComponentFactory.createRadioButton (
    79:                   approveGroup, reboot, "approve reboot", this),
    80:                 ""),
    81:               20, 20),
    82:             Multilingual.mlnText (
    83:               ComponentFactory.createButton (
    84:                 "Reboot from it", KeyEvent.VK_R, this),
    85:               "ja", "ここから再起動"),
    86:             Box.createHorizontalStrut (12),
    87:             openRadioButton =
    88:             ComponentFactory.setFixedSize (
    89:               ComponentFactory.setText (
    90:                 ComponentFactory.createRadioButton (
    91:                   approveGroup, !reboot, "approve open", this),
    92:                 ""),
    93:               20, 20),
    94:             Multilingual.mlnText (
    95:               ComponentFactory.createButton (
    96:                 "Open", KeyEvent.VK_O, this),
    97:               "ja", "開く"),
    98:             Box.createHorizontalStrut (20),
    99:             Multilingual.mlnText (
   100:               ComponentFactory.createButton (
   101:                 "Cancel", KeyEvent.VK_C, this),
   102:               "ja", "キャンセル"),
   103:             Box.createHorizontalStrut (12)
   104:             ),
   105:           Box.createVerticalStrut (12)
   106:           )  //createVerticalBox
   107:         );  //createBorderPanel
   108: 
   109:     this.getContentPane ().add (component, BorderLayout.CENTER);
   110:     this.pack ();
   111:     this.setVisible (false);
   112:   }  //super
   113: 
   114:   //readOnly = getReadOnly ()
   115:   //  書き込み禁止フラグを取得する
   116:   public boolean getReadOnly () {
   117:     return readOnly;
   118:   }  //getReadOnly
   119: 
   120:   //setReadOnly (readOnly)
   121:   //  書き込み禁止フラグを設定する
   122:   public void setReadOnly (boolean readOnly) {
   123:     this.readOnly = readOnly;
   124:     if (readOnlyCheckBox != null) {
   125:       readOnlyCheckBox.setSelected (readOnly);
   126:     }
   127:   }  //setReadOnly
   128: 
   129:   //reboot = getReboot ()
   130:   //  ここから再起動フラグを取得する
   131:   public boolean getReboot () {
   132:     return reboot;
   133:   }  //getReboot
   134: 
   135:   //setReboot (reboot)
   136:   //  ここから再起動フラグを設定する
   137:   public void setReboot (boolean reboot) {
   138:     this.reboot = reboot;
   139:     if (reboot) {
   140:       if (rebootRadioButton != null) {
   141:         rebootRadioButton.setSelected (true);
   142:       }
   143:     } else {
   144:       if (openRadioButton != null) {
   145:         openRadioButton.setSelected (true);
   146:       }
   147:     }
   148:   }  //setReboot
   149: 
   150:   //addHistory (files)
   151:   //  ファイルをヒストリに追加する
   152:   public void addHistory (File[] files) {
   153:     fileChooser.addHistory (files);
   154:     fileChooser.selectLastFiles ();
   155:   }  //addHistory
   156: 
   157:   //pathsList = getHistory ()
   158:   //  ヒストリを取り出す
   159:   public ArrayList<String> getHistory () {
   160:     return fileChooser.getHistory ();
   161:   }  //getHistory
   162: 
   163:   //rescanCurrentDirectory ()
   164:   //  ファイルの一覧を作り直す
   165:   public void rescanCurrentDirectory () {
   166:     fileChooser.rescanCurrentDirectory ();
   167:   }  //rescanCurrentDirectory
   168: 
   169:   //actionPerformed (ae)
   170:   //  アクションリスナー
   171:   @Override public void actionPerformed (ActionEvent ae) {
   172:     switch (ae.getActionCommand ()) {
   173:     case JFileChooser.APPROVE_SELECTION:
   174:       approve (reboot);
   175:       break;
   176:     case "Read-only":  //書き込み禁止
   177:       readOnly = ((JCheckBox) ae.getSource ()).isSelected ();
   178:       break;
   179:     case "approve reboot":
   180:       reboot = true;
   181:       break;
   182:     case "Reboot from it":  //ここから再起動
   183:       approve (true);
   184:       break;
   185:     case "approve open":
   186:       reboot = false;
   187:       break;
   188:     case "Open":  //開く
   189:       approve (false);
   190:       break;
   191:     case JFileChooser.CANCEL_SELECTION:
   192:     case "Cancel":  //キャンセル
   193:       this.setVisible (false);  //ダイアログを消す
   194:       break;
   195:     }
   196:   }  //actionPerformed
   197: 
   198:   //approve (reboot)
   199:   //  ファイルがダブルクリックされた
   200:   //  ここから再起動ボタンが押された
   201:   //  開くボタンが押された
   202:   protected void approve (boolean reboot) {
   203:     File[] files = fileChooser.getSelectedFiles ();
   204:     if (files.length == 0 && directory) {
   205:       files = new File[] { fileChooser.getCurrentDirectory () };
   206:     }
   207:     if (0 < files.length) {  //ファイルが選択されている
   208:       openFiles (files, reboot);
   209:       this.setVisible (false);  //ダイアログを消す。ファイルが選択されていないときは消さない
   210:     }
   211:   }  //approve
   212: 
   213:   //openFiles (files, reboot)
   214:   //  ファイルを開く
   215:   public abstract void openFiles (File[] files, boolean reboot);
   216: 
   217: }  //class AbstractOpenDialog