001    // This file is part of the Attempto Java Packages.
002    // Copyright 2008-2009, Attempto Group, University of Zurich (see http://attempto.ifi.uzh.ch).
003    //
004    // The Attempto Java Packages is free software: you can redistribute it and/or modify it under the
005    // terms of the GNU Lesser General Public License as published by the Free Software Foundation,
006    // either version 3 of the License, or (at your option) any later version.
007    //
008    // The Attempto Java Packages is distributed in the hope that it will be useful, but WITHOUT ANY
009    // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
010    // PURPOSE. See the GNU Lesser General Public License for more details.
011    //
012    // You should have received a copy of the GNU Lesser General Public License along with the Attempto
013    // Java Packages. If not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.acewiki.gui.page;
016    
017    import ch.uzh.ifi.attempto.acewiki.Wiki;
018    import ch.uzh.ifi.attempto.acewiki.gui.Title;
019    import ch.uzh.ifi.attempto.echocomp.HSpace;
020    import ch.uzh.ifi.attempto.echocomp.Label;
021    import ch.uzh.ifi.attempto.echocomp.SmallButton;
022    import ch.uzh.ifi.attempto.echocomp.Style;
023    import ch.uzh.ifi.attempto.echocomp.VSpace;
024    import nextapp.echo2.app.Border;
025    import nextapp.echo2.app.Button;
026    import nextapp.echo2.app.Color;
027    import nextapp.echo2.app.Column;
028    import nextapp.echo2.app.Component;
029    import nextapp.echo2.app.Extent;
030    import nextapp.echo2.app.Font;
031    import nextapp.echo2.app.Insets;
032    import nextapp.echo2.app.Row;
033    import nextapp.echo2.app.event.ActionListener;
034    
035    /**
036     * This is the superclass of all page classes. It represents a wiki page of AceWiki.
037     * 
038     * @author Tobias Kuhn
039     */
040    public abstract class WikiPage extends Column {
041            
042            private Wiki wiki;
043            private Title title;
044            private Row tabRow;
045            
046            /**
047             * Initializes a new wiki page.
048             * 
049             * @param wiki The wiki instance.
050             * @param title The title component.
051             */
052            public WikiPage(Wiki wiki, Title title) {
053                    this.wiki = wiki;
054                    this.title = title;
055                    
056                    tabRow = new Row();
057                    tabRow.setInsets(new Insets(10, 0, 0, 0));
058                    add(tabRow);
059                    add(new VSpace(20));
060                    add(title);
061                    addHorizontalLine();
062            }
063            
064            /**
065             * Checks whether the page still exists and updates the page content.
066             */
067            public final void update() {
068                    if (isExpired()) {
069                            removeAll();
070                            add(new ErrorPage(wiki, "This article does no longer exist."));
071                    } else {
072                            doUpdate();
073                    }
074            }
075            
076            /**
077             * Updates the page content.
078             */
079            protected void doUpdate() {}
080            
081            /**
082             * Returns the wiki instance this page belongs to.
083             * 
084             * @return The wiki instance.
085             */
086            public Wiki getWiki() {
087                    return wiki;
088            }
089            
090            /**
091             * Returns the title component.
092             * 
093             * @return The title component.
094             */
095            protected Title getTitle() {
096                    return title;
097            }
098            
099            /**
100             * Writes a log entry.
101             * 
102             * @param type The type of the log entry.
103             * @param text The log text.
104             */
105            protected void log(String type, String text) {
106                    wiki.log(type, text);
107            }
108            
109            /**
110             * Checks if the page has expired. A page has expired it represents an ontology
111             * element that has been deleted.
112             * 
113             * @return true if the page has expired.
114             */
115            public boolean isExpired() {
116                    return false;
117            }
118            
119            /**
120             * Adds a new tab to the tab row.
121             * 
122             * @param tabName The name of the tab.
123             * @param actionCommand The action command that is given to the action event object.
124             * @param actionListener The actionlistener.
125             */
126            protected void addTab(String tabName, String actionCommand, ActionListener actionListener) {
127                    SmallButton b = new SmallButton(tabName, actionListener, true);
128                    if (actionCommand != null) {
129                            b.setActionCommand(actionCommand);
130                    }
131                    tabRow.add(b);
132                    tabRow.add(new HSpace(8));
133                    tabRow.add(createTabSeparator());
134                    tabRow.add(new HSpace(8));
135            }
136            
137            /**
138             * Adds a new tab to the tab row.
139             * 
140             * @param tabName The name of the tab.
141             * @param actionListener The actionlistener.
142             */
143            protected void addTab(String tabName, ActionListener actionListener) {
144                    addTab(tabName, null, actionListener);
145            }
146            
147            /**
148             * Adds a new tab to the tab row that is currently selected.
149             * 
150             * @param tabName The name of the tab.
151             */
152            protected void addSelectedTab(String tabName) {
153                    tabRow.add(new SmallButton(tabName, null, false));
154                    tabRow.add(new HSpace(8));
155                    tabRow.add(createTabSeparator());
156                    tabRow.add(new HSpace(8));
157            }
158            
159            private Button createTabSeparator() {
160                    Button tabSeparator = new Button();
161                    tabSeparator.setBorder(new Border(1, Color.DARKGRAY, Border.STYLE_SOLID));
162                    tabSeparator.setHeight(new Extent(12));
163                    return tabSeparator;
164            }
165            
166            /**
167             * Adds a horizontal line to the page content.
168             */
169            protected void addHorizontalLine() {
170                    Column horizontalLine = new Column();
171                    horizontalLine.setInsets(new Insets(10, 0, 10, 0));
172                    Column c = new Column();
173                    c.setBackground(Color.DARKGRAY);
174                    c.setInsets(new Insets(0, 1, 0, 0));
175                    horizontalLine.add(c);
176                    add(horizontalLine);
177            }
178            
179            /**
180             * Adds a headline to the page content.
181             * 
182             * @param text The headline text.
183             */
184            protected void addHeadline(String text) {
185                    addHeadline(text, null);
186            }
187            
188            /**
189             * Adds a headline to the page content. The component is shown after the headline text.
190             * 
191             * @param text The headline text.
192             * @param comp 
193             */
194            protected void addHeadline(String text, Component comp) {
195                    Row headline = new Row();
196                    headline.setInsets(new Insets(10, 10, 0, 0));
197                    headline.setCellSpacing(new Extent(5));
198                    Label title = new Label(text);
199                    title.setFont(new Font(Style.fontTypeface, Font.ITALIC | Font.UNDERLINE, new Extent(13)));
200                    title.setLineWrap(false);
201                    headline.add(title);
202                    if (comp != null) {
203                            headline.add(comp);
204                    }
205                    add(headline);
206            }
207    
208            public abstract boolean equals(Object obj);
209    
210    }