auto-edit mode

This commit is contained in:
bancika
2011-02-24 00:02:32 +00:00
parent 3856af3e18
commit 4154225e71
10 changed files with 249 additions and 145 deletions
@@ -40,6 +40,7 @@ public interface IPlugInPort {
public static final String SNAP_TO_GRID_KEY = "snapToGrid";
public static final String AUTO_PADS_KEY = "autoCreatePads";
public static final String CONTINUOUS_CREATION_KEY = "continuousCreation";
public static final String AUTO_EDIT_KEY = "autoEdit";
/**
* Returns size of the canvas that takes project dimensions into account as
@@ -163,8 +164,9 @@ public interface IPlugInPort {
* @param ctrlDown
* @param shiftDown
* @param altDown
* @param clickCount
*/
void mouseClicked(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown);
void mouseClicked(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown, int clickCount);
/**
* Notifies the presenter that mouse is moved.
@@ -1,5 +1,10 @@
package org.diylc.core;
import java.util.List;
import java.util.Set;
import org.diylc.common.PropertyWrapper;
/**
* Base interface for the main GUI component.
@@ -28,4 +33,6 @@ public interface IView {
void showMessage(String message, String title, int messageType);
int showConfirmDialog(String message, String title, int optionType, int messageType);
boolean editProperties(List<PropertyWrapper> properties, Set<PropertyWrapper> defaultedProperties);
}
@@ -19,6 +19,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import org.diylc.common.ComponentType;
import org.diylc.common.DrawOption;
@@ -348,120 +350,160 @@ public class Presenter implements IPlugInPort {
}
@Override
public void mouseClicked(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown) {
public void mouseClicked(Point point, boolean ctrlDown, boolean shiftDown, boolean altDown,
int clickCount) {
LOG.debug(String
.format("mouseClicked(%s, %s, %s, %s)", point, ctrlDown, shiftDown, altDown));
Point scaledPoint = scalePoint(point);
if (instantiationManager.getComponentTypeSlot() != null) {
// Keep the reference to component type for later.
ComponentType componentTypeSlot = instantiationManager.getComponentTypeSlot();
Project oldProject = cloner.deepClone(currentProject);
switch (componentTypeSlot.getCreationMethod()) {
case SINGLE_CLICK:
try {
if (ConfigurationManager.getInstance().readBoolean(
IPlugInPort.SNAP_TO_GRID_KEY, true)) {
CalcUtils.snapPointToGrid(scaledPoint, currentProject.getGridSpacing());
}
IDIYComponent<?> component = instantiationManager.instantiateComponent(
componentTypeSlot, scaledPoint, currentProject);
addComponent(component, componentTypeSlot, true);
// Select the new component
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
// selectedComponents);
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
// calculateSelectionDimension());
messageDispatcher.dispatchMessage(EventType.REPAINT);
List<IDIYComponent<?>> newSelection = new ArrayList<IDIYComponent<?>>();
newSelection.add(component);
updateSelection(newSelection);
} catch (Exception e) {
LOG.error("Error instatiating component of type: "
+ componentTypeSlot.getInstanceClass().getName(), e);
}
if (ConfigurationManager.getInstance().readBoolean(
IPlugInPort.CONTINUOUS_CREATION_KEY, false)) {
setNewComponentTypeSlot(componentTypeSlot);
} else {
setNewComponentTypeSlot(null);
}
break;
case POINT_BY_POINT:
// First click is just to set the controlPointSlot and
// componentSlot.
if (ConfigurationManager.getInstance().readBoolean(IPlugInPort.SNAP_TO_GRID_KEY,
true)) {
CalcUtils.snapPointToGrid(scaledPoint, currentProject.getGridSpacing());
}
if (instantiationManager.getComponentSlot() == null) {
if (clickCount >= 2) {
editSelection();
} else {
if (instantiationManager.getComponentTypeSlot() != null) {
// Keep the reference to component type for later.
ComponentType componentTypeSlot = instantiationManager.getComponentTypeSlot();
Project oldProject = cloner.deepClone(currentProject);
switch (componentTypeSlot.getCreationMethod()) {
case SINGLE_CLICK:
try {
instantiationManager.instatiatePointByPoint(scaledPoint, currentProject);
if (ConfigurationManager.getInstance().readBoolean(
IPlugInPort.SNAP_TO_GRID_KEY, true)) {
CalcUtils.snapPointToGrid(scaledPoint, currentProject.getGridSpacing());
}
IDIYComponent<?> component = instantiationManager.instantiateComponent(
componentTypeSlot, scaledPoint, currentProject);
addComponent(component, componentTypeSlot, true);
// Select the new component
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
// selectedComponents);
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
// calculateSelectionDimension());
messageDispatcher.dispatchMessage(EventType.REPAINT);
List<IDIYComponent<?>> newSelection = new ArrayList<IDIYComponent<?>>();
newSelection.add(component);
updateSelection(newSelection);
} catch (Exception e) {
view.showMessage("Could not create component. Check log for details.",
"Error", IView.ERROR_MESSAGE);
LOG.error("Could not create component", e);
LOG.error("Error instatiating component of type: "
+ componentTypeSlot.getInstanceClass().getName(), e);
}
messageDispatcher.dispatchMessage(EventType.SLOT_CHANGED, componentTypeSlot,
instantiationManager.getFirstControlPoint());
messageDispatcher.dispatchMessage(EventType.REPAINT);
} else {
// On the second click, add the component to the project.
IDIYComponent<?> componentSlot = instantiationManager.getComponentSlot();
componentSlot.setControlPoint(scaledPoint, 1);
addComponent(componentSlot, componentTypeSlot, true);
// Select the new component if it's not locked.
List<IDIYComponent<?>> newSelection = new ArrayList<IDIYComponent<?>>();
if (!isComponentLocked(componentSlot)) {
newSelection.add(componentSlot);
if (ConfigurationManager.getInstance().readBoolean(IPlugInPort.AUTO_EDIT_KEY,
false)) {
editSelection();
}
updateSelection(newSelection);
messageDispatcher.dispatchMessage(EventType.REPAINT);
if (ConfigurationManager.getInstance().readBoolean(
IPlugInPort.CONTINUOUS_CREATION_KEY, false)) {
setNewComponentTypeSlot(componentTypeSlot);
} else {
setNewComponentTypeSlot(null);
}
}
break;
default:
LOG.error("Unknown creation method: " + componentTypeSlot.getCreationMethod());
}
// Notify the listeners.
if (!oldProject.equals(currentProject)) {
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject, cloner
.deepClone(currentProject), "Add " + componentTypeSlot.getName());
projectFileManager.notifyFileChange();
}
} else {
List<IDIYComponent<?>> newSelection = new ArrayList<IDIYComponent<?>>(
selectedComponents);
List<IDIYComponent<?>> components = findComponentsAt(scaledPoint);
// If there's nothing under mouse cursor deselect all.
if (components.isEmpty()) {
newSelection.clear();
} else {
IDIYComponent<?> component = components.get(components.size() - 1);
// If ctrl is pressed just toggle the component under mouse
// cursor.
if (ctrlDown) {
if (newSelection.contains(component)) {
newSelection.removeAll(findAllGroupedComponents(component));
break;
case POINT_BY_POINT:
// First click is just to set the controlPointSlot and
// componentSlot.
if (ConfigurationManager.getInstance().readBoolean(
IPlugInPort.SNAP_TO_GRID_KEY, true)) {
CalcUtils.snapPointToGrid(scaledPoint, currentProject.getGridSpacing());
}
if (instantiationManager.getComponentSlot() == null) {
try {
instantiationManager
.instatiatePointByPoint(scaledPoint, currentProject);
} catch (Exception e) {
view.showMessage("Could not create component. Check log for details.",
"Error", IView.ERROR_MESSAGE);
LOG.error("Could not create component", e);
}
messageDispatcher.dispatchMessage(EventType.SLOT_CHANGED,
componentTypeSlot, instantiationManager.getFirstControlPoint());
messageDispatcher.dispatchMessage(EventType.REPAINT);
} else {
// On the second click, add the component to the
// project.
IDIYComponent<?> componentSlot = instantiationManager.getComponentSlot();
componentSlot.setControlPoint(scaledPoint, 1);
addComponent(componentSlot, componentTypeSlot, true);
// Select the new component if it's not locked.
List<IDIYComponent<?>> newSelection = new ArrayList<IDIYComponent<?>>();
if (!isComponentLocked(componentSlot)) {
newSelection.add(componentSlot);
}
updateSelection(newSelection);
messageDispatcher.dispatchMessage(EventType.REPAINT);
if (ConfigurationManager.getInstance().readBoolean(IPlugInPort.AUTO_EDIT_KEY, false)) {
editSelection();
}
if (ConfigurationManager.getInstance().readBoolean(
IPlugInPort.CONTINUOUS_CREATION_KEY, false)) {
setNewComponentTypeSlot(componentTypeSlot);
} else {
setNewComponentTypeSlot(null);
}
}
break;
default:
LOG.error("Unknown creation method: " + componentTypeSlot.getCreationMethod());
}
// Notify the listeners.
if (!oldProject.equals(currentProject)) {
messageDispatcher.dispatchMessage(EventType.PROJECT_MODIFIED, oldProject,
cloner.deepClone(currentProject), "Add " + componentTypeSlot.getName());
projectFileManager.notifyFileChange();
}
} else {
List<IDIYComponent<?>> newSelection = new ArrayList<IDIYComponent<?>>(
selectedComponents);
List<IDIYComponent<?>> components = findComponentsAt(scaledPoint);
// If there's nothing under mouse cursor deselect all.
if (components.isEmpty()) {
newSelection.clear();
} else {
IDIYComponent<?> component = components.get(components.size() - 1);
// If ctrl is pressed just toggle the component under mouse
// cursor.
if (ctrlDown) {
if (newSelection.contains(component)) {
newSelection.removeAll(findAllGroupedComponents(component));
} else {
newSelection.addAll(findAllGroupedComponents(component));
}
} else {
// Otherwise just select that one component.
newSelection.clear();
newSelection.addAll(findAllGroupedComponents(component));
}
} else {
// Otherwise just select that one component.
newSelection.clear();
newSelection.addAll(findAllGroupedComponents(component));
}
updateSelection(newSelection);
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
// selectedComponents);
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
// calculateSelectionDimension());
messageDispatcher.dispatchMessage(EventType.REPAINT);
}
}
}
private void editSelection() {
List<PropertyWrapper> properties = getMutualSelectionProperties();
if (properties != null && !properties.isEmpty()) {
Set<PropertyWrapper> defaultedProperties = new HashSet<PropertyWrapper>();
boolean edited = view.editProperties(properties, defaultedProperties);
if (edited) {
try {
applyPropertiesToSelection(properties);
} catch (Exception e1) {
view.showMessage(
"Error occured while editing selection. Check the log for details.",
"Error", JOptionPane.ERROR_MESSAGE);
LOG.error("Error applying properties", e1);
}
// Save default values.
for (PropertyWrapper property : defaultedProperties) {
if (property.getValue() != null) {
setSelectionDefaultPropertyValue(property.getName(), property.getValue());
}
}
}
updateSelection(newSelection);
// messageDispatcher.dispatchMessage(EventType.SELECTION_CHANGED,
// selectedComponents);
// messageDispatcher.dispatchMessage(EventType.SELECTION_SIZE_CHANGED,
// calculateSelectionDimension());
messageDispatcher.dispatchMessage(EventType.REPAINT);
}
}
@@ -556,7 +598,7 @@ public class Presenter implements IPlugInPort {
LOG.debug(String.format("dragStarted(%s)", point));
if (instantiationManager.getComponentTypeSlot() != null) {
LOG.debug("Cannot start drag because a new component is being created.");
mouseClicked(point, ctrlDown, shiftDown, altDown);
mouseClicked(point, ctrlDown, shiftDown, altDown, 1);
return;
}
dragInProgress = true;
@@ -11,7 +11,9 @@ import java.awt.event.WindowEvent;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.Action;
import javax.swing.BoxLayout;
@@ -35,10 +37,12 @@ import org.diylc.common.EventType;
import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort;
import org.diylc.common.ITask;
import org.diylc.common.PropertyWrapper;
import org.diylc.core.IView;
import org.diylc.images.IconLoader;
import org.diylc.presenter.Presenter;
import org.diylc.swing.ISwingUI;
import org.diylc.swing.gui.editor.PropertyEditorDialog;
import org.diylc.swing.plugins.canvas.CanvasPlugin;
import org.diylc.swing.plugins.config.ConfigPlugin;
import org.diylc.swing.plugins.edit.EditMenuPlugin;
@@ -48,6 +52,8 @@ import org.diylc.swing.plugins.layers.LayersMenuPlugin;
import org.diylc.swing.plugins.statusbar.StatusBar;
import org.diylc.swing.plugins.toolbox.ToolBox;
import com.diyfever.gui.ButtonDialog;
public class MainFrame extends JFrame implements ISwingUI {
private static final Logger LOG = Logger.getLogger(MainFrame.class);
@@ -196,6 +202,15 @@ public class MainFrame extends JFrame implements ISwingUI {
return JOptionPane.showConfirmDialog(this, message, title, optionType, messageType);
}
@Override
public boolean editProperties(List<PropertyWrapper> properties, Set<PropertyWrapper> defaultedProperties) {
PropertyEditorDialog editor = DialogFactory.getInstance().createPropertyEditorDialog(
properties, "Edit Selection");
editor.setVisible(true);
defaultedProperties.addAll(editor.getDefaultedProperties());
return ButtonDialog.OK.equals(editor.getSelectedButtonCaption());
}
private JMenu findOrCreateMenu(String menuName) {
JMenu menu;
if (menuMap.containsKey(menuName)) {
@@ -15,6 +15,7 @@ import java.io.File;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
@@ -34,6 +35,7 @@ import javax.swing.event.ListSelectionListener;
import org.apache.log4j.Logger;
import org.diylc.common.DrawOption;
import org.diylc.common.IPlugInPort;
import org.diylc.common.PropertyWrapper;
import org.diylc.core.IView;
import org.diylc.presenter.Presenter;
@@ -77,6 +79,11 @@ public class TemplateDialog extends JDialog {
public void showMessage(String message, String title, int messageType) {
JOptionPane.showMessageDialog(TemplateDialog.this, message, title, messageType);
}
@Override
public boolean editProperties(List<PropertyWrapper> properties, Set<PropertyWrapper> defaultedProperties) {
return false;
}
});
// this.presenter.installPlugin(new IPlugIn() {
//
@@ -25,11 +25,14 @@ import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import org.apache.log4j.Logger;
import org.diylc.common.DrawOption;
import org.diylc.common.IPlugInPort;
import org.diylc.common.PropertyWrapper;
import org.diylc.presenter.Presenter;
import org.diylc.swing.gui.DialogFactory;
import org.diylc.swing.gui.editor.PropertyEditorDialog;
@@ -43,6 +46,8 @@ import com.diyfever.gui.miscutils.ConfigurationManager;
*/
class CanvasPanel extends JComponent implements Autoscroll {
private static final Logger LOG = Logger.getLogger(CanvasPanel.class);
private static final long serialVersionUID = 1L;
private IPlugInPort plugInPort;
@@ -185,53 +190,61 @@ class CanvasPanel extends JComponent implements Autoscroll {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
if (e.getClickCount() == 2) {
List<PropertyWrapper> properties = plugInPort
.getMutualSelectionProperties();
if (properties == null || properties.isEmpty()) {
// properties = plugInPort.getProjectProperties();
// PropertyEditorDialog editor =
// DialogFactory.getInstance()
// .createPropertyEditorDialog(properties,
// "Edit Project");
// editor.setVisible(true);
// if
// (ButtonDialog.OK.equals(editor.getSelectedButtonCaption()))
// {
// plugInPort.applyPropertiesToProject(properties);
// // Save default values.
// for (PropertyWrapper property :
// editor.getDefaultedProperties()) {
// if (property.getValue() != null) {
// plugInPort.setProjectDefaultPropertyValue(property.getName(),
// property.getValue());
// }
// }
// }
} else {
PropertyEditorDialog editor = DialogFactory.getInstance()
.createPropertyEditorDialog(properties, "Edit Selection");
editor.setVisible(true);
if (ButtonDialog.OK.equals(editor.getSelectedButtonCaption())) {
try {
plugInPort.applyPropertiesToSelection(properties);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Save default values.
for (PropertyWrapper property : editor.getDefaultedProperties()) {
if (property.getValue() != null) {
plugInPort.setSelectionDefaultPropertyValue(property
.getName(), property.getValue());
}
}
}
}
} else {
plugInPort.mouseClicked(e.getPoint(), e.isControlDown(), e.isShiftDown(), e
.isAltDown());
}
// if (e.getClickCount() == 2) {
// List<PropertyWrapper> properties = plugInPort
// .getMutualSelectionProperties();
// if (properties == null || properties.isEmpty()) {
// // properties = plugInPort.getProjectProperties();
// // PropertyEditorDialog editor =
// // DialogFactory.getInstance()
// // .createPropertyEditorDialog(properties,
// // "Edit Project");
// // editor.setVisible(true);
// // if
// //
// (ButtonDialog.OK.equals(editor.getSelectedButtonCaption()))
// // {
// // plugInPort.applyPropertiesToProject(properties);
// // // Save default values.
// // for (PropertyWrapper property :
// // editor.getDefaultedProperties()) {
// // if (property.getValue() != null) {
// //
// plugInPort.setProjectDefaultPropertyValue(property.getName(),
// // property.getValue());
// // }
// // }
// // }
// } else {
// PropertyEditorDialog editor = DialogFactory.getInstance()
// .createPropertyEditorDialog(properties,
// "Edit Selection");
// editor.setVisible(true);
// if
// (ButtonDialog.OK.equals(editor.getSelectedButtonCaption()))
// {
// try {
// plugInPort.applyPropertiesToSelection(properties);
// } catch (Exception e1) {
// JOptionPane.showMessageDialog(CanvasPanel.this,
// "Error occured while editing selection. Check the log for details.",
// "Error", JOptionPane.ERROR_MESSAGE);
// LOG.error("Error applying properties", e1);
// }
// // Save default values.
// for (PropertyWrapper property :
// editor.getDefaultedProperties()) {
// if (property.getValue() != null) {
// plugInPort.setSelectionDefaultPropertyValue(property
// .getName(), property.getValue());
// }
// }
// }
// }
// } else {
plugInPort.mouseClicked(e.getPoint(), e.isControlDown(), e.isShiftDown(), e
.isAltDown(), e.getClickCount());
// }
}
}
});
@@ -50,6 +50,8 @@ public class ConfigPlugin implements IPlugIn {
"Snap to Grid", IPlugInPort.SNAP_TO_GRID_KEY, true), CONFIG_MENU);
swingUI.injectMenuAction(ActionFactory.getInstance().createConfigAction(plugInPort,
"Auto-Create Pads", IPlugInPort.AUTO_PADS_KEY, false), CONFIG_MENU);
swingUI.injectMenuAction(ActionFactory.getInstance().createConfigAction(plugInPort,
"Auto-Edit Mode", IPlugInPort.AUTO_EDIT_KEY, false), CONFIG_MENU);
swingUI.injectMenuAction(ActionFactory.getInstance().createConfigAction(plugInPort,
"Continuous Creation", IPlugInPort.CONTINUOUS_CREATION_KEY, false), CONFIG_MENU);
File themeDir = new File("themes");
@@ -10,6 +10,8 @@ import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
@@ -18,6 +20,7 @@ import javax.swing.JPanel;
import org.diylc.common.DrawOption;
import org.diylc.common.IPlugInPort;
import org.diylc.common.PropertyWrapper;
import org.diylc.core.IView;
import org.diylc.core.Project;
import org.diylc.presenter.Presenter;
@@ -58,6 +61,11 @@ public class ProjectPreview extends JPanel implements PropertyChangeListener, IF
@Override
public void showMessage(String message, String title, int messageType) {
}
@Override
public boolean editProperties(List<PropertyWrapper> properties, Set<PropertyWrapper> defaultedProperties) {
return false;
}
});
xStream = new XStream(new DomDriver());