Changeset 3008 for trunk


Ignore:
Timestamp:
07/23/07 14:19:56 (13 years ago)
Author:
curtis
Message:

Update cache GUI component to respond to cache events. Now the GUI updates
whenever cache changes, and multiple GUI components stay synchronized properly.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/gui/CacheComponent.java

    r3004 r3008  
    1111import loci.formats.*; 
    1212import loci.formats.cache.*; 
     13import java.lang.reflect.InvocationTargetException; 
     14import java.lang.reflect.Constructor; 
    1315 
    1416/** GUI component for managing a cache. */ 
     
    2123  protected static final String[] SOURCES = 
    2224    {"Byte arrays", "BufferedImages", "ImageProcessors"}; 
     25  protected static final Class[] SOURCE_VALUES = { 
     26    ByteArraySource.class, 
     27    BufferedImageSource.class, 
     28    ImageProcessorSource.class 
     29  }; 
     30  protected static final Class[] SOURCE_PARAMS = {String.class}; 
    2331  protected static final String[] STRATEGIES = {"Crosshair", "Rectangle"}; 
     32  protected static final Class[] STRATEGY_VALUES = { 
     33    CrosshairStrategy.class, 
     34    RectangleStrategy.class 
     35  }; 
     36  protected static final Class[] STRATEGY_PARAMS = {int[].class}; 
     37 
    2438  protected static final String[] PRIORITIES = 
    2539    {"Maximum", "High", "Normal", "Low", "Minimum"}; 
     40  protected static final int[] PRIORITY_VALUES = { 
     41    ICacheStrategy.MAX_PRIORITY, ICacheStrategy.HIGH_PRIORITY, 
     42    ICacheStrategy.NORMAL_PRIORITY, ICacheStrategy.LOW_PRIORITY, 
     43    ICacheStrategy.MIN_PRIORITY 
     44  }; 
    2645  protected static final String[] ORDERS = {"Centered", "Forward", "Backward"}; 
     46  protected static final int[] ORDER_VALUES = { 
     47    ICacheStrategy.CENTERED_ORDER, 
     48    ICacheStrategy.FORWARD_ORDER, 
     49    ICacheStrategy.BACKWARD_ORDER 
     50  }; 
     51 
    2752 
    2853  // -- Fields -- 
     
    3156  private Cache cache; 
    3257 
     58  /** Combo box for choosing cache source. */ 
     59  private JComboBox sourceChooser; 
     60 
     61  /** Combo box for choosing cache strategy. */ 
     62  private JComboBox strategyChooser; 
     63 
    3364  /** Spinners for choosing range of slices to cache. */ 
    3465  private JSpinner[] range; 
     
    4071  private JComboBox[] order; 
    4172 
    42   /** File that the cache is working with (debugging only). */ 
    43   private String file; 
     73  /** File name that the cache is working with (debugging only). */ 
     74  private String id; 
     75 
     76  /** Length of each dimensional axis, obtained from cache strategy. */ 
     77  private int[] lengths; 
    4478 
    4579  // -- Constructors -- 
    4680 
     81  /** Creates a cache GUI component. */ 
    4782  public CacheComponent(Cache cache, String[] axisLabels) { 
    4883    this(cache, axisLabels, null); 
    4984  } 
    5085 
    51   public CacheComponent(Cache cache, String[] axisLabels, String file) { 
     86  /** 
     87   * Creates a cache GUI component with the ability to change between the 
     88   * various source types (mainly for debugging purposes). 
     89   */ 
     90  public CacheComponent(Cache cache, String[] axisLabels, String id) { 
    5291    super(); 
    5392    this.cache = cache; 
    54     this.file = file; 
     93    this.id = id; 
     94    lengths = cache.getStrategy().getLengths(); 
    5595 
    5696    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 
     
    60100    JPanel top = new JPanel(); 
    61101    FormLayout layout = new FormLayout("pref,3dlu,pref:grow", 
    62       file == null ? "pref:grow" : "pref:grow,3dlu,pref:grow"); 
     102      id == null ? "pref:grow" : "pref:grow,3dlu,pref:grow"); 
    63103    top.setLayout(layout); 
    64104 
     
    66106 
    67107    // add source choices, if desired 
    68     JComboBox sourceChooser = null; 
    69     if (file != null) { 
     108    if (id != null) { 
    70109      JLabel label = new JLabel("Objects to cache: "); 
    71110      sourceChooser = new JComboBox(SOURCES); 
     111      sourceChooser.setSelectedIndex(sourceIndex(cache.getSource())); 
    72112      sourceChooser.setActionCommand("source"); 
    73113      sourceChooser.addActionListener(this); 
     
    80120    } 
    81121 
     122    ICacheStrategy strategy = cache.getStrategy(); 
     123 
    82124    // add strategy choices 
    83125    JLabel label = new JLabel("Caching strategy: "); 
    84     JComboBox strategyChooser = new JComboBox(STRATEGIES); 
     126    strategyChooser = new JComboBox(STRATEGIES); 
     127    strategyChooser.setSelectedIndex(strategyIndex(strategy)); 
    85128    strategyChooser.setActionCommand("strategy"); 
    86129    strategyChooser.addActionListener(this); 
     
    91134    top.add(strategyChooser, cc.xy(col, row)); 
    92135    row += 2; 
    93  
    94     // add cache size choices 
    95136 
    96137    JPanel bottom = new JPanel(); 
     
    103144    bottom.setLayout(layout); 
    104145 
     146    // add strategy parameter choices 
    105147    col = row = 1; 
    106148    bottom.add(new JLabel("Axis"), cc.xy(col, row)); 
     
    113155    row += 2; 
    114156 
    115     int[] lengths = cache.getStrategy().getLengths(); 
    116  
    117157    range = new JSpinner[lengths.length]; 
    118158    priority = new JComboBox[lengths.length]; 
    119159    order = new JComboBox[lengths.length]; 
    120160 
     161    int[] rng = strategy.getRange(); 
     162    int[] prio = strategy.getPriorities(); 
     163    int[] ord = strategy.getOrder(); 
    121164    for (int i=0; i<axisLabels.length; i++) { 
    122165      JLabel l = new JLabel(axisLabels[i]); 
    123       range[i] = new JSpinner(new SpinnerNumberModel(0, 0, lengths[i], 1)); 
     166      range[i] = new JSpinner(new SpinnerNumberModel(rng[i], 0, lengths[i], 1)); 
    124167      priority[i] = new JComboBox(PRIORITIES); 
     168      priority[i].setSelectedIndex(priorityIndex(prio[i])); 
    125169      order[i] = new JComboBox(ORDERS); 
     170      order[i].setSelectedIndex(orderIndex(ord[i])); 
    126171 
    127172      col = 1; 
     
    160205  public void actionPerformed(ActionEvent e) { 
    161206    String cmd = e.getActionCommand(); 
    162     Object src = e.getSource(); 
    163  
    164     if ("source".equals(cmd)) updateSource((JComboBox) src); 
    165     else if ("strategy".equals(cmd)) updateStrategy((JComboBox) src); 
     207    if ("source".equals(cmd)) updateSource(); 
     208    else if ("strategy".equals(cmd)) updateStrategy(); 
    166209    else { // priority or order change 
     210      Object src = e.getSource(); 
    167211      for (int i=0; i<priority.length; i++) { 
    168212        if (src == priority[i]) { 
     
    182226  // -- CacheListener API methods -- 
    183227 
     228  /** Updates GUI to match latest cache state. */ 
    184229  public void cacheUpdated(CacheEvent e) { 
    185     //TODO 
     230    int type = e.getType(); 
     231    ICacheStrategy strategy = cache.getStrategy(); 
     232    switch (type) { 
     233      case CacheEvent.SOURCE_CHANGED: 
     234        sourceChooser.removeActionListener(this); 
     235        sourceChooser.setSelectedIndex(sourceIndex(cache.getSource())); 
     236        sourceChooser.addActionListener(this); 
     237        break; 
     238      case CacheEvent.STRATEGY_CHANGED: 
     239        strategyChooser.removeActionListener(this); 
     240        strategyChooser.setSelectedIndex(strategyIndex(strategy)); 
     241        strategyChooser.addActionListener(this); 
     242        break; 
     243      case CacheEvent.PRIORITIES_CHANGED: 
     244        int[] prio = strategy.getPriorities(); 
     245        for (int i=0; i<prio.length; i++) { 
     246          priority[i].removeActionListener(this); 
     247          priority[i].setSelectedIndex(priorityIndex(prio[i])); 
     248          priority[i].addActionListener(this); 
     249        } 
     250        break; 
     251      case CacheEvent.ORDER_CHANGED: 
     252        int[] ord = strategy.getOrder(); 
     253        for (int i=0; i<ord.length; i++) { 
     254          order[i].removeActionListener(this); 
     255          order[i].setSelectedIndex(orderIndex(ord[i])); 
     256          order[i].addActionListener(this); 
     257        } 
     258        break; 
     259      case CacheEvent.RANGE_CHANGED: 
     260        int[] rng = strategy.getRange(); 
     261        for (int i=0; i<rng.length; i++) { 
     262          range[i].removeChangeListener(this); 
     263          range[i].setValue(new Integer(rng[i])); 
     264          range[i].addChangeListener(this); 
     265        } 
     266        break; 
     267    } 
    186268  } 
    187269 
     
    199281  } 
    200282 
    201   // -- Helper methods -- 
    202  
    203   private void updateSource(JComboBox box) { 
    204     String s = (String) box.getSelectedItem(); 
    205  
    206     ICacheSource source = null; 
     283  // -- Helper methods - GUI component update -- 
     284 
     285  /** Updates cache source to match the state of the GUI. */ 
     286  private void updateSource() { 
    207287    try { 
    208       if (s.equals(SOURCES[0])) { // byte arrays 
    209         if (cache.getSource().getClass() == ByteArraySource.class) return; 
    210         source = new ByteArraySource(file); 
    211       } 
    212       else if (s.equals(SOURCES[1])) { // BufferedImages 
    213         if (cache.getSource().getClass() == BufferedImageSource.class) return; 
    214         source = new BufferedImageSource(file); 
    215       } 
    216       else if (s.equals(SOURCES[2])) { // ImageProcessors 
    217         if (cache.getSource().getClass() == ImageProcessorSource.class) return; 
    218         source = new ImageProcessorSource(file); 
    219       } 
    220       cache.setSource(source); 
    221     } 
    222     catch (CacheException exc) { 
    223       LogTools.trace(exc); 
    224     } 
    225   } 
    226  
    227   private void updateStrategy(JComboBox box) { 
    228     String s = (String) box.getSelectedItem(); 
    229     ICacheStrategy strategy = null; 
    230  
     288      ICacheSource source = sourceValue(sourceChooser.getSelectedIndex()); 
     289      if (source != null) cache.setSource(source); 
     290    } 
     291    catch (CacheException exc) { LogTools.trace(exc); } 
     292  } 
     293 
     294  /** Updates cache strategy to match the state of the GUI. */ 
     295  private void updateStrategy() { 
    231296    try { 
    232       int[] lengths = cache.getStrategy().getLengths(); 
    233       if (s.equals(STRATEGIES[0])) { // Crosshair 
    234         if (cache.getStrategy().getClass() == CrosshairStrategy.class) return; 
    235         strategy = new CrosshairStrategy(lengths); 
    236       } 
    237       else if (s.equals(STRATEGIES[1])) { // Rectangle 
    238         if (cache.getStrategy().getClass() == RectangleStrategy.class) return; 
    239         strategy = new RectangleStrategy(lengths); 
    240       } 
    241       cache.setStrategy(strategy); 
    242     } 
    243     catch (CacheException exc) { 
    244       LogTools.trace(exc); 
    245     } 
    246   } 
    247  
     297      ICacheStrategy strategy = 
     298        strategyValue(strategyChooser.getSelectedIndex()); 
     299      if (strategy != null) cache.setStrategy(strategy); 
     300    } 
     301    catch (CacheException exc) { LogTools.trace(exc); } 
     302  } 
     303 
     304  /** Updates cache range to match the state of the GUI. */ 
    248305  private void updateRange(int index) { 
    249306    int rng = ((Integer) range[index].getValue()).intValue(); 
     
    254311  } 
    255312 
     313  /** Updates cache priority to match the state of the GUI. */ 
    256314  private void updatePriority(int index) { 
    257     String s = (String) priority[index].getSelectedItem(); 
    258  
    259     int prio = 0; 
    260     if (s.equals(PRIORITIES[0])) { // Maximum 
    261       prio = ICacheStrategy.MAX_PRIORITY; 
    262     } 
    263     else if (s.equals(PRIORITIES[1])) { // High 
    264       prio = ICacheStrategy.HIGH_PRIORITY; 
    265     } 
    266     else if (s.equals(PRIORITIES[2])) { // Normal 
    267       prio = ICacheStrategy.NORMAL_PRIORITY; 
    268     } 
    269     else if (s.equals(PRIORITIES[3])) { // Low 
    270       prio = ICacheStrategy.LOW_PRIORITY; 
    271     } 
    272     else if (s.equals(PRIORITIES[4])) { // Minimum 
    273       prio = ICacheStrategy.MIN_PRIORITY; 
    274     } 
    275  
     315    int prio = priorityValue(priority[index].getSelectedIndex()); 
    276316    ICacheStrategy strategy = cache.getStrategy(); 
    277317    int[] priorities = strategy.getPriorities(); 
     
    279319  } 
    280320 
     321  /** Updates cache order to match the state of the GUI. */ 
    281322  private void updateOrder(int index) { 
    282     String s = (String) order[index].getSelectedItem(); 
    283  
    284     int ord = 0; 
    285     if (s.equals(ORDERS[0])) { // Centered 
    286       ord = ICacheStrategy.CENTERED_ORDER; 
    287     } 
    288     else if (s.equals(ORDERS[1])) { // Forward 
    289       ord = ICacheStrategy.FORWARD_ORDER; 
    290     } 
    291     else if (s.equals(ORDERS[2])) { // Backward 
    292       ord = ICacheStrategy.BACKWARD_ORDER; 
    293     } 
    294  
     323    int ord = orderValue(order[index].getSelectedIndex()); 
    295324    ICacheStrategy strategy = cache.getStrategy(); 
    296325    int[] orders = strategy.getOrder(); 
     
    298327  } 
    299328 
     329  // -- Helper methods - data conversion -- 
     330 
     331  /** Converts cache source to source chooser index. */ 
     332  private int sourceIndex(ICacheSource s) { 
     333    Class c = s.getClass(); 
     334    for (int i=0; i<SOURCE_VALUES.length; i++) { 
     335      if (SOURCE_VALUES[i] == c) return i; 
     336    } 
     337    return -1; 
     338  } 
     339 
     340  /** Generates a new cache source matching the source chooser index. */ 
     341  private ICacheSource sourceValue(int index) { 
     342    Class c = SOURCE_VALUES[index]; 
     343    if (c == cache.getSource().getClass()) return null; 
     344    try { 
     345      Constructor con = c.getConstructor(SOURCE_PARAMS); 
     346      return (ICacheSource) con.newInstance(new Object[] {id}); 
     347    } 
     348    catch (NoSuchMethodException exc) { LogTools.trace(exc); } 
     349    catch (InstantiationException exc) { LogTools.trace(exc); } 
     350    catch (IllegalAccessException exc) { LogTools.trace(exc); } 
     351    catch (IllegalArgumentException exc) { LogTools.trace(exc); } 
     352    catch (InvocationTargetException exc) { LogTools.trace(exc); } 
     353    return null; 
     354  } 
     355 
     356  /** Converts cache strategy to strategy chooser index. */ 
     357  private int strategyIndex(ICacheStrategy s) { 
     358    Class c = s.getClass(); 
     359    for (int i=0; i<STRATEGY_VALUES.length; i++) { 
     360      if (STRATEGY_VALUES[i] == c) return i; 
     361    } 
     362    return -1; 
     363  } 
     364 
     365  /** Generates a new cache strategy matching the strategy chooser index. */ 
     366  private ICacheStrategy strategyValue(int index) { 
     367    Class c = STRATEGY_VALUES[index]; 
     368    if (c == cache.getStrategy().getClass()) return null; 
     369    try { 
     370      Constructor con = c.getConstructor(STRATEGY_PARAMS); 
     371      return (ICacheStrategy) con.newInstance(new Object[] {lengths}); 
     372    } 
     373    catch (NoSuchMethodException exc) { LogTools.trace(exc); } 
     374    catch (InstantiationException exc) { LogTools.trace(exc); } 
     375    catch (IllegalAccessException exc) { LogTools.trace(exc); } 
     376    catch (IllegalArgumentException exc) { LogTools.trace(exc); } 
     377    catch (InvocationTargetException exc) { LogTools.trace(exc); } 
     378    return null; 
     379  } 
     380 
     381  /** Converts enumerated priority value to priority chooser index. */ 
     382  private int priorityIndex(int prio) { 
     383    for (int i=0; i<PRIORITY_VALUES.length; i++) { 
     384      if (PRIORITY_VALUES[i] == prio) return i; 
     385    } 
     386    return -1; 
     387  } 
     388 
     389  /** Converts priority chooser index to enumerated priority value. */ 
     390  private int priorityValue(int index) { return PRIORITY_VALUES[index]; } 
     391 
     392  /** Converts enumerated order value to order chooser index. */ 
     393  private int orderIndex(int ord) { 
     394    for (int i=0; i<ORDER_VALUES.length; i++) { 
     395      if (ORDER_VALUES[i] == ord) return i; 
     396    } 
     397    return -1; 
     398  } 
     399 
     400  /** Converts order chooser index to enumerated order value. */ 
     401  private int orderValue(int index) { return ORDER_VALUES[index]; } 
     402 
    300403} 
Note: See TracChangeset for help on using the changeset viewer.