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  // ParameterTrack.java
20  // Since: 2007/06/15
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track.lib;
26  
27  import java.util.ArrayList;
28  
29  import org.utgenome.gwt.utgb.client.track.Track;
30  import org.utgenome.gwt.utgb.client.track.TrackBase;
31  import org.utgenome.gwt.utgb.client.track.TrackFrame;
32  import org.utgenome.gwt.utgb.client.track.TrackGroup;
33  import org.utgenome.gwt.utgb.client.track.TrackGroupProperty;
34  import org.utgenome.gwt.utgb.client.track.TrackGroupPropertyChange;
35  import org.utgenome.gwt.utgb.client.track.TrackGroupPropertyWriter;
36  
37  import com.google.gwt.event.dom.client.KeyCodes;
38  import com.google.gwt.event.dom.client.KeyUpEvent;
39  import com.google.gwt.event.dom.client.KeyUpHandler;
40  import com.google.gwt.user.client.ui.FlexTable;
41  import com.google.gwt.user.client.ui.TextBox;
42  import com.google.gwt.user.client.ui.Widget;
43  
44  /**
45   * 
46   * @author ssksn
47   * 
48   */
49  public class ParameterTrack extends TrackBase {
50  	public static TrackFactory factory() {
51  		return new TrackFactory() {
52  			public Track newInstance() {
53  				return new ParameterTrack();
54  			}
55  		};
56  	}
57  
58  	protected ArrayList<KeyAndEntry> entries = new ArrayList<KeyAndEntry>();
59  
60  	protected FlexTable gridPanel = new FlexTable();
61  
62  	public ParameterTrack() {
63  		super("Parameter Track");
64  		gridPanel.setStyleName("parameter-track");
65  		gridPanel.setWidth("100%");
66  	}
67  
68  	// @see org.utgenome.gwt.utgb.client.track.Track#getWidget()
69  	public Widget getWidget() {
70  		return gridPanel;
71  	}
72  
73  	public int getDefaultWindowHeight() {
74  		return 80;
75  	}
76  
77  	public void addParameter(final String key) {
78  		addParameter(key, key);
79  	}
80  
81  	public void addParameter(final String key, final String displayName) {
82  		entries.add(new KeyAndEntry(key, displayName));
83  	}
84  
85  	class KeyAndEntry {
86  		private String key;
87  		private String entry;
88  
89  		public KeyAndEntry(final String key, final String entry) {
90  			this.key = key;
91  			this.entry = entry;
92  		}
93  
94  		public String getKey() {
95  			return key;
96  		}
97  
98  		public String getEntry() {
99  			return entry;
100 		}
101 	}
102 
103 	public void draw() {
104 		gridPanel.clear();
105 
106 		final TrackGroupProperty propertyReader = getTrackGroup().getPropertyReader();
107 		final TrackGroupPropertyWriter propertyWriter = getTrackGroup().getPropertyWriter();
108 
109 		final int SIZE = entries.size();
110 		for (int i = 0; i < SIZE; i++) {
111 			final KeyAndEntry entry = (entries.get(i));
112 
113 			final String key = entry.getKey();
114 			final String displayName = entry.getEntry();
115 
116 			final String propertyValue = propertyReader.getProperty(key);
117 
118 			gridPanel.setText(i / 3, (2 * i) % 6, displayName);
119 			final TextBox textBox = new TextBox();
120 			textBox.setText(propertyValue);
121 			textBox.addKeyUpHandler(new KeyUpHandler() {
122 
123 				public void onKeyUp(KeyUpEvent e) {
124 					if (e.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
125 						propertyWriter.setProperty(key, textBox.getText());
126 					}
127 				}
128 			});
129 			gridPanel.setWidget(i / 3, (2 * i + 1) % 6, textBox);
130 		}
131 	}
132 
133 	public void onChangeTrackGroupProperty(final TrackGroupPropertyChange change) {
134 		final int SIZE = entries.size();
135 		final String[] array = new String[SIZE];
136 		for (int i = 0; i < SIZE; i++) {
137 			final KeyAndEntry entry = (entries.get(i));
138 
139 			final String key = entry.getKey();
140 			array[i] = key;
141 		}
142 
143 		if (change.containsOneOf(array))
144 			draw();
145 	}
146 
147 	public void setUp(TrackFrame trackFrame, TrackGroup group) {
148 		trackFrame.pack();
149 	}
150 
151 }