1 19 20 25 26 package org.netbeans.modules.xml.wsdl.ui.view; 27 28 import java.awt.Point ; 29 import java.awt.Rectangle ; 30 import java.awt.event.FocusEvent ; 31 import java.awt.event.FocusListener ; 32 import java.util.HashMap ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 import javax.swing.JViewport ; 37 import javax.swing.event.ListSelectionEvent ; 38 import javax.swing.event.ListSelectionListener ; 39 import javax.swing.event.TableModelEvent ; 40 import javax.swing.event.TableModelListener ; 41 import javax.swing.table.TableColumn ; 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 51 public class CommonMessageConfigurationPanel extends javax.swing.JPanel { 52 53 private Map <String , String > namespaceToPrefixMap; 54 private Project mProject; 55 56 private ElementOrTypeTableCellRenderer elementOrTypeRenderer; 57 private WSDLModel mModel; 58 59 60 public CommonMessageConfigurationPanel(Project project, Map <String , String > namespaceToPrefixMap, WSDLModel model) { 61 mProject = project; 62 this.namespaceToPrefixMap = namespaceToPrefixMap; 63 mModel = model; 64 initComponents(); 65 initGUI(); 66 } 67 68 70 public CommonMessageConfigurationPanel() { 71 namespaceToPrefixMap = new HashMap <String , String >(); 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 89 private void initComponents() { 91 jPanel1 = new javax.swing.JPanel (); 92 partsScrollPane = new javax.swing.JScrollPane (); 93 partsTable = new javax.swing.JTable (); 94 addButton = new javax.swing.JButton (); 95 removeButton = new javax.swing.JButton (); 96 97 partsScrollPane.setAutoscrolls(true); 98 partsTable.setModel(new javax.swing.table.DefaultTableModel ( 99 new Object [][] { 100 {null, null}, 101 {null, null} 102 }, 103 new String [] { 104 "Message Part Name", "Element Or Type" 105 } 106 ) { 107 Class [] types = new Class [] { 108 java.lang.String .class, java.lang.Object .class 109 }; 110 111 public Class 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")); addButton.addActionListener(new java.awt.event.ActionListener () { 121 public void actionPerformed(java.awt.event.ActionEvent evt) { 122 addButtonActionPerformed(evt); 123 } 124 }); 125 126 org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getMessage(CommonMessageConfigurationPanel.class, "CommonMessageConfigurationPanel.removeButton.textNoMnemonic")); removeButton.setEnabled(false); 128 removeButton.addActionListener(new java.awt.event.ActionListener () { 129 public void actionPerformed(java.awt.event.ActionEvent 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 } 167 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { 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 } 179 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { PartAndElementOrTypeTableModel model = (PartAndElementOrTypeTableModel) partsTable.getModel(); 181 model.addNewRow(); 182 } 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 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 () { 196 public void valueChanged(ListSelectionEvent 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 elementOrTypeColumn) { 212 elementOrTypeColumn.setCellEditor(new ElementOrTypeTableCellEditor(partsTable, namespaceToPrefixMap, mProject, mModel)); 213 214 elementOrTypeRenderer = 216 new ElementOrTypeTableCellRenderer(); 217 elementOrTypeRenderer.setToolTipText(NbBundle.getMessage(CommonMessageConfigurationPanel.class, "CommonMessageConfigurationPanel.renderer.toolTipText")); 218 elementOrTypeColumn.setCellRenderer(elementOrTypeRenderer); 219 } 220 221 public List <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 )) { 237 return; 238 } 239 JViewport viewport = (JViewport )partsTable.getParent(); 240 241 Rectangle rect = partsTable.getCellRect(rowIndex, vColIndex, true); 244 245 Point pt = viewport.getViewPosition(); 247 248 rect.setLocation(rect.x-pt.x, rect.y-pt.y); 252 253 viewport.scrollRectToVisible(rect); 255 } 256 257 258 class TableModelChangeListener implements TableModelListener { 259 260 public void tableChanged(TableModelEvent 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 { 299 public void focusGained(FocusEvent 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 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 [] args) { 312 313 320 321 } 322 323 public void clearSelection() { 324 partsTable.clearSelection(); 325 } 326 327 private javax.swing.JButton addButton; 329 private javax.swing.JPanel jPanel1; 330 private javax.swing.JScrollPane partsScrollPane; 331 private javax.swing.JTable partsTable; 332 private javax.swing.JButton removeButton; 333 335 } 336 | Popular Tags |