Commit 5f65cfc1 authored by Dumoulin Nicolas's avatar Dumoulin Nicolas
Browse files

* new: if field is null, a button allow to instanciate it with type declared...

 * new: if field is null, a button allow to instanciate it with type declared or given via Meta-inf services
 * example added in demo, works for the File attribute, but not yet for the list, because the tree isn't updated
parent 6737f702
......@@ -13,4 +13,18 @@
<exec.executable>java</exec.executable>
</properties>
</action>
<action>
<actionName>debug</actionName>
<preAction>build-with-dependencies</preAction>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.1.1:exec</goal>
</goals>
<properties>
<exec.classpathScope>runtime</exec.classpathScope>
<exec.args>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath ${packageClassName}</exec.args>
<jpda.listen>true</jpda.listen>
<exec.executable>java</exec.executable>
</properties>
</action>
</actions>
......@@ -6,6 +6,7 @@ import fr.cemagref.ohoui.reflect.IntrospectionException;
import fr.cemagref.ohoui.ui.swing.NodesAndPanelsPanel;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
......@@ -28,6 +29,8 @@ class MyClass {
double d = 0.5;
String string = "hello";
List<Number> numbers = new ArrayList<Number>();
File file;
List<Number> numbers2;
public MyClass() {
numbers.add(1);
......
......@@ -31,6 +31,11 @@
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
......
......@@ -26,6 +26,10 @@
<groupId>com.sun</groupId>
<artifactId>jlfgr</artifactId>
</dependency>
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-swing-outline</artifactId>
......@@ -41,3 +45,4 @@
</project>
......@@ -20,13 +20,19 @@ import fr.cemagref.ohoui.structure.OhObject;
import fr.cemagref.ohoui.structure.OhObjectComplex;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.objenesis.ObjenesisHelper;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
/**
* Default implementation of editor for an ohObjectComplex, it produces an editor by
......@@ -37,24 +43,62 @@ import org.openide.util.Exceptions;
public class DefaultObjectEditor extends AbstractOhEditor<Object, JComponent> {
private List<AbstractOhEditor> childrenEditors = new ArrayList<AbstractOhEditor>();
private JPanel pagePanel = new JPanel();
public DefaultObjectEditor(OhObjectComplex<?> object) {
super(object);
panel = new JPanel();
JPanel panel2 = new JPanel();
panel.add(panel2, BorderLayout.NORTH);
panel2.setLayout(new BoxLayout(panel2, BoxLayout.PAGE_AXIS));
panel.add(pagePanel, BorderLayout.NORTH);
pagePanel.setLayout(new BoxLayout(pagePanel, BoxLayout.PAGE_AXIS));
for (OhObject child : object) {
List<Class> availableTypes = new ArrayList<Class>();
availableTypes.add(child.getDeclaredType());
availableTypes.addAll(Lookup.getDefault().lookupResult(child.getDeclaredType()).allClasses());
// TODO filter added types to retain only instanciable types
if (child.getValue() == null) {
// TODO add button for object creation
} else if (EditorsProvider.hasEditorFor(child)) {
AbstractOhEditor<Object, JComponent> editor = EditorsProvider.createEditorFor(child);
panel2.add((JComponent) editor.getPanel());
// object has null value, so building specific panel for instanciating it
OhEditorPanel editor = new OhEditorPanel(object) {
@Override
public Object getValue() {
return null;
}
};
editor.setEnabled(false);
editor.addLabel(child.getName(), child.getDescription());
if (availableTypes.size() == 0) {
editor.addComponent(new JLabel("null"), "No type have been found to affect a new instance in this variable.");
} else {
editor.addComponent(ControlsFactory.makeButton("general/New16",
new EditTypeAction(this, editor, child, availableTypes.toArray(new Class[]{}), childrenEditors.size()),
"", "New"), "Create a new instance");
}
pagePanel.add((JComponent) editor.getPanel());
childrenEditors.add(editor);
} else if (EditorsProvider.hasEditorFor(child)) {
// adding provided editor
addEditorFor(child);
}
}
}
private void addEditorFor(OhObject object, int index) {
AbstractOhEditor<Object, JComponent> editor = EditorsProvider.createEditorFor(object);
if (editor != null) {
// TODO add button for reinstanciate object
pagePanel.add((JComponent) editor.getPanel(), index);
childrenEditors.add(editor);
} else {
// TODO it shouldn't occur, but as collections don't have editor, it can
pagePanel.add(new JPanel(), index);
}
pagePanel.revalidate();
}
private void addEditorFor(OhObject object) {
addEditorFor(object, childrenEditors.size());
}
@Override
public boolean isEnabled() {
return panel.isEnabled();
......@@ -78,6 +122,7 @@ public class DefaultObjectEditor extends AbstractOhEditor<Object, JComponent> {
@Override
public Object getValue() {
// update objects values for each registered editors
for (AbstractOhEditor editor : childrenEditors) {
if (editor.isEnabled()) {
try {
......@@ -91,4 +136,53 @@ public class DefaultObjectEditor extends AbstractOhEditor<Object, JComponent> {
}
return getOhObject();
}
/**
* Action to edit the type of an object and update the editor.
*/
private final class EditTypeAction implements ActionListener {
private DefaultObjectEditor container;
private OhEditorPanel currentObjectEditor;
private OhObject ohObject;
private Class[] availableTypes;
private int editorIndex;
public EditTypeAction(DefaultObjectEditor container, OhEditorPanel currentObjectEditor, OhObject ohObject, Class[] availableTypes, int editorIndex) {
this.container = container;
this.currentObjectEditor = currentObjectEditor;
this.ohObject = ohObject;
this.availableTypes = availableTypes;
this.editorIndex = editorIndex;
}
@Override
public void actionPerformed(ActionEvent e) {
Class chosenType;
if (availableTypes.length == 1) {
chosenType = availableTypes[0];
} else {
// bring a dialog to choose the type
chosenType = (Class) JOptionPane.showInputDialog(panel,
"Choose the type you want to use", "Choose a type", JOptionPane.QUESTION_MESSAGE, null,
availableTypes, availableTypes[0]);
}
if (chosenType != null) {
try {
// instanciate
ohObject.setValue(ObjenesisHelper.newInstance(chosenType));
// remove current editor
childrenEditors.remove(currentObjectEditor);
pagePanel.remove(editorIndex);
// build the corresponding panel and add the panel in place
container.addEditorFor(ohObject, editorIndex);
// TODO notify the tree if a list is added for example
} catch (IllegalArgumentException ex) {
Exceptions.printStackTrace(ex);
} catch (IllegalAccessException ex) {
Exceptions.printStackTrace(ex);
}
}
}
}
}
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