import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import javax.swing.DefaultCellEditor;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.LayoutStyle;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit [url=http://www.cloudgarden.com]Cloud Garden (Java Resources)[/url] for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
* @author Daniel
* @version 1.2
* GUI by Jigloo GPL Software.
* Neues Frame um ein extra Fenster aufzurufen welches eine neue Reihe in der Tabelle erzeugt
* und/oder neu Personen / Projekte hinzufügt.
*/
public class TablePanel extends JFrame implements ActionListener,TableModelListener,MouseListener{
/**
* Einfache GUI für das Anzeigen der Daten aus der "table.txt"
* Beinhaldet: JTable / DefaultTableModel
* JMenuBar / JMenu / MenuItems
* JScrollPane
*/
static TablePanel g2;
private Global global = new Global();
private Eigenschaften c_props = new Eigenschaften();
private static final long serialVersionUID = 1L;
private InitData c_initData;
private TableMethoden c_TableM;
private RefactorDataPanel c_NewLine;
private Options c_options;
private JScrollPane jScrollPane;
private JTable jTable;
private DefaultTableModel dtm;
private JMenuBar jMenuBar;
private JMenu Menu;
private JMenu jMenu1;
private JMenuItem jMenuItem_deleteRow;
private JMenuItem jMenuItem_save;
private JMenuItem jMenuItem_PersonAndProjekt;
private JMenuItem jMenuItem_exit;
private JLabel jLabelstatus;
private JButton jButton2;
private JButton jButton1;
private JPanel tool;
private JMenuItem jMenuItem_addRow;
private JMenuItem jMenuItem_Refresh;
private JMenuItem jMenuItem_selectAll;
private JMenuItem jMenuItem_options;
private JComboBox comboBox_names;
private JComboBox comboBox_projekts;
private JTextPane jErroPane;
private int nZwischensicherung,selectedRow=-1,selectedCol=-1,tablewidth;
private String[] namen,projekt,week;
private String value;
{
//Set Look & Feel
try {
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch(Exception e) {
e.printStackTrace();
}
}
/** Konstruktor für das Table
* Initalisiert die Daten um sie in der Tabelle wiederzugeben und startet die GUI.
* @param table
* @param namen
* @param projekt
* @param week
*/
public TablePanel(String[] table,String[] namen,String[] projekt,String[] week) {
super();
assignIcon();
if(table != null && namen != null && projekt != null && week != null){
this.namen = namen;
this.projekt = projekt;
this.week = week;
c_initData = InitData.fl;
c_NewLine = new RefactorDataPanel();
c_options = new Options(this);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
g2 = TablePanel.this;
if(global.getPreferedDimenson() != null){
System.out.println(global.getPreferedDimenson());
setSize(global.getPreferedDimenson());
}
setLocation(global.getPositionX(),global.getPositionY());
setVisible(true);
}
});
initGUI(true);
nZwischensicherung = 1000; // Wartezeit für die Zwsichensicherung in Millisekunden ( 5 Sekunden )
Thread thread1 = new Thread(new RunnableThread("TableChangedThread")); // Neuer Thread f�r die Zwischensicherung.
thread1.start();
}else{
initGUI(false);
}
}
/** GUI wird initalisiert.
* @param error Wenn etwas schief läuft.
*/
/**Application Icon wird verändert.
*
*/
private void assignIcon() {
URL iconURI;
Class<? extends TablePanel> res = getClass();
iconURI = res.getResource("/daten/table.gif");
String path = iconURI.getFile().replaceAll("%20", " ");
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(path);
setIconImage(img);
}
private void initGUI(boolean error) {
try {
BorderLayout thisLayout = new BorderLayout();
getContentPane().setLayout(thisLayout);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setJMenuBar(jMenuBar);
this.setVisible(false);
this.setTitle("Ressourcen Planer");
if(error){
{
jScrollPane = new JScrollPane();
getContentPane().add(jScrollPane, BorderLayout.CENTER);
jScrollPane.setBounds(0, 0, 137, 100);
jScrollPane.setPreferredSize(new java.awt.Dimension(797, 240));
{
// Tabelle wird initalsiert und mit Daten besetzt.
tableSetData();
jScrollPane.setViewportView(jTable);
jTable.setFillsViewportHeight(true);
jTable.getTableHeader().setOpaque(true);
jTable.getTableHeader().setResizingAllowed(true);
this.setTablewidth(0);
// Größe wird speziell festgelegt weil sich die Tabelle permanent ändern kann.
int size = c_initData.getTable().length * jTable.getRowHeight();
if(global.getnZeigewochen() < global.getKalenderwochen()){
TableColumn column = null;
column = jTable.getColumnModel().getColumn(0);
setTablewidth(global.getnZeigewochen()*column.getPreferredWidth());;
}else{
setTablewidth(jTable.getWidth());
}
jTable.setPreferredSize(new Dimension(jTable.getWidth(),size+jTable.getRowHeight()));
jTable.setPreferredScrollableViewportSize(jTable.getPreferredSize());
System.out.println(jTable.getPreferredScrollableViewportSize());
jTable.getTableHeader().setReorderingAllowed(false);
}
}
{
tool = new JPanel();
GroupLayout toolLayout = new GroupLayout((JComponent)tool);
tool.setLayout(toolLayout);
getContentPane().add(tool, BorderLayout.SOUTH);
tool.setMaximumSize(new java.awt.Dimension(800, 20));
tool.setRequestFocusEnabled(false);
tool.setPreferredSize(new java.awt.Dimension(797, 21));
{
jButton1 = new JButton();
jButton1.setText("L\u00F6schen");
jButton1.setActionCommand("4");
jButton1.addActionListener(this);
}
{
jButton2 = new JButton();
jButton2.setText("Neue Reihe");
jButton2.setSize(87, 14);
jButton2.setActionCommand("3");
jButton2.addActionListener(this);
}
{
jLabelstatus = new JLabel();
jLabelstatus.setText("Status:");
}
toolLayout.setHorizontalGroup(toolLayout.createSequentialGroup()
.addComponent(jLabelstatus, GroupLayout.PREFERRED_SIZE, 369, GroupLayout.PREFERRED_SIZE)
.addGap(254)
.addComponent(jButton2, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
toolLayout.setVerticalGroup(toolLayout.createParallelGroup()
.addComponent(jLabelstatus, GroupLayout.Alignment.LEADING, 0, 21, Short.MAX_VALUE)
.addGroup(toolLayout.createSequentialGroup()
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, Short.MAX_VALUE)
.addGroup(toolLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jButton2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE))));
}
{
jMenuBar = new JMenuBar();
setJMenuBar(jMenuBar);
{
Menu = new JMenu();
jMenuBar.add(Menu);
Menu.setText("Menu");
{
jMenuItem_PersonAndProjekt = new JMenuItem();
Menu.add(jMenuItem_PersonAndProjekt);
jMenuItem_PersonAndProjekt.setText("Personen & Projekt Bearbeitung");
jMenuItem_PersonAndProjekt.setEnabled(true);
jMenuItem_PersonAndProjekt.setActionCommand("1");
jMenuItem_PersonAndProjekt.addActionListener(this);
}
{
jMenuItem_save = new JMenuItem("Speichern");
jMenuItem_save.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_S, ActionEvent.CTRL_MASK));
Menu.add(jMenuItem_save);
jMenuItem_save.setActionCommand("2");
jMenuItem_save.addActionListener(this);
}
{
jMenuItem_addRow = new JMenuItem("Neue Reihe");
jMenuItem_addRow.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_ADD, ActionEvent.CTRL_MASK));
Menu.add(jMenuItem_addRow);
jMenuItem_addRow.setActionCommand("3");
jMenuItem_addRow.addActionListener(this);
}
{
jMenuItem_exit = new JMenuItem("Beenden");
jMenuItem_exit.setAccelerator(KeyStroke.getKeyStroke("ESCAPE"));
Menu.add(jMenuItem_exit);
jMenuItem_exit.setActionCommand("8");
jMenuItem_exit.addActionListener(this);
}
}
{
jMenu1 = new JMenu();
jMenuBar.add(jMenu1);
jMenu1.setText("Bearbeiten");
{
jMenuItem_deleteRow = new JMenuItem();
jMenu1.add(jMenuItem_deleteRow);
jMenuItem_deleteRow.setText("Markierte Reihe l\u00F6schen");
jMenuItem_deleteRow.setAccelerator(KeyStroke.getKeyStroke("DELETE"));
jMenuItem_deleteRow.setActionCommand("4");
jMenuItem_deleteRow.addActionListener(this);
}
{
jMenuItem_Refresh = new JMenuItem();
jMenu1.add(jMenuItem_addRow);
jMenuItem_Refresh.setText("Refresh");
jMenu1.add(jMenuItem_Refresh);
jMenuItem_Refresh.setAccelerator(KeyStroke.getKeyStroke("F5"));
jMenuItem_Refresh.setActionCommand("5");
jMenuItem_Refresh.addActionListener(this);
}
{
jMenuItem_selectAll = new JMenuItem("Alles Markiern");
jMenu1.add(jMenuItem_selectAll);
jMenuItem_selectAll.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_A, ActionEvent.CTRL_MASK));
jMenuItem_selectAll.setActionCommand("6");
jMenuItem_selectAll.addActionListener(this);
}
{
jMenuItem_options = new JMenuItem("Optionen");
jMenu1.add(jMenuItem_options);
jMenuItem_options.setActionCommand("7");
jMenuItem_options.addActionListener(this);
}
}
}
// Dies wird aufgerufen falls ein Fehler ensteht wie z.B eine Datei ist gelöscht oder null.
}else{
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane emptyLabel = new JTextPane();
emptyLabel.setForeground(Color.red);
emptyLabel.setPreferredSize(new Dimension(175, 100));
this.setVisible(true);
this.setPreferredSize(new java.awt.Dimension(800, 300));
this.setMinimumSize(new java.awt.Dimension(500, 250));
{
jErroPane = new JTextPane();
jErroPane.setText("jTextPane1");
jErroPane.setEditable(false);
jErroPane.setEnabled(false);
jErroPane.setForeground(Color.RED);
jErroPane.setText("Ups, da ist etwas schiefgelaufen. \nBitte prüfen sie nocheinmal alle Datein und starten sie neu!");
}
}
pack();
c_TableM = new TableMethoden(jTable,jLabelstatus,getDtm());
c_initData = null;
this.setSize(805, 317);
} catch (NullPointerException e) {
System.err.println("GUI : 1");
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
}
/**
* Lässt die Tabelle verschwinden.
*/
public void refresh() {
c_TableM.save();
dispose();
}
/**
* Setzt das DefaultTableModel
* @param dtm
*/
public void setDtm(DefaultTableModel dtm) {
this.dtm = dtm;
}
class GroupingTable extends JTable {
/**
* Das JTable wird überschrieben um eine Gruppierung zu ermöglichen.
*/
private static final long serialVersionUID = 1L;
private int keyColumn;
private Color[] colorModel;
private int[] colorIndex; //"Model" zur Speichern der Zeilenfarbe
private Color lightergray = new Color(215, 215, 215);
public GroupingTable(TableModel model, int column) {
super(model);
this.keyColumn = column;
colorModel = new Color[] {this.getBackground(), lightergray};
determineColor();
}
//tableChanged Ueberschrieben, damit das "Farbenmodel" aktualisiert wird
public void tableChanged(TableModelEvent evt) {
if (evt.getColumn()==keyColumn)
determineColor();
super.tableChanged(evt);
}
//Methode zum Ermitteln der Zeilenfarbe
private void determineColor() {
colorIndex = new int[this.getRowCount()];
if (colorIndex.length<=0)
return;
int colorCount=0;
colorIndex[0] = colorCount;
for (int row=1; row<this.getRowCount(); row++) {
if(this.getValueAt(row, this.convertColumnIndexToView(keyColumn)) != null && this.getValueAt(row, this.convertColumnIndexToView(keyColumn))
.equals(this.getValueAt(row-1, this.convertColumnIndexToView(keyColumn))))
colorIndex[row] = colorCount & 1;
else
colorIndex[row] = ++colorCount & 1;
}
}
//Ueberschrieben, um die Zeilenfarbe zu setzen.
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = (Component) super.prepareRenderer(renderer, row, column);
for (int i=0; i<this.getSelectedRows().length; i++) {
if (this.getSelectedRows()[i]==row)
return c;
}
try{
c.setBackground(colorModel[colorIndex[row]]);
}catch(ArrayIndexOutOfBoundsException ex){
}
return c;
}
}
/**
* Hier wird die Tabelle mit Daten gefüllt , initalisiert und sowie TableModel festgelegt.
*/
private void tableSetData() {
String[][] data = new String[c_initData.get2DTable().size()][global.getKalenderwochen()];
String[] header = new String[global.getKalenderwochen()];
header[0] = "Namen";
header[1] = "Projekte";
for(int i = 2; i < header.length; i++){
header[i] = week[i-2];
}
int first = c_initData.get2DTable().size();
for(int l = 0; l < first; l++){
int second = c_initData.get2DTable().get(l).size()-1;
for(int i = 0; i < second; i++){
data[l][i] = c_initData.get2DTable().get(l).get(i);
}
}
/*Eigenes DefaultTableModel um nur Zahlen zwischen 0- 100 auf der Tabelle
* nach Zeile 2 zu zulassen.
*/
setDtm(new DefaultTableModel(data,header){
private static final long serialVersionUID = 1L;
@Override
public void setValueAt(Object aValue, int row, int col)
{
// Abbruch wenn der Wert kein Integer ist.
if (aValue instanceof Integer)
{
//Abbruch wenn der Wert nicht zwischen 0 - 100 liegt.
int value = ((Integer)aValue).intValue();
if(value < 0 || value > 100){
return;
}
}
// Setze den Wert.
super.setValueAt(aValue, row, col);
}
@Override
public Class<?> getColumnClass(int col)
{
if (col >=2)
return Integer.class;
else{
return String.class;
}
}
});
jTable = new GroupingTable(dtm, 0);
jTable.setLayout(null);
jTable.setAutoCreateRowSorter(true);
//ComboBoxen werden auf das JTable gelegt.
TableColumn tColumnNames = jTable.getColumnModel().getColumn(0);
comboBox_names = new JComboBox();
TableColumn tColumnProjekts = jTable.getColumnModel().getColumn(1);
comboBox_projekts = new JComboBox();
jTable.setPreferredSize(new java.awt.Dimension(814, 261));
jTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
jTable.getTableHeader().setBounds(0, 0, 793, 0);
jTable.addMouseListener(this);
//ComboxBox für Namen und Projekte werden mit den Namen und Projekten gefüllt.
comboBox_names.addItem("");
comboBox_projekts.addItem("");
for(int i = 0; i < namen.length; i++){
comboBox_names.addItem(namen[i]);
}
for(int i = 0; i < projekt.length; i++){
comboBox_projekts.addItem(projekt[i]);
}
//CellEditor f�r die ComboBoxen.
tColumnNames.setCellEditor(new DefaultCellEditor(comboBox_names));
tColumnProjekts.setCellEditor(new DefaultCellEditor(comboBox_projekts));
//ModelListener für die Tabelle um %te an Zahlen zu ergänzen
getDtm().addTableModelListener(this);
//c_TableM.getNotFull();
}
/**Erzeugen einer Thread Klasse um Threads zu starten für z.B Zwischensicherung.
*
*/
class RunnableThread implements Runnable {
Thread runner;
public RunnableThread() {
}
public RunnableThread(String threadName) {
runner = new Thread(this, threadName); // (1) Create a new thread.
runner.start(); // (2) Start the thread.
}
public void run() {
try {
boolean endless = true;
while(endless){
Thread.sleep(nZwischensicherung);
c_TableM.save();
c_props.dimensionCheck(getSize());
c_props.positionChanged(g2.getX(), g2.getY());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**ActionListener,
* Switch durch die jeweiligen ActionCommands.
* Commandos sind Integer und werden geparsed !!!
* @param ActionEvent.
*/
public void actionPerformed(ActionEvent e) {
String status = "Status: ";
jLabelstatus.setText("");
c_initData = InitData.fl;
try{
c_TableM.save();
int actioncommand = Integer.parseInt(e.getActionCommand());
switch(actioncommand){
case 1: //Neue Person/Projekt hinzufuegen
c_NewLine.main();
break;
case 2:
c_TableM.save(); //SAVE
jLabelstatus.setText(status+"Gespeichert.");
break;
case 3:
if(c_TableM.checkEmpty()){
jTable = c_TableM.addEmpty();
jLabelstatus.setForeground(Color.BLACK);
jLabelstatus.setText(status+"Reihe hinzugefügt.");
}
break;
case 4: //Delete
jTable.getEditorComponent();
jTable.setEnabled(false);
c_TableM.delete();
jTable.setEnabled(true);
jLabelstatus.setForeground(Color.BLACK);
jLabelstatus.setText(status+"Löschen erfolgreich.");
break;
case 5: //Refresh
c_initData.refresh();
break;
case 6: //Alles markieren
jTable.selectAll();
break;
case 7:
c_options.main(); // Optionen
case 8: //Beenden
System.exit(1);
default:
break;
}
}catch(NumberFormatException ex){
System.err.println("Fehler beim Parsen !");
ex.printStackTrace();
}
}
/**
*
* @return DefaultTableModel.
*/
public DefaultTableModel getDtm() {
return dtm;
}
/**
* TableListener um % an die Werte in der Tabelle anzuhängen.
*/
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel)e.getSource();
//String columnName = model.getColumnName(column);
if(column >= 2){
Object data = model.getValueAt(row, column);
if(data != null){
String daten = data.toString().trim();
if(daten.contains("%")){
daten.replaceAll("%", "");
}else{
if(!daten.equals("")){
try{
int prozent = Integer.parseInt(daten);
model.setValueAt(prozent+"%", row, column);
}catch(ClassCastException ex){
model.setValueAt(daten, row, column);
}
}
}
}
c_TableM.save();
}
}
@Override
public void mouseClicked(MouseEvent e) {
try{
if(selectedCol != -1 && selectedRow != -1){
if(selectedRow != jTable.rowAtPoint(e.getPoint()) || selectedCol != jTable.columnAtPoint(e.getPoint())){
if(jTable.getValueAt(selectedRow, selectedCol) == null || jTable.getValueAt(selectedRow, selectedCol).toString().trim().equals("")){
if(!value.equals("")){
jTable.setValueAt(value, selectedRow, selectedCol);
}
}
}
}
}catch(IndexOutOfBoundsException ex){
selectedCol = -1;
selectedRow = -1;
value = "";
jTable.clearSelection();
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
try{
if(selectedCol != -1 && selectedRow != -1){
if(selectedRow != jTable.rowAtPoint(e.getPoint()) || selectedCol != jTable.columnAtPoint(e.getPoint())){
if(jTable.getValueAt(selectedRow, selectedCol) == null || jTable.getValueAt(selectedRow, selectedCol).toString().trim().equals("")){
if(!value.equals("")){
jTable.setValueAt(value, selectedRow, selectedCol);
}
}
}
}
selectedRow=jTable.rowAtPoint(e.getPoint());
selectedCol= jTable.columnAtPoint(e.getPoint());
if(jTable.getValueAt(selectedRow,selectedCol) != null){
value = jTable.getValueAt(selectedRow,selectedCol).toString();
}else{
value = "";
}
jTable.setValueAt("",selectedRow,selectedCol);
}catch(IndexOutOfBoundsException ex){
selectedCol = -1;
selectedRow = -1;
value = "";
jTable.clearSelection();
}
}
public void setTablewidth(int tablewidth) {
this.tablewidth = tablewidth;
}
public int getTablewidth() {
return tablewidth;
}
}