Commit 98106549 authored by Dumoulin Nicolas's avatar Dumoulin Nicolas
Browse files

* node class introduced in treemodel

 * simple field are not displayed in the tree
 * begin of implementation for applying modification
parent 3fd71f05
......@@ -97,7 +97,7 @@ public class NodesAndPanelsPanel extends JPanel implements ListSelectionListener
} else {
int selectedRow = lsm.getMinSelectionIndex();
LOGGER.fine("Selection : " + selectedRow + " - " + outlineModel.getValueAt(selectedRow, 0));
OhObject selectedObject = (OhObject) outlineModel.getValueAt(selectedRow, 0);
OhObject selectedObject = ((OhComplexTreeNode) outlineModel.getValueAt(selectedRow, 0)).getContent();
if (editorsMap.containsKey(selectedObject)) {
// editor is already in cache
editorsCardLayout.show(editorsPanel, getCode(selectedObject));
......@@ -110,7 +110,7 @@ public class NodesAndPanelsPanel extends JPanel implements ListSelectionListener
} else if (selectedObject instanceof OhObjectCollection) {
// TODO bring an editor with a list and add/remove buttons
} else if (selectedObject instanceof OhObjectComplex) {
editor = new DefaultObjectEditor(selectedObject);
editor = new DefaultObjectEditor((OhObjectComplex<?>) selectedObject);
} else {
// TODO pay attention if object value is null ?
editor = EditorsProvider.createEditorFor(selectedObject);
......@@ -131,6 +131,17 @@ public class NodesAndPanelsPanel extends JPanel implements ListSelectionListener
return Integer.toString(selectedObject.hashCode());
}
/**
* Apply all the modifications done in the forms to the edited object.
* @return the object edited after applying all the modifications.
*/
public Object applyChanges() {
// TODO
// - browse the tree and invoke AbstractOhEditor.getValue ?
// - beware of the parts of tree that are involved in several editors
return ((OhComplexTreeNode)treeModel.getRoot()).getContent().getValue();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
......
/*
* 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.nodesandpanels;
import fr.cemagref.ohoui.structure.OhObject;
import fr.cemagref.ohoui.structure.OhObjectCollection;
import fr.cemagref.ohoui.structure.OhObjectComplex;
import fr.cemagref.ohoui.ui.panels.EditorsProvider;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Nicolas Dumoulin <nicolas.dumoulin@cemagref.fr>
*/
public class OhComplexTreeNode {
private OhComplexTreeNode parent;
private OhObjectComplex ohObjectComplex;
private List<OhObjectComplex> complexChilds;
public OhComplexTreeNode(OhComplexTreeNode parent, OhObjectComplex<?> ohObjectComplex) {
this.ohObjectComplex = ohObjectComplex;
this.complexChilds = new ArrayList<OhObjectComplex>();
for (OhObject object : ohObjectComplex) {
if (((object instanceof OhObjectComplex) && !(EditorsProvider.hasEditorFor(object))) || (ohObjectComplex instanceof OhObjectCollection)) {
complexChilds.add((OhObjectComplex) object);
}
}
}
public OhObjectComplex getContent() {
return ohObjectComplex;
}
public String getName() {
return ohObjectComplex.getName();
}
public OhComplexTreeNode getChildAt(int childIndex) {
return new OhComplexTreeNode(this, complexChilds.get(childIndex));
}
public int getChildCount() {
return complexChilds.size();
}
public OhComplexTreeNode getParent() {
return parent;
}
public int getIndex(OhComplexTreeNode node) {
return complexChilds.indexOf(((OhComplexTreeNode)node).ohObjectComplex);
}
}
......@@ -17,7 +17,6 @@
package fr.cemagref.ohoui.ui.nodesandpanels;
import fr.cemagref.ohoui.structure.OhObject;
import java.awt.Color;
import javax.swing.Icon;
import org.netbeans.swing.outline.RenderDataProvider;
......@@ -30,7 +29,7 @@ public class OhDataProvider implements RenderDataProvider {
@Override
public String getDisplayName(Object o) {
return ((OhObject)o).getName();
return ((OhComplexTreeNode)o).getName();
}
@Override
......
......@@ -16,9 +16,7 @@
*/
package fr.cemagref.ohoui.ui.nodesandpanels;
import fr.cemagref.ohoui.structure.OhObject;
import fr.cemagref.ohoui.structure.OhObjectComplex;
import fr.cemagref.ohoui.ui.panels.EditorsProvider;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
......@@ -29,10 +27,10 @@ import javax.swing.tree.TreePath;
*/
public class OhTreeModel implements TreeModel {
private OhObjectComplex root;
private OhComplexTreeNode root;
public OhTreeModel(OhObjectComplex root) {
this.root = root;
this.root = new OhComplexTreeNode(null, root);
}
@Override
......@@ -42,18 +40,17 @@ public class OhTreeModel implements TreeModel {
@Override
public Object getChild(Object parent, int index) {
return ((OhObjectComplex) parent).getChildren().get(index);
return ((OhComplexTreeNode) parent).getChildAt(index);
}
@Override
public int getChildCount(Object parent) {
return ((OhObjectComplex) parent).getChildren().size();
return ((OhComplexTreeNode) parent).getChildCount();
}
@Override
public boolean isLeaf(Object node) {
OhObject ohObject = (OhObject) node;
return (!(node instanceof OhObjectComplex) || (EditorsProvider.hasEditorFor(ohObject)));
return getChildCount(node) == 0;
}
@Override
......@@ -63,7 +60,7 @@ public class OhTreeModel implements TreeModel {
@Override
public int getIndexOfChild(Object parent, Object child) {
return ((OhObjectComplex) parent).getChildren().indexOf(child);
return ((OhComplexTreeNode) parent).getIndex((OhComplexTreeNode) child);
}
@Override
......
......@@ -20,41 +20,51 @@ import fr.cemagref.ohoui.structure.OhObject;
import fr.cemagref.ohoui.structure.OhObjectComplex;
import java.awt.Component;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JPanel;
/**
* Default implementation of editor for an ohObjectComplex, it produces an editor by
* assembling editors for each field.
*
* @author Nicolas Dumoulin <nicolas.dumoulin@cemagref.fr>
*/
public class DefaultObjectEditor extends AbstractOhEditor<Object, JComponent> {
public DefaultObjectEditor(OhObject object) {
private List<FieldAndEditor> fieldAndOUIPanels = new ArrayList<FieldAndEditor>();
public DefaultObjectEditor(OhObjectComplex<?> object) {
super(object);
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
if (object instanceof OhObjectComplex) {
for (OhObject child : (OhObjectComplex<?>) object) {
if (child.getValue() == null) {
// TODO add button for object creation
} else if (EditorsProvider.hasEditorFor(child)) {
panel.add((JComponent) EditorsProvider.createEditorFor(child).getPanel());
}
for (OhObject child : object) {
if (child.getValue() == null) {
// TODO add button for object creation
} else if (EditorsProvider.hasEditorFor(child)) {
panel.add((JComponent) EditorsProvider.createEditorFor(child).getPanel());
}
}
}
private static class FieldAndOUIPanel {
public Field field;
public OhEditorPanel ouipanel;
public FieldAndOUIPanel(Field field, OhEditorPanel ouipanel) {
this.field = field;
this.ouipanel = ouipanel;
@Override
public OhObject getObject() {
for (FieldAndEditor fieldAndOUIPanel : fieldAndOUIPanels) {
if (fieldAndOUIPanel.editor.isEnabled()) {
try {
fieldAndOUIPanel.field.set(super.object, fieldAndOUIPanel.editor.getObject().getValue());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
System.err.println("Error while trying to modify : " + fieldAndOUIPanel.field);
e.printStackTrace();
}
}
}
return super.object;
}
@Override
......@@ -77,4 +87,15 @@ public class DefaultObjectEditor extends AbstractOhEditor<Object, JComponent> {
subComponent.setEnabled(enabled);
}
}
private static class FieldAndEditor {
public Field field;
public OhEditorPanel editor;
public FieldAndEditor(Field field, OhEditorPanel editor) {
this.field = field;
this.editor = editor;
}
}
}
......@@ -25,16 +25,18 @@ public class OhEditorPanel<T> extends AbstractOhEditor<T, JComponent> {
protected JLabel addLabel(String label, String tooltip) {
JLabel jlabel = new JLabel(label);
jlabel.setPreferredSize(SwingDefaults.labelMinimumSize);
if ((tooltip != null) && (!"".equals(tooltip)))
if ((tooltip != null) && (!"".equals(tooltip))) {
jlabel.setToolTipText(tooltip);
}
panel.add(jlabel);
return jlabel;
}
protected void addComponent(JComponent component, String tooltip) {
component.setAlignmentX(SwingDefaults.labelAlignment);
if (!"".equals(tooltip))
if (!"".equals(tooltip)) {
component.setToolTipText(tooltip);
}
panel.add(component);
}
......
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