Skip to content

Commit

Permalink
Creates the new string matrix. Improves and implement the usage of th…
Browse files Browse the repository at this point in the history
…e new matrix in the matrix editor and the matrix manager.
  • Loading branch information
KlasJoensson committed Oct 24, 2013
1 parent a7199ce commit ad1e86b
Show file tree
Hide file tree
Showing 17 changed files with 2,425 additions and 273 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.bindings.keys.ParseException;

import net.bioclipse.core.PublishedClass;
import net.bioclipse.core.PublishedMethod;
Expand Down Expand Up @@ -44,6 +46,20 @@ public IMatrixResource create(
)
public IMatrixResource create(double[][] values);

@Recorded
@PublishedMethod(methodSummary=
"Creates a new matrix domain object, starting from a array of " +
"String arrays."
)
public IMatrixResource create(String[][] values);

@Recorded
@PublishedMethod(methodSummary=
"Creates a new matrix domain object, from a file."
)
public IMatrixResource create(IFile file) throws ParseException;
public IMatrixResource create(String file) throws ParseException;

@Recorded
@PublishedMethod(methodSummary=
"Creates a new, empty matrix domain object."
Expand All @@ -57,7 +73,7 @@ public IMatrixResource create(
"when the number of values is incompatible with the number of columns " +
"of the existing matrix."
)
public IMatrixResource addRow(IMatrixResource matrix, List<Double> values)
public IMatrixResource addRow(IMatrixResource matrix, List<String> values)
throws BioclipseException;

@Recorded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.bindings.keys.ParseException;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.part.FileEditorInput;

public class MatrixManager implements IBioclipseManager {

Expand All @@ -37,6 +39,19 @@ public String getManagerName() {
return "matrix";
}

public IMatrixResource create( IFile file ) throws ParseException {
String name = file.getName();
IFileEditorInput fei = new FileEditorInput( file );
IMatrixResource matrix =
new MatrixResource(name, fei);

if (!matrix.parseResource())
throw new ParseException( "Could not parse " +
file.getFullPath().toOSString() + " into an matrix." );

return matrix;
}

public IMatrixResource create(
String valueSequence, int ncol) {
StringTokenizer tokenizer = new StringTokenizer(valueSequence);
Expand All @@ -51,15 +66,17 @@ public IMatrixResource create(
int rowCount = 0;
int colCount = 0;
while (tokenizer.hasMoreTokens()) {
double value = Double.NaN;
try {
value = Double.parseDouble(tokenizer.nextToken());
} catch (NumberFormatException exception) {}
try {
matrix.set(rowCount+1, colCount+1, value);
} catch (Exception exception) {
exception.printStackTrace();
}
String value = tokenizer.nextToken();
matrix.set(rowCount+1, colCount+1, value);
// double value = Double.NaN;
// try {
// value = Double.parseDouble(tokenizer.nextToken());
// } catch (NumberFormatException exception) {}
// try {
// matrix.set(rowCount+1, colCount+1, value);
// } catch (Exception exception) {
// exception.printStackTrace();
// }
colCount++;
if (colCount == ncol) {
colCount = 0;
Expand All @@ -84,6 +101,21 @@ public IMatrixResource create(double[][] values) {
}
return matrix;
}

public IMatrixResource create(String[][] values) {
IMatrixResource matrix =
new MatrixResource("", (IFileEditorInput)null);
int ncol = values.length;
int nrow = values[0].length;
matrix.setSize(ncol, nrow);

for (int row=0; row<nrow; row++) {
for (int col=0; col<ncol; col++) {
matrix.set(row+1, col+1, values[row][col]);
}
}
return matrix;
}

public void setColumnLabels(IMatrixResource matrix, String[] names) {
int col = 1;
Expand All @@ -107,7 +139,7 @@ public IMatrixResource empty() {
return matrix;
}

public IMatrixResource addRow(IMatrixResource matrix, List<Double> values)
public IMatrixResource addRow(IMatrixResource matrix, List<String> values)
throws BioclipseException {
int row = matrix.getRowCount();
if (row != 0 && values.size() != matrix.getColumnCount())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import java.util.ArrayList;

import org.apache.log4j.Logger;

import net.bioclipse.statistics.model.IMatrixImplementationResource;
import net.bioclipse.statistics.model.MatrixImplementationResource;

Expand All @@ -23,6 +25,7 @@ public class JamaMatrix extends MatrixImplementationResource {
private Matrix matrix = null;
private boolean hasRowHeader, hasColHeader, hasResponseColumn;
private int responseColumnIndex;
private Logger logger = Logger.getLogger( this.getClass() );

//Column and row headers
private ArrayList<String> colIds;
Expand All @@ -40,15 +43,15 @@ private JamaMatrix(int rows, int cols) {
responseColumn = new ArrayList<String>(rows);
}

public double get(int row, int col) throws Exception {
public String get(int row, int col) throws Exception {
if (row > getRowCount())
throw new ArrayIndexOutOfBoundsException("Matrix does not have so many rows!");
if (col > getColumnCount())
throw new ArrayIndexOutOfBoundsException("Matrix does not have so many columns!");

return matrix.get(row-1, col-1);
return Double.toString( matrix.get(row-1, col-1) );
}

public void set(int row, int col, double value) throws Exception {
if (row < 1)
throw new ArrayIndexOutOfBoundsException(
Expand All @@ -72,6 +75,16 @@ public void set(int row, int col, double value) throws Exception {
matrix.set(row-1, col-1, value);
}

public void set(int row, int col, String value) throws Exception {
try {
double val = Double.parseDouble( value );
this.set( row, col, val );
} catch (NumberFormatException e) {
throw new NumberFormatException( "The JamaMatrix can only handle " +
"numbers" );
}
}

public int getColumnCount() throws Exception {
return matrix.getColumnDimension();
}
Expand Down Expand Up @@ -104,7 +117,14 @@ public IMatrixImplementationResource getInstance(int rows, int cols, int respons
// newInstance.responseColumn=this.responseColumn;
return newInstance;
}


public IMatrixImplementationResource getInstance( int rows, int cols,
boolean lowerTriangular,
boolean symmetric ) {
logger.warn( "The jama matrix do not handle triangular matrixes, " +
"returning an ordinary" );
return getInstance( rows, cols );
}
public String getRowName( int index ) {
if( !rowIds.isEmpty() && index<=rowIds.size())
return rowIds.get(index-1);
Expand Down
3 changes: 2 additions & 1 deletion plugins/net.bioclipse.statistics/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ Export-Package: net.bioclipse.statistics,
Bundle-Activator: net.bioclipse.statistics.Activator
Bundle-ClassPath: .,
jars/org.eclipse.nebula.widgets.grid_1.0.0.jar
Import-Package: org.apache.log4j
Import-Package: net.bioclipse.ui.business.describer,
org.apache.log4j
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Binary file added plugins/net.bioclipse.statistics/icons/ColHeader.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added plugins/net.bioclipse.statistics/icons/RowHeader.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions plugins/net.bioclipse.statistics/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,38 @@
priority="normal">
</content-type>
</extension>
<extension
point="net.bioclipse.ui.bioobjectDescriber">
<BioObject
describer="net.bioclipse.statistics.describer.MatrixResourceDescriber"
id="net.bioclipse.statistics.describer"
objectClass="net.bioclipse.statistics.model.IMatrixResource">
</BioObject>
</extension>
<extension
point="org.eclipse.ui.editorActions">
<editorContribution
id="net.bioclipse.statistics.matrixEditorContribution"
targetID="net.bioclipse.editors.MatrixGridEditor">
<action
class="net.bioclipse.statistics.RowHeaderDelegate"
icon="icons/RowHeader.gif"
id="net.bioclipse.statistics.rowHeaderAction"
label="Row header"
style="toggle"
toolbarPath="IWorkbenchActionConstants.MB_ADDITIONS"
tooltip="Add / remove row headers">
</action>
<action
class="net.bioclipse.statistics.ColHeaderDelegate"
icon="icons/ColHeader.gif"
id="net.bioclipse.statistics.colHeaderAction"
label="Column header"
style="toggle"
toolbarPath="IWorkbenchActionConstants.MB_ADDITIONS"
tooltip="Add / remove column headers">
</action>
</editorContribution>
</extension>

</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.bioclipse.statistics;

import net.bioclipse.statistics.editors.MatrixEditor;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;

/**
* Handles the column-header toggle-button.
*
* @author Klas Jšnsson (klas.joensson@gmail.com)
*
*/
public class ColHeaderDelegate implements IEditorActionDelegate {

private IEditorPart targetPart;

public void run( IAction action ) {
if (targetPart != null && action != null)
if (targetPart instanceof MatrixEditor)
action.setChecked( ((MatrixEditor) targetPart).runColumnHeaderAction() );

}

public void selectionChanged( IAction action, ISelection selection ) {
if (targetPart instanceof MatrixEditor)
action.setChecked( ((MatrixEditor) targetPart).hasColumnHeader() );
}

public void setActiveEditor( IAction action, IEditorPart targetEditor ) {
targetPart = targetEditor;
if (targetPart instanceof MatrixEditor) {
action.setChecked( ((MatrixEditor) targetPart).hasColumnHeader() );
((MatrixEditor) targetPart).addActionDelegate( this );
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.bioclipse.statistics;

import net.bioclipse.statistics.editors.MatrixEditor;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;

/**
* Handles the row-header toggle-button.
*
* @author Klas Jšnsson (klas.joensson@gmail.com)
*
*/
public class RowHeaderDelegate implements IEditorActionDelegate {

private IEditorPart targetPart;

public void run( IAction action ) {
if (targetPart != null && action != null) {
if (targetPart instanceof MatrixEditor)
action.setChecked( ((MatrixEditor) targetPart).runRowHeaderAction() );
}

}

public void selectionChanged( IAction action, ISelection selection ) {
if (targetPart instanceof MatrixEditor) {
action.setChecked( ((MatrixEditor) targetPart).hasRowHeader() );

}
}

public void setActiveEditor( IAction action, IEditorPart targetEditor ) {
targetPart = targetEditor;
if (targetPart instanceof MatrixEditor) {
action.setChecked( ((MatrixEditor) targetPart).hasRowHeader() );
((MatrixEditor) targetPart).addActionDelegate( this );
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.bioclipse.statistics.describer;

import net.bioclipse.core.business.BioclipseException;
import net.bioclipse.core.domain.IBioObject;
import net.bioclipse.statistics.model.IMatrixResource;
import net.bioclipse.ui.business.describer.IBioObjectDescriber;


public class MatrixResourceDescriber implements IBioObjectDescriber {

public MatrixResourceDescriber() {

}

public String getPreferredEditorID( IBioObject object )
throws BioclipseException {

if (object instanceof IMatrixResource) {
String [] possibleEditorIDs =
((IMatrixResource) object).getEditorIDs();
/* Let's assume that the first in the array is the one that is
* preferred.*/
return possibleEditorIDs[0];
}
return null;
}

}
Loading

0 comments on commit ad1e86b

Please sign in to comment.