package mindmap.client;
import java.util.LinkedList;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.types.Visibility;
import com.smartgwt.client.widgets.AnimationCallback;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.MouseOutEvent;
import com.smartgwt.client.widgets.events.MouseOutHandler;
import com.smartgwt.client.widgets.events.MouseOverEvent;
import com.smartgwt.client.widgets.events.MouseOverHandler;
import com.smartgwt.client.widgets.layout.VLayout;
/**
* An object to contain and display a list of operations. Will show
* when the mouse hovers over the top of an entity and fade after a
* specified amount of time once the mouse moves away.
* @author Stuart Clarke
*/
public class Operations extends LinkedList<Operation> {
/**
* Generated Serial Version UID.
*/
private static final long serialVersionUID = 7044559806818078180L;
/**
* List of operations.
*/
protected static LinkedList<VLayout> TOTAL_OPERATIONS;
/**
* Layout to place operation labels into.
*/
protected VLayout LAYOUT;
/**
* Fade time of the operation list in ms.
*/
private int FADE_TIME = 500;
/**
* Timer used to determine how long ago the user's mouse
* was over the Layout.
*/
private Timer TIMER;
/**
* Boolean to determine whether operation text is wrapped or not.
*/
protected static boolean WRAP = false;
/**
* Creates the list of operations for this object.
*/
public void getMenu(){
LAYOUT = new VLayout();
LAYOUT.setAutoHeight();
LAYOUT.setAutoWidth();
LAYOUT.setPadding(3);
LAYOUT.setBorder("1px solid #CCCCCC");
LAYOUT.setBackgroundColor("#FFFFFF");
for(final Operation operation : this){
if(operation.DESCRIPTION == null)
operation.DESCRIPTION = operation.NAME;
final Label label = new Label(operation.NAME);
label.setAutoFit(true);
if(!WRAP)
label.setWrap(false);
label.setPadding(3);
if(operation.ICON!=null)
label.setIcon(operation.ICON);
label.setBackgroundColor("#FFFFFF");
label.addMouseOverHandler(new MouseOverHandler(){
/* (non-Javadoc)
* @see com.smartgwt.client.widgets.events.MouseOverHandler#onMouseOver(com.smartgwt.client.widgets.events.MouseOverEvent)
*/
public void onMouseOver(MouseOverEvent event){
label.setBackgroundColor("#EEEECC");
}
});
label.addMouseOutHandler(new MouseOutHandler(){
/* (non-Javadoc)
* @see com.smartgwt.client.widgets.events.MouseOutHandler#onMouseOut(com.smartgwt.client.widgets.events.MouseOutEvent)
*/
public void onMouseOut(MouseOutEvent event){
label.setBackgroundColor("#FFFFFF");
}
});
label.addClickHandler(new ClickHandler(){
/* (non-Javadoc)
* @see com.smartgwt.client.widgets.events.ClickHandler#onClick(com.smartgwt.client.widgets.events.ClickEvent)
*/
public void onClick(ClickEvent event){
com.smartgwt.client.widgets.Window window = new com.smartgwt.client.widgets.Window();
int width = operation.WIDTH;
int height = operation.HEIGHT;
int left = Window.getScrollLeft() + (Home.WIDTH/2) - (width/2);
int top = Window.getScrollTop() + (Home.HEIGHT/2) - (height/2);
RootPanel.get().add(window, left, top);
window.setShowMinimizeButton(false);
window.setSize(String.valueOf(width),String.valueOf(height));
window.setCanDragReposition(true);
window.setCanDrop(true);
window.setTitle(operation.DESCRIPTION);
window.setSrc(operation.COMMAND);
window.draw();
if(width == 0 || height == 0){
window.setVisibility(Visibility.HIDDEN);
}
}
});
LAYOUT.addMember(label);
}
LAYOUT.hide();
TOTAL_OPERATIONS.add(LAYOUT);
}
/**
* Show the list of operations.
*/
public void show(){
if(this.size() != 0){
if(TIMER!=null)
TIMER.cancel();
hideAll();
LAYOUT.show();
LAYOUT.bringToFront();
LAYOUT.setOpacity(100);
}
}
/**
* Hide the list of operations.
*/
public void hide() {
TIMER = new Timer(){
/* (non-Javadoc)
* @see com.google.gwt.user.client.Timer#run()
*/
public void run() {
LAYOUT.animateFade(0,new AnimationCallback(){
/* (non-Javadoc)
* @see com.smartgwt.client.widgets.AnimationCallback#execute(boolean)
*/
public void execute(boolean earlyFinish) {
LAYOUT.hide();
}
},FADE_TIME);
}
};
TIMER.schedule(FADE_TIME);
}
/**
* Hides all operation lists.
*/
public static void hideAll(){
for(VLayout layout : TOTAL_OPERATIONS){
layout.hide();
}
}
}