AbstractUnit.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13: package xeij;
14:
15: import java.awt.event.*;
16: import java.lang.*;
17: import javax.swing.*;
18:
19: public abstract class AbstractUnit {
20:
21:
22: protected static final int ABU_WRONG_INSERTION = 0b00000001;
23: protected static final int ABU_INSERTED = 0b00000010;
24: protected static final int ABU_NOT_READY = 0b00000100;
25: protected static final int ABU_WRITE_PROTECTED = 0b00001000;
26: protected static final int ABU_USER_PREVENTED = 0b00010000;
27: protected static final int ABU_BUFFERED = 0b00100000;
28: protected static final int ABU_EJECT_PREVENTED = 0b01000000;
29: protected static final int ABU_BLINKING = 0b10000000;
30:
31: protected int abuNumber;
32:
33: protected int abuNumberOfModes;
34: protected int abuCurrentMode;
35: protected JButton abuModeButton;
36:
37: protected boolean abuConnected;
38: protected boolean abuInserted;
39: protected boolean abuWriteProtected;
40: protected boolean abuBuffered;
41: protected boolean abuEjectPrevented;
42:
43: protected boolean abuDisconnectable;
44: protected boolean abuUnprotectable;
45:
46: protected boolean abuEjected;
47:
48: protected String abuPath;
49:
50: protected Box abuMenuBox;
51: protected JCheckBox abuConnectCheckBox;
52: protected JCheckBox abuProtectCheckBox;
53: protected JButton abuOpenButton;
54: protected JTextField abuOpenTextField;
55: protected JButton abuEjectButton;
56:
57:
58:
59:
60: public int abuGetNumberOfModes () {
61: return 0;
62: }
63:
64:
65:
66:
67: public ImageIcon abuGetModeIcon (int mode, boolean enabled) {
68: return null;
69: }
70:
71:
72:
73:
74: public String abuGetModeTextEn (int mode, boolean enabled) {
75: return (enabled ? "Mode " + mode + " / Change mode to " + (mode + 1 < abuNumberOfModes ? mode + 1 : 0) :
76: "Mode " + mode);
77: }
78:
79:
80:
81:
82: public String abuGetModeTextJa (int mode, boolean enabled) {
83: return (enabled ? "モード " + mode + " / モード " + (mode + 1 < abuNumberOfModes ? mode + 1 : 0) + " に切り替える" :
84: "モード " + mode);
85: }
86:
87:
88:
89:
90: public void abuSetMode (int mode) {
91: abuCurrentMode = mode;
92: abuUpdateModeButton ();
93: }
94:
95:
96:
97: public void abuUpdateModeButton () {
98: if (abuNumberOfModes > 0) {
99: boolean enabled = abuConnected && !abuInserted;
100: abuModeButton.setEnabled (enabled);
101: abuModeButton.setIcon (abuGetModeIcon (abuCurrentMode, true));
102: abuModeButton.setDisabledIcon (abuGetModeIcon (abuCurrentMode, false));
103: Multilingual.mlnToolTipText (abuModeButton,
104: "en", abuGetModeTextEn (abuCurrentMode, enabled),
105: "ja", abuGetModeTextJa (abuCurrentMode, enabled));
106: }
107: }
108:
109:
110:
111:
112: public Box getMenuBox () {
113: return abuMenuBox;
114: }
115:
116:
117:
118: public boolean isConnected () {
119: return abuConnected;
120: }
121:
122:
123:
124: protected boolean hasBeenEjected () {
125: boolean t = abuEjected;
126: abuEjected = false;
127: return t;
128: }
129:
130:
131:
132: @SuppressWarnings ("this-escape") protected AbstractUnit (int number) {
133: abuNumber = number;
134:
135: abuNumberOfModes = abuGetNumberOfModes ();
136: abuCurrentMode = 0;
137:
138: abuConnected = false;
139: abuInserted = false;
140: abuWriteProtected = false;
141: abuBuffered = false;
142: abuEjectPrevented = false;
143:
144: abuDisconnectable = true;
145: abuUnprotectable = true;
146:
147: abuEjected = false;
148:
149: abuPath = "";
150:
151: ActionListener listener = new ActionListener () {
152: @Override public void actionPerformed (ActionEvent ae) {
153: String command = ae.getActionCommand ();
154: switch (command) {
155: case "connect":
156: if (abuConnectCheckBox.isSelected ()) {
157: connect (true);
158: } else {
159: disconnect ();
160: }
161: break;
162: case "mode":
163: if (abuConnected && !abuInserted) {
164: abuSetMode (abuCurrentMode + 1 < abuNumberOfModes ? abuCurrentMode + 1 : 0);
165: }
166: break;
167: case "eject":
168: eject ();
169: break;
170: case "open":
171: open ();
172: break;
173: case "protect":
174: if (abuProtectCheckBox.isSelected ()) {
175: protect (true);
176: } else {
177: unprotect ();
178: }
179: break;
180: }
181: }
182: };
183:
184: abuMenuBox = ComponentFactory.createHorizontalBox (
185: abuConnectCheckBox = Multilingual.mlnToolTipText (
186: ComponentFactory.setEnabled (
187: ComponentFactory.createIconCheckBox (false,
188: LnF.LNF_NUMBER_IMAGE_ARRAY[abuNumber], LnF.LNF_NUMBER_SELECTED_IMAGE_ARRAY[abuNumber],
189: LnF.LNF_NUMBER_IMAGE_ARRAY[abuNumber], LnF.LNF_NUMBER_SELECTED_IMAGE_ARRAY[abuNumber],
190: "connect", listener),
191: true),
192: "en", "Connect", "ja", "接続する")
193: );
194:
195:
196: if (abuNumberOfModes > 0) {
197: boolean enabled = abuConnected && !abuInserted;
198: abuModeButton = Multilingual.mlnToolTipText (
199: ComponentFactory.setEnabled (
200: ComponentFactory.createIconButton (abuGetModeIcon (abuCurrentMode, true),
201: abuGetModeIcon (abuCurrentMode, false),
202: "mode",
203: listener),
204: enabled),
205: "en", abuGetModeTextEn (abuCurrentMode, enabled),
206: "ja", abuGetModeTextJa (abuCurrentMode, enabled)
207: );
208: ComponentFactory.addComponents (
209: abuMenuBox,
210: abuModeButton
211: );
212: }
213:
214:
215: abuEjectButton = Multilingual.mlnToolTipText (
216: ComponentFactory.setEnabled (
217: ComponentFactory.createImageButton (LnF.LNF_EJECT_IMAGE, LnF.LNF_EJECT_DISABLED_IMAGE, "eject", listener),
218: false),
219: "en", null, "ja", null);
220:
221:
222: abuOpenButton = Multilingual.mlnToolTipText (
223: ComponentFactory.setEnabled (
224: ComponentFactory.setPreferredSize (
225: ComponentFactory.setText (
226: ComponentFactory.createButton (LnF.LNF_OPEN_IMAGE, LnF.LNF_OPEN_DISABLED_IMAGE, "open", listener),
227: ""),
228: LnF.lnfFontSize + 4, LnF.lnfFontSize + 4),
229: false),
230: "en", null, "ja", null);
231: abuOpenTextField = ComponentFactory.addListener (
232: ComponentFactory.setEditable (
233: ComponentFactory.setPreferredSize (
234: ComponentFactory.setHorizontalAlignment (
235: ComponentFactory.createTextField ("", 20),
236: SwingConstants.CENTER),
237: 144, LnF.lnfFontSize + 4),
238: false),
239: new MouseAdapter () {
240: @Override public void mouseClicked (MouseEvent me) {
241: if (!abuInserted) {
242: abuOpenButton.doClick ();
243: }
244: }
245: });
246:
247:
248: abuProtectCheckBox = Multilingual.mlnToolTipText (
249: ComponentFactory.setEnabled (
250: ComponentFactory.createIconCheckBox (true,
251: LnF.LNF_PROTECT_IMAGE, LnF.LNF_PROTECT_SELECTED_IMAGE,
252: LnF.LNF_PROTECT_DISABLED_IMAGE, LnF.LNF_PROTECT_DISABLED_SELECTED_IMAGE,
253: "protect", listener),
254: false),
255: "en", null, "ja", null);
256:
257: ComponentFactory.addComponents (
258: abuMenuBox,
259: abuEjectButton,
260: abuOpenButton,
261: abuOpenTextField,
262: Box.createHorizontalGlue (),
263: abuProtectCheckBox
264: );
265:
266: }
267:
268:
269:
270: protected void connect (boolean disconnectable) {
271: if (abuConnected) {
272: return;
273: }
274: abuConnected = true;
275: abuDisconnectable = disconnectable;
276: abuConnectCheckBox.setSelected (true);
277: abuConnectCheckBox.setEnabled (disconnectable);
278: Multilingual.mlnToolTipText (abuConnectCheckBox, "en", disconnectable ? "Disconnect" : null, "ja", disconnectable ? "切り離す" : null);
279: abuSetMode (abuCurrentMode);
280: abuOpenButton.setEnabled (true);
281: Multilingual.mlnToolTipText (abuOpenButton,
282: "en", "Open",
283: "ja", "開く");
284: }
285:
286:
287:
288: protected void disconnect () {
289: if (!abuConnected ||
290: abuInserted ||
291: !abuDisconnectable) {
292: return;
293: }
294: abuConnected = false;
295: abuConnectCheckBox.setSelected (false);
296: Multilingual.mlnToolTipText (abuConnectCheckBox, "en", "Connect", "ja", "接続する");
297: abuSetMode (abuCurrentMode);
298: abuOpenButton.setEnabled (false);
299: Multilingual.mlnToolTipText (abuOpenButton, "en", null, "ja", null);
300: }
301:
302:
303:
304: protected void protect (boolean unprotectable) {
305: if (!abuInserted ||
306: abuWriteProtected) {
307: return;
308: }
309: abuWriteProtected = true;
310: abuUnprotectable = unprotectable;
311: abuProtectCheckBox.setSelected (true);
312: abuProtectCheckBox.setEnabled (unprotectable);
313: Multilingual.mlnToolTipText (abuProtectCheckBox, "en", "Write-Protected", "ja", "書き込みが禁止されています");
314: }
315:
316:
317:
318: protected void unprotect () {
319: if (!abuInserted ||
320: !abuWriteProtected ||
321: !abuUnprotectable) {
322: return;
323: }
324: abuWriteProtected = false;
325: abuProtectCheckBox.setSelected (false);
326: Multilingual.mlnToolTipText (abuProtectCheckBox, "en", "Write-Enabled", "ja", "書き込みが許可されています");
327: }
328:
329:
330:
331: protected void prevent () {
332: if (!abuConnected ||
333: !abuInserted ||
334: abuEjectPrevented) {
335: return;
336: }
337: abuEjectPrevented = true;
338: abuOpenButton.setEnabled (false);
339: Multilingual.mlnToolTipText (abuOpenButton, "en", null, "ja", null);
340: abuEjectButton.setEnabled (false);
341: Multilingual.mlnToolTipText (abuEjectButton, "en", null, "ja", null);
342: }
343:
344:
345:
346: protected void allow () {
347: if (!abuConnected ||
348: !abuInserted ||
349: !abuEjectPrevented) {
350: return;
351: }
352: abuEjectPrevented = false;
353: abuOpenButton.setEnabled (true);
354: Multilingual.mlnToolTipText (abuOpenButton,
355: "en", "Open",
356: "ja", "開く");
357: abuEjectButton.setEnabled (true);
358: Multilingual.mlnToolTipText (abuEjectButton, "en", "Eject", "ja", "イジェクトする");
359: }
360:
361:
362:
363: protected boolean eject () {
364: if (!abuConnected ||
365: abuEjectPrevented) {
366: return false;
367: }
368: if (!abuInserted) {
369: return true;
370: }
371: abuInserted = false;
372: abuWriteProtected = false;
373: abuBuffered = false;
374: abuEjected = true;
375: abuPath = "";
376: abuOpenTextField.setText ("");
377: abuConnectCheckBox.setEnabled (abuDisconnectable);
378: Multilingual.mlnToolTipText (abuConnectCheckBox, "en", abuDisconnectable ? "Disconnect" : null, "ja", abuDisconnectable ? "切り離す" : null);
379: abuSetMode (abuCurrentMode);
380: abuProtectCheckBox.setSelected (true);
381: abuProtectCheckBox.setEnabled (false);
382: Multilingual.mlnToolTipText (abuProtectCheckBox, "en", null, "ja", null);
383: abuEjectButton.setEnabled (false);
384: Multilingual.mlnToolTipText (abuEjectButton, "en", null, "ja", null);
385: return true;
386: }
387:
388:
389:
390:
391: protected boolean open () {
392: if (!abuConnected ||
393: abuInserted && abuEjectPrevented) {
394: return false;
395: }
396: return eject ();
397: }
398:
399:
400:
401: protected boolean insert (String path, boolean writeProtected) {
402: if (path.length () == 0 ||
403: !abuConnected ||
404: abuInserted && abuEjectPrevented) {
405: return false;
406: }
407: abuInserted = true;
408: abuPath = path;
409: String tempPath = abuPath;
410: while (2 < tempPath.length () &&
411: (tempPath.endsWith ("/.") ||
412: tempPath.endsWith ("\\."))) {
413: tempPath = tempPath.substring (0, tempPath.length () - 2);
414: }
415: abuOpenTextField.setText (tempPath);
416:
417: abuOpenTextField.setCaretPosition (tempPath.length ());
418: abuConnectCheckBox.setEnabled (false);
419: Multilingual.mlnToolTipText (abuConnectCheckBox, "en", null, "ja", null);
420: abuSetMode (abuCurrentMode);
421: abuProtectCheckBox.setEnabled (true);
422: abuProtectCheckBox.setSelected (false);
423: Multilingual.mlnToolTipText (abuProtectCheckBox, "en", "Write-Enabled", "ja", "書き込みが許可されています");
424: abuEjectButton.setEnabled (true);
425: Multilingual.mlnToolTipText (abuEjectButton, "en", "Eject", "ja", "イジェクトする");
426:
427: if (writeProtected) {
428: protect (false);
429: }
430:
431: if (!load (path)) {
432: eject ();
433: return false;
434: }
435: return true;
436: }
437:
438:
439:
440: protected boolean load (String path) {
441: return true;
442: }
443:
444:
445:
446: protected String getName () {
447: return abuPath;
448: }
449:
450: }
451:
452:
453: