Right click rotates component while laying out on the canvas

This commit is contained in:
bancika
2012-08-16 10:24:00 +00:00
parent 21e46b3835
commit c38bd87a7d
4 changed files with 99 additions and 32 deletions
@@ -10,12 +10,13 @@ public interface IMouseProcessor {
* Note: point coordinates are display based, i.e. scaled for zoom factor.
*
* @param point
* @param button
* @param ctrlDown
* @param shiftDown
* @param altDown
* @param clickCount
*/
void mouseClicked(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown,
void mouseClicked(Point point, int button, boolean ctrlDown, boolean shiftDown, boolean altDown,
int clickCount);
/**
@@ -48,6 +48,14 @@ public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor {
public static final int DND_TOGGLE_STICKY = 0x1;
public static final int DND_TOGGLE_SNAP = 0x40000000;
/**
* Mouse button constants.
*/
public static final int NOBUTTON = 0;
public static final int BUTTON1 = 1;
public static final int BUTTON2 = 2;
public static final int BUTTON3 = 3;
/**
* Returns size of the canvas that takes project dimensions into account as
* well as zoom level. Each dimension is calculated as the product of the
@@ -224,6 +232,11 @@ public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor {
*/
void applyPropertiesToProject(List<PropertyWrapper> properties);
/**
* Gets the current new component slot.
*/
ComponentType getNewComponentTypeSlot();
/**
* Sets the new component slot. Specified component type will be used to
* instantiate new component.
@@ -10,6 +10,7 @@ import org.apache.log4j.Logger;
import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.common.ComponentType;
import org.diylc.common.IPlugInPort;
import org.diylc.common.Orientation;
import org.diylc.common.PropertyWrapper;
import org.diylc.core.IDIYComponent;
import org.diylc.core.Project;
@@ -24,7 +25,8 @@ import com.rits.cloning.Cloner;
*/
public class InstantiationManager {
private static final Logger LOG = Logger.getLogger(InstantiationManager.class);
private static final Logger LOG = Logger
.getLogger(InstantiationManager.class);
private static final int MAX_RECENT_COMPONENTS = 16;
@@ -55,8 +57,8 @@ public class InstantiationManager {
return potentialControlPoint;
}
public void setComponentTypeSlot(ComponentType componentTypeSlot, Project currentProject)
throws Exception {
public void setComponentTypeSlot(ComponentType componentTypeSlot,
Project currentProject) throws Exception {
this.componentTypeSlot = componentTypeSlot;
if (componentTypeSlot == null) {
this.componentSlot = null;
@@ -66,8 +68,8 @@ public class InstantiationManager {
this.componentSlot = null;
break;
case SINGLE_CLICK:
this.componentSlot = instantiateComponent(componentTypeSlot, new Point(0, 0),
currentProject);
this.componentSlot = instantiateComponent(componentTypeSlot,
new Point(0, 0), currentProject);
break;
}
}
@@ -75,9 +77,11 @@ public class InstantiationManager {
this.potentialControlPoint = null;
}
public void instatiatePointByPoint(Point scaledPoint, Project currentProject) throws Exception {
public void instatiatePointByPoint(Point scaledPoint, Project currentProject)
throws Exception {
firstControlPoint = scaledPoint;
componentSlot = instantiateComponent(componentTypeSlot, firstControlPoint, currentProject);
componentSlot = instantiateComponent(componentTypeSlot,
firstControlPoint, currentProject);
// Set the other control point to the same location, we'll
// move it later when mouse moves.
@@ -100,7 +104,8 @@ public class InstantiationManager {
return changeMade;
}
public boolean updateSingleClick(Point scaledPoint, boolean snapToGrid, Size gridSpacing) {
public boolean updateSingleClick(Point scaledPoint, boolean snapToGrid,
Size gridSpacing) {
if (potentialControlPoint == null) {
potentialControlPoint = new Point(0, 0);
}
@@ -129,12 +134,14 @@ public class InstantiationManager {
}
@SuppressWarnings("unchecked")
public IDIYComponent<?> instantiateComponent(ComponentType componentType, Point point,
Project currentProject) throws Exception {
LOG.info("Instatiating component of type: " + componentType.getInstanceClass().getName());
public IDIYComponent<?> instantiateComponent(ComponentType componentType,
Point point, Project currentProject) throws Exception {
LOG.info("Instatiating component of type: "
+ componentType.getInstanceClass().getName());
// Instantiate the component.
IDIYComponent<?> component = componentType.getInstanceClass().newInstance();
IDIYComponent<?> component = componentType.getInstanceClass()
.newInstance();
component.setName(createUniqueName(componentType, currentProject));
@@ -151,10 +158,12 @@ public class InstantiationManager {
fillWithDefaultProperties(component);
// Write to recent components
List<String> recentComponentTypes = (List<String>) ConfigurationManager.getInstance()
.readObject(IPlugInPort.RECENT_COMPONENTS_KEY, new ArrayList<ComponentType>());
List<String> recentComponentTypes = (List<String>) ConfigurationManager
.getInstance().readObject(IPlugInPort.RECENT_COMPONENTS_KEY,
new ArrayList<ComponentType>());
String className = componentType.getInstanceClass().getName();
if (recentComponentTypes.size() == 0 || !recentComponentTypes.get(0).equals(className)) {
if (recentComponentTypes.size() == 0
|| !recentComponentTypes.get(0).equals(className)) {
// Remove if it's already somewhere in the list.
recentComponentTypes.remove(className);
// Add to the end of the list.
@@ -163,14 +172,15 @@ public class InstantiationManager {
if (recentComponentTypes.size() > MAX_RECENT_COMPONENTS) {
recentComponentTypes.remove(recentComponentTypes.size() - 1);
}
ConfigurationManager.getInstance().writeValue(IPlugInPort.RECENT_COMPONENTS_KEY,
recentComponentTypes);
ConfigurationManager.getInstance().writeValue(
IPlugInPort.RECENT_COMPONENTS_KEY, recentComponentTypes);
}
return component;
}
public String createUniqueName(ComponentType componentType, Project currentProject) {
public String createUniqueName(ComponentType componentType,
Project currentProject) {
int i = 0;
boolean exists = true;
List<IDIYComponent<?>> components = currentProject.getComponents();
@@ -203,17 +213,19 @@ public class InstantiationManager {
* @throws NoSuchMethodException
* @throws SecurityException
*/
public void fillWithDefaultProperties(Object object) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException, SecurityException,
NoSuchMethodException {
public void fillWithDefaultProperties(Object object)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException, SecurityException, NoSuchMethodException {
// Extract properties.
List<PropertyWrapper> properties = ComponentProcessor.getInstance().extractProperties(
object.getClass());
List<PropertyWrapper> properties = ComponentProcessor.getInstance()
.extractProperties(object.getClass());
// Override with default values if available.
for (PropertyWrapper property : properties) {
Object defaultValue = ConfigurationManager.getInstance().readObject(
Presenter.DEFAULTS_KEY_PREFIX + object.getClass().getName() + ":"
+ property.getName(), null);
Object defaultValue = ConfigurationManager.getInstance()
.readObject(
Presenter.DEFAULTS_KEY_PREFIX
+ object.getClass().getName() + ":"
+ property.getName(), null);
if (defaultValue != null) {
property.setValue(cloner.deepClone(defaultValue));
property.writeTo(object);
@@ -221,4 +233,35 @@ public class InstantiationManager {
}
}
public void tryToRotateComponentSlot() {
if (this.componentSlot == null) {
LOG.debug("Component slot is empty, cannot rotate");
return;
}
List<PropertyWrapper> properties = ComponentProcessor.getInstance()
.extractProperties(this.componentTypeSlot.getInstanceClass());
PropertyWrapper angleProperty = null;
for (PropertyWrapper propertyWrapper : properties) {
if (propertyWrapper.getType().getName().equals(
Orientation.class.getName())) {
angleProperty = propertyWrapper;
break;
}
}
if (angleProperty == null) {
LOG
.debug("Component in the slot does not have a property of type Orientation, cannot rotate");
return;
}
try {
angleProperty.readFrom(this.componentSlot);
Orientation value = (Orientation) angleProperty.getValue();
value.ordinal();
angleProperty.setValue(Orientation.values()[(value.ordinal() + 1)
% Orientation.values().length]);
angleProperty.writeTo(this.componentSlot);
} catch (Exception e) {
LOG.warn("Error trying to rotate the component", e);
}
}
}
@@ -361,15 +361,21 @@ public class Presenter implements IPlugInPort {
}
@Override
public void mouseClicked(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown,
public void mouseClicked(Point point, int button, boolean ctrlDown, boolean shiftDown, boolean altDown,
int clickCount) {
LOG.debug(String
.format("mouseClicked(%s, %s, %s, %s)", point, ctrlDown, shiftDown, altDown));
.format("mouseClicked(%s, %s, %s, %s, %s)", point, button, ctrlDown, shiftDown, altDown));
Point scaledPoint = scalePoint(point);
if (clickCount >= 2) {
editSelection();
} else {
if (instantiationManager.getComponentTypeSlot() != null) {
// Try to rotate the component on right click while creating.
if (button == IPlugInPort.BUTTON3) {
instantiationManager.tryToRotateComponentSlot();
messageDispatcher.dispatchMessage(EventType.REPAINT);
return;
}
// Keep the reference to component type for later.
ComponentType componentTypeSlot = instantiationManager.getComponentTypeSlot();
Project oldProject = cloner.deepClone(currentProject);
@@ -379,8 +385,7 @@ public class Presenter implements IPlugInPort {
if (isSnapToGrid()) {
CalcUtils.snapPointToGrid(scaledPoint, currentProject.getGridSpacing());
}
IDIYComponent<?> component = instantiationManager.instantiateComponent(
componentTypeSlot, scaledPoint, currentProject);
IDIYComponent<?> component = instantiationManager.getComponentSlot();
addComponent(component, componentTypeSlot, true);
// Select the new component
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
@@ -613,7 +618,7 @@ public class Presenter implements IPlugInPort {
LOG.debug(String.format("dragStarted(%s, %s)", point, dragAction));
if (instantiationManager.getComponentTypeSlot() != null) {
LOG.debug("Cannot start drag because a new component is being created.");
mouseClicked(point, dragAction == DnDConstants.ACTION_COPY,
mouseClicked(point, IPlugInPort.BUTTON1, dragAction == DnDConstants.ACTION_COPY,
dragAction == DnDConstants.ACTION_LINK, dragAction == DnDConstants.ACTION_MOVE,
1);
return;
@@ -1446,6 +1451,11 @@ public class Presenter implements IPlugInPort {
}
}
@Override
public ComponentType getNewComponentTypeSlot() {
return instantiationManager.getComponentTypeSlot();
}
@Override
public void setNewComponentTypeSlot(ComponentType componentType) {
LOG.info(String.format("setNewComponentSlot(%s)", componentType == null ? null