1 19 20 package org.netbeans.editor; 21 22 import java.awt.Dialog ; 23 import java.util.ResourceBundle ; 24 import javax.swing.*; 25 import java.awt.event.*; 26 import java.text.MessageFormat ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.List ; 30 import java.util.ArrayList ; 31 import javax.swing.text.JTextComponent ; 32 import org.openide.DialogDisplayer; 33 import org.openide.NotifyDescriptor; 34 import org.openide.util.NbBundle; 35 36 37 42 public class MacroDialogSupport implements ActionListener { 43 44 JButton okButton; 45 JButton cancelButton; 46 47 MacroSavePanel panel; 48 Dialog macroDialog; 49 Class kitClass; 50 51 52 public MacroDialogSupport( Class kitClass ) { 53 this.kitClass = kitClass; 54 panel = new MacroSavePanel(kitClass); 55 ResourceBundle bundle = NbBundle.getBundle(MacroDialogSupport.class); 56 okButton = new JButton(bundle.getString("MDS_ok")); cancelButton = new JButton(bundle.getString("MDS_cancel")); okButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MDS_ok")); cancelButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MDS_cancel")); } 61 62 public void setBody( String body ) { 63 panel.setBody( body ); 64 } 65 66 public void showMacroDialog() { 67 macroDialog = DialogSupport.createDialog( 68 NbBundle.getBundle(MacroDialogSupport.class).getString("MDS_title"), 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 getKBList(){ 78 Settings.KitAndValue[] kav = Settings.getValueHierarchy(kitClass, SettingsNames.KEY_BINDING_LIST); 79 List kbList = null; 80 for (int i = 0; i < kav.length; i++) { 81 if (kav[i].kitClass == kitClass) { 82 kbList = (List )kav[i].value; 83 } 84 } 85 if (kbList == null) { 86 kbList = new ArrayList (); 87 } 88 89 int cnt = kbList.size(); 91 for (int i = 0; i < cnt; i++) { 92 Object o = kbList.get(i); 93 if (!(o instanceof MultiKeyBinding) && o != null) { 94 JTextComponent.KeyBinding b = (JTextComponent.KeyBinding )o; 95 kbList.set(i, new MultiKeyBinding(b.key, b.actionName)); 96 } 97 } 98 return new ArrayList ( kbList ); 99 } 100 101 private void saveMacro(boolean overwriting){ 102 Map macroMap = (Map )Settings.getValue( kitClass, SettingsNames.MACRO_MAP); 103 Map newMap = new HashMap ( macroMap ); 104 newMap.put( panel.getName(), panel.getBody() ); 105 Settings.setValue( kitClass, SettingsNames.MACRO_MAP, newMap ); 106 List listBindings = panel.getKeySequences(); 107 108 List keybindings = getKBList(); 110 111 if (overwriting) { 112 List removed = new ArrayList (); 114 String 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 actionName = new String (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 Settings.setValue( kitClass, SettingsNames.KEY_BINDING_LIST, keybindings); 138 } 139 140 protected int showConfirmDialog(String macroName){ 141 return JOptionPane.showConfirmDialog(panel, 142 MessageFormat.format(NbBundle.getBundle(MacroDialogSupport.class).getString("MDS_Overwrite"), new Object [] {panel.getName()}), 144 NbBundle.getBundle(MacroDialogSupport.class).getString("MDS_Warning"), JOptionPane.YES_NO_CANCEL_OPTION, 146 JOptionPane.WARNING_MESSAGE); 147 } 148 149 public void actionPerformed(java.awt.event.ActionEvent evt ) { 150 Object 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"), NotifyDescriptor.ERROR_MESSAGE 159 ) 160 ); 161 162 panel.nameField.requestFocusInWindow(); 163 return; 164 } 165 Map macroMap = (Map )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 |