~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to xml/tax/src/org/netbeans/modules/xml/tax/beans/editor/TreeElementAttributeListCustomizer.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
package org.netbeans.modules.xml.tax.beans.editor;
 
42
 
 
43
import java.awt.Component;
 
44
import java.awt.event.KeyAdapter;
 
45
import java.awt.event.MouseAdapter;
 
46
import java.awt.event.MouseEvent;
 
47
import java.awt.event.KeyEvent;
 
48
import java.beans.Customizer;
 
49
import java.beans.PropertyChangeEvent;
 
50
import java.beans.PropertyChangeListener;
 
51
import javax.swing.event.ListSelectionEvent;
 
52
import javax.swing.event.TableModelEvent;
 
53
import javax.swing.event.ListSelectionListener;
 
54
import javax.swing.JPanel;
 
55
import javax.swing.JLabel;
 
56
import javax.swing.UIManager;
 
57
import javax.swing.JTable;
 
58
import javax.swing.ListSelectionModel;
 
59
import javax.swing.DefaultCellEditor;
 
60
import javax.swing.JTextField;
 
61
import javax.swing.table.TableColumn;
 
62
import javax.swing.table.JTableHeader;
 
63
import javax.swing.table.AbstractTableModel;
 
64
import javax.swing.table.TableCellRenderer;
 
65
import javax.swing.table.TableColumnModel;
 
66
 
 
67
import org.netbeans.tax.TreeNamedObjectMap;
 
68
import org.netbeans.tax.TreeAttribute;
 
69
import org.netbeans.tax.TreeName;
 
70
import org.netbeans.tax.TreeException;
 
71
 
 
72
import org.netbeans.modules.xml.tax.beans.TreeObjectListProxyListener;
 
73
import org.netbeans.modules.xml.tax.beans.Lib;
 
74
import org.netbeans.modules.xml.tax.util.TAXUtil;
 
75
 
 
76
/**
 
77
 * Table oriented customizer of TreeElement attribute list.
 
78
 *
 
79
 * @author  Petr Kuzel
 
80
 * @author  Vladimir Zboril
 
81
 * @author  Libor Kramolis
 
82
 * @version 1.0
 
83
 */
 
84
public class TreeElementAttributeListCustomizer extends JPanel implements Customizer, PropertyChangeListener {
 
85
    
 
86
    /** Serial Version UID */
 
87
    private static final long serialVersionUID = 1071471854210683733L;
 
88
    
 
89
    private String headerToolTip;
 
90
    
 
91
    private final int COL_NAME  = 0;
 
92
    private final int COL_VALUE = 1;
 
93
    private final int COL_COUNT = 2;
 
94
    
 
95
    //
 
96
    // init
 
97
    //
 
98
    
 
99
    /** Creates new form TreeElementAttributeListCustomizer */
 
100
    
 
101
    public TreeElementAttributeListCustomizer() {
 
102
        
 
103
        headerToolTip = Util.THIS.getString("PROP_headerTooltip");
 
104
        initComponents ();
 
105
        //mnemonics
 
106
        upButton.setMnemonic(Util.THIS.getChar("MNE_element_attributelist_up")); // NOI18N
 
107
        downButton.setMnemonic(Util.THIS.getChar("MNE_element_attributelist_down")); // NOI18N
 
108
        removeButton.setMnemonic(Util.THIS.getChar("MNE_element_attributelist_remove")); // NOI18N
 
109
        addButton.setMnemonic(Util.THIS.getChar("MNE_element_attributelist_add")); // NOI18N
 
110
        initAccessibility();
 
111
        
 
112
        // Add custom header renderer supporting sorting
 
113
        attrTable.getTableHeader().setDefaultRenderer(new HeaderRenderer());
 
114
 
 
115
        // Cells should become editable on single mouse click
 
116
        final JTextField editorComponent = new JTextField();
 
117
        editorComponent.getCaret().setVisible(true);
 
118
        final DefaultCellEditor singleClickEditor = new DefaultCellEditor(editorComponent);
 
119
        singleClickEditor.setClickCountToStart(1);
 
120
        attrTable.setDefaultEditor(String.class, singleClickEditor);
 
121
        
 
122
        // Set ListSelectionModel
 
123
        attrTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 
124
        ListSelectionModel rowsm = attrTable.getSelectionModel();
 
125
        rowsm.addListSelectionListener (new ListSelectionListener(){
 
126
                public void valueChanged (ListSelectionEvent e) {
 
127
                    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\n#=- TreeElementAttributeListCustomizer::ListSelectionListener.valueChanged: event = " + e);
 
128
                    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("#=-     event.getValueIsAdjusting() = " + e.getValueIsAdjusting());
 
129
                    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("#=-     event.getFirstIndex()       = " + e.getFirstIndex());
 
130
                    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("#=-     event.getLastIndex()        = " + e.getLastIndex());
 
131
 
 
132
                    if (e.getValueIsAdjusting())
 
133
                        return;
 
134
                    ListSelectionModel lsm = (ListSelectionModel)e.getSource();
 
135
 
 
136
                    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("#=-     event.getSource()                 = " + lsm);
 
137
                    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("#=-     selectionModel.isSelectionEmpty() = " + lsm.isSelectionEmpty());
 
138
                    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("#=-     attrTable.getSelectedRow()        = " + attrTable.getSelectedRow());
 
139
                    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("#=-     attrTable.getRowCount()           = " + attrTable.getRowCount());
 
140
 
 
141
                    if ( lsm.isSelectionEmpty() ||
 
142
                         ( attrTable.getRowCount() == 0 ) ) {
 
143
                        upButton.setEnabled (false);
 
144
                        downButton.setEnabled (false);
 
145
                        removeButton.setEnabled (false);
 
146
                    } else {
 
147
                        upButton.setEnabled (attrTable.getSelectedRow() > 0);
 
148
                        downButton.setEnabled (attrTable.getSelectedRow() < (numRows() - 1));
 
149
                        removeButton.setEnabled (true);
 
150
                    }
 
151
                }
 
152
            });
 
153
        
 
154
        //Click to header - sorting
 
155
        attrTable.getTableHeader().addMouseListener(new MouseAdapter() {
 
156
                public void mouseClicked(MouseEvent e) {
 
157
                    TableColumnModel colModel = attrTable.getColumnModel();
 
158
                    int columnIndex = colModel.getColumnIndexAtX(e.getX());
 
159
                    // No column was clicked.
 
160
                    if(columnIndex < 0) return;
 
161
                    int modelIndex = colModel.getColumn(columnIndex).getModelIndex();
 
162
                    // not detected column
 
163
                    if (modelIndex < 0) return;
 
164
                    // Do sort !
 
165
                    AttlistTableModel tm = (AttlistTableModel) attrTable.getModel();
 
166
                    tm.sortByColumn(columnIndex);
 
167
                }
 
168
            });
 
169
        
 
170
    }
 
171
    
 
172
    
 
173
    // Get count of rows
 
174
    private int numRows(){
 
175
        return attrTable.getModel().getRowCount();
 
176
    }
 
177
    
 
178
    //Get count of columns
 
179
    private int numCols(){
 
180
        return attrTable.getModel().getColumnCount();
 
181
    }
 
182
    
 
183
    //
 
184
    // itself
 
185
    //
 
186
    
 
187
    /** This method is called from within the constructor to
 
188
     * initialize the form.
 
189
     * WARNING: Do NOT modify this code. The content of this method is
 
190
     * always regenerated by the FormEditor.
 
191
     */
 
192
    private void initComponents() {//GEN-BEGIN:initComponents
 
193
        java.awt.GridBagConstraints gridBagConstraints;
 
194
 
 
195
        tableScrollPane = new javax.swing.JScrollPane();
 
196
        attrTable = new javax.swing.JTable();
 
197
        upButton = new javax.swing.JButton();
 
198
        downButton = new javax.swing.JButton();
 
199
        addButton = new javax.swing.JButton();
 
200
        removeButton = new javax.swing.JButton();
 
201
 
 
202
        setLayout(new java.awt.GridBagLayout());
 
203
 
 
204
        setPreferredSize(new java.awt.Dimension(350, 230));
 
205
        attrTable.setPreferredScrollableViewportSize(new java.awt.Dimension(200, 150));
 
206
        tableScrollPane.setViewportView(attrTable);
 
207
 
 
208
        gridBagConstraints = new java.awt.GridBagConstraints();
 
209
        gridBagConstraints.gridx = 0;
 
210
        gridBagConstraints.gridy = 0;
 
211
        gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
 
212
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
 
213
        gridBagConstraints.weightx = 1.0;
 
214
        gridBagConstraints.weighty = 1.0;
 
215
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 0, 0);
 
216
        add(tableScrollPane, gridBagConstraints);
 
217
 
 
218
        upButton.setText(Util.THIS.getString ("TEXT_element_attributelist_up"));
 
219
        upButton.setEnabled(false);
 
220
        upButton.addActionListener(new java.awt.event.ActionListener() {
 
221
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
222
                upButtonActionPerformed(evt);
 
223
            }
 
224
        });
 
225
 
 
226
        gridBagConstraints = new java.awt.GridBagConstraints();
 
227
        gridBagConstraints.gridx = 1;
 
228
        gridBagConstraints.gridy = 0;
 
229
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
 
230
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
 
231
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
 
232
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 0, 11);
 
233
        add(upButton, gridBagConstraints);
 
234
 
 
235
        downButton.setText(Util.THIS.getString ("TEXT_element_attributelist_down"));
 
236
        downButton.setEnabled(false);
 
237
        downButton.addActionListener(new java.awt.event.ActionListener() {
 
238
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
239
                downButtonActionPerformed(evt);
 
240
            }
 
241
        });
 
242
 
 
243
        gridBagConstraints = new java.awt.GridBagConstraints();
 
244
        gridBagConstraints.gridx = 1;
 
245
        gridBagConstraints.gridy = 1;
 
246
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
 
247
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
 
248
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
 
249
        gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 11);
 
250
        add(downButton, gridBagConstraints);
 
251
 
 
252
        addButton.setText(Util.THIS.getString ("TEXT_element_attributelist_add"));
 
253
        addButton.addActionListener(new java.awt.event.ActionListener() {
 
254
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
255
                addButtonActionPerformed(evt);
 
256
            }
 
257
        });
 
258
 
 
259
        gridBagConstraints = new java.awt.GridBagConstraints();
 
260
        gridBagConstraints.gridx = 1;
 
261
        gridBagConstraints.gridy = 2;
 
262
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
 
263
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
 
264
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
 
265
        gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 11);
 
266
        add(addButton, gridBagConstraints);
 
267
 
 
268
        removeButton.setText(Util.THIS.getString ("TEXT_element_attributelist_remove"));
 
269
        removeButton.setEnabled(false);
 
270
        removeButton.addActionListener(new java.awt.event.ActionListener() {
 
271
            public void actionPerformed(java.awt.event.ActionEvent evt) {
 
272
                removeButtonActionPerformed(evt);
 
273
            }
 
274
        });
 
275
 
 
276
        gridBagConstraints = new java.awt.GridBagConstraints();
 
277
        gridBagConstraints.gridx = 1;
 
278
        gridBagConstraints.gridy = 3;
 
279
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
 
280
        gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
 
281
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
 
282
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
 
283
        gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 11);
 
284
        add(removeButton, gridBagConstraints);
 
285
 
 
286
    }//GEN-END:initComponents
 
287
 
 
288
    /**
 
289
     * The button at right side was pressed
 
290
     */
 
291
    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
 
292
 
 
293
        AttlistTableModel tm = (AttlistTableModel) attrTable.getModel();
 
294
        tm.addRow();
 
295
        int actualIndex = numRows() - 1;
 
296
        attrTable.getSelectionModel().setSelectionInterval(actualIndex, actualIndex);
 
297
    }//GEN-LAST:event_addButtonActionPerformed
 
298
 
 
299
    /**
 
300
     * The button at right side was pressed
 
301
     */    
 
302
    private void upButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_upButtonActionPerformed
 
303
 
 
304
        AttlistTableModel tm = (AttlistTableModel) attrTable.getModel();
 
305
        int index = attrTable.getSelectedRow();
 
306
        if (index > 0) {
 
307
            tm.moveRow(index, index - 1);
 
308
            attrTable.getSelectionModel().setSelectionInterval(index - 1,index - 1);
 
309
        }
 
310
    }//GEN-LAST:event_upButtonActionPerformed
 
311
 
 
312
    /**
 
313
     * The button at right side was pressed
 
314
     */    
 
315
    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
 
316
 
 
317
        AttlistTableModel tm = (AttlistTableModel) attrTable.getModel();
 
318
        
 
319
        int sel = attrTable.getSelectedRow();
 
320
        if (sel > -1) {
 
321
            tm.removeRow(sel);
 
322
            if (numRows() > 0) {
 
323
                if (sel <= numRows() - 1)
 
324
                    attrTable.getSelectionModel().setSelectionInterval(sel,sel);
 
325
                else
 
326
                    attrTable.getSelectionModel().setSelectionInterval(sel - 1, sel - 1);
 
327
            } else removeButton.setEnabled(false);
 
328
        }
 
329
    }//GEN-LAST:event_removeButtonActionPerformed
 
330
    
 
331
    /**
 
332
     * The button at right side was pressed
 
333
     */    
 
334
    private void downButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downButtonActionPerformed
 
335
 
 
336
        AttlistTableModel tm = (AttlistTableModel) attrTable.getModel();
 
337
        int index = attrTable.getSelectedRow();
 
338
        if (index > -1 && index < numRows() - 1) {
 
339
            tm.moveRow(index, index + 1);
 
340
            attrTable.getSelectionModel().setSelectionInterval(index + 1,index + 1);
 
341
        }
 
342
    }//GEN-LAST:event_downButtonActionPerformed
 
343
    
 
344
    
 
345
    // Variables declaration - do not modify//GEN-BEGIN:variables
 
346
    private javax.swing.JTable attrTable;
 
347
    private javax.swing.JButton addButton;
 
348
    private javax.swing.JScrollPane tableScrollPane;
 
349
    private javax.swing.JButton downButton;
 
350
    private javax.swing.JButton upButton;
 
351
    private javax.swing.JButton removeButton;
 
352
    // End of variables declaration//GEN-END:variables
 
353
    
 
354
    
 
355
    private TreeNamedObjectMap peer;
 
356
    private AttlistTableModel tableModel;
 
357
    
 
358
    /**
 
359
     */
 
360
    public void setObject (Object obj) {
 
361
        peer = (TreeNamedObjectMap) obj;
 
362
        tableModel = new AttlistTableModel(/*peer*/);
 
363
        attrTable.setModel(tableModel);
 
364
// we do not "delete" column anymore
 
365
//        attrTable.addKeyListener(new RowKeyListener(attrTable));
 
366
        
 
367
        /** First table column is row selector. */
 
368
        TableColumn column = null;
 
369
        for (int i = 0; i < COL_COUNT; i++) {
 
370
            column = attrTable.getColumnModel().getColumn (i);
 
371
            //column.setPreferredWidth (50);
 
372
        }
 
373
        
 
374
        updateView();
 
375
        
 
376
        TreeObjectListProxyListener proxy = new TreeObjectListProxyListener(peer);
 
377
        proxy.addPropertyChangeListener(org.openide.util.WeakListeners.propertyChange(this, proxy));
 
378
    }
 
379
    
 
380
    
 
381
    /** Udate state accordingly*/
 
382
    public void propertyChange(final PropertyChangeEvent e) {
 
383
        if (e.getSource() == null)
 
384
            return;
 
385
        
 
386
        updateView();
 
387
    }
 
388
    
 
389
    /** Update visualization accordingly. */
 
390
    private void updateView () {
 
391
        tableModel.fireTableDataChanged();
 
392
    }
 
393
    
 
394
    //
 
395
    // class RowKeyListener
 
396
    //
 
397
    
 
398
    /** 
 
399
     * Deletes whole row by pressing DELETE on row column. 
 
400
     * Unused...
 
401
     */
 
402
    private class RowKeyListener extends KeyAdapter {
 
403
        
 
404
        /** */
 
405
        private JTable table;
 
406
        
 
407
        
 
408
        //
 
409
        // init
 
410
        //
 
411
        
 
412
        public RowKeyListener (JTable table) {
 
413
            this.table = table;
 
414
        }
 
415
        
 
416
        
 
417
        //
 
418
        // itself
 
419
        //
 
420
        
 
421
        /**
 
422
         */
 
423
        public void keyReleased (KeyEvent e) {
 
424
            //if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Event: " + e); // NOI18N
 
425
            if (e.getKeyCode() == KeyEvent.VK_DELETE) {
 
426
                tableModel.removeRow (table.getSelectedRow());
 
427
//                  peer.remove (peer.get (table.getSelectedRow()));
 
428
//                  tableModel.fireTableDataChanged();
 
429
            }
 
430
        }
 
431
    }
 
432
    
 
433
    
 
434
    //
 
435
    // class AttlistTableModel
 
436
    //
 
437
    
 
438
    /**
 
439
     *
 
440
     */
 
441
    private class AttlistTableModel extends AbstractTableModel {
 
442
        
 
443
        /** Serial Version UID */
 
444
        private static final long serialVersionUID = 1567143493464383838L;
 
445
        
 
446
        boolean  ascending = true;
 
447
        int SortedColumn = -1;
 
448
        int indexes[];
 
449
        
 
450
        //
 
451
        // init
 
452
        //
 
453
        
 
454
        /** Create a data node for a given data object.
 
455
         * The provided children object will be used to hold all child nodes.
 
456
         * @param obj object to work with
 
457
         * @param ch children container for the node
 
458
         */
 
459
        public AttlistTableModel () {
 
460
            super();
 
461
        }
 
462
        
 
463
        /** Returns the number of rows in the model */
 
464
        public int getRowCount () {
 
465
            return peer.size();
 
466
        }
 
467
        
 
468
        
 
469
        /** Returns the number of columns in the model */
 
470
        public int getColumnCount () {
 
471
            return COL_COUNT;
 
472
        }
 
473
        
 
474
        /** Returns the class for a model. */
 
475
        public Class getColumnClass (int index) {
 
476
            return String.class;
 
477
        }
 
478
        
 
479
        
 
480
//         // Get unique attribute name for added blank rows
 
481
//         private String getUniqueName(){
 
482
//             int index = 0, i = 0;
 
483
//             final String prefix = "attribute-"; // NOI18N
 
484
//             String result = prefix + Integer.toString(index);
 
485
//             while (i<numRows()){
 
486
//                 if (result.equals(attrTable.getValueAt(i,0))) {
 
487
//                     result = prefix + Integer.toString(++index);
 
488
//                     i = 0;
 
489
//                 } else  ++i;
 
490
//             }
 
491
//             return result;
 
492
//         }
 
493
        
 
494
        /**
 
495
         */
 
496
        public void addRow () {
 
497
            
 
498
            TreeAttribute attr = (TreeAttribute) Lib.createAttributeDialog(true);
 
499
            if (attr != null) {
 
500
                boolean toSet = true;
 
501
                TreeAttribute oldAttribute = (TreeAttribute) peer.get (attr.getTreeName());
 
502
                if ( oldAttribute != null ) {
 
503
                    toSet = Lib.confirmAction
 
504
                        (Util.THIS.getString ("MSG_replace_attribute", attr.getQName()));
 
505
                }
 
506
                if ( toSet ) {
 
507
                    peer.add (attr);
 
508
                    SortedColumn = -1;
 
509
                    fireTableStructureChanged();
 
510
                }
 
511
            }
 
512
            /*
 
513
              try {
 
514
              peer.add (new TreeAttribute (getUniqueName(), ""));
 
515
              SortedColumn = -1;
 
516
              fireTableStructureChanged();
 
517
              } catch (TreeException exc) {
 
518
              TAXUtil.notifyTreeException (exc);
 
519
              }
 
520
            */
 
521
        }
 
522
        
 
523
        /**
 
524
         */
 
525
        public void removeRow (int row) {
 
526
            peer.remove(row);
 
527
            fireTableStructureChanged();
 
528
        }
 
529
 
 
530
        /**
 
531
         */
 
532
        public void moveRow (int fromIndex, int toIndex) {
 
533
            try {
 
534
                peer.switchObjects (fromIndex, toIndex);
 
535
 
 
536
                SortedColumn = -1;
 
537
                fireTableStructureChanged();
 
538
            } catch (TreeException exc) {
 
539
                TAXUtil.notifyTreeException (exc);
 
540
            }
 
541
        };
 
542
        
 
543
        /**
 
544
         */
 
545
        public Object getValueAt (int row, int column) {
 
546
            TreeAttribute attr = (TreeAttribute) peer.get(row);
 
547
            switch (column) {
 
548
            case COL_NAME:
 
549
                return attr.getQName();
 
550
            case COL_VALUE:
 
551
                return attr.getValue();
 
552
            default:
 
553
                return null;
 
554
            }
 
555
        }
 
556
        
 
557
        /**
 
558
         */
 
559
        public void setValueAt (Object val, int row, int column) {
 
560
            TreeAttribute attr = (TreeAttribute) peer.get (row);
 
561
            try {
 
562
                if (column == COL_NAME) {
 
563
 
 
564
                    String attrName = (String) val;
 
565
 
 
566
                    boolean toSet = true;
 
567
                    TreeAttribute oldAttribute = (TreeAttribute) peer.get (new TreeName (attrName));
 
568
                    if ( attr != oldAttribute ) {
 
569
                        if ( oldAttribute != null ) {
 
570
                            toSet = Lib.confirmAction
 
571
                                (Util.THIS.getString ("MSG_replace_attribute", attrName));
 
572
                        }
 
573
                    }
 
574
                    if ( toSet ) {
 
575
                        attr.setQName (attrName);
 
576
                    }
 
577
                    
 
578
                } else if (column == COL_VALUE) {
 
579
                    attr.setValue ((String) val);
 
580
                }
 
581
            } catch (TreeException exc) {
 
582
                TAXUtil.notifyTreeException (exc);
 
583
            }
 
584
        }
 
585
        
 
586
        /**
 
587
         */
 
588
        public String getColumnName (int column) {
 
589
            switch (column) {
 
590
            case COL_NAME:
 
591
                return Util.THIS.getString ("NAME_column_name");
 
592
            case COL_VALUE:
 
593
                return Util.THIS.getString ("NAME_column_value");
 
594
            default:
 
595
                return ""; // NOI18N
 
596
            }
 
597
        }
 
598
        
 
599
        /** Returns true for all cells which are editable. For a
 
600
         * a new cell is editable only name field.
 
601
         */
 
602
        public boolean isCellEditable (int rowIndex, int columnIndex) {
 
603
            return true;
 
604
        }
 
605
        
 
606
        /**
 
607
         */
 
608
        public void sortByColumn (int column) {
 
609
            
 
610
            if (SortedColumn == column)
 
611
                ascending = !ascending;
 
612
            else {
 
613
                SortedColumn = column;
 
614
                ascending = true;
 
615
            }
 
616
            
 
617
            indexes = new int[getRowCount()];
 
618
            for (int row = 0; row < getRowCount(); row++) {
 
619
                indexes[row] = row;
 
620
            }
 
621
            
 
622
            n2sort(column);
 
623
            
 
624
            int[] indx = new int[indexes.length];
 
625
            for (int row = 0; row < indx.length; row++) {
 
626
                indx[indexes[row]] = row;
 
627
            }
 
628
            
 
629
            try {
 
630
                peer.reorder (indx);
 
631
                fireTableChanged(new TableModelEvent(this));
 
632
            } catch (TreeException exc) {
 
633
                TAXUtil.notifyTreeException (exc);
 
634
            }
 
635
        }
 
636
        
 
637
        /**
 
638
         */
 
639
        private void n2sort (int col) {
 
640
            for (int i = 0; i < getRowCount(); i++) {
 
641
                for (int j = i + 1; j < getRowCount(); j++) {
 
642
                    if (compare(indexes[i], indexes[j], col) == -1) {
 
643
                        // swap it
 
644
                        int tmp = indexes[i];
 
645
                        indexes[i] = indexes[j];
 
646
                        indexes[j] = tmp;
 
647
                    }
 
648
                }
 
649
            }
 
650
        }
 
651
        
 
652
        /**
 
653
         */
 
654
        private int compare (int row1, int row2, int col) {
 
655
            int result = compareRowsByColumn(row1, row2, col);
 
656
            if (result != 0) {
 
657
                return ascending ? -result : result;
 
658
            }
 
659
            return 0;
 
660
        }
 
661
        
 
662
        /**
 
663
         */
 
664
        private int compareRowsByColumn (int row1, int row2, int column) {
 
665
            
 
666
            Class type = getColumnClass(column);
 
667
            
 
668
            Object o1 = getValueAt(row1, column);
 
669
            Object o2 = getValueAt(row2, column);
 
670
            
 
671
            
 
672
            // If both values are null, return 0.
 
673
            
 
674
            if (o1 == null && o2 == null) {
 
675
                return 0;
 
676
            } else if (o1 == null) {
 
677
                return -1;
 
678
            } else if (o2 == null) {
 
679
                return 1;
 
680
            }
 
681
            
 
682
            // Compare String
 
683
            
 
684
            if (type == String.class) {
 
685
                String s1 = (String) getValueAt(row1, column);
 
686
                String s2 = (String) getValueAt(row2, column);
 
687
                int result = s1.compareTo(s2);
 
688
                
 
689
                if (result < 0) {
 
690
                    return -1;
 
691
                } else if (result > 0) {
 
692
                    return 1;
 
693
                } else {
 
694
                    return 0;
 
695
                }
 
696
            }
 
697
            return 0;
 
698
        }
 
699
        
 
700
    } // end: class AttlistTableModel
 
701
    
 
702
    
 
703
    //
 
704
    // Header renderer
 
705
    //
 
706
    
 
707
    private class HeaderRenderer extends JLabel implements TableCellRenderer {
 
708
        
 
709
        /** Serial Version UID */
 
710
        private static final long serialVersionUID =-3658206203140258583L;
 
711
        
 
712
        public HeaderRenderer() {
 
713
            super();
 
714
            setHorizontalAlignment(JLabel.LEFT);
 
715
            setBorder(UIManager.getBorder("TableHeader.cellBorder")); // NOI18N
 
716
            setToolTipText(headerToolTip);
 
717
        }
 
718
        
 
719
        public Component getTableCellRendererComponent (JTable table, Object value,
 
720
                                                        boolean isSelected, boolean hasFocus,int row, int column) {
 
721
            
 
722
            if (table != null) {
 
723
                JTableHeader header = table.getTableHeader();
 
724
                if (header != null) {
 
725
                    setForeground(header.getForeground());
 
726
                    setBackground(header.getBackground());
 
727
                    setFont(header.getFont());
 
728
                }
 
729
            }
 
730
            
 
731
            AttlistTableModel tm = (AttlistTableModel)table.getModel();
 
732
            if (column == tm.SortedColumn)
 
733
                setIcon(tm.ascending ? new javax.swing.ImageIcon(getClass().getResource("down.gif")): // NOI18N
 
734
                        new javax.swing.ImageIcon(getClass().getResource("up.gif"))); // NOI18N
 
735
            else setIcon(new javax.swing.ImageIcon(getClass().getResource("no.gif"))); // NOI18N
 
736
            
 
737
            setText((value == null) ? "" : value.toString()); // NOI18N
 
738
            return this;
 
739
        }
 
740
    } // end: class HeaderRenderer
 
741
 
 
742
    /** Initialize accesibility
 
743
     */
 
744
    public void initAccessibility(){
 
745
        
 
746
       this.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_TreeElementAttributeListCustomizer"));
 
747
 
 
748
       addButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_addButton")); 
 
749
       removeButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_removeButton"));
 
750
       upButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_upButton")); 
 
751
       downButton.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_downButton"));
 
752
       
 
753
       attrTable.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_attrTable")); 
 
754
       attrTable.getAccessibleContext().setAccessibleName(Util.THIS.getString("ACSN_attrTable")); 
 
755
    }     
 
756
    
 
757
}