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  // OldUTGBFrameOperationImpl.java
20  // Since: 2007/06/21
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track.lib.old;
26  
27  import org.utgenome.gwt.utgb.client.track.Track;
28  import org.utgenome.gwt.utgb.client.track.TrackGroup;
29  import org.utgenome.gwt.utgb.client.track.TrackGroupPropertyWriter;
30  import org.utgenome.gwt.utgb.client.track.TrackWindow;
31  import org.utgenome.gwt.utgb.client.track.operation.FrameCommand;
32  
33  import com.google.gwt.xml.client.Node;
34  
35  public class OldUTGBFrameCommandImpl implements FrameCommand {
36  
37  	public static FrameCommand newInstance(final Node frameCommandNode) {
38  		final String name = Utilities.getAttributeValue(frameCommandNode, "name");
39  
40  		if (name.equals("moverel")) {
41  			final int value = Integer.parseInt(Utilities.getAttributeValue(frameCommandNode, "value", "0"));
42  
43  			return new FrameMoveRelCommand(value);
44  		}
45  		else if (name.equals("moveabs")) {
46  			final int value = Integer.parseInt(Utilities.getAttributeValue(frameCommandNode, "value", "0"));
47  
48  			return new FrameMoveAbsCommand(value);
49  		}
50  		else if (name.equals("zoomrel")) {
51  			throw new UnsupportedOperationException("zoomrel");
52  		}
53  		else if (name.equals("zoomabs")) {
54  			final int value = Integer.parseInt(Utilities.getAttributeValue(frameCommandNode, "value", "10000"));
55  
56  			return new FrameZoomAbsCommand(value);
57  		}
58  		else if (name.equals("rev")) {
59  			return new FrameRevCommand();
60  		}
61  		else if (name.equals("setspecies")) {
62  			final String value = Utilities.getAttributeValue(frameCommandNode, "value");
63  
64  			if (value != null)
65  				return new FrameSetSpeciesCommand(value);
66  		}
67  		else if (name.equals("setrevision")) {
68  			final String value = Utilities.getAttributeValue(frameCommandNode, "value");
69  
70  			if (value != null)
71  				return new FrameSetRevisionCommand(value);
72  		}
73  		else if (name.equals("settarget")) {
74  			final String value = Utilities.getAttributeValue(frameCommandNode, "value");
75  
76  			if (value != null)
77  				return new FrameSetTargetCommand(value);
78  		}
79  		else if (name.equals("setwidth")) {
80  			throw new UnsupportedOperationException("setwidth");
81  		}
82  		else if (name.equals("setstart")) {
83  			final int value = Integer.parseInt(Utilities.getAttributeValue(frameCommandNode, "value", "1"));
84  
85  			return new FrameSetStartCommand(value);
86  		}
87  		else if (name.equals("setend")) {
88  			final int value = Integer.parseInt(Utilities.getAttributeValue(frameCommandNode, "value", "1"));
89  
90  			return new FrameSetEndCommand(value);
91  		}
92  
93  		throw new UnsupportedOperationException(name);
94  	}
95  
96  	public void execute(Track track) {
97  	}
98  
99  	public static class FrameMoveRelCommand implements FrameCommand {
100 		private final int step;
101 
102 		public FrameMoveRelCommand(final int step) {
103 			this.step = step;
104 		}
105 
106 		public void execute(final Track track) {
107 			final TrackGroup trackGroup = track.getTrackGroup();
108 
109 			final TrackWindow trackWindow = trackGroup.getTrackWindow();
110 
111 			final int newStartIndex = trackWindow.getStartOnGenome() + step;
112 			final int newEndIndex = trackWindow.getEndOnGenome() + step;
113 
114 			trackGroup.setTrackWindowLocation(newStartIndex, newEndIndex);
115 		}
116 	}
117 
118 	public static class FrameMoveAbsCommand implements FrameCommand {
119 		private final int centerIndex;
120 
121 		public FrameMoveAbsCommand(final int centerIndex) {
122 			this.centerIndex = centerIndex;
123 		}
124 
125 		public void execute(final Track track) {
126 			final TrackGroup trackGroup = track.getTrackGroup();
127 
128 			final TrackWindow trackWindow = trackGroup.getTrackWindow();
129 
130 			final int oldStartIndex = trackWindow.getStartOnGenome();
131 			final int oldEndIndex = trackWindow.getEndOnGenome();
132 
133 			final int oldCenterIndex = (oldStartIndex + oldEndIndex) / 2;
134 
135 			final int step = centerIndex - oldCenterIndex;
136 
137 			final int newStartIndex = oldStartIndex + step;
138 			final int newEndIndex = oldEndIndex + step;
139 
140 			trackGroup.setTrackWindowLocation(newStartIndex, newEndIndex);
141 		}
142 	}
143 
144 	public static class FrameZoomAbsCommand implements FrameCommand {
145 		private final int width;
146 
147 		public FrameZoomAbsCommand(final int width) {
148 			this.width = width;
149 		}
150 
151 		public void execute(final Track track) {
152 			final TrackGroup trackGroup = track.getTrackGroup();
153 
154 			final TrackWindow trackWindow = trackGroup.getTrackWindow();
155 
156 			final int oldStartIndex = trackWindow.getStartOnGenome();
157 			final int oldEndIndex = trackWindow.getEndOnGenome();
158 
159 			final int oldCenterIndex = (oldStartIndex + oldEndIndex) / 2;
160 
161 			final int step = width / 2;
162 
163 			final int _diff = oldEndIndex - oldStartIndex;
164 			final boolean isPlus = _diff >= 0;
165 
166 			final int coeff = isPlus ? +1 : -1;
167 
168 			final int newStartIndex = oldCenterIndex - coeff * step;
169 			final int newEndIndex = oldCenterIndex + coeff * step;
170 
171 			trackGroup.setTrackWindowLocation(newStartIndex, newEndIndex);
172 		}
173 	}
174 
175 	public static class FrameRevCommand implements FrameCommand {
176 		public void execute(final Track track) {
177 			final TrackGroup trackGroup = track.getTrackGroup();
178 
179 			final TrackWindow trackWindow = trackGroup.getTrackWindow();
180 
181 			final int oldStartIndex = trackWindow.getStartOnGenome();
182 			final int oldEndIndex = trackWindow.getEndOnGenome();
183 
184 			trackGroup.setTrackWindowLocation(oldEndIndex, oldStartIndex);
185 		}
186 	}
187 
188 	public static class FrameParameterSetCommand implements FrameCommand {
189 		protected final String key;
190 		protected final String value;
191 
192 		public FrameParameterSetCommand(final String key, final String value) {
193 			this.key = key;
194 			this.value = value;
195 		}
196 
197 		public void execute(final Track track) {
198 			final TrackGroup trackGroup = track.getTrackGroup();
199 
200 			final TrackGroupPropertyWriter propertyWriter = trackGroup.getPropertyWriter();
201 
202 			propertyWriter.setProperty(key, value);
203 		}
204 	}
205 
206 	public static class FrameSetSpeciesCommand extends FrameParameterSetCommand {
207 		public FrameSetSpeciesCommand(final String newSpecies) {
208 			super(OldUTGBProperty.SPECIES, newSpecies);
209 		}
210 	}
211 
212 	public static class FrameSetRevisionCommand extends FrameParameterSetCommand {
213 		public FrameSetRevisionCommand(final String newRevision) {
214 			super(OldUTGBProperty.REVISION, newRevision);
215 		}
216 	}
217 
218 	public static class FrameSetTargetCommand extends FrameParameterSetCommand {
219 		public FrameSetTargetCommand(final String newTarget) {
220 			super(OldUTGBProperty.TARGET, newTarget);
221 		}
222 	}
223 
224 	public static class FrameSetStartCommand implements FrameCommand {
225 		private final int newStartIndex;
226 
227 		public FrameSetStartCommand(final int newStartIndex) {
228 			this.newStartIndex = newStartIndex;
229 		}
230 
231 		public void execute(final Track track) {
232 			final TrackGroup trackGroup = track.getTrackGroup();
233 
234 			final TrackWindow trackWindow = trackGroup.getTrackWindow();
235 
236 			trackGroup.setTrackWindowLocation(newStartIndex, trackWindow.getEndOnGenome());
237 		}
238 	}
239 
240 	public static class FrameSetEndCommand implements FrameCommand {
241 		private final int newEndIndex;
242 
243 		public FrameSetEndCommand(final int newEndIndex) {
244 			this.newEndIndex = newEndIndex;
245 		}
246 
247 		public void execute(final Track track) {
248 			final TrackGroup trackGroup = track.getTrackGroup();
249 
250 			final TrackWindow trackWindow = trackGroup.getTrackWindow();
251 
252 			trackGroup.setTrackWindowLocation(trackWindow.getStartOnGenome(), newEndIndex);
253 		}
254 	}
255 }