KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > keymap > KeymapPanel


1 /*
2  * KeymapPanel1.java
3  *
4  * Created on February 20, 2006, 2:59 PM
5  */

6
7 package org.netbeans.modules.options.keymap;
8
9 import java.awt.Component JavaDoc;
10 import java.awt.Dimension JavaDoc;
11 import java.awt.event.ActionEvent JavaDoc;
12 import java.awt.event.ActionListener JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import javax.swing.AbstractButton JavaDoc;
16 import javax.swing.AbstractListModel JavaDoc;
17 import javax.swing.DefaultListModel JavaDoc;
18 import javax.swing.JLabel JavaDoc;
19 import javax.swing.JPanel JavaDoc;
20 import javax.swing.JScrollPane JavaDoc;
21 import javax.swing.SwingUtilities JavaDoc;
22 import javax.swing.event.ListSelectionEvent JavaDoc;
23 import javax.swing.event.ListSelectionListener JavaDoc;
24 import javax.swing.event.TreeSelectionEvent JavaDoc;
25 import javax.swing.event.TreeSelectionListener JavaDoc;
26 import org.openide.DialogDisplayer;
27 import org.openide.NotifyDescriptor;
28 import org.openide.NotifyDescriptor.InputLine;
29 import org.openide.NotifyDescriptor.Message;
30 import org.openide.awt.Mnemonics;
31 import org.openide.util.NbBundle;
32
33 /**
34  *
35  * @author Jan Jancura
36  */

37 public class KeymapPanel extends JPanel JavaDoc implements ActionListener JavaDoc,
38 TreeSelectionListener JavaDoc, ListSelectionListener JavaDoc {
39     
40     
41     private boolean listen = false;
42     
43     /** Creates new form KeymapPanel1 */
44     public KeymapPanel() {
45         initComponents();
46         loc (bDuplicate, "Duplicate");
47         loc (bDelete, "Delete");
48 // loc (rbAction, "Show_Actions");
49
// loc (rbShortcut, "Show_Shortcuts");
50
liShortcuts.getAccessibleContext ().setAccessibleName (loc ("AN_Shortcuts"));
51         liShortcuts.getAccessibleContext ().setAccessibleDescription (loc ("AD_Shortcuts"));
52         tActions.getAccessibleContext ().setAccessibleName (loc ("AN_Actions"));
53         tActions.getAccessibleContext ().setAccessibleDescription (loc ("AD_Actions"));
54         cbProfile.getAccessibleContext ().setAccessibleName (loc ("AN_Profiles"));
55         cbProfile.getAccessibleContext ().setAccessibleDescription (loc ("AD_Profiles"));
56 // bgViewAs.add (rbAction);
57
// bgViewAs.add (rbShortcut);
58
bDuplicate.addActionListener (this);
59         bDelete.addActionListener (this);
60 // rbAction.setSelected (true);
61
// rbAction.addActionListener (this);
62
// rbShortcut.addActionListener (this);
63
tActions.setRootVisible (false);
64         tActions.setShowsRootHandles (true);
65         tActions.addTreeSelectionListener (this);
66         cbProfile.addActionListener (this);
67         loc (bAdd, "Add_Shortcut");
68         loc (bRemove, "Remove_Shortcut");
69         bAdd.addActionListener (this);
70         bRemove.addActionListener (this);
71         liShortcuts.addListSelectionListener (this);
72         bAdd.setEnabled (false);
73         bRemove.setEnabled (false);
74         loc (lShortcuts, "Shortcuts"); // NOI18N
75
lShortcuts.setLabelFor (liShortcuts);
76         loc (lActions, "Actions");
77         lActions.setLabelFor (tActions);
78     }
79     
80     /** This method is called from within the constructor to
81      * initialize the form.
82      * WARNING: Do NOT modify this code. The content of this method is
83      * always regenerated by the Form Editor.
84      */

85     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
86
private void initComponents() {
87         lProfile = new javax.swing.JLabel JavaDoc();
88         cbProfile = new javax.swing.JComboBox JavaDoc();
89         bDuplicate = new javax.swing.JButton JavaDoc();
90         bDelete = new javax.swing.JButton JavaDoc();
91         lActions = new javax.swing.JLabel JavaDoc();
92         spActions = new javax.swing.JScrollPane JavaDoc();
93         tActions = new javax.swing.JTree JavaDoc();
94         lShortcuts = new javax.swing.JLabel JavaDoc();
95         spShortcuts = new javax.swing.JScrollPane JavaDoc();
96         liShortcuts = new javax.swing.JList JavaDoc();
97         bAdd = new javax.swing.JButton JavaDoc();
98         bRemove = new javax.swing.JButton JavaDoc();
99
100         lProfile.setText("Profile:");
101
102         bDuplicate.setText("Duplicate...");
103
104         bDelete.setText("Delete");
105
106         lActions.setText("Actions:");
107
108         spActions.setViewportView(tActions);
109
110         lShortcuts.setText("Shortcuts:");
111
112         spShortcuts.setViewportView(liShortcuts);
113
114         bAdd.setText("Add...");
115
116         bRemove.setText("Remove");
117
118         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
119         this.setLayout(layout);
120         layout.setHorizontalGroup(
121             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
122             .add(layout.createSequentialGroup()
123                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
124                     .add(layout.createSequentialGroup()
125                         .add(lProfile)
126                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
127                         .add(cbProfile, 0, 139, Short.MAX_VALUE)
128                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
129                         .add(bDuplicate))
130                     .add(layout.createSequentialGroup()
131                         .add(spShortcuts, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 175, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
132                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
133                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
134                             .add(bRemove)
135                             .add(bAdd))))
136                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
137                 .add(bDelete))
138             .add(layout.createSequentialGroup()
139                 .add(lActions)
140                 .addContainerGap())
141             .add(spActions, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE)
142             .add(layout.createSequentialGroup()
143                 .add(lShortcuts)
144                 .addContainerGap())
145         );
146
147         layout.linkSize(new java.awt.Component JavaDoc[] {bAdd, bRemove}, org.jdesktop.layout.GroupLayout.HORIZONTAL);
148
149         layout.linkSize(new java.awt.Component JavaDoc[] {bDelete, bDuplicate}, org.jdesktop.layout.GroupLayout.HORIZONTAL);
150
151         layout.setVerticalGroup(
152             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
153             .add(layout.createSequentialGroup()
154                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
155                     .add(bDelete)
156                     .add(bDuplicate)
157                     .add(lProfile)
158                     .add(cbProfile, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
159                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
160                 .add(lActions)
161                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
162                 .add(spActions, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
163                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
164                 .add(lShortcuts)
165                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
166                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
167                     .add(layout.createSequentialGroup()
168                         .add(bAdd)
169                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
170                         .add(bRemove))
171                     .add(spShortcuts, 0, 52, Short.MAX_VALUE)))
172         );
173     }// </editor-fold>//GEN-END:initComponents
174

175     
176     // Variables declaration - do not modify//GEN-BEGIN:variables
177
private javax.swing.JButton JavaDoc bAdd;
178     private javax.swing.JButton JavaDoc bDelete;
179     private javax.swing.JButton JavaDoc bDuplicate;
180     private javax.swing.JButton JavaDoc bRemove;
181     private javax.swing.JComboBox JavaDoc cbProfile;
182     private javax.swing.JLabel JavaDoc lActions;
183     private javax.swing.JLabel JavaDoc lProfile;
184     private javax.swing.JLabel JavaDoc lShortcuts;
185     private javax.swing.JList JavaDoc liShortcuts;
186     private javax.swing.JScrollPane JavaDoc spActions;
187     private javax.swing.JScrollPane JavaDoc spShortcuts;
188     private javax.swing.JTree JavaDoc tActions;
189     // End of variables declaration//GEN-END:variables
190

191     
192     public void actionPerformed (ActionEvent JavaDoc e) {
193         if (!listen) return;
194         Object JavaDoc source = e.getSource ();
195         if (source == bAdd) {
196             Object JavaDoc action = tActions.getSelectionPath ().getLastPathComponent ();
197             String JavaDoc shortcut = ShortcutsDialog.getShortcut (getModel ());
198             if (shortcut == null) return;
199             getModel ().addShortcut (
200                 tActions.getSelectionPath (), shortcut
201             );
202             selectAction (action);
203             if (liShortcuts.getModel ().getSize () > 0)
204                 liShortcuts.setSelectedIndex (0);
205             SwingUtilities.invokeLater (new Runnable JavaDoc () {
206                 public void run () {
207                     liShortcuts.requestFocus ();
208                 }
209             });
210         } else
211         if (source == bRemove) {
212             int index = liShortcuts.getSelectedIndex ();
213             Object JavaDoc action = tActions.getSelectionPath ().getLastPathComponent ();
214             String JavaDoc shortcut = (String JavaDoc) liShortcuts.getSelectedValue ();
215             getModel ().removeShortcut (
216                 tActions.getSelectionPath (), shortcut
217             );
218             selectAction (action);
219             if (liShortcuts.getModel ().getSize () > index)
220                 liShortcuts.setSelectedIndex (index);
221             else
222             if (liShortcuts.getModel ().getSize () > 0)
223                 liShortcuts.setSelectedIndex (0);
224         } else
225         if (source == bDelete) {
226             deleteCurrentProfile ();
227         } else
228         if (source == cbProfile) {
229             String JavaDoc profile = (String JavaDoc) cbProfile.getSelectedItem ();
230             getModel ().setCurrentProfile (profile);
231             if (getModel ().isCustomProfile (profile))
232                 loc (bDelete, "Delete"); // NOI18N
233
else
234                 loc (bDelete, "Restore"); // NOI18N
235
refreshAction ();
236         } else
237         if (source == bDuplicate) {
238             InputLine il = new InputLine (
239                 loc ("CTL_Create_New_Profile_Message"), // NOI18N
240
loc ("CTL_Create_New_Profile_Title") // NOI18N
241
);
242             il.setInputText ((String JavaDoc) cbProfile.
243                 getSelectedItem ());
244             DialogDisplayer.getDefault ().notify (il);
245             if (il.getValue () == NotifyDescriptor.OK_OPTION) {
246                 String JavaDoc newProfile = il.getInputText ();
247                 Iterator JavaDoc it = getModel ().getProfiles ().iterator ();
248                 while (it.hasNext ())
249                     if (newProfile.equals (it.next ())) {
250                         Message md = new Message (
251                             loc ("CTL_Duplicate_Profile_Name"), // NOI18N
252
Message.ERROR_MESSAGE
253                         );
254                         DialogDisplayer.getDefault ().notify (md);
255                         return;
256                     }
257                 getModel ().cloneProfile (newProfile);
258                 cbProfile.addItem (il.getInputText ());
259                 cbProfile.setSelectedItem (il.getInputText ());
260             }
261             return;
262         }
263     }
264     
265     public void valueChanged (TreeSelectionEvent JavaDoc e) {
266         if (!listen) return;
267         refreshAction ();
268     }
269     
270     public void valueChanged (ListSelectionEvent JavaDoc e) {
271         if (!listen) return;
272         // selected shourtcut changed
273
int i = liShortcuts.getSelectedIndex ();
274         if (i < 0) {
275             bRemove.setEnabled (false);
276             return;
277         }
278         bRemove.setEnabled (true);
279     }
280    
281     private boolean initialized = false;
282     void update () {
283         if (!initialized) {
284             initialized = true;
285             listen = false;
286
287             tActions.setCellRenderer (new KeymapListRenderer (getModel ()));
288             tActions.setModel (getModel ());
289
290             // cbProfile
291
List JavaDoc keymaps = getModel ().getProfiles ();
292             cbProfile.removeAllItems ();
293             int i, k = keymaps.size ();
294             for (i = 0; i < k; i++)
295                 cbProfile.addItem (keymaps.get (i));
296             listen = true;
297         }
298         cbProfile.setSelectedItem (getModel ().getCurrentProfile ());
299     }
300     
301     private void deleteCurrentProfile () {
302         String JavaDoc currentProfile = (String JavaDoc) cbProfile.getSelectedItem ();
303         getModel ().deleteProfile (currentProfile);
304         if (getModel ().isCustomProfile (currentProfile)) {
305             cbProfile.removeItem (currentProfile);
306             cbProfile.setSelectedIndex (0);
307         }
308     }
309     
310     void applyChanges () {
311         if (!initialized) return; // not initialized yet.
312
getModel ().apply ();
313     }
314     
315     void cancel () {
316         if (model == null) return;
317         model.cancel ();
318     }
319     
320     boolean dataValid () {
321         return true;
322     }
323     
324     boolean isChanged () {
325         return getModel ().isChanged ();
326     }
327     
328     private KeymapViewModel model;
329
330     KeymapViewModel getModel () {
331         if (model == null)
332             model = new KeymapViewModel ();
333         return model;
334     }
335     
336     
337     // other methods ...........................................................
338

339     private static String JavaDoc loc (String JavaDoc key) {
340         return NbBundle.getMessage (KeymapPanel.class, key);
341     }
342     
343     private static void loc (Component JavaDoc c, String JavaDoc key) {
344         if (!(c instanceof JLabel JavaDoc)) {
345             c.getAccessibleContext ().setAccessibleName (loc ("AN_" + key));
346             c.getAccessibleContext ().setAccessibleDescription (loc ("AD_" + key));
347         }
348         if (c instanceof AbstractButton JavaDoc) {
349             Mnemonics.setLocalizedText (
350                 (AbstractButton JavaDoc) c,
351                 loc ("CTL_" + key)
352             );
353         } else {
354             Mnemonics.setLocalizedText (
355                 (JLabel JavaDoc) c,
356                 loc ("CTL_" + key)
357             );
358         }
359     }
360     
361     void refreshAction () {
362         Object JavaDoc action = tActions.getSelectionPath () == null ?
363             null : tActions.getSelectionPath ().getLastPathComponent ();
364         selectAction (action);
365         if (liShortcuts.getModel ().getSize () > 0)
366             liShortcuts.setSelectedIndex (0);
367     }
368     
369     void selectAction (Object JavaDoc action) {
370         if (action == null || action instanceof String JavaDoc) {
371             liShortcuts.setModel (new DefaultListModel JavaDoc ());
372             bAdd.setEnabled (false);
373             bRemove.setEnabled (false);
374             return;
375         }
376         bAdd.setEnabled (true);
377         bRemove.setEnabled (false);
378         final String JavaDoc[] shortcuts = getModel ().getShortcuts ((ActionImpl) action);
379         liShortcuts.setModel (new AbstractListModel JavaDoc () {
380             public int getSize () {
381                 return shortcuts.length;
382             }
383             public Object JavaDoc getElementAt (int i) {
384                 return shortcuts [i];
385             }
386         });
387     }
388 }
389
Popular Tags