Author: tchemit Date: 2008-02-08 18:39:46 +0000 (Fri, 08 Feb 2008) New Revision: 765 Added: trunk/simexplorer-is-swing/src/test/fr/cemagref/simexplorer/is/ui/swing/ trunk/simexplorer-is-swing/src/test/fr/cemagref/simexplorer/is/ui/swing/SimExplorerUIRefreshHelperTest.java Modified: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/SimExplorerUIRefreshHelper.java Log: optimisation ui (essaye de remplir au plus)+ test Modified: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/SimExplorerUIRefreshHelper.java =================================================================== --- trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/SimExplorerUIRefreshHelper.java 2008-02-08 18:07:34 UTC (rev 764) +++ trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/SimExplorerUIRefreshHelper.java 2008-02-08 18:39:46 UTC (rev 765) @@ -19,8 +19,8 @@ package fr.cemagref.simexplorer.is.ui.swing; import fr.cemagref.simexplorer.is.attachment.Attachment; +import fr.cemagref.simexplorer.is.entities.data.ExplorationApplication; import fr.cemagref.simexplorer.is.entities.data.LoggableElement; -import fr.cemagref.simexplorer.is.entities.data.ExplorationApplication; import fr.cemagref.simexplorer.is.entities.metadata.MetaData; import fr.cemagref.simexplorer.is.entities.metadata.Version; import fr.cemagref.simexplorer.is.ui.SimExplorer; @@ -40,12 +40,12 @@ import javax.swing.AbstractButton; import javax.swing.DefaultComboBoxModel; import javax.swing.DefaultListModel; +import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JList; import javax.swing.JTabbedPane; import javax.swing.JTable; import javax.swing.SwingUtilities; -import javax.swing.JButton; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeNode; import java.awt.Dimension; @@ -188,17 +188,17 @@ /** * Rafraichit les actions d'un node dans l'arbre de navigation * - * @param ui l'onglet de détail + * @param ui l'onglet de détail * @param model le model de d'onglet detail - * @param node le node sélectionné dans l'arbre de navigation + * @param node le node sélectionné dans l'arbre de navigation */ public static void refreshDetailActions(JApplicationDetailTab ui, DetailTabModel model, LoggableElementTreeNode node) { // refresh download LoggableElement button - refreshDownloadLoggableElementAction(ui, model,node); + refreshDownloadLoggableElementAction(ui, model, node); // refresh download Attachment button refreshDownloadAttachmentAction(ui, model); // refresh delete button - refreshDeleteAction(ui,model); + refreshDeleteAction(ui, model); // refresh export button refreshExportAction(ui, model); // refresh tree buttons @@ -208,40 +208,40 @@ protected static void refreshTreeActions(JApplicationDetailTab ui, LoggableElementTreeNode node) { String tooltip; - boolean enabled = node!=null && !node.isLeaf() && node.getChildCount()>0; - tooltip = !enabled?null: _("simexplorer.action.application.collapseAll2", node.getUserObject()); + boolean enabled = node != null && !node.isLeaf() && node.getChildCount() > 0; + tooltip = !enabled ? null : _("simexplorer.action.application.collapseAll2", node.getUserObject()); updateButton(ui.getCollapseAllApplication(), enabled, tooltip); - tooltip = !enabled?null: _("simexplorer.action.application.expandAll2.tooltip", node.getUserObject()); + tooltip = !enabled ? null : _("simexplorer.action.application.expandAll2.tooltip", node.getUserObject()); updateButton(ui.getExpandAllApplication(), enabled, tooltip); } - protected static void refreshDownloadLoggableElementAction(JApplicationDetailTab ui, DetailTabModel model, LoggableElementTreeNode node) { + protected static void refreshDownloadLoggableElementAction(JApplicationDetailTab ui, DetailTabModel model, LoggableElementTreeNode node) { MetaData detail = model.getDetail(); - boolean enabled = detail !=null || LoggableElementTreeHelper.canDownload(node); - String tooltip = !enabled?null:_("simexplorer.action.dowloadLoggableElement2", detail==null?node.getUserObject():detail.getName()+" - "+model.getSelectedVersion()); + boolean enabled = detail != null || LoggableElementTreeHelper.canDownload(node); + String tooltip = !enabled ? null : _("simexplorer.action.dowloadLoggableElement2", detail == null ? node.getUserObject() : detail.getName() + " - " + model.getSelectedVersion()); updateButton(ui.getDownloadLoggableElement(), enabled, tooltip); } public static void refreshDownloadAttachmentAction(JApplicationDetailTab ui, DetailTabModel model) { Attachment attachment = model.getSelectedAttachment(); MetaData detail = model.getDetail(); - boolean enabled = detail!=null && attachment !=null; - String tooltip = !enabled?null:_("simexplorer.action.dowloadAttachment2", attachment,detail.getName()+" - "+model.getSelectedVersion()); + boolean enabled = detail != null && attachment != null; + String tooltip = !enabled ? null : _("simexplorer.action.dowloadAttachment2", attachment, detail.getName() + " - " + model.getSelectedVersion()); updateButton(ui.getDownloadAttachment(), enabled, tooltip); } protected static void refreshDeleteAction(JApplicationDetailTab ui, DetailTabModel model) { MetaData detail = model.getDetail(); - boolean enabled = detail!=null; - String tooltip = !enabled?null:_("simexplorer.action.delete2", detail.getName()+" - "+model.getSelectedVersion()); + boolean enabled = detail != null; + String tooltip = !enabled ? null : _("simexplorer.action.delete2", detail.getName() + " - " + model.getSelectedVersion()); updateButton(ui.getDelete(), enabled, tooltip); } protected static void refreshExportAction(JApplicationDetailTab ui, DetailTabModel model) { MetaData detail = model.getDetail(); LoggableElement sNode = model.getSelectedNode(); - boolean enabled = sNode!=null && sNode instanceof ExplorationApplication; - String tooltip = !enabled?null:_("simexplorer.action.export2", detail.getName()+" - "+model.getSelectedVersion()); + boolean enabled = sNode != null && sNode instanceof ExplorationApplication; + String tooltip = !enabled ? null : _("simexplorer.action.export2", detail.getName() + " - " + model.getSelectedVersion()); updateButton(ui.getExportApplication(), enabled, tooltip); } @@ -333,8 +333,8 @@ ui.getDetailType().setText(detail.getType()); String s = detail.getDescription(); String tooltip = s; - if (s.length()>32) { - s = s.substring(0,32)+"..."; + if (s.length() > 32) { + s = s.substring(0, 32) + "..."; } ui.getDetailDescription().setText(s); if (!s.equals(tooltip)) { @@ -344,33 +344,37 @@ ui.getDetail().setVisible(true); JTable table = ui.getDetailTable(); ((JDetailTableModel) table.getModel()).setData(detail); - int nbRows = table.getRowCount() + 1; - if (nbRows > 6) { - nbRows = 6; - } - ui.getScrollTable().setPreferredSize(new Dimension((int) table.getSize().getWidth(), 2 + (nbRows * (4 + table.getFont().getSize())))); + int nbDescriptorRows = table.getRowCount(); + List<Attachment> attachments = detail.getAttachments(); JList listAttachments = ui.getDetailAttachments(); DefaultListModel listModel = (DefaultListModel) listAttachments.getModel(); listModel.setSize(0); listModel.trimToSize(); + int nbAttachmentRows; if (attachments.isEmpty()) { - ui.getDetailAttachmentsHeader().setText(_("simexplorer.node.noattachments")); - ui.getScrollAttachments().setVisible(false); + ui.getDetailAttachmentsHeader().setText(_("simexplorer.node.noattachments")); + ui.getScrollAttachments().setVisible(false); + nbAttachmentRows = 0; } else { ui.getDetailAttachmentsHeader().setText(_("simexplorer.node.attachments", attachments.size())); for (Attachment attachment : attachments) { listModel.addElement(attachment); } listAttachments.setSelectedIndex(0); - nbRows = listModel.getSize(); - if (nbRows > 5) { - nbRows = 5; - } - //Dimension dimHead = ui.getDetailHeaderAttachmentsPanel().getPreferredSize(); + nbAttachmentRows = listModel.getSize(); ui.getScrollAttachments().setVisible(true); + } + + int[] rows = computeRows(nbDescriptorRows, nbAttachmentRows); + + Dimension dimOneRow = table.getUI().getPreferredSize(table.getTableHeader()); + + ui.getScrollTable().setPreferredSize(new Dimension((int) dimOneRow.getWidth(), 2 + ((rows[0] + 1) * (4 + table.getFont().getSize())))); + //ui.getScrollTable().setPreferredSize(new Dimension((int) table.getSize().getWidth(), 2 + (nbDescriptorRows * (4 + table.getFont().getSize())))); + if (nbAttachmentRows > 0) { Rectangle rect = listAttachments.getUI().getCellBounds(listAttachments, 0, 0); - ui.getScrollAttachments().setPreferredSize(new Dimension((int) rect.getWidth()-4, (int) (3+ nbRows * rect.getHeight()))); + ui.getScrollAttachments().setPreferredSize(new Dimension((int) rect.getWidth() - 4, (int) (3 + rows[1] * rect.getHeight()))); } table.invalidate(); @@ -379,6 +383,42 @@ ui.repaint(); } + public static int[] computeRows(int nbDescriptorRows, int nbAttachmentRows) { + int[] result = new int[2]; + if (nbDescriptorRows == 0) { + result[0] = 0; + result[1] = nbAttachmentRows > 10 ? 10 : nbAttachmentRows; + return result; + } + if (nbAttachmentRows == 0) { + result[0] = nbDescriptorRows > 10 ? 10 : nbDescriptorRows; + result[1] = 0; + return result; + } + if (nbAttachmentRows < 6 && nbDescriptorRows < 6) { + result[0] = nbDescriptorRows; + result[1] = nbAttachmentRows; + return result; + } + if (nbDescriptorRows > 5 && nbAttachmentRows > 5) { + result[0] = result[1] = 5; + return result; + } + if (nbDescriptorRows < 6) { + result[0] = nbDescriptorRows; + int i = 10 - nbDescriptorRows; + result[1] = nbAttachmentRows < i ? nbAttachmentRows : i; + return result; + } + if (nbAttachmentRows < 6) { + int i = 10 - nbAttachmentRows; + result[0] = nbDescriptorRows < i ? nbDescriptorRows : i; + result[1] = nbAttachmentRows; + return result; + } + throw new IllegalStateException("can not come her :)"); + } + public static void updateDetailNavigationTree(JApplicationDetailTab ui, DetailTabModel model) { // create navigation tree TreeNode root = LoggableElementTreeHelper.buildLoggableElementNode(model.getRootNode()); Added: trunk/simexplorer-is-swing/src/test/fr/cemagref/simexplorer/is/ui/swing/SimExplorerUIRefreshHelperTest.java =================================================================== --- trunk/simexplorer-is-swing/src/test/fr/cemagref/simexplorer/is/ui/swing/SimExplorerUIRefreshHelperTest.java (rev 0) +++ trunk/simexplorer-is-swing/src/test/fr/cemagref/simexplorer/is/ui/swing/SimExplorerUIRefreshHelperTest.java 2008-02-08 18:39:46 UTC (rev 765) @@ -0,0 +1,76 @@ +package fr.cemagref.simexplorer.is.ui.swing; + +import junit.framework.TestCase; + +/** + * SimExplorerUIRefreshHelper Tester. + * + * @author chemit + * @version 1.0 + * @since <pre>02/08/2008</pre> + */ +public class SimExplorerUIRefreshHelperTest extends TestCase { + public SimExplorerUIRefreshHelperTest(String name) { + super(name); + } + + public void testComputeRowLimitCases() throws Exception { + for (int i = 0; i < 11; i++) { + assertComputeRow(0, i, SimExplorerUIRefreshHelper.computeRows(0, i)); + } + for (int i = 0; i < 11; i++) { + assertComputeRow(i, 0, SimExplorerUIRefreshHelper.computeRows(i, 0)); + } + assertComputeRow(0, 10, SimExplorerUIRefreshHelper.computeRows(0, 11)); + assertComputeRow(10, 0, SimExplorerUIRefreshHelper.computeRows(11, 0)); + } + + + public void testComputeRowDescriptor() throws Exception { + for (int i = 1; i < 9; i++) { + testComputeRowDescriptor(i); + } + } + + public void testComputeRowAttachment() throws Exception { + for (int i = 1; i < 9; i++) { + testComputeRowAttachment(i); + } + } + + public void testComputeRowAttachment(int nb) throws Exception { + int pivot = 10 - nb; + for (int i = 0; i < pivot + 1; i++) { + assertComputeRow(i, nb, SimExplorerUIRefreshHelper.computeRows(i, nb)); + } + for (int i = pivot + 1; i < 11; i++) { + if (nb + i > 10) { + int[] res = SimExplorerUIRefreshHelper.computeRows(i, nb); + assertEquals(10, res[0] + res[1]); + } else { + assertComputeRow(pivot, nb, SimExplorerUIRefreshHelper.computeRows(i, nb)); + } + } + } + + public void testComputeRowDescriptor(int nb) throws Exception { + int pivot = 10 - nb; + for (int i = 0; i < pivot + 1; i++) { + assertComputeRow(nb, i, SimExplorerUIRefreshHelper.computeRows(nb, i)); + } + for (int i = pivot + 1; i < 11; i++) { + if (nb + i > 10) { + int[] res = SimExplorerUIRefreshHelper.computeRows(nb, i); + assertEquals(10, res[0] + res[1]); + } else { + assertComputeRow(nb, pivot, SimExplorerUIRefreshHelper.computeRows(nb, i)); + } + } + } + + public void assertComputeRow(int expectedOne, int expectedTwo, int[] result) { + assertEquals(expectedOne, result[0]); + assertEquals(expectedTwo, result[1]); + } + +}