1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.edit.internal

File DefaultEditorManager.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

12
30
6
1
136
93
14
0.47
5
6
2.33

Classes

Class Line # Actions
DefaultEditorManager 49 30 0% 14 11
0.770833377.1%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Default {@link EditorManager} implementation.
43    *
44    * @version $Id: a755ad08a54965237c44f409ad7cf331c9e76096 $
45    * @since 8.2RC1
46    */
47    @Component
48    @Singleton
 
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   
 
58  73 toggle @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    // No editors found.
66  0 return Collections.emptyList();
67    }
68    }
69   
 
70  73 toggle @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   
 
82  264 toggle @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    // No such editor component found.
96  0 return null;
97    }
98    }
99   
 
100  0 toggle @Override
101    public <D> Editor<D> getDefaultEditor(Type dataType)
102    {
103  0 return getDefaultEditor(dataType, null);
104    }
105   
 
106  337 toggle @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    // There's no editor configured for the specified data type and category. See if there is any editor
113    // available in the specified category. This way we don't force the user to configure the default editor for
114    // each category when there's only one editor available per category.
115  73 List<Editor<D>> editors = getEditors(dataType, category);
116  73 if (editors.isEmpty()) {
117    // See if there is any editor available for the specified data type.
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   
 
127  264 toggle private <D> Editor<D> getEditorWithFallBackOnCategory(Type dataType, String roleHint)
128    {
129  264 Editor<D> editor = getEditor(dataType, roleHint);
130  264 if (editor == null) {
131    // Consider the roleHint to be a category of editors and return the default one.
132  0 editor = getDefaultEditor(dataType, roleHint);
133    }
134  264 return editor;
135    }
136    }