[29] | 1 | /* |
---|
| 2 | * This program is free software; you can redistribute it and/or modify |
---|
| 3 | * it under the terms of the GNU General Public License as published by |
---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 5 | * (at your option) any later version. |
---|
| 6 | * |
---|
| 7 | * This program is distributed in the hope that it will be useful, |
---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 10 | * GNU General Public License for more details. |
---|
| 11 | * |
---|
| 12 | * You should have received a copy of the GNU General Public License |
---|
| 13 | * along with this program; if not, write to the Free Software |
---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | * ArffViewer.java |
---|
| 19 | * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.gui.arffviewer; |
---|
| 24 | |
---|
| 25 | import weka.core.Memory; |
---|
| 26 | import weka.gui.ComponentHelper; |
---|
| 27 | import weka.gui.LookAndFeel; |
---|
| 28 | |
---|
| 29 | import java.awt.BorderLayout; |
---|
| 30 | import java.awt.event.WindowEvent; |
---|
| 31 | import java.awt.event.WindowListener; |
---|
| 32 | |
---|
| 33 | import javax.swing.JFrame; |
---|
| 34 | import javax.swing.JOptionPane; |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * A little tool for viewing ARFF files. |
---|
| 38 | * |
---|
| 39 | * |
---|
| 40 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
| 41 | * @version $Revision: 4719 $ |
---|
| 42 | */ |
---|
| 43 | |
---|
| 44 | public class ArffViewer |
---|
| 45 | extends JFrame |
---|
| 46 | implements WindowListener { |
---|
| 47 | |
---|
| 48 | /** for serialization */ |
---|
| 49 | static final long serialVersionUID = -7455845566922685175L; |
---|
| 50 | |
---|
| 51 | /** the main panel */ |
---|
| 52 | private ArffViewerMainPanel m_MainPanel; |
---|
| 53 | |
---|
| 54 | /** for monitoring the Memory consumption */ |
---|
| 55 | private static Memory m_Memory = new Memory(true); |
---|
| 56 | |
---|
| 57 | /** the viewer if started from command line */ |
---|
| 58 | private static ArffViewer m_Viewer; |
---|
| 59 | |
---|
| 60 | /** whether the files were already loaded */ |
---|
| 61 | private static boolean m_FilesLoaded; |
---|
| 62 | |
---|
| 63 | /** the command line arguments */ |
---|
| 64 | private static String[] m_Args; |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * initializes the object |
---|
| 68 | */ |
---|
| 69 | public ArffViewer() { |
---|
| 70 | super("ARFF-Viewer"); |
---|
| 71 | createFrame(); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * creates all the components in the frame |
---|
| 76 | */ |
---|
| 77 | protected void createFrame() { |
---|
| 78 | // basic setup |
---|
| 79 | setIconImage(ComponentHelper.getImage("weka_icon.gif")); |
---|
| 80 | setSize(ArffViewerMainPanel.WIDTH, ArffViewerMainPanel.HEIGHT); |
---|
| 81 | setCenteredLocation(); |
---|
| 82 | setDefaultCloseOperation(DISPOSE_ON_CLOSE); |
---|
| 83 | // remove the listener - otherwise we get the strange behavior that one |
---|
| 84 | // frame receives a window-event for every single open frame! |
---|
| 85 | removeWindowListener(this); |
---|
| 86 | // add listener anew |
---|
| 87 | addWindowListener(this); |
---|
| 88 | |
---|
| 89 | getContentPane().setLayout(new BorderLayout()); |
---|
| 90 | |
---|
| 91 | m_MainPanel = new ArffViewerMainPanel(this); |
---|
| 92 | m_MainPanel.setConfirmExit(false); |
---|
| 93 | getContentPane().add(m_MainPanel, BorderLayout.CENTER); |
---|
| 94 | |
---|
| 95 | setJMenuBar(m_MainPanel.getMenu()); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | * returns the left coordinate if the frame would be centered |
---|
| 100 | * |
---|
| 101 | * @return the left coordinate |
---|
| 102 | */ |
---|
| 103 | protected int getCenteredLeft() { |
---|
| 104 | int width; |
---|
| 105 | int x; |
---|
| 106 | |
---|
| 107 | width = getBounds().width; |
---|
| 108 | x = (getGraphicsConfiguration().getBounds().width - width) / 2; |
---|
| 109 | |
---|
| 110 | if (x < 0) |
---|
| 111 | x = 0; |
---|
| 112 | |
---|
| 113 | return x; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /** |
---|
| 117 | * returns the top coordinate if the frame would be centered |
---|
| 118 | * |
---|
| 119 | * @return the top coordinate |
---|
| 120 | */ |
---|
| 121 | protected int getCenteredTop() { |
---|
| 122 | int height; |
---|
| 123 | int y; |
---|
| 124 | |
---|
| 125 | height = getBounds().height; |
---|
| 126 | y = (getGraphicsConfiguration().getBounds().height - height) / 2; |
---|
| 127 | |
---|
| 128 | if (y < 0) |
---|
| 129 | y = 0; |
---|
| 130 | |
---|
| 131 | return y; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | /** |
---|
| 135 | * positions the window at the center of the screen |
---|
| 136 | */ |
---|
| 137 | public void setCenteredLocation() { |
---|
| 138 | setLocation(getCenteredLeft(), getCenteredTop()); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | * whether to present a MessageBox on Exit or not |
---|
| 143 | * @param confirm whether a MessageBox pops up or not to confirm |
---|
| 144 | * exit |
---|
| 145 | */ |
---|
| 146 | public void setConfirmExit(boolean confirm) { |
---|
| 147 | m_MainPanel.setConfirmExit(confirm); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /** |
---|
| 151 | * returns the setting of whether to display a confirm messagebox or not |
---|
| 152 | * on exit |
---|
| 153 | * @return whether a messagebox is displayed or not |
---|
| 154 | */ |
---|
| 155 | public boolean getConfirmExit() { |
---|
| 156 | return m_MainPanel.getConfirmExit(); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
| 160 | * whether to do a System.exit(0) on close |
---|
| 161 | * |
---|
| 162 | * @param value enables/disables the System.exit(0) |
---|
| 163 | */ |
---|
| 164 | public void setExitOnClose(boolean value) { |
---|
| 165 | m_MainPanel.setExitOnClose(value); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /** |
---|
| 169 | * returns TRUE if a System.exit(0) is done on a close |
---|
| 170 | * |
---|
| 171 | * @return true if System.exit(0) is done |
---|
| 172 | */ |
---|
| 173 | public boolean getExitOnClose() { |
---|
| 174 | return m_MainPanel.getExitOnClose(); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | * returns the main panel |
---|
| 179 | * |
---|
| 180 | * @return the main panel |
---|
| 181 | */ |
---|
| 182 | public ArffViewerMainPanel getMainPanel() { |
---|
| 183 | return m_MainPanel; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | /** |
---|
| 187 | * validates and repaints the frame |
---|
| 188 | */ |
---|
| 189 | public void refresh() { |
---|
| 190 | validate(); |
---|
| 191 | repaint(); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | /** |
---|
| 195 | * invoked when a window is activated |
---|
| 196 | * |
---|
| 197 | * @param e the window event |
---|
| 198 | */ |
---|
| 199 | public void windowActivated(WindowEvent e) { |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | /** |
---|
| 203 | * invoked when a window is closed |
---|
| 204 | * |
---|
| 205 | * @param e the window event |
---|
| 206 | */ |
---|
| 207 | public void windowClosed(WindowEvent e) { |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | /** |
---|
| 211 | * invoked when a window is in the process of closing |
---|
| 212 | * |
---|
| 213 | * @param e the window event |
---|
| 214 | */ |
---|
| 215 | public void windowClosing(WindowEvent e) { |
---|
| 216 | int button; |
---|
| 217 | |
---|
| 218 | while (getMainPanel().getTabbedPane().getTabCount() > 0) |
---|
| 219 | getMainPanel().closeFile(false); |
---|
| 220 | |
---|
| 221 | if (getConfirmExit()) { |
---|
| 222 | button = ComponentHelper.showMessageBox( |
---|
| 223 | this, |
---|
| 224 | "Quit - " + getTitle(), |
---|
| 225 | "Do you really want to quit?", |
---|
| 226 | JOptionPane.YES_NO_OPTION, |
---|
| 227 | JOptionPane.QUESTION_MESSAGE); |
---|
| 228 | if (button == JOptionPane.YES_OPTION) |
---|
| 229 | dispose(); |
---|
| 230 | } |
---|
| 231 | else { |
---|
| 232 | dispose(); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | if (getExitOnClose()) |
---|
| 236 | System.exit(0); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | /** |
---|
| 240 | * invoked when a window is deactivated |
---|
| 241 | * |
---|
| 242 | * @param e the window event |
---|
| 243 | */ |
---|
| 244 | public void windowDeactivated(WindowEvent e) { |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | /** |
---|
| 248 | * invoked when a window is deiconified |
---|
| 249 | * |
---|
| 250 | * @param e the window event |
---|
| 251 | */ |
---|
| 252 | public void windowDeiconified(WindowEvent e) { |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | /** |
---|
| 256 | * invoked when a window is iconified |
---|
| 257 | * |
---|
| 258 | * @param e the window event |
---|
| 259 | */ |
---|
| 260 | public void windowIconified(WindowEvent e) { |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | /** |
---|
| 264 | * invoked when a window is has been opened |
---|
| 265 | * |
---|
| 266 | * @param e the window event |
---|
| 267 | */ |
---|
| 268 | public void windowOpened(WindowEvent e) { |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | /** |
---|
| 272 | * returns only the classname |
---|
| 273 | * |
---|
| 274 | * @return the classname |
---|
| 275 | */ |
---|
| 276 | public String toString() { |
---|
| 277 | return this.getClass().getName(); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | /** |
---|
| 281 | * shows the frame and it tries to load all the arff files that were |
---|
| 282 | * provided as arguments. |
---|
| 283 | * |
---|
| 284 | * @param args the commandline parameters |
---|
| 285 | * @throws Exception if something goes wrong |
---|
| 286 | */ |
---|
| 287 | public static void main(String[] args) throws Exception { |
---|
| 288 | weka.core.logging.Logger.log(weka.core.logging.Logger.Level.INFO, "Logging started"); |
---|
| 289 | LookAndFeel.setLookAndFeel(); |
---|
| 290 | |
---|
| 291 | try { |
---|
| 292 | // uncomment to disable the memory management: |
---|
| 293 | //m_Memory.setEnabled(false); |
---|
| 294 | |
---|
| 295 | m_Viewer = new ArffViewer(); |
---|
| 296 | m_Viewer.setExitOnClose(true); |
---|
| 297 | m_Viewer.setVisible(true); |
---|
| 298 | m_FilesLoaded = false; |
---|
| 299 | m_Args = args; |
---|
| 300 | |
---|
| 301 | Thread memMonitor = new Thread() { |
---|
| 302 | public void run() { |
---|
| 303 | while(true) { |
---|
| 304 | try { |
---|
| 305 | if ( (m_Args.length > 0) && (!m_FilesLoaded) ) { |
---|
| 306 | for (int i = 0; i < m_Args.length; i++) { |
---|
| 307 | System.out.println("Loading " + (i+1) + "/" |
---|
| 308 | + m_Args.length + ": '" + m_Args[i] + "'..."); |
---|
| 309 | m_Viewer.getMainPanel().loadFile(m_Args[i]); |
---|
| 310 | } |
---|
| 311 | m_Viewer.getMainPanel().getTabbedPane().setSelectedIndex(0); |
---|
| 312 | System.out.println("Finished!"); |
---|
| 313 | m_FilesLoaded = true; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | //System.out.println("before sleeping"); |
---|
| 317 | Thread.sleep(4000); |
---|
| 318 | |
---|
| 319 | System.gc(); |
---|
| 320 | |
---|
| 321 | if (m_Memory.isOutOfMemory()) { |
---|
| 322 | // clean up |
---|
| 323 | m_Viewer.dispose(); |
---|
| 324 | m_Viewer = null; |
---|
| 325 | System.gc(); |
---|
| 326 | |
---|
| 327 | // stop threads |
---|
| 328 | m_Memory.stopThreads(); |
---|
| 329 | |
---|
| 330 | // display error |
---|
| 331 | System.err.println("\ndisplayed message:"); |
---|
| 332 | m_Memory.showOutOfMemory(); |
---|
| 333 | System.err.println("\nrestarting..."); |
---|
| 334 | |
---|
| 335 | // restart GUI |
---|
| 336 | System.gc(); |
---|
| 337 | m_Viewer = new ArffViewer(); |
---|
| 338 | m_Viewer.setExitOnClose(true); |
---|
| 339 | m_Viewer.setVisible(true); |
---|
| 340 | // Note: no re-loading of datasets, otherwise we could end up |
---|
| 341 | // in an endless loop! |
---|
| 342 | } |
---|
| 343 | } |
---|
| 344 | catch(InterruptedException ex) { |
---|
| 345 | ex.printStackTrace(); |
---|
| 346 | } |
---|
| 347 | } |
---|
| 348 | } |
---|
| 349 | }; |
---|
| 350 | |
---|
| 351 | memMonitor.setPriority(Thread.NORM_PRIORITY); |
---|
| 352 | memMonitor.start(); |
---|
| 353 | } |
---|
| 354 | catch (Exception ex) { |
---|
| 355 | ex.printStackTrace(); |
---|
| 356 | System.err.println(ex.getMessage()); |
---|
| 357 | } |
---|
| 358 | } |
---|
| 359 | } |
---|