KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > codetemplates > CodeTemplatesPanel


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.codetemplates;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.FlowLayout JavaDoc;
25 import java.awt.Font JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import javax.swing.AbstractButton JavaDoc;
34 import javax.swing.JLabel JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36 import javax.swing.KeyStroke JavaDoc;
37 import javax.swing.ListSelectionModel JavaDoc;
38 import javax.swing.SwingUtilities JavaDoc;
39 import javax.swing.event.ListSelectionEvent JavaDoc;
40 import javax.swing.event.ListSelectionListener JavaDoc;
41 import javax.swing.table.TableModel JavaDoc;
42 import org.netbeans.spi.options.OptionsPanelController;
43 import org.openide.DialogDisplayer;
44 import org.openide.NotifyDescriptor;
45 import org.openide.NotifyDescriptor.InputLine;
46 import org.openide.awt.Mnemonics;
47 import org.openide.text.CloneableEditorSupport;
48 import org.openide.util.Lookup;
49 import org.openide.util.NbBundle;
50
51 /**
52  * Implementation of one panel in Options Dialog.
53  *
54  * @author Jan Jancura
55  */

56 public class CodeTemplatesPanel extends JPanel JavaDoc implements
57 ActionListener JavaDoc, ListSelectionListener JavaDoc {
58     
59     private CodeTemplatesModel model;
60     
61     /**
62      * Creates new form CodeTemplatesPanel.
63      */

64     public CodeTemplatesPanel () {
65         initComponents ();
66         
67         loc (lLanguage, "Language");
68         loc (lTemplates, "Templates");
69         loc (bNew, "New");
70         loc (bRemove, "Remove");
71         cbExpandTemplateOn.addItem (loc ("SPACE"));
72         cbExpandTemplateOn.addItem (loc ("S-SPACE"));
73         cbExpandTemplateOn.addItem (loc ("TAB"));
74         cbExpandTemplateOn.addItem (loc ("ENTER"));
75         loc (lExpandedText, "Expanded_Text");
76         loc (lExplandTemplateOn, "ExpandTemplateOn");
77         
78         bRemove.setEnabled (false);
79         JLabel JavaDoc lExpander = new JLabel JavaDoc ();
80         tTemplates.getTableHeader ().setReorderingAllowed (false);
81         tTemplates.getSelectionModel ().setSelectionMode
82             (ListSelectionModel.SINGLE_SELECTION);
83         Font JavaDoc f = tTemplates.getFont ();
84         JLabel JavaDoc lbTemplates = new JLabel JavaDoc ();
85         lbTemplates.setLabelFor (tTemplates);
86         JLabel JavaDoc lbExpandedText = new JLabel JavaDoc ();
87         lbExpandedText.setLabelFor (epExpandedText);
88     }
89     
90     
91     private static String JavaDoc loc (String JavaDoc key) {
92         return NbBundle.getMessage (CodeTemplatesPanel.class, key);
93     }
94     
95     private static void loc (Component JavaDoc c, String JavaDoc key) {
96         if (!(c instanceof JLabel JavaDoc)) {
97             c.getAccessibleContext ().setAccessibleName (loc ("AN_" + key));
98             c.getAccessibleContext ().setAccessibleDescription (loc ("AD_" + key));
99         }
100         if (c instanceof AbstractButton JavaDoc) {
101             Mnemonics.setLocalizedText (
102                 (AbstractButton JavaDoc) c,
103                 loc ("CTL_" + key)
104             );
105         } else {
106             Mnemonics.setLocalizedText (
107                 (JLabel JavaDoc) c,
108                 loc ("CTL_" + key)
109             );
110         }
111     }
112     
113     
114     // OptionsCategory.Panel ...................................................
115

116     void update () {
117         model = new CodeTemplatesModel ();
118
119         cbLanguage.removeActionListener (this);
120         bNew.removeActionListener (this);
121         bRemove.removeActionListener (this);
122         cbExpandTemplateOn.removeActionListener (this);
123         tTemplates.getSelectionModel ().removeListSelectionListener (this);
124         
125         cbLanguage.removeAllItems ();
126         List JavaDoc languages = new ArrayList JavaDoc (model.getLanguages ());
127         Collections.sort (languages);
128         Iterator JavaDoc it = languages.iterator ();
129         while (it.hasNext ()) cbLanguage.addItem (it.next ());
130         KeyStroke JavaDoc expander = model.getExpander ();
131         if (KeyStroke.getKeyStroke (KeyEvent.VK_SPACE, KeyEvent.SHIFT_MASK).equals (expander))
132             cbExpandTemplateOn.setSelectedIndex (1);
133         else
134         if (KeyStroke.getKeyStroke (KeyEvent.VK_TAB, 0).equals (expander))
135             cbExpandTemplateOn.setSelectedIndex (2);
136         else
137         if (KeyStroke.getKeyStroke (KeyEvent.VK_ENTER, 0).equals (expander))
138             cbExpandTemplateOn.setSelectedIndex (3);
139         else
140             cbExpandTemplateOn.setSelectedIndex (0);
141         
142         cbLanguage.addActionListener (this);
143         bNew.addActionListener (this);
144         bRemove.addActionListener (this);
145         cbExpandTemplateOn.addActionListener (this);
146         tTemplates.getSelectionModel ().addListSelectionListener (this);
147         cbLanguage.setSelectedItem ("Java");
148     }
149     
150     void applyChanges () {
151         if (model != null) {
152             saveCurrentTemplate ();
153             model.saveChanges ();
154         }
155     }
156     
157     void cancel () {
158     }
159     
160     boolean dataValid () {
161         return true;
162     }
163     
164     boolean isChanged () {
165         if (model == null) return false;
166         return model.isChanged ();
167     }
168     
169     // ActionListener ..........................................................
170

171     public void actionPerformed (ActionEvent JavaDoc e) {
172         if (e.getSource () == cbLanguage) {
173             saveCurrentTemplate ();
174             final String JavaDoc language = (String JavaDoc) cbLanguage.getSelectedItem ();
175             final TableModel JavaDoc tableModel = model.getTableModel (language);
176             tTemplates.setModel (tableModel);
177             SwingUtilities.invokeLater (new Runnable JavaDoc () {
178                 public void run () {
179                     epExpandedText.setEditorKit(CloneableEditorSupport.getEditorKit(model.getMimeType (language)));
180                     if (tableModel.getRowCount () > 0) {
181                         lastIndex = -1;
182                         tTemplates.getSelectionModel ().setSelectionInterval (0, 0);
183                         lastIndex = 0;
184                     } else
185                         lastIndex = -1;
186                 }
187             });
188         } else
189         if (e.getSource () == bNew) {
190             saveCurrentTemplate ();
191             InputLine descriptor = new InputLine (
192                 loc ("CTL_Enter_template_name"),
193                 loc ("CTL_New_template_dialog_title")
194             );
195             if (DialogDisplayer.getDefault ().notify (descriptor) ==
196                 InputLine.OK_OPTION
197             ) {
198                 String JavaDoc templateName = descriptor.getInputText ().trim ();
199                 if (templateName.length () == 0) {
200                     DialogDisplayer.getDefault ().notify (
201                         new NotifyDescriptor.Message (
202                             loc ("CTL_Empty_template_name"),
203                             NotifyDescriptor.ERROR_MESSAGE
204                         )
205                     );
206                 } else {
207                     TableModel JavaDoc tableModel = tTemplates.getModel ();
208                     int i, k = tableModel.getRowCount ();
209                     for (i = 0; i < k; i++) {
210                         String JavaDoc name = (String JavaDoc) tableModel.getValueAt (i, 0);
211                         if (templateName.equals (name)) {
212                             DialogDisplayer.getDefault ().notify (
213                                 new NotifyDescriptor.Message (
214                                     loc ("CTL_Duplicate_template_name"),
215                                     NotifyDescriptor.ERROR_MESSAGE
216                                 )
217                             );
218                             break;
219                         }
220                     }
221                     if (i == k) {
222                         String JavaDoc language = (String JavaDoc) cbLanguage.getSelectedItem ();
223                         lastIndex = -1;
224                         model.addRow (language, templateName, "");
225                         tTemplates.getSelectionModel ().setSelectionInterval (0, 0);
226                         spTemplates.getVerticalScrollBar ().setValue (0);
227                     }
228                 }
229             }
230             SwingUtilities.invokeLater (new Runnable JavaDoc () {
231                 public void run () {
232                     epExpandedText.requestFocus ();
233                 }
234             });
235         } else
236         if (e.getSource () == bRemove) {
237             String JavaDoc language = (String JavaDoc) cbLanguage.getSelectedItem ();
238             int index = tTemplates.getSelectedRow ();
239             model.removeRow (language, index);
240             lastIndex = -1;
241             if (index < tTemplates.getModel ().getRowCount ())
242                 tTemplates.getSelectionModel ().setSelectionInterval
243                     (index, index);
244             else
245             if (tTemplates.getModel ().getRowCount () > 0)
246                 tTemplates.getSelectionModel ().setSelectionInterval (
247                     tTemplates.getModel ().getRowCount () - 1,
248                     tTemplates.getModel ().getRowCount () - 1
249                 );
250             else
251                 bRemove.setEnabled (false);
252         } else
253         if (e.getSource () == cbExpandTemplateOn) {
254             switch (cbExpandTemplateOn.getSelectedIndex ()) {
255                 case 0:model.setExpander (KeyStroke.getKeyStroke (KeyEvent.VK_SPACE, 0));
256                     break;
257                 case 1:model.setExpander (KeyStroke.getKeyStroke (KeyEvent.VK_SPACE, KeyEvent.SHIFT_MASK));
258                     break;
259                 case 2:model.setExpander (KeyStroke.getKeyStroke (KeyEvent.VK_TAB, 0));
260                     break;
261                 case 3:model.setExpander (KeyStroke.getKeyStroke (KeyEvent.VK_ENTER, 0));
262                     break;
263             }
264         }
265     }
266     
267     public void valueChanged (ListSelectionEvent JavaDoc e) {
268         // new line in code templates table has been selected
269
int index = tTemplates.getSelectedRow ();
270         if (index < 0) {
271             epExpandedText.setText ("");
272             bRemove.setEnabled (false);
273             lastIndex = -1;
274             return;
275         }
276         saveCurrentTemplate ();
277         TableModel JavaDoc tableModel = tTemplates.getModel ();
278         bRemove.setEnabled (true);
279         String JavaDoc text = (String JavaDoc) tableModel.getValueAt (index, 1);
280         epExpandedText.setText (text);
281         lastIndex = index;
282     }
283     
284     private Lookup lookup;
285     
286     void setLookup (Lookup lookup) {
287         this.lookup = lookup;
288     }
289     
290     private int lastIndex = -1;
291     
292     void saveCurrentTemplate () {
293         TableModel JavaDoc tableModel = tTemplates.getModel ();
294         if (lastIndex < 0) return;
295         if (epExpandedText.getText ().equals (tableModel.getValueAt (lastIndex, 1)))
296             return;
297         tableModel.setValueAt (
298             epExpandedText.getText (),
299             lastIndex,
300             1
301         );
302         firePropertyChange (OptionsPanelController.PROP_CHANGED, null, null);
303     }
304     
305     
306     // UI form .................................................................
307

308     /** This method is called from within the constructor to
309      * initialize the form.
310      * WARNING: Do NOT modify this code. The content of this method is
311      * always regenerated by the Form Editor.
312      */

313     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
314
private void initComponents() {
315         lLanguage = new javax.swing.JLabel JavaDoc();
316         cbLanguage = new javax.swing.JComboBox JavaDoc();
317         lTemplates = new javax.swing.JLabel JavaDoc();
318         spTemplates = new javax.swing.JScrollPane JavaDoc();
319         tTemplates = new javax.swing.JTable JavaDoc();
320         bNew = new javax.swing.JButton JavaDoc();
321         bRemove = new javax.swing.JButton JavaDoc();
322         lExpandedText = new javax.swing.JLabel JavaDoc();
323         spExpandedText = new javax.swing.JScrollPane JavaDoc();
324         epExpandedText = new javax.swing.JEditorPane JavaDoc();
325         lExplandTemplateOn = new javax.swing.JLabel JavaDoc();
326         cbExpandTemplateOn = new javax.swing.JComboBox JavaDoc();
327
328         lLanguage.setLabelFor(cbLanguage);
329         lLanguage.setText("Language:");
330
331         lTemplates.setLabelFor(tTemplates);
332         lTemplates.setText("Templates:");
333
334         tTemplates.setModel(new javax.swing.table.DefaultTableModel JavaDoc(
335             new Object JavaDoc [][] {
336                 {null, null},
337                 {null, null},
338                 {null, null},
339                 {null, null}
340             },
341             new String JavaDoc [] {
342                 "Abbreviation", "Expanded Text"
343             }
344         ) {
345             Class JavaDoc[] types = new Class JavaDoc [] {
346                 java.lang.String JavaDoc.class, java.lang.String JavaDoc.class
347             };
348             boolean[] canEdit = new boolean [] {
349                 false, false
350             };
351
352             public Class JavaDoc getColumnClass(int columnIndex) {
353                 return types [columnIndex];
354             }
355
356             public boolean isCellEditable(int rowIndex, int columnIndex) {
357                 return canEdit [columnIndex];
358             }
359         });
360         spTemplates.setViewportView(tTemplates);
361
362         bNew.setText("New");
363
364         bRemove.setText("Remove");
365
366         lExpandedText.setLabelFor(epExpandedText);
367         lExpandedText.setText("Expanded Text:");
368
369         spExpandedText.setViewportView(epExpandedText);
370
371         lExplandTemplateOn.setLabelFor(cbExpandTemplateOn);
372         lExplandTemplateOn.setText("Expand Template on:");
373
374         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
375         this.setLayout(layout);
376         layout.setHorizontalGroup(
377             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
378             .add(layout.createSequentialGroup()
379                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
380                     .add(layout.createSequentialGroup()
381                         .add(lLanguage)
382                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
383                         .add(cbLanguage, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
384                     .add(lTemplates)
385                     .add(lExpandedText)
386                     .add(layout.createSequentialGroup()
387                         .add(lExplandTemplateOn)
388                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
389                         .add(cbExpandTemplateOn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
390                     .add(layout.createSequentialGroup()
391                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
392                             .add(org.jdesktop.layout.GroupLayout.LEADING, spExpandedText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
393                             .add(org.jdesktop.layout.GroupLayout.LEADING, spTemplates, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE))
394                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
395                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
396                             .add(bRemove, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
397                             .add(bNew, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))
398                         .add(10, 10, 10)))
399                 .addContainerGap())
400         );
401         layout.setVerticalGroup(
402             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
403             .add(layout.createSequentialGroup()
404                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
405                     .add(lLanguage)
406                     .add(cbLanguage, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
407                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
408                 .add(lTemplates)
409                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
410                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
411                     .add(layout.createSequentialGroup()
412                         .add(bNew)
413                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
414                         .add(bRemove))
415                     .add(spTemplates, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE))
416                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
417                 .add(lExpandedText)
418                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
419                 .add(spExpandedText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
420                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
421                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
422                     .add(lExplandTemplateOn)
423                     .add(cbExpandTemplateOn, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
424         );
425     }// </editor-fold>//GEN-END:initComponents
426

427     // Variables declaration - do not modify//GEN-BEGIN:variables
428
private javax.swing.JButton JavaDoc bNew;
429     private javax.swing.JButton JavaDoc bRemove;
430     private javax.swing.JComboBox JavaDoc cbExpandTemplateOn;
431     private javax.swing.JComboBox JavaDoc cbLanguage;
432     private javax.swing.JEditorPane JavaDoc epExpandedText;
433     private javax.swing.JLabel JavaDoc lExpandedText;
434     private javax.swing.JLabel JavaDoc lExplandTemplateOn;
435     private javax.swing.JLabel JavaDoc lLanguage;
436     private javax.swing.JLabel JavaDoc lTemplates;
437     private javax.swing.JScrollPane JavaDoc spExpandedText;
438     private javax.swing.JScrollPane JavaDoc spTemplates;
439     private javax.swing.JTable JavaDoc tTemplates;
440     // End of variables declaration//GEN-END:variables
441
}
442
Popular Tags