Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ProblemListPresenter |
|
| 2.25;2.25 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.editor.policy.presenter; | |
11 | ||
12 | import java.util.ArrayList; | |
13 | import java.util.Iterator; | |
14 | import java.util.List; | |
15 | import java.util.ListIterator; | |
16 | ||
17 | import org.jdesktop.observablecollections.ObservableCollections; | |
18 | import org.jdesktop.observablecollections.ObservableList; | |
19 | import org.slf4j.Logger; | |
20 | import org.slf4j.LoggerFactory; | |
21 | ||
22 | import org.openpermis.editor.policy.beans.PropertyChange; | |
23 | import org.openpermis.policy.bean.PolicyBean; | |
24 | ||
25 | ||
26 | /** | |
27 | * Presenter for the problem list tool. | |
28 | * @since 0.3.0 | |
29 | */ | |
30 | public class ProblemListPresenter | |
31 | extends PartPresenter<PolicyBean> | |
32 | { | |
33 | ||
34 | //---- Static | |
35 | ||
36 | /** | |
37 | * The logger object of this class. | |
38 | * @since 0.1.0 | |
39 | */ | |
40 | 0 | private static final Logger LOGGER = |
41 | LoggerFactory.getLogger(ProblemListPresenter.class); | |
42 | ||
43 | //---- State | |
44 | ||
45 | /** | |
46 | * The list of problems in the policy of this presenter. | |
47 | * @since 0.3.0 | |
48 | */ | |
49 | private final ObservableList<Problem> problemList; | |
50 | ||
51 | //---- Constructors | |
52 | ||
53 | /** | |
54 | * Creates a problem list presenter. | |
55 | * @param model the policy for which to provide the problem list. | |
56 | * @since 0.3.0 | |
57 | */ | |
58 | public ProblemListPresenter (PolicyBean model, PolicyContext context) { | |
59 | 0 | super(model, context); |
60 | 0 | this.problemList = ObservableCollections.observableList( |
61 | Problem.createProblemList(getModel()) | |
62 | ); | |
63 | 0 | } |
64 | ||
65 | //---- Methods | |
66 | ||
67 | /** | |
68 | * Returns an observable list of problems. | |
69 | * @return an observable list of problems. | |
70 | * @since 0.3.0 | |
71 | */ | |
72 | public ObservableList<Problem> getProblemList () { | |
73 | 0 | return this.problemList; |
74 | } | |
75 | ||
76 | /** | |
77 | * Merges the specified problems with the internal problem list. | |
78 | * @param problems the problems to merge into the internal problem list, | |
79 | * the list of problems passed in is destroyed in the process. | |
80 | * @since 0.3.0 | |
81 | */ | |
82 | protected void mergeProblemList (List<Problem> problems) { | |
83 | 0 | LOGGER.debug("Updating problem list."); |
84 | 0 | final List<Problem> seen = new ArrayList<Problem>(); |
85 | 0 | final ListIterator<Problem> iterator = problems.listIterator(); |
86 | 0 | while (iterator.hasNext()) { |
87 | 0 | final Problem problem = iterator.next(); |
88 | 0 | if (this.problemList.contains(problem)) { |
89 | 0 | LOGGER.debug("Problem [{}] still around.", problem); |
90 | 0 | seen.add(problem); |
91 | 0 | iterator.remove(); |
92 | } | |
93 | 0 | } |
94 | 0 | final ListIterator<Problem> removeUnseen = this.problemList.listIterator(); |
95 | 0 | while (removeUnseen.hasNext()) { |
96 | 0 | final Problem problem = removeUnseen.next(); |
97 | 0 | if (!seen.contains(problem)) { |
98 | 0 | LOGGER.debug("Problem [{}] vanished.", problem); |
99 | 0 | removeUnseen.remove(); |
100 | } | |
101 | 0 | } |
102 | 0 | final Iterator<Problem> newProblems = problems.iterator(); |
103 | 0 | while (newProblems.hasNext()) { |
104 | 0 | final Problem problem = newProblems.next(); |
105 | 0 | LOGGER.debug("New problem [{}] found.", problem); |
106 | 0 | this.problemList.add(problem); |
107 | 0 | } |
108 | 0 | } |
109 | ||
110 | //---- PropertyChanges | |
111 | ||
112 | /** | |
113 | * Handles changes in the underlying policy. | |
114 | * @since 0.3.0 | |
115 | */ | |
116 | @PropertyChange(bean = PolicyBean.class) | |
117 | public void policyChanged () { | |
118 | 0 | mergeProblemList(Problem.createProblemList(getModel())); |
119 | 0 | } |
120 | ||
121 | } |