1 19 20 package org.apache.excalibur.instrument.client; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Dimension ; 24 import java.awt.Frame ; 25 import java.awt.Toolkit ; 26 import java.awt.event.WindowAdapter ; 27 import java.awt.event.WindowEvent ; 28 import java.io.ByteArrayOutputStream ; 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.FileOutputStream ; 32 import java.io.InputStream ; 33 import java.io.OutputStream ; 34 import java.net.MalformedURLException ; 35 import java.net.URL ; 36 import java.util.ArrayList ; 37 import java.util.HashMap ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 import java.util.Map ; 41 42 import javax.swing.ImageIcon ; 43 import javax.swing.JDesktopPane ; 44 import javax.swing.JFileChooser ; 45 import javax.swing.JFrame ; 46 import javax.swing.JInternalFrame ; 47 import javax.swing.JOptionPane ; 48 import javax.swing.JPanel ; 49 import javax.swing.JSplitPane ; 50 import javax.swing.JTabbedPane ; 51 import javax.swing.SwingUtilities ; 52 import javax.swing.border.BevelBorder ; 53 import javax.swing.filechooser.FileFilter ; 54 55 import org.apache.avalon.framework.configuration.Configuration; 56 import org.apache.avalon.framework.configuration.ConfigurationException; 57 import org.apache.avalon.framework.configuration.DefaultConfiguration; 58 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 59 import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer; 60 import org.apache.avalon.framework.container.ContainerUtil; 61 import org.apache.avalon.framework.logger.LogEnabled; 62 import org.apache.avalon.framework.logger.Logger; 63 64 import org.apache.excalibur.instrument.client.http.HTTPInstrumentManagerConnection; 65 66 72 class InstrumentClientFrame 73 extends JFrame 74 implements InstrumentManagerConnectionListener, LogEnabled 75 { 76 protected static final String MEDIA_PATH = "org/apache/excalibur/instrument/client/media/"; 77 78 static final String SHUTDOWN_HOOK_NAME = "InstrumentClientShutdownHook"; 79 80 private String m_title; 81 82 private JTabbedPane m_connectionsPane; 83 private JDesktopPane m_desktopPane; 84 private JSplitPane m_splitPane; 85 private MenuBar m_menuBar; 86 private StatusBar m_statusBar; 87 88 private boolean m_antialias; 89 90 private File m_desktopFile; 91 private File m_desktopFileDir; 92 93 private Map m_connections = new HashMap (); 94 private InstrumentManagerConnection[] m_connectionArray; 95 96 97 private Thread m_hook; 98 99 private Logger m_logger; 100 101 104 109 InstrumentClientFrame( String title ) 110 { 111 super(); 112 113 m_title = title; 114 } 115 116 public void initialize() 117 { 118 init(); 119 120 ClassLoader cl = this.getClass().getClassLoader(); 121 setIconImage( new ImageIcon ( cl.getResource( MEDIA_PATH + "client.gif") ).getImage() ); 122 } 123 124 127 133 public void opened( InstrumentManagerConnection connection ) 134 { 135 getLogger().debug( "opened: " + connection.getKey() ); 136 updateConnectionTab( connection ); 137 } 138 139 145 public void closed( InstrumentManagerConnection connection ) 146 { 147 getLogger().debug( "closed: " + connection.getKey() ); 148 updateConnectionTab( connection ); 149 } 150 151 156 public void deleted( InstrumentManagerConnection connection ) 157 { 158 getLogger().debug( "deleted: " + connection.getKey() ); 159 int tabIndex = m_connectionsPane.indexOfComponent( connection ); 161 if ( tabIndex >= 0 ) 162 { 163 if ( Thread.currentThread() != m_hook ) 165 { 166 m_connectionsPane.remove( connection ); 167 } 168 } 169 170 connection.removeInstrumentManagerConnectionListener( this ); 171 Object key = connection.getKey(); 172 synchronized ( m_connections ) 173 { 174 m_connections.remove( key ); 175 m_connectionArray = null; 176 } 177 } 178 179 182 public void enableLogging( Logger logger ) 183 { 184 m_logger = logger; 185 } 186 187 Logger getLogger() 188 { 189 return m_logger; 190 } 191 192 195 202 void setDefaultStateFile( File defaultStateFile ) 203 { 204 File defaultStateFileDir = defaultStateFile.getParentFile(); 206 if ( defaultStateFileDir.exists() ) 207 { 208 m_desktopFileDir = defaultStateFileDir; 209 } 210 if ( defaultStateFile.exists() ) 211 { 212 try 213 { 214 m_desktopFile = null; 215 loadStateFromFile( defaultStateFile, true ); 216 m_desktopFile = defaultStateFile; 217 } 218 catch( Exception e ) 219 { 220 String msg = "Unable to load desktop file."; 221 getLogger().debug( msg, e ); 222 showErrorDialog( msg, e ); 223 } 224 updateTitle(); 225 } 226 } 227 228 237 void loadStateFromFile( File stateFile, boolean showErrorDialog ) throws Exception 238 { 239 long now = System.currentTimeMillis(); 240 getLogger().debug( "Loading Instrument Client state from: " + 241 stateFile.getAbsolutePath() ); 242 243 FileInputStream is = new FileInputStream ( stateFile ); 244 try 245 { 246 loadStateFromStream( is, showErrorDialog ); 247 } 248 finally 249 { 250 is.close(); 251 } 252 253 getLogger().debug( "Loading Instrument Client state took " + 254 ( System.currentTimeMillis() - now ) + "ms." ); 255 } 256 257 266 void loadStateFromStream( InputStream is, boolean showErrorDialog ) throws Exception 267 { 268 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 270 Configuration stateConfig = builder.build( is ); 271 272 loadStateFromConfiguration( stateConfig, showErrorDialog ); 273 } 274 275 285 void loadStateFromConfiguration( Configuration state, boolean showErrorDialog ) 286 throws ConfigurationException 287 { 288 290 try 291 { 292 Configuration frameState = state.getChild( "frame" ); 294 setLocation( frameState.getAttributeAsInteger( "x" ), 296 frameState.getAttributeAsInteger( "y" ) ); 297 setSize( frameState.getAttributeAsInteger( "width" ), 298 frameState.getAttributeAsInteger( "height" ) ); 299 if ( frameState.getAttributeAsBoolean( "iconized", false ) ) 301 { 302 setState( Frame.ICONIFIED ); 303 } 304 m_splitPane.setDividerLocation( 306 frameState.getAttributeAsInteger( "divider-location" ) ); 307 m_splitPane.setLastDividerLocation( 308 frameState.getAttributeAsInteger( "last-divider-location" ) ); 309 m_antialias = frameState.getAttributeAsBoolean( "antialias", false ); 311 } 312 catch ( ConfigurationException e ) 313 { 314 String msg = "Unable to fully load the frame state."; 315 if ( showErrorDialog ) 316 { 317 getLogger().debug( msg, e ); 318 showErrorDialog( msg, e ); 319 } 320 else 321 { 322 getLogger().warn( msg, e ); 323 } 324 } 325 326 show(); 328 329 Map oldConnections; 331 synchronized( m_connections ) 332 { 333 oldConnections = new HashMap ( m_connections ); 334 } 335 336 Configuration[] connConfs = state.getChildren( "connection" ); 338 for( int i = 0; i < connConfs.length; i++ ) 339 { 340 Configuration connConf = connConfs[ i ]; 341 String tURL = connConf.getAttribute( "url" ); 342 URL url; 343 try 344 { 345 url = new URL ( tURL ); 346 } 347 catch ( MalformedURLException e ) 348 { 349 throw new ConfigurationException( "Invalid url, '" + tURL + "'", e ); 350 } 351 352 InstrumentManagerConnection conn; 353 synchronized ( m_connections ) 354 { 355 conn = (InstrumentManagerConnection)m_connections.get( url ); 356 357 if ( conn == null ) 358 { 359 conn = createConnection( url ); 361 } 362 363 oldConnections.remove( url ); 364 } 365 366 try 368 { 369 conn.loadState( connConf ); 370 } 371 catch ( ConfigurationException e ) 372 { 373 String msg = "Unable to fully load the state of connection, " + conn.getKey(); 374 if ( showErrorDialog ) 375 { 376 getLogger().debug( msg, e ); 377 showErrorDialog( msg, e ); 378 } 379 else 380 { 381 getLogger().warn( msg, e ); 382 } 383 } 384 } 385 386 for ( Iterator iter = oldConnections.values().iterator(); iter.hasNext(); ) 388 { 389 InstrumentManagerConnection conn = (InstrumentManagerConnection)iter.next(); 390 conn.delete(); 391 } 392 393 394 JInternalFrame frames[] = m_desktopPane.getAllFrames(); 396 for ( int i = 0; i < frames.length; i++ ) 397 { 398 if ( frames[i] instanceof AbstractInternalFrame ) 399 { 400 ((AbstractInternalFrame)frames[i]).hideFrame(); 401 } 402 } 403 404 Configuration[] frameConfs = state.getChildren( "inner-frame" ); 406 for( int i = 0; i < frameConfs.length; i++ ) 407 { 408 Configuration frameConf = frameConfs[ i ]; 409 String type = frameConf.getAttribute( "type" ); 410 411 if ( type.equals( InstrumentSampleFrame.FRAME_TYPE ) ) 412 { 413 String tURL = frameConf.getAttribute( "url" ); 415 URL url; 416 try 417 { 418 url = new URL ( tURL ); 419 } 420 catch ( MalformedURLException e ) 421 { 422 throw new ConfigurationException( "Invalid url, '" + tURL + "'", e ); 423 } 424 425 InstrumentManagerConnection connection = getConnection( url ); 426 if ( connection == null ) 427 { 428 String msg = "Sample frame not being loaded becase no connection to " + 430 url.toExternalForm() + " exists."; 431 if ( showErrorDialog ) 432 { 433 getLogger().debug( msg ); 434 showErrorDialog( msg ); 435 } 436 else 437 { 438 getLogger().warn( msg ); 439 } 440 } else { 441 try 443 { 444 connection.loadSampleFrame( frameConf ); 445 } 446 catch ( ConfigurationException e ) 447 { 448 String msg = 449 "Unable to fully load the state of an inner frame for sample: " + 450 frameConf.getAttribute( "sample", "Sample name missing" ); 451 if ( showErrorDialog ) 452 { 453 getLogger().debug( msg, e ); 454 showErrorDialog( msg, e ); 455 } 456 else 457 { 458 getLogger().warn( msg, e ); 459 } 460 } 461 } 462 } 463 else 464 { 465 getLogger().warn( "Not loading inner frame due to unknown type: " + type ); 467 } 468 } 469 } 470 471 480 void saveStateToFile( File stateFile ) throws Exception 481 { 482 long now = System.currentTimeMillis(); 483 getLogger().debug( "Saving Instrument Client state to: " + stateFile.getAbsolutePath() ); 484 485 ByteArrayOutputStream os = new ByteArrayOutputStream (); 490 byte[] data; 491 try 492 { 493 saveStateToStream( os ); 494 data = os.toByteArray(); 495 } 496 finally 497 { 498 os.close(); 499 } 500 501 File renameFile = null; 504 boolean success = false; 505 if( stateFile.exists() ) 506 { 507 renameFile = new File ( stateFile.getAbsolutePath() + "." + now + ".backup" ); 508 stateFile.renameTo( renameFile ); 509 } 510 511 FileOutputStream fos = new FileOutputStream ( stateFile ); 513 try 514 { 515 fos.write( data ); 516 success = true; 517 } 518 finally 519 { 520 fos.close(); 521 522 if ( !success ) 523 { 524 stateFile.delete(); 526 } 527 528 if ( renameFile != null ) 530 { 531 if ( success ) 532 { 533 renameFile.delete(); 535 } 536 else 537 { 538 renameFile.renameTo( stateFile ); 540 } 541 } 542 } 543 544 getLogger().debug( "Saving Instrument Client state took " + 545 ( System.currentTimeMillis() - now ) + "ms." ); 546 } 547 548 555 void saveStateToStream( OutputStream os ) throws Exception 556 { 557 Configuration stateConfig = saveStateToConfiguration(); 558 559 DefaultConfigurationSerializer serializer = new DefaultConfigurationSerializer(); 561 serializer.setIndent( true ); 562 serializer.serialize( os, stateConfig ); 563 } 564 565 570 Configuration saveStateToConfiguration() 571 { 572 DefaultConfiguration state = new DefaultConfiguration( "instrument-client-state", "-" ); 573 574 DefaultConfiguration frameState = new DefaultConfiguration( "frame", "-" ); 576 frameState.setAttribute( "x", Integer.toString( getX() ) ); 578 frameState.setAttribute( "y", Integer.toString( getY() ) ); 579 frameState.setAttribute( "width", Integer.toString( getWidth() ) ); 580 frameState.setAttribute( "height", Integer.toString( getHeight() ) ); 581 if ( getState() == Frame.ICONIFIED ) 583 { 584 frameState.setAttribute( "iconized", "true" ); 585 } 586 frameState.setAttribute( "divider-location", Integer.toString( m_splitPane.getDividerLocation() ) ); 588 frameState.setAttribute( "last-divider-location", Integer.toString( m_splitPane.getLastDividerLocation() ) ); 589 frameState.setAttribute( "antialias", Boolean.toString( m_antialias ) ); 591 state.addChild( frameState ); 593 594 InstrumentManagerConnection[] connections = getConnections(); 596 for ( int i = 0; i < connections.length; i++ ) 597 { 598 state.addChild( connections[i].saveState() ); 599 } 600 601 JInternalFrame frames[] = m_desktopPane.getAllFrames(); 603 for ( int i = 0; i < frames.length; i++ ) 604 { 605 if ( frames[i] instanceof AbstractInternalFrame ) 606 { 607 AbstractInternalFrame internalFrame = (AbstractInternalFrame)frames[i]; 608 state.addChild( internalFrame.getState() ); 609 } 610 } 611 612 return state; 613 } 614 615 618 private void init() 619 { 620 updateTitle(); 621 622 m_hook = new Thread ( SHUTDOWN_HOOK_NAME ) 624 { 625 public void run() 626 { 627 getLogger().debug( "InstrumentClientFrame.shutdownHook start"); 628 629 shutdown(); 630 631 getLogger().debug( "InstrumentClientFrame.shutdownHook end"); 632 } 633 }; 634 Runtime.getRuntime().addShutdownHook( m_hook ); 635 636 addWindowListener( new WindowAdapter () 638 { 639 public void windowClosing( WindowEvent event ) 640 { 641 fileExit(); 642 } 643 }); 644 645 getContentPane().setLayout( new BorderLayout () ); 646 647 m_connectionsPane = new JTabbedPane ( JTabbedPane.TOP ); 649 650 m_desktopPane = new DesktopPane(); 652 JPanel dBorder = new JPanel (); 653 dBorder.setBorder( new BevelBorder ( BevelBorder.LOWERED ) ); 654 dBorder.setLayout( new BorderLayout () ); 655 dBorder.add( m_desktopPane, BorderLayout.CENTER ); 656 657 m_splitPane = 659 new JSplitPane ( JSplitPane.HORIZONTAL_SPLIT, true, m_connectionsPane, dBorder ); 660 m_splitPane.setOneTouchExpandable( true ); 661 m_splitPane.setDividerLocation( 250 ); 662 663 getContentPane().add( m_splitPane, BorderLayout.CENTER ); 664 665 m_menuBar = new MenuBar( this ); 667 setJMenuBar( m_menuBar ); 668 669 m_statusBar = new StatusBar(); 670 getContentPane().add( m_statusBar, BorderLayout.SOUTH ); 671 672 Toolkit toolkit = getToolkit(); 673 Dimension screenSize = toolkit.getScreenSize(); 674 675 int screenWidth = screenSize.width; 678 int screenHeight = screenSize.height; 679 setLocation( screenWidth / 20, screenHeight / 20 ); 680 setSize( screenWidth * 9 / 10, screenHeight * 8 / 10 ); 681 } 682 683 boolean isAntialias() 684 { 685 return m_antialias; 686 } 687 void setAntialias( boolean antialias ) 688 { 689 m_antialias = antialias; 690 } 691 692 void updateConnectionTab( InstrumentManagerConnection connection ) 693 { 694 int tabIndex = m_connectionsPane.indexOfComponent( connection ); 696 if ( tabIndex >= 0 ) 697 { 698 m_connectionsPane.setTitleAt( tabIndex, connection.getTabTitle() ); 699 m_connectionsPane.setToolTipTextAt( tabIndex, connection.getTabTooltip() ); 700 } 701 } 702 703 private void updateTitle() 704 { 705 if( m_desktopFile == null ) 706 { 707 setTitle( m_title ); 708 } 709 else 710 { 711 setTitle( m_title + " - " + m_desktopFile.getAbsolutePath() ); 712 } 713 } 714 715 void setStatusMessage( String message ) 716 { 717 m_statusBar.setStatusMessage( message ); 718 } 719 720 JDesktopPane getDesktopPane() 721 { 722 return m_desktopPane; 723 } 724 725 void closeAllFrames() 726 { 727 JInternalFrame [] frames = m_desktopPane.getAllFrames(); 728 for( int i = 0; i < frames.length; i++ ) 729 { 730 frames[ i ].setVisible( false ); 731 frames[ i ].dispose(); 732 } 733 } 734 735 738 void tileFrames() 739 { 740 JInternalFrame [] openFrames = getOpenFrames(); 741 742 int count = openFrames.length; 743 if ( count == 0) 744 { 745 return; 746 } 747 748 float targetRatio = 5.0f; 753 754 Dimension size = getDesktopPane().getSize(); 755 int cols = 1; 756 int rows = count; 757 int frameWidth = size.width / cols; 758 int frameHeight = size.height / rows; 759 float ratio = (float)frameWidth / frameHeight; 760 while ( ( rows > 1 ) && ( ( ratio > targetRatio ) || 761 ( ( frameHeight < 70 ) && ( frameWidth > 100 ) ) ) ) 762 { 763 cols++; 764 rows = (int)Math.ceil( (float)count / cols ); 765 frameWidth = size.width / cols; 766 frameHeight = size.height / rows; 767 ratio = (float)frameWidth / frameHeight; 768 } 769 770 reorganizeFrames( rows, cols, openFrames ); 771 } 772 773 778 JInternalFrame [] getOpenFrames() 779 { 780 JInternalFrame [] frames = m_desktopPane.getAllFrames(); 781 int count = frames.length; 782 783 if (count == 0) 785 { 786 return frames; 788 } 789 790 List openFrames = new ArrayList (); 792 for ( int i = 0; i < count; i++ ) 793 { 794 JInternalFrame f = frames[i]; 795 if( ( f.isClosed() == false ) && ( f.isIcon() == false ) ) 796 { 797 openFrames.add( f ); 798 } 799 } 800 801 frames = new JInternalFrame [ openFrames.size() ]; 803 openFrames.toArray( frames ); 804 805 return frames; 806 } 807 808 816 void reorganizeFrames( int rows, int cols, JInternalFrame [] frames ) 817 { 818 Dimension desktopsize = m_desktopPane.getSize(); 820 int w = desktopsize.width / cols; 821 int h = desktopsize.height / rows; 822 int x = 0; 823 int y = 0; 824 int count = frames.length; 825 826 for ( int i = 0; i < rows; ++i) 827 { 828 for ( int j = 0; j < cols && ( ( i * cols ) + j < count ); ++j ) 829 { 830 JInternalFrame f = frames[ ( i * cols ) + j ]; 831 m_desktopPane.getDesktopManager().resizeFrame( f, x, y, w, h ); 832 x += w; 833 } 834 y += h; 835 x = 0; 836 } 837 } 838 839 842 void tileFramesH() 843 { 844 JInternalFrame [] openFrames = getOpenFrames(); 845 846 int count = openFrames.length; 847 if ( count == 0 ) 848 { 849 return; 850 } 851 reorganizeFrames( count, 1, openFrames ); 852 } 853 854 857 void tileFramesV() 858 { 859 JInternalFrame [] openFrames = getOpenFrames(); 860 861 int count=openFrames.length; 862 if ( count == 0) 863 { 864 return; 865 } 866 reorganizeFrames( 1, count, openFrames ); 867 } 868 869 InstrumentManagerConnection[] getConnections() 870 { 871 InstrumentManagerConnection[] array = m_connectionArray; 873 if ( array == null ) 874 { 875 synchronized ( m_connections ) 876 { 877 m_connectionArray = new InstrumentManagerConnection[m_connections.size()]; 878 m_connections.values().toArray( m_connectionArray ); 879 array = m_connectionArray; 880 } 881 } 882 return array; 883 } 884 885 InstrumentManagerConnection getConnection( URL url ) 886 { 887 synchronized ( m_connections ) 888 { 889 return (InstrumentManagerConnection)m_connections.get( url ); 890 } 891 } 892 893 void showConnectDialog() 894 { 895 SwingUtilities.invokeLater( new Runnable () 896 { 897 public void run() 898 { 899 URL defaultURL; 900 try 901 { 902 defaultURL = new URL ( "http://localhost:15080" ); 903 } 904 catch ( MalformedURLException e ) 905 { 906 e.printStackTrace(); 908 return; 909 } 910 911 ConnectDialog dialog = new ConnectDialog( InstrumentClientFrame.this ); 912 dialog.setURL( defaultURL ); 913 dialog.show(); 914 if ( dialog.getAction() == ConnectDialog.BUTTON_OK ) 915 { 916 synchronized( m_connections ) 917 { 918 createConnection( dialog.getURL() ); 919 } 920 } 921 } 922 } ); 923 } 924 925 934 private InstrumentManagerConnection createConnection( URL url ) 935 { 936 InstrumentManagerConnection conn = new HTTPInstrumentManagerConnection( url ); 937 ContainerUtil.enableLogging( 938 conn, getLogger().getChildLogger( url.getHost() + ":" + url.getPort() ) ); 939 conn.setFrame( this ); 940 conn.init(); 941 m_connections.put( conn.getKey(), conn ); 942 m_connectionArray = null; 943 944 conn.addInstrumentManagerConnectionListener( this ); 945 946 m_connectionsPane.add( conn.getTabTitle(), conn ); 947 948 return conn; 949 } 950 951 private void showErrorDialog( String message ) 952 { 953 JOptionPane.showMessageDialog( this, 954 "<html><body><font color=\"black\">" + message + "</font>" + 955 "</body></html>", m_title + " Error", 956 JOptionPane.ERROR_MESSAGE ); 957 } 958 959 private void showErrorDialog( String message, Throwable t ) 960 { 961 JOptionPane.showMessageDialog( this, 962 "<html><body><font color=\"black\">" + message + 963 "</font><br><br><font color=\"black\">Reason: " + 964 t.getMessage() + "</font></body></html>", 965 m_title + " Error", JOptionPane.ERROR_MESSAGE ); 966 } 967 968 969 972 private void shutdown() 973 { 974 getLogger().debug( "InstrumentClientFrame.shutdown()" ); 975 boolean fallThrough = false; 976 if ( m_hook != null ) 977 { 978 if ( m_hook == Thread.currentThread() ) 979 { 980 fallThrough = true; 982 } 983 else 984 { 985 Runtime.getRuntime().removeShutdownHook( m_hook ); 987 m_hook = null; 988 } 989 } 990 991 InstrumentManagerConnection[] connections = getConnections(); 993 for ( int i = 0; i < connections.length; i++ ) 994 { 995 connections[i].delete(); 996 } 997 998 if ( !fallThrough ) 999 { 1000 System.exit( 1 ); 1002 } 1003 } 1004 1005 1008 1011 void fileNew() 1012 { 1013 m_desktopFile = null; 1014 closeAllFrames(); 1015 updateTitle(); 1016 } 1017 1018 1021 void fileOpen() 1022 { 1023 JFileChooser chooser = new JFileChooser (); 1024 1025 FileFilter filter = new FileFilter () 1026 { 1027 public boolean accept( File f ) 1028 { 1029 if( f.isDirectory() ) 1030 { 1031 return true; 1032 } 1033 else 1034 { 1035 return f.getName().endsWith( ".desktop" ); 1036 } 1037 } 1038 1039 public String getDescription() 1040 { 1041 return "Desktop state files"; 1042 } 1043 }; 1044 1045 if ( m_desktopFileDir != null ) 1046 { 1047 chooser.setCurrentDirectory( m_desktopFileDir ); 1048 } 1049 else 1050 { 1051 chooser.setCurrentDirectory( new File ( System.getProperty( "user.dir" ) ) ); 1052 } 1053 1054 chooser.setFileFilter( filter ); 1055 1056 int returnVal = chooser.showOpenDialog( this ); 1057 if( returnVal == JFileChooser.APPROVE_OPTION ) 1058 { 1059 try 1060 { 1061 m_desktopFile = null; 1062 File file = chooser.getSelectedFile(); 1063 m_desktopFileDir = file.getParentFile(); 1064 loadStateFromFile( file, true ); 1065 m_desktopFile = file; 1066 } 1067 catch( Exception e ) 1068 { 1069 String msg = "Unable to load desktop file."; 1070 getLogger().debug( msg, e ); 1071 showErrorDialog( msg, e ); 1072 } 1073 updateTitle(); 1074 } 1075 } 1076 1077 void fileSave() 1078 { 1079 if( m_desktopFile != null ) 1080 { 1081 try 1082 { 1083 saveStateToFile( m_desktopFile ); 1084 } 1085 catch( Exception e ) 1086 { 1087 String msg = "Unable to save desktop file."; 1088 getLogger().debug( msg, e ); 1089 showErrorDialog( msg, e ); 1090 } 1091 } 1092 else 1093 { 1094 fileSaveAs(); 1095 } 1096 } 1097 1098 void fileSaveAs() 1099 { 1100 JFileChooser chooser = new JFileChooser (); 1101 1102 FileFilter filter = new FileFilter () 1103 { 1104 public boolean accept( File f ) 1105 { 1106 if( f.isDirectory() ) 1107 { 1108 return true; 1109 } 1110 else 1111 { 1112 return f.getName().endsWith( ".desktop" ); 1113 } 1114 } 1115 1116 public String getDescription() 1117 { 1118 return "Desktop state files"; 1119 } 1120 }; 1121 1122 if ( m_desktopFileDir != null ) 1123 { 1124 chooser.setCurrentDirectory( m_desktopFileDir ); 1125 } 1126 else 1127 { 1128 chooser.setCurrentDirectory( new File ( System.getProperty( "user.dir" ) ) ); 1129 } 1130 1131 chooser.setFileFilter( filter ); 1132 1133 int returnVal = chooser.showSaveDialog( this ); 1134 if( returnVal == JFileChooser.APPROVE_OPTION ) 1135 { 1136 File file = chooser.getSelectedFile(); 1137 if( file.getName().indexOf( '.' ) < 0 ) 1138 { 1139 file = new File ( file.getAbsolutePath() + ".desktop" ); 1141 } 1142 1143 try 1144 { 1145 saveStateToFile( file ); 1146 1147 m_desktopFile = file; 1150 m_desktopFileDir = m_desktopFile.getParentFile(); 1151 1152 } 1153 catch( Exception e ) 1154 { 1155 String msg = "Unable to save desktop file."; 1156 getLogger().debug( msg, e ); 1157 showErrorDialog( msg, e ); 1158 } 1159 updateTitle(); 1160 } 1161 } 1162 1163 1166 void fileExit() 1167 { 1168 SwingUtilities.invokeLater( new Runnable () 1169 { 1170 public void run() 1171 { 1172 shutdown(); 1173 } 1174 } ); 1175 } 1176} 1177 1178 | Popular Tags |