home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-06-18 | 18.1 KB | 556 lines | [TEXT/dosa] |
- // DesktopFrame.java : this is a Java source code file for the program Facade.
- // Copyright 1998, Andrew S. Downs
- // andrew.downs@tulane.edu
- //
- // This source code is distributed as freeware.
- // Just keep this author information in the file. Enjoy!
-
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.util.*;
- import com.sun.java.swing.*;
-
- public class DesktopFrame extends DesktopWindow implements Serializable, MouseListener, MouseMotionListener, FilenameFilter {
- boolean drag = false;
- boolean shade = false;
- boolean zoom = false;
- boolean resize = false;
-
- int resizeX = 0;
- int resizeY = 0;
- int initialX = 0;
- int initialY = 0;
- int prevX = 0;
- int prevY = 0;
- int prevWidth = 0;
- int prevHeight = 0;
- int prevHeightShade = 0;
-
- transient Polygon upArrowPolygon = new Polygon();
- transient Polygon downArrowPolygon = new Polygon();
-
- transient Rectangle sizeBox = new Rectangle( 2, 2, 10, 10 );
- transient Rectangle closeBox = new Rectangle( 2, 2, 10, 10 );
- transient Rectangle titleBar = new Rectangle( 12, 2, 10, 10 );
- transient Rectangle zoomBox = new Rectangle( 12, 2, 10, 10 );
- transient Rectangle shadeBox = new Rectangle( 12, 2, 10, 10 );
- transient Rectangle upArrowBox = new Rectangle( -1, -1, 10, 10 );
- transient Rectangle downArrowBox = new Rectangle( -1, -1, 10, 10 );
- transient Rectangle vScrollBar = new Rectangle( -1, -1, 16, 10 );
-
- String path;
- transient String[] contents = new String[ 20 ];
-
- transient JScrollPane pane;
- transient JPanel panel;
-
- DesktopFrame() {
- super();
- this.addMouseListener( this );
- this.addMouseMotionListener( this );
- Desktop.addWindow( this );
- Desktop.setTopFrame( this );
- }
-
- DesktopFrame( String s ) {
- super();
- this.setPath( s );
- this.setLayout( null );
- this.setBounds( 100, 100, 300, 200 );
- this.addFileTree( s );
- this.addMouseListener( this );
- this.addMouseMotionListener( this );
- Desktop.addWindow( this );
- Desktop.setTopFrame( this );
- }
-
- public boolean accept( File f, String s ) {
- return true;
- }
-
- public Insets getInsets() {
- // return new Insets( 20, 5, 10, 5 );
- return new Insets( 20, 5, 4, 4 );
- }
-
- public void addFileTree( String s ) {
- pane = new JScrollPane();
- pane.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
- pane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
- // pane.getViewport().setLayout( new GridLayout( 3, 3 ) );
-
- String defaultVolume = s;
-
- if ( defaultVolume == null )
- return;
-
- File theDir = new File( defaultVolume );
-
- if ( !theDir.isDirectory() )
- return;
-
- contents = theDir.list();
- int tempLength = 0;
-
- if ( contents == null ) {
- System.out.println( "path contents == null" );
- //return;
- }
- else
- tempLength = contents.length;
-
- JPanel p = new JPanel();
- p.setLayout( new GridLayout( 5, 3, 5, 5 ) );
- // pane.getViewport().setLayout( new GridLayout( 4, ( tempLength / 3 ) + 1 ) );
-
- char pathSeparatorChar = theDir.pathSeparatorChar;
- char separatorChar = theDir.separatorChar;
- Vector v = new Vector();
- v.addElement( Global.spaceSep );
- // v.addElement( Global.sep00 );
-
- int loc = 0;
- int k = 0;
-
- for ( int j = 0; j < tempLength; j++ ) {
- File tempFile = new File( theDir, contents[ j ] );
-
- contents[ j ] = new String( contents[ j ].replace( pathSeparatorChar, ' ' ) );
- contents[ j ] = new String( contents[ j ].replace( separatorChar, ' ' ) );
-
- for ( int l = 0; l < v.size(); l++ ) {
- // Parse root volume name.
- // Remove leading '%20' character.
- loc = contents[ j ].indexOf( ( String )v.elementAt( l ) );
-
- if ( loc == 0 ) {
- contents[ j ] = new String( contents[ j ].substring( ( ( String )v.elementAt( l ) ).length() ) );
- }
-
- // Volume name includes first '%20'.
- // Rework this for MacOS.
- loc = contents[ j ].indexOf( ( String )v.elementAt( l ) );
-
- while ( ( loc > 0 ) && ( loc < contents[ j ].length() + 1 ) ) {
- String s1 = new String( contents[ j ].substring( 0, loc ) );
- String s2 = new String( contents[ j ].substring( loc + ( ( String )v.elementAt( l ) ).length() ) );
- contents[ j ] = new String( s1 + " " + s2 );
- loc = contents[ j ].indexOf( ( String )v.elementAt( l ) );
- }
- }
-
- Image theImage;
- int tempWidth = 0;
- DesktopFrameItem d;
- ImageIcon ii = new ImageIcon();
-
- if ( tempFile.isFile() ) {
- d = new DesktopDoc();
- ii = new ImageIcon( ( ( DesktopDoc )d ).getImage() );
- }
- else {
- d = new DesktopFolder();
- ii = new ImageIcon( ( ( DesktopFolder )d ).getImage() );
- }
-
- d.setLabel( contents[ j ] );
- d.setText( contents[ j ] );
- d.setPath( this.getPath() + System.getProperty( "file.separator" ) + contents[ j ] );
- d.setIcon( ii );
-
- d.setHorizontalAlignment( JLabel.CENTER );
- d.setHorizontalTextPosition( JLabel.CENTER );
- d.setVerticalTextPosition( JLabel.BOTTOM );
- ii.setImageObserver( d );
- p.add( d );
- }
-
- //
-
- p.setVisible( true );
-
- pane.getViewport().add( p, "Center" );
- pane.setVisible( true );
-
- panel = new JPanel();
- panel.setLayout( new BorderLayout() );
- panel.add( pane, "Center" );
- this.add( panel );
- panel.reshape( this.getInsets().left, this.getInsets().top, this.getBounds().width - this.getInsets().right - this.getInsets().left, this.getBounds().height - this.getInsets().top - this.getInsets().bottom );
- panel.setVisible( true );
- this.pack();
- this.validate();
- }
-
- public void paint( Graphics g ) {
- // public void paint( Graphics g2 ) {
- g.setFont( this.getFont() );
- FontMetrics theFontMetrics = g.getFontMetrics();
-
- if ( this.drag ) {
- // g.setColor( Color.lightGray );
- // g.fillRect( 0, 0, this.getBounds().width, this.getBounds().height );
- g.setColor( Color.black );
- g.drawRect( 0, 0, this.getBounds().width - 1, this.getBounds().height - 1 );
- //g2.drawImage( offscreenImage, 0, 0, this );
- return;
- }
-
- String defaultVolume = this.getPath();
-
- if ( defaultVolume == null )
- return;
-
- File theDir = new File( defaultVolume );
-
- if ( !theDir.isDirectory() )
- return;
-
- contents = theDir.list();
- int tempLength = 0;
-
- if ( contents == null ) {
- System.out.println( "contents == null" );
- //return;
- }
- else
- tempLength = contents.length;
-
- char pathSeparatorChar = theDir.pathSeparatorChar;
- char separatorChar = theDir.separatorChar;
-
- g.setColor( Color.lightGray );
-
- int tempHeight = 20;
-
- g.fillRect( 0, 0, this.getBounds().width, this.getBounds().height );
-
- if ( this.isVisible() )
- g.setColor( Color.black );
-
- // Content area
- g.drawRect( 0, 0, this.getBounds().width + 1, this.getBounds().height + 1 );
-
- // Drop shadow border
- g.fillRect( this.getBounds().width - 1, 0, 1, this.getBounds().height );
- g.fillRect( 0, this.getBounds().height - 1, this.getBounds().width, 1 );
-
- // Close box
- this.getCloseBox().setBounds( 5, 5, 10, 10 );
- g.setColor( Color.lightGray );
- g.fillRect( this.getCloseBox().getBounds().x, this.getCloseBox().getBounds().y, this.getCloseBox().getBounds().width, this.getCloseBox().getBounds().height );
- g.setColor( Color.black );
- g.drawRect( this.getCloseBox().getBounds().x, this.getCloseBox().getBounds().y, this.getCloseBox().getBounds().width, this.getCloseBox().getBounds().height );
-
- // Shade box
- this.getShadeBox().setBounds( this.getBounds().width - 15, 5, 10, 10 );
- g.setColor( Color.lightGray );
- g.fillRect( this.getShadeBox().getBounds().x, this.getShadeBox().getBounds().y, this.getShadeBox().getBounds().width, this.getShadeBox().getBounds().height );
- g.setColor( Color.black );
- g.drawRect( this.getShadeBox().getBounds().x, this.getShadeBox().getBounds().y, this.getShadeBox().getBounds().width, this.getShadeBox().getBounds().height );
- g.drawRect( this.getShadeBox().getBounds().x, this.getShadeBox().getBounds().y + ( this.getShadeBox().getBounds().height / 2 ) - 1, this.getShadeBox().getBounds().width, 2 );
-
- // Zoom box
- this.getZoomBox().setBounds( this.getShadeBox().getBounds().x - 15, 5, 10, 10 );
- g.setColor( Color.lightGray );
- g.fillRect( this.getZoomBox().getBounds().x, this.getZoomBox().getBounds().y, this.getZoomBox().getBounds().width, this.getZoomBox().getBounds().height );
- g.setColor( Color.black );
- g.drawRect( this.getZoomBox().getBounds().x, this.getZoomBox().getBounds().y, this.getZoomBox().getBounds().width, this.getZoomBox().getBounds().height );
- g.drawRect( this.getZoomBox().getBounds().x, this.getZoomBox().getBounds().y, this.getZoomBox().getBounds().width / 2, this.getZoomBox().getBounds().height / 2 );
-
- int endPoint = ( this.getBounds().width / 2 ) - ( theFontMetrics.stringWidth( this.getLabel() ) / 2 ) - 5;
- int startH = this.getCloseBox().getBounds().width + 10;
- int startV = this.getCloseBox().getBounds().y;
-
- // Draw lines in title bar
- for ( int count = 0; count < 5; count++ ) {
- g.setColor( Color.white );
- g.drawLine( startH, startV, endPoint, startV );
- endPoint += theFontMetrics.stringWidth( this.getLabel() ) + 10;
- g.drawLine( endPoint, startV, this.getZoomBox().getBounds().x - 5, startV );
- endPoint -= theFontMetrics.stringWidth( this.getLabel() ) + 10;
- startV++;
- g.setColor( Color.darkGray );
- g.drawLine( startH, startV, endPoint, startV );
- endPoint += theFontMetrics.stringWidth( this.getLabel() ) + 10;
- g.drawLine( endPoint, startV, this.getZoomBox().getBounds().x - 5, startV );
- endPoint -= theFontMetrics.stringWidth( this.getLabel() ) + 10;
- startV++;
- }
-
- // Volume name
- g.drawString( this.getLabel(), ( this.getBounds().width / 2 ) - ( theFontMetrics.stringWidth( this.getLabel() ) / 2 ), this.getCloseBox().getBounds().y + this.getCloseBox().getBounds().height );
-
- if ( this.getShade() ) {
- // g2.drawImage( offscreenImage, 0, 0, this );
- return;
- }
-
- startV = this.getCloseBox().getBounds().y + this.getCloseBox().getBounds().height + 5;
-
- // Draw interior
- g.setColor( Color.white );
- g.fillRect( this.getInsets().left, this.getInsets().top, this.getBounds().width - this.getInsets().right - this.getInsets().left - 1, this.getBounds().height - this.getInsets().top - this.getInsets().bottom - 1 );
- g.setColor( Color.black );
- g.drawRect( this.getInsets().left, this.getInsets().top, this.getBounds().width - this.getInsets().right - this.getInsets().left - 1, this.getBounds().height - this.getInsets().top - this.getInsets().bottom - 1 );
-
- // All else should get drawn.
- g.setColor( Color.black );
-
- // panel.reshape( this.getInsets().left, this.getInsets().top, this.getBounds().width - this.getInsets().right - this.getInsets().left, this.getBounds().height - this.getInsets().top - this.getInsets().bottom );
- // panel.paint( g );
- // panel.repaint();
- }
-
- public void adjustClipRect( Rectangle r ) {
- this.getGraphics().clipRect( r.getBounds().x, r.getBounds().y, r.getBounds().width, r.getBounds().height );
- }
- /*
- public void update( Graphics g ) {
- paint( g );
- // panel.reshape( this.getInsets().left, this.getInsets().top, this.getBounds().width - this.getInsets().right - this.getInsets().left, this.getBounds().height - this.getInsets().top - this.getInsets().bottom );
- // panel.paint( g );
- // this.adjustClipRect( this.getBounds() );
- }
- */
- public Rectangle getTitleBar() {
- return this.titleBar;
- }
-
- public Rectangle getShadeBox() {
- return this.shadeBox;
- }
-
- public Rectangle getCloseBox() {
- return this.closeBox;
- }
-
- public Rectangle getZoomBox() {
- return this.zoomBox;
- }
-
- public Rectangle getUpArrowBox() {
- return this.upArrowBox;
- }
-
- public Rectangle getDownArrowBox() {
- return this.downArrowBox;
- }
-
- public Polygon getUpArrowPolygon() {
- return this.upArrowPolygon;
- }
-
- public Polygon getDownArrowPolygon() {
- return this.downArrowPolygon;
- }
-
- public void mouseDragged( MouseEvent e ) {
- Desktop.setTopFrame( this );
-
- if ( this.drag ) {
- Desktop.adjustClipRect( this.getBounds() );
- this.setLocation( this.getBounds().x - this.initialX + e.getX(), this.getBounds().y - this.initialY + e.getY() );
- repaint();
- }
- else if ( this.getResize() ) {
- this.setSize( this.getBounds().width - this.resizeX + e.getX(), this.getBounds().height - this.resizeY + e.getY() );
-
- panel.reshape( this.getInsets().left, this.getInsets().top, this.getBounds().width - this.getInsets().right - this.getInsets().left, this.getBounds().height - this.getInsets().top - this.getInsets().bottom );
- this.pack();
- this.validate();
- this.resizeX = e.getX();
- this.resizeY = e.getY();
-
- }
- }
-
- public void mouseMoved( MouseEvent e ) {
- }
-
- public void mouseClicked( MouseEvent e ) {
- if ( e.getClickCount() == 1 ) {
- Desktop.setTopFrame( this );
- this.repaint();
-
- if ( this.getCloseBox().contains( e.getPoint() ) ) {
- this.setVisible( false );
- this.repaint();
- Desktop.removeWindow( this );
- }
- else if ( this.getShadeBox().contains( e.getPoint() ) ) {
- if ( !this.getShade() ) {
- this.prevHeightShade = this.getBounds().height;
- this.setHeight( this.getCloseBox().getBounds().y + this.getCloseBox().getBounds().height + 5 );
- this.setShade( true );
- }
- else {
- if ( !this.getZoom() )
- this.setHeight( this.prevHeightShade );
- else
- this.setHeight( this.prevHeight );
-
- panel.reshape( this.getInsets().left, this.getInsets().top, this.getBounds().width - this.getInsets().right - this.getInsets().left, this.getBounds().height - this.getInsets().top - this.getInsets().bottom );
-
- this.pack();
- this.validate();
-
- this.setShade( false );
- }
-
- this.repaint();
- }
- else if ( this.getZoomBox().contains( e.getPoint() ) ) {
- Toolkit tk = Toolkit.getDefaultToolkit();
- Dimension d = tk.getScreenSize();
- // Set Window properties based on screen size.
- int res = tk.getScreenResolution();
-
- if ( !this.getZoom() ) {
- this.prevX = this.getBounds().x;
- this.prevY = this.getBounds().y;
- this.prevWidth = this.getBounds().width;
-
- if ( this.getShade() )
- this.prevHeight = this.prevHeightShade;
- else
- this.prevHeight = this.getBounds().height;
-
- this.setBounds( 0, Global.defaultMenuBarHeight, d.width, d.height - Global.defaultMenuBarHeight );
-
- panel.reshape( this.getInsets().left, this.getInsets().top, this.getBounds().width - this.getInsets().right - this.getInsets().left, this.getBounds().height - this.getInsets().top - this.getInsets().bottom );
-
- this.pack();
- this.validate();
- this.setShade( false );
- this.setZoom( true );
- }
- else {
- if ( this.getShade() )
- this.setHeight( this.prevHeight );
-
- this.setBounds( this.prevX, this.prevY, this.prevWidth, this.prevHeight );
-
- panel.reshape( this.getInsets().left, this.getInsets().top, this.getBounds().width - this.getInsets().right - this.getInsets().left, this.getBounds().height - this.getInsets().top - this.getInsets().bottom );
-
- this.pack();
- this.validate();
- this.setShade( false );
- this.setZoom( false );
- }
-
- this.repaint();
- }
- }
- else if ( e.getClickCount() == 2 ) {
- Rectangle r = new Rectangle( 0, 0, this.getBounds().width, this.getCloseBox().getBounds().y + this.getCloseBox().getBounds().height + 5 );
- // Rectangle r = new Rectangle( this.getBounds().x, this.getBounds().y, this.getBounds().width, this.getCloseBox().getBounds().y + this.getCloseBox().getBounds().height + 5 );
- if ( r.contains( e.getPoint() ) ) {
- if ( !this.getShade() ) {
- this.prevHeightShade = this.getBounds().height;
- this.setHeight( this.getCloseBox().getBounds().y + this.getCloseBox().getBounds().height + 5 );
- this.setShade( true );
- }
- else {
- if ( !this.getZoom() )
- this.setHeight( this.prevHeightShade );
- else
- this.setHeight( this.prevHeight );
-
- panel.reshape( this.getInsets().left, this.getInsets().top, this.getBounds().width - this.getInsets().right - this.getInsets().left, this.getBounds().height - this.getInsets().top - this.getInsets().bottom );
-
- this.pack();
- this.validate();
- this.setShade( false );
- }
-
- this.repaint();
- }
- }
- }
-
- public void mouseEntered( MouseEvent e ) {
- }
-
- public void mouseExited( MouseEvent e ) {
- }
-
- public void mousePressed( MouseEvent e ) {
- Desktop.setTopFrame( this );
-
- FontMetrics theFontMetrics = this.getGraphics().getFontMetrics();
- this.titleBar.setBounds( this.getCloseBox().getBounds().x + this.getCloseBox().getBounds().width + 2, 0, this.getBounds().width - 8, theFontMetrics.getHeight() );
-
- if ( this.getTitleBar().contains( e.getPoint() ) ) {
- this.initialX = e.getX();
- this.initialY = e.getY();
- this.drag = true;
- return;
- }
-
- this.sizeBox.setBounds( this.getBounds().width - Global.defaultGrowBoxSize, this.getBounds().height - Global.defaultGrowBoxSize, Global.defaultGrowBoxSize, Global.defaultGrowBoxSize );
-
- if ( this.sizeBox.contains( e.getPoint() ) ) {
- this.resizeX = e.getX();
- this.resizeY = e.getY();
- this.setResize( true );
- return;
- }
- }
-
- public void mouseReleased( MouseEvent e ) {
- if ( this.drag ) {
- this.drag = false;
- this.repaint();
- }
- else if ( this.getResize() ) {
- this.setResize( false );
- this.repaint();
- }
- }
-
- public String getPath() {
- return this.path;
- }
-
- public void setPath( String s ) {
- this.path = s;
- }
-
- public boolean getShade() {
- return this.shade;
- }
-
- public void setShade( boolean b ) {
- this.shade = b;
- }
-
- public boolean getZoom() {
- return this.zoom;
- }
-
- public void setZoom( boolean b ) {
- this.zoom = b;
- }
-
- public boolean getResize() {
- return this.resize;
- }
-
- public void setResize( boolean b ) {
- this.resize = b;
- }
-
- public void restoreHeight() {
- if ( this.getShade() )
- this.setHeight( this.prevHeightShade );
- }
-
- public void setBounds( Rectangle r ) {
- super.setBounds( r );
- panel.reshape( this.getInsets().left, this.getInsets().top, r.width - this.getInsets().right - this.getInsets().left, r.height - this.getInsets().top - this.getInsets().bottom );
- }
- }
-
-