Lines 1-314
Link Here
|
1 |
/* |
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* Portions Copyrighted 2007 Sun Microsystems, Inc. |
27 |
*/ |
28 |
package org.netbeans.modules.java.hints; |
29 |
|
30 |
import java.awt.Dialog; |
31 |
import java.awt.GridBagConstraints; |
32 |
import java.awt.GridBagLayout; |
33 |
import java.awt.Insets; |
34 |
import java.util.ArrayList; |
35 |
import java.util.List; |
36 |
import java.util.StringTokenizer; |
37 |
import java.util.prefs.Preferences; |
38 |
import javax.swing.BorderFactory; |
39 |
import javax.swing.DefaultListModel; |
40 |
import javax.swing.JButton; |
41 |
import javax.swing.JLabel; |
42 |
import javax.swing.JPanel; |
43 |
import javax.swing.JTextField; |
44 |
import javax.swing.event.DocumentEvent; |
45 |
import javax.swing.event.DocumentListener; |
46 |
import javax.swing.event.ListSelectionEvent; |
47 |
import javax.swing.event.ListSelectionListener; |
48 |
import org.openide.DialogDescriptor; |
49 |
import org.openide.DialogDisplayer; |
50 |
import org.openide.util.NbBundle; |
51 |
|
52 |
/** |
53 |
* |
54 |
* @author phrebejk |
55 |
*/ |
56 |
public class ForbiddenImportsCustomizer extends javax.swing.JPanel { |
57 |
|
58 |
private static String FORBIDDEN_IMPORTS_KEY = "ForbiddenImports"; // NOI18N |
59 |
private static String FORBIDDEN_IMPORTS_DEFAULT = "sun.**"; // NOI18N |
60 |
private static String IMPORTS_DELIMITER = ";"; // NOI18N |
61 |
|
62 |
private Preferences prefs; |
63 |
|
64 |
/** Creates new form ForbiddenImports */ |
65 |
public ForbiddenImportsCustomizer(Preferences node) { |
66 |
this.prefs = node; |
67 |
initComponents(); |
68 |
DefaultListModel model = new DefaultListModel(); |
69 |
for( String item : getForbiddenImports(node) ) { |
70 |
model.addElement( item ); |
71 |
} |
72 |
listItems.setModel( model ); |
73 |
listItems.addListSelectionListener( new ListSelectionListener() { |
74 |
public void valueChanged(ListSelectionEvent e) { |
75 |
enableButtons(); |
76 |
} |
77 |
}); |
78 |
enableButtons(); |
79 |
} |
80 |
|
81 |
private void enableButtons() { |
82 |
int selIndex = listItems.getSelectedIndex(); |
83 |
btnEdit.setEnabled( selIndex >= 0 ); |
84 |
btnRemove.setEnabled( selIndex >= 0 ); |
85 |
} |
86 |
|
87 |
private void updatePreferences() { |
88 |
String[] items = new String[listItems.getModel().getSize()]; |
89 |
for( int i=0; i<((DefaultListModel)listItems.getModel()).size(); i++ ) { |
90 |
items[i] = (String)listItems.getModel().getElementAt(i); |
91 |
} |
92 |
prefs.put(FORBIDDEN_IMPORTS_KEY, encodeForbiddenImports( items )); |
93 |
} |
94 |
|
95 |
private String showInputDialog( String initialValue ) { |
96 |
String title = null == initialValue ? NbBundle.getMessage( ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.titleAdd" ) //NOI18N |
97 |
: NbBundle.getMessage( ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.titleEdit" ); //NOI18N |
98 |
final JButton btnOk = new JButton( null == initialValue ? NbBundle.getMessage( ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.btnAdd" ) //NOI18N |
99 |
: NbBundle.getMessage( ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.btnEdit" ) ); //NOI18N |
100 |
btnOk.setEnabled( null != initialValue ); |
101 |
JButton btnCancel = new JButton( NbBundle.getMessage( ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.btnCancel" ) ); //NOI18N |
102 |
|
103 |
JPanel panel = new JPanel( new GridBagLayout() ); |
104 |
panel.setBorder( BorderFactory.createEmptyBorder(10, 10, 0, 10) ); |
105 |
|
106 |
JTextField input = new JTextField(); |
107 |
input.setText( null == initialValue ? "" : initialValue ); |
108 |
input.getDocument().addDocumentListener( new DocumentListener() { |
109 |
|
110 |
public void insertUpdate(DocumentEvent e) { |
111 |
btnOk.setEnabled( e.getDocument().getLength() > 0 ); |
112 |
} |
113 |
|
114 |
public void removeUpdate(DocumentEvent e) { |
115 |
btnOk.setEnabled( e.getDocument().getLength() > 0 ); |
116 |
} |
117 |
|
118 |
public void changedUpdate(DocumentEvent e) { |
119 |
btnOk.setEnabled( e.getDocument().getLength() > 0 ); |
120 |
} |
121 |
}); |
122 |
|
123 |
panel.add( new JLabel(NbBundle.getMessage( ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.label" ) ), //NOI18N |
124 |
new GridBagConstraints(0,0,1,1,0.0,0.0,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(0,0,0,5),0,0) ); |
125 |
panel.add( input, new GridBagConstraints(1,0,1,1,1.0,0.0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,new Insets(0,0,0,0),0,0) ); |
126 |
panel.add( new JLabel(NbBundle.getMessage(ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.hint") ), //NOI18N |
127 |
new GridBagConstraints(1,1,1,1,1.0,0.0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,new Insets(5,0,0,0),0,0) ); |
128 |
|
129 |
DialogDescriptor dd = new DialogDescriptor(panel, title, true, |
130 |
new Object[] { btnOk, btnCancel }, btnOk, |
131 |
DialogDescriptor.DEFAULT_ALIGN, null, null ); |
132 |
Dialog dlg = DialogDisplayer.getDefault().createDialog(dd); |
133 |
dlg.setVisible( true ); |
134 |
if( btnOk == dd.getValue() ) { |
135 |
return input.getText(); |
136 |
} |
137 |
return null; |
138 |
} |
139 |
|
140 |
/** This method is called from within the constructor to |
141 |
* initialize the form. |
142 |
* WARNING: Do NOT modify this code. The content of this method is |
143 |
* always regenerated by the Form Editor. |
144 |
*/ |
145 |
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
146 |
private void initComponents() { |
147 |
java.awt.GridBagConstraints gridBagConstraints; |
148 |
|
149 |
jScrollPane1 = new javax.swing.JScrollPane(); |
150 |
listItems = new javax.swing.JList(); |
151 |
jPanel1 = new javax.swing.JPanel(); |
152 |
btnAdd = new javax.swing.JButton(); |
153 |
btnEdit = new javax.swing.JButton(); |
154 |
btnRemove = new javax.swing.JButton(); |
155 |
|
156 |
setOpaque(false); |
157 |
setLayout(new java.awt.GridBagLayout()); |
158 |
|
159 |
listItems.setModel(new javax.swing.AbstractListModel() { |
160 |
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; |
161 |
public int getSize() { return strings.length; } |
162 |
public Object getElementAt(int i) { return strings[i]; } |
163 |
}); |
164 |
jScrollPane1.setViewportView(listItems); |
165 |
listItems.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.listItems.AccessibleContext.accessibleName")); // NOI18N |
166 |
listItems.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.listItems.AccessibleContext.accessibleDescription")); // NOI18N |
167 |
|
168 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
169 |
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; |
170 |
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; |
171 |
gridBagConstraints.weightx = 1.0; |
172 |
gridBagConstraints.weighty = 1.0; |
173 |
add(jScrollPane1, gridBagConstraints); |
174 |
jScrollPane1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.jScrollPane1.AccessibleContext.accessibleName")); // NOI18N |
175 |
jScrollPane1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.jScrollPane1.AccessibleContext.accessibleDescription")); // NOI18N |
176 |
|
177 |
jPanel1.setOpaque(false); |
178 |
jPanel1.setLayout(new java.awt.GridBagLayout()); |
179 |
|
180 |
org.openide.awt.Mnemonics.setLocalizedText(btnAdd, org.openide.util.NbBundle.getMessage(ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.btnAdd.text")); // NOI18N |
181 |
btnAdd.addActionListener(new java.awt.event.ActionListener() { |
182 |
public void actionPerformed(java.awt.event.ActionEvent evt) { |
183 |
addItem(evt); |
184 |
} |
185 |
}); |
186 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
187 |
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; |
188 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
189 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; |
190 |
gridBagConstraints.weightx = 1.0; |
191 |
gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0); |
192 |
jPanel1.add(btnAdd, gridBagConstraints); |
193 |
btnAdd.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.btnAdd.AccessibleContext.accessibleDescription")); // NOI18N |
194 |
|
195 |
org.openide.awt.Mnemonics.setLocalizedText(btnEdit, org.openide.util.NbBundle.getMessage(ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.btnEdit.text")); // NOI18N |
196 |
btnEdit.addActionListener(new java.awt.event.ActionListener() { |
197 |
public void actionPerformed(java.awt.event.ActionEvent evt) { |
198 |
editItem(evt); |
199 |
} |
200 |
}); |
201 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
202 |
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; |
203 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
204 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; |
205 |
gridBagConstraints.weightx = 1.0; |
206 |
gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0); |
207 |
jPanel1.add(btnEdit, gridBagConstraints); |
208 |
|
209 |
org.openide.awt.Mnemonics.setLocalizedText(btnRemove, org.openide.util.NbBundle.getMessage(ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.btnRemove.text")); // NOI18N |
210 |
btnRemove.addActionListener(new java.awt.event.ActionListener() { |
211 |
public void actionPerformed(java.awt.event.ActionEvent evt) { |
212 |
removeItem(evt); |
213 |
} |
214 |
}); |
215 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
216 |
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; |
217 |
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; |
218 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
219 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; |
220 |
gridBagConstraints.weightx = 1.0; |
221 |
gridBagConstraints.weighty = 1.0; |
222 |
jPanel1.add(btnRemove, gridBagConstraints); |
223 |
|
224 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
225 |
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; |
226 |
gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; |
227 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; |
228 |
gridBagConstraints.insets = new java.awt.Insets(0, 8, 0, 0); |
229 |
add(jPanel1, gridBagConstraints); |
230 |
|
231 |
getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.AccessibleContext.accessibleName")); // NOI18N |
232 |
getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ForbiddenImportsCustomizer.class, "ForbiddenImportsCustomizer.AccessibleContext.accessibleDescription")); // NOI18N |
233 |
}// </editor-fold>//GEN-END:initComponents |
234 |
|
235 |
private void removeItem(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeItem |
236 |
int selIndex = listItems.getSelectedIndex();//GEN-LAST:event_removeItem |
237 |
if( selIndex < 0 ) |
238 |
return; |
239 |
((DefaultListModel)listItems.getModel()).remove( selIndex ); |
240 |
selIndex++; |
241 |
if( selIndex > listItems.getModel().getSize()-1 ) |
242 |
selIndex = listItems.getModel().getSize()-1; |
243 |
listItems.getSelectionModel().setSelectionInterval( selIndex, selIndex ); |
244 |
updatePreferences(); |
245 |
} |
246 |
|
247 |
private void editItem(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editItem |
248 |
int selIndex = listItems.getSelectedIndex();//GEN-LAST:event_editItem |
249 |
if( selIndex < 0 ) |
250 |
return; |
251 |
String initialValue = (String)((DefaultListModel)listItems.getModel()).get( selIndex ); |
252 |
String newValue = showInputDialog( initialValue ); |
253 |
if( null != newValue ) { |
254 |
((DefaultListModel)listItems.getModel()).set( selIndex, newValue ); |
255 |
listItems.getSelectionModel().setSelectionInterval( selIndex, selIndex ); |
256 |
updatePreferences(); |
257 |
} |
258 |
} |
259 |
|
260 |
private void addItem(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addItem |
261 |
String newValue = showInputDialog( null );//GEN-LAST:event_addItem |
262 |
if( null != newValue ) { |
263 |
((DefaultListModel)listItems.getModel()).addElement( newValue ); |
264 |
int count = listItems.getModel().getSize(); |
265 |
if( count > 0 ) |
266 |
listItems.getSelectionModel().setSelectionInterval( count-1, count-1 ); |
267 |
updatePreferences(); |
268 |
} |
269 |
|
270 |
} |
271 |
|
272 |
|
273 |
// Variables declaration - do not modify//GEN-BEGIN:variables |
274 |
private javax.swing.JButton btnAdd; |
275 |
private javax.swing.JButton btnEdit; |
276 |
private javax.swing.JButton btnRemove; |
277 |
private javax.swing.JPanel jPanel1; |
278 |
private javax.swing.JScrollPane jScrollPane1; |
279 |
private javax.swing.JList listItems; |
280 |
// End of variables declaration//GEN-END:variables |
281 |
|
282 |
public static String encodeForbiddenImports( String[] forbiddenImports ) { |
283 |
|
284 |
StringBuffer sb = new StringBuffer(); |
285 |
|
286 |
for( String fi : forbiddenImports ) { |
287 |
sb.append(fi); |
288 |
sb.append(IMPORTS_DELIMITER); |
289 |
} |
290 |
|
291 |
return sb.toString(); |
292 |
} |
293 |
|
294 |
public static String[] decodeForbiddenImports( String forbiddenImports ) { |
295 |
|
296 |
StringTokenizer st = new StringTokenizer(forbiddenImports, IMPORTS_DELIMITER, false ); |
297 |
|
298 |
List<String> imList = new ArrayList<String>(); |
299 |
|
300 |
while( st.hasMoreTokens() ) { |
301 |
String im = st.nextToken(); |
302 |
imList.add(im); |
303 |
} |
304 |
|
305 |
return imList.toArray(new String[imList.size()]); |
306 |
|
307 |
} |
308 |
|
309 |
public static String[] getForbiddenImports(Preferences node ) { |
310 |
String text = node.get(FORBIDDEN_IMPORTS_KEY, FORBIDDEN_IMPORTS_DEFAULT); |
311 |
return decodeForbiddenImports(text); |
312 |
} |
313 |
|
314 |
} |