Browse Source

Update/Edit of properties and Elements (step 1)

Edgardo Palza 8 years ago
parent
commit
164bed9d29
2 changed files with 58 additions and 21 deletions
  1. 25 0
      src/ui/view/DefaulTable.java
  2. 33 21
      src/ui/view/GUI.java

+ 25 - 0
src/ui/view/DefaulTable.java

@@ -0,0 +1,25 @@
+package ui.view;
+
+import javax.swing.table.DefaultTableModel;
+
+public class DefaulTable extends DefaultTableModel {
+	private boolean[][] editable_cells; // 2d array to represent rows and
+										// columns
+
+	DefaulTable(int rows, int cols) { // constructor
+		super(rows, cols);
+		this.editable_cells = new boolean[rows][cols];
+	}
+
+	@Override
+	public boolean isCellEditable(int row, int column) { // custom //
+														// isCellEditable
+															// function
+		return this.editable_cells[row][column];
+	}
+
+	public void setCellEditable(int row, int col, boolean value) {
+		this.editable_cells[row][col] = value; // set cell true/false
+		this.fireTableCellUpdated(row, col);
+	}
+}

+ 33 - 21
src/ui/view/GUI.java

@@ -115,7 +115,7 @@ public class GUI implements CategoryListener {
 
 	private JTable tableProperties = new JTable();
 	private JPanel graphLabel = new JPanel();
-	private DefaultTableModel tableModelProperties = new DefaultTableModel();
+	private DefaulTable tableModelProperties;
 	private final JScrollPane scrollProperties = new JScrollPane();
 
 	// In this section is the graph for the selected HolonElement of the clicked
@@ -174,8 +174,6 @@ public class GUI implements CategoryListener {
 		this.canvas = new MyCanvas(model, control);
 		this.unitGraph = new UnitGraph(model, control); // for testing, remove
 
-		
-
 		// later
 		control.initListener(this);
 		initialize();
@@ -265,12 +263,12 @@ public class GUI implements CategoryListener {
 
 		// Set up of the Properties section
 		Object[] colNames = { "Field", "Information" };
+		tableModelProperties = new DefaulTable(100, colNames.length);
 		tableModelProperties.setColumnIdentifiers(colNames);
 		tableProperties.setModel(tableModelProperties);
 		tableProperties.setFillsViewportHeight(true);
 		tableProperties.setCellSelectionEnabled(true);
 		tableProperties.setColumnSelectionAllowed(true);
-
 		// Set up of the Graph section
 
 		Object[] tempText = { "Here comes the graph for each clicked HolonElement" };
@@ -353,6 +351,17 @@ public class GUI implements CategoryListener {
 				}
 			}
 		});
+
+		tableProperties.addMouseListener(new MouseAdapter() {
+			public void mousePressed(MouseEvent e) {
+				int selectedX = (int) Math
+						.floor(e.getX() / (tableProperties.getColumnModel().getTotalColumnWidth() / 2));
+				int selectedY = (int) Math.floor(e.getY() / 16);
+				if (tableModelProperties.isCellEditable(selectedY, selectedX) && e.getClickCount() == 2) {
+					System.out.println("QUEEE");
+				}
+			}
+		});
 		frmCyberPhysical.getContentPane().setLayout(new BorderLayout(0, 0));
 
 		TreeCellRenderer customRenderer = new TreeCellRenderer() {
@@ -442,12 +451,15 @@ public class GUI implements CategoryListener {
 			public void mousePressed(MouseEvent e) {
 				try {
 					actualObjectClicked = tree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent().toString();
-					//if an Object was selected, the porperties are shown in the table
-					DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent();
-					if(selectedNode.getLevel() == 2){
-						CpsObject selected = controller.searchObjInCat(selectedNode.toString(), selectedNode.getParent().toString());
+					// if an Object was selected, the porperties are shown in
+					// the table
+					DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
+							.getPathForLocation(e.getX(), e.getY()).getLastPathComponent();
+					if (selectedNode.getLevel() == 2) {
+						CpsObject selected = controller.searchObjInCat(selectedNode.toString(),
+								selectedNode.getParent().toString());
 						deleteRows();
-						if(selected instanceof HolonObject && selected != null){
+						if (selected instanceof HolonObject && selected != null) {
 							selected = (HolonObject) selected;
 							fillElementTable(((HolonObject) selected).getElements());
 						}
@@ -557,6 +569,7 @@ public class GUI implements CategoryListener {
 				if (canvas.tempCps != null) {
 					Object[] tempName = { "Name", canvas.tempCps.getName() };
 					tableModelProperties.addRow(tempName);
+					tableModelProperties.setCellEditable(0, 1, true);
 					Object[] tempId = { "ID", canvas.tempCps.getID() };
 					tableModelProperties.addRow(tempId);
 					if (canvas.tempCps.getClass() == HolonObject.class) {
@@ -704,14 +717,14 @@ public class GUI implements CategoryListener {
 
 		timePanel = new TimePanel(model, controller);
 		timePanel.setBorder(null);
-		((JSlider)(timePanel.getComponent(1))).addChangeListener(new ChangeListener() {
-			
+		((JSlider) (timePanel.getComponent(1))).addChangeListener(new ChangeListener() {
+
 			@Override
 			public void stateChanged(ChangeEvent e) {
 				unitGraph.repaint();
 			}
 		});
-		
+
 		splitPane.setRightComponent(splitPane_1);
 		splitPane.setDividerLocation(200);
 		splitPane_1.setDividerLocation(500);
@@ -854,24 +867,23 @@ public class GUI implements CategoryListener {
 		deleteRows();
 		if (canvas.dataSelected != null) {
 			fillElementTable(canvas.dataSelected);
-		}
-		/**
-		 * hinzugef�gt damit man auch nach dem 
-		 * objekt platziert wurde elemente von Objekten in Kategorien ansehen kann
-		 */
+		} /**
+			 * hinzugef�gt damit man auch nach dem objekt platziert wurde
+			 * elemente von Objekten in Kategorien ansehen kann
+			 */
 		canvas.dataSelected = null;
 
 	}
-	
-	public void deleteRows(){
+
+	public void deleteRows() {
 		if (tableModelHolonElement.getRowCount() > 0) {
 			for (int i = tableModelHolonElement.getRowCount() - 1; i > -1; i--) {
 				tableModelHolonElement.removeRow(i);
 			}
 		}
 	}
-	
-	public void fillElementTable(ArrayList<HolonElement> elements){
+
+	public void fillElementTable(ArrayList<HolonElement> elements) {
 		for (HolonElement he : elements) {
 			Object[] temp = { he.getEleName(), he.getEnergy(), he.getAmount() };
 			tableModelHolonElement.addRow(temp);