Guest User

Untitled

a guest
Jan 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.18 KB | None | 0 0
  1. package group4;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Dimension;
  5. import java.awt.Point;
  6. import java.awt.Rectangle;
  7. import java.util.ArrayList;
  8.  
  9. import javax.swing.JFrame;
  10. import javax.swing.JScrollPane;
  11. import javax.swing.JTable;
  12. import javax.swing.UIManager;
  13. import javax.swing.table.TableColumn;
  14.  
  15. public class TimelineUI extends JFrame {
  16.  
  17. TimeLine timeline;
  18. JScrollPane scrollpane;
  19. public TimelineUI() {
  20. initTimeline();
  21. }
  22.  
  23. public final void initTimeline() {
  24. try {
  25. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30. this.setLayout(new BorderLayout());
  31. this.setTitle("Timeline");
  32.  
  33. timeline = new TimeLine(5,3);
  34. scrollpane = new JScrollPane();
  35. scrollpane.setViewportView(timeline);
  36. this.add(scrollpane,BorderLayout.CENTER);
  37.  
  38. addData();
  39.  
  40. this.pack();
  41. this.setSize(1200,200);
  42. this.setVisible(true);
  43. }
  44. public JTable getTable () {
  45. return timeline;
  46. }
  47. private void addData(){
  48. ArrayList<String> columndata = new ArrayList<String>();
  49. columndata.add("Blig (1)");
  50. columndata.add("Workbench (2)");
  51. timeline.addColumn("", columndata);
  52.  
  53. columndata = new ArrayList<String>();
  54. columndata.add("Making spunks");
  55. columndata.add("Service bligs");
  56. timeline.addColumn(0, columndata);
  57.  
  58. columndata = new ArrayList<String>();
  59. columndata.add("Finding plonks");
  60. columndata.add("Finding plonks");
  61. timeline.addColumn(60, columndata);
  62.  
  63. columndata = new ArrayList<String>();
  64. columndata.add("Finding plonks");
  65. columndata.add("Finding plonks");
  66. timeline.addColumn(90, columndata);
  67.  
  68. columndata = new ArrayList<String>();
  69. columndata.add("Finding plonks");
  70. columndata.add("Finding plonks");
  71. timeline.addColumn(120, columndata);
  72.  
  73.  
  74. columndata = new ArrayList<String>();
  75. columndata.add("Finding plonks");
  76. columndata.add("Finding plonks");
  77. timeline.addColumn(150, columndata);
  78.  
  79. columndata = new ArrayList<String>();
  80. columndata.add("Finding plonks");
  81. columndata.add("Finding plonks");
  82. timeline.addColumn(180, columndata);
  83.  
  84.  
  85. timeline.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  86. timeline.getColumnModel().getColumn(1).setMinWidth(200);
  87. timeline.getColumnModel().getColumn(1).setMaxWidth(200);
  88. timeline.getColumnModel().getColumn(1).setPreferredWidth(200);
  89.  
  90. }
  91. }
  92.  
  93. package group4;
  94.  
  95. import java.awt.Color;
  96. import java.awt.Component;
  97. import java.awt.Dimension;
  98. import java.awt.Graphics;
  99. import java.util.ArrayList;
  100. import javax.swing.JTable;
  101. import javax.swing.ListSelectionModel;
  102. import javax.swing.SwingUtilities;
  103. import javax.swing.table.AbstractTableModel;
  104. import javax.swing.table.TableCellRenderer;
  105. import javax.swing.table.TableColumn;
  106.  
  107.  
  108. public class TimeLine extends JTable{
  109. private final int ROW_DISTANCE = 5;
  110. private final int TIME_ROW_HEIGHT = 20;
  111. private ImageTableModel itm;
  112. private int maxRowHeight = 20;
  113.  
  114. /**
  115. * Generates a new TimeLine object
  116. * @param rows - number of rows of the default grid
  117. * @param columns - number of columns of the default grid
  118. */
  119. public TimeLine(int rows, int columns){
  120. setTimeLineLookAndFeel();
  121. itm = initModel();
  122. itm.initGrid(rows, columns);
  123. setModel(itm); // set the table model
  124. System.out.println(itm.getValueAt(2, 1));
  125.  
  126. }
  127.  
  128. /**
  129. * @return ImageTableModel - a new ImageTableModel
  130. */
  131. private ImageTableModel initModel(){
  132. return new ImageTableModel();
  133. }
  134.  
  135. /**
  136. * Adds a new column to the table
  137. * @param date - The column name as java.util.Date
  138. * @param columndata - The row values for this column.
  139. */
  140. public void addColumn(int time, ArrayList<String> columndata){
  141. itm.addColumn(time+"", columndata);
  142. }
  143.  
  144. /**
  145. * Adds a new column to the table
  146. * @param columnName - The column name
  147. * @param columndata - The row values for this column.
  148. */
  149. public void addColumn(String columnName, ArrayList<String> columndata){
  150. itm.addColumn(columnName, columndata);
  151. }
  152.  
  153. /**
  154. * Utility function to set the scroll pane
  155. */
  156. public int getRowCount(){
  157. return itm.getRowCount();
  158. }
  159.  
  160. /**
  161. * Utility function to set the scroll pane
  162. */
  163. public int getColumnCount(){
  164. return itm.getColumnCount();
  165. }
  166.  
  167. /**
  168. * Set some JTable properties to make it
  169. * look more like a timeline
  170. */
  171. private void setTimeLineLookAndFeel(){
  172. this.getTableHeader().setReorderingAllowed(false);
  173. this.setCellSelectionEnabled(true);
  174. this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  175. this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  176. this.setIntercellSpacing(new Dimension(0,0));
  177. this.setShowGrid(false);
  178. this.setTableHeader(null);
  179. }
  180.  
  181. @Override
  182. public void paintComponent(Graphics g){
  183. super.paintComponent(g);
  184. g.setColor( Color.BLACK );
  185. int y = getRowHeight(0) * (itm.getRowCount() - 1) - 1;
  186. g.drawLine(0, y, getSize().width, y);
  187. }
  188.  
  189. @Override
  190. public Component prepareRenderer(TableCellRenderer renderer, int row, int column){
  191. Component c = super.prepareRenderer(renderer, row, column);
  192.  
  193. if(row == itm.getRowCount()-1){
  194. c.setBackground(java.awt.SystemColor.control);
  195. }
  196. else if( !this.isCellSelected(row, column)) {
  197. c.setBackground(column % 2 != 0 ? new Color(241, 245, 250) : null);
  198. }
  199. return c;
  200. }
  201.  
  202.  
  203. @Override
  204. /**
  205. * makes the date column look like a table header
  206. */
  207. public void changeSelection(int row, int column, boolean toggle, boolean extend){
  208. Object o = getValueAt(row, column);
  209. if (o != null && row != itm.getRowCount()-1){
  210. super.changeSelection(row, column, toggle, extend);
  211. }
  212. }
  213.  
  214.  
  215. /**
  216. * The table model for this timeline *
  217. */
  218. class ImageTableModel extends AbstractTableModel {
  219. private ArrayList<String> columnnames; // holds the column names
  220. private ArrayList<ArrayList<String>> data; // holds the table data
  221. private int maxRowCount;
  222. private int columnCursor; // points on the current column
  223.  
  224. public ImageTableModel(){
  225. columnnames = new ArrayList<String>();
  226. data = new ArrayList<ArrayList<String>>();
  227. maxRowCount = 0;
  228. columnCursor = 0;
  229. }
  230.  
  231. public Object getValueAt(int row, int column) {
  232. if (data.get(column).size()-1<row) {
  233. return null;
  234. }
  235. else{
  236. return data.get(column).get(row);
  237. }
  238. }
  239.  
  240. public int getRowCount(){
  241. return maxRowCount;
  242. }
  243.  
  244. public int getColumnCount(){
  245. return columnnames.size();
  246. }
  247.  
  248. public String getColumnName( int columnIndex ){
  249. return columnnames.get(columnIndex);
  250. }
  251.  
  252. /**
  253. * Adds a new column to the table
  254. * @param columnName - The column name
  255. * @param columndata - The row values for this column.
  256. */
  257. public void addColumn(String columnName, ArrayList<String> columndata) {
  258. if(columnCursor >= columnnames.size()){
  259. columnnames.add(columnName);
  260. data.add(rotateFillList(columnName,columndata));
  261.  
  262. }
  263. else{
  264. columnnames.set(columnCursor, columnName);
  265. data.set(columnCursor, rotateFillList(columnName,columndata));
  266. }
  267. SwingUtilities.invokeLater (new Runnable(){ // fixes a nasty java vector bug
  268. public void run () {
  269. fireTableStructureChanged();
  270. fireTableDataChanged();
  271. }
  272. });
  273. columnCursor++;
  274. }
  275.  
  276. public void initGrid(int rows, int columns){
  277. for(int i = 0; i < columns+100; i++){
  278. ArrayList<String> newdata = new ArrayList<String>();
  279. for(int j = 0; j < rows; j++){
  280. newdata.add(null);
  281. }
  282. columnnames.add(String.valueOf(i));
  283. data.add(newdata);
  284. maxRowCount = rows;
  285. }
  286. SwingUtilities.invokeLater (new Runnable(){ // fixes a nasty java vector bug
  287. public void run () {
  288. fireTableStructureChanged();
  289. fireTableDataChanged();
  290. }
  291. });
  292. }
  293.  
  294. /**
  295. * Rotates the list. If list.size() is smaller than
  296. * maxRowCount the list if filled with null values
  297. * This generates the bottom up effect
  298. * @param columnName - The column name
  299. * @param list
  300. * @return list
  301. */
  302. private ArrayList<String> rotateFillList(String columnName, ArrayList<String> list){
  303. list.add(0,columnName); // set column name to be on the bottom
  304.  
  305. if(maxRowCount < list.size()){
  306. // adjust all rows to the new maxRowCount
  307. maxRowCount = list.size();
  308. for(int i = 0; i < data.size(); i++){
  309. int diff = maxRowCount - data.get(i).size();
  310. for(int j = 0; j < diff; j++){
  311. data.get(i).add(0,null);
  312. }
  313. }
  314. }
  315. else { // fill with null values
  316. int diff = maxRowCount - list.size();
  317. for(int i = 0; i < diff; i++){
  318. list.add(null);
  319. }
  320. }
  321.  
  322. ArrayList<String> rotatedList = new ArrayList<String>();
  323. for(int i= list.size()-1;i>=0;i--){ // rotate list
  324. rotatedList.add(list.get(i));
  325. }
  326.  
  327. return rotatedList;
  328. }
  329. }
  330. }
Add Comment
Please, Sign In to add comment