001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    /*******************************************************************************
021     * Copyright (c) 2003, 2007 IBM Corporation and others.
022     * All rights reserved. This program and the accompanying materials
023     * are made available under the terms of the Eclipse Public License v1.0
024     * which accompanies this distribution, and is available at
025     * http://www.eclipse.org/legal/epl-v10.html
026     * 
027     * Contributors:
028     *     IBM Corporation - Initial API and implementation
029     *******************************************************************************/
030    package org.apache.directory.studio.apacheds.views;
031    
032    
033    import org.eclipse.jface.viewers.Viewer;
034    import org.eclipse.jface.viewers.ViewerComparator;
035    
036    
037    /**
038     * This class implements the servers table viewer comparator.
039     *
040     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041     * @version $Rev$, $Date$
042     */
043    public class ServersViewerComparator extends ViewerComparator
044    {
045        public static final int MAX_DEPTH = 3;
046        public static final int ASCENDING = 1;
047        public static final int DESCENDING = -1;
048    
049        protected ServersViewLabelProvider labelProvider;
050    
051        protected int[] priorities = new int[]
052            { 0 };
053    
054        protected int[] directions = new int[]
055            { ASCENDING };
056    
057    
058        /**
059         * Creates a new instance of ServersViewerComparator.
060         *
061         * @param labelProvider
062         *      the label provider
063         */
064        public ServersViewerComparator( ServersViewLabelProvider labelProvider )
065        {
066            this.labelProvider = labelProvider;
067        }
068    
069    
070        /**
071         * Sets the top priority.
072         *
073         * @param priority
074         *      the priority
075         */
076        public void setTopPriority( int priority )
077        {
078            if ( priorities[0] == priority )
079            {
080                return;
081            }
082    
083            int len = priorities.length + 1;
084            if ( len > MAX_DEPTH )
085            {
086                len = MAX_DEPTH;
087            }
088    
089            int[] temp = new int[len];
090            System.arraycopy( priorities, 0, temp, 1, len - 1 );
091            temp[0] = priority;
092            priorities = temp;
093    
094            temp = new int[len];
095            System.arraycopy( directions, 0, temp, 1, len - 1 );
096            temp[0] = ASCENDING;
097            directions = temp;
098        }
099    
100    
101        /**
102         * Gets the top priority.
103         *
104         * @return
105         *      the top priority
106         */
107        public int getTopPriority()
108        {
109            return priorities[0];
110        }
111    
112    
113        /**
114         * Sets the top priority direction
115         *
116         * @param direction
117         *      the direction
118         */
119        public void setTopPriorityDirection( int direction )
120        {
121            if ( direction == ASCENDING || direction == DESCENDING )
122            {
123                directions[0] = direction;
124            }
125        }
126    
127    
128        /**
129         * Gets the top priority direction
130         *
131         * @return
132         *      the top priority direction
133         */
134        public int getTopPriorityDirection()
135        {
136            return directions[0];
137        }
138    
139    
140        /**
141         * Reverses the top priority
142         */
143        public void reverseTopPriority()
144        {
145            directions[0] *= -1;
146        }
147    
148    
149        /**
150         * Returns a negative, zero, or positive number depending on whether
151         * the first element is less than, equal to, or greater than
152         * the second element.
153         * <p>
154         * The default implementation of this method is based on
155         * comparing the elements' categories as computed by the <code>category</code>
156         * framework method. Elements within the same category are further 
157         * subjected to a case insensitive compare of their label strings, either
158         * as computed by the content viewer's label provider, or their 
159         * <code>toString</code> values in other cases. Subclasses may override.
160         * </p>
161         * 
162         * @param viewer the viewer
163         * @param e1 the first element
164         * @param e2 the second element
165         * @param a the direction
166         * @return a negative number if the first element is less  than the 
167         *  second element; the value <code>0</code> if the first element is
168         *  equal to the second element; and a positive number if the first
169         *  element is greater than the second element
170         */
171        public int compare( Viewer viewer, Object e1, Object e2, int a )
172        {
173            int col = priorities[a];
174    
175            String s1 = labelProvider.getColumnText( e1, col );
176            String s2 = labelProvider.getColumnText( e2, col );
177    
178            int s = s1.compareToIgnoreCase( s2 ) * directions[a];
179            if ( s == 0 )
180            {
181                if ( a == priorities.length - 1 )
182                {
183                    return 0;
184                }
185                return compare( viewer, e1, e2, a + 1 );
186            }
187            return s;
188        }
189    
190    
191        /* (non-Javadoc)
192         * @see org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
193         */
194        public int compare( Viewer viewer, Object e1, Object e2 )
195        {
196            return compare( viewer, e1, e2, 0 );
197        }
198    }