ResultsPane.java 14.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
package morfeusz;

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.text.*;
import java.util.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

import pl.sgjp.morfeusz.*;

@SuppressWarnings("serial")
public class ResultsPane extends JTable 
{

	public static final String[] HEADERS1 = { "", "", "Forma", "Lemat", "Tag", "Nazwa", "Kwalifikatory" };
	public static final String[] HEADERS2 = { "Forma", "Lemat", "Tag", "Nazwa", "Kwalifikatory" };
	public static final Border emptyBorder = BorderFactory.createEmptyBorder(0, 2, 0, 2);
	public static final Border lineBorder = BorderFactory.createCompoundBorder(
		BorderFactory.createCompoundBorder(
			BorderFactory.createEmptyBorder(0, -1, -1, -1),
			BorderFactory.createLineBorder(Color.lightGray)),
		BorderFactory.createEmptyBorder(0, 2, 0, 2));
	
	private Morfeusz controller;
	private JTable resultsTable;
	private ColumnSorter columnSorter;
	private java.util.List<MorphInterpretation> results;
	private HashSet<Integer> interpBoundaries;
	private PlaceHolder placeHolder;
	private String[] headers;
	private TableColumn[] tableColumns;
	private boolean analyze;
	private boolean columnsAreVisible;
	
	public ResultsPane(boolean analyze, Morfeusz controller, PlaceHolder placeHolder)
	{
		super();
		this.controller = controller;
		this.analyze = analyze;
		this.placeHolder = placeHolder;
		resultsTable = this;
		initTable();
	}
	
	public void initTable()
	{

		if (analyze) headers = HEADERS1;
		else headers = HEADERS2;
		
		TableViewModel model = new TableViewModel();
		FontMetrics fm = getFontMetrics(Morfeusz.boldFont);

		columnSorter = new ColumnSorter();
		setModel(model);
		setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createEmptyBorder(-1, -1 , 0, -1),
				BorderFactory.createLineBorder(Color.lightGray)));
		setBackground(Morfeusz.LIGHT_GRAY);
		setGridColor(Color.lightGray);
		setShowHorizontalLines(false);
		setAutoResizeMode(AUTO_RESIZE_OFF);
		setRowSelectionAllowed(false);
		setRowHeight(fm.getHeight() + 4);

		TableColumnModel columnModel = getColumnModel();
		TableCellRenderer cellRenderer = new RowRenderer();
		TableHeader tableHeader = new TableHeader(columnModel);
		setTableHeader(tableHeader);
		tableColumns = new TableColumn[headers.length];

		for (int i = 0; i < columnModel.getColumnCount(); i++) {
			TableColumn column = columnModel.getColumn(i);
			
			tableColumns[i] = column;
			column.setHeaderValue(headers[i]);
			column.setCellRenderer(cellRenderer);
			column.setHeaderRenderer(tableHeader.getDefaultRenderer());
		}
		
		getTableHeader().setReorderingAllowed(false);
		columnsAreVisible = true;
		showColumns(false);
	}
	
	public void sortTable(int sortedColumn)
	{
		if (controller.getPreferences().sortedColumn != sortedColumn) {
			controller.getPreferences().sortedColumn = sortedColumn;
			columnSorter.setSortedColumn(sortedColumn);
			resetResults();
		}
	}
	
	public void showColumns(boolean show)
	{
		if (columnsAreVisible != show) {
			columnsAreVisible = show;
			for (int i = 0; i < tableColumns.length; i++) {
				if (show) addColumn(tableColumns[i]);
				else removeColumn(tableColumns[i]);
			}
			if (show) {
				TableColumnModel columnModel = getColumnModel();
				
				for (int i = 0; i < columnModel.getColumnCount(); i++) {
					TableColumn column = columnModel.getColumn(i);
					
					column.setHeaderRenderer(tableHeader.getDefaultRenderer());
				}
			}
		}
		placeHolder.showBottomLine(show);
	}
	
	public void calculateSizes()
	{
		TableColumnModel columnModel = getColumnModel();
		TableColumn tableColumn;
		TableCellRenderer renderer;
		Component component;
		Object value;
		int w;
		
		for (int column = 0; column < columnModel.getColumnCount(); column++) {
			w = 0;
			tableColumn = columnModel.getColumn(column);
			for (int row = 0; row < getRowCount(); row++) {
				renderer = getCellRenderer(row, column);
				value = getValueAt(row, column);
				component = renderer.getTableCellRendererComponent(this, 
					value, false, false, row, column);
				w = Math.max (component.getPreferredSize().width, w); 
			}
			renderer = tableColumn.getHeaderRenderer();
			value = tableColumn.getHeaderValue();
			component = renderer.getTableCellRendererComponent(this, 
				value, false, false, 0, column);
			w = Math.max (component.getPreferredSize().width, w);
			columnModel.getColumn(column).setPreferredWidth(w + 4);
		}
	}
	
	public void changeFont()
	{
		FontMetrics fm = getFontMetrics(Morfeusz.boldFont);
		TableHeader tableHeader = new TableHeader(columnModel);
		TableColumnModel columnModel = getColumnModel();
		
		setRowHeight(fm.getHeight() + 4);
		for (int i = 0; i < columnModel.getColumnCount(); i++) {
			TableColumn column = columnModel.getColumn(i);
			
			column.setHeaderRenderer(tableHeader.getDefaultRenderer());
		}
		calculateSizes();
		revalidate();
		repaint();
		getTableHeader().repaint();
	}
	
	public boolean isCellEditable(int row, int column)
	{
		return false;
	}
	
	public void setResults(java.util.List<MorphInterpretation> results, 
		HashSet<Integer> interpBoundaries)
	{
		this.results = results;
		this.interpBoundaries = interpBoundaries;
		resetResults();
	}
	
	public void resetResults()
	{
		if (results == null) showColumns(false);
		else {
			if (!analyze) Collections.sort(results, columnSorter);
			showColumns(true);
			calculateSizes();
			revalidate();
			repaint();
		}
		getTableHeader().repaint();
	}
	
	public boolean newNode(int row)
	{
		boolean f = true;
		
		if (row > 0) {
			MorphInterpretation i1 = results.get(row);
			MorphInterpretation i2 = results.get(row - 1);
			int sn1 = i1.getStartNode(), en1 = i1.getEndNode(),
				sn2 = i2.getStartNode(), en2 = i2.getEndNode();
			
			if (sn1 == sn2 && en1 == en2 && !interpBoundaries.contains(row)) f = false;
		}
		return f;
	}
	
	private class TableViewModel extends AbstractTableModel
	{
		
		public TableViewModel()
		{
			super();
		}
		
		public int getRowCount()
		{
			if (results == null) return 0;
			return (int)results.size();
		}
		
		public int getColumnCount()
		{
			return headers.length;
		}
		
		public Object getValueAt(int row, int column)
		{
			MorphInterpretation interp = results.get(row);
			boolean f;
			
			if (analyze) f = newNode(row);
			else {
				f = true;
				column += 2;
			}
				
			switch (column) {
				case 0 : if (f) return(Integer.toString(interp.getStartNode()));
						 return null;
				case 1 : if (f) return(Integer.toString(interp.getEndNode()));
						 return null;
				case 2 : if (f) {
							String str = interp.getOrth();
							
							if (str.contains(" ")) str = str.replace(' ', '\u23B5');
							return(str);
						 }
						 return null;
				case 3 : return(interp.getLemma());
				case 4 : return(interp.getTag(controller.getMorfeusz()));
				case 5 : return(interp.getName(controller.getMorfeusz()));
				case 6 : 
					Collection<String> labels = interp.getLabels(controller.getMorfeusz());
					String str = null;
					boolean append = false;
					
					if (labels.size() > 0) {
						str = "[";
						for (String s : labels) {
							if (append) str += "; ";
							str += s;
							append = true;
						}
						str += "]";
					}
					return str;
				default : return null;
			}
		}
		
	}
	
	private class RowRenderer extends DefaultTableCellRenderer
	{
		
		public Component getTableCellRendererComponent(JTable table, Object value,
				boolean isSelected, boolean hasFocus, int row, int column)
		{
			JLabel label = (JLabel)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

			if (analyze) {
				boolean f = newNode(row) && row > 0;
				
				if (column <= 1) label.setHorizontalAlignment(JLabel.CENTER);
				else label.setHorizontalAlignment(JLabel.LEFT);
				if (column == 2) setFont(Morfeusz.boldFont);
				else if (column == 5 || column == 6) setFont(Morfeusz.italicFont);
				else setFont(Morfeusz.plainFont);
				if (f) setBorder(lineBorder);
				else setBorder(emptyBorder);
			}
			else {
				if (column == 0) setFont(Morfeusz.boldFont);
				else if (column == 1) setFont(Morfeusz.plainFont);
				else setFont(Morfeusz.italicFont);
				setBorder(emptyBorder);
			}
			if (row % 2 == 0) setBackground(Morfeusz.VLIGHT_BLUE);
			else setBackground(Color.white);
			setForeground(Color.black);
			setValue(value);
			return this;
		}
		
	}
	
	private class TableHeader extends JTableHeader
	{
		
		private PlaceHolder corner = new PlaceHolder();
		private boolean initialized = false;
		private int cursorPos = -1;
		private int column = -1;
		
		public TableHeader(TableColumnModel model)
		{
			super(model);
			setDefaultRenderer(new HeaderRenderer());
			setResizingAllowed(true);
			corner.setLocation(0, 0);
			corner.showBottomLine(columnsAreVisible);
			add(corner);
			if (!analyze) {
				addMouseListener(new MouseAdapter() 
				{
					public void mouseClicked(MouseEvent event)
					{
						cursorPos = event.getX();
						
						TableColumnModel tcm = getColumnModel();
						
						column = tcm.getColumnIndexAtX(event.getX());
						
						if (column >= 0 && column <= 2) sortTable(column);
					}
					
					public void mouseReleased(MouseEvent event)
					{
					    cursorPos = event.getX();
					    
						TableColumnModel tcm = getColumnModel();
						
						column = tcm.getColumnIndexAtX(cursorPos);
						adjustCursor();
					}
				});
				addMouseMotionListener(new MouseMotionAdapter() {
					public void mouseMoved(MouseEvent event) 
					{
						cursorPos = event.getX();
						
					    TableColumnModel model = resultsTable.getColumnModel();
					    int c = model.getColumnIndexAtX(cursorPos);
					    
					    TableColumn col = null;
					    if (c >= 0 && c <= 2) col = model.getColumn(c);
					    if (col != null) {
					    	if (c != column) {
					    		switch (c) {
					    			case 0 : setToolTipText("Sortuj formy."); break;
					    			case 1 : setToolTipText("Sortuj lematy."); break;
					    			case 2 : setToolTipText("Sortuj tagi."); break;
					    			default : setToolTipText(null);
					    		}
					    	}
					    }
					    else setToolTipText(null);
					    if (c != column) column = c;
					    adjustCursor();
					}
				});
			}
		}
		
		public void adjustCursor()
		{
		    if (column >= 0 && column <= 2) {
		    	if (isResizeSpot(column, cursorPos)) 
		    		setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
		    	else setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		    }
		    else {
		    	if (isResizeSpot(column, cursorPos))
		    		setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
		    	else setCursor(Cursor.getDefaultCursor());
		    }
		}
		
		public boolean isResizeSpot(int column, int x)
		{
			TableColumnModel model = resultsTable.getColumnModel();
			Rectangle rect = getHeaderRect(column);
			int left, right, count = model.getColumnCount();
			
			if (column < 0 || column >= count) return false;
			else if (column == 0) {
				left = rect.x;
				right = left + rect.width - 3;
			}
			else {
				left = rect.x += 3;
				right = left + rect.width - 6;
			}
			if (x >= left && x < right) return false;
			return true;
		}
		
		public Dimension getPreferredSize()
		{
			Dimension dim = super.getPreferredSize();
			FontMetrics fm = getFontMetrics(Morfeusz.boldFont);
			
			dim.height = fm.getHeight() + 4;
			return dim;
		}
		
		public void setBounds(int x, int y, int width, int height)
		{
			super.setBounds(x,  y,  width,  height);
			moveCorner();
			initialized = true;
		}
		
		public void moveCorner()
		{
			TableColumnModel tcm = getColumnModel();
			Rectangle rect;
			
			rect = getHeaderRect(tcm.getColumnCount() - 1);
			corner.setLocation(rect.x + rect.width, 0);
			corner.setSize(getWidth() - rect.x - rect.width, getHeight());
		}
		
		public void resizeAndRepaint()
		{
			super.resizeAndRepaint();
			if (initialized) {
				moveCorner();
				corner.showBottomLine(columnsAreVisible);
			}
		}
	}
	
	private class HeaderRenderer implements TableCellRenderer
	{
		
		private JLabel renderer;
		private Icon startNode, endNode, downArrow;
		
		public HeaderRenderer()
		{
			Border headerBorder = UIManager.getBorder("TableHeader.cellBorder");
			URL url;
			
			renderer = new JLabel();
			renderer.setFont(Morfeusz.boldFont);
			renderer.setBorder(headerBorder);
			renderer.setBackground(UIManager.getColor("TableHeader.background"));
			renderer.setForeground(UIManager.getColor("TableHeader.foreground"));
			if (analyze) {
				url = ClassLoader.getSystemClassLoader().getResource("start_node.gif");
				startNode = new ImageIcon(url);
				url = ClassLoader.getSystemClassLoader().getResource("end_node.gif");
				endNode = new ImageIcon(url);
			}
			else {
				url = ClassLoader.getSystemClassLoader().getResource("down.gif");
				downArrow = new ImageIcon(url);
			}
		}
		
		public Component getTableCellRendererComponent(JTable table,
			Object value, boolean isSelected, boolean hasFocus, int row, int column) 
		{
			renderer.setText((String)value);
			if (analyze) {
				if (column == 0) {
					renderer.setIcon(startNode);
					renderer.setHorizontalAlignment(JLabel.CENTER);
				}
				else if (column == 1) {
					renderer.setIcon(endNode);
					renderer.setHorizontalAlignment(JLabel.CENTER);
				}
				else {
					renderer.setIcon(null);
					renderer.setHorizontalAlignment(JLabel.LEFT);
				}
			}
			else {
				int sortedColumn = controller.getPreferences().sortedColumn;
				
				if (column == sortedColumn)	renderer.setIcon(downArrow);
				else renderer.setIcon(null);
				renderer.setHorizontalAlignment(JLabel.LEFT);
			}
			return renderer;
		}
		
	}
	
	private class ColumnSorter implements Comparator<MorphInterpretation>
	{

		private Collator collator = Collator.getInstance(new Locale("pl", "PL"));
		private int sortedColumn;
		
		public ColumnSorter()
		{
			sortedColumn = controller.getPreferences().sortedColumn;
		}
		
		public void setSortedColumn(int sortedColumn)
		{
			this.sortedColumn = sortedColumn;
		}
		
		public int compare(MorphInterpretation mi1, MorphInterpretation mi2) 
		{
			if (sortedColumn == 0) return compareForms(mi1, mi2);
			else if (sortedColumn == 1) return compareLemmas(mi1, mi2);
			return compareTags(mi1, mi2);
		}
		
		public int compareForms(MorphInterpretation mi1, MorphInterpretation mi2)
		{
			String s1 = mi1.getOrth(), s2 = mi2.getOrth();
			int res = collator.compare(s1, s2);
			
			if (res == 0) {
				s1 = mi1.getLemma(); 
				s2 = mi2.getLemma();
				res = collator.compare(s1, s2);
			}
			return res;
		}
		
		public int compareLemmas(MorphInterpretation mi1, MorphInterpretation mi2)
		{
			String s1 = mi1.getLemma(), s2 = mi2.getLemma();
			int res = collator.compare(s1, s2);
			
			if (res == 0) {
				s1 = mi1.getOrth();
				s2 = mi2.getOrth();
				res = collator.compare(s1, s2);
			}
			return res;
		}
		
		public int compareTags(MorphInterpretation mi1, MorphInterpretation mi2)
		{
			String s1 = mi1.getTag(controller.getMorfeusz()), s2 = mi2.getTag(controller.getMorfeusz());
			
			return collator.compare(s1,  s2);
		}
		
	}
	
}