better error handling when drawing

This commit is contained in:
bancika
2011-01-30 14:10:38 +00:00
parent 0b10f0bfe3
commit a8cc661d09
5 changed files with 76 additions and 19 deletions
@@ -9,7 +9,7 @@
</entry> </entry>
<entry> <entry>
<string>lastPath</string> <string>lastPath</string>
<string>C:\Documents and Settings\Branislav Stojkovic\Desktop</string> <string>C:\Documents and Settings\Branislav Stojkovic\Desktop\oldFiles</string>
</entry> </entry>
<entry> <entry>
<string>showTemplatesAtStartup</string> <string>showTemplatesAtStartup</string>
@@ -72,6 +72,12 @@ public enum EventType {
* current cursor location (not taking zoom into account). * current cursor location (not taking zoom into account).
*/ */
MOUSE_MOVED, MOUSE_MOVED,
LAYER_STATE_CHANGED; /**
; * Called when a layer status is changed. The first parameter is a set of indices of locked layers. All layers not included are considered unlocked.
*/
LAYER_STATE_CHANGED,
/**
* Called to update the status message. New status message is passed as a first parameter.
*/
STATUS_MESSAGE_CHANGED;
} }
@@ -16,6 +16,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.log4j.Logger;
import org.diylc.common.DrawOption; import org.diylc.common.DrawOption;
import org.diylc.common.EventType; import org.diylc.common.EventType;
import org.diylc.common.GridType; import org.diylc.common.GridType;
@@ -38,6 +39,8 @@ import com.diyfever.gui.simplemq.MessageDispatcher;
*/ */
public class DrawingManager { public class DrawingManager {
private static final Logger LOG = Logger.getLogger(DrawingManager.class);
public static int CONTROL_POINT_SIZE = 7; public static int CONTROL_POINT_SIZE = 7;
public static final String ANTIALIASING_KEY = "anti-aliasing"; public static final String ANTIALIASING_KEY = "anti-aliasing";
@@ -56,6 +59,7 @@ public class DrawingManager {
private Composite slotComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f); private Composite slotComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
private Composite lockedComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f); private Composite lockedComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
private List<IDIYComponent<?>> failedComponents = new ArrayList<IDIYComponent<?>>();
private double zoomLevel = 1d;// ConfigurationManager.getInstance().readDouble(ZOOM_KEY, private double zoomLevel = 1d;// ConfigurationManager.getInstance().readDouble(ZOOM_KEY,
// 1d); // 1d);
@@ -69,12 +73,31 @@ public class DrawingManager {
lastDrawnStateMap = new HashMap<IDIYComponent<?>, ComponentState>(); lastDrawnStateMap = new HashMap<IDIYComponent<?>, ComponentState>();
} }
public void drawProject(Graphics2D g2d, Project project, Set<DrawOption> drawOptions, /**
IComponentFiler filter, Rectangle selectionRect, List<IDIYComponent<?>> selectedComponents, * Paints the project onto the canvas and returns the list of components
Set<IDIYComponent<?>> lockedComponents, Set<IDIYComponent<?>> groupedComponents, * that failed to draw.
List<Point> controlPointSlot, IDIYComponent<?> componentSlot, boolean dragInProgress) { *
* @param g2d
* @param project
* @param drawOptions
* @param filter
* @param selectionRect
* @param selectedComponents
* @param lockedComponents
* @param groupedComponents
* @param controlPointSlot
* @param componentSlot
* @param dragInProgress
* @return
*/
public List<IDIYComponent<?>> drawProject(Graphics2D g2d, Project project,
Set<DrawOption> drawOptions, IComponentFiler filter, Rectangle selectionRect,
List<IDIYComponent<?>> selectedComponents, Set<IDIYComponent<?>> lockedComponents,
Set<IDIYComponent<?>> groupedComponents, List<Point> controlPointSlot,
IDIYComponent<?> componentSlot, boolean dragInProgress) {
failedComponents.clear();
if (project == null) { if (project == null) {
return; return failedComponents;
} }
G2DWrapper g2dWrapper = new G2DWrapper(g2d); G2DWrapper g2dWrapper = new G2DWrapper(g2d);
@@ -158,7 +181,12 @@ public class DrawingManager {
g2d.setComposite(lockedComposite); g2d.setComposite(lockedComposite);
} }
// Draw the component through the g2dWrapper. // Draw the component through the g2dWrapper.
try {
component.draw(g2dWrapper, state, project, g2dWrapper); component.draw(g2dWrapper, state, project, g2dWrapper);
} catch (Exception e) {
LOG.error("Error drawing " + component.getName(), e);
failedComponents.add(component);
}
Area area = g2dWrapper.finishedDrawingComponent(); Area area = g2dWrapper.finishedDrawingComponent();
if (trackArea) { if (trackArea) {
componentAreaMap.put(component, area); componentAreaMap.put(component, area);
@@ -214,7 +242,12 @@ public class DrawingManager {
if (componentSlot != null) { if (componentSlot != null) {
g2dWrapper.startedDrawingComponent(); g2dWrapper.startedDrawingComponent();
g2dWrapper.setComposite(slotComposite); g2dWrapper.setComposite(slotComposite);
try {
componentSlot.draw(g2dWrapper, ComponentState.NORMAL, project, g2dWrapper); componentSlot.draw(g2dWrapper, ComponentState.NORMAL, project, g2dWrapper);
} catch (Exception e) {
LOG.error("Error drawing " + componentSlot.getName(), e);
failedComponents.add(componentSlot);
}
g2dWrapper.finishedDrawingComponent(); g2dWrapper.finishedDrawingComponent();
} }
@@ -258,6 +291,8 @@ public class DrawingManager {
g2d.draw(area); g2d.draw(area);
} }
} }
return failedComponents;
} }
public double getZoomLevel() { public double getZoomLevel() {
@@ -299,7 +299,8 @@ public class Presenter implements IPlugInPort {
groupedComponents.add(component); groupedComponents.add(component);
} }
} }
// Don't draw the component in the slot if both control points match. // Don't draw the component in the slot if both control points
// match.
IDIYComponent<?> componentSlotToDraw; IDIYComponent<?> componentSlotToDraw;
if (instantiationManager.getFirstControlPoint() != null if (instantiationManager.getFirstControlPoint() != null
&& instantiationManager.getPotentialControlPoint() != null && instantiationManager.getPotentialControlPoint() != null
@@ -309,10 +310,23 @@ public class Presenter implements IPlugInPort {
} else { } else {
componentSlotToDraw = instantiationManager.getComponentSlot(); componentSlotToDraw = instantiationManager.getComponentSlot();
} }
drawingManager.drawProject(g2d, currentProject, drawOptions, filter, selectionRect, List<IDIYComponent<?>> failedComponents = drawingManager.drawProject(g2d, currentProject,
selectedComponents, getLockedComponents(), groupedComponents, Arrays.asList( drawOptions, filter, selectionRect, selectedComponents, getLockedComponents(),
instantiationManager.getFirstControlPoint(), instantiationManager groupedComponents, Arrays.asList(instantiationManager.getFirstControlPoint(),
.getPotentialControlPoint()), componentSlotToDraw, dragInProgress); instantiationManager.getPotentialControlPoint()), componentSlotToDraw,
dragInProgress);
List<String> failedComponentNames = new ArrayList<String>();
for (IDIYComponent<?> component : failedComponents) {
failedComponentNames.add(component.getName());
}
Collections.sort(failedComponentNames);
if (!failedComponentNames.isEmpty()) {
messageDispatcher.dispatchMessage(EventType.STATUS_MESSAGE_CHANGED,
"<html><font color='red'>Failed to draw components: "
+ Utils.toCommaString(failedComponentNames) + "</font></html>");
} else {
messageDispatcher.dispatchMessage(EventType.STATUS_MESSAGE_CHANGED, "");
}
} }
/** /**
@@ -66,6 +66,7 @@ public class StatusBar extends JPanel implements IPlugIn {
private List<String> componentNamesUnderCursor; private List<String> componentNamesUnderCursor;
private List<String> selectedComponentNames; private List<String> selectedComponentNames;
private List<String> stuckComponentNames; private List<String> stuckComponentNames;
private String statusMessage;
public StatusBar(ISwingUI swingUI) { public StatusBar(ISwingUI swingUI) {
super(); super();
@@ -196,7 +197,8 @@ public class StatusBar extends JPanel implements IPlugIn {
@Override @Override
public EnumSet<EventType> getSubscribedEventTypes() { public EnumSet<EventType> getSubscribedEventTypes() {
return EnumSet.of(EventType.ZOOM_CHANGED, EventType.SLOT_CHANGED, return EnumSet.of(EventType.ZOOM_CHANGED, EventType.SLOT_CHANGED,
EventType.AVAILABLE_CTRL_POINTS_CHANGED, EventType.SELECTION_CHANGED); EventType.AVAILABLE_CTRL_POINTS_CHANGED, EventType.SELECTION_CHANGED,
EventType.STATUS_MESSAGE_CHANGED);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -247,11 +249,13 @@ public class StatusBar extends JPanel implements IPlugIn {
Collections.sort(componentNamesUnderCursor); Collections.sort(componentNamesUnderCursor);
refreshStatusText(); refreshStatusText();
break; break;
case STATUS_MESSAGE_CHANGED:
statusMessage = (String) params[0];
} }
} }
private void refreshStatusText() { private void refreshStatusText() {
String statusText = ""; String statusText = this.statusMessage;
if (componentSlot == null) { if (componentSlot == null) {
if (componentNamesUnderCursor != null && !componentNamesUnderCursor.isEmpty()) { if (componentNamesUnderCursor != null && !componentNamesUnderCursor.isEmpty()) {
String formattedNames = Utils.toCommaString(componentNamesUnderCursor); String formattedNames = Utils.toCommaString(componentNamesUnderCursor);
@@ -265,8 +269,6 @@ public class StatusBar extends JPanel implements IPlugIn {
builder.append(")"); builder.append(")");
} }
statusText = "<html>Selection: " + builder.toString() + "</html>"; statusText = "<html>Selection: " + builder.toString() + "</html>";
} else {
statusText = "";
} }
} else { } else {
switch (componentSlot.getCreationMethod()) { switch (componentSlot.getCreationMethod()) {