1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package org.openoffice.accessibility.awb.view.text; 25 26 import java.lang.Integer; 27 import java.util.Vector; 28 import javax.swing.SpinnerModel; 29 import javax.swing.event.ChangeEvent; 30 import javax.swing.event.ChangeListener; 31 32 import com.sun.star.accessibility.XAccessibleText; 33 import com.sun.star.lang.IndexOutOfBoundsException; 34 35 36 /** A simple model for JSpinner objects that clips the spinner values to valid 37 text indices. 38 */ 39 public class CaretSpinnerModel 40 implements SpinnerModel 41 { CaretSpinnerModel(XAccessibleText xText)42 public CaretSpinnerModel (XAccessibleText xText) 43 { 44 mxText = xText; 45 maListeners = new Vector (); 46 } 47 addChangeListener(ChangeListener aListener)48 public void addChangeListener (ChangeListener aListener) 49 { 50 if (aListener != null) 51 maListeners.add (aListener); 52 } 53 removeChangeListener(ChangeListener aListener)54 public void removeChangeListener (ChangeListener aListener) 55 { 56 maListeners.removeElement (aListener); 57 } 58 getNextValue()59 public Object getNextValue () 60 { 61 if (mxText != null) 62 { 63 int nPosition = mxText.getCaretPosition(); 64 if (nPosition+1 <= mxText.getCharacterCount()) 65 return new Integer (nPosition+1); 66 } 67 return null; 68 } 69 getPreviousValue()70 public Object getPreviousValue () 71 { 72 if (mxText != null) 73 { 74 int nPosition = mxText.getCaretPosition(); 75 if (nPosition > 0) 76 return new Integer (nPosition-1); 77 } 78 return null; 79 } 80 getValue()81 public Object getValue () 82 { 83 if (mxText != null) 84 return new Integer (mxText.getCaretPosition()); 85 else 86 return null; 87 } 88 setValue(Object aValue)89 public void setValue (Object aValue) 90 { 91 if (mxText != null) 92 if (aValue instanceof Integer) 93 { 94 try 95 { 96 if( ((Integer)aValue).intValue() != mxText.getCaretPosition() ) 97 mxText.setCaretPosition (((Integer)aValue).intValue()); 98 } 99 catch (IndexOutOfBoundsException aException) 100 { 101 } 102 } 103 } 104 105 /** Call this method when the caret position has changes so that the model 106 can inform its listeners about it. 107 */ Update()108 public void Update () 109 { 110 ChangeEvent aEvent = new ChangeEvent (this); 111 for (int i=0; i<maListeners.size(); i++) 112 ((ChangeListener)maListeners.elementAt(i)).stateChanged (aEvent); 113 } 114 115 private XAccessibleText mxText; 116 private Integer maValue; 117 private Vector maListeners; 118 } 119