View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.viewer;
21  
22  import javax.swing.Icon;
23  import javax.swing.JComponent;
24  
25  import org.xnap.plugin.AbstractPlugin;
26  
27  /***
28   * Provides the default implementation for a viewer.
29   */
30  public abstract class AbstractViewer extends AbstractPlugin implements Viewer
31  {
32      
33      //--- Data Field(s) ---
34  
35  	public String[] extensions;
36  	public JComponent component;
37  	private Icon icon = null;
38  
39  	//--- Constructor(s) ---
40  
41  	public AbstractViewer(String[] extensions)
42  	{
43  		this(extensions, null);
44  	}
45  
46  	/***
47  	 * Overwrite {@link #getExtensions()} if you use this constructor.
48  	 */
49  	public AbstractViewer()
50  	{
51  	}
52  
53  	public AbstractViewer(String[] extensions, Icon icon)
54  	{
55  		this.extensions = extensions;
56  		this.icon = icon;
57  	}
58  
59      //--- Method(s) ---
60  
61  	/***
62  	 * Does nothing.
63  	 */
64  	public void close()
65  	{
66  	}
67  
68  	public abstract JComponent createComponent();
69  
70  	public JComponent getComponent()
71  	{
72  		if (component == null) {
73  			component = createComponent();
74  		}
75  		return component;
76  	}
77  
78  	/***
79  	 * Returns a an array of extensions that this plugin can display.
80  	 */
81  	public String[] getExtensions()
82  	{
83  		return extensions;
84  	}
85  
86  	public Icon getIcon()
87  	{
88  		return icon;
89  	}
90  
91      /***
92       * Implements the Viewer interface.
93  	 *
94  	 * @return the name of the plugin
95  	 * @see xnap.plugin.PluginInfo#getName()
96       */
97      public String getName()
98      {
99  		return getInfo().getName();
100     }
101 
102     /***
103 	 * Does nothing.
104 	 *
105      * @see xnap.plugin.Plugin#start()
106      */
107     public void start()
108     {
109 		// do nothing, we are a GUI only plugin
110     }
111 
112 
113     /***
114      * Registers the extensions at the {@link ViewerManager}.
115      */
116     public void startGUI()
117     {
118 		ViewerManager.getInstance().register(getExtensions(), this);
119     }
120 
121     /***
122 	 * Does nothing.
123 	 *
124      * @see xnap.plugin.Plugin#stop()
125      */
126     public void stop()
127     {
128 		// do nothing, we are a GUI only plugin
129     }
130 
131     /***
132      * Unregisters the extensions at the {@link ViewerManager}.
133      */
134     public void stopGUI()
135     {
136 		ViewerManager.getInstance().unregister(getExtensions(), this);
137 		component = null;
138     }
139 
140     /***
141      * Returns the value get.Info().getName().
142      */
143     public String toString()
144     {
145 		return getInfo().getName();
146     }
147 
148 }