KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > keyboard > KeyboardEditor


1 package SnowMailClient.keyboard;
2
3 import snow.sortabletable.*;
4 import snow.Language.*;
5 import snow.utils.gui.*;
6 import snow.utils.storage.*;
7
8 import java.util.*;
9 import java.awt.*;
10 import java.awt.event.*;
11 import javax.swing.*;
12 import javax.swing.border.*;
13 import javax.swing.table.*;
14
15
16 public final class KeyboardEditor extends JDialog
17 {
18   final KeyboardTableModel keyboardTableModel = new KeyboardTableModel();
19   final JTable table = new JTable(keyboardTableModel);
20   final KeyboardDialog keyboardDialog;
21
22   public KeyboardEditor( KeyboardDialog owner )
23   {
24     super(owner, Language.translate("Keyboard editor"), false);
25     keyboardDialog = owner;
26
27     keyboardTableModel.setMap( owner.getActualKeyboardMap() );
28
29     getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
30
31     JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
32     getContentPane().add(controlPanel, BorderLayout.NORTH);
33     JButton searchUnicode = new JButton(Language.translate("Edit / Add keys"));
34     controlPanel.add(searchUnicode);
35     searchUnicode.addActionListener(new ActionListener()
36     {
37         public void actionPerformed(ActionEvent ae)
38         {
39            int[] sel = table.getSelectedRows();
40            int[] selCodes = new int[sel.length];
41            for(int i=0; i<sel.length; i++)
42            {
43               KeyboardKey key = keyboardTableModel.getKeyAt(sel[i]);
44               selCodes[i] = key.codeSMALL;
45            }
46
47            UnicodeSearchDialog uv = new UnicodeSearchDialog(KeyboardEditor.this, selCodes);
48            if(!uv.wasCancelled())
49            {
50               int[] retCodes = uv.getSelectedCodes();
51               Arrays.sort(retCodes);
52               //System.out.println(""+retCodes.length);
53
for(int i=0; i<retCodes.length; i++)
54               {
55                  KeyboardKey key = new KeyboardKey(
56                     "", (int) Character.toUpperCase( (char) retCodes[i] ),
57                     "" + (char) retCodes[i]);
58                  keyboardTableModel.addKey(key);
59               }
60            }
61         }
62     });
63
64     JButton removeSelected = new JButton(Language.translate("Remove Selected"));
65     controlPanel.add(removeSelected);
66     removeSelected.addActionListener(new ActionListener()
67     {
68         public void actionPerformed(ActionEvent ae)
69         {
70            int[] sel = table.getSelectedRows();
71            Arrays.sort(sel);
72            for(int i=sel.length-1; i>=0; i--)
73            {
74               keyboardTableModel.removeKeyAt(sel[i]);
75            }
76         }
77     });
78
79     setSize(400,400);
80     this.setLocationRelativeTo(owner);
81     setVisible(true);
82   } // Constructor
83

84
85   class KeyboardTableModel extends AbstractTableModel
86   {
87     KeyboardMap map;
88     public KeyboardTableModel()
89     {
90     }
91
92     public void setMap(KeyboardMap map)
93     {
94       this.map = map;
95     }
96
97     public KeyboardKey getKeyAt(int row)
98     {
99       if(row<0 || row>=map.getKeys().size()) return null;
100       return map.getKeys().elementAt(row);
101     }
102
103
104     public Object JavaDoc getValueAt(int row, int col)
105     {
106       KeyboardKey key = getKeyAt(row);
107       if(col==0) return ""+(row+1);
108       if(col==1) return ""+key.codeSMALL;
109       if(col==2) return ""+key.codeCAPITAL;
110       if(col==3) return ""+key.toStringSMALL();
111       if(col==4) return ""+key.toStringCAPITAL();
112       if(col==5) return ""+key.toStringAllShortcuts();
113       return "?";
114     }
115
116     public int getColumnCount()
117     {
118
119       return 6;
120     }
121
122     public String JavaDoc getColumnName(int col)
123     {
124       if(col==0) return "Number";
125       if(col==1) return "Unicode small";
126       if(col==2) return "Unicode capital";
127       if(col==3) return "small key";
128       if(col==4) return "capital key";
129       if(col==5) return "shortcuts";
130       return "";
131     }
132
133     public int getRowCount()
134     {
135       if(map==null) return 0;
136       return map.getKeys().size();
137     }
138
139     public boolean isCellEditable(int row, int col)
140     {
141       if(col==5) return true;
142       return false;
143     }
144
145     public void setValueAt(Object JavaDoc val, int row, int col)
146     {
147       if(col==5)
148       {
149          KeyboardKey key = getKeyAt(row);
150          key.shortcuts = new String JavaDoc[]{ ""+val };
151          // reload the map
152
updatekeyboardView();
153       }
154     }
155
156     public void updatekeyboardView()
157     {
158        keyboardDialog.setKeyboard( keyboardDialog.getActualKeyboardMap() );
159        keyboardDialog.pack();
160     }
161
162     public void removeKeyAt(int row)
163     {
164       if(row<0 || row>=map.getKeys().size()) return;
165
166       map.getKeys().removeElementAt(row);
167       updatekeyboardView();
168       this.fireTableDataChanged();
169
170     }
171
172     public void addKey(KeyboardKey key)
173     {
174       map.getKeys().add(key);
175       updatekeyboardView();
176       this.fireTableDataChanged();
177     }
178   }
179
180   public static void main(String JavaDoc[] a)
181   {
182      KeyboardDialog.main(null);
183   }
184
185 } // KeyboardEditor
Popular Tags