Clover coverage report -
Coverage timestamp: Sun Apr 18 2004 21:32:30 EDT
file stats: LOC: 403   Methods: 21
NCLOC: 150   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractPlugin.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  * SimplyHTML, a word processor based on Java, HTML and CSS
 3   
  * Copyright (C) 2002 Ulrich Hilger
 4   
  *
 5   
  * This program is free software; you can redistribute it and/or
 6   
  * modify it under the terms of the GNU General Public License
 7   
  * as published by the Free Software Foundation; either version 2
 8   
  * of the License, or (at your option) any later version.
 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   
 import javax.swing.Action;
 21   
 import javax.swing.JMenu;
 22   
 import javax.swing.JMenuItem;
 23   
 import javax.swing.JComponent;
 24   
 //import com.lightdev.app.shtm.Util;
 25   
 import java.util.ResourceBundle;
 26   
 import java.util.MissingResourceException;
 27   
 import java.util.Locale;
 28   
 //import com.lightdev.app.shtm.FrmMain;
 29   
 import javax.swing.JFrame;
 30   
 import java.util.prefs.*;
 31   
 //import javax.help.*;
 32   
 //import javax.help.event.*;
 33   
 
 34   
 /**
 35   
  * Base class for plug-ins of application SimplyHTML.
 36   
  *
 37   
  * <p>Defines some common methods for reuse in plug-ins. All
 38   
  * settings such as dockLocation or activation state
 39   
  * of a plug-in are stored persistently in a preferences file
 40   
  * with the help of class <code>Prefs</code>. The preferences
 41   
  * file is valid for the current user, so each user has own
 42   
  * plug-in settings.</p>
 43   
  *
 44   
  * <p>Menus are constructed with the help of class
 45   
  * <code>DynamicResource</code>. This class needs menu definitions
 46   
  * accessible in a .properties file as described in the API docs
 47   
  * of <code>DynamicResource</code>. I.e., methods of class
 48   
  * <code>AbstractPlugin</code> only work as defined herein when
 49   
  * accompanied by such .properties file accordingly.</p>
 50   
  *
 51   
  * @author Ulrich Hilger
 52   
  * @author Light Development
 53   
  * @author <a href="http://www.lightdev.com">http://www.lightdev.com</a>
 54   
  * @author <a href="mailto:info@lightdev.com">info@lightdev.com</a>
 55   
  * @author published under the terms and conditions of the
 56   
  *      GNU General Public License,
 57   
  *      for details see file gpl.txt in the distribution
 58   
  *      package of this software
 59   
  *
 60   
  * @version stage 11, April 27, 2003
 61   
  *
 62   
  * @see com.lightdev.app.shtm.DynamicResource
 63   
  */
 64   
 
 65   
 public abstract class AbstractPlugin implements SHTMLPlugin {
 66   
 
 67   
   /**
 68   
    * construct an AbstractPlugin
 69   
    *
 70   
    * <p>Constructor may not have parameters so that
 71   
    * java.lang.Class.newInstance can be used on it.</p>
 72   
    */
 73  0
   public AbstractPlugin() {
 74   
     //System.out.println("AbstractPlugin constructor");
 75   
     /*SecurityManager security = System.getSecurityManager();
 76   
     if (security != null) {
 77   
       security.
 78   
     }
 79   
     */
 80  0
     prefs = Preferences.userNodeForPackage( getClass() );
 81   
   }
 82   
 
 83   
   /**
 84   
    * init the plug-in
 85   
    *
 86   
    * this is called by the PluginManager directly after instantiating the plug-in
 87   
    */
 88  0
   public void initPlugin(FrmMain owner) {  }
 89   
 
 90   
   /**
 91   
    * create the plug-in menu
 92   
    */
 93  0
   protected void createPluginMenu() {
 94  0
     if(pluginMenuId != null) {
 95  0
       pMenu = FrmMain.dynRes.createMenu(this.resources, pluginMenuId);
 96   
     }
 97   
   }
 98   
 
 99   
   /**
 100   
    * create the help menu
 101   
    */
 102  0
   protected void createHelpMenu() {
 103  0
     if(helpMenuId != null) {
 104  0
       hMenu = FrmMain.dynRes.createMenu(this.resources, helpMenuId);
 105  0
       initHelpMenu();
 106   
     }
 107   
   }
 108   
 
 109  0
   public void initHelpMenu() {
 110   
   }
 111   
 
 112   
   /**
 113   
    * create a frame for the component of this plug-in, if it
 114   
    * has a JComponent to display.
 115   
    */
 116  0
   protected void createFrame() {
 117  0
     if(c != null) {
 118  0
       frame = new JFrame(getGUIName());
 119  0
       frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
 120  0
       frame.setSize(300, 400);
 121  0
       frame.getContentPane().add(c);
 122   
     }
 123  0
     frame.pack();
 124   
   }
 125   
 
 126   
   /**
 127   
    * create, show or hide frame as needed, depending
 128   
    * on a given dock location
 129   
    *
 130   
    * @param location  the dock location of the plug-in, one of
 131   
    * DOCK_LOCATION_TOP, DOCK_LOCATION_BOTTOM,
 132   
    * DOCK_LOCATION.LEFT, DOCK_LOCATION_RIGHT or DOCK_LOCATION_NONE, if the
 133   
    * component shall not dock.
 134   
    */
 135  0
   protected void createFrameAsNeeded(int location) {
 136  0
     if(location == SHTMLPlugin.DOCK_LOCATION_NONE) {
 137  0
       if((frame == null) && (c != null)) {
 138  0
         createFrame();
 139   
       }
 140  0
       if(frame != null) {
 141  0
         frame.show();
 142   
       }
 143   
     }
 144   
     else {
 145  0
       if(frame != null) {
 146  0
         frame.hide();
 147   
       }
 148   
     }
 149   
   }
 150   
 
 151   
   /* ----------- SimplyHTML plugin interface implementation start --------- */
 152   
 
 153   
   /**
 154   
    * initialize the plugin
 155   
    *
 156   
    * @param owner  the owner of this plug-in
 157   
    * @param internalName  the internal name this plug-in shall have
 158   
    * @param pluginMenuId  the id of the plug-in menu in the ResourceBundle,
 159   
    * or null if no plugin-in menu is to be created
 160   
    * @param helpMenuId  the id of the help menu for this plug-in in the
 161   
    * ResourceBundle, or null if no help menu is to be created
 162   
    */
 163  0
   public void initPlugin(FrmMain owner, String internalName, String pluginMenuId,
 164   
                         String helpMenuId) {
 165  0
     this.internalName = internalName;
 166  0
     this.pluginMenuId = pluginMenuId;
 167  0
     this.helpMenuId = helpMenuId;
 168  0
     try {
 169   
       //System.out.println("AbstractPlugin this.getClass.getName=" + this.getClass().getName());
 170  0
       if(FrmMain.pluginManager != null) {
 171  0
         ClassLoader plLoader = FrmMain.pluginManager.getPluginLoader();
 172  0
         if(plLoader != null) {
 173  0
           resources = ResourceBundle.getBundle(
 174   
               this.getClass().getName(), Locale.getDefault(), plLoader);
 175   
           //System.out.println("AbstractPlugin plLoader != null, resources=" + resources);
 176   
         }
 177   
         else {
 178  0
           resources = ResourceBundle.getBundle(
 179   
               this.getClass().getName(), Locale.getDefault());
 180   
           //System.out.println("AbstractPlugin plLoader == null, resources=" + resources);
 181   
         }
 182   
       }
 183   
       else {
 184  0
         resources = ResourceBundle.getBundle(
 185   
             this.getClass().getName(), Locale.getDefault());
 186   
         //System.out.println("AbstractPlugin pluginManager = null, resources=" + resources);
 187   
       }
 188  0
       this.active = prefs.getBoolean(internalName + PREFSID_PLUGIN_ACTIVE, true);
 189  0
       dockLocation = prefs.getInt(internalName + PREFSID_PLUGIN_DOCK_LOCATION,
 190   
                                   SHTMLPlugin.DOCK_LOCATION_LEFT);
 191  0
       createFrameAsNeeded(dockLocation);
 192   
     }
 193   
     catch(MissingResourceException mre) {
 194  0
       Util.errMsg(null, this.getClass().getName() +
 195   
                   ".properties not found", mre);
 196   
     }
 197   
 
 198   
   }
 199   
 
 200   
 
 201   
   /**
 202   
    * set the owner of this plug-in
 203   
    *
 204   
    * @param owner  the main frame of the instance of SimplyHTML creating the plug-in
 205   
    */
 206  0
   public void setOwner(FrmMain owner) {
 207  0
     this.owner = owner;
 208   
   }
 209   
 
 210   
   /**
 211   
    * get the owner of this plug-in
 212   
    *
 213   
    * @return the owner of this plug-in
 214   
    */
 215  0
   public FrmMain getOwner() {
 216  0
     return owner;
 217   
   }
 218   
 
 219   
   /**
 220   
    * set status of plug-in and persistently store setting in
 221   
    * a preferences file
 222   
    *
 223   
    * @param isActive  indicates whether or not the plug-in shall be activated
 224   
    */
 225  0
   public void setStatus(boolean isActive) {
 226  0
     this.active = isActive;
 227  0
     prefs.putBoolean(getInternalName() + PREFSID_PLUGIN_ACTIVE, isActive);
 228  0
     try {
 229  0
       prefs.flush();
 230   
     }
 231   
     catch(Exception e) {
 232  0
       Util.errMsg(null, e.getMessage(), e);
 233   
     }
 234   
   }
 235   
 
 236   
   /**
 237   
    * set the location the component returned by getDockComponent()
 238   
    * shall be docked at. Persistently store setting in
 239   
    * a preferences file.
 240   
    *
 241   
    * @param location the dock location, one of DOCK_LOCATION_TOP, DOCK_LOCATION_BOTTOM,
 242   
    * DOCK_LOCATION.LEFT, DOCK_LOCATION_RIGHT or DOCK_LOCATION_NONE, if the
 243   
    * component shall not dock.
 244   
    */
 245  0
   public void setDockLocation(int location) {
 246  0
     dockLocation = location;
 247  0
     prefs.putInt(getInternalName() + PREFSID_PLUGIN_DOCK_LOCATION, location);
 248  0
     try {
 249  0
       prefs.flush();
 250   
     }
 251   
     catch(Exception e) {
 252  0
       Util.errMsg(null, e.getMessage(), e);
 253   
     }
 254  0
     createFrameAsNeeded(location);
 255   
   }
 256   
 
 257   
   /**
 258   
    * get a menu of actions this plug-in provides.
 259   
    *
 260   
    * <p><code>JMenu</code> is a decendant of <code>JMenuItem</code>
 261   
    * so this method may return a single menu item up to a whole
 262   
    * structure of submenus in its return value.</p>
 263   
    *
 264   
    * @return the plug-in menu
 265   
    */
 266  0
   public JMenuItem getPluginMenu() {
 267  0
     return pMenu;
 268   
   }
 269   
 
 270   
   /**
 271   
    * get a menu item providing documentation about this
 272   
    * plug-in.
 273   
    *
 274   
    * <p><code>JMenu</code> is a decendant of <code>JMenuItem</code>
 275   
    * so this method may return a single menu item up to a whole
 276   
    * structure of submenus in its return value.</p>
 277   
    *
 278   
    * @return a menu item with help for this plug-in
 279   
    */
 280  0
   public JMenuItem getHelpMenu() {
 281  0
     return hMenu;
 282   
   }
 283   
 
 284   
   /**
 285   
    * get the name of the plug-in as it shall appear
 286   
    * on a GUI.
 287   
    *
 288   
    * @return the name of the plug-in
 289   
    */
 290  0
   public String getGUIName() {
 291  0
     return "AbstractPlugin";
 292   
   }
 293   
 
 294   
   /**
 295   
    * get the name used internally for this plug-in
 296   
    *
 297   
    * @return the internal name of this plug-in
 298   
    */
 299  0
   public String getInternalName() {
 300  0
     return internalName;
 301   
   }
 302   
 
 303   
   /**
 304   
    * get the location the component returned by getDockComponent()
 305   
    * shall be docked at.
 306   
    *
 307   
    * @return the dock location, one of DOCK_LOCATION_TOP, DOCK_LOCATION_BOTTOM,
 308   
    * DOCK_LOCATION.LEFT, DOCK_LOCATION_RIGHT or DOCK_LOCATION_NONE, if the
 309   
    * component shall not dock.
 310   
    */
 311  0
   public int getDockLocation() {
 312  0
     return dockLocation;
 313   
   }
 314   
 
 315   
   /**
 316   
    * get the component that this plug-in produces, if any
 317   
    *
 318   
    * @return the component produced by this plug-in, or null if none
 319   
    * is produced
 320   
    */
 321  0
   public JComponent getComponent() {
 322  0
     return c;
 323   
   }
 324   
 
 325   
   /**
 326   
    * get the status of the plug-in
 327   
    *
 328   
    * @return true, if activated, false if not
 329   
    */
 330  0
   public boolean isActive() {
 331  0
     return this.active;
 332   
   }
 333   
 
 334   
   /**
 335   
    * get a string from the resource bundle of the owner of this plug-in
 336   
    *
 337   
    * @param nm  the name of the string resource to get
 338   
    *
 339   
    * @return the string with the given name or null, if none is found
 340   
    */
 341  0
   public String getOwnerResString(String nm) {
 342  0
     return owner.getDynRes().getResourceString(owner.getResources(), nm);
 343   
   }
 344   
 
 345   
   /**
 346   
    * get an action from the resource bundle of the owner of this plug-in
 347   
    *
 348   
    * @param cmd  the name of the action to get
 349   
    *
 350   
    * @return the action with the given name or null, if none is found
 351   
    */
 352  0
   public Action getOwnerAction(String cmd) {
 353  0
     return owner.getDynRes().getAction(cmd);
 354   
   }
 355   
 
 356   
   /* ----------- SimplyHTML plugin interface implementation end --------- */
 357   
 
 358   
   /* --------------- class fields start --------------------- */
 359   
 
 360   
   /** ResourceBundle of plug-in */
 361   
   public static ResourceBundle resources;
 362   
 
 363   
   /** constant for active setting in preferences file */
 364   
   public static final String PREFSID_PLUGIN_ACTIVE = "Active";
 365   
 
 366   
   /** constant for dock location setting in preferences file */
 367   
   public static final String PREFSID_PLUGIN_DOCK_LOCATION = "DockLocation";
 368   
 
 369   
   /** the internal name of this plug-in */
 370   
   protected String internalName;
 371   
 
 372   
   /** the id in the ResourceFile for the menu of this plug-in */
 373   
   protected String pluginMenuId;
 374   
 
 375   
   /** the id in the ResourceFile for the help menu of this plug-in */
 376   
   protected String helpMenuId;
 377   
 
 378   
   /** the plug-in menu provided by this plug-in */
 379   
   protected JMenuItem pMenu = null;
 380   
 
 381   
   /** the help menu provided by this plug-in */
 382   
   protected JMenuItem hMenu = null;
 383   
 
 384   
   /** status of plug-in */
 385   
   protected boolean active = true;
 386   
 
 387   
   /** current dock location */
 388   
   protected int dockLocation = SHTMLPlugin.DOCK_LOCATION_LEFT;
 389   
 
 390   
   /** component of this plug-in */
 391   
   protected JComponent c = null;
 392   
 
 393   
   /** JFrame for dockLocation=none */
 394   
   protected JFrame frame = null;
 395   
 
 396   
   /** reference for user preferences for this class */
 397   
   protected Preferences prefs;
 398   
 
 399   
   /** the owner of this plug in */
 400   
   protected static FrmMain owner;
 401   
 
 402   
   /* ------------- class fields end ------------------ */
 403   
 }