added selection size

This commit is contained in:
bancika
2011-01-08 15:35:33 +00:00
parent a4309a169c
commit ad43ecd0ed
10 changed files with 139 additions and 16 deletions
@@ -5,6 +5,6 @@
</entry>
<entry>
<string>metric</string>
<boolean>true</boolean>
<boolean>false</boolean>
</entry>
</map>
@@ -1,10 +1,10 @@
package org.diylc.common;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import org.diylc.core.Project;
/**
* Enumerates all possible events between {@link IPlugInPort} and
* {@link IPlugIn}. Some events overlap so be careful which ones you subscribe
@@ -37,6 +37,12 @@ public enum EventType {
* {@link ComponentSelection} is attached as a parameter.
*/
SELECTION_CHANGED,
/**
* Called when selection size changes, together with a {@link Point2D}
* object containing selection size (x = width, y = height). Size is
* expressed in the default measure (cm for metric, inches for imperial).
*/
SELECTION_SIZE_CHANGED,
/**
* Called when display needs to be repainted. No parameters are passed.
*/
@@ -51,5 +57,12 @@ public enum EventType {
* {@link Project} are passed as parameters, one before and one after the
* change. The third parameter is a string containing change description.
*/
PROJECT_MODIFIED, AVAILABLE_CTRL_POINTS_CHANGED, SELECTED_CTRL_POINTS_CHANGED, ;
PROJECT_MODIFIED,
/**
* Called when control points under the cursor are changed. A single
* parameter is passed, an instance of
* <code>Map<IDIYComponent<?>, Set<Integer>></code> containing all the
* components and indices of their control points that are under the cursor.
*/
AVAILABLE_CTRL_POINTS_CHANGED, SELECTED_CTRL_POINTS_CHANGED, ;
}
@@ -288,4 +288,11 @@ public interface IPlugInPort {
* instantiate new component.
*/
void setNewComponentSlot(ComponentType componentType);
/**
* Changes default size notation, true for metric, false for imperial.
*
* @param isMetric
*/
void setMetric(boolean isMetric);
}
@@ -1,13 +1,11 @@
package org.diylc.core.measures;
import java.awt.Toolkit;
import org.diylc.utils.Constants;
public class Size extends AbstractMeasure<SizeUnit> {
private static final long serialVersionUID = 1L;
public static final int PIXELS_PER_INCH = Toolkit.getDefaultToolkit().getScreenResolution();
public Size() {
super();
// TODO Auto-generated constructor stub
@@ -20,7 +18,7 @@ public class Size extends AbstractMeasure<SizeUnit> {
public int convertToPixels() {
double factor = getUnit().getFactor() / SizeUnit.in.getFactor();
return (int) (factor * PIXELS_PER_INCH * getValue());
return (int) (factor * Constants.PIXELS_PER_INCH * getValue());
}
@Override
@@ -13,6 +13,7 @@ import org.diylc.common.EventType;
import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort;
import org.diylc.plugins.file.ProjectDrawingProvider;
import org.diylc.presenter.Presenter;
import com.diyfever.gui.miscutils.ConfigurationManager;
import com.diyfever.gui.ruler.IRulerListener;
@@ -22,7 +23,7 @@ public class CanvasPlugin implements IPlugIn {
private static final Logger LOG = Logger.getLogger(CanvasPlugin.class);
private static final String METRIC_KEY = "metric";
private RulerScrollPane scrollPane;
private CanvasPanel canvasPanel;
@@ -46,7 +47,7 @@ public class CanvasPlugin implements IPlugIn {
if (scrollPane == null) {
scrollPane = new RulerScrollPane(canvasPanel, new ProjectDrawingProvider(plugInPort));
Boolean metric = (Boolean) ConfigurationManager.getInstance().getConfigurationItem(
METRIC_KEY);
Presenter.METRIC_KEY);
if (metric == null) {
metric = true;
}
@@ -55,7 +56,7 @@ public class CanvasPlugin implements IPlugIn {
@Override
public void unitsChanged(boolean isMetric) {
ConfigurationManager.getInstance().setConfigurationItem(METRIC_KEY, isMetric);
plugInPort.setMetric(isMetric);
}
});
}
@@ -5,6 +5,9 @@ import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import java.text.DecimalFormat;
import java.text.Format;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
@@ -15,7 +18,10 @@ import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.diylc.common.BadPositionException;
import org.diylc.common.ComponentType;
@@ -24,8 +30,10 @@ import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort;
import org.diylc.core.IDIYComponent;
import org.diylc.presenter.ComparatorFactory;
import org.diylc.presenter.Presenter;
import com.diyfever.gui.MemoryBar;
import com.diyfever.gui.miscutils.ConfigurationManager;
import com.diyfever.gui.miscutils.PercentageListCellRenderer;
import com.diyfever.gui.update.UpdateLabel;
@@ -34,11 +42,13 @@ public class StatusBar extends JPanel implements IPlugIn {
private static final long serialVersionUID = 1L;
public static String UPDATE_URL = "http://www.diy-fever.com/update.xml";
private static final Format sizeFormat = new DecimalFormat("0.00");
private JComboBox zoomBox;
private UpdateLabel updateLabel;
private MemoryBar memoryPanel;
private JLabel statusLabel;
private JLabel sizeLabel;
private IPlugInPort plugInPort;
@@ -97,6 +107,16 @@ public class StatusBar extends JPanel implements IPlugIn {
return statusLabel;
}
public JLabel getSizeLabel() {
if (sizeLabel == null) {
sizeLabel = new JLabel();
sizeLabel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(
0, 1, 0, 1, UIManager.getColor("Separator.shadow")), BorderFactory
.createEmptyBorder(0, 4, 0, 4)));
}
return sizeLabel;
}
private void layoutComponents() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
@@ -116,16 +136,25 @@ public class StatusBar extends JPanel implements IPlugIn {
gbc.gridx = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0;
add(zoomPanel, gbc);
add(new JSeparator(SwingConstants.HORIZONTAL), gbc);
gbc.gridx = 2;
add(getUpdateLabel(), gbc);
add(getSizeLabel(), gbc);
gbc.gridx = 3;
add(new JSeparator(SwingConstants.HORIZONTAL), gbc);
gbc.gridx = 4;
add(zoomPanel, gbc);
gbc.gridx = 5;
add(getUpdateLabel(), gbc);
gbc.gridx = 6;
gbc.fill = GridBagConstraints.NONE;
add(getMemoryPanel(), gbc);
gbc.gridx = 4;
gbc.gridx = 7;
add(new JPanel(), gbc);
}
@@ -148,7 +177,7 @@ public class StatusBar extends JPanel implements IPlugIn {
@Override
public EnumSet<EventType> getSubscribedEventTypes() {
return EnumSet.of(EventType.ZOOM_CHANGED, EventType.SLOT_CHANGED,
EventType.AVAILABLE_CTRL_POINTS_CHANGED);
EventType.AVAILABLE_CTRL_POINTS_CHANGED, EventType.SELECTION_SIZE_CHANGED);
}
@SuppressWarnings("unchecked")
@@ -160,6 +189,18 @@ public class StatusBar extends JPanel implements IPlugIn {
getZoomBox().setSelectedItem(params[0]);
}
break;
case SELECTION_SIZE_CHANGED:
Point2D size = (Point2D) params[0];
boolean isMetric = (Boolean) ConfigurationManager.getInstance().getConfigurationItem(
Presenter.METRIC_KEY);
if (size == null) {
getSizeLabel().setText("Size: N/A");
} else {
getSizeLabel().setText(
"Size: " + sizeFormat.format(size.getX()) + " x "
+ sizeFormat.format(size.getY()) + (isMetric ? " cm" : " in"));
}
break;
case SLOT_CHANGED:
componentSlot = (ComponentType) params[0];
refreshStatusText();
@@ -416,6 +416,12 @@ class G2DWrapper extends Graphics2D {
canvasGraphics.fillPolygon(points, points2, points3);
appendShape(new Polygon(points, points2, points3));
}
@Override
public void drawRect(int x, int y, int width, int height) {
canvasGraphics.drawRect(x, y, width, height);
appendShape(new Rectangle(x, y, width, height));
}
@Override
public void fillRect(int x, int y, int width, int height) {
@@ -42,6 +42,7 @@ import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.Project;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.measures.SizeUnit;
import org.diylc.gui.IView;
import org.diylc.utils.Constants;
@@ -63,6 +64,8 @@ public class Presenter implements IPlugInPort {
public static final VersionNumber CURRENT_VERSION = new VersionNumber(0, 0, 0);
public static final String DEFAULTS_KEY = "defaults";
public static final String METRIC_KEY = "metric";
public static final int CONTROL_POINT_SENSITIVITY = 4;
public static final int ICON_SIZE = 32;
public static boolean ENABLE_ANTIALIASING = true;
@@ -443,6 +446,8 @@ public class Presenter implements IPlugInPort {
}
}
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
calculateSelectionDimension());
messageDispatcher.dispatchMessage(EventType.REPAINT);
}
}
@@ -520,11 +525,15 @@ public class Presenter implements IPlugInPort {
selectedComponents.clear();
selectedComponents.addAll(controlPointMap.keySet());
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
calculateSelectionDimension());
messageDispatcher.dispatchMessage(EventType.REPAINT);
} else if (components.isEmpty()) {
// If there are no components are under the cursor, reset selection.
selectedComponents.clear();
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
calculateSelectionDimension());
messageDispatcher.dispatchMessage(EventType.REPAINT);
} else {
// Take the last component, i.e. the top order component.
@@ -535,6 +544,8 @@ public class Presenter implements IPlugInPort {
selectedComponents.clear();
selectedComponents.add(component);
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
calculateSelectionDimension());
messageDispatcher.dispatchMessage(EventType.REPAINT);
}
// If there aren't any control points, try to add all the selected
@@ -594,6 +605,8 @@ public class Presenter implements IPlugInPort {
if (repaint) {
messageDispatcher.dispatchMessage(EventType.REPAINT);
}
messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
calculateSelectionDimension());
return true;
}
@@ -619,6 +632,8 @@ public class Presenter implements IPlugInPort {
}
selectionRect = null;
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
calculateSelectionDimension());
}
// There is selection, so we need to finalize the drag&drop
// operation.
@@ -686,6 +701,41 @@ public class Presenter implements IPlugInPort {
}
}
@Override
public void setMetric(boolean isMetric) {
ConfigurationManager.getInstance().setConfigurationItem(Presenter.METRIC_KEY, isMetric);
messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
calculateSelectionDimension());
}
private Point2D calculateSelectionDimension() {
if (selectedComponents.isEmpty()) {
return null;
}
boolean isMetric = (Boolean) ConfigurationManager.getInstance().getConfigurationItem(
METRIC_KEY);
Area area = new Area();
for (IDIYComponent<?> component : selectedComponents) {
Area componentArea = componentAreaMap.get(component);
if (componentArea != null) {
LOG.warn("Area added!" + componentArea.getBounds());
area.add(componentArea);
} else {
LOG.warn("No area!");
}
}
double width = area.getBounds2D().getWidth();
double height = area.getBounds2D().getHeight();
width /= Constants.PIXELS_PER_INCH;
height /= Constants.PIXELS_PER_INCH;
if (isMetric) {
width *= SizeUnit.in.getFactor() / SizeUnit.cm.getFactor();
height *= SizeUnit.in.getFactor() / SizeUnit.cm.getFactor();
}
Point2D dimension = new Point2D.Double(width, height);
return dimension;
}
/**
* Adds a component to the project taking z-order into account.
*
@@ -806,7 +856,9 @@ public class Presenter implements IPlugInPort {
: componentType.getName()));
this.componentSlot = componentType;
selectedComponents.clear();
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED);
messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, selectedComponents);
messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
calculateSelectionDimension());
messageDispatcher.dispatchMessage(EventType.SLOT_CHANGED, componentSlot);
}
@@ -6,9 +6,14 @@ import java.awt.Font;
import java.awt.Stroke;
import java.awt.Toolkit;
import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
public class Constants {
public static final int GRID = (int) (Toolkit.getDefaultToolkit().getScreenResolution() / 10f);
public static final int PIXELS_PER_INCH = Toolkit.getDefaultToolkit().getScreenResolution();
public static Size GRID_SIZE = new Size(0.1d, SizeUnit.in);
public static final int GRID = GRID_SIZE.convertToPixels();
public static Color CANVAS_COLOR = Color.white;
public static Color GRID_COLOR = new Color(240, 240, 240);