Commit 8a544de5 authored by Dumoulin Nicolas's avatar Dumoulin Nicolas
Browse files

better collection management (addition of element is working now)

parent 55fb571d
......@@ -20,17 +20,37 @@ public class CollectionIntrospector {
int i = 0;
if (objectCollection.getValue() != null) {
for (Object element : objectCollection.getValue()) {
// invoke introspection on each element
Type subCollectionGenericType = null;
if (Collection.class.isAssignableFrom(element.getClass())) {
subCollectionGenericType = getItemClass(objectCollection.getDeclaredItemClass());
}
objectCollection.addChild(DefaultObjectIntrospector.introspectChild(ohOui, objectCollection.getDeclaredItemClass(), "" + i++, element, subCollectionGenericType, fieldFilters));
addChild(element, objectCollection, ohOui, i, fieldFilters);
i++;
}
}
return objectCollection;
}
/**
* Add a child in a collection and introspect the object
* @param element object to add
* @param objectCollection the collection in which to add the child
* @param ohOui the ohOui context handler
* @param i the index of the child in the collection
* @param fieldFilters
* @throws IntrospectionException
*/
public static void addChild(Object element, OhObjectCollection objectCollection, OhOuiContext ohOui, int i, FieldFilter[] fieldFilters) throws IntrospectionException {
// invoke introspection on each element
Type subCollectionGenericType = null;
if (Collection.class.isAssignableFrom(element.getClass())) {
subCollectionGenericType = getItemClass(objectCollection.getDeclaredItemClass());
}
objectCollection.addChild(DefaultObjectIntrospector.introspectChild(
ohOui,
objectCollection.getDeclaredItemClass(),
"[" + i + "]",
element,
subCollectionGenericType,
fieldFilters));
}
/**
* <code>getItemClass</code> retrieves the class allowed for the items of
* the field passed as argument. In fact, the class depends of the
......
......@@ -22,6 +22,7 @@
*/
package fr.cemagref.ohoui.ui.swing;
import fr.cemagref.ohoui.filters.FieldFilter;
import fr.cemagref.ohoui.reflect.DefaultObjectIntrospector;
import fr.cemagref.ohoui.reflect.IntrospectionException;
import fr.cemagref.ohoui.reflect.OhOuiContext;
......@@ -33,7 +34,9 @@ import fr.cemagref.ohoui.ui.swing.panels.DefaultObjectEditor;
import fr.cemagref.ohoui.ui.swing.panels.EditorsProvider;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import javax.swing.JComponent;
......@@ -58,6 +61,8 @@ public class NodesAndPanelsPanel extends JPanel implements ListSelectionListener
private final static Dimension editorMinSize = new Dimension(200, 100);
private final static Dimension editorMaxSize = new Dimension(32767, 32767);
private final static Dimension editorPreferredSize = new Dimension(200, 400);
private OhOuiContext ohOuiContext;
private List<FieldFilter> fieldFilters;
private Outline outline;
private ListSelectionModel selectionModel;
private OutlineModel outlineModel;
......@@ -68,8 +73,10 @@ public class NodesAndPanelsPanel extends JPanel implements ListSelectionListener
/** Creates new form NodesAndPanelsPanel */
public NodesAndPanelsPanel(Object o) throws IntrospectionException {
ohOuiContext = new OhOuiContext();
fieldFilters = new ArrayList<FieldFilter>();
// build data model
OhObjectComplex ohObjectComplex = DefaultObjectIntrospector.introspect("test", o, new OhOuiContext());
OhObjectComplex ohObjectComplex = DefaultObjectIntrospector.introspect("test", o, ohOuiContext, getFieldFiltersArray());
treeModel = new OhTreeModel(ohObjectComplex);
outlineModel = DefaultOutlineModel.createOutlineModel(treeModel, new OhRowModel());
// build form and adjust UI settings
......@@ -100,6 +107,18 @@ public class NodesAndPanelsPanel extends JPanel implements ListSelectionListener
return outline;
}
public OhTreeModel getTreeModel() {
return treeModel;
}
public OhOuiContext getOhOuiContext() {
return ohOuiContext;
}
public final FieldFilter[] getFieldFiltersArray() {
return fieldFilters.toArray(new FieldFilter[]{});
}
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
......@@ -151,7 +170,7 @@ public class NodesAndPanelsPanel extends JPanel implements ListSelectionListener
*/
public Object applyChanges() {
// the DefaultObjectEditor will recursively apply changes
// TODO how to manage objects that appears in tree (skipped because of
// FIXME how to manage objects that appears in tree (skipped because of
// EditorsProvider.hasEditorFor(child) in DefaultObjectEditor constructor)
OhObjectComplex rootOhObjectComplex = ((OhComplexTreeNode) treeModel.getRoot()).getContent();
AbstractOhEditor<Object, JComponent> rootEditor = editorsMap.get(rootOhObjectComplex);
......
/*
* Copyright (C) 2010 Cemagref
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.cemagref.ohoui.ui.swing;
import fr.cemagref.ohoui.structure.OhObjectCollection;
import fr.cemagref.ohoui.structure.OhObjectComplex;
/**
*
* @author Nicolas Dumoulin <nicolas.dumoulin@cemagref.fr>
*/
public class OhCollectionTreeNode extends OhComplexTreeNode<OhObjectCollection> {
public OhCollectionTreeNode(OhComplexTreeNode parent, OhObjectCollection ohObjectComplex) {
super(parent, ohObjectComplex);
}
@Override
protected OhObjectComplex getOhChildAt(int childIndex) {
return (OhObjectComplex) this.content.getChildren().get(childIndex);
}
@Override
public int getChildCount() {
return this.content.getChildren().size();
}
@Override
public int getIndex(OhComplexTreeNode node) {
return this.content.getChildren().indexOf(node);
}
}
......@@ -14,7 +14,6 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.cemagref.ohoui.ui.swing;
import fr.cemagref.ohoui.structure.OhObject;
......@@ -28,14 +27,14 @@ import java.util.List;
*
* @author Nicolas Dumoulin <nicolas.dumoulin@cemagref.fr>
*/
public class OhComplexTreeNode {
public class OhComplexTreeNode<T extends OhObjectComplex<?>> {
private OhComplexTreeNode parent;
private OhObjectComplex ohObjectComplex;
protected T content;
private List<OhObjectComplex> complexChilds;
public OhComplexTreeNode(OhComplexTreeNode parent, OhObjectComplex<?> ohObjectComplex) {
this.ohObjectComplex = ohObjectComplex;
public OhComplexTreeNode(OhComplexTreeNode parent, T ohObjectComplex) {
this.content = ohObjectComplex;
this.complexChilds = new ArrayList<OhObjectComplex>();
for (OhObject object : ohObjectComplex) {
if (((object instanceof OhObjectComplex) && !(EditorsProvider.hasEditorFor(object))) || (ohObjectComplex instanceof OhObjectCollection)) {
......@@ -44,16 +43,25 @@ public class OhComplexTreeNode {
}
}
public OhObjectComplex getContent() {
return ohObjectComplex;
public T getContent() {
return content;
}
public String getName() {
return ohObjectComplex.getName();
return content.getName();
}
protected OhObjectComplex getOhChildAt(int childIndex) {
return complexChilds.get(childIndex);
}
public OhComplexTreeNode getChildAt(int childIndex) {
return new OhComplexTreeNode(this, complexChilds.get(childIndex));
OhObjectComplex child = getOhChildAt(childIndex);
if (child instanceof OhObjectCollection) {
return new OhCollectionTreeNode(this, (OhObjectCollection) child);
} else {
return new OhComplexTreeNode(this, child);
}
}
public int getChildCount() {
......@@ -65,7 +73,6 @@ public class OhComplexTreeNode {
}
public int getIndex(OhComplexTreeNode node) {
return complexChilds.indexOf(((OhComplexTreeNode)node).ohObjectComplex);
return complexChilds.indexOf(((OhComplexTreeNode) node).content);
}
}
......@@ -17,6 +17,9 @@
package fr.cemagref.ohoui.ui.swing;
import fr.cemagref.ohoui.structure.OhObjectComplex;
import java.util.ArrayList;
import java.util.List;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
......@@ -27,9 +30,11 @@ import javax.swing.tree.TreePath;
*/
public class OhTreeModel implements TreeModel {
private List<TreeModelListener> treeModelListeners;
private OhComplexTreeNode root;
public OhTreeModel(OhObjectComplex root) {
this.treeModelListeners = new ArrayList<TreeModelListener>();
this.root = new OhComplexTreeNode(null, root);
}
......@@ -63,13 +68,20 @@ public class OhTreeModel implements TreeModel {
return ((OhComplexTreeNode) parent).getIndex((OhComplexTreeNode) child);
}
public void addChild() {
// TODO finer event on child updated
for (TreeModelListener treeModelListener : treeModelListeners) {
treeModelListener.treeStructureChanged(new TreeModelEvent(this, new Object[]{root}));
}
}
@Override
public void addTreeModelListener(TreeModelListener l) {
// Nothing
treeModelListeners.add(l);
}
@Override
public void removeTreeModelListener(TreeModelListener l) {
// Nothing
treeModelListeners.remove(l);
}
}
......@@ -16,6 +16,8 @@
*/
package fr.cemagref.ohoui.ui.swing.actions;
import fr.cemagref.ohoui.reflect.CollectionIntrospector;
import fr.cemagref.ohoui.reflect.IntrospectionException;
import fr.cemagref.ohoui.structure.OhObjectCollection;
import fr.cemagref.ohoui.ui.swing.NodesAndPanelsPanel;
import fr.cemagref.ohoui.ui.swing.OhComplexTreeNode;
......@@ -26,7 +28,6 @@ import java.util.Iterator;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.event.TableModelEvent;
import org.objenesis.ObjenesisHelper;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
......@@ -66,8 +67,8 @@ public class AddElementInCollection extends AbstractAction {
Class chosenType;
if (availableTypes.isEmpty()) {
JOptionPane.showMessageDialog(panel, "It is impossible to add a new "
+ "element as no implementation of the type"
+ baseType + "has been configured",
+ "element: No implementation of the type "
+ baseType + " has been configured",
"Creation not possible", JOptionPane.ERROR_MESSAGE);
// TODO propose to try to find an implementation
return;
......@@ -81,10 +82,17 @@ public class AddElementInCollection extends AbstractAction {
}
if (chosenType != null) {
try {
// instanciate
ohCollection.getValue().add(ObjenesisHelper.newInstance(chosenType));
// TODO notify the tree
panel.getOutline().tableChanged(new TableModelEvent(panel.getOutline().getOutlineModel()));
Object newInstance = ObjenesisHelper.newInstance(chosenType);
// instanciate and add in the collection
ohCollection.getValue().add(newInstance);
CollectionIntrospector.addChild(newInstance, ohCollection, panel.getOhOuiContext(), ohCollection.getValue().size() - 1, panel.getFieldFiltersArray());
// notify the tree
panel.getTreeModel().addChild();
// doesn't work:
// panel.getOutline().tableChanged(new TableModelEvent(panel.getOutline().getOutlineModel()));
// panel.getOutline().revalidate();
} catch (IntrospectionException ex) {
Exceptions.printStackTrace(ex);
} catch (IllegalArgumentException ex) {
Exceptions.printStackTrace(ex);
}
......
/*
* Copyright (C) 2010 Cemagref
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Provides abstract classes and utility classes for editor definition.
*/
package fr.cemagref.ohoui.ui.swing.panels;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!--
Copyright (C) 2010 Cemagref
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
</head>
<body>
Provides abstract classes and utility classes for editor definition.
</body>
</html>
/*
* Copyright (C) 2010 Cemagref
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Provides editors and their producers for common types.
*/
package fr.cemagref.ohoui.ui.swing.panels.producers;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!--
Copyright (C) 2010 Cemagref
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
</head>
<body>
Provides editors and their producers for common types.
</body>
</html>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment