View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // GenomeBrowser Project
18  //
19  // OperationArea.java
20  // Since: 2007/06/13
21  //
22  // $URL$ 
23  // $Author$ ssksn
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track.operation;
26  
27  import org.utgenome.gwt.utgb.client.track.lib.old.Utilities;
28  
29  import com.google.gwt.event.dom.client.ClickHandler;
30  import com.google.gwt.event.dom.client.MouseOutEvent;
31  import com.google.gwt.event.dom.client.MouseOutHandler;
32  import com.google.gwt.event.dom.client.MouseOverEvent;
33  import com.google.gwt.event.dom.client.MouseOverHandler;
34  import com.google.gwt.event.shared.EventHandler;
35  import com.google.gwt.user.client.DOM;
36  import com.google.gwt.user.client.ui.AbsolutePanel;
37  import com.google.gwt.user.client.ui.Image;
38  import com.google.gwt.xml.client.Node;
39  
40  /**
41   * 
42   * 
43   * @author ssksn
44   * @since GWT 1.4
45   * @version 0.1
46   */
47  public class OperationArea extends Image implements MouseOverHandler, MouseOutHandler {
48  	protected final int startX;
49  	protected final int startY;
50  	protected final int endX;
51  	protected final int endY;
52  
53  	protected final String activeImageURL;
54  	protected final String inactiveImageURL;
55  
56  	private static final String DEFAULT_ACTIVE_IMAGE_URL = "theme/image/pink.gif";
57  	private static final String DEFAULT_INACTIVE_IMAGE_URL = "theme/image/transparent.gif";
58  
59  	public OperationArea(final int startX, final int startY, final int endX, final int endY, final String activeImageURL, final String inactiveImageURL) {
60  		this.startX = startX;
61  		this.startY = startY;
62  		this.endX = endX;
63  		this.endY = endY;
64  		this.activeImageURL = activeImageURL;
65  		this.inactiveImageURL = inactiveImageURL;
66  
67  		setUrl(this.inactiveImageURL);
68  		setStyleName("operation-area");
69  		setPixelSize(getWidth(), getHeight());
70  
71  		addMouseOverHandler(this);
72  		addMouseOutHandler(this);
73  	}
74  
75  	public OperationArea(final int startX, final int startY, final int endX, final int endY) {
76  		this(startX, startY, endX, endY, DEFAULT_ACTIVE_IMAGE_URL, DEFAULT_INACTIVE_IMAGE_URL);
77  	}
78  
79  	public static OperationArea newInstance(final Node rectAreaNode) {
80  		final String nodeName = rectAreaNode.getNodeName();
81  		if (!nodeName.equals("rect_area"))
82  			throw new IllegalArgumentException(nodeName);
83  
84  		// set area
85  		final String rectStr = Utilities.getAttributeValue(rectAreaNode, "rect");
86  		{
87  			final String _trimmedStr = rectStr.trim();
88  			final String trimmedStr = _trimmedStr.substring(1, _trimmedStr.length() - 1);
89  
90  			final String[] elements = trimmedStr.split(",");
91  
92  			final int startX = (int) (Math.floor(Double.parseDouble(elements[0])));
93  			final int startY = (int) (Math.floor(Double.parseDouble(elements[1])));
94  			final int endX = (int) (Math.ceil(Double.parseDouble(elements[2])));
95  			final int endY = (int) (Math.ceil(Double.parseDouble(elements[3])));
96  
97  			return new OperationArea(startX, startY, endX, endY);
98  		}
99  	}
100 
101 	public int getStartX() {
102 		return startX;
103 	}
104 
105 	public int getStartY() {
106 		return startY;
107 	}
108 
109 	public int getEndX() {
110 		return endX;
111 	}
112 
113 	public int getEndY() {
114 		return endY;
115 	}
116 
117 	public int getWidth() {
118 		return endX - startX;
119 	}
120 
121 	public int getHeight() {
122 		return endY - startY;
123 	}
124 
125 	public void addEventHandler(final EventHandler eventHandler) {
126 		if (eventHandler instanceof MouseOverHandler) {
127 			final MouseOverHandler mouseHandler = (MouseOverHandler) eventHandler;
128 			addMouseOverHandler(mouseHandler);
129 		}
130 		if (eventHandler instanceof MouseOutHandler) {
131 			final MouseOutHandler mouseHandler = (MouseOutHandler) eventHandler;
132 			addMouseOutHandler(mouseHandler);
133 		}
134 
135 		if (eventHandler instanceof ClickHandler) {
136 			final ClickHandler clickHandler = (ClickHandler) eventHandler;
137 			addClickHandler(clickHandler);
138 		}
139 	}
140 
141 	public void onMouseOver(MouseOverEvent e) {
142 		DOM.setCapture(getElement());
143 		setActive();
144 	}
145 
146 	public void onMouseOut(MouseOutEvent e) {
147 		setInactive();
148 		DOM.releaseCapture(getElement());
149 	}
150 
151 	public static void add(final AbsolutePanel absolutePanel, final OperationArea operationArea) {
152 		absolutePanel.add(operationArea, operationArea.getStartX(), operationArea.getStartY());
153 	}
154 
155 	protected void setActive() {
156 		setUrl(activeImageURL);
157 	}
158 
159 	protected void setInactive() {
160 		setUrl(inactiveImageURL);
161 	}
162 }