KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > view > CommonMessageConfigurationPanel


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  * CommonMessageConfigurationPanel.java
22  *
23  * Created on August 25, 2006, 1:18 PM
24  */

25
26 package org.netbeans.modules.xml.wsdl.ui.view;
27
28 import java.awt.Point JavaDoc;
29 import java.awt.Rectangle JavaDoc;
30 import java.awt.event.FocusEvent JavaDoc;
31 import java.awt.event.FocusListener JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import javax.swing.JViewport JavaDoc;
37 import javax.swing.event.ListSelectionEvent JavaDoc;
38 import javax.swing.event.ListSelectionListener JavaDoc;
39 import javax.swing.event.TableModelEvent JavaDoc;
40 import javax.swing.event.TableModelListener JavaDoc;
41 import javax.swing.table.TableColumn JavaDoc;
42
43 import org.netbeans.api.project.Project;
44 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
45 import org.openide.util.NbBundle;
46
47 /**
48  *
49  * @author radval
50  */

51 public class CommonMessageConfigurationPanel extends javax.swing.JPanel JavaDoc {
52     
53     private Map JavaDoc<String JavaDoc, String JavaDoc> namespaceToPrefixMap;
54     private Project mProject;
55     
56     private ElementOrTypeTableCellRenderer elementOrTypeRenderer;
57     private WSDLModel mModel;
58     
59     /** Creates new form CommonMessageConfigurationPanel */
60     public CommonMessageConfigurationPanel(Project project, Map JavaDoc<String JavaDoc, String JavaDoc> namespaceToPrefixMap, WSDLModel model) {
61         mProject = project;
62         this.namespaceToPrefixMap = namespaceToPrefixMap;
63         mModel = model;
64         initComponents();
65         initGUI();
66     }
67     
68     /** Mattise require default constructor otherwise will not load in design view of mattise
69      **/

70     public CommonMessageConfigurationPanel() {
71         namespaceToPrefixMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
72         if (!namespaceToPrefixMap.containsKey("xsd")) {
73             namespaceToPrefixMap.put("http://www.w3.org/2001/XMLSchema", "xsd");
74         }
75         initComponents();
76         initGUI();
77     }
78     
79     public void addNewRow() {
80         PartAndElementOrTypeTableModel model = (PartAndElementOrTypeTableModel) partsTable.getModel();
81         model.addNewRow();
82     }
83     
84     /** This method is called from within the constructor to
85      * initialize the form.
86      * WARNING: Do NOT modify this code. The content of this method is
87      * always regenerated by the Form Editor.
88      */

89     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
90
private void initComponents() {
91         jPanel1 = new javax.swing.JPanel JavaDoc();
92         partsScrollPane = new javax.swing.JScrollPane JavaDoc();
93         partsTable = new javax.swing.JTable JavaDoc();
94         addButton = new javax.swing.JButton JavaDoc();
95         removeButton = new javax.swing.JButton JavaDoc();
96
97         partsScrollPane.setAutoscrolls(true);
98         partsTable.setModel(new javax.swing.table.DefaultTableModel JavaDoc(
99             new Object JavaDoc [][] {
100                 {null, null},
101                 {null, null}
102             },
103             new String JavaDoc [] {
104                 "Message Part Name", "Element Or Type"
105             }
106         ) {
107             Class JavaDoc[] types = new Class JavaDoc [] {
108                 java.lang.String JavaDoc.class, java.lang.Object JavaDoc.class
109             };
110
111             public Class JavaDoc getColumnClass(int columnIndex) {
112                 return types [columnIndex];
113             }
114         });
115         partsTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
116         partsTable.setSurrendersFocusOnKeystroke(true);
117         partsScrollPane.setViewportView(partsTable);
118
119         org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getMessage(CommonMessageConfigurationPanel.class, "CommonMessageConfigurationPanel.addButton.textNoMnemonic")); // NOI18N
120
addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
121             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
122                 addButtonActionPerformed(evt);
123             }
124         });
125
126         org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getMessage(CommonMessageConfigurationPanel.class, "CommonMessageConfigurationPanel.removeButton.textNoMnemonic")); // NOI18N
127
removeButton.setEnabled(false);
128         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
129             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
130                 removeButtonActionPerformed(evt);
131             }
132         });
133
134         org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
135         jPanel1.setLayout(jPanel1Layout);
136         jPanel1Layout.setHorizontalGroup(
137             jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
138             .add(org.jdesktop.layout.GroupLayout.TRAILING, partsScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
139             .add(jPanel1Layout.createSequentialGroup()
140                 .add(addButton)
141                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
142                 .add(removeButton)
143                 .add(272, 272, 272))
144         );
145         jPanel1Layout.setVerticalGroup(
146             jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
147             .add(jPanel1Layout.createSequentialGroup()
148                 .add(partsScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
149                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
150                 .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
151                     .add(addButton)
152                     .add(removeButton)))
153         );
154
155         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
156         this.setLayout(layout);
157         layout.setHorizontalGroup(
158             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
159             .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
160         );
161         layout.setVerticalGroup(
162             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
163             .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
164         );
165     }// </editor-fold>//GEN-END:initComponents
166

167     private void removeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
168
PartAndElementOrTypeTableModel model = (PartAndElementOrTypeTableModel) partsTable.getModel();
169         int[] rows = partsTable.getSelectedRows();
170         if(rows != null) {
171             for(int i = rows.length; i > 0; i--) {
172                 if (rows[i-1] < partsTable.getRowCount()) {
173                     model.removeSelectedRow(rows[i-1]);
174                 }
175             }
176         }
177     }//GEN-LAST:event_removeButtonActionPerformed
178

179     private void addButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
180
PartAndElementOrTypeTableModel model = (PartAndElementOrTypeTableModel) partsTable.getModel();
181         model.addNewRow();
182     }//GEN-LAST:event_addButtonActionPerformed
183

184     private void initGUI() {
185         PartAndElementOrTypeTableModel model = new PartAndElementOrTypeTableModel(namespaceToPrefixMap);
186         partsTable.setModel(model);
187         partsTable.getColumnModel().getColumn(0).setCellRenderer(new ElementOrTypeTableCellRenderer());
188         setUpElementOrTypeColumn(partsTable.getColumnModel().getColumn(1));
189         model.addTableModelListener(new TableModelChangeListener());
190         FocusListener JavaDoc fl = new PanelFocusListener();
191         partsTable.addFocusListener(fl);
192         addButton.addFocusListener(fl);
193         removeButton.addFocusListener(fl);
194         this.addFocusListener(fl);
195         partsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener JavaDoc() {
196             public void valueChanged(ListSelectionEvent JavaDoc e) {
197                 if (!e.getValueIsAdjusting()) {
198                     if (partsTable.getSelectedRowCount() > 0) {
199                         removeButton.setEnabled(true);
200                     } else {
201                         removeButton.setEnabled(false);
202                     }
203                 }
204             }
205         });
206         
207         partsTable.getTableHeader().setReorderingAllowed(false);
208     }
209     
210     
211     public void setUpElementOrTypeColumn(TableColumn JavaDoc elementOrTypeColumn) {
212         elementOrTypeColumn.setCellEditor(new ElementOrTypeTableCellEditor(partsTable, namespaceToPrefixMap, mProject, mModel));
213         
214         //Set up tool tips for the sport cells.
215
elementOrTypeRenderer =
216                 new ElementOrTypeTableCellRenderer();
217         elementOrTypeRenderer.setToolTipText(NbBundle.getMessage(CommonMessageConfigurationPanel.class, "CommonMessageConfigurationPanel.renderer.toolTipText"));
218         elementOrTypeColumn.setCellRenderer(elementOrTypeRenderer);
219     }
220     
221     public List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType> getPartAndElementOrType() {
222         PartAndElementOrTypeTableModel model = (PartAndElementOrTypeTableModel) partsTable.getModel();
223         return model.getPartAndElementOrType();
224     }
225     
226     public void setEnabled(boolean enable) {
227         super.setEnabled(enable);
228         addButton.setEnabled(enable);
229         removeButton.setEnabled(enable);
230         partsTable.setEnabled(enable);
231         partsScrollPane.setEnabled(enable);
232         
233     }
234     
235     public void scrollToVisible(int rowIndex, int vColIndex) {
236         if (!(partsTable.getParent() instanceof JViewport JavaDoc)) {
237             return;
238         }
239         JViewport JavaDoc viewport = (JViewport JavaDoc)partsTable.getParent();
240     
241         // This rectangle is relative to the table where the
242
// northwest corner of cell (0,0) is always (0,0).
243
Rectangle JavaDoc rect = partsTable.getCellRect(rowIndex, vColIndex, true);
244     
245         // The location of the viewport relative to the table
246
Point JavaDoc pt = viewport.getViewPosition();
247     
248         // Translate the cell location so that it is relative
249
// to the view, assuming the northwest corner of the
250
// view is (0,0)
251
rect.setLocation(rect.x-pt.x, rect.y-pt.y);
252     
253         // Scroll the area into view
254
viewport.scrollRectToVisible(rect);
255     }
256     
257     
258     class TableModelChangeListener implements TableModelListener JavaDoc {
259         
260         public void tableChanged(TableModelEvent JavaDoc e) {
261             PartAndElementOrTypeTableModel model = (PartAndElementOrTypeTableModel) e.getSource();
262             if (e.getType() == TableModelEvent.DELETE) {
263                 int firstRow = e.getFirstRow();
264                 int lastRow = e.getLastRow();
265                 int newRow = firstRow >= lastRow ? firstRow -1 : lastRow -1;
266                 if (newRow < 0) {
267                     newRow = 0;
268                 }
269                 if (newRow < model.getRowCount()) {
270                     partsTable.setRowSelectionInterval(newRow, newRow);
271                     scrollToVisible(newRow, 0);
272                 }
273             } else if (e.getType() == TableModelEvent.INSERT) {
274                 int rowCount = model.getRowCount();
275                 int newRow = rowCount - 1;
276                 if (newRow > -1) {
277                     partsTable.setRowSelectionInterval(newRow, newRow);
278                     scrollToVisible(newRow, 0);
279                 }
280             } else if (e.getType() == TableModelEvent.UPDATE) {
281                 int lastRow = e.getLastRow();
282                 
283                 boolean allColumns = e.getColumn() == TableModelEvent.ALL_COLUMNS;
284                 
285                 if (lastRow > -1 && lastRow < model.getRowCount()) {
286                     partsTable.setRowSelectionInterval(lastRow, lastRow);
287                     if (!allColumns) {
288                         partsTable.setColumnSelectionInterval(e.getColumn(), e.getColumn());
289                         scrollToVisible(lastRow, e.getColumn());
290                     }
291                 }
292             }
293             partsTable.requestFocus();
294         }
295     }
296     
297     
298     class PanelFocusListener implements FocusListener JavaDoc {
299         public void focusGained(FocusEvent JavaDoc e) {
300             org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getMessage(CommonMessageConfigurationPanel.class, "CommonMessageConfigurationPanel.addButton.text"));
301             org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getMessage(CommonMessageConfigurationPanel.class, "CommonMessageConfigurationPanel.removeButton.text"));
302         }
303         
304         public void focusLost(FocusEvent JavaDoc e) {
305             org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getMessage(CommonMessageConfigurationPanel.class, "CommonMessageConfigurationPanel.addButton.textNoMnemonic"));
306             org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getMessage(CommonMessageConfigurationPanel.class, "CommonMessageConfigurationPanel.removeButton.textNoMnemonic"));
307         }
308         
309     }
310     
311     public static void main(String JavaDoc[] args) {
312         
313 // JFrame frame = new JFrame();
314
// frame.getContentPane().setLayout(new BorderLayout());
315
// CommonMessageConfigurationPanel p = new CommonMessageConfigurationPanel();
316
// frame.getContentPane().add(p, BorderLayout.CENTER);
317
// frame.setSize(200, 200);
318
// frame.setVisible(true);
319

320         
321     }
322
323     public void clearSelection() {
324         partsTable.clearSelection();
325     }
326     
327     // Variables declaration - do not modify//GEN-BEGIN:variables
328
private javax.swing.JButton JavaDoc addButton;
329     private javax.swing.JPanel JavaDoc jPanel1;
330     private javax.swing.JScrollPane JavaDoc partsScrollPane;
331     private javax.swing.JTable JavaDoc partsTable;
332     private javax.swing.JButton JavaDoc removeButton;
333     // End of variables declaration//GEN-END:variables
334

335 }
336
Popular Tags