Source Code Cross Referenced for ResourceComparator.java in  » IDE-Eclipse » ui-ide » org » eclipse » ui » internal » ide » dialogs » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Eclipse » ui ide » org.eclipse.ui.internal.ide.dialogs 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Copyright (c) 2000, 2006 IBM Corporation and others.
003:         * All rights reserved. This program and the accompanying materials
004:         * are made available under the terms of the Eclipse Public License v1.0
005:         * which accompanies this distribution, and is available at
006:         * http://www.eclipse.org/legal/epl-v10.html
007:         *
008:         * Contributors:
009:         *     IBM Corporation - initial API and implementation
010:         *******************************************************************************/package org.eclipse.ui.internal.ide.dialogs;
011:
012:        import org.eclipse.core.resources.IContainer;
013:        import org.eclipse.core.resources.IResource;
014:        import org.eclipse.jface.viewers.Viewer;
015:        import org.eclipse.jface.viewers.ViewerComparator;
016:
017:        /**
018:         * Sorter for viewers that display items of type <code>IResource</code>.
019:         * The sorter supports two sort criteria:
020:         * <p>
021:         * <code>NAME</code>: Folders are given order precedence, followed by files.
022:         * Within these two groups resources are ordered by name.  All name comparisons
023:         * are case-insensitive.
024:         * </p>
025:         * <p>
026:         * <code>TYPE</code>: Folders are given order precedence, followed by files.
027:         * Within these two groups resources are ordered by extension.  All extension
028:         * comparisons are case-insensitive.
029:         * </p>
030:         * <p>
031:         * This class may be instantiated; it is not intended to be subclassed.
032:         * </p>
033:         */
034:        public class ResourceComparator extends ViewerComparator {
035:
036:            /**
037:             * Constructor argument value that indicates to sort items by name.
038:             */
039:            public final static int NAME = 1;
040:
041:            /**
042:             * Constructor argument value that indicates to sort items by extension.
043:             */
044:            public final static int TYPE = 2;
045:
046:            private int criteria;
047:
048:            /**
049:             * Creates a resource sorter that will use the given sort criteria.
050:             *
051:             * @param criteria the sort criterion to use: one of <code>NAME</code> or 
052:             *   <code>TYPE</code>
053:             */
054:            public ResourceComparator(int criteria) {
055:                super ();
056:                this .criteria = criteria;
057:            }
058:
059:            /**
060:             * Returns an integer value representing the relative sort priority of the 
061:             * given element based on its class.
062:             * <p>
063:             * <ul>
064:             *   <li>resources (<code>IResource</code>) - 2</li>
065:             *   <li>project references (<code>ProjectReference</code>) - 1</li>
066:             *   <li>everything else - 0</li>
067:             * </ul>
068:             * </p>
069:             *
070:             * @param element the element
071:             * @return the sort priority (larger numbers means more important)
072:             */
073:            protected int classComparison(Object element) {
074:                if (element instanceof  IResource) {
075:                    return 2;
076:                }
077:                return 0;
078:            }
079:
080:            /* (non-Javadoc)
081:             * Method declared on ViewerSorter.
082:             */
083:            public int compare(Viewer viewer, Object o1, Object o2) {
084:                //have to deal with non-resources in navigator
085:                //if one or both objects are not resources, returned a comparison 
086:                //based on class.
087:                if (!(o1 instanceof  IResource && o2 instanceof  IResource)) {
088:                    return compareClass(o1, o2);
089:                }
090:                IResource r1 = (IResource) o1;
091:                IResource r2 = (IResource) o2;
092:
093:                if (r1 instanceof  IContainer && r2 instanceof  IContainer) {
094:                    return compareNames(r1, r2);
095:                } else if (r1 instanceof  IContainer) {
096:                    return -1;
097:                } else if (r2 instanceof  IContainer) {
098:                    return 1;
099:                } else if (criteria == NAME) {
100:                    return compareNames(r1, r2);
101:                } else if (criteria == TYPE) {
102:                    return compareTypes(r1, r2);
103:                } else {
104:                    return 0;
105:                }
106:            }
107:
108:            /**
109:             * Returns a number reflecting the collation order of the given elements
110:             * based on their class.
111:             *
112:             * @param element1 the first element to be ordered
113:             * @param element2 the second element to be ordered
114:             * @return a negative number if the first element is less  than the 
115:             *  second element; the value <code>0</code> if the first element is
116:             *  equal to the second element; and a positive number if the first
117:             *  element is greater than the second element
118:             */
119:            protected int compareClass(Object element1, Object element2) {
120:                return classComparison(element1) - classComparison(element2);
121:            }
122:
123:            /**
124:             * Returns a number reflecting the collation order of the given resources
125:             * based on their resource names.
126:             *
127:             * @param resource1 the first resource element to be ordered
128:             * @param resource2 the second resource element to be ordered
129:             * @return a negative number if the first element is less  than the 
130:             *  second element; the value <code>0</code> if the first element is
131:             *  equal to the second element; and a positive number if the first
132:             *  element is greater than the second element
133:             */
134:            protected int compareNames(IResource resource1, IResource resource2) {
135:                return getComparator().compare(resource1.getName(),
136:                        resource2.getName());
137:            }
138:
139:            /**
140:             * Returns a number reflecting the collation order of the given resources
141:             * based on their respective file extensions. Resources with the same file
142:             * extension will be collated based on their names.
143:             *
144:             * @param resource1 the first resource element to be ordered
145:             * @param resource2 the second resource element to be ordered
146:             * @return a negative number if the first element is less  than the 
147:             *  second element; the value <code>0</code> if the first element is
148:             *  equal to the second element; and a positive number if the first
149:             *  element is greater than the second element
150:             */
151:            protected int compareTypes(IResource resource1, IResource resource2) {
152:                String ext1 = getExtensionFor(resource1);
153:                String ext2 = getExtensionFor(resource2);
154:
155:                // Compare extensions.  If they're different then return a value that
156:                // indicates correct extension ordering.  If they're the same then
157:                // return a value that indicates the correct NAME ordering.
158:                int result = getComparator().compare(ext1, ext2);
159:
160:                if (result != 0) {
161:                    return result;
162:                }
163:
164:                return compareNames(resource1, resource2);
165:            }
166:
167:            /**
168:             * Returns the sort criteria of this this sorter.
169:             *
170:             * @return the sort criterion: one of <code>NAME</code> or <code>TYPE</code>
171:             */
172:            public int getCriteria() {
173:                return criteria;
174:            }
175:
176:            /**
177:             * Returns the extension portion of the given resource.
178:             *
179:             * @param resource the resource
180:             * @return the file extension, possibily the empty string
181:             */
182:            private String getExtensionFor(IResource resource) {
183:                String ext = resource.getFileExtension();
184:                return ext == null ? "" : ext; //$NON-NLS-1$
185:            }
186:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.