Swing/RenameIfCanWriteFileChooser のバックアップソース(No.8)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/RenameIfCanWriteFileChooser へ行く。
- 1 (2011-10-21 (金) 02:12:49)
- 2 (2011-12-15 (木) 20:34:29)
- 3 (2012-12-16 (日) 01:40:28)
- 4 (2013-09-05 (木) 00:39:56)
- 5 (2014-11-25 (火) 03:03:31)
- 6 (2015-02-05 (木) 14:35:02)
- 7 (2016-08-19 (金) 16:19:26)
- 8 (2017-10-05 (木) 14:30:25)
- 9 (2019-04-04 (木) 14:26:18)
- 10 (2021-01-14 (木) 17:59:30)
- 11 (2023-07-27 (木) 18:37:23)
- 12 (2024-03-18 (月) 14:17:20)
--- category: swing folder: RenameIfCanWriteFileChooser title: JFileChooserで読み取り専用ファイルのリネームを禁止 tags: [JFileChooser, File, BasicDirectoryModel] author: aterai pubdate: 2011-10-17T12:57:18+09:00 description: JFileChooserで読み取り専用属性ファイルのリネームを禁止します。 image: https://lh5.googleusercontent.com/-7ODSj6DaIig/TpsQEay0NqI/AAAAAAAABDs/8fz14wjADj0/s800/RenameIfCanWriteFileChooser.png --- * 概要 [#summary] `JFileChooser`で読み取り専用属性ファイルのリネームを禁止します。 #download(https://lh5.googleusercontent.com/-7ODSj6DaIig/TpsQEay0NqI/AAAAAAAABDs/8fz14wjADj0/s800/RenameIfCanWriteFileChooser.png) * サンプルコード [#sourcecode] #code(link){{ class MetalCanWriteFileChooserUI extends MetalFileChooserUI { private BasicDirectoryModel model2; protected MetalCanWriteFileChooserUI(JFileChooser chooser) { super(chooser); } public static ComponentUI createUI(JComponent c) { if (c instanceof JFileChooser) { return new MetalCanWriteFileChooserUI((JFileChooser) c); } throw new InternalError("Should never happen"); } @Override public void createModel() { if (model2 != null) { model2.invalidateFileCache(); } model2 = new BasicDirectoryModel(getFileChooser()) { @Override public boolean renameFile(File oldFile, File newFile) { return oldFile.canWrite() ? super.renameFile(oldFile, newFile) : false; } }; } @Override public BasicDirectoryModel getModel() { return model2; } } }} * 解説 [#explanation] 上記のサンプルでは、`BasicDirectoryModel#renameFile(...)`メソッドをオーバーライドし、読み取り専用属性の付いたファイルの名前変更を確定しようとすると警告ダイアログが表示されて、変更が取り消されるように設定しています。 ---- `sun.swing.FilePane#canWrite(File)`をオーバーライドすれば、ファイル名の編集開始自体を禁止にすることもできそうですが、`MetalFileChooserUI`などの`UI`で`FilePane`が`private`になっているためかなり面倒です。 * 参考リンク [#reference] - [https://community.oracle.com/thread/2300004 Swing - How does the JFileChooser "readOnly" property work?] - [[JFileChooserを編集不可にする>Swing/ROFileChooser]] * コメント [#comment] #comment #comment