KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > java > ParamsPanel


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.i18n.java;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26 import javax.swing.AbstractListModel JavaDoc;
27 import javax.swing.event.ListSelectionListener JavaDoc;
28 import javax.swing.event.ListSelectionEvent JavaDoc;
29 import javax.swing.JPanel JavaDoc;
30
31 import org.netbeans.modules.i18n.I18nUtil;
32
33 import org.openide.util.HelpCtx;
34
35
36 /**
37  * Panel for adding parameters to MessageFormat.format code by i18n action.
38  *
39  * @author Petr Jiricka
40  */

41 public class ParamsPanel extends JPanel JavaDoc {
42
43     /** List of arguments. */
44     private ArrayList JavaDoc arguments = new ArrayList JavaDoc();
45
46     /** Edited row. */
47     private int editingRow = -1;
48
49     /** List model for parameters. */
50     private ParamsListModel model;
51
52     private final ResourceBundle JavaDoc bundle;
53
54     /** Generated serailized version UID. */
55     static final long serialVersionUID =-3754019215574878093L;
56     
57     
58     /** Creates new form ParamsPanel */
59     public ParamsPanel() {
60         bundle = org.openide.util.NbBundle.getBundle(ParamsPanel.class);
61         initComponents ();
62         initAccessibility();
63         paramsList.setModel(getListModel());
64         paramsList.getSelectionModel().addListSelectionListener(
65             new ListSelectionListener JavaDoc() {
66                 public void valueChanged(ListSelectionEvent JavaDoc e) {
67                     if (paramsList.getSelectedIndex() != -1)
68                         updateEditor(paramsList.getSelectedIndex());
69                     removeParamButton.setEnabled(paramsList.getSelectedIndex() != -1);
70                 }
71             }
72         );
73         removeParamButton.setEnabled(paramsList.getSelectedIndex() != -1);
74         HelpCtx.setHelpIDString(this, I18nUtil.HELP_ID_ADDPARAMS);
75     }
76
77     /** Sets arguments. */
78     public void setArguments(String JavaDoc[] args) {
79         arguments.clear();
80         for (int i = 0; i < args.length; i++) {
81             arguments.add(args[i]);
82         }
83         // equalize();
84
if (getListModel().getSize() > 0)
85             getListModel().fireIntervalAdded(0, getListModel().getSize() - 1);
86         if (getListModel().getSize() > 0)
87             editRow(0);
88         else
89             editRow(-1);
90     }
91
92     /** Gets arguments. */
93     public String JavaDoc[] getArguments() {
94         commitChanges();
95
96         // j is the last non-empty index
97
int j = -1;
98         for (int i = 0; i < arguments.size(); i++)
99             if (((String JavaDoc)arguments.get(i)).trim().length() > 0)
100                 j = i;
101
102         String JavaDoc[] args = new String JavaDoc[j + 1];
103         for (int i = 0; i <= j; i++)
104             args[i] = (String JavaDoc)arguments.get(i);
105
106         return args;
107     }
108
109     /** Commits changes. */
110     private void commitChanges() {
111         //mainComment = mainCommentTextArea.getText();
112
if (editingRow != -1) {
113             //comments.set (editingRow, commentTextArea.getText());
114
arguments.set(editingRow, codePane.getText());
115             getListModel().fireContentsChanged(editingRow, editingRow);
116         }
117     }
118
119     /** Sets the index of the row being edited to row or disables editing if row == -1.
120      * Should only be called with -1 if there is no data. */

121     private void editRow(int row) {
122         if (row != -1)
123             paramsList.setSelectedIndex(row);
124         else
125             paramsList.setSelectedIndices(new int[0]);
126     }
127
128     /** Updates editor. */
129     private void updateEditor(int row) {
130         commitChanges();
131         editingRow = row;
132         if (row == -1) {
133             //commentTextArea.setText("");
134
codePane.setText(""); // NOI18N
135
//commentTextArea.setEnabled(false);
136
codePane.setEnabled(false);
137         }
138         else {
139             //commentTextArea.setText((String)comments.get(editingRow));
140
codePane.setText((String JavaDoc)arguments.get(editingRow));
141             //commentTextArea.setEnabled(true);
142
codePane.setEnabled(true);
143             codePane.requestFocus();
144         }
145     }
146
147     /** Gets list model for parameters. */
148     private ParamsListModel getListModel() {
149         if (model == null)
150             model = new ParamsListModel ();
151         return model;
152     }
153
154     
155     private void initAccessibility() {
156         this.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_ParamsPanel"));
157         removeParamButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_CTL_RemoveButton"));
158         addParamButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_CTL_AddButton"));
159         paramsList.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_paramsList"));
160     }
161     
162     /** This method is called from within the constructor to
163      * initialize the form.
164      * WARNING: Do NOT modify this code. The content of this method is
165      * always regenerated by the FormEditor.
166      */

167     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
168
private void initComponents() {
169         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
170
171         paramLabel = new javax.swing.JLabel JavaDoc();
172         codeLabel = new javax.swing.JLabel JavaDoc();
173         paramsScroll = new javax.swing.JScrollPane JavaDoc();
174         paramsList = new javax.swing.JList JavaDoc();
175         addRemovePanel = new javax.swing.JPanel JavaDoc();
176         addParamButton = new javax.swing.JButton JavaDoc();
177         removeParamButton = new javax.swing.JButton JavaDoc();
178         codeScroll = new javax.swing.JScrollPane JavaDoc();
179         codePane = new javax.swing.JEditorPane JavaDoc();
180
181         setLayout(new java.awt.GridBagLayout JavaDoc());
182
183         paramLabel.setLabelFor(paramsList);
184         org.openide.awt.Mnemonics.setLocalizedText(paramLabel, bundle.getString("LBL_Parameters")); // NOI18N
185
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
186         gridBagConstraints.gridwidth = 2;
187         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
188         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
189         add(paramLabel, gridBagConstraints);
190
191         codeLabel.setLabelFor(codePane);
192         org.openide.awt.Mnemonics.setLocalizedText(codeLabel, bundle.getString("LBL_Code")); // NOI18N
193
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
194         gridBagConstraints.gridx = 0;
195         gridBagConstraints.gridy = 2;
196         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
197         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
198         add(codeLabel, gridBagConstraints);
199
200         paramsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
201         paramsList.setVisibleRowCount(3);
202         paramsScroll.setViewportView(paramsList);
203
204         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
205         gridBagConstraints.gridx = 0;
206         gridBagConstraints.gridy = 1;
207         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
208         gridBagConstraints.weightx = 1.0;
209         gridBagConstraints.weighty = 1.0;
210         gridBagConstraints.insets = new java.awt.Insets JavaDoc(2, 12, 0, 0);
211         add(paramsScroll, gridBagConstraints);
212
213         addRemovePanel.setLayout(new java.awt.GridBagLayout JavaDoc());
214
215         org.openide.awt.Mnemonics.setLocalizedText(addParamButton, bundle.getString("CTL_AddButton")); // NOI18N
216
addParamButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
217             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
218                 addParamButtonActionPerformed(evt);
219             }
220         });
221         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
222         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
223         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
224         gridBagConstraints.weightx = 1.0;
225         addRemovePanel.add(addParamButton, gridBagConstraints);
226
227         org.openide.awt.Mnemonics.setLocalizedText(removeParamButton, bundle.getString("CTL_RemoveButton")); // NOI18N
228
removeParamButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
229             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
230                 removeParamButtonActionPerformed(evt);
231             }
232         });
233         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
234         gridBagConstraints.gridx = 0;
235         gridBagConstraints.gridy = 1;
236         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
237         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
238         gridBagConstraints.weightx = 1.0;
239         gridBagConstraints.weighty = 1.0;
240         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 0);
241         addRemovePanel.add(removeParamButton, gridBagConstraints);
242
243         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
244         gridBagConstraints.gridx = 1;
245         gridBagConstraints.gridy = 1;
246         gridBagConstraints.gridheight = 2;
247         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
248         gridBagConstraints.weighty = 1.0;
249         gridBagConstraints.insets = new java.awt.Insets JavaDoc(2, 11, 0, 11);
250         add(addRemovePanel, gridBagConstraints);
251
252         codePane.setContentType("text/x-java");
253         codePane.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
254             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
255                 codePaneFocusLost(evt);
256             }
257         });
258         codeScroll.setViewportView(codePane);
259
260         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
261         gridBagConstraints.gridx = 0;
262         gridBagConstraints.gridy = 3;
263         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
264         gridBagConstraints.weightx = 1.0;
265         gridBagConstraints.weighty = 1.0;
266         gridBagConstraints.insets = new java.awt.Insets JavaDoc(2, 12, 11, 0);
267         add(codeScroll, gridBagConstraints);
268     }// </editor-fold>//GEN-END:initComponents
269

270     private void codePaneFocusLost (java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_codePaneFocusLost
271
commitChanges();
272     }//GEN-LAST:event_codePaneFocusLost
273

274     private void removeParamButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeParamButtonActionPerformed
275
int index = paramsList.getSelectedIndex();
276         if (index == -1) return;
277         arguments.remove(index);
278         getListModel().fireIntervalRemoved(index, index);
279         if (index >= arguments.size()) index--;
280         editingRow = -1; // so the row is not updated
281
editRow(index);
282     }//GEN-LAST:event_removeParamButtonActionPerformed
283

284     private void addParamButtonActionPerformed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addParamButtonActionPerformed
285
arguments.add("");
286         //comments.add("");
287
getListModel().fireIntervalAdded(getListModel().getSize() - 1, getListModel().getSize() - 1);
288         editRow(getListModel().getSize() - 1);
289     }//GEN-LAST:event_addParamButtonActionPerformed
290

291
292     // Variables declaration - do not modify//GEN-BEGIN:variables
293
private javax.swing.JButton JavaDoc addParamButton;
294     private javax.swing.JPanel JavaDoc addRemovePanel;
295     private javax.swing.JLabel JavaDoc codeLabel;
296     private javax.swing.JEditorPane JavaDoc codePane;
297     private javax.swing.JScrollPane JavaDoc codeScroll;
298     private javax.swing.JLabel JavaDoc paramLabel;
299     private javax.swing.JList JavaDoc paramsList;
300     private javax.swing.JScrollPane JavaDoc paramsScroll;
301     private javax.swing.JButton JavaDoc removeParamButton;
302     // End of variables declaration//GEN-END:variables
303

304     /** List model for the list of parameters */
305     protected class ParamsListModel extends AbstractListModel JavaDoc {
306
307         /** Generated serial version UID. */
308         static final long serialVersionUID =6832148996617470334L;
309         
310         /** DEfault constructor. */
311         public ParamsListModel () {
312         }
313
314
315         /** Gets number of arguments in model. */
316         public int getSize() {
317             return arguments.size();
318         }
319
320         /** Gets n-th arguments from list model.
321          * @param index index of argument from list to get */

322         public Object JavaDoc getElementAt(int index) {
323             return "{" + index + "} " + (String JavaDoc)arguments.get(index);
324         }
325
326         /** Fires that one or more elements from interval were changed.
327          * @param index0 start index
328          * @param index1 end index */

329         public void fireContentsChanged(int index0, int index1) {
330             super.fireContentsChanged(this, index0, index1);
331         }
332
333         /** Fires that one or more elements from interval were added.
334          * @param index0 start index
335          * @param index1 end index */

336         public void fireIntervalAdded(int index0, int index1) {
337             super.fireIntervalAdded(this, index0, index1);
338         }
339
340         /** Fires that one or more elements from interval were removed.
341          * @param index0 start index
342          * @param index1 end index */

343         public void fireIntervalRemoved(int index0, int index1) {
344             super.fireIntervalRemoved(this, index0, index1);
345         }
346     }
347 }
348
Popular Tags