Changeset 4333


Ignore:
Timestamp:
08/26/08 13:17:42 (12 years ago)
Author:
melissa
Message:

Use an instance of CacheComponent to handle cache modification.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/plugins/BrowserOptionsWindow.java

    r4329 r4333  
    2727 
    2828import java.awt.Dimension; 
    29 import java.awt.GridLayout; 
    30 import java.awt.event.*; 
    31 import javax.swing.*; 
    32 import javax.swing.event.*; 
    33 import loci.formats.cache.*; 
     29import javax.swing.JFrame; 
     30import loci.formats.cache.Cache; 
     31import loci.formats.gui.CacheComponent; 
    3432 
    3533/** 
     
    4038 * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/loci/plugins/BrowserOptionsWindow.java">SVN</a></dd></dl> 
    4139 */ 
    42 public class BrowserOptionsWindow extends JFrame 
    43   implements ActionListener, ChangeListener 
    44 { 
    45  
    46   // -- Fields -- 
    47  
    48   private Cache cache; 
    49  
    50   private JSpinner cSpinner, zSpinner, tSpinner; 
     40public class BrowserOptionsWindow extends JFrame { 
    5141 
    5242  // -- Constructor -- 
     
    5444  public BrowserOptionsWindow(String title, Cache cache) { 
    5545    super(title); 
    56     this.cache = cache; 
    5746 
    58     // construct the range spinners 
    59  
    60     int[] range = cache.getStrategy().getRange(); 
    61     int[] lengths = cache.getStrategy().getLengths(); 
    62  
    63     cSpinner = new JSpinner(new SpinnerNumberModel(range[0], 0, lengths[0], 1)); 
    64     zSpinner = new JSpinner(new SpinnerNumberModel(range[1], 0, lengths[1], 1)); 
    65     tSpinner = new JSpinner(new SpinnerNumberModel(range[2], 0, lengths[2], 1)); 
    66     cSpinner.addChangeListener(this); 
    67     zSpinner.addChangeListener(this); 
    68     tSpinner.addChangeListener(this); 
    69  
    70     // construct the radio buttons to select axis priority 
    71  
    72     String[] priorities = new String[] { 
    73       "ZCT", "ZTC", "CZT", "CTZ", "TCZ", "TZC" 
    74     }; 
    75     JRadioButton[] priorityButtons = new JRadioButton[priorities.length]; 
    76     ButtonGroup priorityGroup = new ButtonGroup(); 
    77  
    78     for (int i=0; i<priorityButtons.length; i++) { 
    79       priorityButtons[i] = new JRadioButton(priorities[i]); 
    80       priorityButtons[i].setActionCommand(priorities[i]); 
    81       priorityButtons[i].addActionListener(this); 
    82       priorityGroup.add(priorityButtons[i]); 
    83     } 
    84  
    85     // construct the radio buttons to select caching strategy 
    86  
    87     ButtonGroup strategyGroup = new ButtonGroup(); 
    88  
    89     JRadioButton crosshairStrategy = new JRadioButton("Crosshair", 
    90       cache.getStrategy() instanceof CrosshairStrategy); 
    91     crosshairStrategy.setActionCommand("crosshair"); 
    92     crosshairStrategy.addActionListener(this); 
    93     strategyGroup.add(crosshairStrategy); 
    94  
    95     JRadioButton rectangleStrategy = new JRadioButton("Rectangle", 
    96       cache.getStrategy() instanceof RectangleStrategy); 
    97     rectangleStrategy.setActionCommand("rectangle"); 
    98     rectangleStrategy.addActionListener(this); 
    99     strategyGroup.add(rectangleStrategy); 
    100  
    101     // construct the radio buttons to select caching order 
    102  
    103     // add everything to the window 
    104  
    105     JPanel panel = new JPanel(); 
    106  
    107     JLabel rangeHeader = 
    108       new JLabel("Number of planes to cache along each axis"); 
    109     JLabel priorityHeader = new JLabel("Order in which to cache planes"); 
    110     JLabel strategyHeader = new JLabel("Caching strategy"); 
    111     JLabel zLabel = new JLabel("Z: "); 
    112     JLabel cLabel = new JLabel("C: "); 
    113     JLabel tLabel = new JLabel("T: "); 
    114  
    115     JButton ok = new JButton("OK"); 
    116     ok.setActionCommand("ok"); 
    117     ok.addActionListener(this); 
    118  
    119     GridLayout layout = new GridLayout(15, 2); 
    120     panel.setLayout(layout); 
    121  
    122     panel.add(rangeHeader); 
    123     panel.add(new JLabel("")); // spacer 
    124  
    125     panel.add(zLabel); 
    126     panel.add(zSpinner); 
    127  
    128     panel.add(cLabel); 
    129     panel.add(cSpinner); 
    130  
    131     panel.add(tLabel); 
    132     panel.add(tSpinner); 
    133  
    134     panel.add(priorityHeader); 
    135     panel.add(new JLabel("")); // spacer 
    136     for (int i=0; i<priorityButtons.length; i++) { 
    137       panel.add(priorityButtons[i]); 
    138       panel.add(new JLabel("")); // spacer 
    139     } 
    140  
    141     panel.add(strategyHeader); 
    142     panel.add(new JLabel("")); // spacer 
    143  
    144     panel.add(crosshairStrategy); 
    145     panel.add(new JLabel("")); // spacer 
    146  
    147     panel.add(rectangleStrategy); 
    148     panel.add(new JLabel("")); // spacer 
    149  
    150     panel.add(new JLabel("")); // spacer 
    151     panel.add(ok); 
     47    CacheComponent panel = 
     48      new CacheComponent(cache, new String[] {"C", "Z", "T"}); 
    15249 
    15350    panel.setMinimumSize(new Dimension(300, 500)); 
     
    15754  } 
    15855 
    159   // -- ActionListener API methods -- 
    160  
    161   public void actionPerformed(ActionEvent e) { 
    162     String command = e.getActionCommand(); 
    163     if (command == null) return; 
    164  
    165     ICacheStrategy strategy = cache.getStrategy(); 
    166  
    167     if (command.equals("crosshair")) { 
    168       // switch to crosshair caching strategy 
    169       if (strategy instanceof CrosshairStrategy) return; 
    170  
    171       int[] lengths = strategy.getLengths(); 
    172       int[] priorities = strategy.getPriorities(); 
    173       int[] order = strategy.getOrder(); 
    174       int[] range = strategy.getRange(); 
    175  
    176       strategy = new CrosshairStrategy(lengths); 
    177       for (int i=0; i<lengths.length; i++) { 
    178         strategy.setPriority(priorities[i], i); 
    179         strategy.setOrder(order[i], i); 
    180         strategy.setRange(range[i], i); 
    181       } 
    182     } 
    183     else if (command.equals("rectangle")) { 
    184       // switch to rectangle caching strategy 
    185       if (strategy instanceof RectangleStrategy) return; 
    186  
    187       int[] lengths = strategy.getLengths(); 
    188       int[] priorities = strategy.getPriorities(); 
    189       int[] order = strategy.getOrder(); 
    190       int[] range = strategy.getRange(); 
    191  
    192       strategy = new RectangleStrategy(lengths); 
    193       for (int i=0; i<lengths.length; i++) { 
    194         strategy.setPriority(priorities[i], i); 
    195         strategy.setOrder(order[i], i); 
    196         strategy.setRange(range[i], i); 
    197       } 
    198     } 
    199     else if (command.equals("ok")) { 
    200       dispose(); 
    201     } 
    202     else { 
    203       // adjust axis priorities 
    204       int cPriority = getPriority(command.indexOf("C")); 
    205       int zPriority = getPriority(command.indexOf("Z")); 
    206       int tPriority = getPriority(command.indexOf("T")); 
    207  
    208       strategy.setPriority(cPriority, 0); 
    209       strategy.setPriority(zPriority, 1); 
    210       strategy.setPriority(tPriority, 2); 
    211     } 
    212     try { 
    213       cache.setStrategy(strategy); 
    214     } 
    215     catch (CacheException exc) { 
    216       exc.printStackTrace(); 
    217     } 
    218   } 
    219  
    220   // -- ChangeListener API methods -- 
    221  
    222   public void stateChanged(ChangeEvent e) { 
    223     Object source = e.getSource(); 
    224     if (source.equals(cSpinner)) { 
    225       int value = ((Integer) cSpinner.getValue()).intValue(); 
    226       cache.getStrategy().setRange(value, 0); 
    227     } 
    228     else if (source.equals(zSpinner)) { 
    229       int value = ((Integer) zSpinner.getValue()).intValue(); 
    230       cache.getStrategy().setRange(value, 1); 
    231     } 
    232     else if (source.equals(tSpinner)) { 
    233       int value = ((Integer) tSpinner.getValue()).intValue(); 
    234       cache.getStrategy().setRange(value, 2); 
    235     } 
    236   } 
    237  
    238   // -- Helper method -- 
    239  
    240   private int getPriority(int relativePriority) { 
    241     switch (relativePriority) { 
    242       case 0: 
    243         return ICacheStrategy.MAX_PRIORITY; 
    244       case 1: 
    245         return ICacheStrategy.NORMAL_PRIORITY; 
    246       case 2: 
    247         return ICacheStrategy.MIN_PRIORITY; 
    248     } 
    249     return ICacheStrategy.MIN_PRIORITY; 
    250   } 
    251  
    25256} 
Note: See TracChangeset for help on using the changeset viewer.