Ecco un punto di partenza / idea per il design:
// Your framework probably provides this
class Point {
private int x, y; // x,y coordinates of top left of View
}
// Your framework probably provides something like this
/** Container class for all other types */
abstract class MindMapView {
public MindMapView(int width, int height, Point position) {
// Initialize fields
}
/** Top-Left */
private Point mPosition;
/** Width, Height */
private int mWidth, mHeight; // Width and height of view
/** Implement this to fulfill a request to draw the view on the screen */
abstract void onDraw();
/** Implement this to fulfill a request to move the view across the screen */
abstract void onMove(Point delta);
/** Implement this to fulfill a request to resize the view */
abstract void onResize(int width, int height);
/**
Implement this to fulfill a request to drag this view. Can be similar
to onMove, except you will want to hook the point where the view is
"dropped", which would be the last point in the drag.
We'd consider a drag to be a left mouse click, mouse movement, and mouse
un-click.
*/
abstract void onDrag(Point delta);
/** Called by other classes that desire to draw this View */
public final void draw() {
onDraw();
}
/** Called by other classes that desire to move this view */
public final void move(Point delta) {
onMove(delta);
}
/** Called by other classes that desire to resize this view */
public final void resize() {
onResize();
}
public final void drag(Point delta) {
onDrag(delta);
}
/** Implement this to
public Point getPosition() {
return mPosition;
}
}
Collegherete la vostra infrastruttura per incanalare i vari eventi in questa vista, ma più che probabile, il vostro framework ha una classe simile pronta per voi. Una volta che lo avete, potete estenderlo per rendere gli altri vostri elementi
Molto simile a MindMapView, eccetto che può contenere altre MindMapViews
abstract class MindMapContainer extends MindMapView {
private ArrayList<MindMapView> mViews;
public ArrayList<MindMapView> getViews() {
return mViews;
}
public MindMapThought(...) {
super(...);
}
public void addView(MindMapView view) {
mViews.add(view);
}
public void removeView(int index) {
...
}
// Subclasses must call super() if these are overridden!
@Override
protected void onMove(Point delta) {
for (MindMapView view : mViews) {
view.move(delta);
}
}
// Do similar for the following
implement onDraw();
implement onResize(int width, int height);
implement onDrag(Point delta);
}
Una volta definite le classi base, puoi eseguire il LOOP su ogni sottoview per spostarlo insieme agli altri
public class MindMapThought extends MindMapView {
/** Implement this to fulfill a request to draw the view on the screen */
@Override
protected void onDraw() {
canvas.draw(...);
}
// Do similar for the following
implement onMove(Point delta);
implement onResize(int width, int height);
implement onDrag(Point delta);
}
Infine, puoi creare qualcosa che si muove come un gruppo
public class MindMapThoughtContainer extends MindMapViewContainer {
...
}
MindMapThought thought1 = new MindMapThought();
MindMapThought thought2 = new MindMapThought();
MindMapThoughtContainer thoughts = new MindMapThoughtContainer();
thoughts.add(thought1);
thoughts.add(thought2);
Point delta = new Point(5,5);
// Move the thoughts container, its' onMove event will iterate its sub-views
// and move them along by the same delta
thoughts.move(delta);
Puoi avere un numero qualsiasi di tipi concreti (implementazioni effettive) finché estendono MindMapView in modo che utilizzino la stessa interfaccia .
Per quanto riguarda le specifiche del cablaggio del progetto, consulta la documentazione del framework.