KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > MacroSavePanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor;
21
22
23 import java.awt.Component JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Insets JavaDoc;
26 import java.awt.Dialog JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.awt.event.ActionEvent JavaDoc;
29 import java.beans.PropertyChangeEvent JavaDoc;
30 import java.beans.PropertyChangeSupport JavaDoc;
31 import java.beans.PropertyChangeListener JavaDoc;
32 import java.text.MessageFormat JavaDoc;
33
34 import javax.swing.event.DocumentListener JavaDoc;
35 import javax.swing.event.DocumentEvent JavaDoc;
36 import javax.swing.JLabel JavaDoc;
37 import javax.swing.JList JavaDoc;
38 import javax.swing.JButton JavaDoc;
39 import javax.swing.JDialog JavaDoc;
40 import javax.swing.ListCellRenderer JavaDoc;
41 import javax.swing.KeyStroke JavaDoc;
42 import javax.swing.border.*;
43 import java.util.*;
44 import org.openide.util.NbBundle;
45
46 /** The component for displaying and editing just recorded macro.
47  * It allows you to define a name for the macro and bound keystrokes to it.
48  *
49  * @author Petr Nejedly
50  * @version 1.0
51  */

52 public class MacroSavePanel extends javax.swing.JPanel JavaDoc {
53     
54     private final ResourceBundle bundle = NbBundle.getBundle(BaseKit.class);
55     private Vector bindings = new Vector();
56     private Class JavaDoc kitClass;
57     
58     /** Creates new form SaveMacroPanel */
59     public MacroSavePanel( Class JavaDoc kitClass ) {
60         this.kitClass = kitClass;
61         initComponents ();
62         
63         nameLabel.setDisplayedMnemonic(bundle.getString("MSP_Name_Mnemonic").charAt(0)); // NOI18N
64
macroLabel.setDisplayedMnemonic(bundle.getString("MSP_Macro_Mnemonic").charAt(0)); // NOI18N
65
bindingLabel.setDisplayedMnemonic(bundle.getString("MSP_Keys_Mnemonic").charAt(0)); // NOI18N
66
nameField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MSP_Name")); // NOI18N
67
macroField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MSP_Macro")); // NOI18N
68
bindingList.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MSP_Keys")); // NOI18N
69
getAccessibleContext().setAccessibleName(bundle.getString("MDS_title")); // NOI18N
70
getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MSP")); // NOI18N
71

72         // temporary loss of function
73
setMaximumSize( new Dimension JavaDoc( 400, 200 ) );
74     }
75
76     public Dimension JavaDoc getPreferredSize() {
77         Dimension JavaDoc pref = super.getPreferredSize();
78         Dimension JavaDoc max = getMaximumSize();
79         if( pref.width > max.width ) pref.width = max.width;
80         if( pref.height > max.height ) pref.height = max.height;
81     return pref;
82     }
83     
84     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
85
private void initComponents() {
86         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
87
88         macroPanel = new javax.swing.JPanel JavaDoc();
89         nameLabel = new javax.swing.JLabel JavaDoc();
90         macroLabel = new javax.swing.JLabel JavaDoc();
91         nameField = new javax.swing.JTextField JavaDoc();
92         macroField = new javax.swing.JTextField JavaDoc();
93         bindingPanel = new javax.swing.JPanel JavaDoc();
94         bindingLabel = new javax.swing.JLabel JavaDoc();
95         bindingScrollPane = new javax.swing.JScrollPane JavaDoc();
96         bindingList = new javax.swing.JList JavaDoc();
97         addButton = new javax.swing.JButton JavaDoc();
98         removeButton = new javax.swing.JButton JavaDoc();
99
100         setLayout(new java.awt.GridBagLayout JavaDoc());
101
102         setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 11, 11));
103         macroPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
104
105         nameLabel.setLabelFor(nameField);
106         nameLabel.setText(bundle.getString( "MSP_Name"));
107         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
108         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
109         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
110         macroPanel.add(nameLabel, gridBagConstraints);
111
112         macroLabel.setLabelFor(macroField);
113         macroLabel.setText(bundle.getString( "MSP_Macro"));
114         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
115         gridBagConstraints.gridx = 0;
116         gridBagConstraints.gridy = 1;
117         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
118         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 12);
119         macroPanel.add(macroLabel, gridBagConstraints);
120
121         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
122         gridBagConstraints.gridx = 1;
123         gridBagConstraints.gridy = 0;
124         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
125         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
126         gridBagConstraints.weightx = 1.0;
127         macroPanel.add(nameField, gridBagConstraints);
128
129         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
130         gridBagConstraints.gridx = 1;
131         gridBagConstraints.gridy = 1;
132         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
133         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
134         gridBagConstraints.weightx = 1.0;
135         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 0);
136         macroPanel.add(macroField, gridBagConstraints);
137
138         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
139         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
140         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
141         add(macroPanel, gridBagConstraints);
142
143         bindingPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
144
145         bindingLabel.setLabelFor(bindingList);
146         bindingLabel.setText(bundle.getString("MSP_Keys"));
147         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
148         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
149         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 2, 0);
150         bindingPanel.add(bindingLabel, gridBagConstraints);
151
152         bindingList.setCellRenderer(new KeySequenceCellRenderer());
153         bindingScrollPane.setViewportView(bindingList);
154
155         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
156         gridBagConstraints.gridx = 0;
157         gridBagConstraints.gridy = 1;
158         gridBagConstraints.gridheight = 3;
159         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
160         gridBagConstraints.weightx = 1.0;
161         gridBagConstraints.weighty = 1.0;
162         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
163         bindingPanel.add(bindingScrollPane, gridBagConstraints);
164
165         addButton.setMnemonic(bundle.getString("MSP_Add_Mnemonic").charAt(0));
166         addButton.setText(bundle.getString("MSP_Add"));
167         addButton.setToolTipText(bundle.getString("MSP_AddToolTip"));
168         addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
169             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
170                 addBindingActionPerformed(evt);
171             }
172         });
173
174         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
175         gridBagConstraints.gridx = 1;
176         gridBagConstraints.gridy = 1;
177         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
178         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
179         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
180         bindingPanel.add(addButton, gridBagConstraints);
181
182         removeButton.setMnemonic(bundle.getString("MSP_Remove_Mnemonic").charAt(0));
183         removeButton.setText(bundle.getString("MSP_Remove"));
184         removeButton.setToolTipText(bundle.getString( "MSP_RemoveToolTip"));
185         removeButton.setEnabled(false);
186         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
187             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
188                 removeBindingActionPerformed(evt);
189             }
190         });
191
192         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
193         gridBagConstraints.gridx = 1;
194         gridBagConstraints.gridy = 2;
195         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
196         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
197         bindingPanel.add(removeButton, gridBagConstraints);
198
199         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
200         gridBagConstraints.gridx = 0;
201         gridBagConstraints.gridy = 1;
202         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
203         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
204         gridBagConstraints.weightx = 1.0;
205         gridBagConstraints.weighty = 1.0;
206         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
207         add(bindingPanel, gridBagConstraints);
208
209     }// </editor-fold>//GEN-END:initComponents
210

211     private void removeBindingActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeBindingActionPerformed
212
int index = bindingList.getSelectedIndex();
213         if( index >= 0 ) {
214             bindings.remove(index);
215             bindingList.setListData(bindings);
216         }
217         if (bindingList.getModel().getSize() <= 0)
218             removeButton.setEnabled(false);
219         else
220             bindingList.setSelectedIndex(0);
221     }//GEN-LAST:event_removeBindingActionPerformed
222

223     private void addBindingActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addBindingActionPerformed
224
KeyStroke JavaDoc[] newKeyStrokes = new KeySequenceRequester().getKeySequence();
225         
226         if (newKeyStrokes != null)
227         {
228             bindings.add(newKeyStrokes);
229             bindingList.setListData(bindings);
230             bindingList.setSelectedIndex(0);
231             removeButton.setEnabled(true);
232         }
233     }//GEN-LAST:event_addBindingActionPerformed
234

235     public String JavaDoc getName() {
236         return nameField.getText();
237     }
238
239     public void setName( String JavaDoc name ) {
240         nameField.setText( name );
241     }
242
243     public String JavaDoc getBody() {
244         return macroField.getText();
245     }
246
247     public void setBody( String JavaDoc body ) {
248         macroField.setText( body );
249     }
250
251     /** @return List of KeyStroke[] */
252     public List JavaDoc getKeySequences() {
253         return new ArrayList( bindings );
254     }
255
256     /** @param sequences List of KeyStroke[] bounds to this macro */
257     public void setKeySequences( List JavaDoc sequences ) {
258         bindings = new Vector( sequences );
259         bindingList.setListData( bindings );
260     }
261
262     // Variables declaration - do not modify//GEN-BEGIN:variables
263
public javax.swing.JButton JavaDoc addButton;
264     public javax.swing.JLabel JavaDoc bindingLabel;
265     public javax.swing.JList JavaDoc bindingList;
266     public javax.swing.JPanel JavaDoc bindingPanel;
267     public javax.swing.JScrollPane JavaDoc bindingScrollPane;
268     public javax.swing.JTextField JavaDoc macroField;
269     public javax.swing.JLabel JavaDoc macroLabel;
270     public javax.swing.JPanel JavaDoc macroPanel;
271     public javax.swing.JTextField JavaDoc nameField;
272     public javax.swing.JLabel JavaDoc nameLabel;
273     public javax.swing.JButton JavaDoc removeButton;
274     // End of variables declaration//GEN-END:variables
275

276     
277     public void popupNotify() {
278         nameField.requestFocus();
279     }
280
281     
282     private static class KeySequenceCellRenderer extends JLabel JavaDoc implements ListCellRenderer JavaDoc {
283         public KeySequenceCellRenderer() {
284             setOpaque(true);
285         }
286
287         public Component JavaDoc getListCellRendererComponent( JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
288             setText( Utilities.keySequenceToString( (KeyStroke JavaDoc[])value ) );
289             setBackground(isSelected ? list.getSelectionBackground() : list.getBackground() );
290             setForeground(isSelected ? list.getSelectionForeground() : list.getForeground() );
291             return this;
292         }
293     }
294     
295
296     /**
297      * Encapsulation for components of dialog asking for new KeySequence
298      */

299     class KeySequenceRequester {
300
301         KeySequenceInputPanel panel;
302         Dialog JavaDoc dial;
303
304         JButton JavaDoc[] buttons = { new JButton JavaDoc(bundle.getString("MSP_ok")), // NOI18N
305
new JButton JavaDoc(bundle.getString("MSP_clear")), // NOI18N
306
new JButton JavaDoc(bundle.getString("MSP_cancel"))}; // NOI18N
307

308         KeyStroke JavaDoc[] retVal = null;
309
310
311         KeySequenceRequester() {
312             ((JButton JavaDoc)buttons[0]).getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MSP_ok")); // NOI18N
313
((JButton JavaDoc)buttons[1]).getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MSP_clear")); // NOI18N
314
((JButton JavaDoc)buttons[2]).getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MSP_cancel")); // NOI18N
315
((JButton JavaDoc)buttons[1]).setMnemonic(bundle.getString("MSP_clear_Mnemonic").charAt (0)); // NOI18N
316
((JButton JavaDoc)buttons[0]).setEnabled( false ); // default initial state
317

318             // Prepare KeySequence input dialog
319
panel = new KeySequenceInputPanel();
320             panel.addPropertyChangeListener( new PropertyChangeListener JavaDoc() {
321                                                  public void propertyChange( PropertyChangeEvent JavaDoc evt ) {
322                                                      if( KeySequenceInputPanel.PROP_KEYSEQUENCE != evt.getPropertyName() ) return;
323                                                      KeyStroke JavaDoc[] seq = panel.getKeySequence();
324                                                      String JavaDoc warn = isAlreadyBounded( seq );
325                                                      if (warn == null)
326                                                         warn = getCollisionString( seq );
327                                                      ((JButton JavaDoc)buttons[0]).setEnabled( seq.length > 0 && warn == null );
328                                                      panel.setInfoText( warn == null ? "" : warn ); // NOI18N
329
}
330                                              } );
331
332             dial = DialogSupport.createDialog(
333                 bundle.getString("MSP_AddTitle"), // NOI18N
334
panel, true, buttons, false, -1, 2, new ActionListener JavaDoc(){
335                                             public void actionPerformed( ActionEvent JavaDoc evt ) {
336                                                 if( evt.getSource() == buttons[1] ) { // Clear pressed
337
panel.clear(); // Clear entered KeyStrokes, start again
338
panel.requestFocus(); // Make user imediately able to enter new strokes
339
} else if( evt.getSource() == buttons[0] ) { // OK pressed
340
retVal = panel.getKeySequence();
341                                                     dial.dispose(); // Done
342
} else if( evt.getSource() == buttons[2] ) { // OK pressed
343
retVal = null;
344                                                     dial.dispose(); // Done
345
}
346                                             }
347                                         });
348
349         }
350
351         KeyStroke JavaDoc[] getKeySequence() {
352             dial.pack();
353             panel.requestFocus();
354             dial.show();
355             return retVal;
356         }
357
358         /** Check whether this KeyStroke is already bounded to this macro or not.
359          * Disallow to duplicate the KeyStroke.
360          */

361         String JavaDoc isAlreadyBounded( KeyStroke JavaDoc[] seq ) {
362             if( seq.length == 0 ) return null; // NOI18N not valid sequence, but don't alert user
363

364             Iterator it = bindings.iterator();
365             while( it.hasNext() ) {
366                 if( isOverlapingSequence( (KeyStroke JavaDoc[])it.next(), seq ) ) {
367                     return bundle.getString( "MSP_Collision" ); // NOI18N
368
}
369             }
370             return null; // no colliding sequence
371
}
372
373         String JavaDoc getCollisionString( KeyStroke JavaDoc[] seq ) {
374             if( seq.length == 0 ) return null; // NOI18N not valid sequence, but don't alert user
375

376             Settings.KitAndValue[] kv = Settings.getValueHierarchy( kitClass, SettingsNames.KEY_BINDING_LIST );
377             for (int i = 0; i < kv.length; i++)
378             {
379                 Iterator iter = ((List JavaDoc)kv[i].value).iterator();
380                 while( iter.hasNext() ) {
381                     MultiKeyBinding b = (MultiKeyBinding)iter.next();
382                     KeyStroke JavaDoc[] ks = b.keys;
383                     if (ks == null && b.key != null)
384                     {
385                         ks = new KeyStroke JavaDoc[1];
386                         ks[0] = b.key;
387                     }
388                     if( ks != null && isOverlapingSequence( ks, seq ) ) {
389                         Object JavaDoc[] values = { Utilities.keySequenceToString( ks ), b.actionName };
390                         return MessageFormat.format( bundle.getString( "MSP_FMT_Collision" ), values ); // NOI18N
391
}
392                 }
393             }
394             return null; // no colliding sequence
395
}
396         
397         private boolean isOverlapingSequence( KeyStroke JavaDoc[] s1, KeyStroke JavaDoc[] s2 ) {
398             int l = Math.min( s1.length, s2.length );
399             if (l == 0)
400                 return false;
401             while( l-- > 0 ) if( !s1[l].equals( s2[l] ) ) return false;
402             return true;
403         }
404     }
405     
406 }
407
Popular Tags