Advertisement
Guest User

simlei

a guest
Jan 30th, 2010
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.87 KB | None | 0 0
  1. package org.jctyptool.analysis.transpositionanalysis.ui;
  2.  
  3. import org.eclipse.swt.SWT;
  4. import org.eclipse.swt.events.SelectionAdapter;
  5. import org.eclipse.swt.events.SelectionEvent;
  6. import org.eclipse.swt.graphics.Font;
  7. import org.eclipse.swt.layout.GridData;
  8. import org.eclipse.swt.widgets.Composite;
  9. import org.eclipse.swt.widgets.Table;
  10. import org.eclipse.swt.widgets.TableColumn;
  11. import org.eclipse.swt.widgets.TableItem;
  12.  
  13. /**
  14. * A Table where you can do the following:
  15. * * Set Columns named after a given scheme
  16. * * Set a text through a wizard, which is displayed in the defined columns
  17. * * Access every "cell" by coordinates
  18. * @author Simon Leischnig
  19. */
  20. public class TranspositionTable extends Composite {
  21.  
  22. private Table transpTable;
  23.  
  24. /**
  25. * The actual (but not transformed) text in the table
  26. */
  27. private String displayedText;
  28. /**
  29. * The Text in the table as it was set by the user
  30. */
  31. private String unchangedText;
  32. /**
  33. * the actually displayed cells are stored there
  34. */
  35. private char[][] cells;
  36. /**
  37. * the actually displayed rows are stored here
  38. */
  39. private String[] rows;
  40. /**
  41. * the actually displayers columns are stored here
  42. */
  43. private String[] columns;
  44. /**
  45. * The last row could have a different count of characters, so
  46. * it is stored here.
  47. */
  48. private int lastRowLength = 0;
  49. /**
  50. * The column width in which a text is arranged in this table
  51. * 0 means, the text will be arranged in one single row
  52. * (no "line break" after column width is reached)
  53. */
  54. private int columnCount = 9;
  55. /**
  56. * decides, if the whole text set should be displayed, when
  57. * no column width is set (the whole text would be in a single
  58. * line), or if only a part of it (first [another parameter to set]
  59. * singleLineDisplayCount characters).
  60. * As soon as columnCount is set to a value greater than zero,
  61. * the full text will be displayed
  62. */
  63. private boolean displayEverythingSingleline = true;
  64. /**
  65. * How much characters should be displayed, if
  66. * displayEverythingSingleline is false and columnCount=0?
  67. */
  68. private int singleLineDisplayCount = 40;
  69. /**
  70. * The width of a column (inhabited by 1 character) in pixel
  71. */
  72. private int columnWidth = 26;
  73. /**
  74. * a fat FontFace
  75. */
  76. private Font fatFont;
  77. /**
  78. * Columns are saved for disposal at redraw
  79. */
  80. private TableColumn[] disposableColumns;
  81.  
  82. public TranspositionTable(Composite parent, int style) {
  83. super(parent, SWT.NONE);
  84.  
  85. transpTable = new Table(this, SWT.FULL_SELECTION | SWT.BORDER);
  86. GridData transpTableLData = new GridData();
  87. transpTableLData.grabExcessHorizontalSpace = true;
  88. transpTableLData.horizontalAlignment = GridData.FILL;
  89. transpTableLData.verticalAlignment = GridData.FILL;
  90. transpTableLData.grabExcessVerticalSpace = true;
  91. transpTable.setLayoutData(transpTableLData);
  92. transpTable.setHeaderVisible(true);
  93.  
  94. String name = transpTable.getFont().getFontData()[0].getName();
  95. int height = transpTable.getFont().getFontData()[0].getHeight();
  96. fatFont = new Font(transpTable.getDisplay(), name, height, SWT.BOLD);
  97. //transpTable.setFont(fatFont);
  98.  
  99. //revert selection highlighting
  100. transpTable.addSelectionListener(new SelectionAdapter() {
  101. public void widgetSelected(SelectionEvent e) {
  102. transpTable.setSelection(-1);
  103. }
  104. });
  105.  
  106. }
  107.  
  108. private String unchangedToDisplayText(String text) {
  109. if((!displayEverythingSingleline) && columnCount == 0)
  110. return text.substring(0, singleLineDisplayCount);
  111. else return text;
  112. }
  113.  
  114. private void calculateArrays() {
  115. int w=0; int h=0; int textL = displayedText.length();
  116.  
  117. if(columnCount == 0) {
  118. w = textL; h = 1;
  119. lastRowLength = textL;
  120. } else {
  121. w = columnCount;
  122. h = (int)Math.ceil((double)textL / (double)w);
  123. lastRowLength = textL%w;
  124. if(lastRowLength == 0) lastRowLength = w;
  125. }
  126. //Cells
  127. cells = new char[w][];
  128. for(int i=0; i<w; i++) {
  129. if(i<lastRowLength) cells[i] = new char[h];
  130. else cells[i] = new char[h-1];
  131. }
  132. for(int i=0; i<textL; i++) {
  133. cells[i%w][(int)Math.floor((double)i / (double)w)] = displayedText.charAt(i);
  134. }
  135. //Rows
  136. rows = new String[h];
  137. for(int i=0; i<h; i++) {
  138. rows[i] = "";
  139. for(int k=0; (k<w)&&(cells[k].length > i); k++)
  140. rows[i] = rows[i].concat(String.valueOf(cells[k][i]));
  141. }
  142. //Columns
  143. columns = new String[w];
  144. for(int i=0; i<w; i++) {
  145. columns[i] = String.valueOf(cells[i]);
  146. }
  147. }
  148.  
  149. /**
  150. * put the Text from the rows / columns field into the Table
  151. */
  152. private void drawTableText() {
  153. if((rows != null) && (columns != null)) {
  154. transpTable.clearAll();
  155. if(disposableColumns != null) {
  156. for(int i=0; i<disposableColumns.length; i++) {
  157. disposableColumns[i].dispose();
  158. }
  159. }
  160. disposableColumns = new TableColumn[0];
  161. transpTable.setItemCount(0);
  162.  
  163. //create columns
  164. disposableColumns = new TableColumn[columns.length];
  165. for(int i=0; i<columns.length; i++) {
  166. disposableColumns[i] = new TableColumn(transpTable, SWT.NONE);
  167. disposableColumns[i].setText(""+(i+1));
  168. disposableColumns[i].setWidth(columnWidth);
  169. disposableColumns[i].setMoveable(true);
  170. disposableColumns[i].setAlignment(SWT.CENTER);
  171. }
  172. for(int i=0; i<rows.length; i++) {
  173. TableItem myItem = new TableItem(transpTable, SWT.NONE);
  174. String[] myRow = new String[rows[i].length()];
  175. for(int k=0; k<myRow.length; k++) {
  176. myRow[k] = String.valueOf(rows[i].charAt(k));
  177. }
  178. myItem.setText(myRow);
  179. myItem.setFont(fatFont);
  180. }
  181. transpTable.layout();
  182. }
  183. }
  184.  
  185. /**
  186. * Set a text to be displayed in the table rows.
  187. * The text will be arranged in given columns,
  188. * if a column width is set.
  189. * @param text the text to be displayed
  190. */
  191. public void setText(String text) {
  192. setText(text, columnCount, true);
  193. }
  194.  
  195. /** set a text to display; this method only calls setcolumnCount and
  196. * setText in this order; read there for more information.
  197. * @param text the text to display
  198. * @param columnCount number of columns, in which the text should be
  199. * arranged. Zero = no arrangement, but single line display.
  200. */
  201. public void setText(String text, int columnCount) {
  202. setText(text, columnCount, true); // not calling inner method because this
  203. // originates from the outside
  204. }
  205.  
  206. /** inner setText procedure. Methods in this class should call this method only
  207. * if they want to control whether a redraw/recalculate should occur or not.
  208. * @param text the text
  209. * @param columnCount the column count
  210. * @param refresh redraw/calculation, or not
  211. */
  212. private void setText(String text, int columnCount, boolean refresh) {
  213. unchangedText = text;
  214. setColumnCount(columnCount, false);
  215. displayedText = unchangedToDisplayText(unchangedText);
  216.  
  217. if(refresh) {
  218. calculateArrays();
  219. drawTableText();
  220. }
  221. }
  222.  
  223. /**
  224. * @param columnCount the column Width to set in which the text is displayed.
  225. * <br /> zero: no colum-arrangement (singleline)
  226. */
  227. public void setColumnCount(int columnCount) {
  228. setColumnCount(columnCount, true);
  229. }
  230.  
  231. /** inner columnCount procedure. methods in this class should call this
  232. * if they want to control whether a redraw/recalculate should occur or not.
  233. * @param columnCount the column width
  234. * @param refresh redraw/calculation, or not
  235. */
  236. private void setColumnCount(int columnCount, boolean refresh) {
  237. if(columnCount <= 0) this.columnCount = 0;
  238. else this.columnCount = columnCount;
  239. displayedText = unchangedToDisplayText(unchangedText);
  240.  
  241. if(refresh) {
  242. calculateArrays();
  243. drawTableText();
  244. }
  245. }
  246.  
  247. /**
  248. * @return the column count in which the text is displayed.
  249. * <br /> zero: no colum-arrangement (singleline)
  250. */
  251. public int getColumnCount() {
  252. return columnCount;
  253. }
  254.  
  255.  
  256. /**
  257. * @return the displayEverythingSingleline
  258. */
  259. public boolean isDisplayEverythingSingleline() {
  260. return displayEverythingSingleline;
  261. }
  262.  
  263. /**
  264. * @param displayEverythingSingleline the displayEverythingSingleline to set
  265. */
  266. public void setDisplayEverythingSingleline(boolean displayEverythingSingleline) {
  267. this.displayEverythingSingleline = displayEverythingSingleline;
  268. }
  269.  
  270. /**
  271. * @return the transpTable
  272. */
  273. public Table getTranspTable() {
  274. return transpTable;
  275. }
  276.  
  277. /**
  278. * @return the cells
  279. */
  280. public char[][] getCells() {
  281. return cells;
  282. }
  283.  
  284. /**
  285. * @return the rows
  286. */
  287. public String[] getRows() {
  288. return rows;
  289. }
  290.  
  291. /**
  292. * @return the columns
  293. */
  294. public String[] getColumns() {
  295. return columns;
  296. }
  297.  
  298.  
  299. /**
  300. * @param singleLineDisplayCount the singleLineDisplayCount to set
  301. */
  302. public void setSingleLineDisplayCount(int singleLineDisplayCount) {
  303. this.singleLineDisplayCount = singleLineDisplayCount;
  304. }
  305.  
  306. /**
  307. * @param columnCount the columnCount to set
  308. */
  309. public void setColumnWidth(int columnWidth) {
  310. this.columnWidth = columnWidth;
  311. }
  312.  
  313. }
  314.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement