2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2004, The Institute for Genomic Research (TIGR). |
2 |
26 Feb 07 |
jari |
All rights reserved. |
2 |
26 Feb 07 |
jari |
4 |
*/ |
2 |
26 Feb 07 |
jari |
5 |
/* |
2 |
26 Feb 07 |
jari |
* $RCSfile: HistoryViewer.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.7 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2006/03/24 15:49:44 $ |
2 |
26 Feb 07 |
jari |
* $Author: eleanorahowe $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
11 |
*/ |
2 |
26 Feb 07 |
jari |
12 |
/* |
2 |
26 Feb 07 |
jari |
* HistoryViewer.java |
2 |
26 Feb 07 |
jari |
14 |
* |
2 |
26 Feb 07 |
jari |
* Created on February 3, 2004, 10:47 PM |
2 |
26 Feb 07 |
jari |
16 |
*/ |
2 |
26 Feb 07 |
jari |
17 |
|
2 |
26 Feb 07 |
jari |
18 |
package org.tigr.microarray.mev; |
2 |
26 Feb 07 |
jari |
19 |
|
2 |
26 Feb 07 |
jari |
20 |
import java.awt.Color; |
2 |
26 Feb 07 |
jari |
21 |
import java.awt.Insets; |
2 |
26 Feb 07 |
jari |
22 |
import java.awt.event.ActionEvent; |
2 |
26 Feb 07 |
jari |
23 |
import java.awt.event.ActionListener; |
2 |
26 Feb 07 |
jari |
24 |
import java.awt.event.MouseAdapter; |
2 |
26 Feb 07 |
jari |
25 |
import java.awt.event.MouseEvent; |
2 |
26 Feb 07 |
jari |
26 |
import java.io.File; |
2 |
26 Feb 07 |
jari |
27 |
import java.io.FileWriter; |
2 |
26 Feb 07 |
jari |
28 |
import java.io.IOException; |
2 |
26 Feb 07 |
jari |
29 |
import java.io.ObjectInputStream; |
2 |
26 Feb 07 |
jari |
30 |
import java.io.ObjectOutputStream; |
2 |
26 Feb 07 |
jari |
31 |
import java.io.PrintWriter; |
2 |
26 Feb 07 |
jari |
32 |
import java.text.DateFormat; |
2 |
26 Feb 07 |
jari |
33 |
import java.util.Date; |
2 |
26 Feb 07 |
jari |
34 |
import java.util.StringTokenizer; |
2 |
26 Feb 07 |
jari |
35 |
import java.util.TimeZone; |
2 |
26 Feb 07 |
jari |
36 |
|
2 |
26 Feb 07 |
jari |
37 |
import javax.swing.JComponent; |
2 |
26 Feb 07 |
jari |
38 |
import javax.swing.JFileChooser; |
2 |
26 Feb 07 |
jari |
39 |
import javax.swing.JMenuItem; |
2 |
26 Feb 07 |
jari |
40 |
import javax.swing.JOptionPane; |
2 |
26 Feb 07 |
jari |
41 |
import javax.swing.JPopupMenu; |
2 |
26 Feb 07 |
jari |
42 |
import javax.swing.JTextArea; |
2 |
26 Feb 07 |
jari |
43 |
|
2 |
26 Feb 07 |
jari |
44 |
import org.tigr.microarray.mev.cluster.gui.impl.GUIFactory; |
2 |
26 Feb 07 |
jari |
45 |
import org.tigr.microarray.mev.cluster.gui.impl.ViewerAdapter; |
2 |
26 Feb 07 |
jari |
46 |
|
2 |
26 Feb 07 |
jari |
/** The HistoryViewer records analysis events in text |
2 |
26 Feb 07 |
jari |
* format. For each event a time stamp and an event |
2 |
26 Feb 07 |
jari |
* text entry is made for the event. |
2 |
26 Feb 07 |
jari |
50 |
* |
2 |
26 Feb 07 |
jari |
* @author braisted |
2 |
26 Feb 07 |
jari |
52 |
*/ |
2 |
26 Feb 07 |
jari |
53 |
public class HistoryViewer extends ViewerAdapter { |
2 |
26 Feb 07 |
jari |
54 |
|
2 |
26 Feb 07 |
jari |
/** Contains the text contents of the viewer. |
2 |
26 Feb 07 |
jari |
56 |
*/ |
2 |
26 Feb 07 |
jari |
57 |
private JTextArea content; |
2 |
26 Feb 07 |
jari |
/** Context menu. |
2 |
26 Feb 07 |
jari |
59 |
*/ |
2 |
26 Feb 07 |
jari |
60 |
private JPopupMenu menu; |
2 |
26 Feb 07 |
jari |
61 |
|
2 |
26 Feb 07 |
jari |
/** Creates a new instance of HistoryViewer */ |
2 |
26 Feb 07 |
jari |
63 |
public HistoryViewer() { |
2 |
26 Feb 07 |
jari |
64 |
initContent(); |
2 |
26 Feb 07 |
jari |
65 |
addHistory("Open Multiple Array Viewer"); |
2 |
26 Feb 07 |
jari |
66 |
initMenu(); |
2 |
26 Feb 07 |
jari |
67 |
content.addMouseListener(new HistoryListener()); |
2 |
26 Feb 07 |
jari |
68 |
} |
2 |
26 Feb 07 |
jari |
69 |
|
2 |
26 Feb 07 |
jari |
70 |
public HistoryViewer(JTextArea content, JComponent header){ |
2 |
26 Feb 07 |
jari |
71 |
this.content = content; |
2 |
26 Feb 07 |
jari |
72 |
initMenu(); |
2 |
26 Feb 07 |
jari |
73 |
content.addMouseListener(new HistoryListener()); |
2 |
26 Feb 07 |
jari |
74 |
} |
2 |
26 Feb 07 |
jari |
75 |
|
2 |
26 Feb 07 |
jari |
/** Initialized the viewer's content. |
2 |
26 Feb 07 |
jari |
77 |
*/ |
2 |
26 Feb 07 |
jari |
78 |
private void initContent() { |
2 |
26 Feb 07 |
jari |
79 |
content = new JTextArea(); |
2 |
26 Feb 07 |
jari |
80 |
content.setEditable(false); |
2 |
26 Feb 07 |
jari |
81 |
content.setMargin(new Insets(10,10,10,10)); |
2 |
26 Feb 07 |
jari |
82 |
content.setBackground(new Color(252,255,168)); |
2 |
26 Feb 07 |
jari |
83 |
} |
2 |
26 Feb 07 |
jari |
84 |
|
2 |
26 Feb 07 |
jari |
/** Initializes the <CODE> JPopupMenu</CODE>. |
2 |
26 Feb 07 |
jari |
86 |
*/ |
2 |
26 Feb 07 |
jari |
87 |
private void initMenu() { |
2 |
26 Feb 07 |
jari |
88 |
menu = new JPopupMenu(); |
2 |
26 Feb 07 |
jari |
89 |
JMenuItem saveItem = new JMenuItem("Save History to File", GUIFactory.getIcon("save16.gif")); |
2 |
26 Feb 07 |
jari |
90 |
saveItem.setActionCommand("save"); |
2 |
26 Feb 07 |
jari |
91 |
saveItem.addActionListener(new HistoryListener()); |
2 |
26 Feb 07 |
jari |
92 |
menu.add(saveItem); |
2 |
26 Feb 07 |
jari |
93 |
} |
2 |
26 Feb 07 |
jari |
94 |
|
2 |
26 Feb 07 |
jari |
/** Adds a history entry |
2 |
26 Feb 07 |
jari |
* @param msg Messge to add to history. |
2 |
26 Feb 07 |
jari |
97 |
*/ |
2 |
26 Feb 07 |
jari |
98 |
public void addHistory(String msg) { |
2 |
26 Feb 07 |
jari |
99 |
content.append(getDateStamp()+msg+"\n\n"); |
2 |
26 Feb 07 |
jari |
100 |
} |
2 |
26 Feb 07 |
jari |
101 |
|
2 |
26 Feb 07 |
jari |
/** Returns the current date/time stamp in <CODE>String</CODE> |
2 |
26 Feb 07 |
jari |
* format. |
2 |
26 Feb 07 |
jari |
104 |
*/ |
2 |
26 Feb 07 |
jari |
105 |
private String getDateStamp() { |
2 |
26 Feb 07 |
jari |
106 |
Date date = new Date(System.currentTimeMillis()); |
2 |
26 Feb 07 |
jari |
107 |
DateFormat format = DateFormat.getDateTimeInstance(); |
2 |
26 Feb 07 |
jari |
108 |
format.setTimeZone(TimeZone.getDefault()); |
2 |
26 Feb 07 |
jari |
109 |
return format.format(date) + " -- "; |
2 |
26 Feb 07 |
jari |
110 |
} |
2 |
26 Feb 07 |
jari |
111 |
|
2 |
26 Feb 07 |
jari |
/** Returns the viewers content. |
2 |
26 Feb 07 |
jari |
* @return |
2 |
26 Feb 07 |
jari |
114 |
*/ |
2 |
26 Feb 07 |
jari |
115 |
public JComponent getContentComponent() { |
2 |
26 Feb 07 |
jari |
116 |
return content; |
2 |
26 Feb 07 |
jari |
117 |
} |
2 |
26 Feb 07 |
jari |
118 |
|
2 |
26 Feb 07 |
jari |
/** Writes the HistoryViewer to an <CODE>ObjectOutputStream</CODE> |
2 |
26 Feb 07 |
jari |
* @param oos ObjectOutputStream |
2 |
26 Feb 07 |
jari |
* @throws IOException |
2 |
26 Feb 07 |
jari |
122 |
*/ |
2 |
26 Feb 07 |
jari |
123 |
private void writeObject(ObjectOutputStream oos) throws IOException { |
2 |
26 Feb 07 |
jari |
124 |
oos.writeObject(content.getText()); |
2 |
26 Feb 07 |
jari |
125 |
} |
2 |
26 Feb 07 |
jari |
126 |
|
2 |
26 Feb 07 |
jari |
/** Reads the HistoryViewer to an <CODE>ObjectOuputStream</CODE> |
2 |
26 Feb 07 |
jari |
128 |
* |
2 |
26 Feb 07 |
jari |
* @param ois ObjectInputStream |
2 |
26 Feb 07 |
jari |
* @throws IOException |
2 |
26 Feb 07 |
jari |
* @throws ClassNotFoundException |
2 |
26 Feb 07 |
jari |
132 |
*/ |
2 |
26 Feb 07 |
jari |
133 |
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException { |
2 |
26 Feb 07 |
jari |
134 |
String text = (String)ois.readObject(); |
2 |
26 Feb 07 |
jari |
135 |
initContent(); |
2 |
26 Feb 07 |
jari |
136 |
content.append(text); |
2 |
26 Feb 07 |
jari |
137 |
addHistory("Load Analysis From File"); |
2 |
26 Feb 07 |
jari |
138 |
initMenu(); |
2 |
26 Feb 07 |
jari |
139 |
content.addMouseListener(new HistoryListener()); |
2 |
26 Feb 07 |
jari |
140 |
} |
2 |
26 Feb 07 |
jari |
141 |
|
2 |
26 Feb 07 |
jari |
/** Saves the history to file. |
2 |
26 Feb 07 |
jari |
143 |
*/ |
2 |
26 Feb 07 |
jari |
144 |
private void saveHistory() { |
2 |
26 Feb 07 |
jari |
145 |
String sep = System.getProperty("file.separator"); |
2 |
26 Feb 07 |
jari |
146 |
String dataPath = TMEV.getDataPath(); |
2 |
26 Feb 07 |
jari |
147 |
File fileLoc = TMEV.getFile("data/"); |
2 |
26 Feb 07 |
jari |
// if the data path is null go to default, if not null and not exist then to to default |
2 |
26 Feb 07 |
jari |
// else use the dataPath |
2 |
26 Feb 07 |
jari |
150 |
if(dataPath != null) { |
2 |
26 Feb 07 |
jari |
151 |
fileLoc = new File(dataPath); |
2 |
26 Feb 07 |
jari |
152 |
if(!fileLoc.exists()) { |
2 |
26 Feb 07 |
jari |
153 |
fileLoc = TMEV.getFile("data/"); |
2 |
26 Feb 07 |
jari |
154 |
} |
2 |
26 Feb 07 |
jari |
155 |
} |
2 |
26 Feb 07 |
jari |
156 |
JFileChooser chooser = new JFileChooser(fileLoc); |
2 |
26 Feb 07 |
jari |
157 |
|
2 |
26 Feb 07 |
jari |
158 |
if(chooser.showSaveDialog(content) == JOptionPane.OK_OPTION) { |
2 |
26 Feb 07 |
jari |
159 |
try { |
2 |
26 Feb 07 |
jari |
160 |
File file = chooser.getSelectedFile(); |
2 |
26 Feb 07 |
jari |
161 |
PrintWriter bfr = new PrintWriter(new FileWriter(file)); |
2 |
26 Feb 07 |
jari |
162 |
|
2 |
26 Feb 07 |
jari |
163 |
StringTokenizer stok = new StringTokenizer(content.getText(), "\n\n"); |
2 |
26 Feb 07 |
jari |
164 |
|
2 |
26 Feb 07 |
jari |
165 |
while(stok.hasMoreTokens()){ |
2 |
26 Feb 07 |
jari |
166 |
bfr.println(stok.nextToken()); |
2 |
26 Feb 07 |
jari |
167 |
} |
2 |
26 Feb 07 |
jari |
168 |
|
2 |
26 Feb 07 |
jari |
169 |
bfr.flush(); |
2 |
26 Feb 07 |
jari |
170 |
bfr.close(); |
2 |
26 Feb 07 |
jari |
171 |
|
2 |
26 Feb 07 |
jari |
172 |
} catch (IOException ioe) { |
2 |
26 Feb 07 |
jari |
173 |
String msg = ioe.getMessage(); |
2 |
26 Feb 07 |
jari |
174 |
JOptionPane.showMessageDialog(content, "An error occurred while saving history. \nMessage: "+msg, "Error Saving History", JOptionPane.WARNING_MESSAGE); |
2 |
26 Feb 07 |
jari |
175 |
} |
2 |
26 Feb 07 |
jari |
176 |
} |
2 |
26 Feb 07 |
jari |
177 |
} |
2 |
26 Feb 07 |
jari |
178 |
|
2 |
26 Feb 07 |
jari |
179 |
|
2 |
26 Feb 07 |
jari |
/** HistoryListener is responsible for listening to mouse |
2 |
26 Feb 07 |
jari |
* and menu events |
2 |
26 Feb 07 |
jari |
182 |
*/ |
2 |
26 Feb 07 |
jari |
183 |
public class HistoryListener extends MouseAdapter implements ActionListener { |
2 |
26 Feb 07 |
jari |
/** Responds to press events. |
2 |
26 Feb 07 |
jari |
185 |
*/ |
2 |
26 Feb 07 |
jari |
186 |
public void mousePressed(MouseEvent me) { |
2 |
26 Feb 07 |
jari |
187 |
if(me.isPopupTrigger()) { |
2 |
26 Feb 07 |
jari |
188 |
menu.show(content, me.getX(), me.getY()); |
2 |
26 Feb 07 |
jari |
189 |
} |
2 |
26 Feb 07 |
jari |
190 |
} |
2 |
26 Feb 07 |
jari |
191 |
|
2 |
26 Feb 07 |
jari |
/** Responds to mouse released events. |
2 |
26 Feb 07 |
jari |
193 |
*/ |
2 |
26 Feb 07 |
jari |
194 |
public void mouseReleased(MouseEvent me) { |
2 |
26 Feb 07 |
jari |
195 |
if(me.isPopupTrigger()) { |
2 |
26 Feb 07 |
jari |
196 |
menu.show(content, me.getX(), me.getY()); |
2 |
26 Feb 07 |
jari |
197 |
} |
2 |
26 Feb 07 |
jari |
198 |
} |
2 |
26 Feb 07 |
jari |
/** Responds to menu events. |
2 |
26 Feb 07 |
jari |
200 |
*/ |
2 |
26 Feb 07 |
jari |
201 |
public void actionPerformed(ActionEvent ae) { |
2 |
26 Feb 07 |
jari |
202 |
if(ae.getActionCommand().equals("save")){ |
2 |
26 Feb 07 |
jari |
203 |
saveHistory(); |
2 |
26 Feb 07 |
jari |
204 |
} |
2 |
26 Feb 07 |
jari |
205 |
} |
2 |
26 Feb 07 |
jari |
206 |
} |
2 |
26 Feb 07 |
jari |
207 |
|
2 |
26 Feb 07 |
jari |
208 |
} |