|
From: <be...@us...> - 2006-10-24 11:38:59
|
Revision: 65
http://svn.sourceforge.net/jtreemap/?rev=65&view=rev
Author: benoitx
Date: 2006-10-24 04:38:30 -0700 (Tue, 24 Oct 2006)
Log Message:
-----------
Sorted most of issues raised by FindBugs, down to 1.
Modified Paths:
--------------
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/ColorProvider.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/DefaultToolTip.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/DefaultToolTipBuilder.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/DefaultValue.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/IToolTipBuilder.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/JTreeMap.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/SplitByNumber.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/SplitBySlice.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/SplitBySortedWeight.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/SplitByWeight.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/SplitSquarified.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/SplitStrategy.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/TreeMapNode.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/TreeMapNodeBuilder.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/UniqueColorProvider.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/Value.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/example/BuilderTM3.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/example/BuilderXML.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/example/HSBTreeMapColorProvider.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/example/JTreeMapAppletExample.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/example/JTreeMapExample.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/example/RandomColorProvider.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/example/RedGreenColorProvider.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/example/ValuePercent.java
trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/example/ZoomPopupMenu.java
Modified: trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/ColorProvider.java
===================================================================
--- trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/ColorProvider.java 2006-10-24 10:28:55 UTC (rev 64)
+++ trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/ColorProvider.java 2006-10-24 11:38:30 UTC (rev 65)
@@ -1,30 +1,33 @@
package net.sf.jtreemap.swing;
-import javax.swing.JPanel;
import java.awt.Color;
+import java.io.Serializable;
+import javax.swing.JPanel;
+
/**
* Abstract class with the methods who attribute color to the elements of
* JTreeMap.
- *
+ *
* @author Laurent DUTHEIL
*/
-public abstract class ColorProvider {
+public abstract class ColorProvider implements Serializable {
- /**
- * get the associated color to the value.
- *
- *@param value double value
- *@return the associated color to the value
- */
- public abstract Color getColor(Value value);
+ /**
+ * get the associated color to the value.
+ *
+ * @param value
+ * double value
+ * @return the associated color to the value
+ */
+ public abstract Color getColor(Value value);
- /**
- * get a legend JPanel.
- *
- *@return a legend JPanel
- */
- public abstract JPanel getLegendPanel();
+ /**
+ * get a legend JPanel.
+ *
+ * @return a legend JPanel
+ */
+ public abstract JPanel getLegendPanel();
}
Modified: trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/DefaultToolTip.java
===================================================================
--- trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/DefaultToolTip.java 2006-10-24 10:28:55 UTC (rev 64)
+++ trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/DefaultToolTip.java 2006-10-24 11:38:30 UTC (rev 65)
@@ -12,47 +12,48 @@
/**
* Default ToolTip for the jTreeMap.
- *
+ *
* @author Laurent DUTHEIL
*/
public class DefaultToolTip extends JToolTip {
- private static final long serialVersionUID = -2492627777999093973L;
- private JTreeMap jTreeMap;
- private Font labelFont;
- private Font valueFont;
+ private static final long serialVersionUID = -2492627777999093973L;
- /**
- * Constructor.
- *
- * @param jTreeMap the jTreeMap who display the tooltip
- */
- public DefaultToolTip(JTreeMap jTreeMap) {
- this.jTreeMap = jTreeMap;
- this.labelFont = new Font("Default", Font.BOLD, 14);
- this.valueFont = new Font("Default", Font.PLAIN, 10);
+ private JTreeMap jTreeMap;
- int width = 160;
- int height = getFontMetrics(this.labelFont).getHeight()
- + getFontMetrics(this.valueFont).getHeight();
+ private Font labelFont;
- Dimension size = new Dimension(width, height);
- this.setSize(size);
- this.setPreferredSize(size);
- }
+ private Font valueFont;
- @Override
- public void paint(Graphics g) {
- if (this.jTreeMap.getActiveLeaf() != null) {
- g.setColor(Color.lightGray);
- g.fill3DRect(0, 0, this.getWidth(), this.getHeight(), true);
- g.setColor(Color.black);
- g.setFont(this.labelFont);
- g.drawString(this.jTreeMap.getActiveLeaf().getLabel(), 5, g
- .getFontMetrics(this.labelFont).getAscent());
- g.setFont(this.valueFont);
- g.drawString(this.jTreeMap.getActiveLeaf().getLabelValue(), 5, this
- .getHeight() - 5);
+ /**
+ * Constructor.
+ *
+ * @param jTreeMap
+ * the jTreeMap who display the tooltip
+ */
+ public DefaultToolTip(final JTreeMap jTreeMap) {
+ this.jTreeMap = jTreeMap;
+ this.labelFont = new Font("Default", Font.BOLD, 14);
+ this.valueFont = new Font("Default", Font.PLAIN, 10);
+
+ final int width = 160;
+ final int height = getFontMetrics(this.labelFont).getHeight() + getFontMetrics(this.valueFont).getHeight();
+
+ final Dimension size = new Dimension(width, height);
+ this.setSize(size);
+ this.setPreferredSize(size);
}
- }
+ @Override
+ public void paint(final Graphics g) {
+ if (this.jTreeMap.getActiveLeaf() != null) {
+ g.setColor(Color.lightGray);
+ g.fill3DRect(0, 0, this.getWidth(), this.getHeight(), true);
+ g.setColor(Color.black);
+ g.setFont(this.labelFont);
+ g.drawString(this.jTreeMap.getActiveLeaf().getLabel(), 5, g.getFontMetrics(this.labelFont).getAscent());
+ g.setFont(this.valueFont);
+ g.drawString(this.jTreeMap.getActiveLeaf().getLabelValue(), 5, this.getHeight() - 5);
+ }
+ }
+
}
Modified: trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/DefaultToolTipBuilder.java
===================================================================
--- trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/DefaultToolTipBuilder.java 2006-10-24 10:28:55 UTC (rev 64)
+++ trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/DefaultToolTipBuilder.java 2006-10-24 11:38:30 UTC (rev 65)
@@ -3,35 +3,46 @@
*/
package net.sf.jtreemap.swing;
+import java.io.Serializable;
+
import javax.swing.JToolTip;
/**
* Default class to build the DefaultToolTip displayed by the JTreeMap.<BR>
- *
+ *
* @see net.sf.jtreemap.swing.DefaultToolTip
* @author Laurent DUTHEIL
- *
+ *
*/
-public class DefaultToolTipBuilder implements IToolTipBuilder {
- private static JToolTip instance = null;
- private JTreeMap jTreeMap;
+public class DefaultToolTipBuilder implements IToolTipBuilder, Serializable {
+ /**
+ *
+ */
+ private static final long serialVersionUID = -1646772942428571187L;
- /**
- * Constructor.
- *
- * @param jTreeMap the linked JTreeMap
- */
- public DefaultToolTipBuilder(JTreeMap jTreeMap) {
- this.jTreeMap = jTreeMap;
- }
+ private static JToolTip instance = null;
- /* (non-Javadoc)
- * @see net.sf.jtreemap.swing.IToolTipBuilder#getToolTip()
- */
- public JToolTip getToolTip() {
- if (instance == null) {
- instance = new DefaultToolTip(this.jTreeMap);
+ private JTreeMap jTreeMap;
+
+ /**
+ * Constructor.
+ *
+ * @param jTreeMap
+ * the linked JTreeMap
+ */
+ public DefaultToolTipBuilder(final JTreeMap jTreeMap) {
+ this.jTreeMap = jTreeMap;
}
- return instance;
- }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sf.jtreemap.swing.IToolTipBuilder#getToolTip()
+ */
+ public JToolTip getToolTip() {
+ if (instance == null) {
+ instance = new DefaultToolTip(this.jTreeMap);
+ }
+ return instance;
+ }
}
Modified: trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/DefaultValue.java
===================================================================
--- trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/DefaultValue.java 2006-10-24 10:28:55 UTC (rev 64)
+++ trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/DefaultValue.java 2006-10-24 11:38:30 UTC (rev 65)
@@ -6,67 +6,72 @@
/**
* Default Value <BR>
* The getLabel() method returns the "" + getValue()
- *
+ *
* @author Laurent DUTHEIL
*/
public class DefaultValue extends Value {
- private double value;
+ /**
+ *
+ */
+ private static final long serialVersionUID = 367321198951855282L;
+ private double value;
- /**
- * Constructor.
- */
- public DefaultValue() {
- //nothing to do
- }
+ /**
+ * Constructor.
+ */
+ public DefaultValue() {
+ // nothing to do
+ }
- /**
- * Constructor.
- *
- * @param value double value
- */
- public DefaultValue(double value) {
- this.value = value;
- }
+ /**
+ * Constructor.
+ *
+ * @param value
+ * double value
+ */
+ public DefaultValue(final double value) {
+ this.value = value;
+ }
- /*
- * (non-Javadoc)
- *
- * @see net.sf.jtreemap.swing.Value#getValue()
- */
- @Override
- public double getValue() {
- return this.value;
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sf.jtreemap.swing.Value#getValue()
+ */
+ @Override
+ public double getValue() {
+ return this.value;
+ }
- /*
- * (non-Javadoc)
- *
- * @see net.sf.jtreemap.swing.Value#getLabel()
- */
- @Override
- public String getLabel() {
- return "" + this.value;
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sf.jtreemap.swing.Value#getLabel()
+ */
+ @Override
+ public String getLabel() {
+ return "" + this.value;
+ }
- /*
- * (non-Javadoc)
- *
- * @see net.sf.jtreemap.swing.Value#setValue(double)
- */
- @Override
- public void setValue(double value) {
- this.value = value;
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sf.jtreemap.swing.Value#setValue(double)
+ */
+ @Override
+ public void setValue(final double value) {
+ this.value = value;
+ }
- /*
- * (non-Javadoc)
- *
- * @see net.sf.jtreemap.swing.Value#setLabel(java.lang.String)
- */
- @Override
- public void setLabel(String newLabel) {
- // ignore
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sf.jtreemap.swing.Value#setLabel(java.lang.String)
+ */
+ @Override
+ public void setLabel(final String newLabel) {
+ // ignore
- }
+ }
}
Modified: trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/IToolTipBuilder.java
===================================================================
--- trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/IToolTipBuilder.java 2006-10-24 10:28:55 UTC (rev 64)
+++ trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/IToolTipBuilder.java 2006-10-24 11:38:30 UTC (rev 65)
@@ -8,18 +8,17 @@
/**
* Interface used to build your own JToolTip for the jTreeMap.
- *
+ *
* @see net.sf.jtreemap.swing.JTreeMap#setToolTipBuilder(IToolTipBuilder)
* @author Laurent DUTHEIL
*/
public interface IToolTipBuilder {
- /**
- * Return the instance of the JToolTip.<BR>
- * Override this method to build your own JToolTip
- *
- * @return the instance of the JToolTip
- */
- public abstract JToolTip getToolTip();
-
+ /**
+ * Return the instance of the JToolTip.<BR>
+ * Override this method to build your own JToolTip
+ *
+ * @return the instance of the JToolTip
+ */
+ JToolTip getToolTip();
}
\ No newline at end of file
Modified: trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/JTreeMap.java
===================================================================
--- trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/JTreeMap.java 2006-10-24 10:28:55 UTC (rev 64)
+++ trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/JTreeMap.java 2006-10-24 11:38:30 UTC (rev 65)
@@ -1,13 +1,14 @@
package net.sf.jtreemap.swing;
-import java.util.Enumeration;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;
+import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
-import java.awt.event.MouseEvent;
+import java.io.Serializable;
+import java.util.Enumeration;
import javax.swing.JComponent;
import javax.swing.JToolTip;
@@ -27,555 +28,587 @@
* <p>
* So, in a JTreeMap, you can see the size and the value of an element in a
* tree.
- *
+ *
* @see net.sf.jtreemap.swing.TreeMapNode
* @author Laurent Dutheil
*/
public class JTreeMap extends JComponent {
- private static final long serialVersionUID = 7255952672238300249L;
- private static final Color transparencyColor = new Color(204, 204, 204, 128);
- // active leaf
- private TreeMapNode activeLeaf = null;
- // color provider
- private ColorProvider colorProvider = null;
- // displayed root
- private TreeMapNode displayedRoot = null;
- // root of the tree
- private TreeMapNode root = null;
- // divide strategy
- private SplitStrategy strategy = null;
- // tooltip builder
- private IToolTipBuilder toolTipBuilder;
- // zoom
- private Zoom zoom;
+ private static final long serialVersionUID = 7255952672238300249L;
- /**
- * Constructor of JTreeMap. <BR>
- * The chosen strategy is SplitSquarified. <BR>
- * The chosen color provider is UniqueColorProvider.
- *
- * @see SplitSquarified
- * @see UniqueColorProvider
- * @param root the root of the tree to display
- */
- public JTreeMap(TreeMapNode root) {
- this(root, new SplitSquarified());
- }
+ private static final Color transparencyColor = new Color(204, 204, 204, 128);
- /**
- * Constructor of JTreeMap. <BR>
- * The chosen color provider is UniqueColorProvider.
- *
- * @see UniqueColorProvider
- * @param root the root of the tree to display
- * @param strategy the split strategy
- */
- public JTreeMap(TreeMapNode root, SplitStrategy strategy) {
- // ToolTips appears without delay and stay as long as possible
- ToolTipManager ttm = ToolTipManager.sharedInstance();
- ttm.setInitialDelay(0);
- ttm.setReshowDelay(0);
- ttm.setDismissDelay(100000);
- ttm.setEnabled(true);
- ttm.setLightWeightPopupEnabled(true);
- this.setToolTipText("");
+ // active leaf
+ private TreeMapNode activeLeaf = null;
- // the default DefaultToolTipBuilder
- this.toolTipBuilder = new DefaultToolTipBuilder(this);
+ // color provider
+ private ColorProvider colorProvider = null;
- this.zoom = new Zoom();
+ // displayed root
+ private TreeMapNode displayedRoot = null;
- this.setRoot(root);
- this.setStrategy(strategy);
- this.setColorProvider(new UniqueColorProvider());
+ // root of the tree
+ private TreeMapNode root = null;
- this.addMouseMotionListener(new HandleMouseMotion());
- }
+ // divide strategy
+ private SplitStrategy strategy = null;
- /**
- * calculate the postitions for the displayed root. <BR>
- * The positions of the root must be calculated first.
- */
- public void calculatePositions() {
- if (this.getStrategy() != null && this.displayedRoot != null) {
- this.getStrategy().calculatePositions(this.displayedRoot);
- }
- }
+ // tooltip builder
+ private IToolTipBuilder toolTipBuilder;
- @Override
- public JToolTip createToolTip() {
- return this.toolTipBuilder.getToolTip();
- }
+ // zoom
+ private Zoom zoom;
- /**
- * draw the item.
- *
- * @param g Graphics where you have to draw
- * @param item item to draw
- */
- protected void draw(Graphics g, TreeMapNode item) {
- if (item.isLeaf()) {
- g.setColor(this.colorProvider.getColor(item.getValue()));
- g.fillRect(item.getX(), item.getY(), item.getWidth(), item.getHeight());
- } else {
- for (Enumeration e = item.children(); e.hasMoreElements();) {
- draw(g, (TreeMapNode) (e.nextElement()));
- }
+ /**
+ * Constructor of JTreeMap. <BR>
+ * The chosen strategy is SplitSquarified. <BR>
+ * The chosen color provider is UniqueColorProvider.
+ *
+ * @see SplitSquarified
+ * @see UniqueColorProvider
+ * @param root
+ * the root of the tree to display
+ */
+ public JTreeMap(final TreeMapNode root) {
+ this(root, new SplitSquarified());
}
- }
- /**
- * write the label in the middle of the item. <BR>
- * You have first to define the font of the Graphics. <BR>
- * You may override this method to change the position or the color of the
- * label.
- *
- * @param g Graphics where you have to draw
- * @param item TreeMapNode to draw
- */
- protected void drawLabel(Graphics g, TreeMapNode item) {
- FontMetrics fm = g.getFontMetrics(g.getFont());
- // if the height of the item is high enough
- if (fm.getHeight() < item.getHeight() - 2) {
- String label = item.getLabel();
+ /**
+ * Constructor of JTreeMap. <BR>
+ * The chosen color provider is UniqueColorProvider.
+ *
+ * @see UniqueColorProvider
+ * @param root
+ * the root of the tree to display
+ * @param strategy
+ * the split strategy
+ */
+ public JTreeMap(final TreeMapNode root, final SplitStrategy strategy) {
+ // ToolTips appears without delay and stay as long as possible
+ final ToolTipManager ttm = ToolTipManager.sharedInstance();
+ ttm.setInitialDelay(0);
+ ttm.setReshowDelay(0);
+ ttm.setDismissDelay(100000);
+ ttm.setEnabled(true);
+ ttm.setLightWeightPopupEnabled(true);
+ this.setToolTipText("");
- int y = (item.getHeight() + fm.getAscent() - fm.getDescent()) / 2;
- int stringWidth = fm.stringWidth(label);
- // the width of the label depends on the font :
- // if the width of the label is larger than the item
- if (item.getWidth() - 5 <= stringWidth) {
- // We have to truncate the label
- // number of chars who can be writen in the item
- int nbChar = (label.length() * item.getWidth()) / stringWidth;
- if (nbChar > 3) {
- // and add "..." at the end
- label = label.substring(0, nbChar - 3) + "...";
- } else {
- // if it is not enough large, we display nothing
- label = "";
- }
- }
- int x = (item.getWidth() - fm.stringWidth(label)) / 2;
+ // the default DefaultToolTipBuilder
+ this.toolTipBuilder = new DefaultToolTipBuilder(this);
- // background in black
- g.setColor(Color.black);
- g.drawString(label, item.getX() + x + 1, item.getY() + y + 1);
- g.drawString(label, item.getX() + x - 1, item.getY() + y + 1);
- g.drawString(label, item.getX() + x + 1, item.getY() + y - 1);
- g.drawString(label, item.getX() + x - 1, item.getY() + y - 1);
- g.drawString(label, item.getX() + x + 1, item.getY() + y);
- g.drawString(label, item.getX() + x - 1, item.getY() + y);
- g.drawString(label, item.getX() + x, item.getY() + y + 1);
- g.drawString(label, item.getX() + x, item.getY() + y - 1);
- // label in white
- g.setColor(Color.white);
- g.drawString(label, item.getX() + x, item.getY() + y);
+ this.zoom = new Zoom();
+
+ this.setRoot(root);
+ this.setStrategy(strategy);
+ this.setColorProvider(new UniqueColorProvider());
+
+ this.addMouseMotionListener(new HandleMouseMotion());
}
- }
- /**
- * Draw all the labels to draw. <BR>
- * You may override this method to draw the labels you want. <BR>
- * For exemples, all the leaves, or all the first level children, or all of
- * them...
- *
- * @param g Graphics where you have to draw
- * @param item TreeMapNode to draw
- */
- protected void drawLabels(Graphics g, TreeMapNode item) {
- // add the labels (level -1)
- g.setFont(this.getFont());
- if (this.displayedRoot.isLeaf()) {
- drawLabel(g, this.displayedRoot);
- } else {
- for (Enumeration e = this.displayedRoot.children(); e.hasMoreElements();) {
- drawLabel(g, (TreeMapNode) (e.nextElement()));
- }
+ /**
+ * calculate the postitions for the displayed root. <BR>
+ * The positions of the root must be calculated first.
+ */
+ public void calculatePositions() {
+ if (this.getStrategy() != null && this.displayedRoot != null) {
+ this.getStrategy().calculatePositions(this.displayedRoot);
+ }
}
- /* uncomment to add the labels of the lowered levels (up to depth > 2) */
- // int depth = item.getLevel() - this.displayedRoot.getLevel();
- // float newSize = Math.max(20, this.getFont().getSize2D());
- // java.awt.Font labelFont = this.getFont().deriveFont(java.awt.Font.BOLD,
- // newSize - 3 * depth);
- // g.setFont(labelFont);
- // if (depth > 2) {
- // drawLabel(g, item);
- // return;
- // }
- // if (item.isLeaf()) {
- // drawLabel(g, item);
- // } else {
- // for (Enumeration e = item.children(); e.hasMoreElements();) {
- // drawLabels(g, (TreeMapNode) (e.nextElement()));
- // }
- // }
- }
+ @Override
+ public JToolTip createToolTip() {
+ return this.toolTipBuilder.getToolTip();
+ }
- /**
- * get the active leaf (the one under the mouse).
- *
- * @return Returns the activeLeaf.
- */
- public TreeMapNode getActiveLeaf() {
- return this.activeLeaf;
- }
+ /**
+ * draw the item.
+ *
+ * @param g
+ * Graphics where you have to draw
+ * @param item
+ * item to draw
+ */
+ protected void draw(final Graphics g, final TreeMapNode item) {
+ if (item.isLeaf()) {
+ g.setColor(this.colorProvider.getColor(item.getValue()));
+ g.fillRect(item.getX(), item.getY(), item.getWidth(), item.getHeight());
+ } else {
+ for (final Enumeration e = item.children(); e.hasMoreElements();) {
+ draw(g, (TreeMapNode) (e.nextElement()));
+ }
+ }
+ }
- /**
- * get the ColorProvider.
- *
- * @return the ColorProvider
- */
- public ColorProvider getColorProvider() {
- return this.colorProvider;
- }
+ /**
+ * write the label in the middle of the item. <BR>
+ * You have first to define the font of the Graphics. <BR>
+ * You may override this method to change the position or the color of the
+ * label.
+ *
+ * @param g
+ * Graphics where you have to draw
+ * @param item
+ * TreeMapNode to draw
+ */
+ protected void drawLabel(final Graphics g, final TreeMapNode item) {
+ final FontMetrics fm = g.getFontMetrics(g.getFont());
+ // if the height of the item is high enough
+ if (fm.getHeight() < item.getHeight() - 2) {
+ String label = item.getLabel();
- /**
- * get the displayed root.
- * <p>
- * This may be not the root of the jTreeMap. After a zoom, the displayed root
- * can be the root of an under-tree.
- * </p>
- *
- * @return the displayed root
- */
- public TreeMapNode getDisplayedRoot() {
- return this.displayedRoot;
- }
+ final int y = (item.getHeight() + fm.getAscent() - fm.getDescent()) / 2;
+ final int stringWidth = fm.stringWidth(label);
+ // the width of the label depends on the font :
+ // if the width of the label is larger than the item
+ if (item.getWidth() - 5 <= stringWidth) {
+ // We have to truncate the label
+ // number of chars who can be writen in the item
+ final int nbChar = (label.length() * item.getWidth()) / stringWidth;
+ if (nbChar > 3) {
+ // and add "..." at the end
+ label = label.substring(0, nbChar - 3) + "...";
+ } else {
+ // if it is not enough large, we display nothing
+ label = "";
+ }
+ }
+ final int x = (item.getWidth() - fm.stringWidth(label)) / 2;
- /**
- * get the root.
- *
- * @return the root
- */
- public TreeMapNode getRoot() {
- return this.root;
- }
+ // background in black
+ g.setColor(Color.black);
+ g.drawString(label, item.getX() + x + 1, item.getY() + y + 1);
+ g.drawString(label, item.getX() + x - 1, item.getY() + y + 1);
+ g.drawString(label, item.getX() + x + 1, item.getY() + y - 1);
+ g.drawString(label, item.getX() + x - 1, item.getY() + y - 1);
+ g.drawString(label, item.getX() + x + 1, item.getY() + y);
+ g.drawString(label, item.getX() + x - 1, item.getY() + y);
+ g.drawString(label, item.getX() + x, item.getY() + y + 1);
+ g.drawString(label, item.getX() + x, item.getY() + y - 1);
+ // label in white
+ g.setColor(Color.white);
+ g.drawString(label, item.getX() + x, item.getY() + y);
+ }
+ }
- /**
- * get the SplitStrategy.
- *
- * @return the SplitStrategy
- */
- public SplitStrategy getStrategy() {
- return this.strategy;
- }
+ /**
+ * Draw all the labels to draw. <BR>
+ * You may override this method to draw the labels you want. <BR>
+ * For exemples, all the leaves, or all the first level children, or all of
+ * them...
+ *
+ * @param g
+ * Graphics where you have to draw
+ * @param item
+ * TreeMapNode to draw
+ */
+ protected void drawLabels(final Graphics g, final TreeMapNode item) {
+ // add the labels (level -1)
+ g.setFont(this.getFont());
+ if (this.displayedRoot.isLeaf()) {
+ drawLabel(g, this.displayedRoot);
+ } else {
+ for (final Enumeration e = this.displayedRoot.children(); e.hasMoreElements();) {
+ drawLabel(g, (TreeMapNode) (e.nextElement()));
+ }
+ }
- @Override
- public Point getToolTipLocation(MouseEvent event) {
- int posX;
- int posY;
- JToolTip toolTip = this.createToolTip();
- int XMax = this.displayedRoot.getX() + this.displayedRoot.getWidth();
- int YMin = this.displayedRoot.getY();
- if (this.activeLeaf != null) {
- if (this.activeLeaf.getWidth() >= toolTip.getWidth() + 8
- && this.activeLeaf.getHeight() >= toolTip.getHeight() + 8) {
- posX = this.activeLeaf.getX() + 4;
- posY = this.activeLeaf.getY() + 4;
- } else {
- posX = this.activeLeaf.getX() + this.activeLeaf.getWidth() + 4;
- posY = this.activeLeaf.getY() - toolTip.getHeight() - 4;
- }
+ /* uncomment to add the labels of the lowered levels (up to depth > 2) */
+ // int depth = item.getLevel() - this.displayedRoot.getLevel();
+ // float newSize = Math.max(20, this.getFont().getSize2D());
+ // java.awt.Font labelFont =
+ // this.getFont().deriveFont(java.awt.Font.BOLD,
+ // newSize - 3 * depth);
+ // g.setFont(labelFont);
+ // if (depth > 2) {
+ // drawLabel(g, item);
+ // return;
+ // }
+ // if (item.isLeaf()) {
+ // drawLabel(g, item);
+ // } else {
+ // for (Enumeration e = item.children(); e.hasMoreElements();) {
+ // drawLabels(g, (TreeMapNode) (e.nextElement()));
+ // }
+ // }
+ }
- if (posY < YMin + 4) {
- posY = YMin + 4;
- }
- if ((posX + toolTip.getWidth() > XMax - 4)
- && (this.activeLeaf.getX() >= toolTip.getWidth() + 4)) {
- posX = this.activeLeaf.getX() - 4 - toolTip.getWidth();
- }
-
- return new Point(posX, posY);
+ /**
+ * get the active leaf (the one under the mouse).
+ *
+ * @return Returns the activeLeaf.
+ */
+ public TreeMapNode getActiveLeaf() {
+ return this.activeLeaf;
}
- return null;
- }
- /*
- * (non-Javadoc)
- *
- * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
- */
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- int width = getSize().width;
- int height = getSize().height;
- Insets insets = getInsets();
-
- int border = TreeMapNode.getBorder();
- this.root.setDimension(this.root.getX(), this.root.getY(), width - border
- - insets.left - insets.right, height - border - insets.top
- - insets.bottom);
-
- if (!this.root.equals( this.displayedRoot )) {
- this.displayedRoot.setDimension(this.displayedRoot.getX(), this.displayedRoot.getY(), width - border
- - insets.left - insets.right, height - border - insets.top
- - insets.bottom);
+ /**
+ * get the ColorProvider.
+ *
+ * @return the ColorProvider
+ */
+ public ColorProvider getColorProvider() {
+ return this.colorProvider;
}
- this.calculatePositions();
-
- if (this.displayedRoot.children().hasMoreElements()) {
- // the background
- g.setColor(this.getBackground());
- g.fillRect(this.displayedRoot.getX(), this.displayedRoot.getY(),
- this.displayedRoot.getWidth() + border, this.displayedRoot
- .getHeight()
- + border);
- // the JTreeMapExample
- draw(g, this.displayedRoot);
- // reveal the active leaf
- if (this.activeLeaf != null) {
- reveal(g, this.activeLeaf);
- }
- // the labels
- drawLabels(g, this.displayedRoot);
+ /**
+ * get the displayed root.
+ * <p>
+ * This may be not the root of the jTreeMap. After a zoom, the displayed
+ * root can be the root of an under-tree.
+ * </p>
+ *
+ * @return the displayed root
+ */
+ public TreeMapNode getDisplayedRoot() {
+ return this.displayedRoot;
}
- }
-
- /**
- * reveal the item.
- *
- * @param g Graphics where you have to draw
- * @param item TreeMapNode to reveal
- */
- protected void reveal(Graphics g, TreeMapNode item) {
- if (item.isLeaf()) {
- g.setColor(transparencyColor);
- g.fillRect(item.getX(), item.getY(), item.getWidth(), item.getHeight());
+ /**
+ * get the root.
+ *
+ * @return the root
+ */
+ public TreeMapNode getRoot() {
+ return this.root;
}
- }
- /**
- * set the active leaf.
- *
- * @param newActiveLeaf the new active leaf
- */
- public void setActiveLeaf(TreeMapNode newActiveLeaf) {
- if (newActiveLeaf == null || newActiveLeaf.isLeaf()) {
- this.activeLeaf = newActiveLeaf;
+ /**
+ * get the SplitStrategy.
+ *
+ * @return the SplitStrategy
+ */
+ public SplitStrategy getStrategy() {
+ return this.strategy;
}
- }
- /*
- * (non-Javadoc)
- *
- * @see javax.swing.JComponent#setBorder(javax.swing.border.Border)
- */
- @Override
- public void setBorder(Border border) {
- // Substract the previous border insets
- Insets insets = getInsets();
- this.displayedRoot.setDimension(this.displayedRoot.getX() - insets.left,
- this.displayedRoot.getY() - insets.top, this.displayedRoot.getWidth()
- + insets.left + insets.right, this.displayedRoot.getHeight()
- + insets.top + insets.bottom);
+ @Override
+ public Point getToolTipLocation(final MouseEvent event) {
+ int posX;
+ int posY;
+ final JToolTip toolTip = this.createToolTip();
+ final int XMax = this.displayedRoot.getX() + this.displayedRoot.getWidth();
+ final int YMin = this.displayedRoot.getY();
+ if (this.activeLeaf != null) {
+ if (this.activeLeaf.getWidth() >= toolTip.getWidth() + 8 && this.activeLeaf.getHeight() >= toolTip.getHeight() + 8) {
+ posX = this.activeLeaf.getX() + 4;
+ posY = this.activeLeaf.getY() + 4;
+ } else {
+ posX = this.activeLeaf.getX() + this.activeLeaf.getWidth() + 4;
+ posY = this.activeLeaf.getY() - toolTip.getHeight() - 4;
+ }
- super.setBorder(border);
+ if (posY < YMin + 4) {
+ posY = YMin + 4;
+ }
+ if ((posX + toolTip.getWidth() > XMax - 4) && (this.activeLeaf.getX() >= toolTip.getWidth() + 4)) {
+ posX = this.activeLeaf.getX() - 4 - toolTip.getWidth();
+ }
- // Add the new border insets
- insets = getInsets();
- this.displayedRoot.setDimension(this.displayedRoot.getX() + insets.left,
- this.displayedRoot.getY() + insets.top, this.displayedRoot.getWidth()
- - insets.left - insets.right, this.displayedRoot.getHeight()
- - insets.top - insets.bottom);
- }
+ return new Point(posX, posY);
+ }
+ return null;
+ }
- /**
- * set the ColorProvider.
- *
- * @param newColorProvider the new ColorPorvider
- */
- public void setColorProvider(ColorProvider newColorProvider) {
- this.colorProvider = newColorProvider;
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
+ */
+ @Override
+ protected void paintComponent(final Graphics g) {
+ super.paintComponent(g);
+ final int width = getSize().width;
+ final int height = getSize().height;
+ final Insets insets = getInsets();
- /**
- * set the displayed root.
- * <p>
- * This may be not the root of the jTreeMap. After a zoom, the displayed root
- * can be the root of an under-tree.
- * </p>
- *
- * @param newDisplayedRoot new DiplayedRoot
- */
- public void setDisplayedRoot(TreeMapNode newDisplayedRoot) {
- this.displayedRoot = newDisplayedRoot;
- }
+ final int border = TreeMapNode.getBorder();
+ this.root.setDimension(this.root.getX(), this.root.getY(), width - border - insets.left - insets.right, height - border
+ - insets.top - insets.bottom);
- /**
- * set the new root.
- *
- * @param newRoot the new root to set
- */
- public void setRoot(TreeMapNode newRoot) {
- this.root = newRoot;
- Insets insets = getInsets();
- this.root.setX(insets.left);
- this.root.setY(insets.top);
- this.setDisplayedRoot(this.root);
+ if (!this.root.equals(this.displayedRoot)) {
+ this.displayedRoot.setDimension(this.displayedRoot.getX(), this.displayedRoot.getY(), width - border - insets.left
+ - insets.right, height - border - insets.top - insets.bottom);
+ }
- }
+ this.calculatePositions();
- /**
- * set the new strategy.
- *
- * @param newStrat the new strategy to set
- */
- public void setStrategy(SplitStrategy newStrat) {
- this.strategy = newStrat;
- }
+ if (this.displayedRoot.children().hasMoreElements()) {
+ // the background
+ g.setColor(this.getBackground());
+ g.fillRect(this.displayedRoot.getX(), this.displayedRoot.getY(), this.displayedRoot.getWidth() + border,
+ this.displayedRoot.getHeight() + border);
+ // the JTreeMapExample
+ draw(g, this.displayedRoot);
+ // reveal the active leaf
+ if (this.activeLeaf != null) {
+ reveal(g, this.activeLeaf);
+ }
+ // the labels
+ drawLabels(g, this.displayedRoot);
+ }
- /**
- * Set the builder of the toolTip.<BR>
- *
- * @param toolTipBuilder The toolTipBuilder to set.
- */
- public void setToolTipBuilder(IToolTipBuilder toolTipBuilder) {
- this.toolTipBuilder = toolTipBuilder;
- }
+ }
- /**
- * When you zoom the jTreeMap, you have the choice to keep proportions or not.
- *
- * @param keepProportion true if you want to keep proportions, else false
- */
- public void setZoomKeepProportion(boolean keepProportion) {
- this.zoom.setKeepProportion(keepProportion);
- }
+ /**
+ * reveal the item.
+ *
+ * @param g
+ * Graphics where you have to draw
+ * @param item
+ * TreeMapNode to reveal
+ */
+ protected void reveal(final Graphics g, final TreeMapNode item) {
+ if (item.isLeaf()) {
+ g.setColor(transparencyColor);
+ g.fillRect(item.getX(), item.getY(), item.getWidth(), item.getHeight());
+ }
+ }
- /**
- * Undo the zoom to display the root.
- */
- public void unzoom() {
- this.zoom.undo();
- }
+ /**
+ * set the active leaf.
+ *
+ * @param newActiveLeaf
+ * the new active leaf
+ */
+ public void setActiveLeaf(final TreeMapNode newActiveLeaf) {
+ if (newActiveLeaf == null || newActiveLeaf.isLeaf()) {
+ this.activeLeaf = newActiveLeaf;
+ }
+ }
- /**
- * Zoom the JTreeMap to the dest node.
- *
- * @param dest node we want to zoom
- */
- public void zoom(TreeMapNode dest) {
- // undo the last zoom
- unzoom();
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.swing.JComponent#setBorder(javax.swing.border.Border)
+ */
+ @Override
+ public void setBorder(final Border border) {
+ // Substract the previous border insets
+ Insets insets = getInsets();
+ this.displayedRoot.setDimension(this.displayedRoot.getX() - insets.left, this.displayedRoot.getY() - insets.top,
+ this.displayedRoot.getWidth() + insets.left + insets.right, this.displayedRoot.getHeight() + insets.top
+ + insets.bottom);
- this.zoom.execute(dest);
- }
+ super.setBorder(border);
- /**
- * Listener who define the active leaf and set the tooltip text.
- *
- * @author Laurent Dutheil
- */
- protected class HandleMouseMotion extends MouseMotionAdapter {
+ // Add the new border insets
+ insets = getInsets();
+ this.displayedRoot.setDimension(this.displayedRoot.getX() + insets.left, this.displayedRoot.getY() + insets.top,
+ this.displayedRoot.getWidth() - insets.left - insets.right, this.displayedRoot.getHeight() - insets.top
+ - insets.bottom);
+ }
- @Override
- public void mouseMoved(MouseEvent e) {
- if (getDisplayedRoot().children().hasMoreElements()) {
- TreeMapNode t = getDisplayedRoot().getActiveLeaf(e.getX(), e.getY());
- if (t != null && !t.equals(getActiveLeaf())) {
- setActiveLeaf(t);
- repaint();
- }
- if (t != null) {
- setToolTipText(t.getLabel() + " " + t.getValue().getValue());
- } else {
- setToolTipText(null);
- }
- }
+ /**
+ * set the ColorProvider.
+ *
+ * @param newColorProvider
+ * the new ColorPorvider
+ */
+ public void setColorProvider(final ColorProvider newColorProvider) {
+ this.colorProvider = newColorProvider;
}
- }
- /**
- * Class who zoom and unzoom the JTreeMap.
- *
- * @author Laurent Dutheil
- */
- private class Zoom {
- private boolean enable;
- private boolean keepProportion = false;
-
/**
- * Constructor
+ * set the displayed root.
+ * <p>
+ * This may be not the root of the jTreeMap. After a zoom, the displayed
+ * root can be the root of an under-tree.
+ * </p>
+ *
+ * @param newDisplayedRoot
+ * new DiplayedRoot
*/
- public Zoom() {
- this.enable = true;
+ public void setDisplayedRoot(final TreeMapNode newDisplayedRoot) {
+ this.displayedRoot = newDisplayedRoot;
}
/**
- * Execute the zoom.
- * @param dest TreeMapNode where you want to zoom
+ * set the new root.
+ *
+ * @param newRoot
+ * the new root to set
*/
- public void execute(TreeMapNode dest) {
- if (this.enable) {
- JTreeMap.this.setActiveLeaf(null);
+ public void setRoot(final TreeMapNode newRoot) {
+ this.root = newRoot;
+ final Insets insets = getInsets();
+ this.root.setX(insets.left);
+ this.root.setY(insets.top);
+ this.setDisplayedRoot(this.root);
- setNewDimension(dest);
+ }
- JTreeMap.this.setDisplayedRoot(dest);
+ /**
+ * set the new strategy.
+ *
+ * @param newStrat
+ * the new strategy to set
+ */
+ public void setStrategy(final SplitStrategy newStrat) {
+ this.strategy = newStrat;
+ }
- this.enable = false;
- }
+ /**
+ * Set the builder of the toolTip.<BR>
+ *
+ * @param toolTipBuilder
+ * The toolTipBuilder to set.
+ */
+ public void setToolTipBuilder(final IToolTipBuilder toolTipBuilder) {
+ this.toolTipBuilder = toolTipBuilder;
}
/**
- * @return Returns the keepProportion.
+ * When you zoom the jTreeMap, you have the choice to keep proportions or
+ * not.
+ *
+ * @param keepProportion
+ * true if you want to keep proportions, else false
*/
- public boolean isKeepProportion() {
- return this.keepProportion;
+ public void setZoomKeepProportion(final boolean keepProportion) {
+ this.zoom.setKeepProportion(keepProportion);
}
/**
- * @param keepProportion The keepProportion to set.
+ * Undo the zoom to display the root.
*/
- public void setKeepProportion(boolean keepProportion) {
- this.keepProportion = keepProportion;
+ public void unzoom() {
+ this.zoom.undo();
}
/**
- * set the new dimensions of the dest root
- * @param dest the root to dimension
+ * Zoom the JTreeMap to the dest node.
+ *
+ * @param dest
+ * node we want to zoom
*/
- protected void setNewDimension(TreeMapNode dest) {
- dest.setX(JTreeMap.this.getRoot().getX());
- dest.setY(JTreeMap.this.getRoot().getY());
+ public void zoom(final TreeMapNode dest) {
+ // undo the last zoom
+ unzoom();
- int rootWidth = JTreeMap.this.getRoot().getWidth();
- int rootHeight = JTreeMap.this.getRoot().getHeight();
+ this.zoom.execute(dest);
+ }
- if (isKeepProportion()) {
- int destHeight = dest.getHeight();
- int destWidth = dest.getWidth();
- float divWidth = (float) destWidth / (float) rootWidth;
- float divHeight = (float) destHeight / (float) rootHeight;
+ /**
+ * Listener who define the active leaf and set the tooltip text.
+ *
+ * @author Laurent Dutheil
+ */
+ protected class HandleMouseMotion extends MouseMotionAdapter {
- if (divWidth >= divHeight) {
- dest.setHeight(Math.round(destHeight / divWidth));
- dest.setWidth(rootWidth);
- } else {
- dest.setHeight(rootHeight);
- dest.setWidth(Math.round(destWidth / divHeight));
+ @Override
+ public void mouseMoved(final MouseEvent e) {
+ if (getDisplayedRoot().children().hasMoreElements()) {
+ final TreeMapNode t = getDisplayedRoot().getActiveLeaf(e.getX(), e.getY());
+ if (t != null && !t.equals(getActiveLeaf())) {
+ setActiveLeaf(t);
+ repaint();
+ }
+ if (t != null) {
+ setToolTipText(t.getLabel() + " " + t.getValue().getValue());
+ } else {
+ setToolTipText(null);
+ }
+ }
}
-
- } else {
- dest.setHeight(rootHeight);
- dest.setWidth(rootWidth);
- }
}
/**
- * undo the zoom.
+ * Class who zoom and unzoom the JTreeMap.
+ *
+ * @author Laurent Dutheil
*/
- public void undo() {
- if (!this.enable) {
- JTreeMap.this.setDisplayedRoot(JTreeMap.this.getRoot());
- this.enable = true;
- }
+ private class Zoom implements Serializable {
+ /**
+ *
+ */
+ private static final long serialVersionUID = 6708828099608367996L;
+
+ private boolean enable;
+
+ private boolean keepProportion = false;
+
+ /**
+ * Constructor
+ */
+ public Zoom() {
+ this.enable = true;
+ }
+
+ /**
+ * Execute the zoom.
+ *
+ * @param dest
+ * TreeMapNode where you want to zoom
+ */
+ public void execute(final TreeMapNode dest) {
+ if (this.enable) {
+ JTreeMap.this.setActiveLeaf(null);
+
+ setNewDimension(dest);
+
+ JTreeMap.this.setDisplayedRoot(dest);
+
+ this.enable = false;
+ }
+ }
+
+ /**
+ * @return Returns the keepProportion.
+ */
+ public boolean isKeepProportion() {
+ return this.keepProportion;
+ }
+
+ /**
+ * @param keepProportion
+ * The keepProportion to set.
+ */
+ public void setKeepProportion(final boolean keepProportion) {
+ this.keepProportion = keepProportion;
+ }
+
+ /**
+ * set the new dimensions of the dest root
+ *
+ * @param dest
+ * the root to dimension
+ */
+ protected void setNewDimension(final TreeMapNode dest) {
+ dest.setX(JTreeMap.this.getRoot().getX());
+ dest.setY(JTreeMap.this.getRoot().getY());
+
+ final int rootWidth = JTreeMap.this.getRoot().getWidth();
+ final int rootHeight = JTreeMap.this.getRoot().getHeight();
+
+ if (isKeepProportion()) {
+ final int destHeight = dest.getHeight();
+ final int destWidth = dest.getWidth();
+ final float divWidth = (float) destWidth / (float) rootWidth;
+ final float divHeight = (float) destHeight / (float) rootHeight;
+
+ if (divWidth >= divHeight) {
+ dest.setHeight(Math.round(destHeight / divWidth));
+ dest.setWidth(rootWidth);
+ } else {
+ dest.setHeight(rootHeight);
+ dest.setWidth(Math.round(destWidth / divHeight));
+ }
+
+ } else {
+ dest.setHeight(rootHeight);
+ dest.setWidth(rootWidth);
+ }
+ }
+
+ /**
+ * undo the zoom.
+ */
+ public void undo() {
+ if (!this.enable) {
+ JTreeMap.this.setDisplayedRoot(JTreeMap.this.getRoot());
+ this.enable = true;
+ }
+ }
+
}
- }
-
}
Modified: trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/SplitByNumber.java
===================================================================
--- trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/SplitByNumber.java 2006-10-24 10:28:55 UTC (rev 64)
+++ trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/SplitByNumber.java 2006-10-24 11:38:30 UTC (rev 65)
@@ -4,38 +4,43 @@
/**
* Strategy who split the elements in 2 groups of same cardinal.
- *
+ *
* @author Laurent DUTHEIL
*/
public class SplitByNumber extends SplitStrategy {
- @Override
- public void splitElements(Vector<TreeMapNode> v, Vector<TreeMapNode> v1, Vector<TreeMapNode> v2) {
- int size = v.size();
- int middle = size / 2;
- int index = 0;
- //we add first elements to v1
- for (; index < middle; index++) {
- v1.addElement(v.elementAt(index));
+ /**
+ *
+ */
+ private static final long serialVersionUID = -6484279333222332702L;
+
+ @Override
+ public void splitElements(final Vector<TreeMapNode> v, final Vector<TreeMapNode> v1, final Vector<TreeMapNode> v2) {
+ final int size = v.size();
+ final int middle = size / 2;
+ int index = 0;
+ // we add first elements to v1
+ for (; index < middle; index++) {
+ v1.addElement(v.elementAt(index));
+ }
+ // we add last elements to v2
+ for (; index < size; index++) {
+ v2.addElement(v.elementAt(index));
+ }
}
- //we add last elements to v2
- for (; index < size; index++) {
- v2.addElement(v.elementAt(index));
- }
- }
- @Override
- public double sumWeight(Vector<TreeMapNode> v) {
- //all the elements must have the same weight
- double weight = 0.0;
- for (TreeMapNode node : v) {
- if (node.isLeaf()) {
- weight += 1;
- } else {
- weight += this.sumWeight(node.getChildren());
- }
+ @Override
+ public double sumWeight(final Vector<TreeMapNode> v) {
+ // all the elements must have the same weight
+ double weight = 0.0;
+ for (final TreeMapNode node : v) {
+ if (node.isLeaf()) {
+ weight += 1;
+ } else {
+ weight += this.sumWeight(node.getChildren());
+ }
+ }
+ return weight;
}
- return weight;
- }
}
Modified: trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/SplitBySlice.java
===================================================================
--- trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/SplitBySlice.java 2006-10-24 10:28:55 UTC (rev 64)
+++ trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/SplitBySlice.java 2006-10-24 11:38:30 UTC (rev 65)
@@ -7,105 +7,113 @@
/**
* Split the treemap by slice
- *
+ *
* @author Laurent DUTHEIL
*/
public class SplitBySlice extends SplitStrategy {
- /**
- * Calculate the dimension of the elements of the Vector.
- *
- * @param x0 x-coordinate
- * @param y0 y-coordinate
- * @param w0 width
- * @param h0 height
- * @param v elements to split in the dimensions before
- * @param sumWeight sum of the weights
- */
- public static void splitInSlice(int x0, int y0, int w0, int h0,
- Vector<TreeMapNode> v, double sumWeight) {
- int offset = 0;
- boolean vertical = h0 > w0;
+ /**
+ *
+ */
+ private static final long serialVersionUID = 8484486418097321160L;
- for (TreeMapNode node : v) {
- if (vertical) {
- node.setX(x0);
- node.setWidth(w0);
- node.setY(y0 + offset);
- node.setHeight((int) Math.round(h0 * node.getWeight() / sumWeight));
- offset = offset + node.getHeight();
- } else {
- node.setX(x0 + offset);
- node.setWidth((int) Math.round(w0 * node.getWeight() / sumWeight));
- node.setY(y0);
- node.setHeight(h0);
- offset = offset + node.getWidth();
- }
- }
+ /**
+ * Calculate the dimension of the elements of the Vector.
+ *
+ * @param x0
+ * x-coordinate
+ * @param y0
+ * y-coordinate
+ * @param w0
+ * width
+ * @param h0
+ * height
+ * @param v
+ * elements to split in the dimensions before
+ * @param sumWeight
+ * sum of the weights
+ */
+ public static void splitInSlice(final int x0, final int y0, final int w0, final int h0, final Vector<TreeMapNode> v,
+ final double sumWeight) {
+ int offset = 0;
+ boolean vertical = h0 > w0;
- // Because of the Math.round(), we adjust the last element to fit the
- // correctly the JTreeMap
- if (!v.isEmpty()) {
- TreeMapNode node = v.lastElement();
- if (vertical && h0 != offset) {
- node.setHeight(node.getHeight() - offset + h0);
- } else if (!vertical && w0 != offset) {
- node.setWidth(node.getWidth() - offset + w0);
- }
+ for (final TreeMapNode node : v) {
+ if (vertical) {
+ node.setX(x0);
+ node.setWidth(w0);
+ node.setY(y0 + offset);
+ node.setHeight((int) Math.round(h0 * node.getWeight() / sumWeight));
+ offset = offset + node.getHeight();
+ } else {
+ node.setX(x0 + offset);
+ node.setWidth((int) Math.round(w0 * node.getWeight() / sumWeight));
+ node.setY(y0);
+ node.setHeight(h0);
+ offset = offset + node.getWidth();
+ }
+ }
+
+ // Because of the Math.round(), we adjust the last element to fit the
+ // correctly the JTreeMap
+ if (!v.isEmpty()) {
+ final TreeMapNode node = v.lastElement();
+ if (vertical && h0 != offset) {
+ node.setHeight(node.getHeight() - offset + h0);
+ } else if (!vertical && w0 != offset) {
+ node.setWidth(node.getWidth() - offset + w0);
+ }
+ }
}
- }
- /*
- * (non-Javadoc)
- *
- * @see net.sf.jtreemap.swing.SplitStrategy#splitElements(java.util.Vector,
- * java.util.Vector, java.util.Vector)
- */
- @Override
- public void splitElements(Vector<TreeMapNode> v, Vector<TreeMapNode> v1,
- Vector<TreeMapNode> v2) {
- // ignore
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sf.jtreemap.swing.SplitStrategy#splitElements(java.util.Vector,
+ * java.util.Vector, java.util.Vector)
+ */
+ @Override
+ public void splitElements(final Vector<TreeMapNode> v, final Vector<TreeMapNode> v1, final Vector<TreeMapNode> v2) {
+ // ignore
- }
+ }
- /*
- * (non-Javadoc)
- *
- * @see net.sf.jtreemap.swing.SplitStrategy#calculatePositionsRec(int, int,
- * int, int, double, java.util.Vector)
- */
- @Override
- protected void calculatePositionsRec(int x0, int y0, int w0, int h0,
- double weight0, Vector<TreeMapNode> v) {
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.sf.jtreemap.swing.SplitStrategy#calculatePositionsRec(int, int,
+ * int, int, double, java.util.Vector)
+ */
+ @Override
+ protected void calculatePositionsRec(final int x0, final int y0, final int w0, final int h0, final double weight0,
+ final Vector<TreeMapNode> v) {
- SplitBySlice.splitInSlice(x0, y0, w0, h0, v, weight0);
+ SplitBySlice.splitInSlice(x0, y0, w0, h0, v, weight0);
- for (TreeMapNode node : v) {
- if (node.isLeaf()) {
- node.setX(node.getX() + TreeMapNode.getBorder());
- node.setY(node.getY() + TreeMapNode.getBorder());
- node.setHeight(node.getHeight() - TreeMapNode.getBorder());
- node.setWidth(node.getWidth() - TreeMapNode.getBorder());
- } else {
- // if this is not a leaf, calculation for the children
- if (TreeMapNode.getBorder() > 1) {
- TreeMapNode.setBorder(TreeMapNode.getBorder() - 2);
- calculatePositionsRec(node.getX() + 2, node.getY() + 2, node
- .getWidth() - 2, node.getHeight() - 2, node.getWeight(), node
- .getChildren());
- TreeMapNode.setBorder(TreeMapNode.getBorder() + 2);
- } else if (TreeMapNode.getBorder() == 1) {
- TreeMapNode.setBorder(0);
- calculatePositionsRec(node.getX() + 1, node.getY() + 1, node
- .getWidth() - 1, node.getHeight() - 1, node.getWeight(), node
- .getChildren());
- TreeMapNode.setBorder(1);
- } else {
- calculatePositionsRec(node.getX(), node.getY(), node.getWidth(), node
- .getHeight(), node.getWeight(), node.getChildren());
+ for (final TreeMapNode node : v) {
+ if (node.isLeaf()) {
+ node.setX(node.getX() + TreeMapNode.getBorder());
+ node.setY(node.getY() + TreeMapNode.getBorder());
+ node.setHeight(node.getHeight() - TreeMapNode.getBorder());
+ node.setWidth(node.getWidth() - TreeMapNode.getBorder());
+ } else {
+ // if this is not a leaf, calculation for the children
+ if (TreeMapNode.getBorder() > 1) {
+ TreeMapNode.setBorder(TreeMapNode.getBorder() - 2);
+ calculatePositionsRec(node.getX() + 2, node.getY() + 2, node.getWidth() - 2, node.getHeight() - 2, node
+ .getWeight(), node.getChildren());
+ TreeMapNode.setBorder(TreeMapNode.getBorder() + 2);
+ } else if (TreeMapNode.getBorder() == 1) {
+ TreeMapNode.setBorder(0);
+ calculatePositionsRec(node.getX() + 1, node.getY() + 1, node.getWidth() - 1, node.getHeight() - 1, node
+ .getWeight(), node.getChildren());
+ TreeMapNode.setBorder(1);
+ } else {
+ calculatePositionsRec(node.getX(), node.getY(), node.getWidth(), node.getHeight(), node.getWeight(), node
+ .getChildren());
+ }
+ }
}
- }
+
}
-
- }
}
Modified: trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/SplitBySortedWeight.java
===================================================================
--- trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/SplitBySortedWeight.java 2006-10-24 10:28:55 UTC (rev 64)
+++ trunk/JTreeMap/src/main/java/net/sf/jtreemap/swing/SplitBySortedWeight.java 2006-10-24 11:38:30 UTC (rev 65)
@@ -11,51 +11,60 @@
* <p>
* The heaviest elements are on the top left of the JTreeMap. The lightest
* elements are on the bottom right of the JTreeMap
- *
+ *
* @author Laurent Dutheil
*/
public class SplitBySortedWeight extends SplitStrategy {
- @Override
- public void splitElements(Vector<TreeMapNode> v, Vector<TreeMapNode> v1, Vector<TreeMapNode> v2) {
- Vector<TreeMapNode> vClone = new Vector<TreeMapNode>(v);
- double memWeight = 0.0;
- double sumWeight = sumWeight(v);
- double elemWeight = 0.0;
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1600419780258843122L;
- sortVector(vClone);
+ @Override
+ public void splitElements(final Vector<TreeMapNode> v, final Vector<TreeMapNode> v1, final Vector<TreeMapNode> v2) {
+ final Vector<TreeMapNode> vClone = new Vector<TreeMapNode>(v);
+ double memWeight = 0.0;
+ final double sumWeight = sumWeight(v);
+ double elemWeight = 0.0;
- for (Iterator<TreeMapNode> i = vClone.iterator(); i.hasNext();) {
- TreeMapNode tmn = i.next();
- elemWeight = tmn.getWeight();
- // if adding the current element pass the middle of total weight
- if (memWeight + elemWeight >= sumWeight / 2) {
- // we look at the finest split (the nearest of the middle of weight)
- if (((sumWeight / 2) - memWeight) > ((memWeight + elemWeight) - (sumWeight / 2))) {
- // if it is after the add, we add the element to the first Vector
- memWeight += elemWeight;
- v1.addElement(tmn);
- } else {
- // we must have at least 1 element in the first vector
- if (v1.isEmpty()) {
- v1.addElement(tmn);
- } else {
- // if it is before the add, we add the element to the second Vector
- v2.addElement(tmn);
- }
+ sortVector(vClone);
+
+ for (final Iterator<TreeMapNode> i = vClone.iterator(); i.hasNext();) {
+ TreeMapNode tmn = i.next();
+ elemWeight = tmn.getWeight();
+ // if adding the current element pass the middle of total weight
+ if (memWeight + elemWeight >= sumWeight / 2) {
+ // we look at the finest split (the nearest of the middle of
+ // weight)
+ if (((sumWeight / 2) - memWeight) > ((memWeight + elemWeight) - (sumWeight / 2))) {
+ ...
[truncated message content] |