Commit 55fb571d authored by Dumoulin Nicolas's avatar Dumoulin Nicolas
Browse files

work in progress: popup menu added for collection populating

parent 5f65cfc1
/*
* 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.ui.swing.actions.AddElementInCollection;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
/**
* Listener that is responsible to trigger a popup menu when a right-click is
* performed on a node.
*
* @author Nicolas Dumoulin <nicolas.dumoulin@cemagref.fr>
*/
public class NodePopupListener extends MouseAdapter {
private JPopupMenu popup;
public NodePopupListener(NodesAndPanelsPanel panel) {
super();
//Create the popup menu.
popup = new JPopupMenu();
// TODO add actions dependending on the selected element
JMenuItem menuItem = new JMenuItem(new AddElementInCollection(panel));
popup.add(menuItem);
// TODO add a submenu that displays types available for adding a new element
}
@Override
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
@Override
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(),
e.getX(), e.getY());
}
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<?xml version="1.1" encoding="UTF-8" ?>
<Form version="1.4" maxVersion="1.7" type="org.netbeans.modules.form.forminfo.JPanelFormInfo">
<AuxValues>
......
......@@ -58,6 +58,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 Outline outline;
private ListSelectionModel selectionModel;
private OutlineModel outlineModel;
private OhTreeModel treeModel;
private Map<Object, AbstractOhEditor<Object, JComponent>> editorsMap;
......@@ -76,13 +78,26 @@ public class NodesAndPanelsPanel extends JPanel implements ListSelectionListener
jSplitPane1.setDividerLocation(jTablePreferredSize.width);
jTable.setPreferredScrollableViewportSize(jTable.getPreferredSize());
// adjust the table renderer and listener
Outline outline = (Outline) jTable;
outline = (Outline) jTable;
outline.setRenderDataProvider(new OhDataProvider());
selectionModel = outline.getSelectionModel();
outline.getSelectionModel().addListSelectionListener(this);
outline.addMouseListener(new NodePopupListener(this));
// init editors
editorsMap = new HashMap<Object, AbstractOhEditor<Object, JComponent>>();
editorsCardLayout = (CardLayout) editorsPanel.getLayout();
editorsPanel.add(new JPanel(), EMPTY_EDITOR_CODE);
// select the root
selectionModel.setSelectionInterval(0, 0);
}
public OhComplexTreeNode getSelectedElement() {
int selectedRow = selectionModel.getMinSelectionIndex();
return (OhComplexTreeNode) outlineModel.getValueAt(selectedRow, 0);
}
public Outline getOutline() {
return outline;
}
@Override
......@@ -90,15 +105,13 @@ public class NodesAndPanelsPanel extends JPanel implements ListSelectionListener
if (e.getValueIsAdjusting()) {
return;
}
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
if (lsm.isSelectionEmpty()) {
if (selectionModel.isSelectionEmpty()) {
// clear right panel
editorsCardLayout.show(editorsPanel, EMPTY_EDITOR_CODE);
LOGGER.fine("No selection");
} else {
int selectedRow = lsm.getMinSelectionIndex();
LOGGER.fine("Selection : " + selectedRow + " - " + outlineModel.getValueAt(selectedRow, 0));
OhObject selectedObject = ((OhComplexTreeNode) outlineModel.getValueAt(selectedRow, 0)).getContent();
LOGGER.fine("Selection : " + selectionModel.getMinSelectionIndex() + " - " + getSelectedElement());
OhObject selectedObject = getSelectedElement().getContent();
if (editorsMap.containsKey(selectedObject)) {
// editor is already in cache
editorsCardLayout.show(editorsPanel, getCode(selectedObject));
......
/*
* 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.actions;
import fr.cemagref.ohoui.structure.OhObjectCollection;
import fr.cemagref.ohoui.ui.swing.NodesAndPanelsPanel;
import fr.cemagref.ohoui.ui.swing.OhComplexTreeNode;
import java.awt.event.ActionEvent;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
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;
/**
*
* @author Nicolas Dumoulin <nicolas.dumoulin@cemagref.fr>
*/
public class AddElementInCollection extends AbstractAction {
private NodesAndPanelsPanel panel;
public AddElementInCollection(NodesAndPanelsPanel panel) {
super("Add an element");
this.panel = panel;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO implement and then extract generic part in OhComplexTreeNode?
OhComplexTreeNode node = panel.getSelectedElement();
assert node.getContent() instanceof OhObjectCollection;
OhObjectCollection ohCollection = (OhObjectCollection) node.getContent();
// Editors retrieving for other types
List<Class> availableTypes = new ArrayList<Class>();
Class baseType = ohCollection.getDeclaredItemClass();
availableTypes.add(baseType);
availableTypes.addAll(Lookup.getDefault().lookupResult(baseType).allClasses());
// remove abstract classes
for (Iterator<Class> it = availableTypes.iterator(); it.hasNext();) {
Class type = it.next();
if ((type.isInterface()) || (Modifier.isAbstract(type.getModifiers()))) {
it.remove();
}
}
// select the type
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",
"Creation not possible", JOptionPane.ERROR_MESSAGE);
// TODO propose to try to find an implementation
return;
} else if (availableTypes.size() == 1) {
chosenType = availableTypes.get(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.toArray(new Class[]{}), availableTypes.get(0));
}
if (chosenType != null) {
try {
// instanciate
ohCollection.getValue().add(ObjenesisHelper.newInstance(chosenType));
// TODO notify the tree
panel.getOutline().tableChanged(new TableModelEvent(panel.getOutline().getOutlineModel()));
} 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/>.
*/
package fr.cemagref.ohoui.ui.swing.actions;
import fr.cemagref.ohoui.ui.swing.NodesAndPanelsPanel;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
/**
*
* @author Nicolas Dumoulin <nicolas.dumoulin@cemagref.fr>
*/
public class DeleteCollectionElement extends AbstractAction {
private NodesAndPanelsPanel panel;
public DeleteCollectionElement(NodesAndPanelsPanel panel) {
super("Delete");
this.panel = panel;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO
panel.getSelectedElement();
}
}
/*
* 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.actions;
import fr.cemagref.ohoui.ui.swing.NodesAndPanelsPanel;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
/**
*
* @author Nicolas Dumoulin <nicolas.dumoulin@cemagref.fr>
*/
public class NewInstance extends AbstractAction {
private NodesAndPanelsPanel panel;
public NewInstance(NodesAndPanelsPanel panel) {
super("Delete");
this.panel = panel;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO
panel.getSelectedElement();
}
}
/*
* 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.actions;
import fr.cemagref.ohoui.ui.swing.NodesAndPanelsPanel;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
/**
*
* @author Nicolas Dumoulin <nicolas.dumoulin@cemagref.fr>
*/
public class ResetToNull extends AbstractAction {
private NodesAndPanelsPanel panel;
public ResetToNull(NodesAndPanelsPanel panel) {
super("Reset to null");
this.panel = panel;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO
panel.getSelectedElement();
}
}
/*
* 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 high-level classes for building an editor based on Swing, and classes
* concerning the tree displaying the structure of the edited object.
*/
package fr.cemagref.ohoui.ui.swing;
<!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 high-level classes for building an editor based on Swing, and classes
concerning the tree displaying the structure of the edited object.
</body>
</html>
......@@ -17,6 +17,7 @@
package fr.cemagref.ohoui.ui.swing.panels;
import fr.cemagref.ohoui.structure.OhObject;
import fr.cemagref.ohoui.structure.OhObjectCollection;
import fr.cemagref.ohoui.structure.OhObjectComplex;
import java.awt.BorderLayout;
import java.awt.Component;
......@@ -51,6 +52,12 @@ public class DefaultObjectEditor extends AbstractOhEditor<Object, JComponent> {
panel.add(pagePanel, BorderLayout.NORTH);
pagePanel.setLayout(new BoxLayout(pagePanel, BoxLayout.PAGE_AXIS));
for (OhObject child : object) {
// No editors produced for collections, all edit features are provided
// by the tree view
if (child instanceof OhObjectCollection) {
continue;
}
// Editors retrieving for other types
List<Class> availableTypes = new ArrayList<Class>();
availableTypes.add(child.getDeclaredType());
availableTypes.addAll(Lookup.getDefault().lookupResult(child.getDeclaredType()).allClasses());
......
java.math.BigDecimal
java.math.BigInteger
java.lang.Byte
java.lang.Short
java.lang.Long
java.lang.Float
java.lang.Double
java.lang.Integer
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