Commit 9aaaee4a authored by Dumoulin Nicolas's avatar Dumoulin Nicolas
Browse files

code simplification

parent 30619c98
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>run</actionName>
<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>-classpath %classpath ${packageClassName}</exec.args>
<exec.executable>java</exec.executable>
</properties>
</action>
</actions>
/*
* 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.panels;
import fr.cemagref.ohoui.structure.OhObject;
......@@ -5,8 +21,10 @@ import java.util.ArrayList;
import java.util.Collection;
/**
*
* Defines common behavior for editors for an object.
*
* @author Nicolas Dumoulin <nicolas.dumoulin@cemagref.fr>
* @param <T> The type of the edited object
* @param <P> The type of graphical component used
*/
public abstract class AbstractOhEditor<T, P> {
......@@ -19,21 +37,19 @@ public abstract class AbstractOhEditor<T, P> {
this.object = object;
}
public OhObject<T> getObject() {
public OhObject<T> getOhObject() {
return object;
}
public void setObject(OhObject<T> object) {
public void setOhObject(OhObject<T> object) {
this.object = object;
}
public void setValue(T value) throws IllegalArgumentException, IllegalAccessException {
object.setValue(value);
}
public T getValue() {
return object.getValue();
}
/**
* Returns the value up-to-date of the edited object.
* @return The object edited
*/
public abstract T getValue();
public P getPanel() {
return panel;
......@@ -60,10 +76,6 @@ public abstract class AbstractOhEditor<T, P> {
protected abstract void recursivelyEnableComponent(P container, boolean enabled);
/* @see fr.cemagref.ohoui.producers.basic.BooleanProducer.BooleanOUIPanel#update() */
public void update() {
}
public static class OUIPanelAndAction<C> {
public AbstractOhEditor<?, C> link;
......
/*
* 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.panels;
import java.awt.event.ActionListener;
......
......@@ -51,11 +51,11 @@ public class DefaultObjectEditor extends AbstractOhEditor<Object, JComponent> {
}
@Override
public OhObject getObject() {
public OhObject getOhObject() {
for (FieldAndEditor fieldAndOUIPanel : fieldAndOUIPanels) {
if (fieldAndOUIPanel.editor.isEnabled()) {
try {
fieldAndOUIPanel.field.set(super.object, fieldAndOUIPanel.editor.getObject().getValue());
fieldAndOUIPanel.field.set(super.object, fieldAndOUIPanel.editor.getOhObject().getValue());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
......@@ -88,6 +88,11 @@ public class DefaultObjectEditor extends AbstractOhEditor<Object, JComponent> {
}
}
@Override
public Object getValue() {
throw new UnsupportedOperationException("Not supported yet.");
}
private static class FieldAndEditor {
public Field field;
......
......@@ -19,12 +19,14 @@ package fr.cemagref.ohoui.ui.swing.panels;
import fr.cemagref.ohoui.structure.OhObject;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import org.openide.util.Lookup;
/**
* Singleton class that provides graphical editors depending on the type
* of the object to edit.
*
* @author Nicolas Dumoulin <nicolas.dumoulin@cemagref.fr>
*/
......@@ -47,7 +49,13 @@ public class EditorsProvider {
}
}
}
LOGGER.info("Producers loaded: " + editorProducers);
if (LOGGER.isLoggable(Level.INFO)) {
StringBuilder buf = new StringBuilder("Producers loaded: ");
for (Map.Entry<Class, OhEditorProducer> producer : editorProducers.entrySet()) {
buf.append(" ").append(producer.getKey()).append(" -> ").append(producer.getValue()).append("\n");
}
LOGGER.info(buf.toString());
}
}
private static EditorsProvider getDefault() {
......@@ -72,6 +80,6 @@ public class EditorsProvider {
if (editor == null) {
// TODO return an new DefaultObjectEditor ? should be avoided upstream
}
return (editor==null) ? null : editor.getEditor(ohObject);
return (editor == null) ? null : editor.getEditor(ohObject);
}
}
......@@ -13,7 +13,7 @@ import javax.swing.JPanel;
*
* @author Nicolas Dumoulin <nicolas.dumoulin@cemagref.fr>
*/
public class OhEditorPanel<T> extends AbstractOhEditor<T, JComponent> {
public abstract class OhEditorPanel<T> extends AbstractOhEditor<T, JComponent> {
protected OhEditorPanel(OhObject<T> object) {
super(object);
......@@ -22,7 +22,7 @@ public class OhEditorPanel<T> extends AbstractOhEditor<T, JComponent> {
this.object = object;
}
protected JLabel addLabel(String label, String tooltip) {
public JLabel addLabel(String label, String tooltip) {
JLabel jlabel = new JLabel(label);
jlabel.setPreferredSize(SwingDefaults.labelMinimumSize);
if ((tooltip != null) && (!"".equals(tooltip))) {
......@@ -32,7 +32,7 @@ public class OhEditorPanel<T> extends AbstractOhEditor<T, JComponent> {
return jlabel;
}
protected void addComponent(JComponent component, String tooltip) {
public void addComponent(JComponent component, String tooltip) {
component.setAlignmentX(SwingDefaults.labelAlignment);
if (!"".equals(tooltip)) {
component.setToolTipText(tooltip);
......@@ -40,21 +40,6 @@ public class OhEditorPanel<T> extends AbstractOhEditor<T, JComponent> {
panel.add(component);
}
/**
* Build a panel for editing an object with a label and a JComponent.
* @param object The object edited
* @param component The component used for displaying/editing
* @param label The label describing the object
* @param tooltip A tooltip displayed on the panel
* @return the panel ready to use
*/
public static <T> OhEditorPanel<T> makeLabelComponentOUIPanel(OhObject<T> object, JComponent component, String label, String tooltip) {
OhEditorPanel<T> panel = new OhEditorPanel<T>(object);
panel.addLabel(label, tooltip);
panel.addComponent(component, tooltip);
return panel;
}
@Override
public boolean isEnabled() {
return panel.isEnabled();
......@@ -75,5 +60,4 @@ public class OhEditorPanel<T> extends AbstractOhEditor<T, JComponent> {
subComponent.setEnabled(enabled);
}
}
}
/*
* 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.panels;
import fr.cemagref.ohoui.structure.OhObject;
......
/*
* 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.panels;
import java.awt.Dimension;
......
package fr.cemagref.ohoui.ui.swing.panels.producers;
import fr.cemagref.ohoui.ui.swing.panels.AbstractOhEditor.OUIPanelAndAction;
import fr.cemagref.ohoui.ui.swing.panels.OhEditorPanel;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import org.openide.util.Exceptions;
public class BooleanListener implements ItemListener {
private OhEditorPanel<Boolean> panel;
public BooleanListener(OhEditorPanel<Boolean> panel) {
this.panel = panel;
}
public void validate() {
boolean value = panel.getValue();
for (OUIPanelAndAction<JComponent> link : panel.getLinks()) {
if (link.action.equals("disable"))
link.link.setEnabled(!value);
else if (link.action.equals("enable")) {
link.link.setEnabled(value);
}
}
}
@Override
public void itemStateChanged(ItemEvent ie) {
try {
boolean value = ((JCheckBox) ie.getSource()).isSelected();
panel.setValue(value);
for (OUIPanelAndAction<JComponent> link : panel.getLinks()) {
if (link.action.equals("disable")) {
link.link.setEnabled(!value);
} else if (link.action.equals("enable")) {
link.link.setEnabled(value);
}
}
} catch (IllegalArgumentException ex) {
Exceptions.printStackTrace(ex);
} catch (IllegalAccessException ex) {
Exceptions.printStackTrace(ex);
}
}
}
package fr.cemagref.ohoui.ui.swing.panels.producers;
import fr.cemagref.ohoui.structure.OhObject;
import fr.cemagref.ohoui.ui.swing.panels.OhEditorPanel;
public class BooleanPanel extends OhEditorPanel<Boolean> {
private BooleanListener booleanListener;
public BooleanPanel(OhObject<Boolean> object) {
super(object);
}
public BooleanListener getBooleanListener() {
return booleanListener;
}
public void setBooleanListener(BooleanListener booleanListener) {
this.booleanListener = booleanListener;
}
@Override
public void update() {
super.update();
booleanListener.validate();
}
}
/**
*
/*
* 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.panels.producers;
import fr.cemagref.ohoui.structure.OhObject;
import fr.cemagref.ohoui.ui.swing.panels.OhEditorPanel;
import fr.cemagref.ohoui.ui.swing.panels.OhEditorProducer;
import java.awt.FlowLayout;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
public class BooleanProducer implements OhEditorProducer<Boolean, JComponent> {
private static Class[] ty = new Class[] { Boolean.class, boolean.class };
private static Class[] ty = new Class[]{Boolean.class, boolean.class};
@Override
public Class[] getCompatibleTypes() {
......@@ -23,18 +33,16 @@ public class BooleanProducer implements OhEditorProducer<Boolean, JComponent> {
@Override
public OhEditorPanel<Boolean> getEditor(OhObject<Boolean> object) {
BooleanPanel ouiPanel = new BooleanPanel(object);
ouiPanel.getPanel().setLayout(new FlowLayout(FlowLayout.LEFT));
JCheckBox checkBox = new JCheckBox(object.getName());
final JCheckBox checkBox = new JCheckBox(object.getName());
checkBox.setSelected((Boolean) object.getValue());
BooleanListener listener = new BooleanListener(ouiPanel);
checkBox.addItemListener(listener);
ouiPanel.setBooleanListener(listener);
if (!"".equals(object.getDescription()))
checkBox.setToolTipText(object.getDescription());
ouiPanel.getPanel().add(checkBox);
return ouiPanel;
OhEditorPanel editor = new OhEditorPanel<Boolean>(object) {
@Override
public Boolean getValue() {
return checkBox.isSelected();
}
};
editor.addComponent(checkBox, object.getDescription());
return editor;
}
}
package fr.cemagref.ohoui.ui.swing.panels.producers;
import fr.cemagref.ohoui.ui.swing.panels.OhEditorPanel;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import javax.swing.JTextField;
import org.openide.util.Exceptions;
public class CharacterListener implements InputMethodListener {
private OhEditorPanel panel;
public CharacterListener(OhEditorPanel panel) {
this.panel = panel;
}
/**
* @see java.awt.event.InputMethodListener#inputMethodTextChanged(java.awt.event.InputMethodEvent)
*/
@Override
public void inputMethodTextChanged(InputMethodEvent ev) {
JTextField textField = (JTextField) ev.getSource();
String ch = textField.getText();
// System.out.println("#### >"+ch+"< ####");
if ("".equals(ch)) {
ch = "A";
} else {
ch = "" + ch.charAt(ch.length() - 1);
}
textField.setText(ch);
try {
panel.setValue(ch.charAt(0));
} catch (IllegalArgumentException ex) {
Exceptions.printStackTrace(ex);
} catch (IllegalAccessException ex) {
Exceptions.printStackTrace(ex);
}
}
/**
* @see java.awt.event.InputMethodListener#caretPositionChanged(java.awt.event.InputMethodEvent)
*/
@Override
public void caretPositionChanged(InputMethodEvent arg0) {
}
}
/*
* 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.panels.producers;
import fr.cemagref.ohoui.structure.OhObject;
......@@ -18,12 +34,16 @@ public class CharacterProducer implements OhEditorProducer<Character, JComponent
@Override
public OhEditorPanel<Character> getEditor(OhObject<Character> object) {
JTextField tf = null;
tf = new JTextField("" + object.getValue(), 2);
OhEditorPanel<Character> panel = OhEditorPanel.makeLabelComponentOUIPanel(object, tf, object.getName(), object.getDescription());
CharacterListener gcl = new CharacterListener(panel);
tf.addInputMethodListener(gcl);
final JTextField tf = new JTextField("" + object.getValue(), 2);
OhEditorPanel<Character> panel = new OhEditorPanel<Character>(object) {
@Override
public Character getValue() {
return tf.getText().charAt(0);
}
};
panel.addLabel(object.getName(), object.getDescription());
panel.addComponent(tf, object.getDescription());
return panel;
}
}
/*
* 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.panels.producers;
import fr.cemagref.ohoui.structure.OhObject;
......@@ -11,7 +27,6 @@ import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import org.openide.util.Exceptions;
public class ColorProducer implements OhEditorProducer<Color, JComponent> {
......@@ -24,39 +39,34 @@ public class ColorProducer implements OhEditorProducer<Color, JComponent> {
@Override
public OhEditorPanel getEditor(OhObject<Color> object) {
JButton editButton = new JButton();
editButton.setBackground((Color) object.getValue());
OhEditorPanel<Color> panel = OhEditorPanel.makeLabelComponentOUIPanel(object, editButton, object.getName(),
object.getDescription());
editButton.addActionListener(new EditColorActionListener(panel, editButton, (Color) object.getValue()));
return panel;
return new ColorPanel(object);
}
private class EditColorActionListener implements ActionListener {
private class ColorPanel extends OhEditorPanel<Color> implements ActionListener {
private OhEditorPanel<Color> ouiPanel;
private JButton editButton;
private Color color;
public EditColorActionListener(OhEditorPanel<Color> panel, JButton editButton, Color color) {
this.ouiPanel = panel;
this.editButton = editButton;
this.color = color;
public ColorPanel(OhObject<Color> object) {
super(object);
editButton = new JButton();
editButton.setBackground((Color) object.getValue());
addLabel(object.getName(), object.getDescription());
addComponent(editButton, object.getDescription());
editButton.addActionListener(this);
}
@Override
public Color getValue() {
return color;
}
@Override
public void actionPerformed(ActionEvent e) {
Color answer = JColorChooser.showDialog(ouiPanel.getPanel(), "Choose your color", color);
Color answer = JColorChooser.showDialog(this.getPanel(), "Choose your color", color);
if (answer != null) {