KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tax > beans > editor > TreeElementAttributeListCustomizer


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.tax.beans.editor;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.event.KeyAdapter JavaDoc;
23 import java.awt.event.MouseAdapter JavaDoc;
24 import java.awt.event.MouseEvent JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26 import java.beans.Customizer JavaDoc;
27 import java.beans.PropertyChangeEvent JavaDoc;
28 import java.beans.PropertyChangeListener JavaDoc;
29 import javax.swing.event.ListSelectionEvent JavaDoc;
30 import javax.swing.event.TableModelEvent JavaDoc;
31 import javax.swing.event.ListSelectionListener JavaDoc;
32 import javax.swing.JPanel JavaDoc;
33 import javax.swing.JLabel JavaDoc;
34 import javax.swing.UIManager JavaDoc;
35 import javax.swing.JTable JavaDoc;
36 import javax.swing.ListSelectionModel JavaDoc;
37 import javax.swing.DefaultCellEditor JavaDoc;
38 import javax.swing.JTextField JavaDoc;
39 import javax.swing.table.TableColumn JavaDoc;
40 import javax.swing.table.JTableHeader JavaDoc;
41 import javax.swing.table.AbstractTableModel JavaDoc;
42 import javax.swing.table.TableCellRenderer JavaDoc;
43 import javax.swing.table.TableColumnModel JavaDoc;
44
45 import org.netbeans.tax.TreeNamedObjectMap;
46 import org.netbeans.tax.TreeAttribute;
47 import org.netbeans.tax.TreeName;
48 import org.netbeans.tax.TreeException;
49
50 import org.netbeans.modules.xml.tax.beans.TreeObjectListProxyListener;
51 import org.netbeans.modules.xml.tax.beans.Lib;
52 import org.netbeans.modules.xml.tax.util.TAXUtil;
53
54 /**
55  * Table oriented customizer of TreeElement attribute list.
56  *
57  * @author Petr Kuzel
58  * @author Vladimir Zboril
59  * @author Libor Kramolis
60  * @version 1.0
61  */

62 public class TreeElementAttributeListCustomizer extends JPanel JavaDoc implements Customizer JavaDoc, PropertyChangeListener JavaDoc {
63     
64     /** Serial Version UID */
65     private static final long serialVersionUID = 1071471854210683733L;
66     
67     private String JavaDoc headerToolTip;
68     
69     private final int COL_NAME = 0;
70     private final int COL_VALUE = 1;
71     private final int COL_COUNT = 2;
72     
73     //
74
// init
75
//
76

77     /** Creates new form TreeElementAttributeListCustomizer */
78     
79     public TreeElementAttributeListCustomizer() {
80         
81         headerToolTip = Util.THIS.getString("PROP_headerTooltip");
82         initComponents ();
83         //mnemonics
84
upButton.setMnemonic(Util.THIS.getChar("MNE_element_attributelist_up")); // NOI18N
85
downButton.setMnemonic(Util.THIS.getChar("MNE_element_attributelist_down")); // NOI18N
86
removeButton.setMnemonic(Util.THIS.getChar("MNE_element_attributelist_remove")); // NOI18N
87
addButton.setMnemonic(Util.THIS.getChar("MNE_element_attributelist_add")); // NOI18N
88
initAccessibility();
89         
90         // Add custom header renderer supporting sorting
91
attrTable.getTableHeader().setDefaultRenderer(new HeaderRenderer());
92
93         // Cells should become editable on single mouse click
94
final JTextField JavaDoc editorComponent = new JTextField JavaDoc();
95         editorComponent.getCaret().setVisible(true);
96         final DefaultCellEditor JavaDoc singleClickEditor = new DefaultCellEditor JavaDoc(editorComponent);
97         singleClickEditor.setClickCountToStart(1);
98         attrTable.setDefaultEditor(String JavaDoc.class, singleClickEditor);
99         
100         // Set ListSelectionModel
101
attrTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
102         ListSelectionModel JavaDoc rowsm = attrTable.getSelectionModel();
103         rowsm.addListSelectionListener (new ListSelectionListener JavaDoc(){
104                 public void valueChanged (ListSelectionEvent JavaDoc e) {
105                     if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\n#=- TreeElementAttributeListCustomizer::ListSelectionListener.valueChanged: event = " + e);
106                     if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("#=- event.getValueIsAdjusting() = " + e.getValueIsAdjusting());
107                     if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("#=- event.getFirstIndex() = " + e.getFirstIndex());
108                     if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("#=- event.getLastIndex() = " + e.getLastIndex());
109
110                     if (e.getValueIsAdjusting())
111                         return;
112                     ListSelectionModel JavaDoc lsm = (ListSelectionModel JavaDoc)e.getSource();
113
114                     if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("#=- event.getSource() = " + lsm);
115                     if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("#=- selectionModel.isSelectionEmpty() = " + lsm.isSelectionEmpty());
116                     if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("#=- attrTable.getSelectedRow() = " + attrTable.getSelectedRow());
117                     if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("#=- attrTable.getRowCount() = " + attrTable.getRowCount());
118
119                     if ( lsm.isSelectionEmpty() ||
120                          ( attrTable.getRowCount() == 0 ) ) {
121                         upButton.setEnabled (false);
122                         downButton.setEnabled (false);
123                         removeButton.setEnabled (false);
124                     } else {
125                         upButton.setEnabled (attrTable.getSelectedRow() > 0);
126                         downButton.setEnabled (attrTable.getSelectedRow() < (numRows() - 1));
127                         removeButton.setEnabled (true);
128                     }
129                 }
130             });
131         
132         //Click to header - sorting
133
attrTable.getTableHeader().addMouseListener(new MouseAdapter JavaDoc() {
134                 public void mouseClicked(MouseEvent JavaDoc e) {
135                     TableColumnModel JavaDoc colModel = attrTable.getColumnModel();
136                     int columnIndex = colModel.getColumnIndexAtX(e.getX());
137                     // No column was clicked.
138
if(columnIndex < 0) return;
139                     int modelIndex = colModel.getColumn(columnIndex).getModelIndex();
140                     // not detected column
141
if (modelIndex < 0) return;
142                     // Do sort !
143
AttlistTableModel tm = (AttlistTableModel) attrTable.getModel();
144                     tm.sortByColumn(columnIndex);
145                 }
146             });
147         
148     }
149     
150     
151     // Get count of rows
152
private int numRows(){
153         return attrTable.getModel().getRowCount();
154     }
155     
156     //Get count of columns
157
private int numCols(){
158         return attrTable.getModel().getColumnCount();
159     }
160     
161     //
162
// itself
163
//
164

165     /** This method is called from within the constructor to
166      * initialize the form.
167      * WARNING: Do NOT modify this code. The content of this method is
168      * always regenerated by the FormEditor.
169      */

170     private void initComponents() {//GEN-BEGIN:initComponents
171
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
172
173         tableScrollPane = new javax.swing.JScrollPane JavaDoc();
174         attrTable = new javax.swing.JTable JavaDoc();
175         upButton = new javax.swing.JButton JavaDoc();
176         downButton = new javax.swing.JButton JavaDoc();
177         addButton = new javax.swing.JButton JavaDoc();
178         removeButton = new javax.swing.JButton JavaDoc();
179
180         setLayout(new java.awt.GridBagLayout JavaDoc());
181
182         setPreferredSize(new java.awt.Dimension JavaDoc(350, 230));
183         attrTable.setPreferredScrollableViewportSize(new java.awt.Dimension JavaDoc(200, 150));
184         tableScrollPane.setViewportView(attrTable);
185
186         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
187         gridBagConstraints.gridx = 0;
188         gridBagConstraints.gridy = 0;
189         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
190         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
191         gridBagConstraints.weightx = 1.0;
192         gridBagConstraints.weighty = 1.0;
193         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 0);
194         add(tableScrollPane, gridBagConstraints);
195
196         upButton.setText(Util.THIS.getString ("TEXT_element_attributelist_up"));
197         upButton.setEnabled(false);
198         upButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
199             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
200                 upButtonActionPerformed(evt);
201             }
202         });
203
204         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
205         gridBagConstraints.gridx = 1;
206         gridBagConstraints.gridy = 0;
207         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
208         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
209         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
210         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 11);
211         add(upButton, gridBagConstraints);
212
213         downButton.setText(Util.THIS.getString ("TEXT_element_attributelist_down"));
214         downButton.setEnabled(false);
215         downButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
216             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
217                 downButtonActionPerformed(evt);
218             }
219         });
220
221         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
222         gridBagConstraints.gridx = 1;
223         gridBagConstraints.gridy = 1;
224         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
225         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
226         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
227         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 11);
228         add(downButton, gridBagConstraints);
229
230         addButton.setText(Util.THIS.getString ("TEXT_element_attributelist_add"));
231         addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
232             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
233                 addButtonActionPerformed(evt);
234             }
235         });
236
237         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
238         gridBagConstraints.gridx = 1;
239         gridBagConstraints.gridy = 2;
240         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
241         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
242         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
243         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 11);
244         add(addButton, gridBagConstraints);
245
246         removeButton.setText(Util.THIS.getString ("TEXT_element_attributelist_remove"));
247         removeButton.setEnabled(false);
248         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
249             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
250                 removeButtonActionPerformed(evt);
251             }
252         });
253
254         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
255         gridBagConstraints.gridx = 1;
256         gridBagConstraints.gridy = 3;
257         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
258         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
259         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
260         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
261         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 11);
262         add(removeButton, gridBagConstraints);
263
264     }//GEN-END:initComponents
265

266     /**
267      * The button at right side was pressed
268      */

269     private void addButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
270

271         AttlistTableModel tm = (AttlistTableModel) attrTable.getModel();
272         tm.addRow();
273         int actualIndex = numRows() - 1;
274         attrTable.getSelectionModel().setSelectionInterval(actualIndex, actualIndex);
275     }//GEN-LAST:event_addButtonActionPerformed
276

277     /**
278      * The button at right side was pressed
279      */

280     private void upButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_upButtonActionPerformed
281

282         AttlistTableModel tm = (AttlistTableModel) attrTable.getModel();
283         int index = attrTable.getSelectedRow();
284         if (index > 0) {
285             tm.moveRow(index, index - 1);
286             attrTable.getSelectionModel().setSelectionInterval(index - 1,index - 1);
287         }
288     }//GEN-LAST:event_upButtonActionPerformed
289

290     /**
291      * The button at right side was pressed
292      */

293     private void removeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
294

295         AttlistTableModel tm = (AttlistTableModel) attrTable.getModel();
296         
297         int sel = attrTable.getSelectedRow();
298         if (sel > -1) {
299             tm.removeRow(sel);
300             if (numRows() > 0) {
301                 if (sel <= numRows() - 1)
302                     attrTable.getSelectionModel().setSelectionInterval(sel,sel);
303                 else
304                     attrTable.getSelectionModel().setSelectionInterval(sel - 1, sel - 1);
305             } else removeButton.setEnabled(false);
306         }
307     }//GEN-LAST:event_removeButtonActionPerformed
308

309     /**
310      * The button at right side was pressed
311      */

312     private void downButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_downButtonActionPerformed
313

314         AttlistTableModel tm = (AttlistTableModel) attrTable.getModel();
315         int index = attrTable.getSelectedRow();
316         if (index > -1 && index < numRows() - 1) {
317             tm.moveRow(index, index + 1);
318             attrTable.getSelectionModel().setSelectionInterval(index + 1,index + 1);
319         }
320     }//GEN-LAST:event_downButtonActionPerformed
321

322     
323     // Variables declaration - do not modify//GEN-BEGIN:variables
324
private javax.swing.JTable JavaDoc attrTable;
325     private javax.swing.JButton JavaDoc addButton;
326     private javax.swing.JScrollPane JavaDoc tableScrollPane;
327     private javax.swing.JButton JavaDoc downButton;
328     private javax.swing.JButton JavaDoc upButton;
329     private javax.swing.JButton JavaDoc removeButton;
330     // End of variables declaration//GEN-END:variables
331

332     
333     private TreeNamedObjectMap peer;
334     private AttlistTableModel tableModel;
335     
336     /**
337      */

338     public void setObject (Object JavaDoc obj) {
339         peer = (TreeNamedObjectMap) obj;
340         tableModel = new AttlistTableModel(/*peer*/);
341         attrTable.setModel(tableModel);
342 // we do not "delete" column anymore
343
// attrTable.addKeyListener(new RowKeyListener(attrTable));
344

345         /** First table column is row selector. */
346         TableColumn JavaDoc column = null;
347         for (int i = 0; i < COL_COUNT; i++) {
348             column = attrTable.getColumnModel().getColumn (i);
349             //column.setPreferredWidth (50);
350
}
351         
352         updateView();
353         
354         TreeObjectListProxyListener proxy = new TreeObjectListProxyListener(peer);
355         proxy.addPropertyChangeListener(org.openide.util.WeakListeners.propertyChange(this, proxy));
356     }
357     
358     
359     /** Udate state accordingly*/
360     public void propertyChange(final PropertyChangeEvent JavaDoc e) {
361         if (e.getSource() == null)
362             return;
363         
364         updateView();
365     }
366     
367     /** Update visualization accordingly. */
368     private void updateView () {
369         tableModel.fireTableDataChanged();
370     }
371     
372     //
373
// class RowKeyListener
374
//
375

376     /**
377      * Deletes whole row by pressing DELETE on row column.
378      * Unused...
379      */

380     private class RowKeyListener extends KeyAdapter JavaDoc {
381         
382         /** */
383         private JTable JavaDoc table;
384         
385         
386         //
387
// init
388
//
389

390         public RowKeyListener (JTable JavaDoc table) {
391             this.table = table;
392         }
393         
394         
395         //
396
// itself
397
//
398

399         /**
400          */

401         public void keyReleased (KeyEvent JavaDoc e) {
402             //if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Event: " + e); // NOI18N
403
if (e.getKeyCode() == KeyEvent.VK_DELETE) {
404                 tableModel.removeRow (table.getSelectedRow());
405 // peer.remove (peer.get (table.getSelectedRow()));
406
// tableModel.fireTableDataChanged();
407
}
408         }
409     }
410     
411     
412     //
413
// class AttlistTableModel
414
//
415

416     /**
417      *
418      */

419     private class AttlistTableModel extends AbstractTableModel JavaDoc {
420         
421         /** Serial Version UID */
422         private static final long serialVersionUID = 1567143493464383838L;
423         
424         boolean ascending = true;
425         int SortedColumn = -1;
426         int indexes[];
427         
428         //
429
// init
430
//
431

432         /** Create a data node for a given data object.
433          * The provided children object will be used to hold all child nodes.
434          * @param obj object to work with
435          * @param ch children container for the node
436          */

437         public AttlistTableModel () {
438             super();
439         }
440         
441         /** Returns the number of rows in the model */
442         public int getRowCount () {
443             return peer.size();
444         }
445         
446         
447         /** Returns the number of columns in the model */
448         public int getColumnCount () {
449             return COL_COUNT;
450         }
451         
452         /** Returns the class for a model. */
453         public Class JavaDoc getColumnClass (int index) {
454             return String JavaDoc.class;
455         }
456         
457         
458 // // Get unique attribute name for added blank rows
459
// private String getUniqueName(){
460
// int index = 0, i = 0;
461
// final String prefix = "attribute-"; // NOI18N
462
// String result = prefix + Integer.toString(index);
463
// while (i<numRows()){
464
// if (result.equals(attrTable.getValueAt(i,0))) {
465
// result = prefix + Integer.toString(++index);
466
// i = 0;
467
// } else ++i;
468
// }
469
// return result;
470
// }
471

472         /**
473          */

474         public void addRow () {
475             
476             TreeAttribute attr = (TreeAttribute) Lib.createAttributeDialog(true);
477             if (attr != null) {
478                 boolean toSet = true;
479                 TreeAttribute oldAttribute = (TreeAttribute) peer.get (attr.getTreeName());
480                 if ( oldAttribute != null ) {
481                     toSet = Lib.confirmAction
482                         (Util.THIS.getString ("MSG_replace_attribute", attr.getQName()));
483                 }
484                 if ( toSet ) {
485                     peer.add (attr);
486                     SortedColumn = -1;
487                     fireTableStructureChanged();
488                 }
489             }
490             /*
491               try {
492               peer.add (new TreeAttribute (getUniqueName(), ""));
493               SortedColumn = -1;
494               fireTableStructureChanged();
495               } catch (TreeException exc) {
496               TAXUtil.notifyTreeException (exc);
497               }
498             */

499         }
500         
501         /**
502          */

503         public void removeRow (int row) {
504             peer.remove(row);
505             fireTableStructureChanged();
506         }
507
508         /**
509          */

510         public void moveRow (int fromIndex, int toIndex) {
511             try {
512                 peer.switchObjects (fromIndex, toIndex);
513
514                 SortedColumn = -1;
515                 fireTableStructureChanged();
516             } catch (TreeException exc) {
517                 TAXUtil.notifyTreeException (exc);
518             }
519         };
520         
521         /**
522          */

523         public Object JavaDoc getValueAt (int row, int column) {
524             TreeAttribute attr = (TreeAttribute) peer.get(row);
525             switch (column) {
526             case COL_NAME:
527                 return attr.getQName();
528             case COL_VALUE:
529                 return attr.getValue();
530             default:
531                 return null;
532             }
533         }
534         
535         /**
536          */

537         public void setValueAt (Object JavaDoc val, int row, int column) {
538             TreeAttribute attr = (TreeAttribute) peer.get (row);
539             try {
540                 if (column == COL_NAME) {
541
542                     String JavaDoc attrName = (String JavaDoc) val;
543
544                     boolean toSet = true;
545                     TreeAttribute oldAttribute = (TreeAttribute) peer.get (new TreeName (attrName));
546                     if ( attr != oldAttribute ) {
547                         if ( oldAttribute != null ) {
548                             toSet = Lib.confirmAction
549                                 (Util.THIS.getString ("MSG_replace_attribute", attrName));
550                         }
551                     }
552                     if ( toSet ) {
553                         attr.setQName (attrName);
554                     }
555                     
556                 } else if (column == COL_VALUE) {
557                     attr.setValue ((String JavaDoc) val);
558                 }
559             } catch (TreeException exc) {
560                 TAXUtil.notifyTreeException (exc);
561             }
562         }
563         
564         /**
565          */

566         public String JavaDoc getColumnName (int column) {
567             switch (column) {
568             case COL_NAME:
569                 return Util.THIS.getString ("NAME_column_name");
570             case COL_VALUE:
571                 return Util.THIS.getString ("NAME_column_value");
572             default:
573                 return ""; // NOI18N
574
}
575         }
576         
577         /** Returns true for all cells which are editable. For a
578          * a new cell is editable only name field.
579          */

580         public boolean isCellEditable (int rowIndex, int columnIndex) {
581             return true;
582         }
583         
584         /**
585          */

586         public void sortByColumn (int column) {
587             
588             if (SortedColumn == column)
589                 ascending = !ascending;
590             else {
591                 SortedColumn = column;
592                 ascending = true;
593             }
594             
595             indexes = new int[getRowCount()];
596             for (int row = 0; row < getRowCount(); row++) {
597                 indexes[row] = row;
598             }
599             
600             n2sort(column);
601             
602             int[] indx = new int[indexes.length];
603             for (int row = 0; row < indx.length; row++) {
604                 indx[indexes[row]] = row;
605             }
606             
607             try {
608                 peer.reorder (indx);
609                 fireTableChanged(new TableModelEvent JavaDoc(this));
610             } catch (TreeException exc) {
611                 TAXUtil.notifyTreeException (exc);
612             }
613         }
614         
615         /**
616          */

617         private void n2sort (int col) {
618             for (int i = 0; i < getRowCount(); i++) {
619                 for (int j = i + 1; j < getRowCount(); j++) {
620                     if (compare(indexes[i], indexes[j], col) == -1) {
621                         // swap it
622
int tmp = indexes[i];
623                         indexes[i] = indexes[j];
624                         indexes[j] = tmp;
625                     }
626                 }
627             }
628         }
629         
630         /**
631          */

632         private int compare (int row1, int row2, int col) {
633             int result = compareRowsByColumn(row1, row2, col);
634             if (result != 0) {
635                 return ascending ? -result : result;
636             }
637             return 0;
638         }
639         
640         /**
641          */

642         private int compareRowsByColumn (int row1, int row2, int column) {
643             
644             Class JavaDoc type = getColumnClass(column);
645             
646             Object JavaDoc o1 = getValueAt(row1, column);
647             Object JavaDoc o2 = getValueAt(row2, column);
648             
649             
650             // If both values are null, return 0.
651

652             if (o1 == null && o2 == null) {
653                 return 0;
654             } else if (o1 == null) {
655                 return -1;
656             } else if (o2 == null) {
657                 return 1;
658             }
659             
660             // Compare String
661

662             if (type == String JavaDoc.class) {
663                 String JavaDoc s1 = (String JavaDoc) getValueAt(row1, column);
664                 String JavaDoc s2 = (String JavaDoc) getValueAt(row2, column);
665                 int result = s1.compareTo(s2);
666                 
667                 if (result < 0) {
668                     return -1;
669                 } else if (result > 0) {
670                     return 1;
671                 } else {
672                     return 0;
673                 }
674             }
675             return 0;
676         }
677         
678     } // end: class AttlistTableModel
679

680     
681     //
682
// Header renderer
683
//
684

685     private class HeaderRenderer extends JLabel JavaDoc implements TableCellRenderer JavaDoc {
686         
687         /** Serial Version UID */
688         private static final long serialVersionUID =-3658206203140258583L;
689         
690         public HeaderRenderer() {
691             super();
692             setHorizontalAlignment(JLabel.LEFT);
693             setBorder(UIManager.getBorder("TableHeader.cellBorder")); // NOI18N
694
setToolTipText(headerToolTip);
695         }
696         
697         public Component JavaDoc getTableCellRendererComponent (JTable JavaDoc table, Object JavaDoc value,
698                                                         boolean isSelected, boolean hasFocus,int row, int column) {
699             
700             if (table != null) {
701                 JTableHeader JavaDoc header = table.getTableHeader();
702                 if (header != null) {
703                     setForeground(header.getForeground());
704                     setBackground(header.getBackground());
705                     setFont(header.getFont());
706                 }
707             }
708             
709             AttlistTableModel tm = (AttlistTableModel)table.getModel();
710             if (column == tm.SortedColumn)
711                 setIcon(tm.ascending ? new javax.swing.ImageIcon JavaDoc(getClass().getResource("down.gif")): // NOI18N
712
new javax.swing.ImageIcon JavaDoc(getClass().getResource("up.gif"))); // NOI18N
713
else setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("no.gif"))); // NOI18N
714

715             setText((value == null) ? "" : value.toString()); // NOI18N
716
return this;
717         }
718     } // end: class HeaderRenderer
719

720     /** Initialize accesibility
721      */

722     public void initAccessibility(){
723         
724        this.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_TreeElementAttributeListCustomizer"));
725
726        addButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_addButton"));
727        removeButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_removeButton"));
728        upButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_upButton"));
729        downButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_downButton"));
730        
731        attrTable.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_attrTable"));
732        attrTable.getAccessibleContext().setAccessibleName(Util.THIS.getString("ACSN_attrTable"));
733     }
734     
735 }
736
Popular Tags