OSDN Git Service

動画変換クラスのクラス名変更
[coroid/inqubus.git] / frontend / src / saccubus / OptionComboBoxModel.java
1 /**
2  * 
3  */
4 package saccubus;
5
6 import java.io.File;
7 import java.util.ArrayList;
8
9 import javax.swing.DefaultComboBoxModel;
10
11
12 /**
13  * @author Account01
14  * 
15  */
16 public class OptionComboBoxModel extends DefaultComboBoxModel {
17         /**
18          * 
19          */
20         private static final long serialVersionUID = -8948187216195366156L;
21
22         private final ArrayList<FFmpegSelectedItem> List = new ArrayList<FFmpegSelectedItem>(
23                         20);
24
25         private int Size = 0;
26
27         private int Index = 0;
28
29         public OptionComboBoxModel() {
30                 reload();
31         }
32
33         /**
34          * ファイル読み込みその他を行う
35          */
36     private static final File OPTION_FOLDER = new File("option");
37
38         private static final FFmpegSelectedItem DEFAULT_ITEM = new FFmpegSelectedItem(
39                         0, null, "外部ファイルを用いず、下に直接入力する。");
40
41         protected void reload() {
42                 File original_file = ((FFmpegSelectedItem) getSelectedItem()).getFile();
43                 reload(original_file);
44         }
45
46         protected void reload(File original_file) {
47                 int original_index = -1;
48                 int index = 1;
49                 // リストクリア
50                 List.clear();
51                 // ファイルリスト更新
52                 File[] file_array = OPTION_FOLDER.listFiles();
53                 if(file_array != null){
54                         for (int i = 0; i < file_array.length; i++) {
55                                 File file = file_array[i];
56                                 if (file.getName().endsWith(".xml")) {
57                                         List.add(new FFmpegSelectedItem(index, file, null));
58                                         // 前回示していたのと同じファイルを発見。
59                                         if (original_index < 0 && file.equals(original_file)) {
60                                                 original_index = index;
61                                         }
62                                         index++;
63                                 }
64                         }
65                 }
66                 // 初期化
67                 if (original_index < 0) {
68                         Index = 0;
69                 } else {
70                         Index = original_index;
71                 }
72                 Size = index;
73                 this.fireContentsChanged(this, 0, Size);
74         }
75
76         /**
77          * 選ばれているオブジェクトを返す
78          */
79         public Object getSelectedItem() {
80                 return getElementAt(Index);
81         }
82
83         public File getSelectedFile() {
84                 if (Index == 0) {
85                         return null;
86                 } else {
87                         return List.get(Index - 1).getFile();
88                 }
89         }
90
91         /**
92          * オブジェクトから、インデックスを探す。
93          */
94         public void setSelectedItem(Object anItem) {
95                 if (anItem == null) {
96                         Index = 0;
97                         return;
98                 }
99                 FFmpegSelectedItem item = (FFmpegSelectedItem) anItem;
100                 Index = item.getIndex();
101         }
102
103         /**
104          * インデックスからオブジェクトを返す。
105          */
106         public Object getElementAt(int index) {
107                 if (index == 0) {
108                         return DEFAULT_ITEM;
109                 } else if (index < Size) {
110                         return List.get(index - 1);
111                 } else {
112                         return null;
113                 }
114         }
115
116         /**
117          * サイズを返す。
118          */
119
120         public int getSize() {
121                 return Size;
122         }
123
124         public boolean isFile() {
125                 if (Index == 0) {
126                         return false;
127                 } else {
128                         return true;
129                 }
130         }
131
132 }
133 class FFmpegSelectedItem {
134         private final int Index;
135
136         private final File File;
137
138         private final String Name;
139
140         protected FFmpegSelectedItem(int index, File file, String name) {
141                 Index = index;
142                 File = file;
143                 if (name == null) {
144                         String tmp = file.getName();
145                         Name = tmp.substring(0, tmp.lastIndexOf("."));
146                 } else {
147                         Name = name;
148                 }
149         }
150
151         public String toString() {
152                 return Name;
153         }
154
155         protected File getFile() {
156                 return File;
157         }
158
159         /**
160          * 識別に使う
161          * 
162          * @return
163          */
164         protected int getIndex() {
165                 return Index;
166         }
167 }