Changeset 358 for trunk/loci/visbio/view


Ignore:
Timestamp:
04/29/05 14:22:52 (15 years ago)
Author:
curtis
Message:

Prepare infrastructure for new state logic in simple XML format.

Location:
trunk/loci/visbio/view
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/visbio/view/CaptureHandler.java

    r303 r358  
    3636import loci.visbio.SystemManager; 
    3737import loci.visbio.WindowManager; 
    38 import loci.visbio.state.OptionManager; 
    39 import loci.visbio.state.StateManager; 
     38import loci.visbio.state.*; 
    4039import loci.visbio.util.VisUtil; 
     40import org.w3c.dom.Element; 
    4141import visad.*; 
    4242import visad.data.avi.AVIForm; 
     
    4444 
    4545/** Provides logic for capturing display screenshots and movies. */ 
    46 public class CaptureHandler { 
     46public class CaptureHandler implements Saveable { 
    4747 
    4848  // -- Fields - GUI components -- 
     
    349349  // -- CaptureHandler API methods - state logic -- 
    350350 
    351   /** Writes the current state. */ 
    352   public void saveState() { 
    353     CaptureWindow captureWindow = panel.getCaptureWindow(); 
    354     Vector pos = captureWindow.getPositions(); 
    355     int speed = captureWindow.getSpeed(); 
    356     int fps = captureWindow.getFPS(); 
    357     boolean smooth = captureWindow.isSmooth(); 
    358  
    359     // save display positions 
    360     int numPositions = pos.size(); 
    361     window.setAttr("positions", "" + numPositions); 
    362     for (int i=0; i<numPositions; i++) { 
    363       DisplayPosition position = (DisplayPosition) pos.elementAt(i); 
    364       position.saveState(window, "position" + i); 
    365     } 
    366  
    367     // save other parameters 
    368     window.setAttr("movieSpeed", "" + speed); 
    369     window.setAttr("movieFPS", "" + fps); 
    370     window.setAttr("movieSmooth", "" + smooth); 
    371   } 
    372  
    373   /** Restores the current state. */ 
    374   public void restoreState() { 
    375     int numPositions = Integer.parseInt(window.getAttr("positions")); 
    376     Vector vn = new Vector(numPositions); 
    377     for (int i=0; i<numPositions; i++) { 
    378       DisplayPosition position = new DisplayPosition(); 
    379       position.restoreState(window, "position" + i); 
    380       vn.add(position); 
    381     } 
    382     Vector vo = getPositions(); 
    383     if (vo != null) StateManager.mergeStates(vo, vn); 
    384  
    385     movieSpeed = Integer.parseInt(window.getAttr("movieSpeed")); 
    386     movieFPS = Integer.parseInt(window.getAttr("movieFPS")); 
    387     movieSmooth = window.getAttr("movieSmooth").equalsIgnoreCase("true"); 
    388   } 
    389  
    390351  /** Tests whether two objects are in equivalent states. */ 
    391352  public boolean matches(CaptureHandler handler) { 
     
    447408 
    448409 
     410  // -- Saveable API methods -- 
     411 
     412  /** Writes the current state to the given DOM element ("Display"). */ 
     413  public void saveState(Element el) throws SaveException { 
     414    /* CTR TODO for v3.00 final 
     415    CaptureWindow captureWindow = panel.getCaptureWindow(); 
     416    Vector pos = captureWindow.getPositions(); 
     417    int speed = captureWindow.getSpeed(); 
     418    int fps = captureWindow.getFPS(); 
     419    boolean smooth = captureWindow.isSmooth(); 
     420 
     421    // save display positions 
     422    int numPositions = pos.size(); 
     423    window.setAttr("positions", "" + numPositions); 
     424    for (int i=0; i<numPositions; i++) { 
     425      DisplayPosition position = (DisplayPosition) pos.elementAt(i); 
     426      position.saveState(window, "position" + i); 
     427    } 
     428 
     429    // save other parameters 
     430    window.setAttr("movieSpeed", "" + speed); 
     431    window.setAttr("movieFPS", "" + fps); 
     432    window.setAttr("movieSmooth", "" + smooth); 
     433    */ 
     434  } 
     435 
     436  /** Restores the current state from the given DOM element ("Display"). */ 
     437  public void restoreState(Element el) throws SaveException { 
     438    /* CTR TODO for v3.00 final 
     439    int numPositions = Integer.parseInt(window.getAttr("positions")); 
     440    Vector vn = new Vector(numPositions); 
     441    for (int i=0; i<numPositions; i++) { 
     442      DisplayPosition position = new DisplayPosition(); 
     443      position.restoreState(window, "position" + i); 
     444      vn.add(position); 
     445    } 
     446    Vector vo = getPositions(); 
     447    if (vo != null) StateManager.mergeStates(vo, vn); 
     448 
     449    movieSpeed = Integer.parseInt(window.getAttr("movieSpeed")); 
     450    movieFPS = Integer.parseInt(window.getAttr("movieFPS")); 
     451    movieSmooth = window.getAttr("movieSmooth").equalsIgnoreCase("true"); 
     452    */ 
     453  } 
     454 
    449455  // -- Helper methods -- 
    450456 
  • trunk/loci/visbio/view/ColorHandler.java

    r186 r358  
    2828import loci.visbio.VisBioFrame; 
    2929import loci.visbio.data.*; 
     30import loci.visbio.state.SaveException; 
    3031import loci.visbio.util.*; 
     32import org.w3c.dom.Element; 
    3133import visad.*; 
    3234 
     
    306308  // -- ColorHandler API methods - state logic -- 
    307309 
    308   /** Writes the current state to the given XML object. */ 
    309   public void saveState(String attrName) { 
    310     DisplayWindow window = getWindow(); 
    311     window.setAttr(attrName + "_brightness", "" + brightness); 
    312     window.setAttr(attrName + "_contrast", "" + contrast); 
    313     window.setAttr(attrName + "_opacityValue", "" + opacityValue); 
    314     window.setAttr(attrName + "_opacityModel", "" + opacityModel); 
    315     window.setAttr(attrName + "_colorModel", "" + colorModel); 
    316  
    317     String r = red == null ? "null" : red.getName(); 
    318     String g = green == null ? "null" : green.getName(); 
    319     String b = blue == null ? "null" : blue.getName(); 
    320     window.setAttr(attrName + "_red", r); 
    321     window.setAttr(attrName + "_green", g); 
    322     window.setAttr(attrName + "_blue", b); 
    323  
    324     String min = ObjectUtil.arrayToString(getLo()); 
    325     String max = ObjectUtil.arrayToString(getHi()); 
    326     String fix = ObjectUtil.arrayToString(getFixed()); 
    327     window.setAttr(attrName + "_colorMin", min); 
    328     window.setAttr(attrName + "_colorMax", max); 
    329     window.setAttr(attrName + "_colorFixed", fix); 
    330  
    331     float[][][] tables = getTables(); 
    332     if (tables == null) { 
    333       window.setAttr(attrName + "_tables", "null"); 
    334     } 
    335     else { 
    336       window.setAttr(attrName + "_tables", "" + tables.length); 
    337       for (int i=0; i<tables.length; i++) { 
    338         if (tables[i] == null) window.setAttr(attrName + "_table" + i, "null"); 
    339         else { 
    340           window.setAttr(attrName + "_table" + i, "" + tables[i].length); 
    341           for (int j=0; j<tables[i].length; j++) { 
    342             if (tables[i][j] == null) { 
    343               window.setAttr(attrName + "_table" + i + "-" + j, "null"); 
    344             } 
    345             else { 
    346               window.setAttr(attrName + "_table" + i + "-" + j, 
    347                 ObjectUtil.arrayToString(tables[i][j])); 
    348             } 
    349           } 
    350         } 
    351       } 
    352     } 
    353   } 
    354  
    355   /** Restores the current state from the given XML object. */ 
    356   public void restoreState(String attrName) { 
    357     DisplayWindow window = getWindow(); 
    358     brightness = Integer.parseInt(window.getAttr(attrName + "_brightness")); 
    359     contrast = Integer.parseInt(window.getAttr(attrName + "_contrast")); 
    360     opacityValue = Integer.parseInt( 
    361       window.getAttr(attrName + "_opacityValue")); 
    362     opacityModel = Integer.parseInt( 
    363       window.getAttr(attrName + "_opacityModel")); 
    364     colorModel = Integer.parseInt(window.getAttr(attrName + "_colorModel")); 
    365  
    366     String r = window.getAttr(attrName + "_red"); 
    367     String g = window.getAttr(attrName + "_green"); 
    368     String b = window.getAttr(attrName + "_blue"); 
    369     red = r.equals("null") ? null : RealType.getRealType(r); 
    370     green = g.equals("null") ? null : RealType.getRealType(g); 
    371     blue = b.equals("null") ? null : RealType.getRealType(b); 
    372  
    373     String min = window.getAttr(attrName + "_colorMin"); 
    374     String max = window.getAttr(attrName + "_colorMax"); 
    375     String fix = window.getAttr(attrName + "_colorFixed"); 
    376     lo = ObjectUtil.stringToDoubleArray(min); 
    377     hi = ObjectUtil.stringToDoubleArray(max); 
    378     fixed = ObjectUtil.stringToBooleanArray(fix); 
    379  
    380     colorTables = null; 
    381     String s = window.getAttr(attrName + "_tables"); 
    382     if (!s.equals("null")) { 
    383       int ilen = Integer.parseInt(s); 
    384       colorTables = new float[ilen][][]; 
    385       for (int i=0; i<ilen; i++) { 
    386         s = window.getAttr(attrName + "_table" + i); 
    387         if (s.equals("null")) colorTables[i] = null; 
    388         else { 
    389           int jlen = Integer.parseInt(s); 
    390           colorTables[i] = new float[jlen][]; 
    391           for (int j=0; j<jlen; j++) { 
    392             colorTables[i][j] = ObjectUtil.stringToFloatArray( 
    393               window.getAttr(attrName + "_table" + i + "-" + j)); 
    394           } 
    395         } 
    396       } 
    397     } 
    398   } 
    399  
    400310  /** Tests whether two objects are in equivalent states. */ 
    401311  public boolean matches(ColorHandler handler) { 
     
    438348  } 
    439349 
     350  // -- Saveable API methods -- 
     351 
     352  /** Writes the current state to the given DOM element ("Display"). */ 
     353  public void saveState(Element el) throws SaveException { 
     354    /* CTR TODO for v3.00 final 
     355    DisplayWindow window = getWindow(); 
     356    window.setAttr(attrName + "_brightness", "" + brightness); 
     357    window.setAttr(attrName + "_contrast", "" + contrast); 
     358    window.setAttr(attrName + "_opacityValue", "" + opacityValue); 
     359    window.setAttr(attrName + "_opacityModel", "" + opacityModel); 
     360    window.setAttr(attrName + "_colorModel", "" + colorModel); 
     361 
     362    String r = red == null ? "null" : red.getName(); 
     363    String g = green == null ? "null" : green.getName(); 
     364    String b = blue == null ? "null" : blue.getName(); 
     365    window.setAttr(attrName + "_red", r); 
     366    window.setAttr(attrName + "_green", g); 
     367    window.setAttr(attrName + "_blue", b); 
     368 
     369    String min = ObjectUtil.arrayToString(getLo()); 
     370    String max = ObjectUtil.arrayToString(getHi()); 
     371    String fix = ObjectUtil.arrayToString(getFixed()); 
     372    window.setAttr(attrName + "_colorMin", min); 
     373    window.setAttr(attrName + "_colorMax", max); 
     374    window.setAttr(attrName + "_colorFixed", fix); 
     375 
     376    float[][][] tables = getTables(); 
     377    if (tables == null) { 
     378      window.setAttr(attrName + "_tables", "null"); 
     379    } 
     380    else { 
     381      window.setAttr(attrName + "_tables", "" + tables.length); 
     382      for (int i=0; i<tables.length; i++) { 
     383        if (tables[i] == null) window.setAttr(attrName + "_table" + i, "null"); 
     384        else { 
     385          window.setAttr(attrName + "_table" + i, "" + tables[i].length); 
     386          for (int j=0; j<tables[i].length; j++) { 
     387            if (tables[i][j] == null) { 
     388              window.setAttr(attrName + "_table" + i + "-" + j, "null"); 
     389            } 
     390            else { 
     391              window.setAttr(attrName + "_table" + i + "-" + j, 
     392                ObjectUtil.arrayToString(tables[i][j])); 
     393            } 
     394          } 
     395        } 
     396      } 
     397    } 
     398    */ 
     399  } 
     400 
     401  /** Restores the current state from the given DOM element ("Display"). */ 
     402  public void restoreState(Element el) throws SaveException { 
     403    /* CTR TODO for v3.00 final 
     404    DisplayWindow window = getWindow(); 
     405    brightness = Integer.parseInt(window.getAttr(attrName + "_brightness")); 
     406    contrast = Integer.parseInt(window.getAttr(attrName + "_contrast")); 
     407    opacityValue = Integer.parseInt( 
     408      window.getAttr(attrName + "_opacityValue")); 
     409    opacityModel = Integer.parseInt( 
     410      window.getAttr(attrName + "_opacityModel")); 
     411    colorModel = Integer.parseInt(window.getAttr(attrName + "_colorModel")); 
     412 
     413    String r = window.getAttr(attrName + "_red"); 
     414    String g = window.getAttr(attrName + "_green"); 
     415    String b = window.getAttr(attrName + "_blue"); 
     416    red = r.equals("null") ? null : RealType.getRealType(r); 
     417    green = g.equals("null") ? null : RealType.getRealType(g); 
     418    blue = b.equals("null") ? null : RealType.getRealType(b); 
     419 
     420    String min = window.getAttr(attrName + "_colorMin"); 
     421    String max = window.getAttr(attrName + "_colorMax"); 
     422    String fix = window.getAttr(attrName + "_colorFixed"); 
     423    lo = ObjectUtil.stringToDoubleArray(min); 
     424    hi = ObjectUtil.stringToDoubleArray(max); 
     425    fixed = ObjectUtil.stringToBooleanArray(fix); 
     426 
     427    colorTables = null; 
     428    String s = window.getAttr(attrName + "_tables"); 
     429    if (!s.equals("null")) { 
     430      int ilen = Integer.parseInt(s); 
     431      colorTables = new float[ilen][][]; 
     432      for (int i=0; i<ilen; i++) { 
     433        s = window.getAttr(attrName + "_table" + i); 
     434        if (s.equals("null")) colorTables[i] = null; 
     435        else { 
     436          int jlen = Integer.parseInt(s); 
     437          colorTables[i] = new float[jlen][]; 
     438          for (int j=0; j<jlen; j++) { 
     439            colorTables[i][j] = ObjectUtil.stringToFloatArray( 
     440              window.getAttr(attrName + "_table" + i + "-" + j)); 
     441          } 
     442        } 
     443      } 
     444    } 
     445    */ 
     446  } 
     447 
    440448} 
  • trunk/loci/visbio/view/DisplayManager.java

    r351 r358  
    2525 
    2626import java.awt.Component; 
    27 import java.io.BufferedReader; 
    28 import java.io.PrintWriter; 
    2927//import java.util.Vector; 
    3028import javax.swing.JOptionPane; 
     
    3230import loci.visbio.help.HelpManager; 
    3331import loci.visbio.state.*; 
     32import org.w3c.dom.Element; 
    3433 
    3534/** DisplayManager is the manager encapsulating VisBio's 2D and 3D displays. */ 
     
    140139  // -- Saveable API methods -- 
    141140 
    142   protected static final String DISPLAY_MANAGER = "VisBio_DisplayManager"; 
    143  
    144   /** Writes the current state to the given writer. */ 
    145   public void saveState(PrintWriter out) throws SaveException { 
     141  /** Writes the current state to the given DOM element ("VisBio"). */ 
     142  public void saveState(Element el) throws SaveException { 
    146143    DisplayWindow[] windows = getDisplays(); 
    147144 
     
    157154  } 
    158155 
    159   /** Restores the current state from the given reader. */ 
    160   public void restoreState(BufferedReader in) throws SaveException { 
     156  /** Restores the current state from the given DOM element ("VisBio"). */ 
     157  public void restoreState(Element el) throws SaveException { 
    161158    /* CTR TODO for v3.00 final 
    162159    CAElement custom = ome.getCustomAttr(); 
  • trunk/loci/visbio/view/DisplayPosition.java

    r155 r358  
    2525 
    2626import loci.visbio.state.Dynamic; 
     27import loci.visbio.state.Saveable; 
     28import loci.visbio.state.SaveException; 
    2729import loci.visbio.util.ObjectUtil; 
     30import org.w3c.dom.Element; 
    2831 
    2932/** DisplayPosition represents an orientation of VisAD display. */ 
    30 public class DisplayPosition implements Dynamic { 
     33public class DisplayPosition implements Dynamic, Saveable { 
    3134 
    3235  // -- Fields -- 
     
    5154 
    5255 
    53   // -- DisplayPosition API methods - state logic -- 
     56  // -- DisplayPosition API methods -- 
    5457 
    55   /** Writes the current state to the given XML object. */ 
    56   public void saveState(DisplayWindow window, String attrName) { 
    57     window.setAttr(attrName + "_name", name); 
    58     window.setAttr(attrName + "_matrix", ObjectUtil.arrayToString(matrix)); 
    59   } 
     58  /** Gets the position's string representation (name). */ 
     59  public String toString() { return name; } 
    6060 
    61   /** Restores the current state from the given XML object. */ 
    62   public void restoreState(DisplayWindow window, String attrName) { 
    63     name = window.getAttr(attrName + "_name"); 
    64     matrix = ObjectUtil.stringToDoubleArray( 
    65       window.getAttr(attrName + "_matrix")); 
    66   } 
     61  /** Gets the positions's name. */ 
     62  public String getName() { return name; } 
     63 
     64  /** Gets the position's description. */ 
     65  public double[] getMatrix() { return matrix; } 
    6766 
    6867 
     
    104103 
    105104 
    106   // -- New API methods -- 
     105  // -- Saveable API methods -- 
    107106 
    108   /** Gets the position's string representation (name). */ 
    109   public String toString() { return name; } 
     107  /** Writes the current state to the given DOM element ("Capture"). */ 
     108  public void saveState(Element el) throws SaveException { 
     109    /* CTR TODO for v3.00 final 
     110    window.setAttr(attrName + "_name", name); 
     111    window.setAttr(attrName + "_matrix", ObjectUtil.arrayToString(matrix)); 
     112    */ 
     113  } 
    110114 
    111   /** Gets the positions's name. */ 
    112   public String getName() { return name; } 
    113  
    114   /** Gets the position's description. */ 
    115   public double[] getMatrix() { return matrix; } 
     115  /** Restores the current state from the given DOM element ("Capture"). */ 
     116  public void restoreState(Element el) throws SaveException { 
     117    /* CTR TODO for v3.00 final 
     118    name = window.getAttr(attrName + "_name"); 
     119    matrix = ObjectUtil.stringToDoubleArray( 
     120      window.getAttr(attrName + "_matrix")); 
     121    */ 
     122  } 
    116123 
    117124} 
  • trunk/loci/visbio/view/DisplayWindow.java

    r351 r358  
    2626import java.awt.*; 
    2727import java.awt.event.*; 
    28 import java.io.BufferedReader; 
    29 import java.io.PrintWriter; 
    3028import javax.swing.JFrame; 
    3129import javax.swing.JPanel; 
     
    3331import loci.visbio.WindowManager; 
    3432import loci.visbio.data.DataTransform; 
    35 import loci.visbio.state.Dynamic; 
     33import loci.visbio.state.*; 
    3634import loci.visbio.util.*; 
     35import org.w3c.dom.Element; 
    3736import visad.*; 
    3837 
     
    4241 */ 
    4342public class DisplayWindow extends JFrame 
    44   implements ActionListener, DisplayListener, Dynamic, KeyListener 
     43  implements ActionListener, DisplayListener, Dynamic, KeyListener, Saveable 
    4544{ 
    4645 
     
    199198 
    200199 
    201   // -- DisplayWindow API methods - state logic -- 
    202  
    203   protected static final String DISPLAY_DIALOG = "VisBio_DisplayWindow"; 
    204  
    205   //protected CAElement custom; 
    206   protected int index; 
    207  
    208   /** Writes the current state to the given writer. */ 
    209   public void saveState(PrintWriter out, int id) { 
     200  // -- Component API methods -- 
     201 
     202  /** Shows or hides this window. */ 
     203  public void setVisible(boolean b) { 
     204    super.setVisible(b); 
     205    if (b) controls.reshow(); 
     206  } 
     207 
     208 
     209  // -- Object API methods -- 
     210 
     211  /** Gets a string representation of this display (just its name). */ 
     212  public String toString() { return string; } 
     213 
     214 
     215  // -- ActionListener API methods -- 
     216 
     217  /** Handles button presses. */ 
     218  public void actionPerformed(ActionEvent e) { 
     219    String cmd = e.getActionCommand(); 
     220  } 
     221 
     222 
     223  // -- DisplayListener API methods -- 
     224 
     225  /** Listens for keyboard presses within the display. */ 
     226  public void displayChanged(DisplayEvent e) { 
     227    int id = e.getId(); 
     228    if (id == DisplayEvent.KEY_PRESSED) { 
     229      keyPressed((KeyEvent) e.getInputEvent()); 
     230    } 
     231    else if (id == DisplayEvent.KEY_RELEASED) { 
     232      keyReleased((KeyEvent) e.getInputEvent()); 
     233    } 
     234  } 
     235 
     236 
     237  // -- Dynamic API methods -- 
     238 
     239  /** Tests whether two dynamic objects are equivalent. */ 
     240  public boolean matches(Dynamic dyn) { 
     241    if (!isCompatible(dyn)) return false; 
     242    DisplayWindow window = (DisplayWindow) dyn; 
     243 
     244    return ObjectUtil.objectsEqual(name, window.name) && 
     245      viewHandler.matches(window.viewHandler) && 
     246      captureHandler.matches(window.captureHandler) && 
     247      transformHandler.matches(window.transformHandler); 
     248  } 
     249 
     250  /** 
     251   * Tests whether the given dynamic object can be used as an argument to 
     252   * initState, for initializing this dynamic object. 
     253   */ 
     254  public boolean isCompatible(Dynamic dyn) { 
     255    if (!(dyn instanceof DisplayWindow)) return false; 
     256    DisplayWindow window = (DisplayWindow) dyn; 
     257    return threeD == window.threeD; 
     258  } 
     259 
     260  /** 
     261   * Modifies this object's state to match that of the given object. 
     262   * If the argument is null, the object is initialized according to 
     263   * its current state instead. 
     264   */ 
     265  public void initState(Dynamic dyn) { 
     266    if (dyn != null && !isCompatible(dyn)) return; 
     267    DisplayWindow window = (DisplayWindow) dyn; 
     268 
     269    if (window != null) { 
     270      name = window.name; 
     271      threeD = window.threeD; 
     272    } 
     273    string = name + (threeD ? " (3D)" : " (2D)"); 
     274 
     275    if (display == null) { 
     276      display = VisUtil.makeDisplay(name, threeD, STEREO); 
     277      display.addDisplayListener(this); 
     278    } 
     279    else display.setName(name); 
     280    setTitle("Display - " + name); 
     281 
     282    // handlers 
     283    createHandlers(); 
     284    if (window == null) { 
     285      viewHandler.initState(null); 
     286      captureHandler.initState(null); 
     287      transformHandler.initState(null); 
     288    } 
     289    else { 
     290      // handlers' initState methods are smart enough to reinitialize 
     291      // their components only when necessary, to ensure efficiency 
     292      viewHandler.initState(window.viewHandler); 
     293      captureHandler.initState(window.captureHandler); 
     294      transformHandler.initState(window.transformHandler); 
     295    } 
     296 
     297    if (controls == null) { 
     298      // display window's content pane 
     299      Container pane = getContentPane(); 
     300      pane.setLayout(new BorderLayout()); 
     301 
     302      // panel for dimensional sliders 
     303      sliders = new JPanel(); 
     304      sliders.setLayout(new BorderLayout()); 
     305 
     306      // breakaway panel for display controls 
     307      controls = new BreakawayPanel(pane, "Controls - " + name, true); 
     308      controls.setEdge(BorderLayout.EAST); 
     309 
     310      // add display controls breakaway window to window manager 
     311      WindowManager wm = (WindowManager) 
     312        manager.getVisBio().getManager(WindowManager.class); 
     313      wm.addWindow(controls.getWindow()); 
     314 
     315      // listen for key presses 
     316      //addKeyListener(this); 
     317      //controls.addKeyListener(this); 
     318 
     319      // Note: adding the KeyListener directly to frames and panels is not 
     320      // effective, because some child always has the keyboard focus and eats 
     321      // the event. Better would be to add the keyboard listener to each 
     322      // component that does not need the arrow keys for its own purposes. For 
     323      // now, the display itself must have the focus (just click it first). 
     324 
     325      // lay out components 
     326      pane.add(display.getComponent(), BorderLayout.CENTER); 
     327      controls.setContentPane(FormsUtil.makeColumn(new Object[] { 
     328        viewHandler.getPanel(), FormsUtil.makeRow(new Object[] { 
     329        captureHandler.getPanel()}), "Data", transformHandler.getPanel(), 
     330        sliders}, null, true)); 
     331      pack(); 
     332      repack(); 
     333    } 
     334    else controls.getWindow().setTitle("Controls - " + name); 
     335  } 
     336 
     337  /** 
     338   * Called when this object is being discarded in favor of 
     339   * another object with a matching state. 
     340   */ 
     341  public void discard() { } 
     342 
     343 
     344  // -- KeyListener API methods -- 
     345 
     346  /** Handles key presses. */ 
     347  public void keyPressed(KeyEvent e) { 
     348    int code = e.getKeyCode(); 
     349    int mods = e.getModifiers(); 
     350    if (mods != 0) return; 
     351    if (code == KeyEvent.VK_LEFT || code == KeyEvent.VK_RIGHT) { 
     352      int axis = transformHandler.getPanel().getLeftRightAxis(); 
     353      BioSlideWidget bsw = transformHandler.getSlider(axis); 
     354      if (bsw == null) return; 
     355      if (code == KeyEvent.VK_LEFT) bsw.step(false); 
     356      else bsw.step(true); // code == KeyEvent.VK_RIGHT 
     357    } 
     358    else if (code == KeyEvent.VK_UP || code == KeyEvent.VK_DOWN) { 
     359      int axis = transformHandler.getPanel().getUpDownAxis(); 
     360      BioSlideWidget bsw = transformHandler.getSlider(axis); 
     361      if (bsw == null) return; 
     362      if (code == KeyEvent.VK_DOWN) bsw.step(false); 
     363      else bsw.step(true); // code == KeyEvent.VK_UP 
     364    } 
     365  } 
     366 
     367  /** Handles key releases. */ 
     368  public void keyReleased(KeyEvent e) { } 
     369 
     370  /** Handles key strokes. */ 
     371  public void keyTyped(KeyEvent e) { } 
     372 
     373 
     374  // -- Saveable API methods -- 
     375 
     376  /** Writes the current state to the given DOM element ("Displays"). */ 
     377  public void saveState(Element el) throws SaveException { 
    210378    /* CTR TODO for v3.00 final 
    211379    custom = ome.getCustomAttr(); 
     
    220388  } 
    221389 
    222   /** Restores the current state from the given reader. */ 
    223   public void restoreState(BufferedReader in, int id) { 
     390  /** Restores the current state from the given DOM element ("Displays"). */ 
     391  public void restoreState(Element el) throws SaveException { 
    224392    /* CTR TODO for v3.00 final 
    225393    custom = ome.getCustomAttr(); 
     
    253421  } 
    254422 
    255   /** Sets value for the given attribute. */ 
    256   protected void setAttr(String attr, String value) { 
    257     /*custom.setAttribute(attr, value);*/ 
    258   } 
    259  
    260   /** Gets value for the given attribute. */ 
    261   protected String getAttr(String attr) { 
    262     return null;/*custom.getAttributes(DISPLAY_DIALOG, attr)[index];*/ 
    263   } 
    264  
    265  
    266   // -- Component API methods -- 
    267  
    268   /** Shows or hides this window. */ 
    269   public void setVisible(boolean b) { 
    270     super.setVisible(b); 
    271     if (b) controls.reshow(); 
    272   } 
    273  
    274  
    275   // -- Object API methods -- 
    276  
    277   /** Gets a string representation of this display (just its name). */ 
    278   public String toString() { return string; } 
    279  
    280  
    281   // -- ActionListener API methods -- 
    282  
    283   /** Handles button presses. */ 
    284   public void actionPerformed(ActionEvent e) { 
    285     String cmd = e.getActionCommand(); 
    286   } 
    287  
    288  
    289   // -- DisplayListener API methods -- 
    290  
    291   /** Listens for keyboard presses within the display. */ 
    292   public void displayChanged(DisplayEvent e) { 
    293     int id = e.getId(); 
    294     if (id == DisplayEvent.KEY_PRESSED) { 
    295       keyPressed((KeyEvent) e.getInputEvent()); 
    296     } 
    297     else if (id == DisplayEvent.KEY_RELEASED) { 
    298       keyReleased((KeyEvent) e.getInputEvent()); 
    299     } 
    300   } 
    301  
    302  
    303   // -- Dynamic API methods -- 
    304  
    305   /** Tests whether two dynamic objects are equivalent. */ 
    306   public boolean matches(Dynamic dyn) { 
    307     if (!isCompatible(dyn)) return false; 
    308     DisplayWindow window = (DisplayWindow) dyn; 
    309  
    310     return ObjectUtil.objectsEqual(name, window.name) && 
    311       viewHandler.matches(window.viewHandler) && 
    312       captureHandler.matches(window.captureHandler) && 
    313       transformHandler.matches(window.transformHandler); 
    314   } 
    315  
    316   /** 
    317    * Tests whether the given dynamic object can be used as an argument to 
    318    * initState, for initializing this dynamic object. 
    319    */ 
    320   public boolean isCompatible(Dynamic dyn) { 
    321     if (!(dyn instanceof DisplayWindow)) return false; 
    322     DisplayWindow window = (DisplayWindow) dyn; 
    323     return threeD == window.threeD; 
    324   } 
    325  
    326   /** 
    327    * Modifies this object's state to match that of the given object. 
    328    * If the argument is null, the object is initialized according to 
    329    * its current state instead. 
    330    */ 
    331   public void initState(Dynamic dyn) { 
    332     if (dyn != null && !isCompatible(dyn)) return; 
    333     DisplayWindow window = (DisplayWindow) dyn; 
    334  
    335     if (window != null) { 
    336       name = window.name; 
    337       threeD = window.threeD; 
    338     } 
    339     string = name + (threeD ? " (3D)" : " (2D)"); 
    340  
    341     if (display == null) { 
    342       display = VisUtil.makeDisplay(name, threeD, STEREO); 
    343       display.addDisplayListener(this); 
    344     } 
    345     else display.setName(name); 
    346     setTitle("Display - " + name); 
    347  
    348     // handlers 
    349     createHandlers(); 
    350     if (window == null) { 
    351       viewHandler.initState(null); 
    352       captureHandler.initState(null); 
    353       transformHandler.initState(null); 
    354     } 
    355     else { 
    356       // handlers' initState methods are smart enough to reinitialize 
    357       // their components only when necessary, to ensure efficiency 
    358       viewHandler.initState(window.viewHandler); 
    359       captureHandler.initState(window.captureHandler); 
    360       transformHandler.initState(window.transformHandler); 
    361     } 
    362  
    363     if (controls == null) { 
    364       // display window's content pane 
    365       Container pane = getContentPane(); 
    366       pane.setLayout(new BorderLayout()); 
    367  
    368       // panel for dimensional sliders 
    369       sliders = new JPanel(); 
    370       sliders.setLayout(new BorderLayout()); 
    371  
    372       // breakaway panel for display controls 
    373       controls = new BreakawayPanel(pane, "Controls - " + name, true); 
    374       controls.setEdge(BorderLayout.EAST); 
    375  
    376       // add display controls breakaway window to window manager 
    377       WindowManager wm = (WindowManager) 
    378         manager.getVisBio().getManager(WindowManager.class); 
    379       wm.addWindow(controls.getWindow()); 
    380  
    381       // listen for key presses 
    382       //addKeyListener(this); 
    383       //controls.addKeyListener(this); 
    384  
    385       // Note: adding the KeyListener directly to frames and panels is not 
    386       // effective, because some child always has the keyboard focus and eats 
    387       // the event. Better would be to add the keyboard listener to each 
    388       // component that does not need the arrow keys for its own purposes. For 
    389       // now, the display itself must have the focus (just click it first). 
    390  
    391       // lay out components 
    392       pane.add(display.getComponent(), BorderLayout.CENTER); 
    393       controls.setContentPane(FormsUtil.makeColumn(new Object[] { 
    394         viewHandler.getPanel(), FormsUtil.makeRow(new Object[] { 
    395         captureHandler.getPanel()}), "Data", transformHandler.getPanel(), 
    396         sliders}, null, true)); 
    397       pack(); 
    398       repack(); 
    399     } 
    400     else controls.getWindow().setTitle("Controls - " + name); 
    401   } 
    402  
    403   /** 
    404    * Called when this object is being discarded in favor of 
    405    * another object with a matching state. 
    406    */ 
    407   public void discard() { } 
    408  
    409  
    410   // -- KeyListener API methods -- 
    411  
    412   /** Handles key presses. */ 
    413   public void keyPressed(KeyEvent e) { 
    414     int code = e.getKeyCode(); 
    415     int mods = e.getModifiers(); 
    416     if (mods != 0) return; 
    417     if (code == KeyEvent.VK_LEFT || code == KeyEvent.VK_RIGHT) { 
    418       int axis = transformHandler.getPanel().getLeftRightAxis(); 
    419       BioSlideWidget bsw = transformHandler.getSlider(axis); 
    420       if (bsw == null) return; 
    421       if (code == KeyEvent.VK_LEFT) bsw.step(false); 
    422       else bsw.step(true); // code == KeyEvent.VK_RIGHT 
    423     } 
    424     else if (code == KeyEvent.VK_UP || code == KeyEvent.VK_DOWN) { 
    425       int axis = transformHandler.getPanel().getUpDownAxis(); 
    426       BioSlideWidget bsw = transformHandler.getSlider(axis); 
    427       if (bsw == null) return; 
    428       if (code == KeyEvent.VK_DOWN) bsw.step(false); 
    429       else bsw.step(true); // code == KeyEvent.VK_UP 
    430     } 
    431   } 
    432  
    433   /** Handles key releases. */ 
    434   public void keyReleased(KeyEvent e) { } 
    435  
    436   public void keyTyped(KeyEvent e) { } 
    437  
    438423 
    439424  // -- Helper methods -- 
  • trunk/loci/visbio/view/TransformHandler.java

    r329 r358  
    3838import loci.visbio.data.DataCache; 
    3939import loci.visbio.data.DataTransform; 
    40 import loci.visbio.state.StateManager; 
     40import loci.visbio.state.*; 
    4141import loci.visbio.util.VisUtil; 
     42import org.w3c.dom.Element; 
    4243import visad.*; 
    4344 
    4445/** Provides logic for linking data transforms to a display. */ 
    45 public class TransformHandler implements ChangeListener, Runnable  { 
     46public class TransformHandler implements ChangeListener, Runnable, Saveable { 
    4647 
    4748  // -- Constant -- 
     
    294295 
    295296  // -- TransformHandler API methods - state logic -- 
    296  
    297   /** Writes the current state. */ 
    298   public void saveState() { 
    299     // save number of links 
    300     int len = links.size(); 
    301     window.setAttr("numLinks", "" + len); 
    302  
    303     // save all links 
    304     for (int i=0; i<len; i++) { 
    305       TransformLink link = (TransformLink) links.elementAt(i); 
    306       link.saveState(window, "link" + i); 
    307     } 
    308  
    309     // save other parameters 
    310     window.setAttr("animating", "" + animating); 
    311     window.setAttr("animFPS", "" + fps); 
    312     window.setAttr("animAxis", "" + animAxis); 
    313   } 
    314  
    315   /** Restores the current state. */ 
    316   public void restoreState() { 
    317     int len = Integer.parseInt(window.getAttr("numLinks")); 
    318     newLinks = new Vector(); 
    319     for (int i=0; i<len; i++) { 
    320       TransformLink link = new TransformLink(this); 
    321       link.restoreState(window, "link" + i); 
    322       newLinks.add(link); 
    323     } 
    324     animating = window.getAttr("animating").equalsIgnoreCase("true"); 
    325     fps = Integer.parseInt(window.getAttr("animFPS")); 
    326     animAxis = Integer.parseInt(window.getAttr("animAxis")); 
    327   } 
    328297 
    329298  /** Tests whether two objects are in equivalent states. */ 
     
    579548  } 
    580549 
     550 
     551  // -- Saveable API methods -- 
     552 
     553  /** Writes the current state to the given DOM element ("Display"). */ 
     554  public void saveState(Element el) throws SaveException { 
     555    /* CTR TODO for v3.00 final 
     556    // save number of links 
     557    int len = links.size(); 
     558    window.setAttr("numLinks", "" + len); 
     559 
     560    // save all links 
     561    for (int i=0; i<len; i++) { 
     562      TransformLink link = (TransformLink) links.elementAt(i); 
     563      link.saveState(window, "link" + i); 
     564    } 
     565 
     566    // save other parameters 
     567    window.setAttr("animating", "" + animating); 
     568    window.setAttr("animFPS", "" + fps); 
     569    window.setAttr("animAxis", "" + animAxis); 
     570    */ 
     571  } 
     572 
     573  /** Restores the current state from the given DOM element ("Display"). */ 
     574  public void restoreState(Element el) throws SaveException { 
     575    /* CTR TODO for v3.00 final 
     576    int len = Integer.parseInt(window.getAttr("numLinks")); 
     577    newLinks = new Vector(); 
     578    for (int i=0; i<len; i++) { 
     579      TransformLink link = new TransformLink(this); 
     580      link.restoreState(window, "link" + i); 
     581      newLinks.add(link); 
     582    } 
     583    animating = window.getAttr("animating").equalsIgnoreCase("true"); 
     584    fps = Integer.parseInt(window.getAttr("animFPS")); 
     585    animAxis = Integer.parseInt(window.getAttr("animAxis")); 
     586    */ 
     587  } 
     588 
    581589} 
  • trunk/loci/visbio/view/TransformLink.java

    r329 r358  
    2727import java.rmi.RemoteException; 
    2828import java.util.Vector; 
    29 import loci.visbio.VisBioFrame; 
    3029import loci.visbio.data.*; 
    31 import loci.visbio.state.Dynamic; 
     30import loci.visbio.state.*; 
    3231import loci.visbio.util.ObjectUtil; 
    3332import loci.visbio.util.VisUtil; 
     33import org.w3c.dom.Element; 
    3434import visad.*; 
    3535import visad.util.Util; 
     
    159159      new VisADException(trans.getName() + ": " + msg); 
    160160    doMessages(false); 
    161   } 
    162  
    163  
    164   // -- TransformLink API methods - state logic -- 
    165  
    166   /** Writes the current state. */ 
    167   public void saveState(DisplayWindow window, String attrName) { 
    168     VisBioFrame bio = handler.getWindow().getVisBio(); 
    169     DataManager dm = (DataManager) bio.getManager(DataManager.class); 
    170     Vector dataList = dm.getDataList(); 
    171     int ndx = dataList.indexOf(trans); 
    172     window.setAttr(attrName + "_dataIndex", "" + ndx); 
    173     if (colorHandler != null) colorHandler.saveState(attrName); 
    174     window.setAttr(attrName + "_visible", "" + isVisible()); 
    175   } 
    176  
    177   /** Restores the current state. */ 
    178   public void restoreState(DisplayWindow window, String attrName) { 
    179     VisBioFrame bio = handler.getWindow().getVisBio(); 
    180     DataManager dm = (DataManager) bio.getManager(DataManager.class); 
    181     Vector dataList = dm.getDataList(); 
    182     int dataIndex = Integer.parseInt(window.getAttr(attrName + "_dataIndex")); 
    183     trans = (DataTransform) dataList.elementAt(dataIndex); 
    184     if (colorHandler != null) colorHandler.restoreState(attrName); 
    185     visible = window.getAttr(attrName + "_visible").equalsIgnoreCase("true"); 
    186161  } 
    187162 
     
    322297      computeData(false); 
    323298    } 
     299  } 
     300 
     301 
     302  // -- Saveable API methods -- 
     303 
     304  /** Writes the current state to the given DOM element ("Display"). */ 
     305  public void saveState(Element el) throws SaveException { 
     306    /* CTR TODO for v3.00 final 
     307    VisBioFrame bio = handler.getWindow().getVisBio(); 
     308    DataManager dm = (DataManager) bio.getManager(DataManager.class); 
     309    Vector dataList = dm.getDataList(); 
     310    int ndx = dataList.indexOf(trans); 
     311    window.setAttr(attrName + "_dataIndex", "" + ndx); 
     312    if (colorHandler != null) colorHandler.saveState(attrName); 
     313    window.setAttr(attrName + "_visible", "" + isVisible()); 
     314    */ 
     315  } 
     316 
     317  /** Restores the current state from the given DOM element ("Display"). */ 
     318  public void restoreState(Element el) throws SaveException { 
     319    /* CTR TODO for 3.00 final 
     320    VisBioFrame bio = handler.getWindow().getVisBio(); 
     321    DataManager dm = (DataManager) bio.getManager(DataManager.class); 
     322    Vector dataList = dm.getDataList(); 
     323    int dataIndex = Integer.parseInt(window.getAttr(attrName + "_dataIndex")); 
     324    trans = (DataTransform) dataList.elementAt(dataIndex); 
     325    if (colorHandler != null) colorHandler.restoreState(attrName); 
     326    visible = window.getAttr(attrName + "_visible").equalsIgnoreCase("true"); 
     327    */ 
    324328  } 
    325329 
  • trunk/loci/visbio/view/ViewHandler.java

    r283 r358  
    2828import loci.visbio.data.DataTransform; 
    2929import loci.visbio.data.ImageTransform; 
     30import loci.visbio.state.Saveable; 
     31import loci.visbio.state.SaveException; 
    3032import loci.visbio.util.ObjectUtil; 
    3133import loci.visbio.util.VisUtil; 
     34import org.w3c.dom.Element; 
    3235import visad.*; 
    3336import visad.java2d.DisplayImplJ2D; 
    3437 
    3538/** Provides logic for controlling a VisAD display's view. */ 
    36 public class ViewHandler { 
     39public class ViewHandler implements Saveable { 
    3740 
    3841  // -- Constants -- 
     
    292295 
    293296  // -- ViewHandler API methods - state logic -- 
    294  
    295   /** Writes the current state. */ 
    296   public void saveState() { 
    297     window.setAttr("matrix", ObjectUtil.arrayToString(getMatrix())); 
    298     window.setAttr("aspectX", "" + xasp); 
    299     window.setAttr("aspectY", "" + yasp); 
    300     window.setAttr("aspectZ", "" + zasp); 
    301     window.setAttr("showScale", "" + showScale); 
    302     window.setAttr("boundingBox", "" + boundingBox); 
    303     window.setAttr("parallel", "" + parallel); 
    304   } 
    305  
    306   /** Restores the current state. */ 
    307   public void restoreState() { 
    308     matrix = ObjectUtil.stringToDoubleArray(window.getAttr("matrix")); 
    309     xasp = ObjectUtil.stringToDouble(window.getAttr("aspectX")); 
    310     yasp = ObjectUtil.stringToDouble(window.getAttr("aspectY")); 
    311     zasp = ObjectUtil.stringToDouble(window.getAttr("aspectZ")); 
    312     showScale = window.getAttr("showScale").equalsIgnoreCase("true"); 
    313     boundingBox = window.getAttr("boundingBox").equalsIgnoreCase("true"); 
    314     parallel = window.getAttr("parallel").equalsIgnoreCase("true"); 
    315   } 
    316297 
    317298  /** Tests whether two objects are in equivalent states. */ 
     
    353334 
    354335 
     336  // -- Saveable API methods -- 
     337 
     338  /** Writes the current state to the given DOM element ("Display"). */ 
     339  public void saveState(Element el) throws SaveException { 
     340    /* CTR TODO for v3.00 final 
     341    window.setAttr("matrix", ObjectUtil.arrayToString(getMatrix())); 
     342    window.setAttr("aspectX", "" + xasp); 
     343    window.setAttr("aspectY", "" + yasp); 
     344    window.setAttr("aspectZ", "" + zasp); 
     345    window.setAttr("showScale", "" + showScale); 
     346    window.setAttr("boundingBox", "" + boundingBox); 
     347    window.setAttr("parallel", "" + parallel); 
     348    */ 
     349  } 
     350 
     351  /** Restores the current state from the given DOM element ("Display"). */ 
     352  public void restoreState(Element el) throws SaveException { 
     353    /* CTR TODO for v3.00 final 
     354    matrix = ObjectUtil.stringToDoubleArray(window.getAttr("matrix")); 
     355    xasp = ObjectUtil.stringToDouble(window.getAttr("aspectX")); 
     356    yasp = ObjectUtil.stringToDouble(window.getAttr("aspectY")); 
     357    zasp = ObjectUtil.stringToDouble(window.getAttr("aspectZ")); 
     358    showScale = window.getAttr("showScale").equalsIgnoreCase("true"); 
     359    boundingBox = window.getAttr("boundingBox").equalsIgnoreCase("true"); 
     360    parallel = window.getAttr("parallel").equalsIgnoreCase("true"); 
     361    */ 
     362  } 
    355363  // -- Helper methods -- 
    356364 
Note: See TracChangeset for help on using the changeset viewer.