View Javadoc
1   /*** PlateWellPropertySelectionWidget.java - part of the MirkE (say murky) application for colormetric analysis emphesizing 
2   kinetics.
3   
4   Created by: Scott Menor on 21 July, 2004.
5   Last modified by: Scott Menor on 12 December, 2004
6   
7   Copyright (c) 2004 Arizona State University - Cancer Research Institute. All rights reserved.
8   
9   MirkE is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13  
14  MirkE is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18  
19  You should have received a copy of the GNU General Public License
20  along with MirkE; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
22  */
23  
24  package edu.asu.cri.MirkE.gui;
25  
26  import edu.asu.cri.MirkE.*;
27  import edu.asu.cri.MirkE.exceptions.MirkEApplicationException;
28  import edu.asu.cri.MirkE.dataStructures.*;
29  
30  import java.awt.GridBagConstraints;
31  import java.awt.event.ActionEvent;
32  import java.awt.event.ActionListener;
33  import java.util.HashMap;
34  import java.util.Iterator;
35  import java.util.Map;
36  import java.util.Vector;
37  
38  import javax.swing.JButton;
39  import javax.swing.JPanel;
40  import javax.swing.JTabbedPane;
41  import javax.swing.JTextField;
42  import javax.swing.event.DocumentEvent;
43  import javax.swing.event.DocumentListener;
44  import javax.swing.text.AbstractDocument;
45  
46  import net.sf.hibernate.Session;
47  import net.sf.hibernate.Transaction;
48  
49  /*** A viewer for multiwell plates used to represent colormetric data and to allow users to select which well(s) belong to a specific group 
50  
51  @author Scott Menor
52  @version 0.3.0 12 december, 2004
53  */
54  public class PlateWellPropertySelectionWidget
55      extends JPanel
56      implements ActionListener, DocumentListener
57  {
58      private MirkE mirke;
59      private JTabbedPane tabbedPane;
60      private Vector selectableParameters;
61      private Map categoryLabelToTextFieldMap = new HashMap();
62      private Map categoryLabelToPlateViewMap = new HashMap();
63      private Map categoryLabelToRemoveButtonMap = new HashMap();
64      private Map categoryLabelToAddButtonMap = new HashMap();
65  
66      /*** constructor
67       * @param mirke
68       * @throws MirkEApplicationException
69      	*/
70      public PlateWellPropertySelectionWidget(MirkE mirke) throws MirkEApplicationException
71      {
72          super();
73  
74          this.mirke = mirke;
75          selectableParameters = new Vector();
76          selectableParameters.add("TODO - label");
77  
78          createPlateWellPropertySelectionWidgetGUI();
79      }
80  
81      /*** constructor
82      	
83      	@param mirke
84      	@param selectableParameters
85       * @throws MirkEApplicationException
86      	*/
87      public PlateWellPropertySelectionWidget(
88          MirkE mirke,
89          Vector selectableParameters) throws MirkEApplicationException
90      {
91          super();
92  
93          this.mirke = mirke;
94          this.selectableParameters = selectableParameters;
95  
96          createPlateWellPropertySelectionWidgetGUI();
97      }
98  
99      /***
100      * @throws MirkEApplicationException
101     	*/
102     public void createPlateWellPropertySelectionWidgetGUI() throws MirkEApplicationException
103     {
104         tabbedPane = new JTabbedPane();
105         add(tabbedPane);
106 
107        
108         mirke.getDataSet().getSession();
109        
110 
111         Iterator iterator = selectableParameters.iterator();
112         while (iterator.hasNext())
113         {
114             String selectableParameter = iterator.next().toString();
115 
116             addTabPanel(selectableParameter);
117         }
118     }
119 
120     /***
121      * @param selectableParameter
122      * @throws MirkEApplicationException
123      */
124     public void addTabPanel(String selectableParameter) throws MirkEApplicationException
125     {
126         if (tabbedPane != null)
127         {
128             tabbedPane.addTab(
129                 selectableParameter,
130                 createTabPanel(selectableParameter));
131 
132         }
133     }
134 
135     /***
136      *
137      * @param selectableParameter
138      *
139      * @return tabPanel 
140      * @throws MirkEApplicationException
141      */
142     JPanel createTabPanel(String selectableParameter) throws MirkEApplicationException
143     {
144         JPanel tabPanel = new JPanel();
145 
146         java.awt.GridBagConstraints gridBagConstraints;
147         gridBagConstraints = new java.awt.GridBagConstraints();
148         gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
149         gridBagConstraints.ipadx = 8;
150         gridBagConstraints.ipady = 8;
151 
152         tabPanel.setLayout(new java.awt.GridBagLayout());
153 
154         gridBagConstraints.gridwidth = 1;
155         gridBagConstraints.gridheight = 1;
156         gridBagConstraints.anchor = GridBagConstraints.CENTER;
157 
158         gridBagConstraints.gridy = 0;
159 
160         gridBagConstraints.gridx = 0;
161         JButton removeButton = new JButton("Remove This Category");
162         removeButton.setActionCommand("remove\t" + selectableParameter);
163         removeButton.addActionListener(this);
164         tabPanel.add(removeButton, gridBagConstraints);
165 
166         categoryLabelToRemoveButtonMap.put(selectableParameter, removeButton);
167 
168         gridBagConstraints.gridx = 2;
169 
170         JButton addButton = new JButton("Add New Category");
171         addButton.setActionCommand("add\t" + selectableParameter);
172         addButton.addActionListener(this);
173         tabPanel.add(addButton, gridBagConstraints);
174 
175         categoryLabelToAddButtonMap.put(selectableParameter, addButton);
176 
177         gridBagConstraints.gridx = 1;
178         JTextField categoryLabelTextField =
179             new JTextField(selectableParameter, 20);
180         categoryLabelTextField.setActionCommand(
181             "textFieldEdited\t" + selectableParameter);
182         categoryLabelTextField.addActionListener(this);
183 
184         categoryLabelToTextFieldMap.put(
185             selectableParameter,
186             categoryLabelTextField);
187 
188         tabPanel.add(categoryLabelTextField, gridBagConstraints);
189 
190         gridBagConstraints.gridy = 1;
191         /*
192         		gridBagConstraints.gridx = 1;
193         
194         		JLabel categoryTypeLabel = new JLabel("Category Name: ");
195         		
196         		tabPanel.add(categoryTypeLabel, gridBagConstraints);
197         		
198         		gridBagConstraints.gridx = 2;
199         		
200         		String[] categoryTypesList = {"Experimental Category", "Organism / Strain", "Control"};
201         		SpinnerListModel categoryTypeSpinnerListModel = new SpinnerListModel(categoryTypesList);
202         		JSpinner categoryTypeSpinner = new JSpinner(categoryTypeSpinnerListModel);
203         		
204         		tabPanel.add(categoryTypeSpinner, gridBagConstraints);
205         		*/
206         // add the PlateView
207         gridBagConstraints.gridx = 0;
208         //		gridBagConstraints.gridy = 2;
209         gridBagConstraints.gridy = 1;
210         gridBagConstraints.gridwidth = 3;
211 
212         PlateView plateView = new PlateView(mirke, selectableParameter);
213 
214         categoryLabelToPlateViewMap.put(selectableParameter, plateView);
215 
216         tabPanel.add(plateView, gridBagConstraints);
217 
218         return tabPanel;
219     }
220 
221     /***
222     	
223     	@param selectableParameter
224     	*/
225     public void removeTabPanel(String selectableParameter)
226     {
227         tabbedPane.remove(tabbedPane.indexOfTab(selectableParameter));
228 
229         // TODO - *really* need a better way of storing references for this (this will end up causing bad bugs that are hard to finds)
230 
231         categoryLabelToTextFieldMap.remove(selectableParameter);
232         categoryLabelToPlateViewMap.remove(selectableParameter);
233         categoryLabelToAddButtonMap.remove(selectableParameter);
234         categoryLabelToRemoveButtonMap.remove(selectableParameter);
235     }
236 
237     /***
238     
239      @param actionEvent
240      
241     	*/
242     public void actionPerformed(ActionEvent actionEvent)
243     {
244         try{
245             	performAction(actionEvent);
246         }catch(MirkEApplicationException mae){
247              // TODO this is one of those instances where we cant throw from the actionlistener so
248             //  swallow it now, pass the exception and control to the exception handle gui.
249             mae.printStackTrace();
250         }
251     }
252 
253     /***
254      * @param actionEvent
255      * @throws MirkEApplicationException
256      */
257     public void performAction(ActionEvent actionEvent)throws MirkEApplicationException{
258 
259         String actionCommand = actionEvent.getActionCommand();
260         String[] tokens = actionCommand.split("//t");
261 
262         if (tokens.length >= 2)
263         {
264             if (tokens[0].equals("add"))
265             {
266                 try{
267                 addTabPanel("new category");
268                 }catch(MirkEApplicationException mae){}
269             }
270             else if (tokens[0].equals("remove"))
271             {
272                 removeTabPanel(tokens[1]);
273 
274             }
275             else if (tokens[0].equals("textFieldEdited"))
276             {
277                 String previousCategoryName = tokens[1];
278 
279                 JTextField categoryLabelTextField =
280                     (JTextField) categoryLabelToTextFieldMap.get(
281                         previousCategoryName);
282 
283                 // TODO - BUG!!! (occasionally get a null-pointer exception on the next line; think it's associated with adding / removing tabs and changing names (perhaps specifically non-unique names) but can't get a proceedure to consistantly reproduce it yet :-(  )
284                 // on further inspection, looks like it's only occurring when I have a duplicated tab (need a system to prevent that from happening anyway;  no time to implement it now - TODO - force tabs (category names) to be unique)
285                 AbstractDocument document =
286                     (AbstractDocument) categoryLabelTextField.getDocument();
287 
288                 try
289                 {
290                     int length = document.getLength();
291 
292                     String newCategoryName = document.getText(0, length);
293 
294                     categoryLabelToTextFieldMap.put(
295                         newCategoryName,
296                         categoryLabelToTextFieldMap.get(
297                             previousCategoryName));
298                     categoryLabelToTextFieldMap.remove(previousCategoryName);
299 
300                     categoryLabelToPlateViewMap.put(
301                         newCategoryName,
302                         categoryLabelToPlateViewMap.get(
303                             previousCategoryName));
304                     categoryLabelToPlateViewMap.remove(previousCategoryName);
305 
306                     categoryLabelToAddButtonMap.put(
307                         newCategoryName,
308                         categoryLabelToAddButtonMap.get(
309                             previousCategoryName));
310                     categoryLabelToAddButtonMap.remove(previousCategoryName);
311 
312                     categoryLabelToRemoveButtonMap.put(
313                         newCategoryName,
314                         categoryLabelToRemoveButtonMap.get(
315                             previousCategoryName));
316                     categoryLabelToRemoveButtonMap.remove(
317                         previousCategoryName);
318 
319                     categoryLabelTextField.setActionCommand(
320                         "textFieldEdited\t" + newCategoryName);
321 
322                     tabbedPane.setTitleAt(
323                         tabbedPane.indexOfTab(previousCategoryName),
324                         newCategoryName);
325 
326                     // TODO - move Map elements from key previousCategoryName to key 
327 
328                     Session session = mirke.getDataSet().getSession();
329                     Transaction transaction = session.beginTransaction();
330 
331                     // TODO - need to go through and replace selectableParameter with categoryName (where appropriate)
332                     java.util.List categoryList =
333                         session.find(
334                             "from PlateWellCategory as c where c.categoryName='"
335                                 + previousCategoryName
336                                 + "'");
337 
338                     if (categoryList.size() > 0)
339                     {
340                         // TODO - it would be better to have the PlateWellCategory objects point to a single object containing a categoryName 
341 
342                         Iterator categoyyListIterator =
343                             categoryList.iterator();
344                         while (categoyyListIterator.hasNext())
345                         {
346                             PlateWellCategory plateWellCategory =
347                                 (PlateWellCategory) categoyyListIterator
348                                     .next();
349 
350                             plateWellCategory.setCategoryName(
351                                 newCategoryName);
352                         }
353                     }
354 
355                     PlateView plateView =
356                         (PlateView) categoryLabelToPlateViewMap.get(
357                             newCategoryName);
358                     plateView.setSelectableParameter(newCategoryName);
359 
360                     // JButton addButton = (JButton)categoryLabelToAddButtonMap.get(newCategoryName); (currently doing nothing with the addButton)
361 
362                     JButton removeButton =
363                         (JButton) categoryLabelToRemoveButtonMap.get(
364                             newCategoryName);
365                     removeButton.setActionCommand(
366                         "remove\t" + newCategoryName);
367 
368                     transaction.commit();
369                     session.close();
370 
371                 }
372                 catch (Exception exception)
373                 {
374                     // TODO - roll back the transaction on a Hibernate exception
375                     throw new MirkEApplicationException("Exception  performAction ", exception);
376                 }
377             }
378         }
379     }
380 
381     /***
382     	
383      @param event
384      */
385     public void changedUpdate(DocumentEvent event)
386     {
387         //		AbstractDocument document = (AbstractDocument)event.getDocument();
388         // TODO - update the name when this is changed
389         /*
390         int length = document.getLength();
391         try {
392         	this.value = document.getText(0, length);
393         	
394         } catch (Exception exception) {
395         	
396         }
397         
398         updateValue();*/
399     }
400 
401     /***
402     	
403      @param event
404      */
405     public void insertUpdate(DocumentEvent event)
406     {
407         /*a
408         AbstractDocument document = (AbstractDocument)event.getDocument();
409         
410         int length = document.getLength();
411         try {
412         	this.value = document.getText(0, length);
413         	
414         } catch (Exception exception) {
415         	
416         }
417         
418         updateValue();*/
419     }
420 
421     /***
422     	
423      @param event
424      */
425     public void removeUpdate(DocumentEvent event)
426     {
427         /*
428         AbstractDocument document = (AbstractDocument)event.getDocument();
429         
430         int length = document.getLength();
431         try {
432         	this.value = document.getText(0, length);
433         	
434         } catch (Exception exception) {
435         	
436         }
437         
438         updateValue();*/
439     }
440 }