| 1 |
|
| 2 |
//============================================================================ |
| 3 |
// |
| 4 |
// file: AmapPanel.java |
| 5 |
// |
| 6 |
// Copyright (c) 2007, Michael E. Smoot |
| 7 |
// |
| 8 |
// This program is free software; you can redistribute it and/or modify it |
| 9 |
// under the terms of the GNU General Public License as published by the |
| 10 |
// Free Software Foundation; either version 2 of the License, or (at your |
| 11 |
// option) any later version. |
| 12 |
// |
| 13 |
// This program is distributed in the hope that it will be useful, but |
| 14 |
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 |
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 16 |
// for more details. |
| 17 |
// |
| 18 |
// You should have received a copy of the GNU General Public License along |
| 19 |
// with this program; if not, write to the Free Software Foundation, Inc., |
| 20 |
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 |
// |
| 22 |
//============================================================================ |
| 23 |
|
| 24 |
package amap; |
| 25 |
|
| 26 |
import java.awt.*; |
| 27 |
import java.awt.event.*; |
| 28 |
import javax.swing.*; |
| 29 |
import javax.swing.event.*; |
| 30 |
import javax.swing.text.NumberFormatter; |
| 31 |
import java.beans.*; |
| 32 |
import java.text.DecimalFormat; |
| 33 |
import java.util.Map; |
| 34 |
import java.util.HashMap; |
| 35 |
|
| 36 |
public class AmapPanel extends JPanel |
| 37 |
implements ChangeListener, PropertyChangeListener, ActionListener { |
| 38 |
|
| 39 |
AlignmentPanel alignPanel; |
| 40 |
JFormattedTextField indexField; |
| 41 |
JFormattedTextField weightField; |
| 42 |
JSlider alignSlider; |
| 43 |
java.util.List<Alignment> aligns; |
| 44 |
JButton nextButton; |
| 45 |
JButton prevButton; |
| 46 |
JButton startStopButton; |
| 47 |
int maxSliderVal; |
| 48 |
Map<JFormattedTextField,Boolean> ok2update; |
| 49 |
Timer timer; |
| 50 |
boolean frozen = true; |
| 51 |
int delay = 200; |
| 52 |
|
| 53 |
public AmapPanel(java.util.List<Alignment> aligns) { |
| 54 |
this.aligns = aligns; |
| 55 |
maxSliderVal = aligns.size()-1; |
| 56 |
|
| 57 |
ok2update = new HashMap<JFormattedTextField,Boolean>(); |
| 58 |
|
| 59 |
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
| 60 |
|
| 61 |
// set up animation timer |
| 62 |
timer = new Timer(delay,this); |
| 63 |
timer.setCoalesce(true); |
| 64 |
|
| 65 |
//Create the label. |
| 66 |
JLabel sliderLabel = new JLabel("Alignment: ", JLabel.CENTER); |
| 67 |
sliderLabel.setAlignmentX(Component.CENTER_ALIGNMENT); |
| 68 |
|
| 69 |
//Create the alignment number field |
| 70 |
java.text.NumberFormat intFormat = java.text.NumberFormat.getIntegerInstance(); |
| 71 |
NumberFormatter intFormatter = new NumberFormatter(intFormat); |
| 72 |
intFormatter.setMinimum(new Integer(0)); |
| 73 |
intFormatter.setMaximum(new Integer(maxSliderVal)); |
| 74 |
indexField = new JFormattedTextField(intFormatter); |
| 75 |
indexField.setColumns(5); //get some space |
| 76 |
indexField.addPropertyChangeListener( this ); |
| 77 |
handleEnterKeyStroke( indexField ); |
| 78 |
ok2update.put(indexField,false); |
| 79 |
|
| 80 |
JLabel weightLabel = new JLabel("Weight: ", JLabel.CENTER); |
| 81 |
|
| 82 |
//Create the alignment weight field |
| 83 |
java.text.NumberFormat numFormat = java.text.NumberFormat.getNumberInstance(); |
| 84 |
NumberFormatter numFormatter = new NumberFormatter(numFormat); |
| 85 |
numFormatter.setMinimum(new Float(0f)); |
| 86 |
numFormatter.setMaximum(new Float(Float.MAX_VALUE)); |
| 87 |
weightField = new JFormattedTextField(numFormatter); |
| 88 |
weightField.setColumns(10); //get some space |
| 89 |
weightField.addPropertyChangeListener( this ); |
| 90 |
handleEnterKeyStroke( weightField ); |
| 91 |
ok2update.put(weightField,false); |
| 92 |
|
| 93 |
// create next button |
| 94 |
nextButton = new JButton("Next"); |
| 95 |
nextButton.addActionListener(this); |
| 96 |
|
| 97 |
startStopButton = new JButton("Start Animation"); |
| 98 |
startStopButton.addActionListener(this); |
| 99 |
|
| 100 |
// create prev button |
| 101 |
prevButton = new JButton("Prev"); |
| 102 |
prevButton.addActionListener(this); |
| 103 |
|
| 104 |
//Create the slider. |
| 105 |
alignSlider = new JSlider(JSlider.HORIZONTAL, 0, maxSliderVal, 0); |
| 106 |
alignSlider.addChangeListener(this); |
| 107 |
|
| 108 |
//Turn on labels at major tick marks. |
| 109 |
alignSlider.setMajorTickSpacing(calcTickSpacing(maxSliderVal)); |
| 110 |
alignSlider.setPaintTicks(true); |
| 111 |
alignSlider.setPaintLabels(true); |
| 112 |
|
| 113 |
//Create the label that displays the animation. |
| 114 |
alignPanel = new AlignmentPanel(aligns,450); |
| 115 |
alignPanel.setBorder(BorderFactory.createCompoundBorder( |
| 116 |
BorderFactory.createLoweredBevelBorder(), |
| 117 |
BorderFactory.createEmptyBorder(10,10,10,10))); |
| 118 |
updatePicture(0); //display first frame |
| 119 |
|
| 120 |
// set up scrolling |
| 121 |
JScrollPane alignScroll = new JScrollPane(alignPanel); |
| 122 |
alignScroll.setPreferredSize(new Dimension(500,700)); |
| 123 |
alignScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
| 124 |
alignScroll.addComponentListener(alignPanel); |
| 125 |
|
| 126 |
//Create a subpanel for the label and text field. |
| 127 |
JPanel labelAndTextField = new JPanel(); //use FlowLayout |
| 128 |
labelAndTextField.add(prevButton); |
| 129 |
labelAndTextField.add(startStopButton); |
| 130 |
labelAndTextField.add(nextButton); |
| 131 |
labelAndTextField.add(sliderLabel); |
| 132 |
labelAndTextField.add(indexField); |
| 133 |
labelAndTextField.add(weightLabel); |
| 134 |
labelAndTextField.add(weightField); |
| 135 |
|
| 136 |
//Put everything together. |
| 137 |
add(alignScroll); |
| 138 |
add(alignSlider); |
| 139 |
add(labelAndTextField); |
| 140 |
setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); |
| 141 |
|
| 142 |
update(maxSliderVal); |
| 143 |
} |
| 144 |
|
| 145 |
public void stateChanged(ChangeEvent e) { |
| 146 |
JSlider source = (JSlider)e.getSource(); |
| 147 |
if ( source == alignSlider ) |
| 148 |
update((int)alignSlider.getValue()); |
| 149 |
} |
| 150 |
|
| 151 |
private void update(int index) { |
| 152 |
updatePicture(index); |
| 153 |
alignSlider.setValue(index); |
| 154 |
indexField.setText(Integer.toString(index)); |
| 155 |
weightField.setText(Double.toString(aligns.get(index).getWeight())); |
| 156 |
PropertyChangeHandler.firePropertyChange(PropertyChangeIDs.CHANGE_ALIGNMENT.toString(), |
| 157 |
null, aligns.get(index)); |
| 158 |
} |
| 159 |
|
| 160 |
public void propertyChange(PropertyChangeEvent e) { |
| 161 |
if (!"value".equals(e.getPropertyName())) |
| 162 |
return; |
| 163 |
//new Exception().printStackTrace(); |
| 164 |
//System.out.println("pc weightfield: " + ok2update.get(weightField)); |
| 165 |
//System.out.println("pc indexfield: " + ok2update.get(indexField)); |
| 166 |
//System.out.print("prop change: " + e.getNewValue() + " " ); |
| 167 |
if ( e.getSource() == indexField && ok2update.get(indexField) ) { |
| 168 |
// System.out.println("index" ); |
| 169 |
Number value = (Number)e.getNewValue(); |
| 170 |
if (value != null) { |
| 171 |
update(value.intValue()); |
| 172 |
ok2update.put(indexField,false); |
| 173 |
// System.out.println("index alignSilder val: " + alignSlider.getValue()); |
| 174 |
} |
| 175 |
} else if ( e.getSource() == weightField && ok2update.get(weightField) ) { |
| 176 |
//System.out.println("weight" ); |
| 177 |
Float weight = (Float)e.getNewValue(); |
| 178 |
int index = getIndexFromWeight( weight.floatValue() ); |
| 179 |
if (index >= 0 && index <= maxSliderVal) { |
| 180 |
update(index); |
| 181 |
ok2update.put(weightField,false); |
| 182 |
//System.out.println("weight alignSilder val: " + alignSlider.getValue()); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
public void actionPerformed(ActionEvent e) { |
| 188 |
int curr = (int)alignSlider.getValue(); |
| 189 |
//System.out.println("curr val: " + curr); |
| 190 |
if ( e.getSource() == nextButton || e.getSource() == timer ) { |
| 191 |
if ( curr == maxSliderVal ) |
| 192 |
update(0); |
| 193 |
else |
| 194 |
update(curr+1); |
| 195 |
} else if ( e.getSource() == prevButton ) { |
| 196 |
if ( curr == 0 ) |
| 197 |
update(maxSliderVal); |
| 198 |
else |
| 199 |
update(curr-1); |
| 200 |
} else if ( e.getSource() == startStopButton ) { |
| 201 |
if ( frozen ) { |
| 202 |
timer.start(); |
| 203 |
frozen = false; |
| 204 |
startStopButton.setText("Stop Animation"); |
| 205 |
} else { |
| 206 |
timer.stop(); |
| 207 |
frozen = true; |
| 208 |
startStopButton.setText("Start Animation"); |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
protected void updatePicture(int frameNum) { |
| 214 |
alignPanel.setIndex(frameNum); |
| 215 |
repaint(); |
| 216 |
} |
| 217 |
|
| 218 |
protected int calcTickSpacing(int val) { |
| 219 |
return ((val/10) - (val%10)); |
| 220 |
} |
| 221 |
|
| 222 |
private int getIndexFromWeight(float w) { |
| 223 |
return bSearch(0,aligns.size()-1,w); |
| 224 |
} |
| 225 |
|
| 226 |
private int bSearch(int low, int high, float weight) { |
| 227 |
//System.out.println(low + " " + high + " " + weight); |
| 228 |
if ( high < low ) |
| 229 |
return -1; |
| 230 |
if ( weight <= aligns.get(high).getWeight() ) |
| 231 |
return high; |
| 232 |
if ( weight >= aligns.get(low).getWeight() ) |
| 233 |
return low; |
| 234 |
if ( low == high - 1 ) |
| 235 |
return low; |
| 236 |
int mid = (high + low) / 2; |
| 237 |
if ( weight < aligns.get(mid).getWeight() ) |
| 238 |
return bSearch(mid,high,weight); |
| 239 |
else if ( weight > aligns.get(mid).getWeight() ) |
| 240 |
return bSearch(low,mid,weight); |
| 241 |
else |
| 242 |
return mid; |
| 243 |
} |
| 244 |
|
| 245 |
//React when the user presses Enter. |
| 246 |
private void handleEnterKeyStroke( final JFormattedTextField field ) { |
| 247 |
field.getInputMap().put(KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0), "check"); |
| 248 |
field.getActionMap().put("check", new AbstractAction() { |
| 249 |
public void actionPerformed(ActionEvent e) { |
| 250 |
//The text is invalid. |
| 251 |
if (!field.isEditValid()) { |
| 252 |
Toolkit.getDefaultToolkit().beep(); |
| 253 |
field.selectAll(); |
| 254 |
//The text is valid, so use it. |
| 255 |
} else try { |
| 256 |
//System.out.println("commitEdit"); |
| 257 |
ok2update.put(field,true); |
| 258 |
//System.out.println("weightfield: " + ok2update.get(weightField)); |
| 259 |
//System.out.println("indexfield: " + ok2update.get(indexField)); |
| 260 |
field.commitEdit(); |
| 261 |
} catch (java.text.ParseException exc) { } |
| 262 |
} |
| 263 |
}); |
| 264 |
} |
| 265 |
} |