Commit 5800d7f6 authored by Nico Mack's avatar Nico Mack

Fixed potential CME through better encapsulation of coronas property in

BaseWidget
Added "extra dark" and "extra bright" shades to RGB colours "shade"
attribute
parent 1ae73fee
......@@ -128,6 +128,8 @@ SECTIONS_NODE=sections
SELECTED_ELEMENT=selected
SHADE_ATTRIBUTE=shade
SHADE_BRIGHTER_VALUE=brighter
SHADE_EXTRA_BRIGHT_VALUE=extra bright
SHADE_EXTRA_DARK_VALUE=extra dark
SHADE_DARKER_VALUE=darker
SHAPE_NODE=shape
SOUND_BASE64_NODE=soundBase64
......
......@@ -592,11 +592,13 @@ public class BootstrappingUtils {
colour = new Color(rgb, true); // With Alpha Channel
if (Externalization.SHADE_DARKER_VALUE.equals(shade)) {
colour = colour.darker();
} else if (Externalization.SHADE_EXTRA_DARK_VALUE.equals(shade)) {
colour = colour.darker().darker();
} else if (Externalization.SHADE_BRIGHTER_VALUE.equals(shade)) {
colour = colour.brighter();
} else if (Externalization.SHADE_EXTRA_BRIGHT_VALUE.equals(shade)) {
colour = colour.brighter().brighter();
}
} else {
String colourString = getContent(colourNode, Externalization.EMPTY_STRING, null);
if ((colourString != null) && (colourString.length() > 0)) {
......
......@@ -46,7 +46,6 @@ public class ColorFactory {
// * Class Body *
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* Color generator using three out of phase sine waves to produce a rainbow effect. see
* http://krazydad.com/tutorials/makecolors.php from an excellent introduction in the subject of
......@@ -77,11 +76,12 @@ public class ColorFactory {
*
* @return a vector containing the generated colors
*/
// ---------------------------------------------------------------------------
public static List<Color> makeColorGradient(int number, double redFreq, double greenFreq, double blueFreq,
double redPhase, double greenPhase, double bluePhase, int center, int spread) {
Preconditions.checkArgument((center > 0) && (center < 255), "Center should be in the range 0 < center < 255"); //$NON-NLS-1$
Preconditions.checkArgument((spread > 0) && (spread < center), "Spread should be in the range 0 < spread < center"); //$NON-NLS-1$
List<Color> gradient = new ArrayList<>(number);
......@@ -90,14 +90,13 @@ public class ColorFactory {
int red = (int) (Math.sin(redFreq * i + redPhase) * spread + center);
int green = (int) (Math.sin(greenFreq * i + greenPhase) * spread + center);
int blue = (int) (Math.sin(blueFreq * i + bluePhase) * spread + center);
gradient.add(new Color(red, green, blue));
}
return gradient;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
/**
* Generates n colors covering the full spectrum of a rainbow, i.e. ranging from purple to red
*
......@@ -105,6 +104,8 @@ public class ColorFactory {
* specifies the number of colors to generate
* @return a vector containing the generated colors
*/
// ---------------------------------------------------------------------------
public static List<Color> makeRainbowColours(int number) {
double frequency = (THREE_PI_HALF / number);
......
......@@ -163,6 +163,8 @@ public class Externalization extends NLS {
public static String SELECTED_ELEMENT;
public static String SHADE_ATTRIBUTE;
public static String SHADE_BRIGHTER_VALUE;
public static String SHADE_EXTRA_BRIGHT_VALUE;
public static String SHADE_EXTRA_DARK_VALUE;
public static String SHADE_DARKER_VALUE;
public static String SHAPE_NODE;
public static String SOUND_BASE64_NODE;
......
......@@ -73,8 +73,7 @@ public class ScreenCoordinates extends CoordinateState {
@Deprecated
public void toScreen(@NonNull Point context) {
/**
* The conversion is naught as the point's coordinates are already given in this reference
* scheme!
* The conversion is naught as the point's coordinates are already given in this reference scheme!
*/
}
......@@ -90,8 +89,8 @@ public class ScreenCoordinates extends CoordinateState {
/** {@inheritDoc} */
@Override
public Point normalize(Point context) {
double normx = (context.x - bounds.x1) / bounds.x2;
double normy = (context.y - bounds.y1) / bounds.y2;
double normx = (context.x - bounds.x1) / bounds.getWidth();
double normy = (context.y - bounds.y1) / bounds.getHeight();
return new Point((float) normx, (float) normy, context.getAngle(), NormalizedCoordinates.class);
}
......@@ -99,8 +98,8 @@ public class ScreenCoordinates extends CoordinateState {
/** {@inheritDoc} */
@Override
public Point transform(Point normalized) {
double screenx = bounds.x1 + (normalized.x * bounds.x2);
double screeny = bounds.y1 + (normalized.y * bounds.y2);
double screenx = bounds.x1 + (normalized.x * bounds.getWidth());
double screeny = bounds.y1 + (normalized.y * bounds.getHeight());
return new Point((float) screenx, (float) screeny, normalized.getAngle(), this.getClass());
}
......
......@@ -385,13 +385,13 @@ public class ForceDirectedGraph implements GraphListener, Runnable {
if (position.getX() < bottomLeft.getX()) {
bottomLeft.setX(position.getX());
}
if (position.getY() < bottomLeft.getY()) {
if (position.getY() > bottomLeft.getY()) {
bottomLeft.setY(position.getY());
}
if (position.getX() > topRight.getX()) {
topRight.setX(position.getX());
}
if (position.getY() > topRight.getY()) {
if (position.getY() < topRight.getY()) {
topRight.setY(position.getY());
}
}
......
......@@ -39,7 +39,6 @@ import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Area;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
......@@ -66,9 +65,9 @@ public class BaseWidget implements Positionable, Touchable {
/** Specifies the ID of the stage the widget is to be presented on. */
protected int stageId;
/** The visual components to display as part of the widget. It's background. */
protected Multimap<Integer, Corona> coronas;
private Multimap<Integer, Corona> coronas;
/** The positions where the widget's handles are at. */
protected HashMap<Integer, Point> positions;
private HashMap<Integer, Point> positions;
/** The name given to this widget. */
protected String name;
/** Specifies whether this widget is controlled by a tangible object or not. */
......@@ -155,23 +154,54 @@ public class BaseWidget implements Positionable, Touchable {
// * Primitive(s)
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* @return
*/
protected boolean addCoronas(Multimap<Integer, Corona> coronasToAdd) {
boolean added = false;
synchronized (this.coronas) {
for (Entry<Integer, Corona> entry : coronasToAdd.entries()) {
if (!this.coronas.containsEntry(entry.getKey(), entry.getValue())) {
if (this.isActive())
entry.getValue().setActive(true);
added |= this.coronas.put(entry.getKey(), entry.getValue());
}
}
}
return added;
}
// ---------------------------------------------------------------------------
public boolean isActive() {
return this.onStage;
protected boolean removeCoronas(Multimap<Integer, Corona> coronasToRemove) {
boolean removed = false;
synchronized (this.coronas) {
for (Entry<Integer, Corona> entry : coronasToRemove.entries()) {
if (this.coronas.containsEntry(entry.getKey(), entry.getValue())) {
entry.getValue().setActive(false);
removed |= this.coronas.remove(entry.getKey(), entry.getValue());
}
}
}
return removed;
}
// ---------------------------------------------------------------------------
/**
* @param activateIt
*/
protected List<Corona> getCoronas(int handleID) {
List<Corona> candidates = new ArrayList<>();
synchronized (this.coronas) {
candidates.addAll(coronas.get(handleID));
}
return candidates;
}
// ---------------------------------------------------------------------------
public void setActive(boolean activateIt) {
this.onStage = activateIt;
protected List<Corona> getAllCoronas() {
List<Corona> allCoronas = new ArrayList<>();
synchronized (this.coronas) {
allCoronas.addAll(coronas.values());
}
return allCoronas;
}
// ---------------------------------------------------------------------------
......@@ -188,7 +218,7 @@ public class BaseWidget implements Positionable, Touchable {
protected void updateCoronas(int handleID) {
assert positions.get(handleID) != null;
Point centre = getPosition(handleID);
for (Corona corona : coronas.get(handleID)) {
for (Corona corona : this.getCoronas(handleID)) {
corona.setHandleCentre(centre);
corona.invalidate();
}
......@@ -205,7 +235,7 @@ public class BaseWidget implements Positionable, Touchable {
// ---------------------------------------------------------------------------
private void startDrawingIfAppropriate(int handleID) {
for (Corona corona : coronas.get(handleID)) {
for (Corona corona : this.getCoronas(handleID)) {
corona.onTable();
}
}
......@@ -220,7 +250,7 @@ public class BaseWidget implements Positionable, Touchable {
// ---------------------------------------------------------------------------
private void stopDrawing(int handleID) {
for (Corona corona : coronas.get(handleID)) {
for (Corona corona : this.getCoronas(handleID)) {
corona.setActive(false);
}
}
......@@ -229,6 +259,26 @@ public class BaseWidget implements Positionable, Touchable {
// ***************************************************************************
// * Class Body
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* @return
*/
// ---------------------------------------------------------------------------
public boolean isActive() {
return this.onStage;
}
// ---------------------------------------------------------------------------
/**
* @param activateIt
*/
// ---------------------------------------------------------------------------
public void setActive(boolean activateIt) {
this.onStage = activateIt;
}
// ---------------------------------------------------------------------------
/**
* Method invoked when the tangible is detected on the table surface for the first time. The
......@@ -338,17 +388,17 @@ public class BaseWidget implements Positionable, Touchable {
public void paint(Graphics2D canvas) {
clippingRegion.reset();
Collection<Corona> snapshot = coronas.values();
synchronized (coronas) {
for (Corona corona : snapshot) {
if ((corona != null) && corona.isInvalidated()) {
corona.paint(canvas);
clippingRegion.add(new Area(corona.getClippingRegion().getBounds()));
corona.validate();
}
// Collection<Corona> snapshot = coronas.values();
// synchronized (coronas) {
for (Corona corona : this.getAllCoronas()) {
if ((corona != null) && corona.isInvalidated()) {
corona.paint(canvas);
clippingRegion.add(new Area(corona.getClippingRegion().getBounds()));
corona.validate();
}
}
}
// }
// ---------------------------------------------------------------------------
/**
......@@ -366,7 +416,7 @@ public class BaseWidget implements Positionable, Touchable {
public <T> List<T> getCoronas(Class<T> clazz) {
List<T> results = new ArrayList<>();
for (Corona corona : coronas.values()) {
for (Corona corona : this.getAllCoronas()) {
if (clazz.isAssignableFrom(corona.getClass())) {
results.add((T) corona);
}
......@@ -384,7 +434,7 @@ public class BaseWidget implements Positionable, Touchable {
// ---------------------------------------------------------------------------
public List<Corona> getCoronas() {
return new ArrayList<>(coronas.values());
return this.getAllCoronas();
}
// ---------------------------------------------------------------------------
......@@ -453,6 +503,7 @@ public class BaseWidget implements Positionable, Touchable {
*/
// ---------------------------------------------------------------------------
@Override
public @Nullable Point getPosition() {
return this.getPosition(getHandleId());
}
......
......@@ -22,7 +22,6 @@
package lu.list.itis.dkd.tui.widget;
import lu.list.itis.dkd.tui.widget.builder.BaseModalWidgetBuilder;
import lu.list.itis.dkd.tui.widget.corona.Corona;
import lu.list.itis.dkd.tui.widget.corona.bundle.CoronaBundle;
import com.google.common.collect.Multimap;
......@@ -33,7 +32,6 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;
/**
* @author Nico Mack [nico.mack@list.lu]
......@@ -156,17 +154,7 @@ public class ModalWidget extends TetherableWidget {
deactivateAllActiveBundles();
this.addToActiveBundles(nameOfBundle);
}
Multimap<Integer, Corona> bundledCoronas = bundle.getCoronas();
synchronized (this.coronas) {
for (Entry<Integer, Corona> entry : bundledCoronas.entries()) {
if (!this.coronas.containsEntry(entry.getKey(), entry.getValue())) {
if (this.isActive())
entry.getValue().setActive(true);
this.coronas.put(entry.getKey(), entry.getValue());
}
}
}
this.addCoronas(bundle.getCoronas());
}
}
return activated;
......@@ -188,15 +176,7 @@ public class ModalWidget extends TetherableWidget {
if (deactivated) {
Collection<CoronaBundle> bundles = coronaBundles.get(nameOfBundle);
for (CoronaBundle bundle : bundles) {
Multimap<Integer, Corona> bundledCoronas = bundle.getCoronas();
synchronized (this.coronas) {
for (Entry<Integer, Corona> entry : bundledCoronas.entries()) {
if (this.coronas.containsEntry(entry.getKey(), entry.getValue())) {
entry.getValue().setActive(false);
this.coronas.remove(entry.getKey(), entry.getValue());
}
}
}
this.removeCoronas(bundle.getCoronas());
}
}
return deactivated;
......
......@@ -48,8 +48,8 @@ public class StatefulWidgetTest {
StatefulWidget widget = new StatefulWidgetBuilder().withCorona(1, new ShapeCoronaBuilder(new Point(0, 0, 2.4f)).withShape(ShapeFactory.buildRoundedSquare(Calibration.getSquareSize())).build()).withHandle(6, new Point(3, 8, 3.6f)).build();
assertNotNull(widget);
assertTrue(widget.coronas.containsKey(1));
assertFalse(widget.coronas.containsKey(2));
assertFalse(widget.getCoronas(1).isEmpty());
assertTrue(widget.getCoronas(2).isEmpty());
assertTrue(widget.getWidgetIds().contains(1));
assertTrue(widget.objectStates.containsKey(1));
assertNotNull(widget.objectStates);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment