View Javadoc
1   package com.github.celldynamics.quimp;
2   
3   import java.awt.Color;
4   import java.awt.Frame;
5   import java.awt.GridLayout;
6   import java.awt.Panel;
7   import java.awt.TextArea;
8   import java.awt.event.WindowEvent;
9   import java.awt.event.WindowListener;
10  import java.util.ArrayList;
11  
12  import javax.swing.JScrollPane;
13  
14  import org.slf4j.Logger;
15  import org.slf4j.LoggerFactory;
16  
17  import com.github.celldynamics.quimp.filesystem.IQuimpSerialize;
18  
19  /**
20   * Builds history logger window and logs.
21   * 
22   * <p>Logs are supposed to be JSon objects that hold current BOA state. Logger is updated only when
23   * window is visible. Closing and then opening window causes erasing its content. Method
24   * addEntry(String, SnakePluginList) should be used after every activity in QuimP, where first
25   * parameter is description of this activity and next parameters define QuimP state.
26   * 
27   * @author p.baniukiewicz
28   * @deprecated No longer in use
29   */
30  public class HistoryLogger implements WindowListener {
31  
32    private static final Logger LOGGER = LoggerFactory.getLogger(HistoryLogger.class.getName());
33    private Frame historyWnd; //!< Window handler
34    private ArrayList<String> history; //!< array with all entries
35    private TextArea info;
36    private int id; //!< message counter
37  
38    /**
39     * Construct main window.
40     */
41    public HistoryLogger() {
42      id = 1;
43      historyWnd = new Frame("History");
44      Panel p = new Panel();
45      p.setLayout(new GridLayout(1, 1)); // main window panel
46      Panel tp = new Panel(); // panel with text area
47      tp.setLayout(new GridLayout(1, 1));
48      info = new TextArea(10, 60); // area to write
49      info.setEditable(false);
50      info.setBackground(Color.WHITE);
51      tp.add(info); // add to panel
52  
53      JScrollPane infoPanel = new JScrollPane(tp);
54      p.add(infoPanel);
55      historyWnd.add(p);
56      historyWnd.pack();
57      historyWnd.addWindowListener(this);
58  
59      history = new ArrayList<String>();
60  
61    }
62  
63    /**
64     * Make window visible.
65     */
66    public void openHistory() {
67      historyWnd.setVisible(true);
68    }
69  
70    /**
71     * Close window and call windowClosing() and windowClosed() methods.
72     */
73    public void closeHistory() {
74      historyWnd.setVisible(false);
75    }
76  
77    /**
78     * Add entry to log.
79     * 
80     * <p>Gather all BOA state and include in log. Uses \c Entry class to pack these information to
81     * JSon object. Particular entries can be null if they may not be logged
82     * 
83     * @param m General message to be included in log
84     * @param bs BOA state machine object
85     */
86    public void addEntry(String m, BOAState bs) {
87      // TODO This method should accept more detailed BOA state (e.g. all segm. params)
88      if (historyWnd.isVisible()) {
89        if (bs == null) {
90          return;
91        }
92        LogEntryimp/HistoryLogger.html#LogEntry">LogEntry en = new LogEntry(id++, m, bs);
93        Serializer<LogEntry> s = new Serializer<>(en, QuimP.TOOL_VERSION);
94  
95        String jsontmp = s.toString();
96        history.add(jsontmp); // store in array
97        info.append(jsontmp + '\n'); // add to log window
98        LOGGER.debug(jsontmp);
99        en = null;
100     }
101 
102   }
103 
104   /**
105    * Check if window is opened.
106    * 
107    * @return true is window is visible
108    */
109   public boolean isOpened() {
110     return historyWnd.isVisible();
111   }
112 
113   /*
114    * (non-Javadoc)
115    * 
116    * @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent)
117    */
118   @Override
119   public void windowOpened(WindowEvent e) {
120   }
121 
122   /*
123    * (non-Javadoc)
124    * 
125    * @see java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)
126    */
127   @Override
128   public void windowClosing(WindowEvent e) {
129     LOGGER.debug("History windowClosing");
130     historyWnd.setVisible(false);
131     info.setText("");
132     id = 1;
133     history.clear();
134     historyWnd.dispose();
135 
136   }
137 
138   /*
139    * (non-Javadoc)
140    * 
141    * @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
142    */
143   @Override
144   public void windowClosed(WindowEvent e) {
145     LOGGER.debug("History windowClosed");
146     historyWnd.setVisible(false);
147     info.setText("");
148     id = 1;
149     history.clear();
150     historyWnd.dispose();
151 
152   }
153 
154   /*
155    * (non-Javadoc)
156    * 
157    * @see java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
158    */
159   @Override
160   public void windowIconified(WindowEvent e) {
161 
162   }
163 
164   /*
165    * (non-Javadoc)
166    * 
167    * @see java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
168    */
169   @Override
170   public void windowDeiconified(WindowEvent e) {
171 
172   }
173 
174   /*
175    * (non-Javadoc)
176    * 
177    * @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
178    */
179   @Override
180   public void windowActivated(WindowEvent e) {
181   }
182 
183   /*
184    * (non-Javadoc)
185    * 
186    * @see java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
187    */
188   @Override
189   public void windowDeactivated(WindowEvent e) {
190   }
191 
192 }
193 
194 /**
195  * Serialization class. Holds all data that should be included in log
196  * 
197  * @author p.baniukiewicz
198  *
199  */
200 class LogEntry implements IQuimpSerialize {
201   public int id; //!< Number of entry
202   public String action; //!< Textual description of taken action
203   // selected fields to be logged (from BOAState)
204   public int frame; //!< current frame, CustomStackWindow.updateSliceSelector()
205   public BOAState.SegParam segParam; //!< Reference to segmentation parameters
206   public String fileName; //!< Current data file name
207   public SnakePluginList snakePluginList; //!< Plugin config
208 
209   /**
210    * Main constructor.
211    * 
212    * <p>Object of this class is created temporarily only for logging purposes.
213    * 
214    * @param counter number of log entry
215    * @param action description of action
216    * @param bs BOA state machine
217    */
218   public LogEntry(int counter, String action, BOAState bs) {
219     // TODO replace with snakePluginLists (beforeSerialize will not be called then)
220     super();
221     this.id = counter;
222     this.action = action;
223     this.frame = bs.boap.frame;
224     this.segParam = bs.segParam;
225     this.fileName = bs.boap.getFileName();
226     this.snakePluginList = bs.snakePluginList;
227 
228   }
229 
230   @Override
231   public void beforeSerialize() {
232     snakePluginList.beforeSerialize();
233   }
234 
235   @Override
236   public void afterSerialize() throws Exception {
237   }
238 }