Renumber selected components by X or Y axis

This commit is contained in:
bancika
2011-08-28 17:09:10 +00:00
parent a0ae53d53c
commit 6b7d1c4277
7 changed files with 136 additions and 11 deletions
@@ -100,7 +100,7 @@ public interface IPlugInPort {
* Saves the current project into the specified file. * Saves the current project into the specified file.
* *
* @param fileName * @param fileName
* @param isBackup * @param isBackup
*/ */
void saveProjectToFile(String fileName, boolean isBackup); void saveProjectToFile(String fileName, boolean isBackup);
@@ -171,7 +171,8 @@ public interface IPlugInPort {
* @param altDown * @param altDown
* @param clickCount * @param clickCount
*/ */
void mouseClicked(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown, int clickCount); void mouseClicked(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown,
int clickCount);
/** /**
* Notifies the presenter that mouse is moved. * Notifies the presenter that mouse is moved.
@@ -191,7 +192,7 @@ public interface IPlugInPort {
* @return * @return
*/ */
List<IDIYComponent<?>> getSelectedComponents(); List<IDIYComponent<?>> getSelectedComponents();
/** /**
* Updates the selection with the specified list of component. Also, updates * Updates the selection with the specified list of component. Also, updates
* control point map with all components that are stuck to the newly * control point map with all components that are stuck to the newly
@@ -285,9 +286,8 @@ public interface IPlugInPort {
*/ */
List<PropertyWrapper> getMutualSelectionProperties(); List<PropertyWrapper> getMutualSelectionProperties();
void editSelection(); void editSelection();
/** /**
* @return a list of editable properties of the current project. * @return a list of editable properties of the current project.
*/ */
@@ -364,4 +364,11 @@ public interface IPlugInPort {
* @param theme * @param theme
*/ */
void setSelectedTheme(Theme theme); void setSelectedTheme(Theme theme);
/**
* Renumbers all the selected components.
*
* @param xAxisFirst
*/
void renumberSelectedComponents(boolean xAxisFirst);
} }
@@ -31,7 +31,7 @@ public enum IconLoader {
"donate.png"), Bug("bug.png"), IconLarge("icon_large.png"), IconMedium( "donate.png"), Bug("bug.png"), IconLarge("icon_large.png"), IconMedium(
"icon_medium.png"), IconSmall("icon_small.png"), DocumentEdit("document_edit.png"), EditComponent( "icon_medium.png"), IconSmall("icon_small.png"), DocumentEdit("document_edit.png"), EditComponent(
"edit_component.png"), Size("size.png"), Front("front.png"), Back("back.png"), Pens( "edit_component.png"), Size("size.png"), Front("front.png"), Back("back.png"), Pens(
"pens.png"); "pens.png"), Sort("sort.png");
protected String name; protected String name;
Binary file not shown.

After

Width:  |  Height:  |  Size: 826 B

@@ -12,6 +12,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
@@ -1066,6 +1067,70 @@ public class Presenter implements IPlugInPort {
drawingManager.setTheme(theme); drawingManager.setTheme(theme);
} }
@Override
public void renumberSelectedComponents(final boolean xAxisFirst) {
if (getSelectedComponents().isEmpty()) {
return;
}
Project oldProject = cloner.deepClone(currentProject);
List<IDIYComponent<?>> components = new ArrayList<IDIYComponent<?>>(getSelectedComponents());
// Sort components by their location.
Collections.sort(components, new Comparator<IDIYComponent<?>>() {
@Override
public int compare(IDIYComponent<?> o1, IDIYComponent<?> o2) {
Area area1 = drawingManager.getComponentArea(o1);
Area area2 = drawingManager.getComponentArea(o2);
if (area1 != null && area2 != null) {
Point point1 = area1.getBounds().getLocation();
Point point2 = area2.getBounds().getLocation();
if (xAxisFirst) {
if (point1.y < point2.y) {
return -1;
} else if (point1.y > point2.y) {
return 1;
} else {
if (point1.x < point1.x) {
return -1;
} else if (point1.x > point1.x) {
return 1;
}
}
} else {
if (point1.x < point2.x) {
return -1;
} else if (point1.x > point2.x) {
return 1;
} else {
if (point1.y < point1.y) {
return -1;
} else if (point1.y > point1.y) {
return 1;
}
}
}
}
return 0;
}
});
// Clear names.
for (IDIYComponent<?> component : components) {
component.setName("");
}
// Assign new ones.
for (IDIYComponent<?> component : components) {
component.setName(instantiationManager.createUniqueName(ComponentProcessor
.getInstance().extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) component.getClass()),
currentProject));
}
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, cloner
.deepClone(currentProject), "Renumber selection");
projectFileManager.notifyFileChange();
messageDispatcher.dispatchMessage(EventType.REPAINT);
}
public void updateSelection(List<IDIYComponent<?>> newSelection) { public void updateSelection(List<IDIYComponent<?>> newSelection) {
this.selectedComponents = newSelection; this.selectedComponents = newSelection;
Map<IDIYComponent<?>, Set<Integer>> controlPointMap = new HashMap<IDIYComponent<?>, Set<Integer>>(); Map<IDIYComponent<?>, Set<Integer>> controlPointMap = new HashMap<IDIYComponent<?>, Set<Integer>>();
@@ -146,6 +146,10 @@ public class ActionFactory {
public ThemeAction createThemeAction(IPlugInPort plugInPort, Theme theme) { public ThemeAction createThemeAction(IPlugInPort plugInPort, Theme theme) {
return new ThemeAction(plugInPort, theme); return new ThemeAction(plugInPort, theme);
} }
public RenumberAction createRenumberAction(IPlugInPort plugInPort, boolean xAxisFirst) {
return new RenumberAction(plugInPort, xAxisFirst);
}
// File menu actions. // File menu actions.
@@ -842,4 +846,25 @@ public class ActionFactory {
plugInPort.setSelectedTheme(theme); plugInPort.setSelectedTheme(theme);
} }
} }
public static class RenumberAction extends AbstractAction {
private static final long serialVersionUID = 1L;
private IPlugInPort plugInPort;
private boolean xAxisFirst;
public RenumberAction(IPlugInPort plugInPort, boolean xAxisFirst) {
super();
this.plugInPort = plugInPort;
this.xAxisFirst = xAxisFirst;
putValue(AbstractAction.NAME, xAxisFirst ? "X Axis First" : "Y Axis First");
}
@Override
public void actionPerformed(ActionEvent e) {
LOG.info(getValue(AbstractAction.NAME) + " triggered");
plugInPort.renumberSelectedComponents(xAxisFirst);
}
}
} }
@@ -12,6 +12,7 @@ import org.diylc.common.EventType;
import org.diylc.common.IPlugIn; import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort; import org.diylc.common.IPlugInPort;
import org.diylc.core.Project; import org.diylc.core.Project;
import org.diylc.images.IconLoader;
import org.diylc.swing.ActionFactory; import org.diylc.swing.ActionFactory;
import org.diylc.swing.ISwingUI; import org.diylc.swing.ISwingUI;
@@ -21,7 +22,8 @@ import com.diyfever.gui.undo.UndoHandler;
public class EditMenuPlugin implements IPlugIn, ClipboardOwner { public class EditMenuPlugin implements IPlugIn, ClipboardOwner {
private static final String EDIT_TITLE = "Edit"; private static final String EDIT_TITLE = "Edit";
private static final String RENUMBER_TITLE = "Renumber Selection";
private IPlugInPort plugInPort; private IPlugInPort plugInPort;
private ISwingUI swingUI; private ISwingUI swingUI;
@@ -36,6 +38,8 @@ public class EditMenuPlugin implements IPlugIn, ClipboardOwner {
private ActionFactory.UngroupAction ungroupAction; private ActionFactory.UngroupAction ungroupAction;
private ActionFactory.SendToBackAction sendToBackAction; private ActionFactory.SendToBackAction sendToBackAction;
private ActionFactory.BringToFrontAction bringToFrontAction; private ActionFactory.BringToFrontAction bringToFrontAction;
private ActionFactory.RenumberAction renumberXAxisAction;
private ActionFactory.RenumberAction renumberYAxisAction;
private UndoHandler<Project> undoHandler; private UndoHandler<Project> undoHandler;
@@ -115,14 +119,14 @@ public class EditMenuPlugin implements IPlugIn, ClipboardOwner {
} }
return ungroupAction; return ungroupAction;
} }
public ActionFactory.SendToBackAction getSendToBackAction() { public ActionFactory.SendToBackAction getSendToBackAction() {
if (sendToBackAction == null) { if (sendToBackAction == null) {
sendToBackAction = ActionFactory.getInstance().createSendToBackAction(plugInPort); sendToBackAction = ActionFactory.getInstance().createSendToBackAction(plugInPort);
} }
return sendToBackAction; return sendToBackAction;
} }
public ActionFactory.BringToFrontAction getBringToFrontAction() { public ActionFactory.BringToFrontAction getBringToFrontAction() {
if (bringToFrontAction == null) { if (bringToFrontAction == null) {
bringToFrontAction = ActionFactory.getInstance().createBringToFrontAction(plugInPort); bringToFrontAction = ActionFactory.getInstance().createBringToFrontAction(plugInPort);
@@ -130,10 +134,26 @@ public class EditMenuPlugin implements IPlugIn, ClipboardOwner {
return bringToFrontAction; return bringToFrontAction;
} }
public ActionFactory.RenumberAction getRenumberXAxisAction() {
if (renumberXAxisAction == null) {
renumberXAxisAction = ActionFactory.getInstance()
.createRenumberAction(plugInPort, true);
}
return renumberXAxisAction;
}
public ActionFactory.RenumberAction getRenumberYAxisAction() {
if (renumberYAxisAction == null) {
renumberYAxisAction = ActionFactory.getInstance().createRenumberAction(plugInPort,
false);
}
return renumberYAxisAction;
}
@Override @Override
public void connect(IPlugInPort plugInPort) { public void connect(IPlugInPort plugInPort) {
this.plugInPort = plugInPort; this.plugInPort = plugInPort;
ActionFactory actionFactory = ActionFactory.getInstance(); ActionFactory actionFactory = ActionFactory.getInstance();
swingUI.injectMenuAction(undoHandler.getUndoAction(), EDIT_TITLE); swingUI.injectMenuAction(undoHandler.getUndoAction(), EDIT_TITLE);
@@ -151,8 +171,12 @@ public class EditMenuPlugin implements IPlugIn, ClipboardOwner {
swingUI.injectMenuAction(getSendToBackAction(), EDIT_TITLE); swingUI.injectMenuAction(getSendToBackAction(), EDIT_TITLE);
swingUI.injectMenuAction(getBringToFrontAction(), EDIT_TITLE); swingUI.injectMenuAction(getBringToFrontAction(), EDIT_TITLE);
swingUI.injectMenuAction(null, EDIT_TITLE); swingUI.injectMenuAction(null, EDIT_TITLE);
swingUI.injectSubmenu(RENUMBER_TITLE, IconLoader.Sort.getIcon(), EDIT_TITLE);
swingUI.injectMenuAction(getRenumberXAxisAction(), RENUMBER_TITLE);
swingUI.injectMenuAction(getRenumberYAxisAction(), RENUMBER_TITLE);
swingUI.injectMenuAction(null, EDIT_TITLE);
swingUI.injectMenuAction(actionFactory.createEditProjectAction(plugInPort), EDIT_TITLE); swingUI.injectMenuAction(actionFactory.createEditProjectAction(plugInPort), EDIT_TITLE);
refreshActions(); refreshActions();
} }
@@ -358,6 +358,10 @@
<changeType>NEW_FEATURE</changeType> <changeType>NEW_FEATURE</changeType>
<description>Double click on a component button in the toolbar selects all components of that type</description> <description>Double click on a component button in the toolbar selects all components of that type</description>
</com.diyfever.gui.update.Change> </com.diyfever.gui.update.Change>
<com.diyfever.gui.update.Change>
<changeType>NEW_FEATURE</changeType>
<description>Renumber selected components by X or Y axis</description>
</com.diyfever.gui.update.Change>
</changes> </changes>
<url>http://code.google.com/p/diy-layout-creator/downloads/list</url> <url>http://code.google.com/p/diy-layout-creator/downloads/list</url>
</com.diyfever.gui.update.Version> </com.diyfever.gui.update.Version>