Author: chatellier Date: 2009-03-19 09:54:08 +0000 (Thu, 19 Mar 2009) New Revision: 2004 Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/widget/editor/ParameterColumnEditor.java Log: Update doc, code style. Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/widget/editor/ParameterColumnEditor.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/widget/editor/ParameterColumnEditor.java 2009-03-19 09:15:26 UTC (rev 2003) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/widget/editor/ParameterColumnEditor.java 2009-03-19 09:54:08 UTC (rev 2004) @@ -1,5 +1,5 @@ /* *##% - * Copyright (C) 2006 + * Copyright (C) 2006 - 2009 * Ifremer, Code Lutin, Cédric Pineau, Benjamin Poussin * * This program is free software; you can redistribute it and/or @@ -17,18 +17,6 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *##%*/ -/* * - * ParameterColumnEditor.java - * - * Created: 25 sept. 06 12:35:21 - * - * @author poussin - * @version $Revision$ - * - * Last update: $Date$ - * by : $Author$ - */ - package fr.ifremer.isisfish.ui.widget.editor; import java.awt.Component; @@ -39,7 +27,6 @@ import java.util.EventObject; import java.util.HashSet; import java.util.List; -import java.util.Map; import javax.swing.JComboBox; import javax.swing.JComponent; @@ -66,156 +53,166 @@ import java.awt.event.FocusListener; /** + * ParameterColumnEditor. + * + * Created: 25 sept. 06 12:35:21 + * * @author poussin * @author chemit + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ */ - public class ParameterColumnEditor extends JComponent implements - TableCellEditor { + TableCellEditor { - private enum TypeClassMapping { - /** - * for simple int - */ - Int(int.class), - /** - * for a topia entity - */ - Topia(TopiaEntity.class), - /** - * for a mounth - */ - Mounth(Month.class), - /** - * for a simple date - */ - Date(Date.class), - /** - * for a simple boolean - */ - Double(double.class), - /** - * for a simple boolean - */ - Boolean(boolean.class), - /** - * for a file location - */ - File(java.io.File.class), - /** - * for a simple String - * - */ - String(java.lang.String.class); - - final Class<?> klazz; + /** Class logger. */ + private static Log log = LogFactory.getLog(ParameterColumnEditor.class); - TypeClassMapping(Class<?> klazz) { - this.klazz = klazz; - } + private enum TypeClassMapping { + /** + * for simple int + */ + Int(int.class), + /** + * for a topia entity + */ + Topia(TopiaEntity.class), + /** + * for a mounth + */ + Mounth(Month.class), + /** + * for a simple date + */ + Date(Date.class), + /** + * for a simple boolean + */ + Double(double.class), + /** + * for a simple boolean + */ + Boolean(boolean.class), + /** + * for a file location + */ + File(java.io.File.class), + /** + * for a simple String + * + */ + String(java.lang.String.class); - protected static TypeClassMapping getMapping(Class<?> klazz) { - for (TypeClassMapping t : TypeClassMapping.values()) - if (t.klazz.isAssignableFrom(klazz)) - return t; - throw new RuntimeException( - "could not found a TypeClassMapping for this class " - + klazz); - } - } + final Class<?> klazz; - private static final long serialVersionUID = 6860330126841984303L; + TypeClassMapping(Class<?> klazz) { + this.klazz = klazz; + } - - protected HashSet<CellEditorListener> listeners = new HashSet<CellEditorListener>(); + protected static TypeClassMapping getMapping(Class<?> klazz) { + for (TypeClassMapping t : TypeClassMapping.values()) + if (t.klazz.isAssignableFrom(klazz)) + return t; + throw new RuntimeException( + "could not found a TypeClassMapping for this class " + + klazz); + } + } - protected RegionStorage region = null; + private static final long serialVersionUID = 6860330126841984303L; - protected Component editor = null; + protected HashSet<CellEditorListener> listeners = new HashSet<CellEditorListener>(); - protected Class type = null; + protected RegionStorage region = null; - /** - * @return Returns the topiaContext. - */ - public RegionStorage getRegion() { - return this.region; - } + protected Component editor = null; - /** - * @param region - * The topiaContext to set. - */ - public void setRegion(RegionStorage region) { - this.region = region; - } + protected Class type = null; - @SuppressWarnings( { "unchecked" }) - public Component getTableCellEditorComponent(JTable table, Object value, - boolean isSelected, int row, int column) { -// Map.Entry<String, Class> p = (Map.Entry<String, Class>) table.getValueAt(row, 0); -// type = p.getValue(); -// log.debug(p.getKey() + " P.type:" + type); - GenericCell p = (GenericCell) table.getValueAt(row, 0); - type = (Class)p.getValue(); - JComboBox c; - //if (!table.hasFocus()) return null; + /** + * @return Returns the topiaContext. + */ + public RegionStorage getRegion() { + return this.region; + } + /** + * @param region + * The topiaContext to set. + */ + public void setRegion(RegionStorage region) { + this.region = region; + } + @SuppressWarnings( { "unchecked" }) + public Component getTableCellEditorComponent(JTable table, Object value, + boolean isSelected, int row, int column) { + // Map.Entry<String, Class> p = (Map.Entry<String, Class>) table.getValueAt(row, 0); + // type = p.getValue(); + // log.debug(p.getKey() + " P.type:" + type); + GenericCell p = (GenericCell) table.getValueAt(row, 0); + type = (Class) p.getValue(); + JComboBox c; + //if (!table.hasFocus()) return null; - TypeClassMapping mapping = TypeClassMapping.getMapping(type); - switch (mapping) { - case Topia: - // on a un bean comme parametre - try { - if (region==null) { - return null; - } - TopiaContext context = region.getStorage().beginTransaction(); - if (context != null) { - List list = context.find("from " + type.getName()); - c = new JComboBox(list.toArray()); - c.setSelectedItem(value); - editor = c; - context.closeContext(); - } - - } catch (Exception eee) { - log.warn("Can't get entity object for combobox", eee); - } - break; - case Boolean: - List<Boolean> listB = new ArrayList<Boolean>(); - listB.add(Boolean.TRUE); - listB.add(Boolean.FALSE); - c = new JComboBox(listB.toArray()); - listB.clear(); - // TODO See how to convert - c.setSelectedItem(value); - editor = c; - break; - case Date: - Date date = (Date) value; - if (date == null) date = new Date(0); - int mou = date.getMonth().getMonthNumber(); - int yea = date.getYear(); - editor = new DateComponent(mou,yea,region==null?null:region.getStorage()); - //editor = new JTextField(String.valueOf(date.getDate())); - break; - case Mounth: - editor = DateComponent.createMounthCombo(((Month)value).getMonthNumber(), region==null?null:region.getStorage()); - break; - case File: - // break; - default: - editor = new JTextField(value.toString()); - } - if (editor != null){ - if(JComboBox.class.isInstance(editor)){ - ((JComboBox)editor).addActionListener(getComboListener()); + TypeClassMapping mapping = TypeClassMapping.getMapping(type); + switch (mapping) { + case Topia: + // on a un bean comme parametre + try { + if (region == null) { + return null; + } + TopiaContext context = region.getStorage().beginTransaction(); + if (context != null) { + List list = context.find("from " + type.getName()); + c = new JComboBox(list.toArray()); + c.setSelectedItem(value); + editor = c; + context.closeContext(); + } + + } catch (Exception eee) { + log.warn("Can't get entity object for combobox", eee); } - else if (JTextField.class.isInstance(editor)){ - ((JTextField)editor).addFocusListener(new FocusListener() { + break; + case Boolean: + List<Boolean> listB = new ArrayList<Boolean>(); + listB.add(Boolean.TRUE); + listB.add(Boolean.FALSE); + c = new JComboBox(listB.toArray()); + listB.clear(); + // TODO See how to convert + c.setSelectedItem(value); + editor = c; + break; + case Date: + Date date = (Date) value; + if (date == null) + date = new Date(0); + int mou = date.getMonth().getMonthNumber(); + int yea = date.getYear(); + editor = new DateComponent(mou, yea, region == null ? null : region + .getStorage()); + //editor = new JTextField(String.valueOf(date.getDate())); + break; + case Mounth: + editor = DateComponent.createMounthCombo(((Month) value) + .getMonthNumber(), region == null ? null : region + .getStorage()); + break; + case File: + // break; + default: + editor = new JTextField(value.toString()); + } + if (editor != null) { + if (JComboBox.class.isInstance(editor)) { + ((JComboBox) editor).addActionListener(getComboListener()); + } else if (JTextField.class.isInstance(editor)) { + ((JTextField) editor).addFocusListener(new FocusListener() { @Override public void focusGained(FocusEvent e) { @@ -227,273 +224,263 @@ stopCellEditing(); } }); - } - else if (DateComponent.class.isInstance(editor)){ - DateComponent date = ((DateComponent)editor); - if (date.getMounthCombo() != null){ + } else if (DateComponent.class.isInstance(editor)) { + DateComponent date = ((DateComponent) editor); + if (date.getMounthCombo() != null) { date.getMounthCombo().addActionListener(getComboListener()); } - if (date.getYearCombo() != null){ + if (date.getYearCombo() != null) { date.getYearCombo().addActionListener(getComboListener()); } } } - log.info("getCellEditorValue [" + type + "] [mapping:" + mapping + "="+editor); - //+ editor==null?null:editor.getClass().getSimpleName()); - return editor; - } - protected ActionListener getComboListener(){ + log.info("getCellEditorValue [" + type + "] [mapping:" + mapping + "=" + + editor); + //+ editor==null?null:editor.getClass().getSimpleName()); + return editor; + } + + protected ActionListener getComboListener() { return new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - stopCellEditing(); - } - }; + @Override + public void actionPerformed(ActionEvent e) { + stopCellEditing(); + } + }; } - public Object getCellEditorValue() { + public Object getCellEditorValue() { - Object result = null; - TopiaContext context = null; - if (editor==null) return ""; - TypeClassMapping mapping = TypeClassMapping.getMapping(type); - switch (mapping) { - case Topia: - result = getComboBoxValue(editor); - break; - case Boolean: - result = getComboBoxValue(editor); - // TODO test - break; - case Mounth: - result = getComboBoxIndex(editor) + ""; - break; - case Date: - result = ((DateComponent)editor).getSelectedValue()+""; - break; - case File: - //TODO - // result = getTextFieldValue(editor);break; - default: - if (editor instanceof JTextField) { - result = ((JTextField) editor).getText(); - } else if (editor instanceof JComboBox) { - result = ((JComboBox) editor).getSelectedItem(); - } - } + Object result = null; + TopiaContext context = null; + if (editor == null) + return ""; + TypeClassMapping mapping = TypeClassMapping.getMapping(type); + switch (mapping) { + case Topia: + result = getComboBoxValue(editor); + break; + case Boolean: + result = getComboBoxValue(editor); + // TODO test + break; + case Mounth: + result = getComboBoxIndex(editor) + ""; + break; + case Date: + result = ((DateComponent) editor).getSelectedValue() + ""; + break; + case File: + //TODO + // result = getTextFieldValue(editor);break; + default: + if (editor instanceof JTextField) { + result = ((JTextField) editor).getText(); + } else if (editor instanceof JComboBox) { + result = ((JComboBox) editor).getSelectedItem(); + } + } - if (result instanceof String && !String.class.equals(type)) { - if (region != null) - context = region.getStorage(); - ConvertUtilsBean cub = ConverterUtil.getConverter(context); - result = cub.convert((String) result, type); - } + if (result instanceof String && !String.class.equals(type)) { + if (region != null) + context = region.getStorage(); + ConvertUtilsBean cub = ConverterUtil.getConverter(context); + result = cub.convert((String) result, type); + } - log.info("getCellEditorValue [" + type + "] [mapping:" + mapping + "]= " + result); - - return result; - } + log.info("getCellEditorValue [" + type + "] [mapping:" + mapping + + "]= " + result); - protected Object getTextFieldValue(Component editor) { - return ((JTextField) editor).getText(); - } + return result; + } - protected Object getComboBoxValue(Component editor) { - return ((JComboBox) editor).getSelectedItem(); - } + protected Object getTextFieldValue(Component editor) { + return ((JTextField) editor).getText(); + } - protected int getComboBoxIndex(Component editor) { - return (((JComboBox) editor)).getSelectedIndex(); - } + protected Object getComboBoxValue(Component editor) { + return ((JComboBox) editor).getSelectedItem(); + } - public Object getCellEditorValueOld() { + protected int getComboBoxIndex(Component editor) { + return (((JComboBox) editor)).getSelectedIndex(); + } - Object result = null; - if (editor instanceof JTextField) { - result = ((JTextField) editor).getText(); - } else if (editor instanceof JComboBox) { - result = ((JComboBox) editor).getSelectedItem(); - } + public Object getCellEditorValueOld() { - if (result instanceof String && !String.class.equals(type)) { - TopiaContext context = null; - if (region != null) { - context = region.getStorage(); - } - ConvertUtilsBean cub = ConverterUtil.getConverter(context); - result = cub.convert((String) result, type); - } + Object result = null; + if (editor instanceof JTextField) { + result = ((JTextField) editor).getText(); + } else if (editor instanceof JComboBox) { + result = ((JComboBox) editor).getSelectedItem(); + } - return result; - } + if (result instanceof String && !String.class.equals(type)) { + TopiaContext context = null; + if (region != null) { + context = region.getStorage(); + } + ConvertUtilsBean cub = ConverterUtil.getConverter(context); + result = cub.convert((String) result, type); + } - /* - * (non-Javadoc) - * - * @see javax.swing.CellEditor#addCellEditorListener(javax.swing.event.CellEditorListener) - */ - public void addCellEditorListener(CellEditorListener l) { - listeners.add(l); - } + return result; + } - /* - * (non-Javadoc) - * - * @see javax.swing.CellEditor#cancelCellEditing() - */ - public void cancelCellEditing() { + /* + * (non-Javadoc) + * + * @see javax.swing.CellEditor#addCellEditorListener(javax.swing.event.CellEditorListener) + */ + public void addCellEditorListener(CellEditorListener l) { + listeners.add(l); + } + + /* + * @see javax.swing.CellEditor#cancelCellEditing() + */ + public void cancelCellEditing() { log.debug("cancelCellEditing"); - ChangeEvent e = new ChangeEvent(this); - for (CellEditorListener listener : listeners) { - listener.editingCanceled(e); - } - } + ChangeEvent e = new ChangeEvent(this); + for (CellEditorListener listener : listeners) { + listener.editingCanceled(e); + } + } - /* - * (non-Javadoc) - * - * @see javax.swing.CellEditor#isCellEditable(java.util.EventObject) - */ - public boolean isCellEditable(EventObject anEvent) { - return true; - } + /* + * @see javax.swing.CellEditor#isCellEditable(java.util.EventObject) + */ + public boolean isCellEditable(EventObject anEvent) { + return true; + } - /* - * (non-Javadoc) - * - * @see javax.swing.CellEditor#removeCellEditorListener(javax.swing.event.CellEditorListener) - */ - public void removeCellEditorListener(CellEditorListener l) { - listeners.remove(l); - } + /* + * @see javax.swing.CellEditor#removeCellEditorListener(javax.swing.event.CellEditorListener) + */ + public void removeCellEditorListener(CellEditorListener l) { + listeners.remove(l); + } - /* - * (non-Javadoc) - * - * @see javax.swing.CellEditor#shouldSelectCell(java.util.EventObject) - */ - public boolean shouldSelectCell(EventObject anEvent) { - return true; - } + /* + * @see javax.swing.CellEditor#shouldSelectCell(java.util.EventObject) + */ + public boolean shouldSelectCell(EventObject anEvent) { + return true; + } - /* - * (non-Javadoc) - * - * @see javax.swing.CellEditor#stopCellEditing() - */ - public boolean stopCellEditing() { - // to prevent concurent modification exception + /* + * @see javax.swing.CellEditor#stopCellEditing() + */ + public boolean stopCellEditing() { + // to prevent concurent modification exception log.debug("stopCellEditing"); - CellEditorListener[] list = listeners - .toArray(new CellEditorListener[listeners.size()]); + CellEditorListener[] list = listeners + .toArray(new CellEditorListener[listeners.size()]); - ChangeEvent e = new ChangeEvent(this); - for (CellEditorListener l : list) { - l.editingStopped(e); - } - return true; - } + ChangeEvent e = new ChangeEvent(this); + for (CellEditorListener l : list) { + l.editingStopped(e); + } + return true; + } - public static class DateComponent extends JPanel { + public static class DateComponent extends JPanel { - private static final long serialVersionUID = -6694461572642939712L; + private static final long serialVersionUID = -6694461572642939712L; - protected JComboBox mounth; + protected JComboBox mounth; - protected JComboBox year; + protected JComboBox year; - public DateComponent(int mounth, int year, TopiaContext context) { - super(new GridLayout(0, 2)); + public DateComponent(int mounth, int year, TopiaContext context) { + super(new GridLayout(0, 2)); - this.mounth = createMounthCombo(mounth, context); - this.year = createYearCombo(200, year); + this.mounth = createMounthCombo(mounth, context); + this.year = createYearCombo(200, year); - this.add(this.mounth); - this.add(this.year); - } + this.add(this.mounth); + this.add(this.year); + } - public JComboBox getYearCombo(){ + public JComboBox getYearCombo() { return year; } - public JComboBox getMounthCombo(){ + + public JComboBox getMounthCombo() { return mounth; } - public int getSelectedMounth() { - return mounth.getSelectedIndex(); - } + public int getSelectedMounth() { + return mounth.getSelectedIndex(); + } - public int getSelectedYear() { - return year.getSelectedIndex(); - } + public int getSelectedYear() { + return year.getSelectedIndex(); + } - public void setSelectedValue(int year,int mounth) { - this.mounth.setSelectedIndex(mounth); - this.year.setSelectedIndex(year); - } - - public int getSelectedValue() { - int selectedYear = getSelectedYear(); - int selectedMounth = getSelectedMounth(); - log.info("selected mounth "+selectedMounth); - log.info("selected year "+selectedYear); - return selectedMounth+selectedYear*12; - } + public void setSelectedValue(int year, int mounth) { + this.mounth.setSelectedIndex(mounth); + this.year.setSelectedIndex(year); + } - public static JComboBox createMounthCombo(int mounth, TopiaContext context) { - JComboBox combo = EnumEditor.newEditor(MonthEnum.class); - combo.setSelectedIndex(mounth); - return combo; - } - /*public static JComboBox createMounthCombo(int mounth, TopiaContext context) { - JComboBox combo = createMounthCombo0(context); - combo.setSelectedIndex(mounth); - return combo; - }*/ - /*public static JComboBox createMounthCombo(Object mounth, TopiaContext context) { - JComboBox combo = createMounthCombo0(context); - combo.setSelectedItem(mounth); - return combo; - }*/ + public int getSelectedValue() { + int selectedYear = getSelectedYear(); + int selectedMounth = getSelectedMounth(); + log.info("selected mounth " + selectedMounth); + log.info("selected year " + selectedYear); + return selectedMounth + selectedYear * 12; + } - /*private static JComboBox createMounthCombo0(TopiaContext context) { + public static JComboBox createMounthCombo(int mounth, + TopiaContext context) { + JComboBox combo = EnumEditor.newEditor(MonthEnum.class); + combo.setSelectedIndex(mounth); + return combo; + } - List<String> listM = new ArrayList<String>(); - // TODO Should use database ? data instead of thoses awfull hardcore - // code... - listM.add("janvier"); - listM.add("fevrier"); - listM.add("mars"); - listM.add("avril"); - listM.add("mai"); - listM.add("juin"); - listM.add("juillet"); - listM.add("aout"); - listM.add("septembre"); - listM.add("octobre"); - listM.add("novembre"); - listM.add("decembre"); - JComboBox combo = new JComboBox(listM.toArray()); - listM.clear(); - return combo; - }*/ + /*public static JComboBox createMounthCombo(int mounth, TopiaContext context) { + JComboBox combo = createMounthCombo0(context); + combo.setSelectedIndex(mounth); + return combo; + }*/ + /*public static JComboBox createMounthCombo(Object mounth, TopiaContext context) { + JComboBox combo = createMounthCombo0(context); + combo.setSelectedItem(mounth); + return combo; + }*/ - public static JComboBox createYearCombo(int nb, int yearS) { + /*private static JComboBox createMounthCombo0(TopiaContext context) { - List<Integer> listI = new ArrayList<Integer>(); - for (int i = 0; i < nb; i++) - listI.add(i); - JComboBox year = new JComboBox(listI.toArray()); - listI.clear(); - year.setSelectedIndex(yearS); - return year; - } - } - - /** - * to use log facility, just put in your code: log.info(\"...\"); - */ - static private Log log = LogFactory.getLog(ParameterColumnEditor.class); + List<String> listM = new ArrayList<String>(); + // TODO Should use database ? data instead of thoses awfull hardcore + // code... + listM.add("janvier"); + listM.add("fevrier"); + listM.add("mars"); + listM.add("avril"); + listM.add("mai"); + listM.add("juin"); + listM.add("juillet"); + listM.add("aout"); + listM.add("septembre"); + listM.add("octobre"); + listM.add("novembre"); + listM.add("decembre"); + JComboBox combo = new JComboBox(listM.toArray()); + listM.clear(); + return combo; + }*/ + public static JComboBox createYearCombo(int nb, int yearS) { + + List<Integer> listI = new ArrayList<Integer>(); + for (int i = 0; i < nb; i++) + listI.add(i); + JComboBox year = new JComboBox(listI.toArray()); + listI.clear(); + year.setSelectedIndex(yearS); + return year; + } + } }