Source Code Cross Referenced for ElementValidator.java in  » IDE-Eclipse » jdt » org » eclipse » jdt » internal » ui » util » 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 » jdt » org.eclipse.jdt.internal.ui.util 
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.jdt.internal.ui.util;
011:
012:        import java.util.HashSet;
013:        import java.util.Set;
014:
015:        import org.eclipse.core.runtime.IAdaptable;
016:        import org.eclipse.core.runtime.IStatus;
017:
018:        import org.eclipse.core.resources.IResource;
019:
020:        import org.eclipse.swt.widgets.Shell;
021:
022:        import org.eclipse.jface.dialogs.ErrorDialog;
023:
024:        import org.eclipse.jdt.core.ICompilationUnit;
025:        import org.eclipse.jdt.core.IJavaElement;
026:
027:        import org.eclipse.jdt.internal.corext.util.Resources;
028:
029:        import org.eclipse.jdt.internal.ui.JavaUIMessages;
030:
031:        /**
032:         * Helper class to check if a set of <tt>IJavaElement</tt> objects can be
033:         * modified by an operation.
034:         * 
035:         * @since 	2.1
036:         */
037:        public class ElementValidator {
038:
039:            private ElementValidator() {
040:                // no instance
041:            }
042:
043:            /**
044:             * Checks if the given element is in sync with the underlying file system.
045:             * 
046:             * @param element the element to be checked
047:             * @param parent a parent shell used to present a dialog to the user if the
048:             * element is not in sync
049:             * @param title a dialog's title used to present a dialog to the user if the
050:             * element is not in sync
051:             * @return boolean <code>true</code> if the element is in sync with the file
052:             * system. Otherwise <code>false</code> is returned
053:             */
054:            public static boolean checkInSync(IAdaptable element, Shell parent,
055:                    String title) {
056:                return checkInSync(new IAdaptable[] { element }, parent, title);
057:            }
058:
059:            /**
060:             * Checks if the given array of elements is in sync with the underlying file
061:             * system.
062:             * 
063:             * @param elements the array of elements to be checked
064:             * @param parent a parent shell used to present a dialog to the user if
065:             * one of the elements is not in sync
066:             * @param title a dialog's title used to present a dialog to the user if
067:             * one of the elements is not in sync
068:             * @return boolean <code>true</code> if the all elements are in sync with
069:             * the file system. Otherwise <code>false</code> is returned
070:             */
071:            public static boolean checkInSync(IAdaptable[] elements,
072:                    Shell parent, String title) {
073:                return checkInSync(getResources(elements), parent, title);
074:            }
075:
076:            /**
077:             * Checks if the given element is read-only and if so the methods tries
078:             * to make the element writable by calling validate edit. If
079:             * <code>validateEdit</code> was able to make the file writable the method
080:             * additionally checks if the file has been changed by calling
081:             * <code>validateEdit</code>.
082:             * 
083:             * @param element the element to be checked
084:             * @param parent a parent shell used to present a dialog to the user if the
085:             * check fails
086:             * @param title a dialog's title used to present a dialog to the user if the
087:             * check fails
088:             * @return boolean <code>true</code> if the element is writable and its
089:             * content didn't change by calling <code>validateEdit</code>. Otherwise
090:             * <code>false</code> is returned
091:             * 
092:             * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
093:             */
094:            public static boolean checkValidateEdit(IJavaElement element,
095:                    Shell parent, String title) {
096:                return checkValidateEdit(new IJavaElement[] { element },
097:                        parent, title);
098:            }
099:
100:            /**
101:             * Checks if the given elements are read-only and if so the methods tries to
102:             * make the element writable by calling <code>validateEdit</code>. If
103:             * <code>validateEdit</code> was able to make the file writable the method
104:             * additionally checks if the file has been changed by calling
105:             * <code>validateEdit</code>.
106:             * 
107:             * @param elements the elements to be checked
108:             * @param parent a parent shell used to present a dialog to the user if the
109:             * check fails
110:             * @param title a dialog's title used to present a dialog to the user if the
111:             * check fails
112:             * @return boolean <code>true</code> if all elements are writable and their
113:             * content didn't change by calling <code>validateEdit</code>. Otherwise
114:             * <code>false</code> is returned
115:             * 
116:             * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
117:             */
118:            public static boolean checkValidateEdit(IJavaElement[] elements,
119:                    Shell parent, String title) {
120:                return checkValidateEdit(getResources(elements), parent, title);
121:            }
122:
123:            /**
124:             * Checks a combination of <code>checkInSync</code> and
125:             * <code>checkValidateEdit</code> depending of the value of
126:             * <code>editor</code>. If <code>editor</code> is <code>true</code> only
127:             * <code>checkValidateEdit</code> is performed since the editor does a in
128:             * sync check on focus change. If <code>editor</code> is <code>false</code>
129:             * both checks are performed.
130:             * 
131:             * @param element the element to be checked
132:             * @param parent a parent shell used to present a dialog to the user if the
133:             * check fails
134:             * @param title a dialog's title used to present a dialog to the user if the
135:             * check fails
136:             * @return boolean <code>true</code> if the element passed the checks.
137:             * Otherwise <code>false</code> is returned
138:             * 
139:             * @see #checkInSync(IAdaptable, Shell, String)
140:             * @see #checkValidateEdit(IJavaElement, Shell, String)
141:             */
142:            public static boolean check(IJavaElement element, Shell parent,
143:                    String title, boolean editor) {
144:                return check(new IJavaElement[] { element }, parent, title,
145:                        editor);
146:            }
147:
148:            /**
149:             * Checks a combination of <code>checkInSync</code> and
150:             * <code>checkValidateEdit</code> depending of the value of
151:             * <code>editor</code>. If <code>editor</code> is <code>true</code> only
152:             * <code>checkValidateEdit</code> is performed since the editor does a in
153:             * sync check on focus change. If <code>editor</code> is <code>false</code>
154:             * both checks are performed.
155:             * 
156:             * @param elements the elements to be checked
157:             * @param parent a parent shell used to present a dialog to the user if the
158:             * check fails
159:             * @param title a dialog's title used to present a dialog to the user if the
160:             * check fails
161:             * @return boolean <code>true</code> if all elements pass the checks.
162:             * Otherwise <code>false</code> is returned
163:             * 
164:             * @see #checkInSync(IAdaptable[], Shell, String)
165:             * @see #checkValidateEdit(IJavaElement[], Shell, String)
166:             */
167:            public static boolean check(IJavaElement[] elements, Shell parent,
168:                    String title, boolean editor) {
169:                IResource[] resources = getResources(elements);
170:                if (!editor && !checkInSync(resources, parent, title))
171:                    return false;
172:                return checkValidateEdit(resources, parent, title);
173:            }
174:
175:            private static boolean checkInSync(IResource[] resources,
176:                    Shell parent, String title) {
177:                IStatus status = Resources.checkInSync(resources);
178:                if (status.isOK())
179:                    return true;
180:                ErrorDialog.openError(parent, title,
181:                        JavaUIMessages.ElementValidator_cannotPerform, status);
182:                return false;
183:            }
184:
185:            private static boolean checkValidateEdit(IResource[] resources,
186:                    Shell parent, String title) {
187:                IStatus status = Resources.makeCommittable(resources, parent);
188:                if (!status.isOK()) {
189:                    ErrorDialog.openError(parent, title,
190:                            JavaUIMessages.ElementValidator_cannotPerform,
191:                            status);
192:                    return false;
193:                }
194:                return true;
195:            }
196:
197:            private static IResource[] getResources(IAdaptable[] elements) {
198:                Set result = new HashSet();
199:                for (int i = 0; i < elements.length; i++) {
200:                    IAdaptable element = elements[i];
201:                    IResource resource = null;
202:                    if (element instanceof  IJavaElement) {
203:                        IJavaElement je = (IJavaElement) element;
204:                        ICompilationUnit cu = (ICompilationUnit) je
205:                                .getAncestor(IJavaElement.COMPILATION_UNIT);
206:                        if (cu != null) {
207:                            je = cu.getPrimary();
208:                        }
209:                        resource = je.getResource();
210:                    } else {
211:                        resource = (IResource) element
212:                                .getAdapter(IResource.class);
213:                    }
214:                    if (resource != null)
215:                        result.add(resource);
216:                }
217:                return (IResource[]) result
218:                        .toArray(new IResource[result.size()]);
219:            }
220:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.