Package de.taliis.plugins.dialogs

Source Code of de.taliis.plugins.dialogs.modfTableModel

package de.taliis.plugins.dialogs;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;

import starlight.alien.*;
import starlight.taliis.apps.editors.AdvJTableCell;
import starlight.taliis.apps.editors.TableColorRenderer;
import starlight.taliis.core.files.*;
import starlight.taliis.core.chunks.adt.*;
import starlight.taliis.helpers.adtObjHelper;
import starlight.taliis.helpers.fileLoader;

/**
* JTable interface for edit MTEX chunks in ADT files
* @author tharo
*
*/

public class adtObjectAppTable extends JPanel
            implements ActionListener, MouseListener, KeyListener {
  adt obj;
  JTable table;
  JToolBar toolBar;
  JPopupMenu popup;
  JMenuItem miCopy, miDelete;
 
  final String ADD = "add";
  final String DEL = "del";
 
  public adtObjectAppTable(adt reference) {
    obj = reference;
    this.setLayout(new BorderLayout());
     
    toolBar = new JToolBar();
    JButton  button = makeNavigationButton("delete", DEL,
                "Delete given texture File",
                "Remove Texture");
    toolBar.add(button);
    button = makeNavigationButton("add", ADD,
                "Add a new texture File",
                "Add Texture");
    toolBar.add(button);
    toolBar.addSeparator();
    add(toolBar, BorderLayout.PAGE_START);
     
        table = new JTable(new modfTableModel(obj.modf, obj.mwmo));
        //table.setPreferredScrollableViewportSize(new Dimension(70, 70));
        table.addMouseListener(this);
    table.addKeyListener(this);
    initPopup();
        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.addComponentListener(new
                CorrectStrangeBehaviourListener(table, scrollPane));
       
        // set some table look and feel
        initTableFace();
       
        //Add the scroll pane to this panel.
        add(scrollPane);
  }
 
  void initTableFace() {
    TableColumn column;
   
    column = table.getColumnModel().getColumn(0);
        column.setPreferredWidth(50);
        column.setCellRenderer(new TableColorRenderer(Color.WHITE));
       
        table.getColumnModel().getColumn(1).setMaxWidth(90);
   
    for(int c=2; c<5; c++) {
      column = table.getColumnModel().getColumn(c);
      column.setMaxWidth(150);
      column.setCellRenderer(new TableColorRenderer(new Color(224,255,224)));
    }
    for(int c=6; c<9; c++) {
      column = table.getColumnModel().getColumn(c);
      column.setMaxWidth(150);
      column.setCellRenderer(new TableColorRenderer(new Color(224,255,224)));
    }
  }
 
  /**
   * We use popup windows for basic operations like
   * - copy
   * - delete
   */
  private void initPopup() {
    popup = new JPopupMenu();
    miCopy = new JMenuItem("Clone selected Row");
    miCopy.addActionListener(this);
    miDelete = new JMenuItem("Delete selected Row(s)");
    miDelete.addActionListener(this);
    popup.add(miCopy);
    popup.add(miDelete);
  }

  protected JButton makeNavigationButton(String imageName,
            String actionCommand,
            String toolTipText,
            String altText) {
    //Look for the image.
    String imgLocation = "images/icons/"
    + imageName
    + ".png";

    //Create and initialize the button.
    JButton button = new JButton();
    button.setActionCommand(actionCommand);
    button.setToolTipText(toolTipText);
    button.addActionListener(this);
   
    if (imageName != "" && imageName != null) {    //image found
      button.setIcon(fileLoader.createImageIcon(imgLocation));
    }
    else {                                     //no image found
      button.setText(altText);
      System.err.println("Resource not found: " + imgLocation);
    }

    return button;
  }

 
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
       
        if (ADD.equals(cmd)) {
          if(obj.mwmo.getLenght()==0) {
            JOptionPane.showMessageDialog(this.getParent(),
                "No Objects registered!",
                "Error",
                JOptionPane.ERROR_MESSAGE);
            return;
          }
          adtObjHelper objh = new adtObjHelper(obj);
          Random r = new Random();
          objh.addWMO(0, r.nextInt(100000)+1);
         
          table.updateUI();
        }
        else if (ADD.equals(cmd)) {
          if(table.getSelectedRow()==-1) return;
          obj.modf.remove( table.getSelectedRow()  );
          table.updateUI();
        }
        else if(e.getSource()==miDelete){
          if(table.getSelectedRow()==-1) return;
          obj.modf.remove( table.getSelectedRow()  );
          table.updateUI();
        }
        else if(e.getSource()==miCopy){
          int row = table.getSelectedRow();
      if(row==-1) return;
          if(obj.mwmo.getLenght()==0) {
            JOptionPane.showMessageDialog(this.getParent(),
                "No Objects registered!",
                "Error",
                JOptionPane.ERROR_MESSAGE);
            return;
          }
          adtObjHelper objh = new adtObjHelper(obj);
          Random r = new Random();
          objh.addWMO(obj.modf.entrys[row].getNameID(), r.nextInt(100000)+1,MDDF_Entry.translate(obj.modf.entrys[row].getX()),MDDF_Entry.translate(obj.modf.entrys[row].getY()),obj.modf.entrys[row].getZ());
         
          table.updateUI();
        }
    }

  @Override
  public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub
    // right click
    if(e.getButton()==MouseEvent.BUTTON3) {
      // menues need selected rows in order to perform
     
      if(table.getSelectedRowCount()>0)
        popup.show(e.getComponent(), e.getX(), e.getY());
    }
  }

  @Override
  public void keyPressed(KeyEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub
   
  }

}



class modfTableModel extends AbstractTableModel {
  MODF archive;
  MWMO files;
 
  // Bezeichnungen
    Vector columnNames = new Vector();
   
    modfTableModel(MODF reference, MWMO file_reference) {
      archive = reference;
      files = file_reference;
    }
   
    public int getColumnCount() {
        return 11;
    }

    public int getRowCount() {
      return (int)archive.getLenght();
    }

    public String getColumnName(int col) {
        switch(col) {
          case 0: return "File ID";
          case 1: return "Unique ID";
          case 2: return "x";
          case 3: return "y";
          case 4: return "z";
          case 5: return "flags";
          case 6: return "rot a";
          case 7: return "rot b";
          case 8: return "rot c";
          case 9: return "DD Set";
          case 10: return "Name Set";
        }
        return "";
    }

    public Object getValueAt(int row, int col) {
      switch(col) {
        case 0{
          int id = archive.entrys[row].getNameID();
          String filename = files.getValueNo(id);
          return new AdvJTableCell(id, filename);
        }
        case 1: return archive.entrys[row].getUniqID();
        case 2: return MODF_Entry.translate(archive.entrys[row].getX());
        case 3: return MODF_Entry.translate(archive.entrys[row].getY());
        case 4: return archive.entrys[row].getZ();
        case 5: return archive.entrys[row].getFlags();
        case 6: return archive.entrys[row].getA();
        case 7: return archive.entrys[row].getB();
        case 8: return archive.entrys[row].getC();
        case 9: return archive.entrys[row].getDoodadSet();
        case 10: return archive.entrys[row].getWMONameSet();
      }
      return null;
    }

/*    public Class getColumnClass(int c) {
        if(c==0) return Icon.class;
        else return String.class;
    }/**/

    public boolean isCellEditable(int row, int col) {
    //  if(col==1) return true;
      return true;
    }

    public void setValueAt(Object value, int row, int col) {
      // translate value
      int vInt = 0;
      float vFloat = 0;
      short vShort = 0;
      try {
        switch(col) {
          case 0: case 1: case 5:
            // integer value
            vInt = Integer.valueOf(value.toString());
            break;
          case 9: case 10:
            vShort = Short.valueOf(value.toString());
            break;
          case 2: case 3: case 4: case 6: case 7: case 8:
            // float value
            vFloat = Float.valueOf(value.toString());
            break;
          default:
            return;
        }
      } catch(Exception exp) {
        return;
      }
  // unique id  TODO: check for aviability
      switch(col) {
        case 0:
          if(vInt>=0 || vInt<archive.getLenght())
                archive.entrys[row].setNameID(vInt);
          break;
        case 1: archive.entrys[row].setUniqID(vInt); break;
        case 2: archive.entrys[row].setX( MODF_Entry.translate( vFloat ) ); break;
        case 3: archive.entrys[row].setY( MODF_Entry.translate( vFloat ) ); break;
        case 4: archive.entrys[row].setZ( vFloat ); break;
        case 5: archive.entrys[row].setFlags(vInt); break;
        case 6: archive.entrys[row].setA(vFloat); break;
        case 7: archive.entrys[row].setB(vFloat); break;
        case 8: archive.entrys[row].setC(vFloat); break;
        case 9: archive.entrys[row].setDoodadSet(vShort); break;
        case 10: archive.entrys[row].setWMONameSet(vShort); break;
      }
    }
   
    public void clear() {
    }
}
TOP

Related Classes of de.taliis.plugins.dialogs.modfTableModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.