]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/PackageEditor/src/org/tianocore/packaging/GuiPkgInstall.java
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@671 6f19259b...
[mirror_edk2.git] / Tools / Source / PackageEditor / src / org / tianocore / packaging / GuiPkgInstall.java
1 /** @file
2 Java class GuiPkgInstall is GUI for package installation.
3
4 Copyright (c) 2006, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 **/
13 package org.tianocore.packaging;
14
15
16 import javax.swing.JPanel;
17 import javax.swing.JFrame;
18 import javax.swing.JButton;
19 import javax.swing.JFileChooser;
20 import javax.swing.JLabel;
21 import javax.swing.JOptionPane;
22 import javax.swing.JTextField;
23
24 import java.awt.Dimension;
25 import java.awt.GridLayout;
26 import java.awt.Toolkit;
27
28 import java.awt.FlowLayout;
29 import java.awt.event.MouseEvent;
30 import java.awt.event.MouseListener;
31 import java.awt.event.WindowAdapter;
32 import java.awt.event.WindowEvent;
33
34 import java.awt.ComponentOrientation;
35 import java.io.File;
36 import java.util.Hashtable;
37
38 import javax.swing.SwingConstants;
39 import javax.swing.JProgressBar;
40 import javax.swing.filechooser.FileFilter;
41
42 import org.apache.xmlbeans.XmlException;
43
44
45 /**
46 GUI for package installation.
47
48 @since PackageEditor 1.0
49 **/
50 public class GuiPkgInstall extends JFrame implements MouseListener {
51
52 final static long serialVersionUID = 0;
53
54 static JFrame frame;
55
56 ///
57 /// backup of "this". As we cannot use "this" to refer outer class inside inner class
58 ///
59 private JFrame pThis = null;
60
61 private JFileChooser chooser = null;
62
63 private JPanel jPanel = null;
64
65 private JPanel jPanel1 = null;
66
67 private JTextField jTextField = null;
68
69 private JButton jButton = null;
70
71 private JPanel jPanel2 = null;
72
73 private JLabel jLabel1 = null;
74
75 private JPanel jPanel4 = null;
76
77 private JTextField jTextField1 = null;
78
79 private JButton jButton1 = null;
80
81 private JPanel jPanel5 = null;
82
83 private JPanel jPanel6 = null;
84
85 private JPanel jPanel7 = null;
86
87 private JLabel jLabel2 = null;
88
89 private JTextField jTextField2 = null;
90
91 private JButton jButton2 = null;
92
93 private JButton jButton3 = null;
94
95 private JPanel jPanel3 = null;
96
97 private JLabel jLabel = null;
98
99 private JProgressBar jProgressBar = null;
100
101 private JButton jButton4 = null;
102
103
104 public GuiPkgInstall() {
105 super();
106 initialize();
107
108 }
109
110 /**
111 GUI initialization
112 **/
113 private void initialize() {
114 this.setSize(new java.awt.Dimension(454, 313));
115 this.setContentPane(getJPanel());
116 this.setTitle("Package Installation");
117 this.addWindowListener(new GuiPkgInstallAdapter(this));
118 this.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
119 this.centerWindow();
120 pThis = this;
121 }
122
123 /**
124 make window appear center of screen
125
126 @param intWidth
127 @param intHeight
128 **/
129 protected void centerWindow(int intWidth, int intHeight) {
130 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
131 this.setLocation((d.width - intWidth) / 2, (d.height - intHeight) / 2);
132 }
133
134 /**
135 Start the window at the center of screen
136 **/
137 protected void centerWindow() {
138 centerWindow(this.getSize().width, this.getSize().height);
139 }
140
141 /**
142 This method initializes jPanel
143 @return javax.swing.JPanel
144 **/
145 private JPanel getJPanel() {
146 if (jPanel == null) {
147 GridLayout gridLayout = new GridLayout();
148 gridLayout.setRows(7);
149 gridLayout.setColumns(1);
150 jPanel = new JPanel();
151 jPanel.setLayout(gridLayout);
152 jPanel.add(getJPanel3(), null);
153 jPanel.add(getJPanel1(), null);
154 jPanel.add(getJPanel2(), null);
155 jPanel.add(getJPanel4(), null);
156 jPanel.add(getJPanel5(), null);
157 jPanel.add(getJPanel6(), null);
158 jPanel.add(getJPanel7(), null);
159 }
160 return jPanel;
161 }
162
163 /**
164 This method initializes jPanel1
165
166 @return javax.swing.JPanel
167 **/
168 private JPanel getJPanel1() {
169 if (jPanel1 == null) {
170 FlowLayout flowLayout = new FlowLayout();
171 flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
172 jPanel1 = new JPanel();
173 jPanel1.setLayout(flowLayout);
174 jPanel1.add(getJTextField(), null);
175 jPanel1.add(getJButton(), null);
176 }
177 return jPanel1;
178 }
179
180 /**
181 This method initializes jTextField
182
183 @return javax.swing.JTextField
184 **/
185 private JTextField getJTextField() {
186 if (jTextField == null) {
187 jTextField = new JTextField();
188 jTextField.setHorizontalAlignment(javax.swing.JTextField.LEFT);
189 jTextField.setPreferredSize(new java.awt.Dimension(350, 20));
190 }
191 return jTextField;
192 }
193
194 /**
195 This method initializes jButton
196
197 @return javax.swing.JButton
198 **/
199 private JButton getJButton() {
200 if (jButton == null) {
201 jButton = new JButton();
202 jButton.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
203 jButton.setText("Browse");
204 jButton.setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT);
205 jButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
206 jButton.setToolTipText("Where is the package?");
207 jButton.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 12));
208
209 jButton.setPreferredSize(new java.awt.Dimension(80, 20));
210 jButton.addMouseListener(new java.awt.event.MouseAdapter() {
211 public void mouseClicked(java.awt.event.MouseEvent e) {
212 if (chooser == null) {
213 chooser = new JFileChooser();
214 }
215 //
216 // disable multi-selection, you can only select one item each time.
217 //
218 chooser.setMultiSelectionEnabled(false);
219 chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
220 chooser.setFileFilter(new PkgFileFilter("fdp"));
221 int retval = chooser.showOpenDialog(frame);
222 if (retval == JFileChooser.APPROVE_OPTION) {
223
224 File theFile = chooser.getSelectedFile();
225 jTextField.setText(theFile.getPath());
226 //
227 // set a default directory for installation (WORKSPACE\PackageFileName)
228 //
229 if (jTextField1.getText().length() > 0) {
230 int indexbegin = jTextField.getText().lastIndexOf(System.getProperty("file.separator"));
231 int indexend = jTextField.getText().lastIndexOf('.');
232 if (indexbegin >= 0 && indexend >= 0) {
233 jTextField2.setText(jTextField1.getText()
234 + jTextField.getText().substring(indexbegin, indexend));
235 } else {
236 JOptionPane.showMessageDialog(frame, "Wrong Path:" + jTextField.getText());
237 }
238 }
239 }
240 }
241 });
242 }
243 return jButton;
244 }
245
246 /**
247 This method initializes jPanel2
248
249 @return javax.swing.JPanel
250 **/
251 private JPanel getJPanel2() {
252 if (jPanel2 == null) {
253 FlowLayout flowLayout1 = new FlowLayout();
254 flowLayout1.setAlignment(java.awt.FlowLayout.LEFT);
255 flowLayout1.setVgap(20);
256 jLabel1 = new JLabel();
257 jLabel1.setText("Enter Workspace Location");
258 jLabel1.setComponentOrientation(java.awt.ComponentOrientation.UNKNOWN);
259 jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.TRAILING);
260 jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
261 jPanel2 = new JPanel();
262 jPanel2.setLayout(flowLayout1);
263 jPanel2.add(jLabel1, null);
264 }
265 return jPanel2;
266 }
267
268 /**
269 This method initializes jPanel4
270
271 @return javax.swing.JPanel
272 **/
273 private JPanel getJPanel4() {
274 if (jPanel4 == null) {
275 FlowLayout flowLayout2 = new FlowLayout();
276 flowLayout2.setAlignment(java.awt.FlowLayout.LEFT);
277 jPanel4 = new JPanel();
278 jPanel4.setLayout(flowLayout2);
279 jPanel4.add(getJTextField1(), null);
280 jPanel4.add(getJButton1(), null);
281 }
282 return jPanel4;
283 }
284
285 /**
286 This method initializes jTextField1
287
288 @return javax.swing.JTextField
289 **/
290 private JTextField getJTextField1() {
291 if (jTextField1 == null) {
292 jTextField1 = new JTextField();
293 jTextField1.setPreferredSize(new java.awt.Dimension(350, 20));
294
295 }
296 //
297 // default value is WORKSPACE environmental variable value
298 //
299 jTextField1.setText(System.getenv("WORKSPACE"));
300 return jTextField1;
301 }
302
303 /**
304 This method initializes jButton1
305
306 @return javax.swing.JButton
307 **/
308 private JButton getJButton1() {
309 if (jButton1 == null) {
310 jButton1 = new JButton();
311 jButton1.setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT);
312
313 jButton1.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
314 jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.TRAILING);
315 jButton1.setText("Browse");
316 jButton1.setPreferredSize(new java.awt.Dimension(80, 20));
317 jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
318 public void mouseClicked(java.awt.event.MouseEvent e) {
319 if (chooser == null) {
320 chooser = new JFileChooser();
321 }
322 //
323 // only directories can be selected for workspace location.
324 //
325 chooser.setMultiSelectionEnabled(false);
326 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
327
328 int retval = chooser.showOpenDialog(frame);
329 if (retval == JFileChooser.APPROVE_OPTION) {
330
331 File theFile = chooser.getSelectedFile();
332 jTextField1.setText(theFile.getPath());
333 //
334 // set a default directory for installation (WORKSPACE\PackageFileName)
335 //
336 if (jTextField.getText().length() > 0) {
337 int indexbegin = jTextField.getText().lastIndexOf(System.getProperty("file.separator"));
338 int indexend = jTextField.getText().lastIndexOf('.');
339 if (indexbegin >= 0 && indexend >= 0) {
340 jTextField2.setText(jTextField1.getText()
341 + jTextField.getText().substring(indexbegin, indexend));
342 } else {
343 JOptionPane.showMessageDialog(frame, "Wrong Path:" + jTextField.getText());
344 }
345 }
346 }
347
348 }
349 });
350 }
351 return jButton1;
352 }
353
354 /**
355 This method initializes jButton4
356
357 @return javax.swing.JButton
358 **/
359 private JButton getJButton4() {
360 if (jButton4 == null) {
361 jButton4 = new JButton();
362 jButton4.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
363
364 jButton4.setHorizontalAlignment(SwingConstants.LEADING);
365 jButton4.setHorizontalTextPosition(SwingConstants.TRAILING);
366 jButton4.setText("Browse");
367 jButton4.setPreferredSize(new Dimension(80, 20));
368 jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
369 public void mouseClicked(java.awt.event.MouseEvent e) {
370 JFileChooser chooser = new JFileChooser(jTextField1.getText());
371
372 chooser.setMultiSelectionEnabled(false);
373 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
374
375 int retval = chooser.showOpenDialog(frame);
376 if (retval == JFileChooser.APPROVE_OPTION) {
377 //
378 // specify installation directory from file chooser
379 //
380 File theFile = chooser.getSelectedFile();
381 jTextField2.setText(theFile.getPath());
382
383 }
384 }
385 });
386
387 }
388 return jButton4;
389 }
390 /**
391 This method initializes jPanel5
392
393 @return javax.swing.JPanel
394 **/
395 private JPanel getJPanel5() {
396 if (jPanel5 == null) {
397 FlowLayout flowLayout3 = new FlowLayout();
398 flowLayout3.setAlignment(java.awt.FlowLayout.LEFT);
399 flowLayout3.setVgap(20);
400 jLabel2 = new JLabel();
401 jLabel2.setComponentOrientation(java.awt.ComponentOrientation.UNKNOWN);
402 jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.TRAILING);
403 jLabel2.setText("Enter Installation Location Within Workspace");
404 jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
405 jPanel5 = new JPanel();
406 jPanel5.setLayout(flowLayout3);
407 jPanel5.add(jLabel2, null);
408 }
409 return jPanel5;
410 }
411
412 /**
413 This method initializes jPanel6
414
415 @return javax.swing.JPanel
416 **/
417 private JPanel getJPanel6() {
418 if (jPanel6 == null) {
419 FlowLayout flowLayout4 = new FlowLayout();
420 flowLayout4.setAlignment(java.awt.FlowLayout.LEFT);
421 jPanel6 = new JPanel();
422 jPanel6.setLayout(flowLayout4);
423 jPanel6.add(getJTextField2(), null);
424 jPanel6.add(getJButton4(), null);
425 }
426 return jPanel6;
427 }
428
429 /**
430 This method initializes jPanel7
431
432 @return javax.swing.JPanel
433 **/
434 private JPanel getJPanel7() {
435 if (jPanel7 == null) {
436 FlowLayout flowLayout5 = new FlowLayout();
437 flowLayout5.setAlignment(java.awt.FlowLayout.RIGHT);
438 jPanel7 = new JPanel();
439 jPanel7.setLayout(flowLayout5);
440 jPanel7.add(getJProgressBar(), null);
441 jPanel7.add(getJButton2(), null);
442 jPanel7.add(getJButton3(), null);
443 }
444 return jPanel7;
445 }
446
447 /**
448 This method initializes jTextField2
449
450 @return javax.swing.JTextField
451 **/
452 private JTextField getJTextField2() {
453 if (jTextField2 == null) {
454 jTextField2 = new JTextField();
455 jTextField2.setPreferredSize(new java.awt.Dimension(350, 20));
456 }
457 return jTextField2;
458 }
459
460 /**
461 This method initializes jButton2
462
463 @return javax.swing.JButton
464 **/
465 private JButton getJButton2() {
466 if (jButton2 == null) {
467 jButton2 = new JButton();
468 jButton2.setPreferredSize(new java.awt.Dimension(80, 20));
469 jButton2.setText("Ok");
470 jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
471 public void mouseClicked(java.awt.event.MouseEvent e) {
472 try {
473 //
474 // The installation directory must under workspace directory
475 //
476 locationcheck(jTextField.getText(), jTextField1.getText(), jTextField2.getText());
477 } catch (Exception ee) {
478 JOptionPane.showMessageDialog(frame, "Package Location Error!");
479 return;
480 }
481
482 try {
483 //
484 // create a new FrameworkPkg object with user-selected package, current workspace location.
485 // install the package to dest dir from jTextField2
486 //
487 int i = new FrameworkPkg(jTextField.getText(), jTextField1.getText())
488 .install(jTextField2
489 .getText());
490 //
491 // the package is installed smoothly
492 //
493 if (i == 0) {
494 JOptionPane.showMessageDialog(frame, "Package" + jTextField.getText()
495 + " Installed Successfully!");
496 }
497 } catch (BasePkgNotInstalled bpni) {
498 //
499 // exception no base package installed
500 //
501 JOptionPane
502 .showMessageDialog(frame,
503 "The Edk package needs to be installed before installing any other packages.");
504 } catch (VerNotEqual vne) {
505 //
506 // show modal GUI PkgInstallTypeChooser with user selected package name,
507 // current workspace location and the list of package info with same base name
508 //
509 ModalFrameUtil.showAsModal(new PkgInstallTypeChooser(jTextField.getText(),
510 jTextField1.getText(), vne.getVersion()),
511 pThis);
512
513 } catch (GuidNotEqual gne) {
514 //
515 // show modal GUI PkgInstallTypeChooser with user selected package name,
516 // current workspace location and the list of package info with same base name and version
517 //
518 ModalFrameUtil.showAsModal(new PkgInstallTypeChooser(jTextField.getText(),
519 jTextField1.getText(), gne.getGuid()),
520 pThis);
521
522 } catch (SameAll sa) {
523 //
524 // the package with same (base, version, guid) already exists. confirm user action.
525 // quit or replace the original info. (So only one package info entry in db file that may be triple same)
526 //
527 int retVal = JOptionPane
528 .showConfirmDialog(
529 frame,
530 "Package already exists. Would you like to replace it?",
531 "Package Installation", JOptionPane.YES_NO_OPTION);
532 if (retVal == JOptionPane.YES_OPTION) {
533 String installDir = sa.getVersion().listIterator().next().getPathArray(0).getStringValue();
534 try {
535 ForceInstallPkg f = new ForceInstallPkg(jTextField.getText(), jTextField1.getText());
536 //
537 // Get old packag info to meet the calling parameter layout of DbFileContents.updatePkgInfo
538 // ForceInstallPkg will call it after installation to update package info.
539 //
540 f.setOldVersion(sa.getVersion().listIterator().next().getVersionArray(0));
541 f.setOldGuid(sa.getVersion().listIterator().next().getGuidArray(0).getStringValue());
542 int i = f.install(jTextField1.getText() + System.getProperty("file.separator") + installDir);
543 if (i == 0) {
544 JOptionPane.showMessageDialog(frame, "Package" + jTextField.getText()
545 + " Installed Successfully!");
546 }
547 } catch (Exception sae) {
548 System.out.println(sae.toString());
549 JOptionPane.showMessageDialog(frame, "Extraction Error!");
550 }
551 }
552 return;
553 } catch (XmlException xmle) {
554 System.out.println(xmle.toString());
555 JOptionPane.showMessageDialog(frame, "Package Format Error!");
556 } catch (DirSame ds) {
557 //
558 // You cannot install different packages into the same directory.
559 //
560 System.out.println(ds.toString());
561 JOptionPane.showMessageDialog(frame,
562 "Another Package Exists There, Please Select Another Directory!");
563 } catch (Exception ext) {
564 System.out.println(ext.toString());
565 JOptionPane.showMessageDialog(frame, "Extraction Error!");
566 }
567 }
568 });
569 }
570 return jButton2;
571 }
572
573 /**
574 * This method initializes jButton3
575 *
576 * @return javax.swing.JButton
577 */
578 private JButton getJButton3() {
579 if (jButton3 == null) {
580 jButton3 = new JButton();
581 jButton3.setPreferredSize(new java.awt.Dimension(80, 20));
582 jButton3.setText("Cancel");
583 jButton3.addMouseListener(this);
584 }
585 return jButton3;
586
587 }
588
589 /**
590 This method initializes jPanel3
591
592 @return javax.swing.JPanel
593 */
594 private JPanel getJPanel3() {
595 if (jPanel3 == null) {
596 jLabel = new JLabel();
597 jLabel.setComponentOrientation(ComponentOrientation.UNKNOWN);
598 jLabel.setHorizontalTextPosition(SwingConstants.TRAILING);
599 jLabel.setText("Enter Package Location");
600 jLabel.setHorizontalAlignment(SwingConstants.TRAILING);
601 FlowLayout flowLayout6 = new FlowLayout();
602 flowLayout6.setVgap(20);
603 flowLayout6.setAlignment(FlowLayout.LEFT);
604 jPanel3 = new JPanel();
605 jPanel3.setLayout(flowLayout6);
606 jPanel3.add(jLabel, null);
607 }
608 return jPanel3;
609 }
610
611 /**
612 check user input validity
613
614 @param s package path
615 @param s1 workspace path
616 @param s2 installation path
617 @throws Exception
618 **/
619 private void locationcheck(String s, String s1, String s2) throws Exception {
620 if (new File(s).isFile() == false)
621 throw new Exception();
622 if (new File(s1).isDirectory() == false)
623 throw new Exception();
624 if (s2.startsWith(s1) == false)
625 throw new Exception();
626 }
627
628
629 public void mouseClicked(MouseEvent arg0) {
630 // TODO Auto-generated method stub
631 int retVal = JOptionPane.showConfirmDialog(frame, "Are you sure to exit?", "Quit", JOptionPane.YES_NO_OPTION);
632 if (retVal == JOptionPane.YES_OPTION) {
633 this.dispose();
634 }
635 return;
636 }
637
638 public void mouseEntered(MouseEvent arg0) {
639 // TODO Auto-generated method stub
640
641 }
642
643 public void mouseExited(MouseEvent arg0) {
644 // TODO Auto-generated method stub
645
646 }
647
648 public void mousePressed(MouseEvent arg0) {
649 // TODO Auto-generated method stub
650
651 }
652
653 public void mouseReleased(MouseEvent arg0) {
654 // TODO Auto-generated method stub
655
656 }
657
658 /**
659 This method initializes jProgressBar
660
661 @return javax.swing.JProgressBar
662 **/
663 private JProgressBar getJProgressBar() {
664 if (jProgressBar == null) {
665 jProgressBar = new JProgressBar();
666 jProgressBar.setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT);
667 jProgressBar.setVisible(false);
668 }
669 return jProgressBar;
670 }
671
672
673
674 } // @jve:decl-index=0:visual-constraint="24,82"
675
676 /**
677 Derived from WindowAdapter, Event adapter for windowClosing event
678
679 @since PackageEditor 1.0
680 **/
681 class GuiPkgInstallAdapter extends WindowAdapter {
682
683 private JFrame frame = null;
684
685 GuiPkgInstallAdapter(JFrame f) {
686 super();
687 frame = f;
688 }
689
690 /* (non-Javadoc)
691 * @see java.awt.event.WindowAdapter#windowClosing(java.awt.event.WindowEvent)
692 */
693 @Override
694 public void windowClosing(WindowEvent arg0) {
695 // TODO Auto-generated method stub
696 super.windowClosing(arg0);
697 int retVal = JOptionPane.showConfirmDialog(frame, "Are you sure to exit?", "Quit",
698 JOptionPane.YES_NO_OPTION);
699 if (retVal == JOptionPane.YES_OPTION) {
700 frame.dispose();
701 }
702
703 }
704
705 }
706
707 /**
708 Filter out some specific type of file
709
710 @since PackageEditor 1.0
711 **/
712 class PkgFileFilter extends FileFilter {
713
714 ///
715 /// hash table used to store filter info.
716 ///
717 private Hashtable<String, String> filters = null;
718
719
720 public PkgFileFilter() {
721 this.filters = new Hashtable<String, String>();
722 }
723
724 /**
725 Create filter and add extension to hash table
726
727 @param extension file extension string (e.g. "exe")
728 **/
729 public PkgFileFilter(String extension) {
730 this();
731 if(extension!=null) {
732 addExtension(extension);
733 }
734
735 }
736
737 public PkgFileFilter(String[] fileFilters) {
738 this();
739 int i = 0;
740 while (i < fileFilters.length) {
741 // add filters one by one
742 addExtension(fileFilters[i]);
743 i++;
744 }
745
746 }
747
748
749 /* (non-Javadoc)
750 * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
751 */
752 public boolean accept(File f) {
753 if (f != null) {
754 if (f.isDirectory()) {
755 return true;
756 }
757
758 if (getExtension(f) != null && filters.get(getExtension(f)) != null) {
759 return true;
760 }
761 }
762 return false;
763 }
764
765
766 /**
767 Get the extension string of file
768
769 @param f target file
770 @return String
771 **/
772 public String getExtension(File f) {
773 if (f != null) {
774 int i = f.getName().lastIndexOf('.');
775 if (i>0 && i<f.getName().length()-1) {
776 return f.getName().substring(i+1).toLowerCase();
777 }
778 }
779 return null;
780 }
781
782
783 /**
784 Add extension info into hash table
785
786 @param ext extension string for file name
787 **/
788 public void addExtension(String ext) {
789 if (filters == null) {
790 filters = new Hashtable<String, String>(5);
791 }
792 filters.put(ext.toLowerCase(), "ext");
793
794 }
795
796 /* (non-Javadoc)
797 * @see javax.swing.filechooser.FileFilter#getDescription()
798 */
799 public String getDescription() {
800 return null;
801 }
802
803 }
804