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: package org.netbeans.modules.websvc.core.webservices.ui.panels;
042:
043: import java.awt.Component;
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.io.IOException;
047: import java.util.List;
048: import java.util.ListIterator;
049: import java.util.Map;
050: import java.util.Set;
051: import javax.swing.JLabel;
052: import javax.swing.JTable;
053: import javax.swing.ListSelectionModel;
054: import javax.swing.event.ListSelectionEvent;
055: import javax.swing.event.ListSelectionListener;
056: import javax.swing.table.DefaultTableModel;
057: import javax.swing.table.JTableHeader;
058: import javax.swing.table.TableCellRenderer;
059: import javax.swing.table.TableModel;
060: import org.netbeans.api.java.project.JavaProjectConstants;
061: import org.netbeans.api.java.source.CancellableTask;
062: import org.netbeans.api.java.source.CompilationController;
063: import org.netbeans.api.java.source.JavaSource;
064: import org.netbeans.api.java.source.JavaSource.Phase;
065: import org.netbeans.api.project.Project;
066: import org.netbeans.api.project.ProjectUtils;
067: import org.netbeans.api.project.SourceGroup;
068: import org.netbeans.api.project.Sources;
069: import org.netbeans.modules.websvc.core.webservices.ui.WSHandlerDialog;
070: import org.openide.DialogDescriptor;
071: import org.openide.DialogDisplayer;
072: import org.openide.ErrorManager;
073: import org.openide.NotifyDescriptor;
074: import org.openide.filesystems.FileObject;
075: import org.openide.util.NbBundle;
076:
077: /**
078: *
079: * @author rico
080: */
081: public class MessageHandlerPanel extends javax.swing.JPanel {
082:
083: private Project project;
084: private List<String> handlerClasses;
085: private boolean isJaxWS;
086: private String componentName;
087: private boolean isChanged;
088: private int protocolIndex = -1;
089: private static final String LOGICAL_TYPE = "Logical";
090: private static final String PROTOCOL_TYPE = "Protocol";
091:
092: /** Creates new form HandlerPanel */
093: public MessageHandlerPanel(Project project,
094: List<String> handlerClasses, boolean isJaxWS,
095: String componentName) {
096: this .project = project;
097: this .handlerClasses = handlerClasses;
098: this .isJaxWS = isJaxWS;
099: this .componentName = componentName;
100: initComponents();
101: addBtn.addActionListener(new AddButtonActionListener());
102: removeBtn.addActionListener(new RemoveButtonActionListener());
103: populateHandlers();
104: isChanged = false;
105: handlerTable.getColumnModel().getColumn(1).setCellRenderer(
106: new TypeCellRenderer());
107: }
108:
109: public boolean isChanged() {
110: return isChanged;
111: }
112:
113: public TableModel getHandlerTableModel() {
114: return handlerTableModel;
115: }
116:
117: private FileObject getFileObjectOfClass(String className) {
118: Sources sources = ProjectUtils.getSources(project);
119: SourceGroup[] sourceGroups = sources
120: .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
121: for (int i = 0; i < sourceGroups.length; i++) {
122: FileObject rootFolder = sourceGroups[i].getRootFolder();
123: FileObject classFileObject = rootFolder
124: .getFileObject(className.replaceAll("\\.", "/")
125: + ".java");
126: if (classFileObject != null) {
127: return classFileObject;
128: }
129: }
130: return null;
131: }
132:
133: private void populateHandlers() {
134: ListIterator<String> listIterator = handlerClasses
135: .listIterator();
136: final int[] handlerType = new int[] { WSHandlerDialog.JAXWS_LOGICAL_HANDLER };
137: while (listIterator.hasNext()) {
138: String handlerClass = listIterator.next();
139: CancellableTask<CompilationController> task = new CancellableTask<CompilationController>() {
140:
141: public void run(CompilationController controller)
142: throws IOException {
143: controller.toPhase(Phase.ELEMENTS_RESOLVED);
144: handlerType[0] = WSHandlerDialog.getHandlerType(
145: controller, isJaxWS);
146: }
147:
148: public void cancel() {
149: }
150: };
151:
152: FileObject classFO = getFileObjectOfClass(handlerClass);
153: if (classFO != null) {
154: try {
155: JavaSource javaSource = JavaSource
156: .forFileObject(classFO);
157: javaSource.runUserActionTask(task, true);
158: } catch (IOException ex) {
159: ErrorManager.getDefault().notify(ex);
160: }
161: }
162: if (handlerType[0] == WSHandlerDialog.JAXWS_LOGICAL_HANDLER) {
163: protocolIndex++;
164: }
165: handlerTableModel.addRow(new Object[] { handlerClass,
166: handlerType[0] });
167: }
168: if (handlerTableModel.getRowCount() > 0) {
169: ((ListSelectionModel) handlerTable.getSelectionModel())
170: .setSelectionInterval(0, 0);
171: }
172: }
173:
174: class RemoveButtonActionListener implements ActionListener {
175:
176: public void actionPerformed(ActionEvent e) {
177: int selectedRow = getSelectedRow();
178: if (selectedRow == -1) {
179: return;
180: }
181: String className = (String) handlerTableModel.getValueAt(
182: selectedRow, 0);
183: if (confirmDeletion(className)) {
184: Integer type = (Integer) handlerTableModel.getValueAt(
185: selectedRow, 1);
186: if (type == WSHandlerDialog.JAXWS_LOGICAL_HANDLER) {
187: --protocolIndex;
188: }
189: handlerTableModel.removeRow(selectedRow);
190: int newSelectedRow = selectedRow - 1;
191: handlerTable.getSelectionModel().setSelectionInterval(
192: newSelectedRow, newSelectedRow);
193: isChanged = true;
194: }
195: }
196:
197: private boolean confirmDeletion(String className) {
198: NotifyDescriptor.Confirmation notifyDesc = new NotifyDescriptor.Confirmation(
199: NbBundle.getMessage(MessageHandlerPanel.class,
200: "MSG_CONFIRM_DELETE", className,
201: componentName), NbBundle.getMessage(
202: MessageHandlerPanel.class,
203: "TTL_CONFIRM_DELETE"),
204: NotifyDescriptor.YES_NO_OPTION);
205: DialogDisplayer.getDefault().notify(notifyDesc);
206: return notifyDesc.getValue() == NotifyDescriptor.YES_OPTION;
207: }
208: }
209:
210: class AddButtonActionListener implements ActionListener {
211:
212: DialogDescriptor dlgDesc = null;
213:
214: public void actionPerformed(ActionEvent evt) {
215: WSHandlerDialog wsHandlerDialog = new WSHandlerDialog(
216: project, isJaxWS);
217: wsHandlerDialog.show();
218: if (wsHandlerDialog.okButtonPressed()) {
219: Map<String, Integer> selectedClasses = wsHandlerDialog
220: .getSelectedClasses();
221: if (selectedClasses.size() > 0) {
222: int newSelectedRow = 0;
223: Set<Map.Entry<String, Integer>> classes = selectedClasses
224: .entrySet();
225: for (Map.Entry<String, Integer> selectedClass : classes) {
226: Integer type = selectedClasses
227: .get(selectedClass.getKey());
228: if (type == WSHandlerDialog.JAXWS_LOGICAL_HANDLER) {
229: handlerTableModel.insertRow(
230: ++protocolIndex, new Object[] {
231: selectedClass.getKey(),
232: type });
233: newSelectedRow = protocolIndex;
234: } else {
235: handlerTableModel.addRow(new Object[] {
236: selectedClass.getKey(), type });
237: newSelectedRow = handlerTableModel
238: .getRowCount() - 1;
239: }
240: }
241: handlerTable.getSelectionModel()
242: .setSelectionInterval(newSelectedRow,
243: newSelectedRow);
244: isChanged = true;
245: }
246: }
247: }
248: }
249:
250: class HandlerTable extends JTable {
251:
252: public HandlerTable() {
253: JTableHeader header = getTableHeader();
254: header.setResizingAllowed(false);
255: header.setReorderingAllowed(false);
256: ListSelectionModel model = getSelectionModel();
257: model
258: .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
259: model
260: .addListSelectionListener(new HandlerListSelectionListener());
261: }
262: }
263:
264: class HandlerListSelectionListener implements ListSelectionListener {
265:
266: public void valueChanged(ListSelectionEvent e) {
267: if (!e.getValueIsAdjusting()) {
268: int selectedRow = getSelectedRow();
269: if (selectedRow == 0) {
270: upBtn.setEnabled(false);
271: } else {
272: if (!upBtn.isEnabled()) {
273: upBtn.setEnabled(true);
274: }
275: }
276: if (selectedRow == handlerTableModel.getRowCount() - 1) {
277: downBtn.setEnabled(false);
278: } else {
279: if (!downBtn.isEnabled()) {
280: downBtn.setEnabled(true);
281: }
282: }
283: }
284: }
285: }
286:
287: /** This method is called from within the constructor to
288: * initialize the form.
289: * WARNING: Do NOT modify this code. The content of this method is
290: * always regenerated by the Form Editor.
291: */
292: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
293: private void initComponents() {
294:
295: addBtn = new javax.swing.JButton();
296: removeBtn = new javax.swing.JButton();
297: upBtn = new javax.swing.JButton();
298: downBtn = new javax.swing.JButton();
299: jScrollPane2 = new javax.swing.JScrollPane();
300: handlerTable = new HandlerTable();
301:
302: java.util.ResourceBundle bundle = java.util.ResourceBundle
303: .getBundle("org/netbeans/modules/websvc/core/webservices/ui/panels/Bundle"); // NOI18N
304: org.openide.awt.Mnemonics.setLocalizedText(addBtn, bundle
305: .getString("Add_DotDotDot_label")); // NOI18N
306: addBtn.setToolTipText(org.openide.util.NbBundle.getMessage(
307: MessageHandlerPanel.class, "HINT_Add")); // NOI18N
308:
309: org.openide.awt.Mnemonics.setLocalizedText(removeBtn, bundle
310: .getString("Remove_label")); // NOI18N
311: removeBtn.setToolTipText(org.openide.util.NbBundle.getMessage(
312: MessageHandlerPanel.class, "HINT_Remove")); // NOI18N
313:
314: org.openide.awt.Mnemonics.setLocalizedText(upBtn,
315: org.openide.util.NbBundle.getMessage(
316: MessageHandlerPanel.class, "LBL_Move_Up")); // NOI18N
317: upBtn.setToolTipText(org.openide.util.NbBundle.getMessage(
318: MessageHandlerPanel.class, "HINT_Move_Up")); // NOI18N
319: upBtn.addActionListener(new java.awt.event.ActionListener() {
320: public void actionPerformed(java.awt.event.ActionEvent evt) {
321: moveUpHandler(evt);
322: }
323: });
324:
325: org.openide.awt.Mnemonics.setLocalizedText(downBtn,
326: org.openide.util.NbBundle.getMessage(
327: MessageHandlerPanel.class, "LBL_Move_Down")); // NOI18N
328: downBtn.setToolTipText(org.openide.util.NbBundle.getMessage(
329: MessageHandlerPanel.class, "HINT_Move_Down")); // NOI18N
330: downBtn.addActionListener(new java.awt.event.ActionListener() {
331: public void actionPerformed(java.awt.event.ActionEvent evt) {
332: moveDownHandler(evt);
333: }
334: });
335:
336: handlerTableModel = new HandlerTableModel(new String[] {
337: NbBundle.getMessage(MessageHandlerPanel.class,
338: "HEADING_HANDLERS"),
339: NbBundle.getMessage(MessageHandlerPanel.class,
340: "HEADING_TYPE") }, 0);
341: handlerTable.setBorder(javax.swing.BorderFactory
342: .createLineBorder(new java.awt.Color(0, 0, 0)));
343: handlerTable.setModel(handlerTableModel);
344: jScrollPane2.setViewportView(handlerTable);
345: handlerTable.getAccessibleContext().setAccessibleName(
346: org.openide.util.NbBundle.getMessage(
347: MessageHandlerPanel.class,
348: "ACSD_MessageHandlerTable")); // NOI18N
349: handlerTable.getAccessibleContext().setAccessibleDescription(
350: org.openide.util.NbBundle.getMessage(
351: MessageHandlerPanel.class,
352: "ACSD_MessageHandlerTable")); // NOI18N
353:
354: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
355: this );
356: this .setLayout(layout);
357: layout
358: .setHorizontalGroup(layout
359: .createParallelGroup(
360: org.jdesktop.layout.GroupLayout.LEADING)
361: .add(
362: layout
363: .createSequentialGroup()
364: .addContainerGap()
365: .add(
366: jScrollPane2,
367: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
368: 364, Short.MAX_VALUE)
369: .addPreferredGap(
370: org.jdesktop.layout.LayoutStyle.RELATED)
371: .add(
372: layout
373: .createParallelGroup(
374: org.jdesktop.layout.GroupLayout.LEADING)
375: .add(
376: addBtn,
377: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
378: 95,
379: Short.MAX_VALUE)
380: .add(
381: upBtn,
382: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
383: 95,
384: Short.MAX_VALUE)
385: .add(
386: removeBtn,
387: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
388: 95,
389: Short.MAX_VALUE)
390: .add(
391: downBtn,
392: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
393: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
394: Short.MAX_VALUE))
395: .addContainerGap()));
396: layout
397: .setVerticalGroup(layout
398: .createParallelGroup(
399: org.jdesktop.layout.GroupLayout.LEADING)
400: .add(
401: layout
402: .createSequentialGroup()
403: .addContainerGap()
404: .add(
405: layout
406: .createParallelGroup(
407: org.jdesktop.layout.GroupLayout.TRAILING)
408: .add(
409: org.jdesktop.layout.GroupLayout.LEADING,
410: layout
411: .createSequentialGroup()
412: .add(
413: addBtn)
414: .addPreferredGap(
415: org.jdesktop.layout.LayoutStyle.RELATED)
416: .add(
417: removeBtn)
418: .add(
419: 23,
420: 23,
421: 23)
422: .add(
423: upBtn)
424: .addPreferredGap(
425: org.jdesktop.layout.LayoutStyle.RELATED)
426: .add(
427: downBtn))
428: .add(
429: org.jdesktop.layout.GroupLayout.LEADING,
430: jScrollPane2,
431: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
432: 219,
433: Short.MAX_VALUE))
434: .addContainerGap()));
435: }// </editor-fold>//GEN-END:initComponents
436:
437: private void moveUpHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveUpHandler
438: int selectedRow = getSelectedRow();
439: if (selectedRow == -1) {
440: return;
441: }
442: Integer type = (Integer) handlerTableModel.getValueAt(
443: selectedRow, 1);
444: if (type == WSHandlerDialog.JAXWS_MESSAGE_HANDLER) {
445: if ((selectedRow - 1) == protocolIndex) {
446: DialogDisplayer
447: .getDefault()
448: .notify(
449: new NotifyDescriptor.Message(
450: NbBundle
451: .getMessage(
452: MessageHandlerPanel.class,
453: "TXT_CannotMoveUp",
454: NotifyDescriptor.WARNING_MESSAGE)));
455: return;
456: }
457: }
458: int newSelectedRow = selectedRow - 1;
459: handlerTableModel.moveRow(selectedRow, selectedRow,
460: newSelectedRow);
461: handlerTable.getSelectionModel().setSelectionInterval(
462: newSelectedRow, newSelectedRow);
463: isChanged = true;
464: }//GEN-LAST:event_moveUpHandler
465:
466: private void moveDownHandler(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveDownHandler
467: int selectedRow = getSelectedRow();
468: if (selectedRow == -1) {
469: return;
470: }
471: Integer type = (Integer) handlerTableModel.getValueAt(
472: selectedRow, 1);
473: if (type == WSHandlerDialog.JAXWS_LOGICAL_HANDLER) {
474: if (selectedRow == protocolIndex) {
475: DialogDisplayer
476: .getDefault()
477: .notify(
478: new NotifyDescriptor.Message(
479: NbBundle
480: .getMessage(
481: MessageHandlerPanel.class,
482: "TXT_CannotMoveDown",
483: NotifyDescriptor.WARNING_MESSAGE)));
484: return;
485: }
486: }
487: int newSelectedRow = selectedRow + 1;
488: handlerTableModel.moveRow(selectedRow, selectedRow,
489: newSelectedRow);
490: handlerTable.getSelectionModel().setSelectionInterval(
491: newSelectedRow, newSelectedRow);
492:
493: isChanged = true;
494: }//GEN-LAST:event_moveDownHandler
495:
496: private int getSelectedRow() {
497: ListSelectionModel lsm = (ListSelectionModel) handlerTable
498: .getSelectionModel();
499: if (lsm.isSelectionEmpty()) {
500: return -1;
501: } else {
502: return lsm.getMinSelectionIndex();
503: }
504: }
505:
506: class HandlerTableModel extends DefaultTableModel {
507:
508: public HandlerTableModel(Object[] columnNames, int rowCount) {
509: super (columnNames, rowCount);
510: }
511:
512: @Override
513: public boolean isCellEditable(int row, int column) {
514: return false;
515: }
516: }
517:
518: class TypeCellRenderer implements TableCellRenderer {
519:
520: public Component getTableCellRendererComponent(JTable table,
521: Object value, boolean isSelected, boolean hasFocus,
522: int row, int column) {
523: JLabel typeLabel = new JLabel();
524: if (column == 1) {
525: Integer type = (Integer) value;
526: if (type != null) {
527: if (type == WSHandlerDialog.JAXWS_LOGICAL_HANDLER) {
528: typeLabel.setText(LOGICAL_TYPE);
529: } else {
530: typeLabel.setText(PROTOCOL_TYPE);
531: }
532: }
533: }
534: return typeLabel;
535: }
536: }
537:
538: // Variables declaration - do not modify//GEN-BEGIN:variables
539: private javax.swing.JButton addBtn;
540: private javax.swing.JButton downBtn;
541: private javax.swing.JTable handlerTable;
542: private javax.swing.table.DefaultTableModel handlerTableModel;
543: private javax.swing.JScrollPane jScrollPane2;
544: private javax.swing.JButton removeBtn;
545: private javax.swing.JButton upBtn;
546: // End of variables declaration//GEN-END:variables
547: }
|