/*
* Created on 05.01.2005
*
*/
package fileRepresentations;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.filechooser.FileSystemView;
import fontFactory.FontFactory;
/**
* @author T05794A
*
* FileSymbol ist eine Extension-Klasse von JLabel.
* Sie ermittelt zu einem File-Objekt das zugehörige System-Icon,
* verwendet dieses als Icon und platziert zentriert darunter den
* Dateinamen.
* Bei Doppelclick auf FileSymbol wird die zur Datei gehörende Anwendung
* gestartet.
*/
public class FileSymbol extends JLabel {
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 6283963523329215268L;
private File _file;
private Cursor _cursorMemo;
private Cursor _thiscursor = new Cursor( Cursor.HAND_CURSOR );
/**
* Erzeugt ein leeres FileSymbol
*/
public FileSymbol() {
this( (File)null );
}
/**
* 07.01.2005 18:32:23
* Erzeugt ein FileSymbol aus dem kompletten Pfadnamen
* sPathnFile
* @param sPathnFile
*/
public FileSymbol( String sPathnFile ) {
this( new File( sPathnFile ) );
}
/**
* Erzeugt ein Label mit Icon und Beschriftung.
* @param icon: das anzuzeigende Icon
* @param file: die Datei, deren Icon gezeigt wird.
* Ihr Name wird unter dem Icon gezeigt.
*/
public FileSymbol( File file ) {
super();
// so arrangieren, dass der Text zentriert unter dem Icon
// angeordnet wird:
setVerticalTextPosition( JLabel.BOTTOM );
setHorizontalTextPosition( JLabel.CENTER );
// kleine Schrift einstellen.
setFont( FontFactory.getFont( "Arial", Font.PLAIN, 10 ) );
_file = file;
if( _file != null ) {
setIconAndText();
}
// einen MausListener anmelden, damit wir bei Doppelclick auf das
// FileSymbol das passende Programm starten können.
// Ausserdem wollen wir den Mauszeiger ändern, wenn der Anwender
// über unser FileSymbol fährt:
addMouseListener( new MouseListener() {
public void mouseClicked(MouseEvent e) {
//if( e.getClickCount() > 1 ) {
//Programm starten, das mit der Datei assoziiert ist
runProgram( getFile() );
//}
}
public void mouseEntered(MouseEvent e) {
_cursorMemo = FileSymbol.this.getCursor();
FileSymbol.this.setCursor( _thiscursor );
}
public void mouseExited(MouseEvent e) {
FileSymbol.this.setCursor( _cursorMemo );
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
}
/**
* 07.01.2005 18:42:30
* Setzt das mit _file verknüpfte Icon und den Dateinamen
*/
private void setIconAndText() {
Icon icon = FileSystemView.getFileSystemView().getSystemIcon( _file );
// das Icon setzen
setIcon( icon );
// den Text setzen
setText( _file.getName() );
}
/**
* 07.01.2005 18:43:00
* Setzt ein neues _file-Objekt
* @param file
*/
public void setFile( File file ) {
_file = file;
setIconAndText();
}
/**
* 07.01.2005 18:43:31
* @return File: das _file-Objekt dieses FileSymbol
*/
public File getFile() {
return _file;
}
private void runProgram( File file )
{
String cmd = "cmd /c \"" + file.getAbsolutePath() + "\"";
//System.out.println(cmd);
try {
Runtime.getRuntime().exec(cmd);
} catch( IOException ex ) { ex.printStackTrace(); }
}
}