Morfeusz.java 27.1 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 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
package morfeusz;

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

import javax.swing.*;
import javax.swing.event.*;

import quitaboutpreferenceshandler.*;

@SuppressWarnings("serial")
public class Morfeusz extends JFrame
{

	public static final String WINDOW_TITLE = "Analizator Morfologiczny Morfeusz";
	public static final String STANDARD = "Standardowy (" + pl.sgjp.morfeusz.Morfeusz.getDefaultDictName() + ")";
	public static final String DICT_MESSAGE = "<html>Wyst\u0105pi\u0142 b\u0142\u0105d przy pr\u00F3bie zamiany s\u0142ownika.<br/>" + 
		"S\u0142ownik nie zosta\u0142 zmieniony.</html>";
	public static final String NATIVE_ERROR_MESSAGE = "Wyst\u0105pi\u0142 nieznany b\u0142\u0105d";
	public static final Color LIGHT_GRAY = new Color(240, 240, 240);
	public static final Color VLIGHT_BLUE	= new Color(237, 243, 254);
	public static final Font FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 10);
	public static final int NUM_OF_FONT_SIZES = 10;
	
	private static boolean isMacOS;
	
	public static Font plainFont;
	public static Font boldFont;
	public static Font italicFont; 
	private java.util.List<String> defaultSearchPaths;
	private java.util.List<String> dictSearchPaths;
	private java.util.List<String> dictionaries;
	private Agent agent;
	private Preferences preferences;
	private JTabbedPane tabbedPane;
	private AnalyzeView analyzeView;
	private GenerateView generateView;
	private JTextArea textPane;
	private JTextField lemmaField;
	private ResultsPane analyzePane;
	private ResultsPane generatePane;
	private Action exitAction;
	private Action aboutAction;
	private Action optionsAction;
	private Action resetAction;
	private Action analyzeAction;
	private Action generateAction;
	private Action openAction;
	private Action saveActionA;
	private Action saveActionG;
	private Action changeAction;
	private Action changeToStandardAction;
	private Action editAction;
	private Action increaseFontAction;
	private Action decreaseFontAction;
	private Action insertNewLineAction;
	private JButton exitButton;
	private JButton resetButton;
	private JButton actionButton;
	private ToolBarButton returnButton;
	private ToolBarButton openButton;
	private ToolBarButton saveButtonA;
	private ToolBarButton saveButtonG;
	private ToolBarButton changeButtonA;
	private ToolBarButton changeButtonG;
	private ToolBarButton aboutButton;
	private ToolBarButton optionsButton;
	private ToolBarButton increaseFontButton;
	private ToolBarButton decreaseFontButton;
	private JCheckBox appendCheckBoxA;
	private JCheckBox appendCheckBoxG;
	private JPopupMenu changePopup;
	private JLabel analyzeDictionaryName;
	private JLabel generateDictionaryName;
	private File inputFile;
	private File outputFile;
	private DictFilter filter;
	private String dictionary;
	private boolean analyze;
	private Rectangle topLeft = new Rectangle(0, 0, 0, 0);
	
	public Morfeusz()
	{
		String str = System.getProperty("os.name");
		
		if (str != null && str.contains("Mac OS X")) {
			new QuitAboutPreferencesHandler() {
				public void handleQuitRequest() 
				{
					exit();
				}
				
				public void handleAboutRequest()
				{
					about();
				}

				public void handlePreferencesRequest() 
				{
					options();
				}
			};
			System.setProperty("apple.laf.useScreenMenuBar", "true");
			isMacOS = true;
		}
		else isMacOS = false;
		
		readPreferences();
		plainFont = FONT.deriveFont(Font.PLAIN, preferences.fontSize + 10);
		boldFont = FONT.deriveFont(Font.BOLD, preferences.fontSize + 10);
		italicFont = FONT.deriveFont(Font.ITALIC, preferences.fontSize + 10);
		initialize();
		agent = Agent.createAgent(this);
		if (agent == null) {
			JOptionPane.showMessageDialog(null, NATIVE_ERROR_MESSAGE, "Uwaga", JOptionPane.ERROR_MESSAGE);
			exit();
		}
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		setTitle(WINDOW_TITLE);
		setSizeAndPosition();
		arrangeComponents();
		getRootPane().setDefaultButton(actionButton);
		checkComponents();
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent event)
			{
				exit();
			}
		});
	}
	
	public void initialize()
	{
		openAction = new AbstractAction("Czytaj z pliku") {
			public void actionPerformed(ActionEvent e)
			{
				open();
			}
		};

		optionsAction = new AbstractAction("Opcje programu") {
			public void actionPerformed(ActionEvent e)
			{
				options();
			}
		};
		
		saveActionA = new AbstractAction("Zapisz Wyniki Analizy") {
			public void actionPerformed(ActionEvent e)
			{
				save();
			}
		};
		
		saveActionG = new AbstractAction("Zapisz Wygenerowane Formy") {
			public void actionPerformed(ActionEvent e)
			{
				save();
			}
		};
		
		changeToStandardAction = new AbstractAction("S\u0142ownik Standardowy") {
			public void actionPerformed(ActionEvent e)
			{
				changeToStandardDictionary();
			}
		};
		
		changeAction = new AbstractAction() {
			public void actionPerformed(ActionEvent e)
			{
				changeDictionary();
			}
		};
		
		editAction = new AbstractAction("Ustaw \u015Acie\u017Ck\u0119 Wyszukiwania S\u0142ownik\u00F3w...") {
			public void actionPerformed(ActionEvent e)
			{
				editDictSearchPaths();
			}
		};
		
		exitAction = new AbstractAction("Zako\u0144cz") {
			public void actionPerformed(ActionEvent e)
			{
				exit();
			}
		};
		
		aboutAction = new AbstractAction("About") {
			public void actionPerformed(ActionEvent e)
			{
				about();
			}
		};
		
		increaseFontAction = new AbstractAction("Powi\u0119ksz czcionk\u0119") {
			public void actionPerformed(ActionEvent e)
			{
				changeFontSize(true);
			}
		};
		
		decreaseFontAction = new AbstractAction("Zmniejsz czcionk\u0119") {
			public void actionPerformed(ActionEvent e)
			{
				changeFontSize(false);
			}
		};
		
		resetAction = new AbstractAction("Wyczy\u015B\u0107") {
			public void actionPerformed(ActionEvent e)
			{
				reset();
			}
		};
		
		analyzeAction = new AbstractAction("Analizuj") {
			public void actionPerformed(ActionEvent e)
			{
				analyze();
			}
		};
		
		generateAction = new AbstractAction("Generuj") {
			public void actionPerformed(ActionEvent e)
			{
				generate();
			}
		};
		
		insertNewLineAction = new AbstractAction() {
			public void actionPerformed(ActionEvent e)
			{
				insertNewLine();
			}
		};
		
		filter = new DictFilter();
		dictSearchPaths = pl.sgjp.morfeusz.Morfeusz.getDictionarySearchPaths();
		defaultSearchPaths = new LinkedList<String>();
		for (String path : dictSearchPaths) defaultSearchPaths.add(path);
		
		if (preferences.dictPaths != null) {
			dictSearchPaths.clear();
			for (String path : preferences.dictPaths) {
				dictSearchPaths.add(path);
			}
		}
		else if (dictSearchPaths.size() > 0) {
			int i = 0;
			
			preferences.dictPaths = new String[dictSearchPaths.size()];
			for (String path : dictSearchPaths) {
				preferences.dictPaths[i++] = path;
			}
		}
		getDictionaries();
		if (preferences.dictionary == null) dictionary = STANDARD;
		else dictionary = preferences.dictionary;
		if (!dictionaries.contains(dictionary))  dictionary = STANDARD;
		analyze = true;
	}
	
	public void getDictionaries()
	{
		ListIterator<String> it;
		
		dictionaries = new LinkedList<String>();
		if (dictSearchPaths.size() > 0) {
			it = dictSearchPaths.listIterator();
			while (it.hasNext()) {
				String path = it.next();
				File dir = new File(path);
				
				if (dir.exists()) addDictionaries(dir);
			}
		}
		else {
			File currentDir = new File(System.getProperty("user.dir"));	
			
			addDictionaries(currentDir);
		}
		changePopup = new JPopupMenu();
		changePopup.add(changeToStandardAction);
		changePopup.addSeparator();
		if (!dictionaries.isEmpty()) {
			for (String d : dictionaries) {
				changePopup.add(new RecentFileAction(d, this));
			}
			changePopup.addSeparator();
		}
		changePopup.add(editAction);
	}
	
	public void addDictionaries(File dir)
	{
		String[] d = dir.list(filter);
		
		for (String str : d) {
			int index = str.lastIndexOf("-");
			
			str = str.substring(0, index);
			if (!dictionaries.contains(str)) dictionaries.add(str);
		}
	}
	
	public void setSizeAndPosition()
	{
		Toolkit	toolkit = Toolkit.getDefaultToolkit();
		Dimension dim = toolkit.getScreenSize();
		GraphicsDevice dev = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		GraphicsConfiguration conf = dev.getDefaultConfiguration();
		Insets insets	= toolkit.getScreenInsets(conf);
		Rectangle rect = new Rectangle();
		
		rect.x = (dim.width - insets.left - insets.right) / 4;
		rect.y = (dim.height - insets.top - insets.bottom) / 8;
		rect.width = 2 * rect.x;
		rect.height = 6 * rect.y;
		
		setBounds(rect);
	}
	
	public void arrangeComponents()
	{
		JPanel mainView = new JPanel(new BorderLayout());
		Box hbox, vbox;
		
		mainView.setBorder(BorderFactory.createEmptyBorder(16, 16, 16, 16));
		tabbedPane = new JTabbedPane();
		analyzeView = new AnalyzeView();
		generateView = new GenerateView();
		tabbedPane.setFocusable(false);
		tabbedPane.addTab("Analizator", analyzeView);
		tabbedPane.addTab("Generator", generateView);
		mainView.add(tabbedPane, BorderLayout.CENTER);
		
		exitButton = new JButton(exitAction);
		resetButton = new JButton(resetAction);
		actionButton = new JButton(analyzeAction);
		aboutButton = new ToolBarButton(aboutAction, "info");
		aboutButton.setToolTipText("Informacje o programie.");
		optionsButton = new ToolBarButton(optionsAction, "conf");
		optionsButton.setToolTipText("Parametry programu.");
		increaseFontButton = new ToolBarButton(increaseFontAction, "font_increase");
		increaseFontButton.setToolTipText("Powi\u0119ksz czcionk\u0119.");
		decreaseFontButton = new ToolBarButton(decreaseFontAction, "font_decrease");
		decreaseFontButton.setToolTipText("Zmniejsz czcionk\u0119.");
		increaseFontButton.setEnabled(preferences.fontSize < NUM_OF_FONT_SIZES - 1);
		decreaseFontButton.setEnabled(preferences.fontSize > 0);
		
		vbox = Box.createVerticalBox();
		if (!isMacOS) vbox.add(Box.createVerticalStrut(16));
		hbox = Box.createHorizontalBox();
		hbox.add(aboutButton);
		hbox.add(Box.createHorizontalStrut(8));
		hbox.add(optionsButton);
		hbox.add(Box.createHorizontalStrut(8));
		hbox.add(increaseFontButton);
		hbox.add(Box.createHorizontalStrut(8));
		hbox.add(decreaseFontButton);
		hbox.add(Box.createHorizontalStrut(8));
		hbox.add(exitButton);
		hbox.add(Box.createHorizontalStrut(8));
		hbox.add(resetButton);
		hbox.add(Box.createHorizontalGlue());
		hbox.add(Box.createHorizontalStrut(8));
		hbox.add(actionButton);
		vbox.add(hbox);
		mainView.add(vbox, BorderLayout.SOUTH);
		getContentPane().add(mainView);
		
		tabbedPane.addChangeListener(new ChangeListener() {
	        public void stateChanged(ChangeEvent e) 
	        {
	        	int i = tabbedPane.getSelectedIndex();
	        	
	        	if (i == 0) {
	        		actionButton.setAction(analyzeAction);
	        		analyze = true;
	        	}
	        	else {
	        		actionButton.setAction(generateAction);
	        		analyze = false;
	        	}
	        	checkComponents();
	        }
	    });
	}
	
	public void open()
	{
		SelectFileDialog dialog = new SelectFileDialog(this, SelectFileDialog.LOAD);
		
		dialog.setVisible(true);
		if (dialog.accept()) {
			inputFile = dialog.getSelectedFile();
			readText(inputFile, dialog.getEncoding());
		}
		dialog.dispose();
	}
	
	public void readText(File file, String encoding)
	{
		try {
			BufferedReader reader = 
				new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
			String line;
			boolean addNewLine = false;
			
			textPane.setText("");
			while ((line = reader.readLine()) != null) {
				if (addNewLine) textPane.append("\n");
				textPane.append(Normalizer.normalize(line, Normalizer.Form.NFC));
				addNewLine = true;
			}
			reader.close();
			EventQueue.invokeLater(new Runnable() {
				public void run()
				{
					textPane.scrollRectToVisible(topLeft);
				}
			});
		}
		catch (Exception exception) {
			exception.getStackTrace();
		}
	}
	
	public void save()
	{
		boolean select = true;
		boolean append = (analyze ? appendCheckBoxA.isSelected() : appendCheckBoxG.isSelected());
		
		if (append) {
			if (outputFile != null && outputFile.exists()) select = false;
		}
		if (select) {
			SelectFileDialog dialog = new SelectFileDialog(this, SelectFileDialog.SAVE);
			
			dialog.setSelectedFile(new File("Results.txt"));
			dialog.setVisible(true);
			if (dialog.accept()) {
				outputFile = dialog.getSelectedFile();
				save(outputFile, append);
				appendCheckBoxA.setSelected(true);
			}
		}
		else {
			save(outputFile, append);
		}
	}
	
	public void save(File file, boolean append)
	{
		try {
			OutputStreamWriter writer = new OutputStreamWriter(
				new FileOutputStream(file, append), "UTF8");
			BufferedWriter bw = new BufferedWriter(writer);
			PrintWriter pw = new PrintWriter(bw);
			
			if (append) pw.println();
			if (analyze) agent.saveAnalyzeResults(pw);
			else agent.saveGenerateResults(pw);
			pw.flush();
			pw.close();
		}
		catch (Exception exception) {
			exception.getStackTrace();
		}
	}
	
	public void changeDictionaryName(String name)
	{
		dictionary = name;
		if (name.equals(STANDARD)) {
			analyzeDictionaryName.setText(name);
			analyzeDictionaryName.setForeground(Color.gray);
			generateDictionaryName.setText(name);
			generateDictionaryName.setForeground(Color.gray);
		}
		else {
			analyzeDictionaryName.setText(name);
			analyzeDictionaryName.setForeground(Color.black);
			generateDictionaryName.setText(name);
			generateDictionaryName.setForeground(Color.black);
		}
	}
	
	public void changeToStandardDictionary()
	{
		preferences.dictionary = null;
		agent.setDictionary(null);
		refreshAnalyzeResults();
		refreshGenerateResults();
		changeDictionaryName(STANDARD);
	}
	
	public void changeDictionary()
	{
		if (analyze) changePopup.show(changeButtonA, 0, 0);
		else changePopup.show(changeButtonG, 0, 0);
	}
	
	public void changeDictionary(String name)
	{
		if (agent.setDictionary(name)) {
			preferences.dictionary = name;
			changeDictionaryName(name);
			refreshAnalyzeResults();
			refreshGenerateResults();
		}
		else {
			JOptionPane.showMessageDialog(null, DICT_MESSAGE, "Uwaga", JOptionPane.ERROR_MESSAGE);
			removeFromChangePopup(name);
		}
	}
	
	public void removeFromChangePopup(String name)
	{
		JMenuItem item = null;
		int count = changePopup.getComponentCount() - 2;
		
		for (int i = 2; i < count; i++) {
			JMenuItem menuItem = (JMenuItem)changePopup.getComponent(i);
			RecentFileAction action = (RecentFileAction)menuItem.getAction();
			
			if (action.getName().equals(name)) {
				item = menuItem;
				break;
			}
		}
		if (item != null) {
			changePopup.remove(item);
			if (count == 3) changePopup.remove(2);
		}
	}
	
	public void exit()
	{
		savePreferences();
		System.exit(0);
	}
	
	public void insertNewLine()
	{
		textPane.insert("\n", textPane.getCaretPosition());
	}
	
	public void options()
	{
		OptionsDialog dialog = new OptionsDialog(this);
		
		if (dialog.doDialog() != 0) {
			dialog.transfer(preferences);
			agent.initialize();
			refreshAnalyzeResults();
			refreshGenerateResults();
		}
		dialog.dispose();
	}
	
	public void editDictSearchPaths()
	{
		DictSearchPathsDialog dialog = new DictSearchPathsDialog(dictSearchPaths, defaultSearchPaths);
		
		if (dialog.doDialog() != 0) {
			getDictionaries();
			preferences.dictPaths = null;
			if (dictSearchPaths.size() > 0) {
				int i = 0;
				
				preferences.dictPaths = new String[dictSearchPaths.size()];
				for (String path : dictSearchPaths) {
					preferences.dictPaths[i++] = path;
				}
			}
		}
		dialog.dispose();
	}
	
	public void readPreferences()
	{
		String path = System.getProperty("user.home") + File.separator + ".morfeuszGUI";
		File file = new File(path);
		ObjectInputStream input;
		
		try {
			input = new ObjectInputStream(new FileInputStream(file));
			preferences = (Preferences)input.readObject();
			input.close();
		}
		catch (Exception exception) {
			preferences = new Preferences();
			preferences.setDefaults();
		}
	}
	
	public void savePreferences()
	{
		String path = System.getProperty("user.home") + File.separator + ".morfeuszGUI";
		File file = new File(path);
		ObjectOutputStream output;

		try {
			output = new ObjectOutputStream(new FileOutputStream(file));
			output.writeObject(preferences);
			output.close();
		}
		catch (Exception exception) {
		}
	}
	
	public Preferences getPreferences()
	{
		return preferences;
	}
	
	public void about()
	{
		AboutDialog dialog = new AboutDialog(agent.getMorfeusz());
		
		dialog.doDialog();
	}
	
	public void changeFontSize(boolean increase)
	{
		if (increase) preferences.fontSize++;
		else preferences.fontSize--;
		increaseFontButton.setEnabled(preferences.fontSize < NUM_OF_FONT_SIZES - 1);
		decreaseFontButton.setEnabled(preferences.fontSize > 0);
		plainFont = new Font(plainFont.getFontName(), plainFont.getStyle(), preferences.fontSize + 10);
		boldFont = new Font(boldFont.getFontName(), boldFont.getStyle(), preferences.fontSize + 10);
		italicFont = new Font(italicFont.getFontName(), italicFont.getStyle(), preferences.fontSize + 10);
		textPane.setFont(plainFont);
		analyzeView.changeFont();
		generateView.changeFont();
	}
	
	public pl.sgjp.morfeusz.Morfeusz getMorfeusz()
	{
		if (agent != null) return agent.getMorfeusz();
		return null;
	}
	
	public void reset()
	{
		if (analyze) {
			textPane.setText(null);
			textPane.requestFocusInWindow();
			analyzePane.showColumns(false);
			agent.clearAnalyzeResults();
		}
		else {
			lemmaField.setText(null);
			lemmaField.requestFocusInWindow();
			generatePane.showColumns(false);
			agent.clearGenerateResults();
		}
		checkComponents();
		System.gc();
	}
	
	public void analyze()
	{
		agent.setTextToAnalyze(textPane.getText());
		if (agent.analyze()) {
			if (agent.hasAnalyzeResults()) {
				analyzePane.setResults(agent.getAnalyzeResults(), agent.getInterpBoundaries());
				analyzePane.scrollRectToVisible(topLeft);
			}
			else analyzePane.setResults(null, null);
			checkComponents();
		}
		else {
			JOptionPane.showMessageDialog(null, NATIVE_ERROR_MESSAGE, "Uwaga", JOptionPane.ERROR_MESSAGE);
			analyzePane.showColumns(false);
			agent.clearAnalyzeResults();
		}
		System.gc();
	}
	
	public void refreshAnalyzeResults()
	{
		if (agent.hasAnalyzeResults()) {
			if (agent.analyze()) {
				if (agent.hasAnalyzeResults()) {
					analyzePane.setResults(agent.getAnalyzeResults(), agent.getInterpBoundaries());
					analyzePane.scrollRectToVisible(topLeft);
				}
				else analyzePane.setResults(null, null);
				checkComponents();
			}
			else {
				JOptionPane.showMessageDialog(null, NATIVE_ERROR_MESSAGE, "Uwaga", JOptionPane.ERROR_MESSAGE);
				analyzePane.showColumns(false);
				agent.clearAnalyzeResults();
			}
			System.gc();
		}
	}
	
	public void generate()
	{
		String text = lemmaField.getText();
		String[] t;
		
		text = text.trim();
		t = text.split(" ");
		text = t[0];
		lemmaField.setText(text);
		agent.setLemma(lemmaField.getText());
		if (agent.generate()) {
			if (agent.hasGenerateResults()) {
				generatePane.setResults(agent.getGenerateResults(), null);
				generatePane.scrollRectToVisible(topLeft);
			}
			else generatePane.setResults(null, null);
			checkComponents();
		}
		else {
			JOptionPane.showMessageDialog(null, NATIVE_ERROR_MESSAGE, "Uwaga", JOptionPane.ERROR_MESSAGE);
			generatePane.showColumns(false);
			agent.clearGenerateResults();
		}
		System.gc();
	}
	
	public void refreshGenerateResults()
	{
		if (agent.hasGenerateResults()) {
			if (agent.generate()) {
				if (agent.hasGenerateResults()) {
					generatePane.setResults(agent.getGenerateResults(), null);
					generatePane.scrollRectToVisible(topLeft);
				}
				else generatePane.setResults(null, null);
				checkComponents();
			}
			else {
				JOptionPane.showMessageDialog(null, NATIVE_ERROR_MESSAGE, "Uwaga", JOptionPane.ERROR_MESSAGE);
				generatePane.showColumns(false);
				agent.clearGenerateResults();
			}
			System.gc();
		}
	}
	
	public void checkComponents()
	{
		boolean f1 = (analyze && textPane.getDocument().getLength() > 0),
				f2 = (!analyze && lemmaField.getDocument().getLength() > 0),
				f3 = (analyze && agent.hasAnalyzeResults()),
				f4 = (!analyze && agent.hasGenerateResults());
		
		analyzeAction.setEnabled(f1);
		generateAction.setEnabled(f2);
		saveActionA.setEnabled(f3);
		saveActionG.setEnabled(f4);
		resetAction.setEnabled(f1 || f2 || f3 || f4);
	}
	
	public static boolean isMacOS()
	{
		return isMacOS;
	}
	
	public static void main(String[] args) 
	{
		Morfeusz morfeuszWnd = new Morfeusz();
		
		morfeuszWnd.setVisible(true);
	}

	private class AnalyzeView extends JPanel
	{
		
		public AnalyzeView()
		{
			super(new BorderLayout());
			arrangeComponents();
		}
		
		public void arrangeComponents()
		{
			Box hbox, vbox;
			JLabel label;
			JScrollPane sp;
			PlaceHolder placeHolder;
			KeyStroke enterStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
			InputMap im;
			int x = (isMacOS() ? -4 : -2);
			
			vbox = Box.createVerticalBox();
			vbox.setBorder(BorderFactory.createEmptyBorder(16, 16, 8, 16));
			hbox = Box.createHorizontalBox();
			hbox.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("S\u0142ownik:"),
				BorderFactory.createEmptyBorder(4, 4, 8, 4)));
			analyzeDictionaryName = new JLabel(dictionary);
			if (dictionary.equals(STANDARD)) analyzeDictionaryName.setForeground(Color.gray);
			hbox.add(analyzeDictionaryName);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(Box.createHorizontalStrut(8));
			changeButtonA = new ToolBarButton(changeAction, "book_open");
			changeButtonA.setFocusable(false);
			changeButtonA.setToolTipText("Zmie\u0144 s\u0142ownik.");
			hbox.add(changeButtonA);
			vbox.add(hbox);
			vbox.add(Box.createVerticalStrut(16));
			hbox = Box.createHorizontalBox();
			hbox.setBorder(BorderFactory.createEmptyBorder(0, 0, x, 0));
			label = new JLabel("Tekst:");
			hbox.add(label);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(Box.createHorizontalStrut(32));
			returnButton = new ToolBarButton(insertNewLineAction, "return");
			returnButton.setFocusable(false);
			returnButton.setToolTipText("Nowa linia.");
			hbox.add(returnButton);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(Box.createHorizontalStrut(32));
			openButton = new ToolBarButton(openAction, "open");
			openButton.setFocusable(false);
			openButton.setToolTipText("Czytaj tekst z pliku.");
			hbox.add(openButton);
			vbox.add(hbox);
			
			textPane = new JTextArea();
			textPane.setFont(plainFont);
			textPane.setLineWrap(true);
			textPane.setWrapStyleWord(true);
			textPane.setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4));
			im = textPane.getInputMap();
			textPane.getActionMap().put(im.get(enterStroke), analyzeAction);
			sp = new JScrollPane(textPane);
			sp.setPreferredSize(new Dimension(0, 100));
			add(sp);
			vbox.add(sp);
			add(vbox, BorderLayout.NORTH);
			
			vbox = Box.createVerticalBox();
			vbox.setBorder(BorderFactory.createEmptyBorder(8, 16, 16, 16));
			hbox = Box.createHorizontalBox();
			hbox.setBorder(BorderFactory.createEmptyBorder(0, 0, x, 0));
			label = new JLabel("Analiza morfologiczna:");
			hbox.add(label);
			hbox.add(Box.createHorizontalGlue());
			appendCheckBoxA = new JCheckBox("Dopisz   ");
			appendCheckBoxA.setFocusable(false);
			hbox.add(appendCheckBoxA);
			saveButtonA = new ToolBarButton(saveActionA, "save");
			saveButtonA.setFocusable(false);
			saveButtonA.setToolTipText("Zapisz wynik analizy do pliku.");
			hbox.add(saveButtonA);
			vbox.add(hbox);
			
			placeHolder = new PlaceHolder();
			analyzePane = new ResultsPane(true, Morfeusz.this, placeHolder);
			sp = new JScrollPane(analyzePane);
			sp.getViewport().setBackground(LIGHT_GRAY);
			sp.setCorner(JScrollPane.UPPER_RIGHT_CORNER, placeHolder);
			vbox.add(sp);
			add(vbox, BorderLayout.CENTER);
			
			textPane.getDocument().addDocumentListener(new DocumentListener() {
				public void changedUpdate(DocumentEvent event) 
				{
				}

				public void insertUpdate(DocumentEvent event) 
				{
					checkComponents();
				}

				public void removeUpdate(DocumentEvent event) 
				{
					checkComponents();
				}
			});
			
			appendCheckBoxA.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) 
				{
					appendCheckBoxG.setSelected(appendCheckBoxA.isSelected());
				}
			});
		}
		
		public void changeFont()
		{
			analyzePane.changeFont();
		}
		
	}
	
	private class GenerateView extends JPanel
	{
		
		public GenerateView()
		{
			super(new BorderLayout());
			arrangeComponents();
		}
		
		public void arrangeComponents()
		{
			Box hbox, vbox;
			JLabel label;
			JScrollPane sp;
			PlaceHolder placeHolder;
			Dimension dim;
			int x = (isMacOS() ? -4 : -2);
			
			vbox = Box.createVerticalBox();
			hbox = Box.createHorizontalBox();
			hbox.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("S\u0142ownik:"),
				BorderFactory.createEmptyBorder(4, 4, 8, 4)));
			generateDictionaryName = new JLabel(dictionary);
			if (dictionary.equals(STANDARD)) generateDictionaryName.setForeground(Color.gray);
			hbox.add(generateDictionaryName);
			hbox.add(Box.createHorizontalGlue());
			hbox.add(Box.createHorizontalStrut(8));
			changeButtonG = new ToolBarButton(changeAction, "book_open");
			changeButtonG.setFocusable(false);
			changeButtonG.setToolTipText("Zmie\u0144 s\u0142ownik.");
			hbox.add(changeButtonG);
			vbox.add(hbox);
			vbox.add(Box.createVerticalStrut(20));
			hbox = Box.createHorizontalBox();
			vbox.setBorder(BorderFactory.createEmptyBorder(16, 16, 8, 16));
			label = new JLabel("Lemat:");
			hbox.add(label);
			hbox.add(Box.createHorizontalStrut(8));
			lemmaField = new JTextField(30);
			hbox.add(lemmaField);
			dim = lemmaField.getPreferredSize();
			lemmaField.setMaximumSize(dim);
			hbox.add(Box.createHorizontalGlue());
			vbox.add(hbox);
			add(vbox, BorderLayout.NORTH);
			
			vbox = Box.createVerticalBox();
			vbox.setBorder(BorderFactory.createEmptyBorder(8, 16, 16, 16));
			hbox = Box.createHorizontalBox();
			hbox.setBorder(BorderFactory.createEmptyBorder(0, 0, x, 0));
			label = new JLabel("Wygenerowane formy:");
			hbox.add(label);
			hbox.add(Box.createHorizontalGlue());
			appendCheckBoxG = new JCheckBox("Dopisz   ");
			appendCheckBoxG.setFocusable(false);
			hbox.add(appendCheckBoxG);
			saveButtonG = new ToolBarButton(saveActionG, "save");
			saveButtonG.setFocusable(false);
			saveButtonG.setToolTipText("Zapisz wygenerowane formy wyrazowe do pliku.");
			hbox.add(saveButtonG);
			vbox.add(hbox);
			
			placeHolder = new PlaceHolder();
			generatePane = new ResultsPane(false, Morfeusz.this, placeHolder);
			sp = new JScrollPane(generatePane);
			sp.getViewport().setBackground(LIGHT_GRAY);
			sp.setCorner(JScrollPane.UPPER_RIGHT_CORNER, placeHolder);
			vbox.add(sp);
			add(vbox, BorderLayout.CENTER);
			
			lemmaField.getDocument().addDocumentListener(new DocumentListener() {
				public void changedUpdate(DocumentEvent event) 
				{
				}

				public void insertUpdate(DocumentEvent event) 
				{
					checkComponents();
				}

				public void removeUpdate(DocumentEvent event) 
				{
					checkComponents();
				}
			});
			
			appendCheckBoxG.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) 
				{
					appendCheckBoxA.setSelected(appendCheckBoxG.isSelected());
				}
			});
		}
		
		public void changeFont()
		{
			generatePane.changeFont();
		}

	}
	
	public class DictFilter implements FilenameFilter
	{

		public boolean accept(File dir, String name) 
		{
			return (name.endsWith("-a.dict") || name.endsWith("-s.dict"));
		}
		
	}
	
}