3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.model
;
13 import java
.io
.PrintWriter
;
14 import java
.io
.StringWriter
;
15 import java
.io
.Writer
;
16 import java
.lang
.reflect
.InvocationTargetException
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Collection
;
19 import java
.util
.LinkedHashMap
;
20 import java
.util
.List
;
21 import java
.util
.TreeSet
;
23 import org
.apache
.log4j
.Logger
;
24 import org
.eclipse
.core
.commands
.ExecutionException
;
25 import org
.eclipse
.core
.commands
.operations
.IOperationHistory
;
26 import org
.eclipse
.core
.runtime
.IAdaptable
;
27 import org
.eclipse
.core
.runtime
.ILog
;
28 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
29 import org
.eclipse
.core
.runtime
.IStatus
;
30 import org
.eclipse
.core
.runtime
.MultiStatus
;
31 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
32 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
33 import org
.eclipse
.core
.runtime
.Status
;
34 import org
.eclipse
.core
.runtime
.SubProgressMonitor
;
35 import org
.eclipse
.core
.runtime
.jobs
.ISchedulingRule
;
36 import org
.eclipse
.jface
.action
.IStatusLineManager
;
37 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
38 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
39 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
40 import org
.eclipse
.jface
.resource
.ColorRegistry
;
41 import org
.eclipse
.jface
.resource
.FontRegistry
;
42 import org
.eclipse
.jface
.window
.ApplicationWindow
;
43 import org
.eclipse
.swt
.graphics
.Color
;
44 import org
.eclipse
.swt
.graphics
.Font
;
45 import org
.eclipse
.swt
.widgets
.Display
;
46 import org
.eclipse
.swt
.widgets
.Shell
;
47 import org
.eclipse
.ui
.IEditorPart
;
48 import org
.eclipse
.ui
.IViewPart
;
49 import org
.eclipse
.ui
.IViewReference
;
50 import org
.eclipse
.ui
.IWorkbench
;
51 import org
.eclipse
.ui
.IWorkbenchPage
;
52 import org
.eclipse
.ui
.IWorkbenchPart
;
53 import org
.eclipse
.ui
.PartInitException
;
54 import org
.eclipse
.ui
.PlatformUI
;
55 import org
.eclipse
.ui
.ide
.undo
.WorkspaceUndoUtil
;
56 import org
.eclipse
.ui
.part
.EditorPart
;
57 import org
.eclipse
.ui
.progress
.IProgressService
;
58 import org
.eclipse
.ui
.progress
.IWorkbenchSiteProgressService
;
59 import org
.eclipse
.ui
.themes
.ITheme
;
60 import org
.eclipse
.ui
.themes
.IThemeManager
;
62 import eu
.etaxonomy
.cdm
.database
.PermissionDeniedException
;
63 import eu
.etaxonomy
.cdm
.model
.common
.IEnumTerm
;
64 //import eu.etaxonomy.cdm.persistence.hibernate.permission.SecurityExceptionUtils;
65 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
66 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
67 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
68 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmDataViewer
;
69 import eu
.etaxonomy
.taxeditor
.view
.detail
.DetailsViewPart
;
70 import eu
.etaxonomy
.taxeditor
.view
.supplementaldata
.SupplementalDataViewPart
;
74 * Abstract AbstractUtility class.
81 public abstract class AbstractUtility
{
83 /** Constant <code>statusLineManager</code> */
84 protected static IStatusLineManager statusLineManager
;
93 public static boolean closeAll() {
94 return getActivePage().closeAllEditors(true);
98 * Close the given editor.
101 * The <tt>MultipageTaxonEditor</tt> to close.
102 * @return <tt>true</tt> on success
104 public static boolean close(EditorPart editor
) {
105 return getActivePage().closeEditor(editor
, true);
113 * @return a {@link org.eclipse.swt.widgets.Shell} object.
115 public static Shell
getShell() {
117 return TaxeditorStorePlugin
.getDefault().getWorkbench()
118 .getActiveWorkbenchWindow().getShell();
126 * @return a {@link org.eclipse.ui.IWorkbenchPage} object.
128 public static IWorkbenchPage
getActivePage() {
130 return TaxeditorStorePlugin
.getDefault().getWorkbench()
131 .getActiveWorkbenchWindow().getActivePage();
139 * @return a {@link org.eclipse.ui.IWorkbenchPart} object.
141 public static IWorkbenchPart
getActivePart() {
142 return getActivePage() != null ?
getActivePage().getActivePart() : null;
145 public static IWorkbench
getWorkbench() {
146 return TaxeditorStorePlugin
.getDefault().getWorkbench();
154 * @return a {@link org.eclipse.jface.window.ApplicationWindow} object.
156 public static ApplicationWindow
getWorkbenchWindow() {
157 if (getWorkbench().getWorkbenchWindowCount() > 1) {
158 throw new IllegalStateException("More than one workbench window");
160 return (ApplicationWindow
) getWorkbench().getWorkbenchWindows()[0];
169 * a {@link java.lang.String} object.
170 * @return a {@link org.eclipse.ui.IViewPart} object.
172 public static IViewPart
showView(String id
) {
174 return PlatformUI
.getWorkbench().getActiveWorkbenchWindow()
176 .showView(id
, null, IWorkbenchPage
.VIEW_VISIBLE
);
177 } catch (PartInitException e
) {
178 errorDialog("Error opening view", AbstractUtility
.class, "Could not open view: " + id
, e
);
189 * a {@link org.eclipse.ui.IViewPart} object.
191 public static void hideView(IViewPart view
) {
192 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage()
202 * a {@link java.lang.String} object.
205 * @return a {@link org.eclipse.ui.IViewPart} object.
207 public static IViewPart
getView(String id
, boolean restore
) {
208 IViewReference
[] references
= PlatformUI
.getWorkbench()
209 .getActiveWorkbenchWindow().getActivePage().getViewReferences();
210 for (IViewReference reference
: references
) {
211 if (reference
.getId().equals(id
)) {
212 return reference
.getView(restore
);
224 * a {@link java.lang.Class} object.
225 * @return a {@link java.lang.Object} object.
227 public static Object
getService(Class api
) {
228 return TaxeditorStorePlugin
.getDefault().getWorkbench().getService(api
);
236 * @return a {@link org.eclipse.ui.themes.ITheme} object.
238 public static ITheme
getCurrentTheme() {
239 IThemeManager themeManager
= TaxeditorStorePlugin
.getDefault()
240 .getWorkbench().getThemeManager();
241 return themeManager
.getCurrentTheme();
245 * Fonts registered to the plugin may be obtained with the Eclipse themeing
246 * functionality. Thus fonts are chooseable by the user via
247 * Preferences->General->Appearance->Colors and Fonts
249 * @return the FontRegistry for the current theme
251 public static FontRegistry
getFontRegistry() {
252 return getCurrentTheme().getFontRegistry();
260 * @param symbolicName
261 * a {@link java.lang.String} object.
262 * @return a {@link org.eclipse.swt.graphics.Font} object.
264 public static Font
getFont(String symbolicName
) {
265 return getFontRegistry().get(symbolicName
);
269 * Color registered to the plugin may be obtained with the Eclipse themeing
270 * functionality. Thus colors are editable by the user via
271 * Preferences->General->Appearance->Colors and Fonts
273 * @return the ColorRegistry for the current theme
275 public static ColorRegistry
getColorRegistry() {
276 return getCurrentTheme().getColorRegistry();
284 * @param symbolicName
285 * a {@link java.lang.String} object.
286 * @return a {@link org.eclipse.swt.graphics.Color} object.
288 public static Color
getColor(String symbolicName
) {
289 return getColorRegistry().get(symbolicName
);
293 * Open a message box that informs the user about unimplemented
294 * functionality. This method is for developer convenience.
297 * a {@link java.lang.Object} object.
299 public static void notImplementedMessage(Object source
) {
300 warningDialog("Not yet implemented", source
,
301 "This functionality is not yet implemented.");
310 * a {@link java.lang.String} object.
312 * a {@link java.lang.String} object.
314 public static void informationDialog(final String title
,
315 final String message
) {
316 Display
.getDefault().asyncExec(new Runnable() {
320 MessageDialog
.openInformation(getShell(), title
, message
);
325 public static void informationDialog(final String title
,
326 final IStatus status
) {
327 informationDialog(title
, status
.getMessage());
338 * The object where the warning was generated (used by log4j)
340 * An informative String to be presented to the user
342 public static void warningDialog(final String title
, final Object source
,
343 final String message
) {
344 Display
.getDefault().asyncExec(new Runnable() {
348 MessageDialog
.openWarning(getShell(), title
, message
);
349 Class
<?
extends Object
> clazz
= source
!= null ? source
350 .getClass() : AbstractUtility
.class;
351 warn(clazz
, message
);
361 public static void warningDialog(String title
, Object source
,
363 warningDialog(title
, source
, status
.getMessage());
374 * The object where the warning was generated (used by log4j)
376 * An informative String to be presented to the user
380 * a Throwable if one exists or null
382 public static void errorDialog(final String title
, final Object source
,
383 final String message
, final Throwable t
) {
384 Display
.getDefault().asyncExec(new Runnable() {
388 MessageDialog
.openError(getShell(), title
, message
+ getCauseRecursively(t
));
389 Class
<?
extends Object
> clazz
= source
!= null ? source
390 .getClass() : this.getClass();
391 error(clazz
, message
, t
);
394 private String
getCauseRecursively(Throwable t
) {
399 if(t
.getCause() != null){
400 return getCauseRecursively(t
.getCause());
402 return String
.format("\n\nException: %s\nMessage: %s", t
.getClass().getSimpleName(), t
.getMessage());
409 public static void errorDialog(final String title
, final Object source
,
410 final String message
){
411 errorDialog(title
, source
, message
, null);
420 * a {@link java.lang.String} object.
422 * a {@link java.lang.Object} object.
424 * a {@link org.eclipse.core.runtime.IStatus} object.
426 public static void errorDialog(final String title
, final Object source
,
427 final IStatus status
) {
428 Display
.getDefault().asyncExec(new Runnable() {
432 ErrorDialog
.openError(getShell(), title
, null, status
);
433 Class
<?
extends Object
> clazz
= source
!= null ? source
434 .getClass() : this.getClass();
435 error(clazz
, status
);
446 * a {@link java.lang.String} object.
448 * a {@link java.lang.String} object.
451 public static boolean confirmDialog(String title
, String message
) {
452 return MessageDialog
.openQuestion(getShell(), title
, message
);
462 * {@link eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation}
464 * @return a {@link org.eclipse.core.runtime.IStatus} object.
466 public static IStatus
executeOperation(final AbstractPostOperation operation
) {
467 if (getOperationHistory() == null) {
468 throw new IllegalArgumentException(
469 "There is no operation history for this context");
472 final IAdaptable uiInfoAdapter
= WorkspaceUndoUtil
473 .getUIInfoAdapter(getShell());
475 IRunnableWithProgress runnable
= new IRunnableWithProgress() {
478 public void run(IProgressMonitor monitor
)
479 throws InvocationTargetException
, InterruptedException
{
480 monitor
.beginTask(operation
.getLabel(), 100);
481 IStatus status
= Status
.CANCEL_STATUS
;
483 operation
.addContext(IOperationHistory
.GLOBAL_UNDO_CONTEXT
);
484 status
= getOperationHistory().execute(operation
, monitor
,
486 } catch (ExecutionException e
) {
487 MultiStatus info
= null;
490 // FIXME cannot access TaxonomicEditorPlugin.PLUGIN_ID from here
491 // String PID = TaxonomicEditorPlugin.PLUGIN_ID;
492 String PID
= "eu.etaxonomy.taxeditor.application";
494 RuntimeException securityRuntimeException
= null;//SecurityExceptionUtils.findSecurityRuntimeException(e);
496 if(securityRuntimeException
!= null){
497 title
= "Your changes could not be saved!";
498 info
= new MultiStatus(PID
, 1, String
.format("You are missing sufficient permissions for the operation \"%s\".", operation
.getLabel()), null);
499 info
.add(new Status(IStatus
.WARNING
, PID
, 1, securityRuntimeException
.getMessage(), null));
502 title
= "Error executing operation";
503 info
= new MultiStatus(PID
, 1, String
.format("An error occured while executing %s.", operation
.getLabel()), null);
504 Writer writer
= new StringWriter();
505 PrintWriter printWriter
= new PrintWriter(writer
);
506 e
.printStackTrace(printWriter
);
507 info
.add(new Status(IStatus
.ERROR
, PID
, 1, writer
.toString(), null));
510 errorDialog(title
, getClass(), info
);
516 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed"
518 setStatusLine(operation
.getLabel() + " " + statusString
+ ".");
524 runInUI(runnable
, null);
525 } catch (Exception e
) {
526 errorDialog("Error executing operation", AbstractUtility
.class, "An error occured while executing " + operation
.getLabel(), e
);
529 // // Start the main progress monitor.
530 // IProgressMonitor newMonitor =
531 // startMainMonitor(getMonitor(),operation.getLabel(), 100);
533 // // Check whether operation was canceled and do some steps.
534 // workedChecked(newMonitor, 10);
537 // IStatus status = getOperationHistory().execute(operation, newMonitor,
538 // WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
540 // // Check whether operation was canceled and do some steps.
541 // workedChecked(newMonitor, 30);
543 // String statusString = status.equals(Status.OK_STATUS) ? "completed" :
545 // setStatusLine(operation.getLabel() + " " + statusString + ".");
548 // } catch (ExecutionException e) {
549 // logger.error("Error executing operation: " + operation.getLabel(),
551 // errorDialog("Error executing operation: " + operation.getLabel(),
552 // "Please refer to the error log.");
556 // // Stop the progress monitor.
557 // newMonitor.done();
560 IPostOperationEnabled postOperationEnabled
= operation
561 .getPostOperationEnabled();
562 if (postOperationEnabled
!= null) {
563 postOperationEnabled
.onComplete();
565 return Status
.OK_STATUS
;
570 * getOperationHistory
573 * @return a {@link org.eclipse.core.commands.operations.IOperationHistory}
576 public static IOperationHistory
getOperationHistory() {
577 return getWorkbench().getOperationSupport().getOperationHistory();
586 * a {@link java.lang.String} object.
588 public static void setStatusLine(final String message
) {
589 Display
.getDefault().asyncExec(new Runnable() {
593 statusLineManager
.setMessage(message
);
605 * @return a {@link org.eclipse.core.runtime.IProgressMonitor} object.
607 public static IProgressMonitor
getMonitor() {
608 statusLineManager
.setCancelEnabled(false);
609 return statusLineManager
.getProgressMonitor();
613 * Starts either the given {@link IProgressMonitor} if it's not
614 * <code>null</code> or a new {@link NullProgressMonitor}.
616 * @param progressMonitor
617 * The {@link IProgressMonitor} or <code>null</code> if no
618 * progress should be reported.
620 * The name of the main task.
622 * The number of steps this task is subdivided into.
623 * @return The {@link IProgressMonitor}.
625 public static IProgressMonitor
startMainMonitor(
626 IProgressMonitor progressMonitor
, String taskName
, int steps
) {
627 IProgressMonitor newMonitor
= progressMonitor
;
628 if (newMonitor
== null) {
629 newMonitor
= new NullProgressMonitor();
631 newMonitor
.beginTask(taskName
== null ?
"" : taskName
, steps
);
632 newMonitor
.subTask(" ");
637 * Creates a {@link SubProgressMonitor} if the given
638 * {@link IProgressMonitor} is not <code>null</code> and not a
639 * {@link NullProgressMonitor}.
641 * @param progressMonitor
642 * The parent {@link IProgressMonitor} of the
643 * {@link SubProgressMonitor} to be created.
645 * The number of steps this subtask is subdivided into. Must be a
646 * positive number and must not be
647 * {@link IProgressMonitor#UNKNOWN}.
648 * @return The {@link IProgressMonitor}.
650 public static IProgressMonitor
getSubProgressMonitor(
651 IProgressMonitor progressMonitor
, int ticks
) {
652 if (progressMonitor
== null) {
653 return new NullProgressMonitor();
655 if (progressMonitor
instanceof NullProgressMonitor
) {
656 return progressMonitor
;
659 return new SubProgressMonitor(progressMonitor
, ticks
);
663 * Checks whether the user canceled this operation. If not canceled, the
664 * given number of steps are declared as done.
667 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
671 public static void workedChecked(IProgressMonitor newMonitor
, int steps
) {
672 // In case the progress monitor was canceled throw an exception.
673 if (newMonitor
.isCanceled()) {
674 throw new OperationCanceledException();
676 // Otherwise declare this step as done.
677 newMonitor
.worked(steps
);
681 * Present a progress dialog to the user. This dialog will block the UI
684 * an implementation of {@link IRunnableWithProgress}
685 * @throws java.lang.InterruptedException
687 * @throws java.lang.reflect.InvocationTargetException
690 public static void busyCursorWhile(IRunnableWithProgress runnable
)
691 throws InvocationTargetException
, InterruptedException
{
692 getProgressService().busyCursorWhile(runnable
);
700 * @see {@link IProgressService#runInUI(org.eclipse.jface.operation.IRunnableContext, IRunnableWithProgress, ISchedulingRule)}
702 * a {@link org.eclipse.jface.operation.IRunnableWithProgress}
705 * a {@link org.eclipse.core.runtime.jobs.ISchedulingRule}
707 * @throws java.lang.reflect.InvocationTargetException
709 * @throws java.lang.InterruptedException
712 public static void runInUI(IRunnableWithProgress runnable
,
713 ISchedulingRule rule
) throws InvocationTargetException
,
714 InterruptedException
{
715 getProgressService().runInUI(getWorkbenchWindow(), runnable
, rule
);
728 * a {@link org.eclipse.jface.operation.IRunnableWithProgress}
730 * @throws java.lang.reflect.InvocationTargetException
732 * @throws java.lang.InterruptedException
735 public static void run(boolean fork
, boolean cancelable
,
736 IRunnableWithProgress runnable
) throws InvocationTargetException
,
737 InterruptedException
{
738 getProgressService().run(fork
, cancelable
, runnable
);
746 * @return a {@link org.eclipse.ui.progress.IProgressService} object.
748 public static IProgressService
getProgressService() {
749 IWorkbench workbench
= PlatformUI
.getWorkbench();
750 return workbench
.getProgressService();
755 * getProgressService2
758 * @return a {@link org.eclipse.ui.progress.IWorkbenchSiteProgressService}
761 public static IWorkbenchSiteProgressService
getProgressService2() {
762 return (IWorkbenchSiteProgressService
) getService(IWorkbenchSiteProgressService
.class);
771 * a {@link java.lang.String} object.
773 public static void info(String message
) {
774 IStatus status
= new Status(IStatus
.INFO
, getPluginId(), message
);
784 * a {@link org.eclipse.core.runtime.IStatus} object.
786 public static void info(IStatus status
) {
796 * a {@link java.lang.Class} object.
798 * a {@link java.lang.String} object.
800 public static void warn(Class source
, String message
) {
801 IStatus status
= new Status(IStatus
.WARNING
, getPluginId(), message
);
802 getLog4JLogger(source
).warn(message
);
806 public static void warn(Class source
, IStatus status
) {
807 getLog4JLogger(source
).warn(status
.getMessage(), status
.getException());
811 public static void warn(Class source
, Throwable t
) {
812 IStatus status
= new Status(IStatus
.WARNING
, getPluginId(), t
.getMessage(), t
);
813 getLog4JLogger(source
).warn(t
);
823 * a {@link java.lang.Class} object.
825 * a {@link java.lang.Throwable} object.
827 public static void error(Class source
, Throwable t
) {
828 error(source
.getClass(), t
.getMessage(), t
);
837 * a {@link java.lang.Class} object.
839 * a {@link java.lang.String} object.
841 * a {@link java.lang.Throwable} object.
843 public static void error(Class source
, String message
, Throwable t
) {
844 IStatus status
= new Status(IStatus
.ERROR
, getPluginId(), message
, t
);
845 error(source
, status
);
854 * a {@link java.lang.Class} object.
856 * a {@link org.eclipse.core.runtime.IStatus} object.
858 public static void error(Class source
, IStatus status
) {
859 getLog4JLogger(source
)
860 .error(status
.getMessage(), status
.getException());
870 * a {@link java.lang.Class} object.
871 * @return a {@link org.apache.log4j.Logger} object.
873 public static Logger
getLog4JLogger(Class clazz
) {
874 return Logger
.getLogger(clazz
);
878 * @see {@link ILog#log(IStatus)}
882 private static void log(IStatus status
) {
883 TaxeditorStorePlugin
.getDefault().getLog().log(status
);
891 * @return a {@link java.lang.String} object.
893 public static String
getPluginId() {
894 return "eu.taxeditor";
902 * @return a {@link org.eclipse.ui.IEditorPart} object.
904 public static IEditorPart
getActiveEditor() {
905 return getActivePage() != null ?
getActivePage().getActiveEditor()
914 * @return a {@link eu.etaxonomy.taxeditor.view.detail.DetailsViewPart}
917 public static DetailsViewPart
getDetailsView() {
918 return (DetailsViewPart
) getView(DetailsViewPart
.ID
, false);
923 * refreshDetailsViewer
926 public static void refreshDetailsViewer() {
927 if (getDetailsView() != null) {
928 ((AbstractCdmDataViewer
) getDetailsView().getViewer()).refresh();
934 * reflowDetailsViewer
937 public static void reflowDetailsViewer() {
938 if (getDetailsView() != null) {
939 ((AbstractCdmDataViewer
) getDetailsView().getViewer()).reflow();
943 public static SupplementalDataViewPart
getSupplementalDataView() {
944 return (SupplementalDataViewPart
) getView(SupplementalDataViewPart
.ID
,
948 public static void reflowSupplementalViewer() {
949 if (getSupplementalDataView() != null) {
950 ((AbstractCdmDataViewer
) getSupplementalDataView().getViewer())
957 * Orders a Collection of {@link IEnumTerm}s according to the term
960 * The returned map will be be ordered primarily by root elements,
961 * secondarily by the child elements and their children resp., both ascending alphabetically. <br>
963 * A {@link Collection} of {@link IEnumTerm}s for which the term
964 * hierarchy should be created
965 * @return a map which holds the terms as keys and their string
966 * representation via {@link IEnumTerm#getMessage()} as values
968 public static <T
extends IEnumTerm
<T
>> LinkedHashMap
<T
, String
> orderTerms(Collection
<T
> terms
) {
969 TreeSet
<TermNode
<T
>> parentElements
= new TreeSet
<TermNode
<T
>>();
970 parentElements
.addAll(getTermHierarchy(terms
));
972 // create list according to the type hierarchy (root elements alphabetically with recursive children also alphabetically)
973 LinkedHashMap
<T
, String
> result
= new LinkedHashMap
<T
, String
>();
974 parseTermTree(parentElements
, result
, -1);
978 private static<T
extends IEnumTerm
<T
>> void parseTermTree(Collection
<TermNode
<T
>> children
, LinkedHashMap
<T
, String
> result
, int depth
){
980 for(TermNode
<T
> node
:children
){
981 String indentString
= "";
982 for(int i
=0;i
<depth
;i
++){
986 indentString
+= "- ";
988 result
.put(node
.term
, indentString
+ node
.term
.getMessage());
989 parseTermTree(node
.children
, result
, depth
);
993 private static<T
extends IEnumTerm
<T
>> void addToParents(List
<TermNode
<T
>> parents
, Collection
<T
> terms
){
994 List
<TermNode
<T
>> hasChildrenList
= new ArrayList
<TermNode
<T
>>();
996 // only terms with parents
997 if(term
.getKindOf()!=null){
998 TermNode
<T
> parentNode
= new TermNode
<T
>(term
.getKindOf());
999 TermNode
<T
> childNode
= new TermNode
<T
>(term
);
1000 if(parents
.contains(parentNode
)){
1001 // parent found in parent list -> add this term to parent's child list
1002 parents
.get(parents
.indexOf(parentNode
)).addChild(childNode
);
1003 if(!term
.getGeneralizationOf().isEmpty()){
1004 // has more children -> add to list which will be the parent for the next recursion
1005 hasChildrenList
.add(childNode
);
1010 if(!hasChildrenList
.isEmpty()){
1011 addToParents(hasChildrenList
, terms
);
1015 private static<T
extends IEnumTerm
<T
>> List
<TermNode
<T
>> getTermHierarchy(Collection
<T
> terms
){
1016 List
<TermNode
<T
>> parents
= new ArrayList
<TermNode
<T
>>();
1017 // get root elements
1019 T parentTerm
= term
.getKindOf();
1020 if(parentTerm
==null){
1022 parents
.add(new TermNode
<T
>(term
));
1025 addToParents(parents
, terms
);
1029 @SuppressWarnings("unchecked")
1031 * Recursively iterates over all term parents until no more parent is found i.e. the root node
1032 * @param term The term for which the parent should be found
1033 * @return the root terms of the term hierarchy
1035 private static<T
extends IEnumTerm
<T
>> T
getParentFor(T term
){
1036 // PP: cast should be safe. Why is Eclipse complaining??
1037 T parent
= term
.getKindOf();
1042 return getParentFor(term
.getKindOf());
1046 private static class TermNode
<T
extends IEnumTerm
<T
>> implements Comparable
<TermNode
<T
>>{
1047 private final T term
;
1048 private final TreeSet
<TermNode
<T
>> children
;
1054 public TermNode(T term
) {
1057 this.children
= new TreeSet
<TermNode
<T
>>();
1060 public void addChild(TermNode
<T
> child
){
1061 this.children
.add(child
);
1065 * @return the children
1067 public TreeSet
<TermNode
<T
>> getChildren() {
1074 public T
getTerm() {
1079 * @see java.lang.Object#hashCode()
1082 public int hashCode() {
1083 final int prime
= 31;
1085 result
= prime
* result
+ ((term
== null) ?
0 : term
.hashCode());
1090 * @see java.lang.Object#equals(java.lang.Object)
1093 public boolean equals(Object obj
) {
1100 if (getClass() != obj
.getClass()) {
1103 TermNode other
= (TermNode
) obj
;
1105 if (other
.term
!= null) {
1108 } else if (!term
.equals(other
.term
)) {
1115 * @see java.lang.Comparable#compareTo(java.lang.Object)
1118 public int compareTo(TermNode
<T
> that
) {
1119 return this.term
.getMessage().compareTo(that
.term
.getMessage());