View Javadoc

1   // $Id:DefaultMutableTreeNodeComparator.java 62 2008-04-20 12:28:56Z me $
2   /*
3    * xsAnalyzer - XML schema analyzing tool. Copyright (C) 2008 Michael Engelhardt
4    * 
5    * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
6    * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
7    * version.
8    * 
9    * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
10   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11   * 
12   * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
13   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14   */
15  /**
16   * 
17   */
18  package de.mindcrimeilab.xsanalyzer.util;
19  
20  import java.util.Comparator;
21  
22  import javax.swing.tree.DefaultMutableTreeNode;
23  
24  /**
25   * @author Michael Engelhardt<me@mindcrime-ilab.de>
26   * @author $Author:me $
27   * @version $Revision:62 $
28   * 
29   */
30  public class DefaultMutableTreeNodeComparator implements Comparator<DefaultMutableTreeNode> {
31  
32      private final Comparator<Object> delegate;
33  
34      @SuppressWarnings("unchecked")
35      public DefaultMutableTreeNodeComparator(Comparator comparator) {
36          delegate = comparator;
37      }
38  
39      /*
40       * (non-Javadoc)
41       * 
42       * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
43       */
44      @Override
45      public int compare(DefaultMutableTreeNode o1, DefaultMutableTreeNode o2) {
46          if (null == o1 && null != o2) {
47              return 1;
48          }
49          else if (null != o1 && null == o2) {
50              return -1;
51          }
52          else if (null == o1 && null == o2) {
53              return 0;
54          }
55          else {
56              return delegate.compare(o1.getUserObject(), o2.getUserObject());
57          }
58      }
59  
60  }