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. * Note: point coordinates are display based, i.e. scaled for zoom factor.
* *
* @param point * @param point
* @param button
* @param ctrlDown * @param ctrlDown
* @param shiftDown * @param shiftDown
* @param altDown * @param altDown
* @param clickCount * @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); int clickCount);
/** /**
@@ -47,6 +47,14 @@ public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor {
public static final int DND_TOGGLE_STICKY = 0x1; public static final int DND_TOGGLE_STICKY = 0x1;
public static final int DND_TOGGLE_SNAP = 0x40000000; 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 * Returns size of the canvas that takes project dimensions into account as
@@ -223,6 +231,11 @@ public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor {
* @param properties * @param properties
*/ */
void applyPropertiesToProject(List<PropertyWrapper> properties); 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 * Sets the new component slot. Specified component type will be used to
@@ -10,6 +10,7 @@ import org.apache.log4j.Logger;
import org.diylc.appframework.miscutils.ConfigurationManager; import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.common.ComponentType; import org.diylc.common.ComponentType;
import org.diylc.common.IPlugInPort; import org.diylc.common.IPlugInPort;
import org.diylc.common.Orientation;
import org.diylc.common.PropertyWrapper; import org.diylc.common.PropertyWrapper;
import org.diylc.core.IDIYComponent; import org.diylc.core.IDIYComponent;
import org.diylc.core.Project; import org.diylc.core.Project;
@@ -24,7 +25,8 @@ import com.rits.cloning.Cloner;
*/ */
public class InstantiationManager { 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; private static final int MAX_RECENT_COMPONENTS = 16;
@@ -55,8 +57,8 @@ public class InstantiationManager {
return potentialControlPoint; return potentialControlPoint;
} }
public void setComponentTypeSlot(ComponentType componentTypeSlot, Project currentProject) public void setComponentTypeSlot(ComponentType componentTypeSlot,
throws Exception { Project currentProject) throws Exception {
this.componentTypeSlot = componentTypeSlot; this.componentTypeSlot = componentTypeSlot;
if (componentTypeSlot == null) { if (componentTypeSlot == null) {
this.componentSlot = null; this.componentSlot = null;
@@ -66,8 +68,8 @@ public class InstantiationManager {
this.componentSlot = null; this.componentSlot = null;
break; break;
case SINGLE_CLICK: case SINGLE_CLICK:
this.componentSlot = instantiateComponent(componentTypeSlot, new Point(0, 0), this.componentSlot = instantiateComponent(componentTypeSlot,
currentProject); new Point(0, 0), currentProject);
break; break;
} }
} }
@@ -75,9 +77,11 @@ public class InstantiationManager {
this.potentialControlPoint = null; this.potentialControlPoint = null;
} }
public void instatiatePointByPoint(Point scaledPoint, Project currentProject) throws Exception { public void instatiatePointByPoint(Point scaledPoint, Project currentProject)
throws Exception {
firstControlPoint = scaledPoint; firstControlPoint = scaledPoint;
componentSlot = instantiateComponent(componentTypeSlot, firstControlPoint, currentProject); componentSlot = instantiateComponent(componentTypeSlot,
firstControlPoint, currentProject);
// Set the other control point to the same location, we'll // Set the other control point to the same location, we'll
// move it later when mouse moves. // move it later when mouse moves.
@@ -100,7 +104,8 @@ public class InstantiationManager {
return changeMade; return changeMade;
} }
public boolean updateSingleClick(Point scaledPoint, boolean snapToGrid, Size gridSpacing) { public boolean updateSingleClick(Point scaledPoint, boolean snapToGrid,
Size gridSpacing) {
if (potentialControlPoint == null) { if (potentialControlPoint == null) {
potentialControlPoint = new Point(0, 0); potentialControlPoint = new Point(0, 0);
} }
@@ -129,12 +134,14 @@ public class InstantiationManager {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public IDIYComponent<?> instantiateComponent(ComponentType componentType, Point point, public IDIYComponent<?> instantiateComponent(ComponentType componentType,
Project currentProject) throws Exception { Point point, Project currentProject) throws Exception {
LOG.info("Instatiating component of type: " + componentType.getInstanceClass().getName()); LOG.info("Instatiating component of type: "
+ componentType.getInstanceClass().getName());
// Instantiate the component. // Instantiate the component.
IDIYComponent<?> component = componentType.getInstanceClass().newInstance(); IDIYComponent<?> component = componentType.getInstanceClass()
.newInstance();
component.setName(createUniqueName(componentType, currentProject)); component.setName(createUniqueName(componentType, currentProject));
@@ -151,10 +158,12 @@ public class InstantiationManager {
fillWithDefaultProperties(component); fillWithDefaultProperties(component);
// Write to recent components // Write to recent components
List<String> recentComponentTypes = (List<String>) ConfigurationManager.getInstance() List<String> recentComponentTypes = (List<String>) ConfigurationManager
.readObject(IPlugInPort.RECENT_COMPONENTS_KEY, new ArrayList<ComponentType>()); .getInstance().readObject(IPlugInPort.RECENT_COMPONENTS_KEY,
new ArrayList<ComponentType>());
String className = componentType.getInstanceClass().getName(); 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. // Remove if it's already somewhere in the list.
recentComponentTypes.remove(className); recentComponentTypes.remove(className);
// Add to the end of the list. // Add to the end of the list.
@@ -163,14 +172,15 @@ public class InstantiationManager {
if (recentComponentTypes.size() > MAX_RECENT_COMPONENTS) { if (recentComponentTypes.size() > MAX_RECENT_COMPONENTS) {
recentComponentTypes.remove(recentComponentTypes.size() - 1); recentComponentTypes.remove(recentComponentTypes.size() - 1);
} }
ConfigurationManager.getInstance().writeValue(IPlugInPort.RECENT_COMPONENTS_KEY, ConfigurationManager.getInstance().writeValue(
recentComponentTypes); IPlugInPort.RECENT_COMPONENTS_KEY, recentComponentTypes);
} }
return component; return component;
} }
public String createUniqueName(ComponentType componentType, Project currentProject) { public String createUniqueName(ComponentType componentType,
Project currentProject) {
int i = 0; int i = 0;
boolean exists = true; boolean exists = true;
List<IDIYComponent<?>> components = currentProject.getComponents(); List<IDIYComponent<?>> components = currentProject.getComponents();
@@ -203,17 +213,19 @@ public class InstantiationManager {
* @throws NoSuchMethodException * @throws NoSuchMethodException
* @throws SecurityException * @throws SecurityException
*/ */
public void fillWithDefaultProperties(Object object) throws IllegalArgumentException, public void fillWithDefaultProperties(Object object)
IllegalAccessException, InvocationTargetException, SecurityException, throws IllegalArgumentException, IllegalAccessException,
NoSuchMethodException { InvocationTargetException, SecurityException, NoSuchMethodException {
// Extract properties. // Extract properties.
List<PropertyWrapper> properties = ComponentProcessor.getInstance().extractProperties( List<PropertyWrapper> properties = ComponentProcessor.getInstance()
object.getClass()); .extractProperties(object.getClass());
// Override with default values if available. // Override with default values if available.
for (PropertyWrapper property : properties) { for (PropertyWrapper property : properties) {
Object defaultValue = ConfigurationManager.getInstance().readObject( Object defaultValue = ConfigurationManager.getInstance()
Presenter.DEFAULTS_KEY_PREFIX + object.getClass().getName() + ":" .readObject(
+ property.getName(), null); Presenter.DEFAULTS_KEY_PREFIX
+ object.getClass().getName() + ":"
+ property.getName(), null);
if (defaultValue != null) { if (defaultValue != null) {
property.setValue(cloner.deepClone(defaultValue)); property.setValue(cloner.deepClone(defaultValue));
property.writeTo(object); 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 @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) { int clickCount) {
LOG.debug(String 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); Point scaledPoint = scalePoint(point);
if (clickCount >= 2) { if (clickCount >= 2) {
editSelection(); editSelection();
} else { } else {
if (instantiationManager.getComponentTypeSlot() != null) { 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. // Keep the reference to component type for later.
ComponentType componentTypeSlot = instantiationManager.getComponentTypeSlot(); ComponentType componentTypeSlot = instantiationManager.getComponentTypeSlot();
Project oldProject = cloner.deepClone(currentProject); Project oldProject = cloner.deepClone(currentProject);
@@ -379,8 +385,7 @@ public class Presenter implements IPlugInPort {
if (isSnapToGrid()) { if (isSnapToGrid()) {
CalcUtils.snapPointToGrid(scaledPoint, currentProject.getGridSpacing()); CalcUtils.snapPointToGrid(scaledPoint, currentProject.getGridSpacing());
} }
IDIYComponent<?> component = instantiationManager.instantiateComponent( IDIYComponent<?> component = instantiationManager.getComponentSlot();
componentTypeSlot, scaledPoint, currentProject);
addComponent(component, componentTypeSlot, true); addComponent(component, componentTypeSlot, true);
// Select the new component // Select the new component
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED, // messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
@@ -613,7 +618,7 @@ public class Presenter implements IPlugInPort {
LOG.debug(String.format("dragStarted(%s, %s)", point, dragAction)); LOG.debug(String.format("dragStarted(%s, %s)", point, dragAction));
if (instantiationManager.getComponentTypeSlot() != null) { if (instantiationManager.getComponentTypeSlot() != null) {
LOG.debug("Cannot start drag because a new component is being created."); 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, dragAction == DnDConstants.ACTION_LINK, dragAction == DnDConstants.ACTION_MOVE,
1); 1);
return; return;
@@ -1445,6 +1450,11 @@ public class Presenter implements IPlugInPort {
drawingManager.fireZoomChanged(); drawingManager.fireZoomChanged();
} }
} }
@Override
public ComponentType getNewComponentTypeSlot() {
return instantiationManager.getComponentTypeSlot();
}
@Override @Override
public void setNewComponentTypeSlot(ComponentType componentType) { public void setNewComponentTypeSlot(ComponentType componentType) {