import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import javax.swing.filechooser.FileSystemView;
/**
*
* Beschreibung
*
* @version 1.0 vom 19.03.2011
* @author
*/
public class profiledit extends JFrame{
// Anfang Attribute
private JButton exepfad = new JButton();
private JTextField pfadtextfield = new JTextField();
private JFileChooser pfad = new JFileChooser("C:/");
private JTextField nametextfield = new JTextField();
private JLabel pfadheadline = new JLabel();
private JLabel nameheadline = new JLabel();
private JLabel iconheadline = new JLabel();
private String[] profilcomboData = {};
private JComboBox profilcombo = new JComboBox(profilcomboData);
private JLabel profilheadline = new JLabel();
private JButton profiladd = new JButton();
private JButton addprog = new JButton();
private JTextField profiltextfield = new JTextField();
private JLabel newprofilheadline = new JLabel();
private JLabel iconbild = new JLabel();
private JButton loeschen = new JButton();
private JButton profildelete = new JButton();
// Ende Attribute
public profiledit(String title) {
// Frame-Initialisierung
super(title);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 620;
int frameHeight = 307;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
Container cp = getContentPane();
cp.setLayout(null);
// Anfang Komponenten
exepfad.setBounds(264, 176, 35, 25);
exepfad.setText("...");
exepfad.setMargin(new Insets(2, 2, 2, 2));
exepfad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
exepfad_ActionPerformed(evt);
}
});
cp.add(exepfad);
pfadtextfield.setBounds(8, 176, 249, 24);
pfadtextfield.setText("");
cp.add(pfadtextfield);
nametextfield.setBounds(8, 120, 185, 24);
nametextfield.setText("");
cp.add(nametextfield);
pfadheadline.setBounds(8, 152, 53, 16);
pfadheadline.setText("Pfad : ");
pfadheadline.setFont(new Font("MS Sans Serif", Font.BOLD, 13));
cp.add(pfadheadline);
nameheadline.setBounds(8, 96, 163, 16);
nameheadline.setText("Name des Programms :");
nameheadline.setFont(new Font("MS Sans Serif", Font.BOLD, 13));
cp.add(nameheadline);
iconheadline.setBounds(216, 96, 43, 16);
iconheadline.setText("Icon :");
iconheadline.setFont(new Font("MS Sans Serif", Font.BOLD, 13));
cp.add(iconheadline);
profilcombo.setBounds(8, 40, 145, 24);
profilcombo.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent evt) {
profilcombo_FocusGained(evt);
}
});
cp.add(profilcombo);
profilheadline.setBounds(8, 16, 122, 16);
profilheadline.setText("Profil auswählen :");
profilheadline.setFont(new Font("MS Sans Serif", Font.BOLD, 13));
cp.add(profilheadline);
profiladd.setBounds(488, 40, 49, 25);
profiladd.setText("add");
profiladd.setMargin(new Insets(2, 2, 2, 2));
profiladd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
profiladd_ActionPerformed(evt);
}
});
cp.add(profiladd);
addprog.setBounds(320, 176, 81, 25);
addprog.setText("Hinzufügen");
addprog.setMargin(new Insets(2, 2, 2, 2));
addprog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
addprog_ActionPerformed(evt);
}
});
cp.add(addprog);
profiltextfield.setBounds(312, 40, 161, 24);
profiltextfield.setText("");
cp.add(profiltextfield);
newprofilheadline.setBounds(312, 16, 158, 16);
newprofilheadline.setText("Neues Profil erstellen :");
newprofilheadline.setFont(new Font("MS Sans Serif", Font.BOLD, 13));
cp.add(newprofilheadline);
loeschen.setBounds(320, 128, 83, 25);
loeschen.setText("Löschen");
loeschen.setMargin(new Insets(2, 2, 2, 2));
loeschen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
loeschen_ActionPerformed(evt);
}
});
cp.add(loeschen);
profildelete.setBounds(168, 40, 89, 25);
profildelete.setText("Profil löschen");
profildelete.setMargin(new Insets(2, 2, 2, 2));
profildelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
profildelete_ActionPerformed(evt);
}
});
cp.add(profildelete);
// Ende Komponenten
iconbild.setBounds(222, 115, 30,30);
add(iconbild);
setResizable(false);
setVisible(true);
}
// Anfang Methoden
public void exepfad_ActionPerformed(ActionEvent evt) {
String pfadstring = pfadOpenFilename();
iconbild.setIcon( null );
FileSystemView fsv = FileSystemView.getFileSystemView();
File f = new File(pfadstring);
iconbild.setIcon(fsv.getSystemIcon(f));
pfadtextfield.setText(pfadstring);
}
public String pfadOpenFilename() {
pfad.setDialogTitle("Pfad auswählen");
if (pfad.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
return pfad.getSelectedFile().getPath();
} else {
return null;
}
}
public void profiladd_ActionPerformed(ActionEvent evt) {
try{
File neuprofil = new File(profiltextfield.getText() + ".txt");
if ( neuprofil.exists()){
fileexistsShowMessageDialog();
}else if(profiltextfield.getText().length() < 5){
profilnameleerShowMessageDialog();
}else{
neuprofil.createNewFile();
profiltextfield.setText("");
successShowMessageDialog();
}
}catch(IOException ioe){
}
}
public void addprog_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
}
public void fileexistsShowMessageDialog() {
JOptionPane.showMessageDialog(this, "Es existiert bereits ein Profil mit diesem Namen! Bitte wählen Sie einen anderen Namen aus.", "Fehler beim Erstellen", JOptionPane.ERROR_MESSAGE);
}
public void successShowMessageDialog() {
JOptionPane.showMessageDialog(this, "Das Profil wurde erstellt.", "", JOptionPane.INFORMATION_MESSAGE);
}
public void profilcombo_FocusGained(FocusEvent evt) {
profilcombo.removeAllItems();
File dir = new File(System.getProperty("user.dir"));
File[] files = dir.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
boolean filter = files[i].getName().endsWith(".txt");
if (filter == true){
profilcombo.addItem(files[i].getName());
}
}
}
}
public void loeschen_ActionPerformed(ActionEvent evt) {
iconbild.setIcon(null);
pfadtextfield.setText("");
nametextfield.setText("");
}
public void profildelete_ActionPerformed(ActionEvent evt) {
File profil = new File(System.getProperty("user.dir")+ "/" + profilcombo.getSelectedItem());
if(profil.exists()){
profil.delete();
profilcombo.removeItem(profilcombo.getSelectedItem());
}else{
profildeleteShowMessageDialog();
}
}
public void profildeleteShowMessageDialog() {
JOptionPane.showMessageDialog(this, "Profil existiert bereits nicht mehr!", "Fehler beim Löschen", JOptionPane.ERROR_MESSAGE);
}
public void profilnameleerShowMessageDialog() {
JOptionPane.showMessageDialog(this, "Es wurde kein Profilname eingegeben.", "", JOptionPane.ERROR_MESSAGE);
}
// Ende Methoden
public static void main(String[] args) {
new profiledit("Profil");
}
}