KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > macros > MacrosPanel


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.modules.options.macros;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import javax.swing.AbstractButton JavaDoc;
27 import javax.swing.JEditorPane JavaDoc;
28 import javax.swing.JLabel JavaDoc;
29 import javax.swing.JPanel JavaDoc;
30 import javax.swing.ListSelectionModel JavaDoc;
31 import javax.swing.SwingUtilities JavaDoc;
32 import javax.swing.event.ListSelectionEvent JavaDoc;
33 import javax.swing.event.ListSelectionListener JavaDoc;
34 import javax.swing.table.DefaultTableModel JavaDoc;
35 import org.netbeans.modules.options.keymap.KeymapViewModel;
36 import org.netbeans.modules.options.keymap.ShortcutsDialog;
37 import org.netbeans.spi.options.OptionsPanelController;
38 import org.openide.DialogDisplayer;
39 import org.openide.NotifyDescriptor;
40 import org.openide.NotifyDescriptor.InputLine;
41 import org.openide.awt.Mnemonics;
42 import org.openide.util.Lookup;
43 import org.openide.util.NbBundle;
44
45
46 /**
47  * Implementation of one panel in Options Dialog.
48  *
49  * @author Jan Jancura
50  */

51 public class MacrosPanel extends JPanel JavaDoc implements ActionListener JavaDoc,
52 ListSelectionListener JavaDoc {
53     
54     private MacrosModel model;
55     private boolean listen = true;
56     private String JavaDoc currentMacro;
57
58     
59     /**
60      * Creates new form MacrosPanel.
61      */

62     public MacrosPanel () {
63         initComponents ();
64         
65         // 1) init components
66
tMacros.getAccessibleContext ().setAccessibleName (loc ("AN_Macros_Table"));
67         tMacros.getAccessibleContext ().setAccessibleDescription (loc ("AD_Macros_Table"));
68         epMacroCode.getAccessibleContext ().setAccessibleName (loc ("AN_Macro"));
69         epMacroCode.getAccessibleContext ().setAccessibleDescription (loc ("AD_Macro"));
70         bRemove.setEnabled (false);
71         bSetShortcut.setEnabled (false);
72         loc (bNew, "New_Macro");
73         loc (bRemove, "Remove_Macro");
74         loc (bSetShortcut, "Shortcut");
75         bNew.addActionListener (this);
76         bRemove.addActionListener (this);
77         bSetShortcut.addActionListener (this);
78         tMacros.getSelectionModel ().setSelectionMode
79             (ListSelectionModel.SINGLE_SELECTION);
80         tMacros.getSelectionModel ().addListSelectionListener (this);
81         tMacros.getTableHeader ().setReorderingAllowed (false);
82         epMacroCode.setEditorKit (JEditorPane.createEditorKitForContentType("text/plain"));
83         loc (lMacros, "Macro_List");
84         lMacros.setLabelFor (tMacros);
85         loc (lMacroCode, "Macro_Code");
86         lMacroCode.setLabelFor (epMacroCode);
87     }
88     
89     
90     // UI form .................................................................
91

92     /** This method is called from within the constructor to
93      * initialize the form.
94      * WARNING: Do NOT modify this code. The content of this method is
95      * always regenerated by the Form Editor.
96      */

97     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
98
private void initComponents() {
99         lMacros = new javax.swing.JLabel JavaDoc();
100         spMacros = new javax.swing.JScrollPane JavaDoc();
101         tMacros = new javax.swing.JTable JavaDoc();
102         bNew = new javax.swing.JButton JavaDoc();
103         bSetShortcut = new javax.swing.JButton JavaDoc();
104         bRemove = new javax.swing.JButton JavaDoc();
105         lMacroCode = new javax.swing.JLabel JavaDoc();
106         sMacroCode = new javax.swing.JScrollPane JavaDoc();
107         epMacroCode = new javax.swing.JEditorPane JavaDoc();
108
109         lMacros.setText("Macros:");
110
111         tMacros.setModel(new javax.swing.table.DefaultTableModel JavaDoc(
112             new Object JavaDoc [][] {
113                 {null, null, null, null},
114                 {null, null, null, null},
115                 {null, null, null, null},
116                 {null, null, null, null}
117             },
118             new String JavaDoc [] {
119                 "Title 1", "Title 2", "Title 3", "Title 4"
120             }
121         ));
122         spMacros.setViewportView(tMacros);
123
124         bNew.setText("New");
125
126         bSetShortcut.setText("Set Shortcut...");
127
128         bRemove.setText("Remove");
129
130         lMacroCode.setText("Macro Code:");
131
132         sMacroCode.setViewportView(epMacroCode);
133
134         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
135         this.setLayout(layout);
136         layout.setHorizontalGroup(
137             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
138             .add(layout.createSequentialGroup()
139                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
140                     .add(lMacros)
141                     .add(lMacroCode)
142                     .add(layout.createSequentialGroup()
143                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
144                             .add(org.jdesktop.layout.GroupLayout.LEADING, sMacroCode, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
145                             .add(org.jdesktop.layout.GroupLayout.LEADING, spMacros, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE))
146                         .add(18, 18, 18)
147                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
148                             .add(bNew, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
149                             .add(bSetShortcut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
150                             .add(org.jdesktop.layout.GroupLayout.LEADING, bRemove, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
151                         .add(13, 13, 13)))
152                 .addContainerGap())
153         );
154         layout.setVerticalGroup(
155             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
156             .add(layout.createSequentialGroup()
157                 .add(lMacros)
158                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
159                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
160                     .add(layout.createSequentialGroup()
161                         .add(bNew)
162                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
163                         .add(bSetShortcut)
164                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
165                         .add(bRemove))
166                     .add(spMacros, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 113, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
167                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
168                 .add(lMacroCode)
169                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
170                 .add(sMacroCode, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE))
171         );
172     }// </editor-fold>//GEN-END:initComponents
173

174     
175     // Variables declaration - do not modify//GEN-BEGIN:variables
176
private javax.swing.JButton JavaDoc bNew;
177     private javax.swing.JButton JavaDoc bRemove;
178     private javax.swing.JButton JavaDoc bSetShortcut;
179     private javax.swing.JEditorPane JavaDoc epMacroCode;
180     private javax.swing.JLabel JavaDoc lMacroCode;
181     private javax.swing.JLabel JavaDoc lMacros;
182     private javax.swing.JScrollPane JavaDoc sMacroCode;
183     private javax.swing.JScrollPane JavaDoc spMacros;
184     private javax.swing.JTable JavaDoc tMacros;
185     // End of variables declaration//GEN-END:variables
186

187     
188     private static String JavaDoc loc (String JavaDoc key) {
189         return NbBundle.getMessage (MacrosPanel.class, key);
190     }
191     
192     private static void loc (Component JavaDoc c, String JavaDoc key) {
193         if (!(c instanceof JLabel JavaDoc)) {
194             c.getAccessibleContext ().setAccessibleName (loc ("AN_" + key));
195             c.getAccessibleContext ().setAccessibleDescription (loc ("AD_" + key));
196         }
197         if (c instanceof AbstractButton JavaDoc) {
198             Mnemonics.setLocalizedText (
199                 (AbstractButton JavaDoc) c,
200                 loc ("CTL_" + key)
201             );
202         } else {
203             Mnemonics.setLocalizedText (
204                 (JLabel JavaDoc) c,
205                 loc ("CTL_" + key)
206             );
207         }
208     }
209     
210     public void update () {
211         model = new MacrosModel (lookup);
212         listen = false;
213         DefaultTableModel JavaDoc tableModel = model.getShortcutsTableModel ();
214         currentMacro = null;
215         tableModel.setColumnIdentifiers (new String JavaDoc[] {
216             loc ("Macro_Name_Title"),
217             loc ("Macro_Code_Title")
218         });
219         tMacros.setModel (tableModel);
220         listen = true;
221         if (model.getMacroNames().size () > 0)
222             tMacros.getSelectionModel ().setSelectionInterval (0, 0);
223     }
224     
225     public void applyChanges () {
226         saveCurrentMacro ();
227         if (model != null)
228             model.applyChanges ();
229     }
230     
231     public void cancel () {
232         if (model != null)
233             model.cancel ();
234     }
235     
236     public boolean dataValid () {
237         return true;
238     }
239     
240     public boolean isChanged () {
241         if (model == null) return false;
242         return model.isChanged ();
243     }
244     
245     private Lookup lookup;
246     
247     void setLookup (Lookup lookup) {
248         this.lookup = lookup;
249     }
250     
251     
252     // ActionListener ..........................................................
253

254     public void actionPerformed (ActionEvent JavaDoc e) {
255         if (!listen) return;
256         if (e.getSource () == bNew) {
257             InputLine descriptor = new InputLine (
258                 loc ("CTL_Enter_macro_name"),
259                 loc ("CTL_New_macro_dialog_title")
260             );
261             if (DialogDisplayer.getDefault ().notify (descriptor) ==
262                 InputLine.OK_OPTION
263             ) {
264                 String JavaDoc macroName = descriptor.getInputText ().trim ();
265                 if (macroName.length () == 0) {
266                     DialogDisplayer.getDefault ().notify (
267                         new NotifyDescriptor.Message (
268                             loc ("CTL_Empty_Macro_Name"),
269                             NotifyDescriptor.ERROR_MESSAGE
270                         )
271                     );
272                 } else {
273                     Iterator JavaDoc it = model.getMacroNames ().iterator ();
274                     boolean duplicate = false;
275                     while (it.hasNext ()) {
276                         String JavaDoc name = (String JavaDoc) it.next ();
277                         if (macroName.equals (name)) {
278                             DialogDisplayer.getDefault ().notify (
279                                 new NotifyDescriptor.Message (
280                                     loc ("CTL_Duplicate_Macro_Name"),
281                                     NotifyDescriptor.ERROR_MESSAGE
282                                 )
283                             );
284                             duplicate = true;
285                             break;
286                         }
287                     }
288                     if (!duplicate) {
289                         listen = false;
290                         model.addMacro (macroName, "");
291                         listen = true;
292                         tMacros.getSelectionModel ().setSelectionInterval (0, 0);
293                         spMacros.getVerticalScrollBar ().setValue (0);
294                     }
295                 }
296             }
297             SwingUtilities.invokeLater (new Runnable JavaDoc () {
298                 public void run () {
299                     epMacroCode.requestFocus ();
300                 }
301             });
302         } else
303         if (e.getSource () == bRemove) {
304             int index = tMacros.getSelectedRow ();
305             listen = false;
306             model.removeMacro (index);
307             listen = true;
308             currentMacro = null;
309             if (index < tMacros.getModel ().getRowCount ())
310                 tMacros.getSelectionModel ().setSelectionInterval
311                     (index, index);
312             else
313             if (tMacros.getModel ().getRowCount () > 0)
314                 tMacros.getSelectionModel ().setSelectionInterval (
315                     tMacros.getModel ().getRowCount () - 1,
316                     tMacros.getModel ().getRowCount () - 1
317                 );
318             else {
319                 bRemove.setEnabled (false);
320                 bSetShortcut.setEnabled (false);
321             }
322         } else
323         if (e.getSource () == bSetShortcut) {
324             int index = tMacros.getSelectedRow ();
325             String JavaDoc shortcut = ShortcutsDialog.getShortcut (
326                 (KeymapViewModel) lookup.lookup (KeymapViewModel.class)
327             );
328             saveCurrentMacro ();
329             if (shortcut != null)
330                 model.setShortcut (index, shortcut);
331             SwingUtilities.invokeLater (new Runnable JavaDoc () {
332                 public void run () {
333                     epMacroCode.requestFocus ();
334                 }
335             });
336         }
337     }
338     
339     public void valueChanged (ListSelectionEvent JavaDoc e) {
340         int index = tMacros.getSelectedRow ();
341         if (index < 0) {
342             epMacroCode.setText ("");
343             bRemove.setEnabled (false);
344             bSetShortcut.setEnabled (false);
345             return;
346         }
347         saveCurrentMacro ();
348         
349         bRemove.setEnabled (true);
350         bSetShortcut.setEnabled (true);
351         String JavaDoc name = getCurrentMacro ();
352         String JavaDoc text = model.getMacroText (name);
353         epMacroCode.setText (text);
354         currentMacro = name;
355     }
356
357     private String JavaDoc getCurrentMacro () {
358         int index = tMacros.getSelectedRow ();
359         if (index < 0) return null;
360         return (String JavaDoc) tMacros.getModel ().getValueAt (index, 0);
361     }
362     
363     private void saveCurrentMacro () {
364         if (currentMacro == null) return;
365         if (epMacroCode.getText ().equals (model.getMacroText (currentMacro)))
366             return;
367         model.setMacroText (currentMacro, epMacroCode.getText ());
368         firePropertyChange (OptionsPanelController.PROP_CHANGED, null, null);
369     }
370 }
371
Popular Tags