KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > motif > MotifFileChooserUI


1 /*
2  * @(#)MotifFileChooserUI.java 1.47 07/06/06
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.motif;
9
10 import javax.swing.*;
11 import javax.swing.filechooser.*;
12 import javax.swing.event.*;
13 import javax.swing.plaf.*;
14 import javax.swing.plaf.basic.*;
15 import java.awt.*;
16 import java.awt.event.*;
17 import java.beans.*;
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.util.*;
21
22 /**
23  * Motif FileChooserUI.
24  *
25  * @version 1.47 06/06/07
26  * @author Jeff Dinkins
27  */

28 public class MotifFileChooserUI extends BasicFileChooserUI {
29
30     private FilterComboBoxModel filterComboBoxModel;
31
32     protected JList directoryList = null;
33     protected JList fileList = null;
34
35     protected JTextField pathField = null;
36     protected JComboBox filterComboBox = null;
37     protected JTextField filenameTextField = null;
38
39     private static final Dimension hstrut10 = new Dimension(10, 1);
40     private static final Dimension vstrut10 = new Dimension(1, 10);
41
42     private static final Insets insets = new Insets(10, 10, 10, 10);
43
44     private static Dimension prefListSize = new Dimension(75, 150);
45
46     private static Dimension WITH_ACCELERATOR_PREF_SIZE = new Dimension(650, 450);
47     private static Dimension PREF_SIZE = new Dimension(350, 450);
48     private static Dimension MIN_SIZE = new Dimension(200, 300);
49
50     private static Dimension PREF_ACC_SIZE = new Dimension(10, 10);
51     private static Dimension ZERO_ACC_SIZE = new Dimension(1, 1);
52
53     private static Dimension MAX_SIZE = new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
54
55     private static final Insets buttonMargin = new Insets(3, 3, 3, 3);
56
57     private JPanel directoryPanel = new JPanel();
58     private JPanel bottomPanel;
59
60     protected JButton approveButton;
61
62     private String JavaDoc enterFileNameLabelText = null;
63     private int enterFileNameLabelMnemonic = 0;
64
65     private String JavaDoc filesLabelText = null;
66     private int filesLabelMnemonic = 0;
67
68     private String JavaDoc foldersLabelText = null;
69     private int foldersLabelMnemonic = 0;
70
71     private String JavaDoc pathLabelText = null;
72     private int pathLabelMnemonic = 0;
73
74     private String JavaDoc filterLabelText = null;
75     private int filterLabelMnemonic = 0;
76
77     private String JavaDoc fileNameString(File JavaDoc file) {
78         if (file == null) {
79             return null;
80         } else {
81             JFileChooser fc = getFileChooser();
82             if (fc.isDirectorySelectionEnabled() && !fc.isFileSelectionEnabled()) {
83                 return file.getPath();
84             } else {
85                 return file.getName();
86             }
87         }
88     }
89
90     private String JavaDoc fileNameString(File JavaDoc[] files) {
91         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
92         for (int i = 0; files != null && i < files.length; i++) {
93             if (i > 0) {
94                 buf.append(" ");
95             }
96             if (files.length > 1) {
97                 buf.append("\"");
98             }
99             buf.append(fileNameString(files[i]));
100             if (files.length > 1) {
101                 buf.append("\"");
102             }
103         }
104         return buf.toString();
105     }
106
107     public MotifFileChooserUI(JFileChooser filechooser) {
108     super(filechooser);
109     }
110
111     public String JavaDoc getFileName() {
112     if(filenameTextField != null) {
113         return filenameTextField.getText();
114     } else {
115         return null;
116     }
117     }
118
119     public void setFileName(String JavaDoc filename) {
120     if(filenameTextField != null) {
121         filenameTextField.setText(filename);
122     }
123     }
124
125     public String JavaDoc getDirectoryName() {
126     return pathField.getText();
127     }
128
129     public void setDirectoryName(String JavaDoc dirname) {
130     pathField.setText(dirname);
131     }
132
133     public void ensureFileIsVisible(JFileChooser fc, File JavaDoc f) {
134     // PENDING(jeff)
135
}
136
137     public void rescanCurrentDirectory(JFileChooser fc) {
138         getModel().validateFileCache();
139     }
140
141     public PropertyChangeListener createPropertyChangeListener(JFileChooser fc) {
142     return new PropertyChangeListener() {
143         public void propertyChange(PropertyChangeEvent e) {
144         String JavaDoc prop = e.getPropertyName();
145         if(prop.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
146             File JavaDoc f = (File JavaDoc) e.getNewValue();
147             if(f != null) {
148             setFileName(getFileChooser().getName(f));
149             }
150         }
151         else if (prop.equals(JFileChooser.SELECTED_FILES_CHANGED_PROPERTY)) {
152             File JavaDoc[] files = (File JavaDoc[]) e.getNewValue();
153             JFileChooser fc = getFileChooser();
154             if (files != null && files.length > 0 && (files.length > 1 || fc.isDirectorySelectionEnabled() || !files[0].isDirectory())) {
155                setFileName(fileNameString(files));
156             }
157         } else if(prop.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)) {
158             directoryList.clearSelection();
159                     ListSelectionModel sm = directoryList.getSelectionModel();
160                     if (sm instanceof DefaultListSelectionModel) {
161                         ((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0);
162                         ((DefaultListSelectionModel)sm).setAnchorSelectionIndex(0);
163                     }
164             fileList.clearSelection();
165                     sm = fileList.getSelectionModel();
166                     if (sm instanceof DefaultListSelectionModel) {
167                         ((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0);
168                         ((DefaultListSelectionModel)sm).setAnchorSelectionIndex(0);
169                     }
170             File JavaDoc currentDirectory = getFileChooser().getCurrentDirectory();
171             if(currentDirectory != null) {
172             try {
173                 setDirectoryName(((File JavaDoc)e.getNewValue()).getCanonicalPath());
174             } catch (IOException JavaDoc ioe) {
175                 setDirectoryName(((File JavaDoc)e.getNewValue()).getAbsolutePath());
176             }
177             if ((getFileChooser().getFileSelectionMode() == JFileChooser.DIRECTORIES_ONLY) && !getFileChooser().isMultiSelectionEnabled()) {
178                 setFileName(getDirectoryName());
179             }
180             }
181         } else if(prop.equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY)) {
182             directoryList.clearSelection();
183         } else if(prop == JFileChooser.MULTI_SELECTION_ENABLED_CHANGED_PROPERTY) {
184             if(getFileChooser().isMultiSelectionEnabled()) {
185             fileList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
186             } else {
187             fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
188             fileList.clearSelection();
189             getFileChooser().setSelectedFiles(null);
190             }
191         } else if(prop == JFileChooser.ACCESSORY_CHANGED_PROPERTY) {
192             if(getAccessoryPanel() != null) {
193             if(e.getOldValue() != null) {
194                 getAccessoryPanel().remove((JComponent) e.getOldValue());
195             }
196             JComponent accessory = (JComponent) e.getNewValue();
197             if(accessory != null) {
198                 getAccessoryPanel().add(accessory, BorderLayout.CENTER);
199                 getAccessoryPanel().setPreferredSize(PREF_ACC_SIZE);
200                 getAccessoryPanel().setMaximumSize(MAX_SIZE);
201             } else {
202                 getAccessoryPanel().setPreferredSize(ZERO_ACC_SIZE);
203                 getAccessoryPanel().setMaximumSize(ZERO_ACC_SIZE);
204             }
205             }
206         } else if (prop == JFileChooser.APPROVE_BUTTON_TEXT_CHANGED_PROPERTY ||
207                prop == JFileChooser.APPROVE_BUTTON_TOOL_TIP_TEXT_CHANGED_PROPERTY ||
208                prop == JFileChooser.DIALOG_TYPE_CHANGED_PROPERTY) {
209             approveButton.setText(getApproveButtonText(getFileChooser()));
210             approveButton.setToolTipText(getApproveButtonToolTipText(getFileChooser()));
211         } else if (prop.equals(JFileChooser.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY)) {
212                     doControlButtonsChanged(e);
213                 } else if (prop.equals("componentOrientation")) {
214             ComponentOrientation o = (ComponentOrientation)e.getNewValue();
215             JFileChooser cc = (JFileChooser)e.getSource();
216             if (o != (ComponentOrientation)e.getOldValue()) {
217             cc.applyComponentOrientation(o);
218             }
219         } else if (prop == JFileChooser.FILE_FILTER_CHANGED_PROPERTY) {
220             fileList.clearSelection();
221             setFileName(null);
222         }
223         }
224     };
225     }
226
227     //
228
// ComponentUI Interface Implementation methods
229
//
230
public static ComponentUI createUI(JComponent c) {
231         return new MotifFileChooserUI((JFileChooser)c);
232     }
233
234     public void installUI(JComponent c) {
235     super.installUI(c);
236     }
237
238     public void uninstallUI(JComponent c) {
239     c.removePropertyChangeListener(filterComboBoxModel);
240     approveButton.removeActionListener(getApproveSelectionAction());
241     filenameTextField.removeActionListener(getApproveSelectionAction());
242     super.uninstallUI(c);
243     }
244
245     public void installComponents(JFileChooser fc) {
246     fc.setLayout(new BorderLayout(10, 10));
247     fc.setAlignmentX(JComponent.CENTER_ALIGNMENT);
248
249     JPanel interior = new JPanel() {
250         public Insets getInsets() {
251         return insets;
252         }
253     };
254     align(interior);
255     interior.setLayout(new BoxLayout(interior, BoxLayout.PAGE_AXIS));
256
257     fc.add(interior, BorderLayout.CENTER);
258
259     // PENDING(jeff) - I18N
260
JLabel l = new JLabel(pathLabelText);
261     l.setDisplayedMnemonic(pathLabelMnemonic);
262     align(l);
263     interior.add(l);
264
265     File JavaDoc currentDirectory = fc.getCurrentDirectory();
266     String JavaDoc curDirName = null;
267     if(currentDirectory != null) {
268         curDirName = currentDirectory.getPath();
269     }
270     pathField = new JTextField(curDirName) {
271         public Dimension getMaximumSize() {
272         Dimension d = super.getMaximumSize();
273         d.height = getPreferredSize().height;
274         return d;
275         }
276     };
277     l.setLabelFor(pathField);
278     align(pathField);
279
280     // Change to folder on return
281
pathField.addActionListener(getUpdateAction());
282     interior.add(pathField);
283
284     interior.add(Box.createRigidArea(vstrut10));
285
286
287     // CENTER: left, right accessory
288
JPanel centerPanel = new JPanel();
289     centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.LINE_AXIS));
290     align(centerPanel);
291
292     // left panel - Filter & folderList
293
JPanel leftPanel = new JPanel();
294     leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
295     align(leftPanel);
296
297     // add the filter PENDING(jeff) - I18N
298
l = new JLabel(filterLabelText);
299     l.setDisplayedMnemonic(filterLabelMnemonic);
300     align(l);
301     leftPanel.add(l);
302
303     filterComboBox = new JComboBox() {
304         public Dimension getMaximumSize() {
305         Dimension d = super.getMaximumSize();
306         d.height = getPreferredSize().height;
307         return d;
308         }
309     };
310         l.setLabelFor(filterComboBox);
311     filterComboBoxModel = createFilterComboBoxModel();
312     filterComboBox.setModel(filterComboBoxModel);
313     filterComboBox.setRenderer(createFilterComboBoxRenderer());
314     fc.addPropertyChangeListener(filterComboBoxModel);
315     align(filterComboBox);
316     leftPanel.add(filterComboBox);
317
318     // leftPanel.add(Box.createRigidArea(vstrut10));
319

320     // Add the Folder List PENDING(jeff) - I18N
321
l = new JLabel(foldersLabelText);
322     l.setDisplayedMnemonic(foldersLabelMnemonic);
323     align(l);
324     leftPanel.add(l);
325     JScrollPane sp = createDirectoryList();
326         sp.getVerticalScrollBar().setFocusable(false);
327         sp.getHorizontalScrollBar().setFocusable(false);
328     l.setLabelFor(sp.getViewport().getView());
329     leftPanel.add(sp);
330
331
332     // create files list
333
JPanel rightPanel = new JPanel();
334     align(rightPanel);
335     rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.PAGE_AXIS));
336
337     l = new JLabel(filesLabelText);
338     l.setDisplayedMnemonic(filesLabelMnemonic);
339     align(l);
340     rightPanel.add(l);
341     sp = createFilesList();
342     l.setLabelFor(sp);
343     rightPanel.add(sp);
344
345     centerPanel.add(leftPanel);
346     centerPanel.add(Box.createRigidArea(hstrut10));
347     centerPanel.add(rightPanel);
348
349     JComponent accessoryPanel = getAccessoryPanel();
350     JComponent accessory = fc.getAccessory();
351     if(accessoryPanel != null) {
352         if(accessory == null) {
353         accessoryPanel.setPreferredSize(ZERO_ACC_SIZE);
354         accessoryPanel.setMaximumSize(ZERO_ACC_SIZE);
355         } else {
356         getAccessoryPanel().add(accessory, BorderLayout.CENTER);
357         accessoryPanel.setPreferredSize(PREF_ACC_SIZE);
358         accessoryPanel.setMaximumSize(MAX_SIZE);
359         }
360         align(accessoryPanel);
361         centerPanel.add(accessoryPanel);
362     }
363     interior.add(centerPanel);
364     interior.add(Box.createRigidArea(vstrut10));
365
366     // add the filename field PENDING(jeff) - I18N
367
l = new JLabel(enterFileNameLabelText);
368     l.setDisplayedMnemonic(enterFileNameLabelMnemonic);
369     align(l);
370     interior.add(l);
371
372     filenameTextField = new JTextField() {
373         public Dimension getMaximumSize() {
374         Dimension d = super.getMaximumSize();
375         d.height = getPreferredSize().height;
376         return d;
377         }
378     };
379     l.setLabelFor(filenameTextField);
380     filenameTextField.addActionListener(getApproveSelectionAction());
381     align(filenameTextField);
382     filenameTextField.setAlignmentX(JComponent.LEFT_ALIGNMENT);
383     interior.add(filenameTextField);
384
385     bottomPanel = getBottomPanel();
386     bottomPanel.add(new JSeparator(), BorderLayout.NORTH);
387
388     // Add buttons
389
JPanel buttonPanel = new JPanel();
390     align(buttonPanel);
391     buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
392     buttonPanel.add(Box.createGlue());
393
394     approveButton = new JButton(getApproveButtonText(fc)) {
395         public Dimension getMaximumSize() {
396         return new Dimension(MAX_SIZE.width, this.getPreferredSize().height);
397         }
398     };
399     approveButton.setMnemonic(getApproveButtonMnemonic(fc));
400     approveButton.setToolTipText(getApproveButtonToolTipText(fc));
401     align(approveButton);
402     approveButton.setMargin(buttonMargin);
403     approveButton.addActionListener(getApproveSelectionAction());
404     buttonPanel.add(approveButton);
405     buttonPanel.add(Box.createGlue());
406
407     JButton updateButton = new JButton(updateButtonText) {
408         public Dimension getMaximumSize() {
409         return new Dimension(MAX_SIZE.width, this.getPreferredSize().height);
410         }
411     };
412     updateButton.setMnemonic(updateButtonMnemonic);
413     updateButton.setToolTipText(updateButtonToolTipText);
414     align(updateButton);
415     updateButton.setMargin(buttonMargin);
416     updateButton.addActionListener(getUpdateAction());
417     buttonPanel.add(updateButton);
418     buttonPanel.add(Box.createGlue());
419
420     JButton cancelButton = new JButton(cancelButtonText) {
421         public Dimension getMaximumSize() {
422         return new Dimension(MAX_SIZE.width, this.getPreferredSize().height);
423         }
424     };
425     cancelButton.setMnemonic(cancelButtonMnemonic);
426     cancelButton.setToolTipText(cancelButtonToolTipText);
427     align(cancelButton);
428     cancelButton.setMargin(buttonMargin);
429     cancelButton.addActionListener(getCancelSelectionAction());
430     buttonPanel.add(cancelButton);
431     buttonPanel.add(Box.createGlue());
432
433     JButton helpButton = new JButton(helpButtonText) {
434         public Dimension getMaximumSize() {
435         return new Dimension(MAX_SIZE.width, this.getPreferredSize().height);
436         }
437     };
438     helpButton.setMnemonic(helpButtonMnemonic);
439     helpButton.setToolTipText(helpButtonToolTipText);
440     align(helpButton);
441     helpButton.setMargin(buttonMargin);
442     helpButton.setEnabled(false);
443     buttonPanel.add(helpButton);
444     buttonPanel.add(Box.createGlue());
445
446     bottomPanel.add(buttonPanel, BorderLayout.SOUTH);
447     if (fc.getControlButtonsAreShown()) {
448            fc.add(bottomPanel, BorderLayout.SOUTH);
449         }
450     }
451
452     protected JPanel getBottomPanel() {
453         if (bottomPanel == null) {
454             bottomPanel = new JPanel(new BorderLayout(0, 4));
455         }
456         return bottomPanel;
457     }
458
459     private void doControlButtonsChanged(PropertyChangeEvent e) {
460         if (getFileChooser().getControlButtonsAreShown()) {
461             getFileChooser().add(bottomPanel,BorderLayout.SOUTH);
462         } else {
463             getFileChooser().remove(getBottomPanel());
464         }
465     }
466
467     public void uninstallComponents(JFileChooser fc) {
468     fc.removeAll();
469     bottomPanel = null;
470     directoryPanel = null;
471     }
472
473     protected void installStrings(JFileChooser fc) {
474         super.installStrings(fc);
475
476         Locale l = fc.getLocale();
477
478     enterFileNameLabelText = UIManager.getString("FileChooser.enterFileNameLabelText",l);
479     enterFileNameLabelMnemonic = UIManager.getInt("FileChooser.enterFileNameLabelMnemonic");
480     
481     filesLabelText = UIManager.getString("FileChooser.filesLabelText",l);
482     filesLabelMnemonic = UIManager.getInt("FileChooser.filesLabelMnemonic");
483     
484     foldersLabelText = UIManager.getString("FileChooser.foldersLabelText",l);
485     foldersLabelMnemonic = UIManager.getInt("FileChooser.foldersLabelMnemonic");
486     
487     pathLabelText = UIManager.getString("FileChooser.pathLabelText",l);
488     pathLabelMnemonic = UIManager.getInt("FileChooser.pathLabelMnemonic");
489     
490     filterLabelText = UIManager.getString("FileChooser.filterLabelText",l);
491     filterLabelMnemonic = UIManager.getInt("FileChooser.filterLabelMnemonic");
492     }
493
494     protected void installIcons(JFileChooser fc) {
495     // Since motif doesn't have button icons, leave this empty
496
// which overrides the supertype icon loading
497
}
498
499     protected void uninstallIcons(JFileChooser fc) {
500     // Since motif doesn't have button icons, leave this empty
501
// which overrides the supertype icon loading
502
}
503
504     protected JScrollPane createFilesList() {
505     fileList = new JList();
506
507     if(getFileChooser().isMultiSelectionEnabled()) {
508         fileList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
509     } else {
510         fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
511     }
512     
513     fileList.setModel(new MotifFileListModel());
514         fileList.getSelectionModel().removeSelectionInterval(0, 0);
515     fileList.setCellRenderer(new FileCellRenderer());
516     fileList.addListSelectionListener(createListSelectionListener(getFileChooser()));
517     fileList.addMouseListener(createDoubleClickListener(getFileChooser(), fileList));
518     align(fileList);
519     JScrollPane scrollpane = new JScrollPane(fileList);
520     scrollpane.setPreferredSize(prefListSize);
521     scrollpane.setMaximumSize(MAX_SIZE);
522     align(scrollpane);
523     return scrollpane;
524     }
525
526     protected JScrollPane createDirectoryList() {
527     directoryList = new JList();
528     align(directoryList);
529
530     directoryList.setCellRenderer(new DirectoryCellRenderer());
531     directoryList.setModel(new MotifDirectoryListModel());
532         directoryList.getSelectionModel().removeSelectionInterval(0, 0);
533     directoryList.addMouseListener(createDoubleClickListener(getFileChooser(), directoryList));
534     directoryList.addListSelectionListener(createListSelectionListener(getFileChooser()));
535
536     JScrollPane scrollpane = new JScrollPane(directoryList);
537     scrollpane.setMaximumSize(MAX_SIZE);
538     scrollpane.setPreferredSize(prefListSize);
539     align(scrollpane);
540     return scrollpane;
541     }
542
543     public Dimension getPreferredSize(JComponent c) {
544     Dimension prefSize =
545         (getFileChooser().getAccessory() != null) ? WITH_ACCELERATOR_PREF_SIZE : PREF_SIZE;
546     Dimension d = c.getLayout().preferredLayoutSize(c);
547     if (d != null) {
548         return new Dimension(d.width < prefSize.width ? prefSize.width : d.width,
549                  d.height < prefSize.height ? prefSize.height : d.height);
550     } else {
551         return prefSize;
552     }
553     }
554
555     public Dimension getMinimumSize(JComponent x) {
556     return MIN_SIZE;
557     }
558
559     public Dimension getMaximumSize(JComponent x) {
560     return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
561     }
562
563     protected void align(JComponent c) {
564     c.setAlignmentX(JComponent.LEFT_ALIGNMENT);
565     c.setAlignmentY(JComponent.TOP_ALIGNMENT);
566     }
567
568     protected class FileCellRenderer extends DefaultListCellRenderer {
569     public Component getListCellRendererComponent(JList list, Object JavaDoc value, int index,
570                               boolean isSelected, boolean cellHasFocus) {
571
572         super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
573         setText(getFileChooser().getName((File JavaDoc) value));
574         return this;
575     }
576     }
577
578     protected class DirectoryCellRenderer extends DefaultListCellRenderer {
579     public Component getListCellRendererComponent(JList list, Object JavaDoc value, int index,
580                               boolean isSelected, boolean cellHasFocus) {
581
582         super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
583         setText(getFileChooser().getName((File JavaDoc) value));
584         return this;
585     }
586     }
587
588     protected class MotifDirectoryListModel extends AbstractListModel implements ListDataListener {
589     public MotifDirectoryListModel() {
590         getModel().addListDataListener(this);
591     }
592
593     public int getSize() {
594         return getModel().getDirectories().size();
595     }
596
597     public Object JavaDoc getElementAt(int index) {
598         return getModel().getDirectories().elementAt(index);
599     }
600
601     public void intervalAdded(ListDataEvent e) {
602             fireIntervalAdded(this, e.getIndex0(), e.getIndex1());
603     }
604
605     public void intervalRemoved(ListDataEvent e) {
606             fireIntervalRemoved(this, e.getIndex0(), e.getIndex1());
607     }
608
609     // PENDING(jeff) - this is inefficient - should sent out
610
// incremental adjustment values instead of saying that the
611
// whole list has changed.
612
public void fireContentsChanged() {
613         fireContentsChanged(this, 0, getModel().getDirectories().size()-1);
614     }
615
616     // PENDING(jeff) - fire the correct interval changed - currently sending
617
// out that everything has changed
618
public void contentsChanged(ListDataEvent e) {
619         fireContentsChanged();
620     }
621
622     }
623
624     protected class MotifFileListModel extends AbstractListModel implements ListDataListener {
625     public MotifFileListModel() {
626         getModel().addListDataListener(this);
627     }
628
629     public int getSize() {
630         return getModel().getFiles().size();
631     }
632
633     public boolean contains(Object JavaDoc o) {
634         return getModel().getFiles().contains(o);
635     }
636
637     public int indexOf(Object JavaDoc o) {
638         return getModel().getFiles().indexOf(o);
639     }
640
641     public Object JavaDoc getElementAt(int index) {
642         return getModel().getFiles().elementAt(index);
643     }
644
645     public void intervalAdded(ListDataEvent e) {
646             fireIntervalAdded(this, e.getIndex0(), e.getIndex1());
647     }
648
649     public void intervalRemoved(ListDataEvent e) {
650             fireIntervalRemoved(this, e.getIndex0(), e.getIndex1());
651     }
652
653     // PENDING(jeff) - this is inefficient - should sent out
654
// incremental adjustment values instead of saying that the
655
// whole list has changed.
656
public void fireContentsChanged() {
657         fireContentsChanged(this, 0, getModel().getFiles().size()-1);
658     }
659
660     // PENDING(jeff) - fire the interval changed
661
public void contentsChanged(ListDataEvent e) {
662         fireContentsChanged();
663     }
664
665     }
666
667     //
668
// DataModel for Types Comboxbox
669
//
670
protected FilterComboBoxModel createFilterComboBoxModel() {
671     return new FilterComboBoxModel();
672     }
673
674     //
675
// Renderer for Types ComboBox
676
//
677
protected FilterComboBoxRenderer createFilterComboBoxRenderer() {
678     return new FilterComboBoxRenderer();
679     }
680
681
682     /**
683      * Render different type sizes and styles.
684      */

685     public class FilterComboBoxRenderer extends DefaultListCellRenderer {
686     public Component getListCellRendererComponent(JList list,
687         Object JavaDoc value, int index, boolean isSelected,
688         boolean cellHasFocus) {
689
690         super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
691
692         if (value != null && value instanceof FileFilter) {
693         setText(((FileFilter)value).getDescription());
694         }
695
696         return this;
697     }
698     }
699
700     /**
701      * Data model for a type-face selection combo-box.
702      */

703     protected class FilterComboBoxModel extends AbstractListModel implements ComboBoxModel, PropertyChangeListener {
704     protected FileFilter[] filters;
705     protected FilterComboBoxModel() {
706         super();
707         filters = getFileChooser().getChoosableFileFilters();
708     }
709
710     public void propertyChange(PropertyChangeEvent e) {
711         String JavaDoc prop = e.getPropertyName();
712         if(prop == JFileChooser.CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY) {
713         filters = (FileFilter[]) e.getNewValue();
714         fireContentsChanged(this, -1, -1);
715         } else if (prop == JFileChooser.FILE_FILTER_CHANGED_PROPERTY) {
716         fireContentsChanged(this, -1, -1);
717         }
718     }
719
720     public void setSelectedItem(Object JavaDoc filter) {
721         if(filter != null) {
722         getFileChooser().setFileFilter((FileFilter) filter);
723         fireContentsChanged(this, -1, -1);
724         }
725     }
726
727     public Object JavaDoc getSelectedItem() {
728         // Ensure that the current filter is in the list.
729
// NOTE: we shouldnt' have to do this, since JFileChooser adds
730
// the filter to the choosable filters list when the filter
731
// is set. Lets be paranoid just in case someone overrides
732
// setFileFilter in JFileChooser.
733
FileFilter currentFilter = getFileChooser().getFileFilter();
734         boolean found = false;
735         if(currentFilter != null) {
736         for(int i=0; i < filters.length; i++) {
737             if(filters[i] == currentFilter) {
738             found = true;
739             }
740         }
741         if(found == false) {
742             getFileChooser().addChoosableFileFilter(currentFilter);
743         }
744         }
745         return getFileChooser().getFileFilter();
746     }
747
748     public int getSize() {
749         if(filters != null) {
750         return filters.length;
751         } else {
752         return 0;
753         }
754     }
755
756     public Object JavaDoc getElementAt(int index) {
757         if(index > getSize() - 1) {
758         // This shouldn't happen. Try to recover gracefully.
759
return getFileChooser().getFileFilter();
760         }
761         if(filters != null) {
762         return filters[index];
763         } else {
764         return null;
765         }
766     }
767     }
768
769     protected JButton getApproveButton(JFileChooser fc) {
770     return approveButton;
771     }
772
773 }
774
Popular Tags