Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.jctyptool.analysis.transpositionanalysis.ui;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.events.SelectionAdapter;
- import org.eclipse.swt.events.SelectionEvent;
- import org.eclipse.swt.graphics.Font;
- import org.eclipse.swt.layout.GridData;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.swt.widgets.Table;
- import org.eclipse.swt.widgets.TableColumn;
- import org.eclipse.swt.widgets.TableItem;
- /**
- * A Table where you can do the following:
- * * Set Columns named after a given scheme
- * * Set a text through a wizard, which is displayed in the defined columns
- * * Access every "cell" by coordinates
- * @author Simon Leischnig
- */
- public class TranspositionTable extends Composite {
- private Table transpTable;
- /**
- * The actual (but not transformed) text in the table
- */
- private String displayedText;
- /**
- * The Text in the table as it was set by the user
- */
- private String unchangedText;
- /**
- * the actually displayed cells are stored there
- */
- private char[][] cells;
- /**
- * the actually displayed rows are stored here
- */
- private String[] rows;
- /**
- * the actually displayers columns are stored here
- */
- private String[] columns;
- /**
- * The last row could have a different count of characters, so
- * it is stored here.
- */
- private int lastRowLength = 0;
- /**
- * The column width in which a text is arranged in this table
- * 0 means, the text will be arranged in one single row
- * (no "line break" after column width is reached)
- */
- private int columnCount = 9;
- /**
- * decides, if the whole text set should be displayed, when
- * no column width is set (the whole text would be in a single
- * line), or if only a part of it (first [another parameter to set]
- * singleLineDisplayCount characters).
- * As soon as columnCount is set to a value greater than zero,
- * the full text will be displayed
- */
- private boolean displayEverythingSingleline = true;
- /**
- * How much characters should be displayed, if
- * displayEverythingSingleline is false and columnCount=0?
- */
- private int singleLineDisplayCount = 40;
- /**
- * The width of a column (inhabited by 1 character) in pixel
- */
- private int columnWidth = 26;
- /**
- * a fat FontFace
- */
- private Font fatFont;
- /**
- * Columns are saved for disposal at redraw
- */
- private TableColumn[] disposableColumns;
- public TranspositionTable(Composite parent, int style) {
- super(parent, SWT.NONE);
- transpTable = new Table(this, SWT.FULL_SELECTION | SWT.BORDER);
- GridData transpTableLData = new GridData();
- transpTableLData.grabExcessHorizontalSpace = true;
- transpTableLData.horizontalAlignment = GridData.FILL;
- transpTableLData.verticalAlignment = GridData.FILL;
- transpTableLData.grabExcessVerticalSpace = true;
- transpTable.setLayoutData(transpTableLData);
- transpTable.setHeaderVisible(true);
- String name = transpTable.getFont().getFontData()[0].getName();
- int height = transpTable.getFont().getFontData()[0].getHeight();
- fatFont = new Font(transpTable.getDisplay(), name, height, SWT.BOLD);
- //transpTable.setFont(fatFont);
- //revert selection highlighting
- transpTable.addSelectionListener(new SelectionAdapter() {
- public void widgetSelected(SelectionEvent e) {
- transpTable.setSelection(-1);
- }
- });
- }
- private String unchangedToDisplayText(String text) {
- if((!displayEverythingSingleline) && columnCount == 0)
- return text.substring(0, singleLineDisplayCount);
- else return text;
- }
- private void calculateArrays() {
- int w=0; int h=0; int textL = displayedText.length();
- if(columnCount == 0) {
- w = textL; h = 1;
- lastRowLength = textL;
- } else {
- w = columnCount;
- h = (int)Math.ceil((double)textL / (double)w);
- lastRowLength = textL%w;
- if(lastRowLength == 0) lastRowLength = w;
- }
- //Cells
- cells = new char[w][];
- for(int i=0; i<w; i++) {
- if(i<lastRowLength) cells[i] = new char[h];
- else cells[i] = new char[h-1];
- }
- for(int i=0; i<textL; i++) {
- cells[i%w][(int)Math.floor((double)i / (double)w)] = displayedText.charAt(i);
- }
- //Rows
- rows = new String[h];
- for(int i=0; i<h; i++) {
- rows[i] = "";
- for(int k=0; (k<w)&&(cells[k].length > i); k++)
- rows[i] = rows[i].concat(String.valueOf(cells[k][i]));
- }
- //Columns
- columns = new String[w];
- for(int i=0; i<w; i++) {
- columns[i] = String.valueOf(cells[i]);
- }
- }
- /**
- * put the Text from the rows / columns field into the Table
- */
- private void drawTableText() {
- if((rows != null) && (columns != null)) {
- transpTable.clearAll();
- if(disposableColumns != null) {
- for(int i=0; i<disposableColumns.length; i++) {
- disposableColumns[i].dispose();
- }
- }
- disposableColumns = new TableColumn[0];
- transpTable.setItemCount(0);
- //create columns
- disposableColumns = new TableColumn[columns.length];
- for(int i=0; i<columns.length; i++) {
- disposableColumns[i] = new TableColumn(transpTable, SWT.NONE);
- disposableColumns[i].setText(""+(i+1));
- disposableColumns[i].setWidth(columnWidth);
- disposableColumns[i].setMoveable(true);
- disposableColumns[i].setAlignment(SWT.CENTER);
- }
- for(int i=0; i<rows.length; i++) {
- TableItem myItem = new TableItem(transpTable, SWT.NONE);
- String[] myRow = new String[rows[i].length()];
- for(int k=0; k<myRow.length; k++) {
- myRow[k] = String.valueOf(rows[i].charAt(k));
- }
- myItem.setText(myRow);
- myItem.setFont(fatFont);
- }
- transpTable.layout();
- }
- }
- /**
- * Set a text to be displayed in the table rows.
- * The text will be arranged in given columns,
- * if a column width is set.
- * @param text the text to be displayed
- */
- public void setText(String text) {
- setText(text, columnCount, true);
- }
- /** set a text to display; this method only calls setcolumnCount and
- * setText in this order; read there for more information.
- * @param text the text to display
- * @param columnCount number of columns, in which the text should be
- * arranged. Zero = no arrangement, but single line display.
- */
- public void setText(String text, int columnCount) {
- setText(text, columnCount, true); // not calling inner method because this
- // originates from the outside
- }
- /** inner setText procedure. Methods in this class should call this method only
- * if they want to control whether a redraw/recalculate should occur or not.
- * @param text the text
- * @param columnCount the column count
- * @param refresh redraw/calculation, or not
- */
- private void setText(String text, int columnCount, boolean refresh) {
- unchangedText = text;
- setColumnCount(columnCount, false);
- displayedText = unchangedToDisplayText(unchangedText);
- if(refresh) {
- calculateArrays();
- drawTableText();
- }
- }
- /**
- * @param columnCount the column Width to set in which the text is displayed.
- * <br /> zero: no colum-arrangement (singleline)
- */
- public void setColumnCount(int columnCount) {
- setColumnCount(columnCount, true);
- }
- /** inner columnCount procedure. methods in this class should call this
- * if they want to control whether a redraw/recalculate should occur or not.
- * @param columnCount the column width
- * @param refresh redraw/calculation, or not
- */
- private void setColumnCount(int columnCount, boolean refresh) {
- if(columnCount <= 0) this.columnCount = 0;
- else this.columnCount = columnCount;
- displayedText = unchangedToDisplayText(unchangedText);
- if(refresh) {
- calculateArrays();
- drawTableText();
- }
- }
- /**
- * @return the column count in which the text is displayed.
- * <br /> zero: no colum-arrangement (singleline)
- */
- public int getColumnCount() {
- return columnCount;
- }
- /**
- * @return the displayEverythingSingleline
- */
- public boolean isDisplayEverythingSingleline() {
- return displayEverythingSingleline;
- }
- /**
- * @param displayEverythingSingleline the displayEverythingSingleline to set
- */
- public void setDisplayEverythingSingleline(boolean displayEverythingSingleline) {
- this.displayEverythingSingleline = displayEverythingSingleline;
- }
- /**
- * @return the transpTable
- */
- public Table getTranspTable() {
- return transpTable;
- }
- /**
- * @return the cells
- */
- public char[][] getCells() {
- return cells;
- }
- /**
- * @return the rows
- */
- public String[] getRows() {
- return rows;
- }
- /**
- * @return the columns
- */
- public String[] getColumns() {
- return columns;
- }
- /**
- * @param singleLineDisplayCount the singleLineDisplayCount to set
- */
- public void setSingleLineDisplayCount(int singleLineDisplayCount) {
- this.singleLineDisplayCount = singleLineDisplayCount;
- }
- /**
- * @param columnCount the columnCount to set
- */
- public void setColumnWidth(int columnWidth) {
- this.columnWidth = columnWidth;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement