001    package jigcell.compare.ui;
002    
003    import java.awt.Component;
004    import java.awt.Frame;
005    import java.beans.PropertyChangeEvent;
006    import java.beans.PropertyChangeListener;
007    import java.net.URL;
008    import java.util.ArrayList;
009    import java.util.List;
010    import javax.swing.JButton;
011    import javax.swing.JDialog;
012    import javax.swing.JMenu;
013    import javax.swing.JPanel;
014    import jigcell.compare.IConfigEditor;
015    import jigcell.compare.ITab;
016    import jigcell.compare.impl.Compare;
017    import jigcell.compare.impl.Config;
018    
019    /**
020     * A default implementation of a tab providing no services.
021     *
022     * <p>
023     * This code is licensed under the DARPA BioCOMP Open Source License.  See LICENSE for more details.
024     * </p>
025     *
026     * @author Nicholas Allen
027     */
028    
029    public abstract class PanelTab extends JPanel implements ITab, PropertyChangeListener {
030    
031       /**
032        * Path where about files are stored
033        */
034    
035       public final static String PATH_ABOUT = "resource/";
036    
037       /**
038        * Path fragment where about description is stored
039        */
040    
041       public final static String PATH_DESCRIPTION_ABOUT = "-about.html";
042    
043       /**
044        * Parent Comparator
045        */
046    
047       protected Compare compare;
048    
049       /**
050        * Configuration markers
051        */
052    
053       protected List configMarkers;
054    
055       /**
056        * Name of this tab
057        */
058    
059       protected String tabName;
060    
061       /**
062        * A standard builder for about dialogs from an external resource.
063        *
064        * @param clazz Tab class for which the about dialog is being built
065        * @param manager Interface manager for the tab
066        */
067    
068       public static JDialog createAboutDialogForTab (Class clazz, InterfaceBuilder manager) {
069          Component display = manager.getDisplay ();
070          JDialog aboutDialog = new JDialog (display instanceof Frame ? (Frame) display : null, false);
071          URL location = null;
072          String name = clazz.getName ();
073          try {
074             location = clazz.getResource (PATH_ABOUT + name.substring (name.lastIndexOf ('.') + 1) + PATH_DESCRIPTION_ABOUT);
075          } catch (Exception e) {
076             Compare.assertion ("Unable to read about information.", e);
077          }
078          InterfaceBuilder.fitComponentInDialog (aboutDialog, InterfaceBuilder.createHTMLViewer (location, 385), 400, 375);
079          return aboutDialog;
080       }
081    
082       /**
083        * Creates a new view with no additional functionality.
084        *
085        * @param compare Comparator backend to interface with
086        * @param configMarker Marker for retrieving configuration information from Comparator backend
087        */
088    
089       public PanelTab (Compare compare, String configMarker) {
090          this.compare = compare;
091          configMarkers = new ArrayList ();
092          configMarkers.add (configMarker);
093          configMarkers.addAll (Config.createStandardMarkers (this));
094          tabName = compare.getConfig ().findValue (configMarkers, CONFIG_TABNAME, false, false, false);
095          initialize ();
096          compare.addPropertyChangeListener (this);
097          createUI ();
098          if (compare instanceof CompareFrontEnd)
099             ((CompareFrontEnd) compare).addTab (this, false, createMenus (), createTools ());
100          else
101             compare.addTab (this);
102       }
103    
104       /**
105        * {@inheritDoc}
106        */
107    
108       public JDialog createAboutDialog () {
109          return null;
110       }
111    
112       /**
113        * {@inheritDoc}
114        */
115    
116       public IConfigEditor createConfigEditor () {
117          return null;
118       }
119    
120       /**
121        * @see jigcell.compare.ui.IBasicTableHost#getCompare()
122        */
123    
124       public Compare getCompare () {
125          return  compare;
126       }
127    
128       /**
129        * @see jigcell.compare.ui.IBasicTableHost#getConfigMarkers()
130        */
131    
132       public List getConfigMarkers () {
133          return configMarkers;
134       }
135    
136       /**
137        * @see jigcell.compare.ui.IBasicTableHost#getHostIdentifier()
138        */
139    
140       public String getHostIdentifier () {
141          return getClass ().toString () + System.identityHashCode (this);
142       }
143    
144       /**
145        * {@inheritDoc}
146        */
147    
148       public String getName () {
149          return tabName;
150       }
151    
152       public void propertyChange (PropertyChangeEvent e) {
153          if (Compare.PROPERTY_CONFIG_EDIT.equals (e.getPropertyName ()))
154             readConfiguration (STATE_RUNNING);
155       }
156    
157       /**
158        * {@inheritDoc}
159        */
160    
161       public void readConfiguration (String state) {}
162    
163       /**
164        * A list of menus for the view.
165        */
166    
167       protected JMenu [] createMenus () {
168          return null;
169       }
170    
171       /**
172        * Creates a tool list for the view.
173        */
174    
175       protected JButton [] createTools () {
176          return null;
177       }
178    
179       /**
180        * Creates an interface for the view.
181        */
182    
183       protected void createUI () {}
184    
185       /**
186        * The configuration for this view.
187        */
188    
189       protected Config getConfigForView () {
190          return compare.getConfig ().getConfig ((String) configMarkers.get (0));
191       }
192    
193       /**
194        * Performs any initialization work for the view.
195        */
196    
197       protected void initialize () {
198          readConfiguration (STATE_INITIALIZE);
199       }
200    }