001    // This file is part of AceWiki.
002    // Copyright 2008-2012, AceWiki developers.
003    // 
004    // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU
005    // Lesser General Public License as published by the Free Software Foundation, either version 3 of
006    // the License, or (at your option) any later version.
007    // 
008    // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009    // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010    // Lesser General Public License for more details.
011    // 
012    // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If
013    // not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.acewiki.gui;
016    
017    import nextapp.echo.app.Button;
018    import nextapp.echo.app.Color;
019    import nextapp.echo.app.Extent;
020    import nextapp.echo.app.Font;
021    import nextapp.echo.app.Insets;
022    import nextapp.echo.app.event.ActionEvent;
023    import nextapp.echo.app.event.ActionListener;
024    import ch.uzh.ifi.attempto.acewiki.Wiki;
025    import ch.uzh.ifi.attempto.acewiki.core.DummyOntologyElement;
026    import ch.uzh.ifi.attempto.acewiki.core.LanguageUtils;
027    import ch.uzh.ifi.attempto.acewiki.core.OntologyElement;
028    import ch.uzh.ifi.attempto.acewiki.core.OntologyTextElement;
029    import ch.uzh.ifi.attempto.echocomp.Style;
030    
031    /**
032     * This class represents a wiki-internal link.
033     * 
034     * @author Tobias Kuhn
035     */
036    public class WikiLink extends Button implements ActionListener {
037            
038            private static final long serialVersionUID = -2234400779848457043L;
039            
040            private OntologyElement ontologyElement;
041            private Wiki wiki;
042            
043            /**
044             * Creates a new link to the article of the given ontology element. The headword of the
045             * ontology element is used as the link text.
046             * 
047             * @param ontologyElement The ontology element whose article should be linked.
048             * @param wiki The wiki instance.
049             */
050            public WikiLink(OntologyElement ontologyElement, Wiki wiki) {
051                    super(LanguageUtils.getHeading(ontologyElement));
052                    this.wiki = wiki;
053                    this.ontologyElement = ontologyElement;
054                    initButton(false);
055            }
056            
057            /**
058             * Creates a new link to the article of the given ontology element.
059             * 
060             * @param ontologyElement The ontology element whose article should be linked.
061             * @param text The link text.
062             * @param wiki The wiki instance.
063             * @param red true if the link text should be displayed in red font.
064             */
065            public WikiLink(OntologyElement ontologyElement, String text, Wiki wiki, boolean red) {
066                    super(text);
067                    this.wiki = wiki;
068                    this.ontologyElement = ontologyElement;
069                    initButton(red);
070            }
071            
072            /**
073             * Creates a new wiki link on the basis of a text element. It links to the article of the
074             * ontology element of the text element. The text of the text element is used as the link
075             * text.
076             * 
077             * @param textElement The text element.
078             * @param wiki The wiki instance.
079             * @param red true if the link text should be displayed in red font.
080             */
081            public WikiLink(OntologyTextElement textElement, Wiki wiki, boolean red) {
082                    super(textElement.getText());
083                    this.wiki = wiki;
084                    this.ontologyElement = textElement.getOntologyElement();
085                    initButton(red);
086            }
087            
088            private void initButton(boolean red) {
089                    setInsets(new Insets(0, 0, 0, 0));
090                    setLineWrap(false);
091                    setRolloverEnabled(true);
092                    if (ontologyElement instanceof DummyOntologyElement) {
093                            setFont(new Font(Style.fontTypeface, Font.ITALIC, new Extent(13)));
094                    } else {
095                            setFont(new Font(Style.fontTypeface, Font.PLAIN, new Extent(13)));
096                    }
097                    setRolloverForeground(Color.BLUE);
098                    if (red) setForeground(new Color(180, 0, 0));
099                    addActionListener(this);
100            }
101    
102            public void actionPerformed(ActionEvent e) {
103                    wiki.log("page", "pressed: link " + ontologyElement.getWord());
104                    wiki.showPage(ontologyElement);
105            }
106    
107    }