Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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")
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";
00069 protected final static String VIEW_TAG = "view";
00070 protected final static String PROPERTY_TAG = "property";
00071 protected final static String INDENT4 = " ";
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
00087 Action a = new Action(){
00088 public String getText(){
00089 return MessageBundle.get("GrxBasePlugin.menu.restoreProperties");
00090 }
00091 public void run(){
00092 restoreProperties();
00093 }
00094 };
00095 setMenuItem(a);
00096
00097
00098 Action item = new Action(){
00099 public String getText(){
00100 return MessageBundle.get("GrxBasePlugin.menu.rename");
00101 }
00102 public void run(){
00103 InputDialog dialog = new InputDialog( null, getText(),
00104 MessageBundle.get("GrxBasePlugin.dialog.message.input"), getName(),null);
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");
00128 String val = propEl.getAttribute("value");
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());
00153 element_.setAttribute("name", getName());
00154 element_.setAttribute("select", String.valueOf(isSelected()));
00155 if (url_ != null)
00156 element_.setAttribute("url", GrxXmlUtil.replaceEnvVal(new File(url_)));
00157 element_.appendChild(doc_.createTextNode("\n"));
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);
00174 propEl.setAttribute("value", val);
00175
00176 element_.appendChild(doc_.createTextNode(INDENT4+INDENT4+INDENT4));
00177 element_.appendChild(propEl);
00178 element_.appendChild(doc_.createTextNode("\n"));
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);
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_ ) ) );
00280
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);
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
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
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 return ret;
00455 }
00456
00463 public boolean propertyChanged(String property, String value) {
00464 if (property.equals("name")){
00465 rename(value);
00466 return true;
00467 }
00468 return false;
00469 }
00470
00471
00477 public Object setProperty(String key, String value){
00478
00479 Object o = super.setProperty(key, value);
00480 notifyObservers("PropertyChange", key, value);
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 }