KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SoundPanel


1 import java.awt.*;
2 import java.awt.event.*;
3 import java.awt.image.ImageObserver JavaDoc;
4 import java.io.*;
5 import java.util.Properties JavaDoc;
6 import javax.swing.*;
7 import javax.swing.event.*;
8 import javax.accessibility.Accessible JavaDoc;
9
10 /** Panel for choosing a sound alert in the optionsDialog.*/
11 public final class SoundPanel extends JPanel{
12     private final Properties JavaDoc prop;
13         
14     private final static String JavaDoc[] SOUND_OPTIONS={"default","none","custom"};
15     private final static String JavaDoc[] DISPLAY={"opt_default sound","opt_no sound","opt_custom sound"};
16     private final String JavaDoc key;
17     private final JLabel cmbLbl=new JLabel();
18     private final JTextField custom=new JTextField(OptionsDialog2.TEXTFIELD_SIZE);
19     private final JButton browse=new JButton("",Icons.BROWSE);
20     private final JComboBox cmb=new JComboBox(SOUND_OPTIONS);
21     private final ChangeListener cl=new ChangeListener();
22     private final OptionsDialog2 dialog;
23         
24     public SoundPanel(OptionsDialog2 dialog,Properties JavaDoc prop, String JavaDoc key){
25         super();
26         this.dialog=dialog;
27         this.key=key;
28         this.prop=prop;
29         setLayout(new GridLayout(2,1,0,0));
30         cmb.setRenderer(new Renderer());
31         cmb.addActionListener(cl);
32         cmb.setSelectedItem(prop.getProperty(key));
33         cmbLbl.setText(Lang.gs(OptionsDialog2.PREFIX+key));
34         add(GUI.panel(cmbLbl,cmb,OptionsDialog2.GAP));
35         browse.setActionCommand("sound browse");
36         browse.addActionListener(cl);
37         browse.setToolTipText(Lang.gs("browse_tt"));
38         browse.setMargin(new Insets(0,0,0,0));
39         final JPanel p=new JPanel(new FlowLayout(FlowLayout.LEFT,OptionsDialog2.GAP,OptionsDialog2.GAP));
40         p.add(custom);
41         p.add(Box.createGlue());
42         p.add(browse);
43         add(p);
44     }
45     
46     final class Renderer extends JLabel implements ListCellRenderer {
47         public Component getListCellRendererComponent(JList list,Object JavaDoc value,int index,boolean isSelected,boolean cellHasFocus){
48             if(index!=-1){
49                 setText(Lang.gs(DISPLAY[index]));
50             }
51             else{
52                 String JavaDoc v=(String JavaDoc) value;
53                 for(int i=0;i<DISPLAY.length;i++){
54                     if(SOUND_OPTIONS[i].equals(v)){
55                         setText(Lang.gs(DISPLAY[i]));
56                         break;
57                     }
58                 }
59             }
60             if(isSelected){
61                 setBackground(list.getSelectionBackground());
62                 setForeground(list.getSelectionForeground());
63             }
64             else {
65                 setBackground(list.getBackground());
66                 setForeground(list.getForeground());
67             }
68             setEnabled(list.isEnabled());
69             setFont(list.getFont());
70             setOpaque(true);
71             return this;
72         }
73     }
74     
75     final class ChangeListener implements ActionListener{
76         public void actionPerformed(ActionEvent ae){
77             if(ae.getActionCommand().equals("sound browse")){
78                 if(OptionsDialog2.fileChooser.showOpenDialog(dialog)==JFileChooser.APPROVE_OPTION){
79                     custom.setText(OptionsDialog2.fileChooser.getSelectedFile().getPath());
80                     prop.setProperty(key,custom.getText());
81                 }
82                 return;
83             }
84             if(cmb.getSelectedIndex()==2){
85                 custom.setEnabled(true);
86                 browse.setEnabled(true);
87                 prop.setProperty(key,custom.getText());
88                 if(!custom.hasFocus()){
89                     custom.requestFocus();
90                 }
91                 return;
92             }
93             else{
94                 prop.setProperty(key,(String JavaDoc) cmb.getSelectedItem());
95                 custom.setEnabled(false);
96                 browse.setEnabled(false);
97             }
98         }
99     }
100 }
Popular Tags