⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 toocom源代码,主要应用在本体匹配方面!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package toocom.ui;

import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.filechooser.*;
import toocom.ocgl.*;
import toocom.iotools.*;
import toocom.matching.*;

/** 
 * This class represents the general graphical window for the application.
 *
 * @author Fr閐閞ic F黵st
 */
public class MainFrame extends JFrame implements ActionListener{

	// Current ontology
	private Ontology ontology;
	// Current ontology file
	private String ontoFile;
	// Cogitant client
	private CogitantClient cc;
	// Current languages
	private Language interfaceLanguage;
	private Language ontologyLanguage;
	
	// Ontology Menu items
	private JMenuItem newOntology;
	private JMenuItem openOntology;
	private JMenuItem importOWLOntology;
	private JMenuItem operationalizeOntology;
	private JMenuItem closeOntology;
	private JMenuItem saveOntology;
	private JMenuItem saveAsOntology;
	private JMenuItem exitOntology;
	
	// Test Menu items
	private JMenu testsMenu;
	
	// Ontology Language Menu
	private JMenu optionsOntologyLanguage;
	private LinkedList languageItemList;
	
	// Internal frames
	private JTabbedPane tabbedPane;
	private OntologySummaryFrame osFrame;
	private ConceptTypesFrame ctFrame;
	private RelationTypesFrame rtFrame;
	private AxiomsFrame axiomsFrame;
	
	public CogitantClient getCogitantClient(){
		return cc;
	}
	
	public ConceptTypesFrame getConceptTypesFrame(){ 
		return this.ctFrame;
	}
	
	public RelationTypesFrame getRelationTypesFrame(){ 
		return this.rtFrame;
	}
	
	public AxiomsFrame getAxiomsFrame(){ 
		return this.axiomsFrame;
	}
	
	public OntologySummaryFrame getOntologySummaryFrame(){ 
		return this.osFrame;
	}
	
	public void selectAxiomFrame(){
		tabbedPane.setSelectedIndex(2);
	}
	public void selectConceptTypesFrame(){
		tabbedPane.setSelectedIndex(0);
	}
	
	public void selectRelationTypesFrame(){
		tabbedPane.setSelectedIndex(1);
	}
	
	public void setOntologyLanguage(Language l){
		this.ontologyLanguage = l;
	}
	
	public Language getOntologyLanguage(){
		return this.ontologyLanguage;	
	}
	
	public void setInterfaceLanguage(Language l){
		this.interfaceLanguage = l;
	}
	
	public Language getInterfaceLanguage(){
		return this.interfaceLanguage;	
	}
	
	public Ontology getOntology(){
		return this.ontology;
	}
	
	public void newOntology(){
		if(ontology != null) this.closeOntology();
		String ontologyName = (String) JOptionPane.showInputDialog(this,null,Constants.NEW_ONTOLOGY_LABEL,JOptionPane.QUESTION_MESSAGE,null,null,Constants.MY_ONTOLOGY_LABEL);
		if(ontologyName != null){
			this.ontology = new Ontology(new Terms(ontologyName,this.getOntologyLanguage()),true);
			this.closeOntology.setEnabled(true);
			this.saveOntology.setEnabled(true);
			this.saveAsOntology.setEnabled(true);
			this.operationalizeOntology.setEnabled(true);
			this.testsMenu.setEnabled(true);
			this.setTitle(Constants.TOOCOM_TITLE + " - " + ontologyName);
			this.setPanelsEnabled(true);
			this.osFrame.refresh();
			this.repaint();
		}
	}
	
	public void openOntology(){
		if((ontology == null) || this.closeOntology()){
			JFileChooser fileChooser = new JFileChooser(Constants.APPLICATION_DIRECTORY);
			fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
			fileChooser.setAcceptAllFileFilterUsed(false);
			fileChooser.setFileFilter(new CGXMLFileFilter());
			if(fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
				try{
					this.ontology = CGXMLParser.loadOntology(fileChooser.getSelectedFile().getPath(),this.getOntologyLanguage(),true);
					if(this.ontology != null){
						this.closeOntology.setEnabled(true);
						this.saveOntology.setEnabled(true);
						this.saveAsOntology.setEnabled(true);
						this.operationalizeOntology.setEnabled(true);
						this.testsMenu.setEnabled(true);
						this.setTitle(Constants.TOOCOM_TITLE + " - " + this.ontology.getTerm(this.getOntologyLanguage()));
						this.ontoFile = fileChooser.getSelectedFile().getPath();
						this.osFrame.refresh();
						this.axiomsFrame.clear();
						this.ctFrame.refreshTree();
						this.rtFrame.refreshTree();
					}
					else{
						JOptionPane.showMessageDialog(this,Constants.LOADING_ONTOLOGY_PROBLEM_MESSAGE);
					}
				}
				catch(Exception e){
					JOptionPane.showMessageDialog(this,Constants.FILE_NOT_FOUND_OR_ACCESS_PROBLEM);
				}
			}
		}
	}
	
	public void openOntologyFile(String fileName){
		try{
			this.ontology = CGXMLParser.loadOntology(fileName,this.getOntologyLanguage(),true);
			if(this.ontology != null){
				this.closeOntology.setEnabled(true);
				this.saveOntology.setEnabled(true);
				this.saveAsOntology.setEnabled(true);
				this.operationalizeOntology.setEnabled(true);
				this.testsMenu.setEnabled(true);
				this.setTitle(Constants.TOOCOM_TITLE + " - " + this.ontology.getTerm(this.getOntologyLanguage()));
				this.ontoFile = fileName;
				this.osFrame.refresh();
				this.axiomsFrame.clear();
				this.ctFrame.refreshTree();
				this.rtFrame.refreshTree();
			}
			else{
				JOptionPane.showMessageDialog(this,Constants.LOADING_ONTOLOGY_PROBLEM_MESSAGE);
			}
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(this,Constants.FILE_NOT_FOUND_OR_ACCESS_PROBLEM);
		}
	}
	
	public void operationalizeOntology(){
		if(this.cc == null){
			JOptionPane.showMessageDialog(this,Constants.COGITANT_SERVER_IS_NOT_RUNNING);
		}
		else new OperationalizationForm(this);
	}
	
	public void launchInferenceEngine(){
		new InferenceEngineFrame(this);
	}
	
	public boolean closeOntology(){
		if(JOptionPane.showConfirmDialog(this,Constants.CLOSE_ONTOLOGY_CONFIRM_MESSAGE) == JOptionPane.OK_OPTION){
			this.closeOntology.setEnabled(false);
			this.saveOntology.setEnabled(false);
			this.saveAsOntology.setEnabled(false);
			this.operationalizeOntology.setEnabled(false);
			this.testsMenu.setEnabled(false);
			this.ontology = null;
			this.setTitle(Constants.TOOCOM_TITLE + " - " + Constants.NO_ONTOLOGY_TITLE);
			this.ontoFile = null;
			this.osFrame.clear();
			this.ctFrame.clear();
			this.rtFrame.clear();
			this.axiomsFrame.clear();
			return true;
		}
		else return false;
	}
	
	/** Saves the current ontology into the current language. */
	public void saveAsOntology(){
		if(ontology != null){
			JFileChooser fileChooser = new JFileChooser(Constants.APPLICATION_DIRECTORY);
			if(ontoFile != null) fileChooser.setCurrentDirectory(new File(this.ontoFile));
			fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
			fileChooser.setAcceptAllFileFilterUsed(false);
			fileChooser.setFileFilter(new CGXMLFileFilter());
			if(fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
				File f = fileChooser.getSelectedFile();
				if(!f.getName().endsWith(CGXMLFileFilter.CGXML_FILE_EXTENSION)) f = new File(f.getPath() + CGXMLFileFilter.CGXML_FILE_EXTENSION);
				if(!f.exists() || (JOptionPane.showConfirmDialog(this,f.getName() + "\n" + Constants.OVERWRITE_FILE_MESSAGE) == JOptionPane.OK_OPTION)){
					try{
						if(CGXMLParser.saveOntology(this.ontology,f.getPath(),this.getOntologyLanguage(),true)){
							JOptionPane.showMessageDialog(this,Constants.SAVING_ONTOLOGY_SUCCESSFULL_MESSAGE);
							this.ontoFile = f.getPath();
						}
						else JOptionPane.showMessageDialog(this,Constants.SAVING_ONTOLOGY_PROBLEM_MESSAGE);
					}
					catch(Exception e){
						JOptionPane.showMessageDialog(this,Constants.SAVING_ONTOLOGY_PROBLEM_MESSAGE + "\n\n" + e.getMessage());
					}
				}
			}
		}
	}
	
	public void saveOntology(){
		if((ontology != null) && (ontoFile != null)){
			try{
				if(CGXMLParser.saveOntology(this.ontology,ontoFile,this.getOntologyLanguage(),true))
					JOptionPane.showMessageDialog(this,Constants.SAVING_ONTOLOGY_SUCCESSFULL_MESSAGE);
				else JOptionPane.showMessageDialog(this,Constants.SAVING_ONTOLOGY_PROBLEM_MESSAGE);
			}
			catch(Exception e){
				JOptionPane.showMessageDialog(this,Constants.SAVING_ONTOLOGY_PROBLEM_MESSAGE + "\n\n" + e.getMessage());
			}
		}
		else this.saveAsOntology();
	}
	
	public void exitOntology(){
		if(JOptionPane.showConfirmDialog(this,Constants.EXIT_CONFIRM_MESSAGE) == JOptionPane.OK_OPTION){
			System.exit(0);
		}
	}
	
	public void testCT(){
		if(this.ontology != null) OntoEval.conceptTypeHierarchyVerification(this.ontology,this.getOntologyLanguage()).displayResults(this,CGConstants.CONCEPT_TYPES_HIERARCHY);
	}
	
	public void testRT(){
		if(this.ontology != null) OntoEval.relationTypeHierarchyVerification(this.ontology,this.getOntologyLanguage()).displayResults(this,CGConstants.RELATION_TYPES_HIERARCHY);
	}
	
	public void testInstances(){
		if(this.ontology != null) OntoEval.instancesVerification(this.ontology,this.getOntologyLanguage()).displayResults(this,CGConstants.INSTANCES);
	}
	
	public void testAxioms(){
		if(this.ontology != null){
			TooCoMProgressBar pb = new TooCoMProgressBar(this,Constants.TEST_MESSAGE);
			OntoEval.axiomsVerification(this.ontology,this.getOntologyLanguage(),this.cc).displayResults(this,CGConstants.AXIOMS);
			pb.dispose();
		}
	}
	
	public void testOntology(){
		if(this.ontology != null){
			OntoEval.ontologyVerification(this.ontology,this.getOntologyLanguage(),this.cc).displayResults(this,CGConstants.ONTOLOGY);
		}
	}
	
	public void match(boolean trace){
		if(this.cc == null){
			JOptionPane.showMessageDialog(this,Constants.COGITANT_SERVER_IS_NOT_RUNNING);
		}
		else{
			JFileChooser fileChooser = new JFileChooser(Constants.APPLICATION_DIRECTORY);
			fileChooser.setDialogTitle(Constants.CGXML_SELECT_TITLE);
			fileChooser.setAcceptAllFileFilterUsed(false);
			fileChooser.setFileFilter(new OntologyFileFilter());
			if(fileChooser.showDialog(this,Constants.SELECT_BUTTON_LABEL) == JFileChooser.APPROVE_OPTION){
				File onto1 = fileChooser.getSelectedFile();
				if(onto1.exists()){
					fileChooser = new JFileChooser(Constants.APPLICATION_DIRECTORY);
					fileChooser.setDialogTitle(Constants.CGXML_SELECT_TITLE);
					fileChooser.setAcceptAllFileFilterUsed(false);
					fileChooser.setFileFilter(new OntologyFileFilter());
					if(fileChooser.showDialog(this,Constants.SELECT_BUTTON_LABEL) == JFileChooser.APPROVE_OPTION){
						File onto2 = fileChooser.getSelectedFile();
						try{
							new MatchingResultForm(MatchingAlgorithm.match(onto1,onto2,this.ontologyLanguage,cc,trace),this.getOntologyLanguage(),this);												
						}
						catch(Exception e){
							System.out.println(e.toString());
							JOptionPane.showMessageDialog(this,Constants.MATCHING_ONTO_PROBLEM_MESSAGE + "\n\n" + e.getMessage());
						}
					}
				}
				else JOptionPane.showMessageDialog(this,Constants.FILE_NOT_FOUND_OR_ACCESS_PROBLEM);
			}
		}
	}
	
	public void optionsInterfaceLanguage(){}
	
	public void optionsOntologyLanguage(ActionEvent e){
		JMenuItem item = (JMenuItem) e.getSource();
		this.setOntologyLanguage(Language.getLanguage(item.getText()));
		for(Iterator i = languageItemList.iterator();i.hasNext();){
			JMenuItem it = (JMenuItem) i.next();
			if(it.getText().equals(item.getText())) it.setSelected(true);
			else it.setSelected(false);
		}
		this.repaint();
		this.osFrame.refresh();
	}	
	
	/** Places \n in the string such as the max number of character on the same line is n. */
	private static String chopString(String s, int n){
		if(s.length() <= n) return s;
		else{
			int cpt = -1;
			for(;(n - 1 - cpt) >= 0;cpt ++){
				if(s.substring(n - 1 - cpt,n - cpt).equals(" ")){
					return (s.substring(0,n - 1 - cpt) + "\n" + MainFrame.chopString(s.substring(n - 1 - cpt),n));
				}
			}
			return (s.substring(0,n) + "\n" + MainFrame.chopString(s.substring(n),n));
		}
	}
	
	public void helpHelp(){
		try{
			JDialog helpFrame = new JDialog(this,Constants.TOOCOM_HELP);
			helpFrame.setSize(new Dimension(this.getWidth()*3/4,this.getHeight()*3/4));
			helpFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			JTextArea text = new JTextArea();
			text.setEditable(false);
			text.append(CGConstants.TOOCOM + CGConstants.TOOCOM_VERSION + "\n");
			BufferedReader buf = new BufferedReader(new FileReader(Constants.HELP_FILE_NAME));
			for(String s = buf.readLine();s != null;s = buf.readLine()){
				text.append(MainFrame.chopString(s,Constants.MAX_HELP_LINE_WIDTH) + "\n");
			}
			buf.close();
			JScrollPane scrollPanel = new JScrollPane(text);
			helpFrame.getContentPane().add(scrollPanel);
		    helpFrame.setResizable(true);
		    //helpFrame.pack();
			helpFrame.setVisible(true);
		}
		catch(IOException e){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -