001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: */
013:
014: package org.netbeans.editor;
015:
016: import java.awt.Dialog;
017: import java.awt.event.ActionListener;
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Map;
022:
023: import javax.swing.JButton;
024: import javax.swing.KeyStroke;
025: import javax.swing.text.JTextComponent;
026:
027: /**
028: * The support for creating macros.
029: *
030: * @author Petr Nejedly
031: * @version 1.0
032: */
033: public class MacroDialogSupport implements ActionListener {
034:
035: JButton okButton;
036: JButton cancelButton;
037:
038: MacroSavePanel panel;
039: Dialog macroDialog;
040: Class kitClass;
041:
042: /** Creates new MacroDialogSupport */
043: public MacroDialogSupport(Class kitClass) {
044: this .kitClass = kitClass;
045: panel = new MacroSavePanel(kitClass);
046: okButton = new JButton(LocaleSupport.getString("MDS_ok")); // NOI18N
047: cancelButton = new JButton(LocaleSupport
048: .getString("MDS_cancel")); // NOI18N
049: okButton.getAccessibleContext().setAccessibleDescription(
050: LocaleSupport.getString("ACSD_MDS_ok")); // NOI18N
051: cancelButton.getAccessibleContext().setAccessibleDescription(
052: LocaleSupport.getString("ACSD_MDS_cancel")); // NOI18N
053: }
054:
055: public void setBody(String body) {
056: panel.setBody(body);
057: }
058:
059: public void showMacroDialog() {
060: macroDialog = DialogSupport.createDialog(
061: LocaleSupport.getString("MDS_title"), // NOI18N
062: panel, true, new JButton[] { okButton, cancelButton },
063: false, 0, 1, this );
064:
065: macroDialog.pack();
066: panel.popupNotify();
067: macroDialog.requestFocus();
068: macroDialog.show();
069: }
070:
071: private List getKBList() {
072: Settings.KitAndValue[] kav = Settings.getValueHierarchy(
073: kitClass, SettingsNames.KEY_BINDING_LIST);
074: List kbList = null;
075: for (int i = 0; i < kav.length; i++) {
076: if (kav[i].kitClass == kitClass) {
077: kbList = (List) kav[i].value;
078: }
079: }
080: if (kbList == null) {
081: kbList = new ArrayList();
082: }
083:
084: // must convert all members to serializable MultiKeyBinding
085: int cnt = kbList.size();
086: for (int i = 0; i < cnt; i++) {
087: Object o = kbList.get(i);
088: if (!(o instanceof MultiKeyBinding) && o != null) {
089: JTextComponent.KeyBinding b = (JTextComponent.KeyBinding) o;
090: kbList.set(i, new MultiKeyBinding(b.key, b.actionName));
091: }
092: }
093: return new ArrayList(kbList);
094: }
095:
096: public void actionPerformed(java.awt.event.ActionEvent evt) {
097: Object source = evt.getSource();
098: if (source == okButton) {
099: Map macroMap = (Map) Settings.getValue(kitClass,
100: SettingsNames.MACRO_MAP);
101: Map newMap = new HashMap(macroMap);
102: newMap.put(panel.getName(), panel.getBody());
103: Settings
104: .setValue(kitClass, SettingsNames.MACRO_MAP, newMap);
105:
106: List listBindings = panel.getKeySequences();
107:
108: // insert listBindings into keybindings
109: if (listBindings.size() > 0) {
110: List keybindings = getKBList();
111: String actionName = new String(
112: BaseKit.macroActionPrefix + panel.getName());
113: for (int i = 0; i < listBindings.size(); i++) {
114: KeyStroke[] keyStrokes = (KeyStroke[]) listBindings
115: .get(i);
116: MultiKeyBinding multiKey = new MultiKeyBinding(
117: keyStrokes, actionName);
118: keybindings.add(multiKey);
119: }
120:
121: // set new KEY_BINDING_LIST
122: Settings.setValue(kitClass,
123: SettingsNames.KEY_BINDING_LIST, keybindings);
124: }
125: }
126: macroDialog.setVisible(false);
127: macroDialog.dispose();
128: }
129:
130: }
|