KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.awt.Dialog JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24 import javax.swing.*;
25 import java.awt.event.*;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import javax.swing.text.JTextComponent JavaDoc;
32 import org.openide.DialogDisplayer;
33 import org.openide.NotifyDescriptor;
34 import org.openide.util.NbBundle;
35
36
37 /** The support for creating macros.
38  *
39  * @author Petr Nejedly
40  * @version 1.0
41  */

42 public class MacroDialogSupport implements ActionListener {
43
44     JButton okButton;
45     JButton cancelButton;
46
47     MacroSavePanel panel;
48     Dialog JavaDoc macroDialog;
49     Class JavaDoc kitClass;
50     
51     /** Creates new MacroDialogSupport */
52     public MacroDialogSupport( Class JavaDoc kitClass ) {
53         this.kitClass = kitClass;
54         panel = new MacroSavePanel(kitClass);
55         ResourceBundle JavaDoc bundle = NbBundle.getBundle(MacroDialogSupport.class);
56         okButton = new JButton(bundle.getString("MDS_ok")); // NOI18N
57
cancelButton = new JButton(bundle.getString("MDS_cancel")); // NOI18N
58
okButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MDS_ok")); // NOI18N
59
cancelButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MDS_cancel")); // NOI18N
60
}
61
62     public void setBody( String JavaDoc body ) {
63         panel.setBody( body );
64     }
65     
66     public void showMacroDialog() {
67         macroDialog = DialogSupport.createDialog(
68                 NbBundle.getBundle(MacroDialogSupport.class).getString("MDS_title"), // NOI18N
69
panel, true, new JButton[] { okButton, cancelButton }, false, 0, 1, this );
70
71         macroDialog.pack();
72         panel.popupNotify();
73         macroDialog.requestFocus();
74         macroDialog.show();
75     }
76     
77     private List JavaDoc getKBList(){
78         Settings.KitAndValue[] kav = Settings.getValueHierarchy(kitClass, SettingsNames.KEY_BINDING_LIST);
79         List JavaDoc kbList = null;
80         for (int i = 0; i < kav.length; i++) {
81             if (kav[i].kitClass == kitClass) {
82                 kbList = (List JavaDoc)kav[i].value;
83             }
84         }
85         if (kbList == null) {
86             kbList = new ArrayList JavaDoc();
87         }
88         
89         // must convert all members to serializable MultiKeyBinding
90
int cnt = kbList.size();
91         for (int i = 0; i < cnt; i++) {
92             Object JavaDoc o = kbList.get(i);
93             if (!(o instanceof MultiKeyBinding) && o != null) {
94                 JTextComponent.KeyBinding JavaDoc b = (JTextComponent.KeyBinding JavaDoc)o;
95                 kbList.set(i, new MultiKeyBinding(b.key, b.actionName));
96             }
97         }
98         return new ArrayList JavaDoc( kbList );
99     }
100
101     private void saveMacro(boolean overwriting){
102         Map JavaDoc macroMap = (Map JavaDoc)Settings.getValue( kitClass, SettingsNames.MACRO_MAP);
103         Map JavaDoc newMap = new HashMap JavaDoc( macroMap );
104         newMap.put( panel.getName(), panel.getBody() );
105         Settings.setValue( kitClass, SettingsNames.MACRO_MAP, newMap );
106         List JavaDoc listBindings = panel.getKeySequences();
107
108           // insert listBindings into keybindings
109
List JavaDoc keybindings = getKBList();
110         
111         if (overwriting) {
112             // overwriting existing macro. Removing all previously attached keybindings.
113
List JavaDoc removed = new ArrayList JavaDoc();
114             String JavaDoc macroName = BaseKit.macroActionPrefix+panel.getName();
115             for (int i=0; i<keybindings.size(); i++){
116                 MultiKeyBinding multiKey = (MultiKeyBinding)keybindings.get(i);
117                 if (multiKey.actionName!=null && multiKey.actionName.equals(macroName)){
118                     removed.add(multiKey);
119                 }
120             }
121             for (int i=0; i<removed.size(); i++){
122                 keybindings.remove(removed.get(i));
123             }
124         }
125         
126         if (listBindings.size() > 0)
127         {
128             String JavaDoc actionName = new String JavaDoc(BaseKit.macroActionPrefix + panel.getName());
129             for (int i = 0; i < listBindings.size(); i++)
130             {
131                 KeyStroke[] keyStrokes = (KeyStroke[])listBindings.get(i);
132                 MultiKeyBinding multiKey = new MultiKeyBinding(keyStrokes, actionName);
133                 keybindings.add(multiKey);
134             }
135         }
136         // set new KEY_BINDING_LIST
137
Settings.setValue( kitClass, SettingsNames.KEY_BINDING_LIST, keybindings);
138     }
139     
140     protected int showConfirmDialog(String JavaDoc macroName){
141         return JOptionPane.showConfirmDialog(panel,
142                         MessageFormat.format(NbBundle.getBundle(MacroDialogSupport.class).getString("MDS_Overwrite"), //NOI18N
143
new Object JavaDoc[] {panel.getName()}),
144                         NbBundle.getBundle(MacroDialogSupport.class).getString("MDS_Warning"), // NOI18N
145
JOptionPane.YES_NO_CANCEL_OPTION,
146                         JOptionPane.WARNING_MESSAGE);
147     }
148     
149     public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt ) {
150         Object JavaDoc source = evt.getSource();
151         if( source == okButton ) {
152             if (panel.getName() == null || panel.getName().length() == 0 ||
153                 panel.getName().trim().length() == 0
154             ) {
155                 DialogDisplayer.getDefault ().notify (
156                     new NotifyDescriptor.Message (
157                         NbBundle.getBundle(MacroDialogSupport.class).getString("MDS_Empty_Name"), //NOI18N
158
NotifyDescriptor.ERROR_MESSAGE
159                     )
160                 );
161                 
162                 panel.nameField.requestFocusInWindow();
163                 return;
164             }
165             Map JavaDoc macroMap = (Map JavaDoc)Settings.getValue( kitClass, SettingsNames.MACRO_MAP);
166             
167             if (!macroMap.containsKey(panel.getName())){
168                 saveMacro(false);
169             }else{
170                 int retVal = showConfirmDialog(panel.getName());
171                 if (retVal == JOptionPane.CANCEL_OPTION || retVal == JOptionPane.CLOSED_OPTION) return;
172                 if (retVal == JOptionPane.OK_OPTION) saveMacro(true);
173             }
174         }
175         macroDialog.setVisible( false );
176         macroDialog.dispose();
177     }
178     
179 }
180
Popular Tags