001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.common.method.impl;
043:
044: import java.awt.Component;
045: import java.awt.event.KeyAdapter;
046: import java.awt.event.KeyEvent;
047: import java.awt.event.MouseAdapter;
048: import java.awt.event.MouseEvent;
049: import java.util.ArrayList;
050: import java.util.List;
051: import java.util.Set;
052: import javax.lang.model.element.TypeElement;
053: import javax.swing.event.ListSelectionEvent;
054: import javax.swing.event.ListSelectionListener;
055: import javax.swing.table.AbstractTableModel;
056: import javax.swing.text.JTextComponent;
057: import org.netbeans.api.java.source.ClassIndex.NameKind;
058: import org.netbeans.api.java.source.ClassIndex.SearchScope;
059: import org.netbeans.api.java.source.ClasspathInfo;
060: import org.netbeans.api.java.source.ElementHandle;
061: import org.netbeans.api.java.source.ui.TypeElementFinder;
062: import org.openide.util.NbBundle;
063:
064: /**
065: *
066: * @author Martin Adamek
067: */
068: public final class ExceptionsPanel extends javax.swing.JPanel {
069:
070: private final ExceptionsTableModel tableModel;
071: private final ClasspathInfo cpInfo;
072:
073: public ExceptionsPanel(List<String> parameters, ClasspathInfo cpInfo) {
074: initComponents();
075: tableModel = new ExceptionsTableModel(parameters);
076: this .cpInfo = cpInfo;
077: table.setModel(tableModel);
078:
079: table.putClientProperty("terminateEditOnFocusLost",
080: Boolean.TRUE); // NOI18N
081:
082: ListSelectionListener listSelectionListener = new ListSelectionListenerImpl();
083: table.getSelectionModel().addListSelectionListener(
084: listSelectionListener);
085: table.getColumnModel().getSelectionModel()
086: .addListSelectionListener(listSelectionListener);
087:
088: table.addMouseListener(new MouseAdapter() {
089: public void mouseClicked(MouseEvent e) {
090: updateButtons();
091: }
092: });
093: table.addKeyListener(new KeyAdapter() {
094: public void keyReleased(KeyEvent e) {
095: updateButtons();
096: }
097: });
098:
099: updateButtons();
100: }
101:
102: public List<String> getExceptions() {
103: return tableModel.getExceptions();
104: }
105:
106: /** This method is called from within the constructor to
107: * initialize the form.
108: * WARNING: Do NOT modify this code. The content of this method is
109: * always regenerated by the Form Editor.
110: */
111: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
112: private void initComponents() {
113:
114: jScrollPane1 = new javax.swing.JScrollPane();
115: table = new javax.swing.JTable();
116: addButton = new javax.swing.JButton();
117: removeButton = new javax.swing.JButton();
118: upButton = new javax.swing.JButton();
119: downButton = new javax.swing.JButton();
120: editButton = new javax.swing.JButton();
121:
122: table.setModel(new javax.swing.table.DefaultTableModel(
123: new Object[][] {
124:
125: }, new String[] {
126:
127: }));
128: jScrollPane1.setViewportView(table);
129:
130: org.openide.awt.Mnemonics.setLocalizedText(addButton,
131: org.openide.util.NbBundle.getMessage(
132: ExceptionsPanel.class,
133: "ParametersPanel.addButton.text")); // NOI18N
134: addButton
135: .addActionListener(new java.awt.event.ActionListener() {
136: public void actionPerformed(
137: java.awt.event.ActionEvent evt) {
138: addButtonActionPerformed(evt);
139: }
140: });
141:
142: org.openide.awt.Mnemonics.setLocalizedText(removeButton,
143: org.openide.util.NbBundle.getMessage(
144: ExceptionsPanel.class,
145: "ParametersPanel.removeButton.text")); // NOI18N
146: removeButton
147: .addActionListener(new java.awt.event.ActionListener() {
148: public void actionPerformed(
149: java.awt.event.ActionEvent evt) {
150: removeButtonActionPerformed(evt);
151: }
152: });
153:
154: org.openide.awt.Mnemonics.setLocalizedText(upButton,
155: org.openide.util.NbBundle.getMessage(
156: ExceptionsPanel.class,
157: "ParametersPanel.upButton.text")); // NOI18N
158: upButton.setEnabled(false);
159: upButton.addActionListener(new java.awt.event.ActionListener() {
160: public void actionPerformed(java.awt.event.ActionEvent evt) {
161: upButtonActionPerformed(evt);
162: }
163: });
164:
165: org.openide.awt.Mnemonics.setLocalizedText(downButton,
166: org.openide.util.NbBundle.getMessage(
167: ExceptionsPanel.class,
168: "ParametersPanel.downButton.text")); // NOI18N
169: downButton.setEnabled(false);
170: downButton
171: .addActionListener(new java.awt.event.ActionListener() {
172: public void actionPerformed(
173: java.awt.event.ActionEvent evt) {
174: downButtonActionPerformed(evt);
175: }
176: });
177:
178: org.openide.awt.Mnemonics.setLocalizedText(editButton,
179: org.openide.util.NbBundle.getMessage(
180: ExceptionsPanel.class,
181: "ExceptionsPanel.editButton.text_1")); // NOI18N
182: editButton
183: .addActionListener(new java.awt.event.ActionListener() {
184: public void actionPerformed(
185: java.awt.event.ActionEvent evt) {
186: editButtonActionPerformed(evt);
187: }
188: });
189:
190: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
191: this );
192: this .setLayout(layout);
193: layout
194: .setHorizontalGroup(layout
195: .createParallelGroup(
196: org.jdesktop.layout.GroupLayout.LEADING)
197: .add(
198: layout
199: .createSequentialGroup()
200: .addContainerGap()
201: .add(
202: jScrollPane1,
203: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
204: 241, Short.MAX_VALUE)
205: .addPreferredGap(
206: org.jdesktop.layout.LayoutStyle.RELATED)
207: .add(
208: layout
209: .createParallelGroup(
210: org.jdesktop.layout.GroupLayout.LEADING)
211: .add(addButton)
212: .add(
213: removeButton)
214: .add(editButton)
215: .add(upButton)
216: .add(downButton))
217: .addContainerGap()));
218:
219: layout.linkSize(new java.awt.Component[] { addButton,
220: downButton, editButton, removeButton, upButton },
221: org.jdesktop.layout.GroupLayout.HORIZONTAL);
222:
223: layout
224: .setVerticalGroup(layout
225: .createParallelGroup(
226: org.jdesktop.layout.GroupLayout.LEADING)
227: .add(
228: layout
229: .createSequentialGroup()
230: .addContainerGap()
231: .add(
232: layout
233: .createParallelGroup(
234: org.jdesktop.layout.GroupLayout.LEADING)
235: .add(
236: layout
237: .createSequentialGroup()
238: .add(
239: addButton)
240: .addPreferredGap(
241: org.jdesktop.layout.LayoutStyle.RELATED)
242: .add(
243: removeButton)
244: .addPreferredGap(
245: org.jdesktop.layout.LayoutStyle.RELATED)
246: .add(
247: editButton)
248: .add(
249: 18,
250: 18,
251: 18)
252: .add(
253: upButton)
254: .addPreferredGap(
255: org.jdesktop.layout.LayoutStyle.RELATED)
256: .add(
257: downButton))
258: .add(
259: jScrollPane1,
260: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
261: 276,
262: Short.MAX_VALUE))
263: .addContainerGap()));
264:
265: addButton.getAccessibleContext().setAccessibleDescription(
266: org.openide.util.NbBundle.getMessage(
267: ExceptionsPanel.class,
268: "ACSD_AddExceptionIntoTab")); // NOI18N
269: removeButton.getAccessibleContext().setAccessibleDescription(
270: org.openide.util.NbBundle.getMessage(
271: ExceptionsPanel.class,
272: "ACSD_RemoveExceptionFromTab")); // NOI18N
273: upButton.getAccessibleContext()
274: .setAccessibleDescription(
275: org.openide.util.NbBundle.getMessage(
276: ExceptionsPanel.class,
277: "ACSD_MoveUpInExcepTab")); // NOI18N
278: downButton.getAccessibleContext().setAccessibleDescription(
279: org.openide.util.NbBundle.getMessage(
280: ExceptionsPanel.class,
281: "ACSD_MoveDownInExceptionTab")); // NOI18N
282: }// </editor-fold>//GEN-END:initComponents
283:
284: private void downButtonActionPerformed(
285: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downButtonActionPerformed
286: int selIndex = table.getSelectedRow();
287: int newIndex = selIndex + 1;
288: if (newIndex < tableModel.getExceptions().size()) {
289: tableModel.set(newIndex, tableModel.set(selIndex,
290: tableModel.getException(newIndex)));
291: table.setRowSelectionInterval(newIndex, newIndex);
292: updateButtons();
293: }
294: }//GEN-LAST:event_downButtonActionPerformed
295:
296: private void upButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_upButtonActionPerformed
297: int selIndex = table.getSelectedRow();
298: int newIndex = selIndex - 1;
299: if (newIndex >= 0) {
300: tableModel.set(newIndex, tableModel.set(selIndex,
301: tableModel.getException(newIndex)));
302: table.setRowSelectionInterval(newIndex, newIndex);
303: updateButtons();
304: }
305: }//GEN-LAST:event_upButtonActionPerformed
306:
307: private void removeButtonActionPerformed(
308: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
309: int selectedRow = table.getSelectedRow();
310: if (selectedRow > -1) {
311: tableModel.removeException(selectedRow);
312: }
313: if (selectedRow == table.getRowCount()) {
314: selectedRow--;
315: }
316: table.getSelectionModel().setSelectionInterval(selectedRow,
317: selectedRow);
318: updateButtons();
319: }//GEN-LAST:event_removeButtonActionPerformed
320:
321: private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
322: ElementHandle<TypeElement> handle = browseExceptions();
323: if (handle != null) {
324: int index = tableModel.addException(handle
325: .getQualifiedName());
326: table.getSelectionModel()
327: .setSelectionInterval(index, index);
328: table.getColumnModel().getSelectionModel()
329: .setSelectionInterval(0, 0);
330: updateButtons();
331: }
332: }//GEN-LAST:event_addButtonActionPerformed
333:
334: private void editButtonActionPerformed(
335: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
336: final ElementHandle<TypeElement> handle = browseExceptions();
337: if (handle != null) {
338: int index = table.getSelectedRow();
339: table.getSelectionModel()
340: .setSelectionInterval(index, index);
341: table.getColumnModel().getSelectionModel()
342: .setSelectionInterval(0, 0);
343: tableModel.setValueAt(handle.getQualifiedName(), index, 0);
344: }
345: }//GEN-LAST:event_editButtonActionPerformed
346:
347: private ElementHandle<TypeElement> browseExceptions() {
348: return TypeElementFinder.find(cpInfo,
349: new TypeElementFinder.Customizer() {
350: public Set<ElementHandle<TypeElement>> query(
351: ClasspathInfo classpathInfo,
352: String textForQuery, NameKind nameKind,
353: Set<SearchScope> searchScopes) {
354: return classpathInfo.getClassIndex()
355: .getDeclaredTypes(textForQuery,
356: nameKind, searchScopes);
357: }
358:
359: public boolean accept(
360: ElementHandle<TypeElement> typeHandle) {
361: return true;
362: }
363: });
364: }
365:
366: // Variables declaration - do not modify//GEN-BEGIN:variables
367: private javax.swing.JButton addButton;
368: private javax.swing.JButton downButton;
369: private javax.swing.JButton editButton;
370: private javax.swing.JScrollPane jScrollPane1;
371: private javax.swing.JButton removeButton;
372: private javax.swing.JTable table;
373: private javax.swing.JButton upButton;
374:
375: // End of variables declaration//GEN-END:variables
376:
377: private void updateButtons() {
378: int selIndex = table.getSelectedRow();
379: boolean oneSelected = table.getSelectedRowCount() == 1;
380:
381: removeButton.setEnabled(oneSelected);
382: editButton.setEnabled(oneSelected);
383: upButton.setEnabled(oneSelected && (selIndex > 0));
384: downButton.setEnabled(oneSelected
385: && (selIndex < tableModel.getRowCount() - 1));
386: }
387:
388: // accessible for test
389: static class ExceptionsTableModel extends AbstractTableModel {
390:
391: private final List<String> exceptions;
392:
393: public ExceptionsTableModel(List<String> parameters) {
394: this .exceptions = new ArrayList<String>(parameters);
395: }
396:
397: public List<String> getExceptions() {
398: return exceptions;
399: }
400:
401: public int addException(String exceptionName) {
402: int index = exceptions.size();
403: exceptions.add(exceptionName);
404: fireTableRowsInserted(index, index);
405: return index;
406: }
407:
408: public void removeException(int index) {
409: exceptions.remove(index);
410: fireTableRowsDeleted(index, index);
411: }
412:
413: public String getException(int index) {
414: return exceptions.get(index);
415: }
416:
417: public String set(int index, String exception) {
418: return exceptions.set(index, exception);
419: }
420:
421: public int getRowCount() {
422: return exceptions.size();
423: }
424:
425: public int getColumnCount() {
426: return 1;
427: }
428:
429: public Object getValueAt(int row, int column) {
430: Object result = null;
431: if (row >= 0) {
432: result = exceptions.get(row);
433: }
434: return result;
435: }
436:
437: public String getColumnName(int column) {
438: return NbBundle.getMessage(ParametersPanel.class,
439: "ExceptionsPanel.LBL_Exception");
440: }
441:
442: public boolean isCellEditable(int row, int column) {
443: return true;
444: }
445:
446: public void setValueAt(Object aValue, int row, int column) {
447: exceptions.set(row, (String) aValue);
448: fireTableCellUpdated(row, column);
449: }
450:
451: // JTable uses this method to determine the default renderer/editor for each cell.
452: // If we didn't implement this method, then the last column would contain
453: // text ("true"/"false"), rather than a check box.
454: public Class getColumnClass(int c) {
455: return getValueAt(0, c).getClass();
456: }
457:
458: }
459:
460: private class ListSelectionListenerImpl implements
461: ListSelectionListener {
462:
463: public void valueChanged(ListSelectionEvent e) {
464: table.editCellAt(table.getSelectedRow(), table
465: .getSelectedColumn());
466: Component editor = table.getEditorComponent();
467: if (editor != null) {
468: editor.requestFocus();
469: }
470: if (editor instanceof JTextComponent) {
471: JTextComponent textComp = (JTextComponent) editor;
472: textComp.selectAll();
473: }
474: }
475:
476: }
477:
478: }
|