Skip to content
Snippets Groups Projects
Commit 308cb80c authored by GIRARDOT Raphael's avatar GIRARDOT Raphael Committed by MADELA Patrick
Browse files

Use Map instead of Collection for IView storage

parent 1377d9f8
No related branches found
No related tags found
No related merge requests found
......@@ -8,35 +8,36 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.prefs.Preferences;
import javax.swing.Action;
import fr.soleil.docking.action.ViewAction;
import fr.soleil.lib.project.ObjectUtils;
/**
* Basic implementation of {@link IViewFactory}
*
* @author girardot
*/
public class ViewFactory implements IViewFactory {
protected final Collection<IView> views;
protected final Map<Object, IView> views;
protected final PropertyChangeSupport support;
public ViewFactory() {
super();
views = Collections.newSetFromMap(new ConcurrentHashMap<IView, Boolean>());
views = new ConcurrentHashMap<Object, IView>();
support = new PropertyChangeSupport(this);
}
@Override
public IView getView(Object id) {
IView result = null;
for (IView view : views) {
if (ObjectUtils.sameObject(id, view.getId())) {
result = view;
break;
}
if (id != null) {
result = views.get(id);
}
return result;
}
......@@ -74,19 +75,38 @@ public class ViewFactory implements IViewFactory {
@Override
public IView addView(IView view) {
if (views.add(view)) {
support.firePropertyChange(VIEWS, null, view);
} else {
view = null;
IView added = null;
if (view != null) {
Object id = view.getId();
if (id != null) {
if (!views.containsKey(id)) {
views.put(id, view);
if (views.get(id) == view) {
added = view;
}
}
}
}
return view;
if (added != null) {
synchronized (support) {
support.firePropertyChange(VIEWS, null, view);
}
}
return added;
}
@Override
public IView removeView(Object id) {
IView toRemove = getView(id);
if (!removeView(toRemove)) {
IView toRemove;
if (id == null) {
toRemove = null;
} else {
toRemove = views.remove(id);
if (toRemove != null) {
synchronized (support) {
support.firePropertyChange(VIEWS, toRemove, null);
}
}
}
return toRemove;
}
......@@ -97,9 +117,13 @@ public class ViewFactory implements IViewFactory {
if (view == null) {
result = false;
} else {
result = views.remove(view);
if (result) {
support.firePropertyChange(VIEWS, view, null);
Object id = view.getId();
IView temp = getView(id);
if (temp == view) {
result = true;
removeView(id);
} else {
result = false;
}
}
return result;
......@@ -108,8 +132,10 @@ public class ViewFactory implements IViewFactory {
@Override
public List<Action> getActionList() {
List<Action> result = new ArrayList<Action>(views.size());
for (IView view : views) {
result.add(new ViewAction(view));
for (IView view : views.values()) {
if (view != null) {
result.add(new ViewAction(view));
}
}
return result;
}
......@@ -121,7 +147,7 @@ public class ViewFactory implements IViewFactory {
@Override
public Collection<IView> getViews() {
return views;
return views.values();
}
@Override
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment