1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.edit.internal; |
21 |
|
|
22 |
|
import java.lang.reflect.Type; |
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Objects; |
27 |
|
|
28 |
|
import javax.inject.Inject; |
29 |
|
import javax.inject.Named; |
30 |
|
import javax.inject.Provider; |
31 |
|
import javax.inject.Singleton; |
32 |
|
|
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.component.manager.ComponentLookupException; |
35 |
|
import org.xwiki.component.manager.ComponentManager; |
36 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
37 |
|
import org.xwiki.edit.EditConfiguration; |
38 |
|
import org.xwiki.edit.Editor; |
39 |
|
import org.xwiki.edit.EditorManager; |
40 |
|
|
41 |
|
|
42 |
|
@link |
43 |
|
|
44 |
|
@version |
45 |
|
@since |
46 |
|
|
47 |
|
@Component |
48 |
|
@Singleton |
|
|
| 77.1% |
Uncovered Elements: 11 (48) |
Complexity: 14 |
Complexity Density: 0.47 |
|
49 |
|
public class DefaultEditorManager implements EditorManager |
50 |
|
{ |
51 |
|
@Inject |
52 |
|
private EditConfiguration configuration; |
53 |
|
|
54 |
|
@Inject |
55 |
|
@Named("context") |
56 |
|
private Provider<ComponentManager> componentManagerProvider; |
57 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
58 |
73 |
@Override... |
59 |
|
public <D> List<Editor<D>> getEditors(Type dataType) |
60 |
|
{ |
61 |
73 |
DefaultParameterizedType editorType = new DefaultParameterizedType(null, Editor.class, dataType); |
62 |
73 |
try { |
63 |
73 |
return this.componentManagerProvider.get().getInstanceList(editorType); |
64 |
|
} catch (ComponentLookupException e) { |
65 |
|
|
66 |
0 |
return Collections.emptyList(); |
67 |
|
} |
68 |
|
} |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
70 |
73 |
@Override... |
71 |
|
public <D> List<Editor<D>> getEditors(Type dataType, String category) |
72 |
|
{ |
73 |
73 |
List<Editor<D>> editors = new ArrayList<>(); |
74 |
73 |
for (Editor<D> editor : this.<D>getEditors(dataType)) { |
75 |
219 |
if (Objects.equals(category, editor.getDescriptor().getCategory())) { |
76 |
73 |
editors.add(editor); |
77 |
|
} |
78 |
|
} |
79 |
73 |
return editors; |
80 |
|
} |
81 |
|
|
|
|
| 66.7% |
Uncovered Elements: 3 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
82 |
264 |
@Override... |
83 |
|
public <D> Editor<D> getEditor(Type dataType, String hint) |
84 |
|
{ |
85 |
264 |
DefaultParameterizedType editorType = new DefaultParameterizedType(null, Editor.class, dataType); |
86 |
264 |
ComponentManager componentManager = this.componentManagerProvider.get(); |
87 |
264 |
if (componentManager.hasComponent(editorType, hint)) { |
88 |
264 |
try { |
89 |
264 |
return componentManager.getInstance(editorType, hint); |
90 |
|
} catch (ComponentLookupException e) { |
91 |
0 |
throw new RuntimeException(String.format("Failed to look up the [%s] editor with hint [%s]", |
92 |
|
dataType.getTypeName(), hint), e); |
93 |
|
} |
94 |
|
} else { |
95 |
|
|
96 |
0 |
return null; |
97 |
|
} |
98 |
|
} |
99 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
100 |
0 |
@Override... |
101 |
|
public <D> Editor<D> getDefaultEditor(Type dataType) |
102 |
|
{ |
103 |
0 |
return getDefaultEditor(dataType, null); |
104 |
|
} |
105 |
|
|
|
|
| 80% |
Uncovered Elements: 3 (15) |
Complexity: 4 |
Complexity Density: 0.44 |
|
106 |
337 |
@Override... |
107 |
|
public <D> Editor<D> getDefaultEditor(Type dataType, String category) |
108 |
|
{ |
109 |
337 |
Editor<D> defaultEditor; |
110 |
337 |
String defaultEditorHint = this.configuration.getDefaultEditor(dataType, category); |
111 |
337 |
if (defaultEditorHint == null) { |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
73 |
List<Editor<D>> editors = getEditors(dataType, category); |
116 |
73 |
if (editors.isEmpty()) { |
117 |
|
|
118 |
0 |
editors = getEditors(dataType); |
119 |
|
} |
120 |
73 |
defaultEditor = editors.isEmpty() ? null : editors.get(0); |
121 |
|
} else { |
122 |
264 |
defaultEditor = getEditorWithFallBackOnCategory(dataType, defaultEditorHint); |
123 |
|
} |
124 |
337 |
return defaultEditor; |
125 |
|
} |
126 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
127 |
264 |
private <D> Editor<D> getEditorWithFallBackOnCategory(Type dataType, String roleHint)... |
128 |
|
{ |
129 |
264 |
Editor<D> editor = getEditor(dataType, roleHint); |
130 |
264 |
if (editor == null) { |
131 |
|
|
132 |
0 |
editor = getDefaultEditor(dataType, roleHint); |
133 |
|
} |
134 |
264 |
return editor; |
135 |
|
} |
136 |
|
} |