Index: lutincommandline/src/java/org/codelutin/option/ui/ConfigUI.java diff -u lutincommandline/src/java/org/codelutin/option/ui/ConfigUI.java:1.1.1.1 lutincommandline/src/java/org/codelutin/option/ui/ConfigUI.java:1.2 --- lutincommandline/src/java/org/codelutin/option/ui/ConfigUI.java:1.1.1.1 Sat Feb 9 15:05:37 2008 +++ lutincommandline/src/java/org/codelutin/option/ui/ConfigUI.java Fri Feb 22 20:57:25 2008 @@ -1,6 +1,5 @@ -/* -* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin, -* Tony Chemit +/* +* ##% Copyright (C) 2007, 2008 Code Lutin, Tony Chemit * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -20,37 +19,87 @@ import org.codelutin.option.Config; +import javax.swing.DefaultListCellRenderer; +import javax.swing.JComponent; +import javax.swing.JList; +import javax.swing.ListCellRenderer; +import java.awt.Component; +import java.util.ArrayList; +import java.util.List; + /** - * L'ui de modification de configuration. + * TODO javadoc * * @author chemit */ public class ConfigUI extends JConfigUI { + + protected List configs; - private static ConfigUI instance; - - public static ConfigUI getInstance() { - if (instance == null) { - instance = new ConfigUI(); + public List getConfigs() { + if (configs == null) { + configs = new ArrayList(); } - return instance; + return configs; } - public static void reloadUI() { - instance = null; + public ListCellRenderer getListener() { + return new DefaultListCellRenderer() { + @Override + public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { + if (value instanceof Config) { + Config config = (Config) value; + JComponent listCellRendererComponent = (JComponent) super.getListCellRendererComponent(list, config.getCategory(), index, isSelected, cellHasFocus); + listCellRendererComponent.setToolTipText(config.getCategory()); + return listCellRendererComponent; + } + return this; + } + }; } - public static void showUI(Config config) { - showUI(config, null); + public void onValueChanged() { + Object value = list.getSelectedValue(); + if (value instanceof Config) { + Config config = (Config) value; + cardLayout.show(preview, config.getCategory()); + } } - public static void showTechnicalUI(Config config) { - showUI(config, ConfigTableModel.TypeModel.tech); + public ConfigUI init(Config... configs) { + for (Config config : configs) { + add(config); + } + if (list.getModel().getSize() > 0) { + list.setSelectedIndex(0); + } + return this; } - public static void showUI(Config config, ConfigTableModel.TypeModel type) { - getInstance().getHandler().doLoad(config, type); - getInstance().setVisible(true); + public boolean add(Config config) { + int index = getConfigs().indexOf(config); + if (index > -1) { + return false; + } + configs.add(config); + ConfigTab ui = new ConfigTab(); + ui.getHandler().init(config); + listModel.addElement(config); + preview.add(ui, config.getCategory()); + return true; } + public boolean remove(Config o) { + if (configs == null) { + return false; + } + int index = configs.indexOf(o); + if (index == -1) { + return false; + } + listModel.remove(index); + preview.remove(index); + configs.remove(index); + return true; + } } Index: lutincommandline/src/java/org/codelutin/option/ui/ConfigUIHandler.java diff -u lutincommandline/src/java/org/codelutin/option/ui/ConfigUIHandler.java:1.3 lutincommandline/src/java/org/codelutin/option/ui/ConfigUIHandler.java:1.4 --- lutincommandline/src/java/org/codelutin/option/ui/ConfigUIHandler.java:1.3 Thu Feb 21 17:16:57 2008 +++ lutincommandline/src/java/org/codelutin/option/ui/ConfigUIHandler.java Fri Feb 22 20:57:25 2008 @@ -20,13 +20,9 @@ import org.codelutin.option.Config; -import javax.swing.SwingUtilities; import javax.swing.JTable; -import javax.swing.table.TableColumn; -import javax.swing.table.TableCellRenderer; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; -import java.awt.Component; /** * Le controleur de l'ui de modification de configuration. @@ -35,80 +31,53 @@ */ public class ConfigUIHandler { - protected JConfigUI ui; + protected ConfigTab ui; protected Config config; protected ConfigTableModel tableModel; protected TableModelListener tableModelListener; - public ConfigUIHandler(JConfigUI ui) { + public ConfigUIHandler(ConfigTab ui) { this.ui = ui; } - public boolean isOkEnabled() { - return !(isEmpty() || !tableModel.isValid()); - } - - public boolean isResetEnabled() { - return !(isEmpty() || !tableModel.isModified()); - } - - public void doLoad(Config config, ConfigTableModel.TypeModel type) { + public void init(Config config) { this.config = config; - tableModel = new ConfigTableModel(config, type); + tableModel = new ConfigTableModel(config); tableModel.addTableModelListener(getTableModelListener()); JTable table = ui.getMain(); table.setModel(tableModel); - initColumnSizes(tableModel,table); + ConfigTableRenderer renderer = new ConfigTableRenderer(tableModel); table.getColumnModel().getColumn(0).setCellRenderer(renderer); table.getColumnModel().getColumn(1).setCellRenderer(renderer); table.getColumnModel().getColumn(1).setCellEditor(new ConfigTableEditor(getTableModel())); - boolean action = tableModel.getType() != ConfigTableModel.TypeModel.tech; - ui.getButons().setVisible(action); - ui.getButons2().setVisible(!action); - tableModel.fireTableDataChanged(); - ui.pack(); - } - - protected void initColumnSizes(ConfigTableModel model,JTable table) { - TableColumn column; - Component comp; - int headerWidth; - int cellWidth; - - TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer(); - - for (int i = 0; i < model.getColumnCount(); i++) { - column = table.getColumnModel().getColumn(i); - comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0); - headerWidth = comp.getPreferredSize().width; - - comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table, model.getColumnName(i),false, false, 0, i); - cellWidth = comp.getPreferredSize().width; - - { - System.out.println("Initializing width of column "+ i + ". "+ "headerWidth = " + headerWidth+ "; cellWidth = " + cellWidth); - } - column.setPreferredWidth(Math.max(headerWidth, cellWidth)); - } + tableModel.fireTableDataChanged(); } - public void doOk() { + public void doSave() { if (!isEmpty() && getTableModel().isModified()) { getTableModel().transfertModified(getConfig()); getConfig().saveSafely(); } - ui.dispose(); + doReset(); } public void doReset() { getTableModel().reset(); } - public void doCancel() { - ui.dispose(); + public void doSelectTechnical(boolean showTechnical) { + tableModel.setType(showTechnical ? ConfigTableModel.TypeModel.all : ConfigTableModel.TypeModel.nontech); + } + + protected boolean isSaveEnabled() { + return isResetEnabled() && tableModel.isValid(); + } + + protected boolean isResetEnabled() { + return !isEmpty() && tableModel.isModified(); } protected boolean isEmpty() { @@ -127,12 +96,8 @@ if (tableModelListener == null) { tableModelListener = new TableModelListener() { public void tableChanged(TableModelEvent e) { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - ui.processDataBinding("ok.enabled"); - ui.processDataBinding("reset.enabled"); - } - }); + ui.getSave().setEnabled(isSaveEnabled()); + ui.getReset().setEnabled(isResetEnabled()); } }; } @@ -140,11 +105,7 @@ } protected void dispose() { - config = null; - if (tableModel != null) { - tableModel.removeTableModelListener(getTableModelListener()); - tableModel = null; - } + getTableModel().reset(); } } \ No newline at end of file Index: lutincommandline/src/java/org/codelutin/option/ui/ConfigTableModel.java diff -u lutincommandline/src/java/org/codelutin/option/ui/ConfigTableModel.java:1.2 lutincommandline/src/java/org/codelutin/option/ui/ConfigTableModel.java:1.3 --- lutincommandline/src/java/org/codelutin/option/ui/ConfigTableModel.java:1.2 Thu Feb 21 17:16:57 2008 +++ lutincommandline/src/java/org/codelutin/option/ui/ConfigTableModel.java Fri Feb 22 20:57:25 2008 @@ -70,12 +70,29 @@ protected Map, Object> props; protected TypeModel type; + protected Config config; + public ConfigTableModel(Config config) { this(config, null); } public ConfigTableModel(Config config, TypeModel type) { - props = new LinkedHashMap, Object>(); + this.config=config; + setType(type); + } + + public TypeModel getType() { + return type; + } + + public void setType(TypeModel type) { + this.type = type; + // rebuild model + if (props == null) { + props = new LinkedHashMap, Object>(); + } else { + props.clear(); + } if (type == null) { type = DEFAULT_TYPE; } @@ -97,12 +114,8 @@ } } } - this.type = type; keys = ArrayUtil.toArray(props.keySet(), ConfigPropertyKey.class); - } - - public TypeModel getType() { - return type; + fireTableDataChanged(); } public ConfigPropertyKey getKey(int rowIndex) {