GrxBasePlugin.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST)
00009  */
00010 /*
00011  *  GrxBasePlugin.java
00012  *
00013  *  Copyright (C) 2007 GeneralRobotix, Inc.
00014  *  All Rights Reserved
00015  *
00016  *  @author Yuichiro Kawasumi (General Robotix, Inc.)
00017  */
00018 package com.generalrobotix.ui;
00019 
00020 import java.io.File;
00021 import java.lang.reflect.Field;
00022 import java.util.ArrayList;
00023 import java.util.Enumeration;
00024 import java.util.ListIterator;
00025 import java.util.Vector;
00026 
00027 import org.eclipse.jface.action.Action;
00028 import org.eclipse.jface.action.MenuManager;
00029 import org.eclipse.jface.dialogs.InputDialog;
00030 import org.eclipse.jface.resource.ImageDescriptor;
00031 import org.eclipse.jface.resource.ImageRegistry;
00032 import org.eclipse.swt.graphics.Image;
00033 import org.eclipse.swt.widgets.Display;
00034 import org.w3c.dom.Document;
00035 import org.w3c.dom.Element;
00036 import org.w3c.dom.Node;
00037 import org.w3c.dom.NodeList;
00038 
00039 import com.generalrobotix.ui.grxui.Activator;
00040 import com.generalrobotix.ui.grxui.PreferenceConstants;
00041 import com.generalrobotix.ui.item.GrxProjectItem;
00042 import com.generalrobotix.ui.util.GrxConfigBundle;
00043 import com.generalrobotix.ui.util.GrxXmlUtil;
00044 import com.generalrobotix.ui.util.MessageBundle;
00045 
00046 @SuppressWarnings("serial") //$NON-NLS-1$
00050 public class GrxBasePlugin extends GrxConfigBundle {
00051         private String name_;
00052         private String oldName_;
00053         protected GrxPluginManager manager_;
00054         private String url_;
00055         private boolean selected_ = true;
00056         private boolean isExclusive_= false;
00057 
00058         private ImageRegistry ireg_;
00059         private String iconName_;
00060 
00061         private Vector<Action> menu_ = new Vector<Action>();
00062         private Vector<MenuManager> subMenu_ = new Vector<MenuManager>();
00063         private String[] menuPath_;
00064 
00065         protected Document doc_;
00066         protected Element element_;
00067 
00068     protected final static String ITEM_TAG = "item"; //$NON-NLS-1$
00069     protected final static String VIEW_TAG = "view"; //$NON-NLS-1$
00070     protected final static String PROPERTY_TAG = "property"; //$NON-NLS-1$
00071     protected final static String INDENT4 = "    "; //$NON-NLS-1$
00072     
00073     protected static final String[] booleanComboItem_ = new String[] {"true", "false" };
00074     
00075     private ArrayList<GrxObserver> observers_ = new ArrayList<GrxObserver>();
00076 
00082         protected GrxBasePlugin(String name, GrxPluginManager manager) {
00083                 manager_ = manager;
00084                 setName(name);
00085                 ireg_ = new ImageRegistry();
00086                 // menu item : restore Properties
00087                 Action a = new Action(){
00088                         public String getText(){
00089                                 return MessageBundle.get("GrxBasePlugin.menu.restoreProperties"); //$NON-NLS-1$
00090                         }
00091                         public void run(){
00092                                 restoreProperties();
00093                         }
00094                 };
00095                 setMenuItem(a);
00096 
00097                 // menu item : rename
00098                 Action item = new Action(){
00099                                 public String getText(){
00100                                         return MessageBundle.get("GrxBasePlugin.menu.rename"); //$NON-NLS-1$
00101                                 }
00102                                 public void run(){
00103                                         InputDialog dialog = new InputDialog( null, getText(),
00104                                                         MessageBundle.get("GrxBasePlugin.dialog.message.input"), getName(),null); //$NON-NLS-1$
00105                                         if ( dialog.open() == InputDialog.OK && dialog.getValue() != null)
00106                                                 rename( dialog.getValue() );
00107                                 }
00108                         };
00109                 setMenuItem(item);
00110         }
00111 
00115         public void restoreProperties() {
00116                 if (element_ == null) {
00117                         return;
00118                 }
00119                 clear();
00120                 if(url_!=null)  
00121                         setProperty("url", url_);
00122                 if(name_!=null)
00123                         setProperty("name", name_);
00124                 NodeList props = element_.getElementsByTagName(PROPERTY_TAG);
00125                 for (int j = 0; j < props.getLength(); j++) {
00126                         Element propEl = (Element) props.item(j);
00127                         String key = propEl.getAttribute("name"); //$NON-NLS-1$
00128                         String val = propEl.getAttribute("value"); //$NON-NLS-1$
00129                         if (!propertyChanged(key, val)){
00130                                 setProperty(key, val);
00131                         }
00132                 }
00133         }
00134 
00139         public Element storeProperties() {
00140                 if (doc_ == null)
00141                         return null;
00142 
00143                 if (element_ != null) {
00144                         Node n = element_.getParentNode();
00145                         if (n != null)
00146                                 n.removeChild(element_);
00147                 }
00148 
00149                 String tag = (this instanceof GrxBaseItem) ? ITEM_TAG:VIEW_TAG;
00150                 element_ = doc_.createElement(tag);
00151 
00152                 element_.setAttribute("class",getClass().getName()); //$NON-NLS-1$
00153                 element_.setAttribute("name", getName()); //$NON-NLS-1$
00154                 element_.setAttribute("select", String.valueOf(isSelected())); //$NON-NLS-1$
00155                 if (url_ != null)
00156                         element_.setAttribute("url", GrxXmlUtil.replaceEnvVal(new File(url_))); //$NON-NLS-1$
00157                 element_.appendChild(doc_.createTextNode("\n")); //$NON-NLS-1$
00158 
00159                 Enumeration<?> keys = propertyNames();
00160                 while (keys.hasMoreElements()) {
00161                         String key = (String)keys.nextElement();
00162                         String val = getProperty(key);
00163                         if (key == null || val == null || key.equals("name") || key.equals("url"))
00164                                 continue;
00165                         
00166                         if(key.equals("setupDirectory"))
00167                                 val = GrxXmlUtil.replaceEnvVal(new File(GrxXmlUtil.expandEnvVal(val)));
00168                         if(key.equals("setupCommand")){
00169                                 String s = Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.BIN_SFX);
00170                                 val = val.replace(s, "$(BIN_SFX)");
00171                         }
00172                         Element propEl = doc_.createElement(GrxProjectItem.PROPERTY_TAG);
00173                         propEl.setAttribute("name",  key); //$NON-NLS-1$
00174                         propEl.setAttribute("value", val); //$NON-NLS-1$
00175 
00176                         element_.appendChild(doc_.createTextNode(INDENT4+INDENT4+INDENT4));
00177                         element_.appendChild(propEl);
00178                         element_.appendChild(doc_.createTextNode("\n")); //$NON-NLS-1$
00179                 }
00180                 element_.appendChild(doc_.createTextNode(INDENT4+INDENT4));
00181 
00182                 return element_;
00183         }
00184 
00189         public void setName(String name) {
00190                 oldName_ = name_;
00191                 name_ = name;
00192                 setProperty("name", name); //$NON-NLS-1$
00193         }
00194 
00199         public final String getName() {
00200                 return name_;
00201         }
00202 
00207         public final String toString() {
00208                 return name_;
00209         }
00210 
00211         public String getOldName(){
00212                 return oldName_;
00213         }
00214         
00219         public void setDocument(Document doc) {
00220                 doc_ = doc;
00221         }
00222 
00227         public void setElement(Element element) {
00228                 element_ = element;
00229                 doc_ = element.getOwnerDocument();
00230         }
00231 
00236         public Element getElement() {
00237                 return element_;
00238         }
00239 
00244         public void setSelected(boolean b) {
00245                 selected_ = b;
00246         }
00247 
00252         public boolean isSelected() {
00253                 return selected_;
00254         }
00255 
00260         public void setExclusive(boolean b) {
00261                 isExclusive_ = b;
00262         }
00263 
00268         public boolean isExclusive() {
00269                 return isExclusive_;
00270         }
00271 
00276         protected void setIcon(String iconName) {
00277                 iconName_ = iconName;
00278                 if( ireg_.get( iconName_ ) == null )
00279                         ireg_.put( iconName_, ImageDescriptor.createFromURL( getClass().getResource( "/resources/images/"+iconName_ ) ) ); //$NON-NLS-1$
00280                 //icon_ = icon;
00281         }
00282 
00287         public Image getIcon() {
00288                 return ireg_.get( iconName_ );
00289         }
00290 
00295         public void setMenuItem( Action a ) {
00296                 menu_.add(a);
00297         }
00298         
00303         public void setSubMenu( MenuManager m ) {
00304                 subMenu_.add(m);
00305         }
00306 
00311         public Vector<Action> getMenu() {
00312                 return menu_;
00313         }
00314 
00319         public Vector<MenuManager> getSubMenu() {
00320                 return subMenu_;
00321         }
00322         
00327         protected void setMenuPath(String[] path) {
00328                 menuPath_ = path;
00329         }
00330 
00336     protected boolean syncExec(Runnable r) {
00337         Display display = Display.getDefault();
00338         if (display != null && !display.isDisposed()) {
00339             display.syncExec(r);
00340             return true;
00341         } else
00342         return false;
00343     }
00344     
00349         public String[] getMenuPath() {
00350                 return menuPath_;
00351         };
00352 
00357         public boolean equals(Object obj) {
00358 
00359                 return this == obj;
00360         }
00361 
00367         public String getURL(boolean expand) {
00368                 if (expand)
00369                         return GrxXmlUtil.expandEnvVal(url_);
00370                 else
00371                         return url_;
00372         }
00373 
00378         public void setURL(String url) {
00379                 url = url.replace('\\','/');
00380                 setProperty("url", url); //$NON-NLS-1$
00381                 url_ =url;
00382         }
00383 
00388         public void rename(String newName) {
00389                 manager_.renamePlugin(this, newName);
00390         };
00391 
00399     public static Object getField(Class<? extends GrxBasePlugin> cls, String field, Object defaultValue) {
00400                 try {
00401                         Field f = cls.getField(field);
00402                         return (Object)f.get(new Object());
00403                 } catch (Exception e) {
00404                         //GrxDebugUtil.println(cls.getName() + ": " + field + " not defined");
00405                 }
00406                 return defaultValue;
00407     }
00408 
00412         public void shutdown() {
00413 
00414         }
00415         
00419         public void unregisterCORBA() {
00420 
00421         }
00422 
00426     public boolean registerCORBA() {
00427         return true;
00428     }
00429         
00434         public GrxBasePlugin clone(){
00435                 GrxBasePlugin ret = (GrxBasePlugin) super.clone();
00436                 
00437                 
00438         ret.setName(name_);
00439         ret.setURL(url_);
00440         
00441 /*      
00442  *              Deep copy suspension list
00443 
00444         private ImageRegistry ireg_;
00445         private String iconName_;
00446 
00447         private Vector<Action> menu_ = new Vector<Action>();
00448         private String[] menuPath_;
00449 
00450         private Document doc_;
00451         protected Element element_;
00452 */
00453         
00454         return ret;
00455         }
00456 
00463         public boolean propertyChanged(String property, String value) {
00464                 if (property.equals("name")){ //$NON-NLS-1$
00465                         rename(value);
00466                         return true;
00467                 }
00468                 return false;
00469         }
00470         
00471 
00477         public Object setProperty(String key, String value){
00478                 //System.out.println("GrxBasePlugin.setProperty("+key+","+value+")");
00479                 Object o = super.setProperty(key, value);
00480                 notifyObservers("PropertyChange", key, value); //$NON-NLS-1$
00481                 return o;
00482         }
00483         
00488         public void setFocused(boolean b){
00489         }
00490         
00491         public void addObserver(GrxObserver v){
00492                 observers_.add(v);
00493         }
00494         
00495         public void deleteObserver(GrxObserver v){
00496                 observers_.remove(v);
00497         }
00498 
00499         public ArrayList<GrxObserver> getObserver(){
00500                 return observers_;
00501         }
00502         
00503         public void notifyObservers(Object... arg) { 
00504         ListIterator<GrxObserver> it = observers_.listIterator();
00505         while (it.hasNext()) {
00506             GrxObserver observer = it.next();
00507             observer.update(this, arg);
00508         }
00509     }
00510 
00515     public ValueEditType GetValueEditType(String key) {
00516         return new ValueEditText();
00517     }
00518         
00519     public class ValueEditType{
00520         private ValueEditType(){}
00521     }
00522     public class ValueEditText extends ValueEditType{
00523         public ValueEditText(){}
00524     }
00525     public class ValueEditCombo extends ValueEditType{
00526         private String[] items_;
00527         public ValueEditCombo(String[] items){
00528             items_ = items;
00529         }
00530         public String[] GetItems(){ return items_; }
00531     }
00532 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:53