00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 package com.generalrobotix.ui;
00020
00021 import java.awt.Toolkit;
00022 import java.awt.datatransfer.Clipboard;
00023 import java.awt.datatransfer.DataFlavor;
00024 import java.awt.datatransfer.Transferable;
00025 import java.io.*;
00026 import java.lang.reflect.Method;
00027 import java.net.URL;
00028 import java.util.*;
00029
00030 import org.eclipse.core.resources.ResourcesPlugin;
00031 import org.eclipse.jface.action.Action;
00032 import org.eclipse.jface.dialogs.MessageDialog;
00033 import org.eclipse.jface.preference.IPreferenceStore;
00034 import org.eclipse.jface.util.IPropertyChangeListener;
00035 import org.eclipse.jface.util.PropertyChangeEvent;
00036 import org.omg.PortableServer.POA;
00037
00038 import org.w3c.dom.Element;
00039
00040 import com.generalrobotix.ui.grxui.Activator;
00041 import com.generalrobotix.ui.grxui.GrxUIPerspectiveFactory;
00042 import com.generalrobotix.ui.grxui.PreferenceConstants;
00043
00044 import com.generalrobotix.ui.util.GrxCorbaUtil;
00045 import com.generalrobotix.ui.util.GrxDebugUtil;
00046 import com.generalrobotix.ui.util.GrxPluginLoader;
00047 import com.generalrobotix.ui.util.GrxProcessManager;
00048 import com.generalrobotix.ui.util.GrxXmlUtil;
00049 import com.generalrobotix.ui.util.MessageBundle;
00050 import com.generalrobotix.ui.util.OrderedHashMap;
00051 import com.generalrobotix.ui.util.SynchronizedAccessor;
00052 import com.generalrobotix.ui.util.FileUtil;
00053 import com.generalrobotix.ui.item.GrxModeInfoItem;
00054 import com.generalrobotix.ui.item.GrxModelItem;
00055 import com.generalrobotix.ui.item.GrxProjectItem;
00056 import com.generalrobotix.ui.item.GrxSimulationItem;
00057 import com.generalrobotix.ui.util.GrxServerManager;
00058
00059 import org.eclipse.osgi.util.NLS;
00060 import org.eclipse.swt.SWT;
00061 import org.eclipse.swt.widgets.Display;
00062 import org.eclipse.swt.widgets.FileDialog;
00063 import org.eclipse.ui.IPerspectiveDescriptor;
00064 import org.eclipse.ui.IViewPart;
00065 import org.eclipse.ui.IViewReference;
00066 import org.eclipse.ui.IWorkbench;
00067 import org.eclipse.ui.IWorkbenchPage;
00068 import org.eclipse.ui.IWorkbenchWindow;
00069 import org.eclipse.ui.PlatformUI;
00070
00079 public class GrxPluginManager implements IPropertyChangeListener {
00080
00081 private GrxProjectItem currentProject_;
00082 private GrxModeInfoItem currentMode_;
00083 private GrxBaseItem focusedItem_ = null;
00084
00085
00086 public GrxPluginLoader pluginLoader_;
00087 public HashMap<Class<? extends GrxBasePlugin>, OrderedHashMap> pluginMap_ = new HashMap<Class<? extends GrxBasePlugin>, OrderedHashMap>();
00088 private List<GrxBaseView> viewList_ = new ArrayList<GrxBaseView>();
00089 private List<GrxBaseView> activeViewList_ = new ArrayList<GrxBaseView>();
00090 private File homePath_;
00091 private Map<Class<? extends GrxBasePlugin>, PluginInfo> pinfoMap_ = new HashMap<Class<? extends GrxBasePlugin>, PluginInfo>();
00092
00093 private Map<Class<? extends GrxBaseItem>, List<GrxItemChangeListener>> itemChangeListener_ =
00094 new HashMap<Class<? extends GrxBaseItem>, List<GrxItemChangeListener>>();
00095 public static final int ADD_ITEM=0;
00096 public static final int REMOVE_ITEM=1;
00097 public static final int SELECTED_ITEM=2;
00098 public static final int NOTSELECTED_ITEM=3;
00099 public static final int SETNAME_ITEM=4;
00100 public static final int FOCUSED_ITEM=5;
00101 public static final int NOTFOCUSED_ITEM=6;
00102 public static final int CHANGE_MODE=7;
00103
00104 private boolean acceptItemChange_ = true;
00105
00106
00107 public POA poa_;
00108 public org.omg.CORBA.ORB orb_;
00109
00123 public GrxPluginManager() {
00124 GrxDebugUtil.println("[PM] GrxPluginManager created");
00125 String dir = Activator.getDefault().getPreferenceStore().getString("PROJECT_DIR");
00126 if(dir.equals(""))
00127 dir = System.getenv("PROJECT_DIR");
00128 if( dir != null ){
00129 homePath_ = new File(dir);
00130 }else
00131 homePath_ = Activator.getDefault().getHomeDir();
00132 System.out.println("[PM] WORKSPACE PATH=" + ResourcesPlugin.getWorkspace().getRoot().getLocation());
00133
00134
00135
00136
00137
00138 pluginLoader_ = new GrxPluginLoader("", GrxPluginManager.class.getClassLoader());
00139 registerPlugin(GrxModeInfoItem.class);
00140
00141 File rtcFile = new File( Activator.getDefault().getTempDir(),"rtc.conf");
00142 if (!rtcFile.exists()){
00143 try{
00144 FileUtil.resourceToFile(getClass(), "/default_rtc.conf", rtcFile);
00145 }catch (IOException ex){
00146 ex.printStackTrace();
00147 }
00148 }
00149
00150 versionCheck();
00151 setInitialMode();
00152 currentProject_ = new GrxProjectItem("newproject", this);
00153 String defaultProject = System.getProperty("PROJECT", null);
00154 if (defaultProject == null || !currentProject_.load(new File(GrxXmlUtil.expandEnvVal(defaultProject))))
00155 currentProject_.create();
00156
00157
00158 GrxServerManager serverManager = (GrxServerManager)createItem(GrxServerManager.class, "serverManager");
00159 if (serverManager != null){
00160 serverManager.initialize();
00161 itemChange(serverManager, GrxPluginManager.ADD_ITEM);
00162 }
00163 GrxProcessManager processManager = (GrxProcessManager)createItem(GrxProcessManager.class, "processManager");
00164 if (processManager != null){
00165 processManager.setProcessList(serverManager);
00166 itemChange(processManager, GrxPluginManager.ADD_ITEM);
00167 }
00168 }
00169
00175 public void focusedItem(GrxBaseItem item) {
00176 if (focusedItem_ != item) {
00177 if (focusedItem_ != null){
00178 focusedItem_.setFocused(false);
00179 itemChange(focusedItem_, NOTFOCUSED_ITEM);
00180 }
00181 if(item != null){
00182 focusedItem_ = item;
00183 focusedItem_.setFocused(true);
00184 itemChange(focusedItem_, FOCUSED_ITEM);
00185 }else
00186 focusedItem_ = null;
00187 }
00188 }
00189
00194 public GrxBaseItem focusedItem() {
00195 return focusedItem_;
00196 }
00197
00202 private boolean isPerspectiveVisible() {
00203 IWorkbench workbench = PlatformUI.getWorkbench();
00204 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
00205 if (window == null)
00206 return false;
00207 IWorkbenchPage page = window.getActivePage();
00208 if (page == null)
00209 return false;
00210 IPerspectiveDescriptor pers = page.getPerspective();
00211 if (!pers.getId().contains(GrxUIPerspectiveFactory.ID))
00212 return false;
00213 return true;
00214 }
00215
00220 private void updateActiveViewList() {
00221 activeViewList_.clear();
00222
00223 Display display = Display.getDefault();
00224 display.syncExec(new Runnable(){
00225 public void run(){
00226 IWorkbench workbench = PlatformUI.getWorkbench();
00227 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
00228 if (window == null)
00229 return;
00230 IWorkbenchPage page = window.getActivePage();
00231 if (page == null) {
00232 return;
00233 }
00234 IPerspectiveDescriptor pers = page.getPerspective();
00235
00236 if (!pers.getId().contains(GrxUIPerspectiveFactory.ID)) {
00237 return;
00238 }
00239
00240 for (IViewReference i : page.getViewReferences()) {
00241
00242 IViewPart v = i.getView(true);
00243 if (v != null && GrxBaseViewPart.class.isAssignableFrom(v.getClass())) {
00244 GrxBaseView view = ((GrxBaseViewPart) v).getGrxBaseView();
00245 if(view != null)
00246 activeViewList_.add(view);
00247 }
00248 }
00249 }
00250 });
00251
00252 }
00253
00254 private void updateViewList() {
00255 viewList_.clear();
00256
00257 Display display = Display.getDefault();
00258 display.syncExec(new Runnable(){
00259 public void run(){
00260 IWorkbench workbench = PlatformUI.getWorkbench();
00261 IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
00262 if (windows.length == 0)
00263 return;
00264 for(IWorkbenchWindow window : windows){
00265 IWorkbenchPage page = window.getActivePage();
00266 if (page != null) {
00267 IPerspectiveDescriptor pers = page.getPerspective();
00268 if (pers.getId().contains(GrxUIPerspectiveFactory.ID)) {
00269 for (IViewReference i : page.getViewReferences()) {
00270
00271 IViewPart v = i.getView(true);
00272 if (v != null && GrxBaseViewPart.class.isAssignableFrom(v.getClass())) {
00273 GrxBaseView view = ((GrxBaseViewPart) v).getGrxBaseView();
00274 if(view != null)
00275 viewList_.add(view);
00276 }
00277 }
00278 }
00279 }
00280 }
00281 }
00282 });
00283 }
00284
00292 public void start() {
00293
00294 System.out.println("[PM] START GrxPluginManager");
00295 new Thread() {
00296 public void run() {
00297 try {
00298 poa_ = GrxCorbaUtil.getRootPOA();
00299 poa_.the_POAManager().activate();
00300 GrxDebugUtil.println("Corba Server Ready.");
00301 orb_ = GrxCorbaUtil.getORB();
00302 orb_.run();
00303 orb_.destroy();
00304 } catch (Exception e) {
00305 e.printStackTrace();
00306 }
00307 GrxCorbaUtil.clearOrb();
00308 }
00309 }.start();
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 }
00342
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00367 public GrxModeInfoItem getMode() {
00368 return currentMode_;
00369 }
00370
00371 public void setCurrentMode(GrxModeInfoItem mode){
00372 currentMode_ = mode;
00373 }
00374
00375 @SuppressWarnings("unchecked")
00376 public Class<? extends GrxBasePlugin> registerPlugin(String className) {
00377 Class<?> cls = pluginLoader_.loadClass(className);
00378 return registerPlugin((Class<? extends GrxBasePlugin>) cls);
00379 }
00380
00387 @SuppressWarnings("unchecked")
00388 public Class<?> registerPlugin(Element el) {
00389 Class<?> cls = pluginLoader_.loadClass(el.getAttribute("class"));
00390 Class<? extends GrxBasePlugin> ret = registerPlugin((Class<? extends GrxBasePlugin>) cls);
00391 if (ret != null) {
00392 PluginInfo pi = pinfoMap_.get(ret);
00393 pi.visible = GrxXmlUtil.getBoolean(el, PluginInfo.VISIBLE, true);
00394 pinfoMap_.put(ret, pi);
00395 }
00396 return ret;
00397 }
00398
00404 public Class<? extends GrxBasePlugin> registerPlugin(Class<? extends GrxBasePlugin> cls) {
00405 if (cls != null && GrxBasePlugin.class.isAssignableFrom(cls)) {
00406 if (pluginMap_.get(cls) == null) {
00407 GrxDebugUtil.println("[PM] register " + cls.getName());
00408
00409 pluginMap_.put(cls, new OrderedHashMap());
00410
00411 PluginInfo pi = new PluginInfo();
00412 pi.title = (String) GrxBasePlugin.getField(cls, "TITLE", cls.getSimpleName());
00413 pi.lastDir = new File(homePath_ + (String) GrxBasePlugin.getField(cls, "DEFAULT_DIR", ""));
00414 String ext = (String) GrxBasePlugin.getField(cls, "FILE_EXTENSION", null);
00415 if (ext != null)
00416 pi.filter = "*." + ext;
00417 pinfoMap_.put(cls, pi);
00418 }
00419
00420 return cls;
00421 }
00422 return null;
00423 }
00424
00433 public GrxBaseItem createItem(Class<? extends GrxBaseItem> cls, String name) {
00434
00435 if (name == null) {
00436 String baseName = "new" + getItemTitle(cls);
00437 baseName = baseName.toLowerCase().replaceAll(" ", "");
00438 for (int i = 0;; i++) {
00439 name = baseName + i;
00440 if (getItem(cls, name) == null)
00441 break;
00442 }
00443 }
00444 GrxBaseItem item = (GrxBaseItem) createPlugin(cls, name);
00445
00446 if (item != null) {
00447 item.create();
00448 }
00449 return item;
00450 }
00451
00459 public GrxBaseItem loadItem(Class<? extends GrxBaseItem> cls, String name, String url) {
00460 String _url = GrxXmlUtil.expandEnvVal(url);
00461 if (_url == null)
00462 return null;
00463
00464 File f = null;
00465 try {
00466 URL u = new URL(_url);
00467 f = new File(u.getFile());
00468 } catch (Exception e) {
00469
00470 f = new File(_url);
00471 }
00472 try {
00473 _url = f.getCanonicalPath();
00474 f = new File(_url);
00475 } catch (IOException e) {
00476 e.printStackTrace();
00477 }
00478
00479 if (!f.isFile()){
00480 FileDialog fdlg = new FileDialog(GrxUIPerspectiveFactory.getCurrentShell(), SWT.OPEN);
00481 PluginInfo pi = pinfoMap_.get(cls);
00482 String[] fe = { pi.filter };
00483 fdlg.setText(_url+" "+MessageBundle.get("GrxPluginManager.fileDialog.notFound"));
00484 fdlg.setFilterExtensions(fe);
00485 fdlg.setFilterPath(pi.lastDir.getAbsolutePath());
00486 String fPath = fdlg.open();
00487 if (fPath != null) {
00488 f = new File(fPath);
00489 _url = fPath;
00490 }else
00491 return null;
00492 }
00493
00494 if (name == null){
00495 String basename = f.getName().split("[.]")[0];
00496 if (getItem(cls, basename) != null){
00497 Integer index = 0;
00498 do {
00499 name = basename + index.toString();
00500 index++;
00501 }while(getItem(cls, name) != null);
00502 }else{
00503 name = basename;
00504 }
00505 }
00506
00507 GrxBaseItem item = (GrxBaseItem) createPlugin(cls, name);
00508 if (item != null) {
00509 if (item.load(f)) {
00510 item.setURL(_url);
00511 item.setDefaultDirectory(f.getParent());
00512 pinfoMap_.get(cls).lastDir = f.getParentFile();
00513 } else {
00514 removeItem(item);
00515 return null;
00516 }
00517 }
00518
00519 return item;
00520 }
00521
00527 public GrxBaseItem pasteItem(Class<? extends GrxBaseItem> cls, GrxBaseItem item) {
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539 return item;
00540 }
00541
00548 public GrxBaseView createView(Class<? extends GrxBaseView> cls, String name) {
00549 if (name == null)
00550 name = pinfoMap_.get(cls).title;
00551 return (GrxBaseView) createPlugin(cls, name);
00552 }
00553
00560 @SuppressWarnings("unchecked")
00561 private GrxBasePlugin createPlugin(Class<? extends GrxBasePlugin> cls, String name) {
00562 if (registerPlugin(cls) == null) {
00563 GrxDebugUtil.println("[PM]@createPlugin registerPlugin failed");
00564 return null;
00565 }
00566 try {
00567 HashMap<String, GrxBasePlugin> map = pluginMap_.get(cls);
00568 GrxBasePlugin plugin = map.get(name);
00569 if ( plugin != null) {
00570 if(plugin instanceof GrxBaseItem)
00571 ((GrxBaseItem)plugin).delete();
00572 }
00573 plugin = pluginLoader_.createPlugin(cls, name, this);
00574 if (registerPluginInstance(plugin)) {
00575 return plugin;
00576 }
00577 return plugin;
00578 } catch (Exception e) {
00579 showExceptionTrace("Couldn't load Class:" + cls.getName(), e);
00580 }
00581 return null;
00582 }
00583
00590 @SuppressWarnings("unchecked")
00591 public boolean registerPluginInstance(GrxBasePlugin instance) {
00592 HashMap<String, GrxBasePlugin> map = pluginMap_.get(instance.getClass());
00593 GrxBasePlugin plugin = map.get(instance.getName());
00594 if ( plugin != null) {
00595 GrxDebugUtil.println("[PM]@createPlugin Plugin instance named "+instance.getName()+" is already registered.");
00596 if(plugin instanceof GrxBaseItem){
00597 ((GrxBaseItem)plugin).delete();
00598 map.put(instance.getName(), instance);
00599 }
00600 return false;
00601 }
00602 map.put(instance.getName(), instance);
00603 return true;
00604 }
00605
00611 private void showExceptionTrace(String m, Exception e) {
00612 GrxDebugUtil.printErr(m, e);
00613
00614 Throwable cause = e.getCause();
00615 StackTraceElement[] trace = null;
00616 String msg = m + "\n\n";
00617 if (cause != null) {
00618 msg = cause.toString() + "\n\n";
00619 trace = cause.getStackTrace();
00620 } else {
00621 trace = e.getStackTrace();
00622 }
00623
00624 for (int i = 0; i < trace.length; i++) {
00625 msg += "at " + trace[i].getClassName() + "." + trace[i].getMethodName() + "(";
00626
00627 if (trace[i].isNativeMethod()) {
00628 msg += "(Native Method)\n";
00629 } else if (trace[i].getFileName() == null) {
00630 msg += "(No source code)\n";
00631 } else {
00632 msg += trace[i].getFileName() + ":" + trace[i].getLineNumber() + ")\n";
00633 }
00634 }
00635 MessageDialog.openWarning(null, "Exception Occered", msg);
00636 }
00637
00642 public void removeItem(GrxBaseItem item) {
00643 Map<?, ?> m = pluginMap_.get(item.getClass());
00644 if (m != null) {
00645 setSelectedItem(item, false);
00646 m.remove(item.getName());
00647 itemChange(item, REMOVE_ITEM);
00648 }
00649 }
00650
00655 public void removeItems(Class<? extends GrxBaseItem> cls) {
00656 Map<?, ?> m = pluginMap_.get(cls);
00657 GrxBaseItem[] items = m.values().toArray(new GrxBaseItem[0]);
00658 if(cls.isAssignableFrom(GrxModelItem.class)){
00659 List<GrxModelItem> modifiedModels = new ArrayList<GrxModelItem>();
00660 for (int i = 0; i < items.length; i++){
00661 int ret = ((GrxModelItem)items[i]).checkModifiedModel(false);
00662 if(ret==GrxModelItem.MODIFIED_NG){
00663 for(GrxModelItem modifiedModel : modifiedModels){
00664 modifiedModel.reload();
00665 }
00666 return;
00667 }else if(ret==GrxModelItem.MODIFIED_OK){
00668 modifiedModels.add((GrxModelItem) items[i]);
00669 }
00670 }
00671 }
00672 for (int i = 0; i < items.length; i++)
00673 items[i].delete();
00674 }
00675
00680 public void removeAllItems() {
00681 if (currentMode_ == null)
00682 return;
00683 for (int i = 0; i < currentMode_.activeItemClassList_.size(); i++)
00684 removeItems(currentMode_.activeItemClassList_.get(i));
00685 }
00686
00695 public boolean renamePlugin(GrxBasePlugin item, String newName) {
00696 OrderedHashMap m = pluginMap_.get(item.getClass());
00697 if (m == null) {
00698 System.out.println("map for " + item.getClass() + " doesn't exist in pluginMap_");
00699 return false;
00700 }
00701 if (m.get(newName) == null) {
00702 if (!newName.equals(item.getName())) {
00703 m.remove(item.getName());
00704 m.put(newName, item);
00705 item.setName(newName);
00706 }
00707 return true;
00708 } else {
00709 System.out.println("GrxPluginManager.renamePlugin() : " + newName + " is already used");
00710 }
00711 return false;
00712 }
00713
00718 public ArrayList<GrxBaseItem> getActiveItemList() {
00719 ArrayList<Class<? extends GrxBaseItem>> cList = currentMode_.activeItemClassList_;
00720 ArrayList<GrxBaseItem> iList = new ArrayList<GrxBaseItem>();
00721 for (int i = 0; i < cList.size(); i++) {
00722 Iterator it = pluginMap_.get(cList.get(i)).values().iterator();
00723 while (it.hasNext()) {
00724 GrxBaseItem item = (GrxBaseItem) it.next();
00725 iList.add(item);
00726 }
00727 }
00728 return iList;
00729 }
00730
00737 public GrxBaseItem getItem(Class<? extends GrxBaseItem> cls, String name) {
00738 Iterator it = pluginMap_.get(cls).values().iterator();
00739 while (it.hasNext()) {
00740 GrxBaseItem item = (GrxBaseItem) it.next();
00741 if (item.toString().equals(name) || name == null) {
00742
00743 return item;
00744 }
00745 }
00746 GrxDebugUtil.println("[PM] fault getItem " + cls.getName() + ":" + name);
00747 return null;
00748 }
00749
00755 public GrxBaseItem getItem(String name) {
00756 Iterator<OrderedHashMap> it = pluginMap_.values().iterator();
00757 while (it.hasNext()) {
00758 HashMap m = (HashMap) it.next();
00759 Object o = m.get(name);
00760 if (o != null && o instanceof GrxBaseItem)
00761 return (GrxBaseItem) o;
00762 }
00763 return null;
00764 }
00765
00771 public Map<?, ?> getItemMap(Class<? extends GrxBaseItem> cls) {
00772 return pluginMap_.get(cls);
00773 }
00774
00781 @SuppressWarnings("unchecked")
00782 public <T> T getSelectedItem(Class<? extends GrxBaseItem> cls, String name) {
00783 Map<String, ? extends GrxBaseItem> oMap = pluginMap_.get(cls);
00784 if(oMap==null) return null;
00785 if(name != null){
00786 GrxBaseItem item = oMap.get(name);
00787 if(item.isSelected())
00788 return (T)item;
00789 else
00790 return null;
00791 }else{
00792 for(GrxBaseItem item : oMap.values()){
00793 if(item.isSelected())
00794 return (T)item;
00795 }
00796 return null;
00797 }
00798 }
00799
00805 @SuppressWarnings("unchecked")
00806 public <T> List<T> getSelectedItemList(Class<? extends GrxBaseItem> cls) {
00807 ArrayList<T> list = new ArrayList<T>();
00808 Map<String, ? extends GrxBaseItem> oMap = pluginMap_.get(cls);
00809 if(oMap==null) return list;
00810 for(GrxBaseItem item : oMap.values()){
00811 if (item.isSelected())
00812 list.add((T)item);
00813 }
00814 return list;
00815 }
00816
00821 public List<GrxBaseView> getViewList() {
00822 updateViewList();
00823 return viewList_;
00824 }
00825
00830 public List<GrxBaseView> getActiveViewList() {
00831 updateActiveViewList();
00832 return activeViewList_;
00833 }
00834
00840 public GrxBaseView getView(Class<? extends GrxBaseView> cls, boolean active) {
00841 if(active){
00842 updateActiveViewList();
00843 for (GrxBaseView v : activeViewList_)
00844 if (v.getClass() == cls)
00845 return v;
00846 }else{
00847 updateViewList();
00848 for (GrxBaseView v : viewList_)
00849 if (v.getClass() == cls)
00850 return v;
00851 }
00852 return null;
00853 }
00854
00855 public synchronized GrxBaseView getView(String name, boolean active) {
00856 if(active){
00857 updateActiveViewList();
00858 for (GrxBaseView v : activeViewList_) {
00859 if (v.getName().equals(name)) {
00860 return v;
00861 }
00862 }
00863 }else{
00864 updateViewList();
00865 for (GrxBaseView v : viewList_) {
00866 if (v.getName().equals(name)) {
00867 return v;
00868 }
00869 }
00870 }
00871 return null;
00872 }
00873
00881 @SuppressWarnings("unchecked")
00882 public void setSelectedItem(GrxBaseItem item, boolean select) {
00883 if (item == null)
00884 return;
00885
00886 if (select && item.isExclusive()) {
00887 for (GrxBaseItem i : (Collection<GrxBaseItem>) getItemMap(item.getClass()).values()) {
00888 if (i != item) {
00889 i.setSelected(false);
00890 itemChange(i, NOTSELECTED_ITEM);
00891 }
00892 }
00893 }
00894
00895
00896
00897
00898 item.setSelected(select);
00899 if(select) itemChange(item, SELECTED_ITEM);
00900 else itemChange(item, NOTSELECTED_ITEM);
00901 }
00902
00903
00907 public void clearItemSelection() {
00908 Iterator<OrderedHashMap> i = pluginMap_.values().iterator();
00909 while (i.hasNext()) {
00910 Iterator j = (i.next()).values().iterator();
00911 while (j.hasNext()) {
00912 GrxBasePlugin p = (GrxBasePlugin) j.next();
00913 if (p instanceof GrxBaseItem)
00914 setSelectedItem((GrxBaseItem) p, false);
00915 }
00916 }
00917 }
00918
00925 public String getItemTitle(Class<? extends GrxBasePlugin> cls) {
00926 return pinfoMap_.get(cls).title;
00927 }
00928
00935 public boolean isItemVisible(Class<? extends GrxBasePlugin> cls) {
00936 return pinfoMap_.get(cls).visible;
00937 }
00938
00942 private class PluginInfo {
00943 static final String VISIBLE = "visible";
00944 String title;
00945 File lastDir;
00946 String filter;
00947 Vector<Action> menu;
00948 boolean visible;
00949 }
00950
00956 public Vector<Action> getItemMenu(final Class<? extends GrxBaseItem> cls) {
00957 final PluginInfo pi = pinfoMap_.get(cls);
00958 Vector<Action> menu = pi.menu;
00959 if (menu != null) {
00960 dynamicChangeMenu(cls, menu);
00961 return menu;
00962 }
00963 menu = pi.menu = new Vector<Action>();
00964
00965
00966 Action create = new Action() {
00967 public String getText() {
00968 return MessageBundle.get("GrxPluginManager.menu.create");
00969 }
00970
00971 public void run() {
00972 GrxBaseItem item = createItem(cls, null);
00973 itemChange(item, GrxPluginManager.ADD_ITEM);
00974 setSelectedItem(item, true);
00975 }
00976 };
00977 menu.add(create);
00978
00979
00980 Action load = new Action() {
00981 public String getText() {
00982 return MessageBundle.get("GrxPluginManager.menu.load");
00983 }
00984
00985 public void run() {
00986 FileDialog fdlg = new FileDialog(GrxUIPerspectiveFactory.getCurrentShell(), SWT.OPEN);
00987 String[] fe = { pi.filter };
00988 fdlg.setFilterExtensions(fe);
00989 fdlg.setFilterPath(pi.lastDir.getAbsolutePath());
00990 String fPath = fdlg.open();
00991 if (fPath != null) {
00992 File f = new File(fPath);
00993 GrxBaseItem newItem = loadItem(cls, null, f.getAbsolutePath());
00994 if(newItem!=null){
00995 itemChange(newItem, GrxPluginManager.ADD_ITEM);
00996 setSelectedItem(newItem, true);
00997 pi.lastDir = f.getParentFile();
00998 }
00999 }
01000 }
01001 };
01002 menu.add(load);
01003
01004
01005 Action clear = new Action() {
01006 public String getText() {
01007 return MessageBundle.get("GrxPluginManager.menu.clear");
01008 }
01009
01010 public void run() {
01011 String mes = MessageBundle.get("GrxPluginManager.dialog.message.removeItem");
01012 mes = NLS.bind(mes, new String[]{GrxPluginManager.this.getItemTitle(cls)});
01013 if (MessageDialog.openConfirm(null, MessageBundle.get("GrxPluginManager.dialog.title.removeItem"), mes)){
01014 refuseItemChange();
01015 removeItems(cls);
01016 acceptItemChange();
01017 }
01018 }
01019 };
01020 menu.add(clear);
01021
01022
01023 Action delete = new Action() {
01024 public String getText() {
01025 return MessageBundle.get("GrxPluginManager.menu.delete");
01026 }
01027
01028 public void run() {
01029 String mes = MessageBundle.get("GrxPluginManager.dialog.message.deletePlugin");
01030 mes = NLS.bind(mes, new String[]{GrxPluginManager.this.getItemTitle(cls)});
01031 if (MessageDialog.openConfirm(null, MessageBundle.get("GrxPluginManager.dialog.title.deletePlugin"), mes))
01032 deletePlugin(cls);
01033 }
01034 };
01035 menu.add(delete);
01036
01037 try {
01038 Method m = cls.getMethod("create", (Class[]) null);
01039 Class<?> c = m.getDeclaringClass();
01040 create.setEnabled(!(c == GrxBaseItem.class));
01041
01042 m = cls.getMethod("load", File.class);
01043 c = m.getDeclaringClass();
01044 load.setEnabled(!(c == GrxBaseItem.class));
01045 m = cls.getMethod("paste", String.class);
01046 c = m.getDeclaringClass();
01047 if (c != GrxBaseItem.class) {
01048 Action paste = new Action() {
01049 public String getText() {
01050 return "paste";
01051 }
01052
01053 public void run() {
01054 GrxDebugUtil.println("GrxPluginManager.GrxModelItemClass paste Action");
01055
01056 }
01057 };
01058 paste.setEnabled(!isEmptyClipBord());
01059 menu.add(paste);
01060 }
01061 } catch (Exception e) {
01062 e.printStackTrace();
01063 }
01064
01065
01066 IPreferenceStore store =Activator.getDefault().getPreferenceStore();
01067 String itemClassList=store.getString(PreferenceConstants.ITEM+"."+PreferenceConstants.CLASS);
01068 String[] itemClass=itemClassList.split(PreferenceConstants.SEPARATOR, -1);
01069 boolean found=false;
01070 for(int i=0; i<itemClass.length; i++){
01071 if(cls.getName().equals(itemClass[i])){
01072 found=true;
01073 break;
01074 }
01075 }
01076 if(!found)
01077 delete.setEnabled(true);
01078 else
01079 delete.setEnabled(false);
01080 return menu;
01081 }
01082
01086 private void dynamicChangeMenu(final Class<? extends GrxBaseItem> cls, Vector<Action> menu) {
01087 try {
01088 Method m = cls.getMethod("paste", String.class);
01089 Class<?> c = m.getDeclaringClass();
01090 if (c != GrxBaseItem.class) {
01091 for (Action action : menu) {
01092 if (action.getText().equals("paste")) {
01093 action.setEnabled(!isEmptyClipBord());
01094 break;
01095 }
01096 }
01097 }
01098 } catch (Exception e) {
01099 e.printStackTrace();
01100 }
01101 }
01102
01106 private boolean isEmptyClipBord() {
01107 GrxPluginManager.setClipBordVal();
01108 return GrxPluginManager.getClipBoardVal().length()==0;
01109 }
01110
01114 public void shutdown() {
01115 GrxDebugUtil.println("[PM] shutdown.");
01116
01117 Iterator<OrderedHashMap> it = pluginMap_.values().iterator();
01118 for (; it.hasNext();) {
01119 Iterator it2 = (it.next()).values().iterator();
01120 for (; it2.hasNext();)
01121 ((GrxBasePlugin) it2.next()).shutdown();
01122 }
01123
01124 GrxProcessManager.shutDown();
01125 if(orb_!=null)
01126 orb_.shutdown(false);
01127 }
01128
01136 public void setProjectProperty(String key, String val) {
01137 currentProject_.setProperty(key, val);
01138 }
01139
01145 public String getProjectProperty(String key) {
01146 return currentProject_.getProperty(key);
01147 }
01148
01153 public String getCurrentModeName() {
01154 if (currentMode_ == null)
01155 return null;
01156 return currentMode_.getName();
01157 }
01158
01163 public Vector<Action> getProjectMenu() {
01164 return currentProject_.getMenu();
01165 }
01166
01171 public File getHomePath() {
01172 return homePath_;
01173 }
01174
01179 public String getProjectName() {
01180 return currentProject_.getName();
01181 }
01182
01187 public GrxProjectItem getProject() {
01188 return currentProject_;
01189 }
01190
01195
01196
01197
01198
01199
01200
01205
01206
01207
01208
01209
01210
01211
01212
01213
01218 private static String getClipBoardVal() {
01219 return GrxPluginManager.clipValue_.get();
01220 }
01221
01225 private static void setClipBordVal() {
01226 Clipboard clp = Toolkit.getDefaultToolkit().getSystemClipboard();
01227 Transferable data = clp.getContents(null);
01228 String ret = "";
01229 if (data != null && data.isDataFlavorSupported(DataFlavor.stringFlavor)) {
01230 try {
01231 ret = (String) data.getTransferData(DataFlavor.stringFlavor);
01232 } catch (Exception e) {
01233 GrxDebugUtil.printErr("GrxPluginManager.setClipBordVal: ", e);
01234 }
01235 }
01236 GrxPluginManager.clipValue_.set(ret);
01237 }
01238
01239 private static SynchronizedAccessor<String> clipValue_ = new SynchronizedAccessor<String>("");
01240
01241
01242 public void registerItemChangeListener(GrxItemChangeListener view, Class<? extends GrxBaseItem> cls){
01243 if(itemChangeListener_.get(cls)==null)
01244 itemChangeListener_.put(cls, new ArrayList<GrxItemChangeListener>());
01245 List<GrxItemChangeListener> list = itemChangeListener_.get(cls);
01246 list.add(view);
01247 }
01248
01249 public void removeItemChangeListener(GrxItemChangeListener view, Class<? extends GrxBaseItem> cls) {
01250 if(itemChangeListener_.get(cls)==null) return;
01251 List<GrxItemChangeListener> list = itemChangeListener_.get(cls);
01252 list.remove(view);
01253
01254 }
01255
01256 public void refuseItemChange(){
01257 acceptItemChange_ = false;
01258 }
01259
01260 public void acceptItemChange(){
01261 List<GrxItemChangeListener> listenerList = new ArrayList<GrxItemChangeListener>();
01262 Iterator<Class<? extends GrxBaseItem>> it = itemChangeListener_.keySet().iterator();
01263 while(it.hasNext()){
01264 List<GrxItemChangeListener> list = itemChangeListener_.get(it.next());
01265 Iterator<GrxItemChangeListener> itl = list.iterator();
01266 while(itl.hasNext()){
01267 GrxItemChangeListener listener = itl.next();
01268 if(!listenerList.contains(listener)){
01269 listener.setUp();
01270 listenerList.add(listener);
01271 }
01272 }
01273
01274 }
01275 acceptItemChange_ = true;
01276 }
01277
01278 public void itemChange(GrxBaseItem item, int event){
01279 if(acceptItemChange_){
01280 Iterator<Class<? extends GrxBaseItem>> it = itemChangeListener_.keySet().iterator();
01281 while(it.hasNext()){
01282 Class<? extends GrxBaseItem> cls = it.next();
01283 if(cls.isAssignableFrom(item.getClass())){
01284 List<GrxItemChangeListener> list = itemChangeListener_.get(cls);
01285 Iterator<GrxItemChangeListener> itView = list.iterator();
01286 while(itView.hasNext())
01287 itView.next().registerItemChange(item, event);
01288 }
01289 }
01290 }
01291 }
01292
01293 private void setInitialMode(){
01294 IPreferenceStore store =Activator.getDefault().getPreferenceStore();
01295 String modes = store.getString(PreferenceConstants.MODE);
01296 String[] mode = modes.split(PreferenceConstants.SEPARATOR, -1);
01297 String itemIndexs = store.getString(PreferenceConstants.MODE+"."+PreferenceConstants.ITEMINDEX);
01298 String[] itemIndex = itemIndexs.split(PreferenceConstants.SEPARATOR,-1);
01299 String itemClassList=store.getString(PreferenceConstants.ITEM+"."+PreferenceConstants.CLASS);
01300 String[] itemClass=itemClassList.split(PreferenceConstants.SEPARATOR, -1);
01301 String itemVisibleList=store.getString(PreferenceConstants.ITEM+"."+PreferenceConstants.VISIBLE);
01302 String[] itemVisible=itemVisibleList.split(PreferenceConstants.SEPARATOR, -1);
01303 String userItemClassList=store.getString(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASS);
01304 String[] userItemClass=userItemClassList.split(PreferenceConstants.SEPARATOR, -1);
01305 String userItemPathList=store.getString(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASSPATH);
01306 String[] userItemPath=userItemPathList.split(PreferenceConstants.SEPARATOR, -1);
01307 String userItemVisibleList=store.getString(PreferenceConstants.USERITEM+"."+PreferenceConstants.VISIBLE);
01308 String[] userItemVisible=userItemVisibleList.split(PreferenceConstants.SEPARATOR, -1);
01309 for(int i=0; i<mode.length; i++){
01310 GrxModeInfoItem item = (GrxModeInfoItem)createItem(GrxModeInfoItem.class, mode[i]);
01311 String[] index=itemIndex[i].split(",",-1);
01312 for(int j=0; j<index.length; j++){
01313 int k=Integer.valueOf(index[j]);
01314 if(k<itemClass.length){
01315 if(pluginLoader_.existClass(itemClass[k])){
01316 Class<? extends GrxBasePlugin> plugin=registerPlugin(itemClass[k]);
01317 if (plugin != null) {
01318 PluginInfo pi = pinfoMap_.get(plugin);
01319 pi.visible = itemVisible[k].equals("true")? true : false;
01320 pinfoMap_.put(plugin, pi);
01321 item.addItemClassList(plugin);
01322 }
01323 }
01324 }
01325 }
01326 for(int j=0; j<userItemClass.length; j++){
01327 if(!userItemClass[j].equals("")){
01328 pluginLoader_.addURL(userItemPath[j]);
01329 if(pluginLoader_.existClass(userItemClass[j])){
01330 Class<? extends GrxBasePlugin> plugin=registerPlugin(userItemClass[j]);
01331 if (plugin != null) {
01332 PluginInfo pi = pinfoMap_.get(plugin);
01333 pi.visible = userItemVisible[j].equals("true")? true : false;
01334 pinfoMap_.put(plugin, pi);
01335 item.addItemClassList(plugin);
01336 }
01337 }
01338 }
01339 }
01340 }
01341 store.addPropertyChangeListener(this);
01342
01343 GrxModeInfoItem item = (GrxModeInfoItem)getItem(mode[0]);
01344 setSelectedItem(item, true);
01345 currentMode_ = item;
01346 }
01347
01348 private void versionCheck(){
01349 IPreferenceStore store =Activator.getDefault().getPreferenceStore();
01350 String version = store.getString(PreferenceConstants.VERSION);
01351 if(version.equals("") || !version.equals(PreferenceConstants.CURRENT_VERSION)){
01352
01353 if(version.equals("")){
01354 store.setToDefault(PreferenceConstants.PROCESS+"."+PreferenceConstants.ID);
01355 store.setToDefault(PreferenceConstants.PROCESS+"."+PreferenceConstants.COM);
01356 store.setToDefault(PreferenceConstants.PROCESS+"."+PreferenceConstants.ARGS);
01357 store.setToDefault(PreferenceConstants.PROCESS+"."+PreferenceConstants.AUTOSTART);
01358 store.setToDefault(PreferenceConstants.PROCESS+"."+PreferenceConstants.USEORB);
01359 }
01360 store.setValue(PreferenceConstants.VERSION, PreferenceConstants.CURRENT_VERSION);
01361 }
01362 }
01363
01364 @SuppressWarnings("unchecked")
01365 public void propertyChange(PropertyChangeEvent event) {
01366 if(event.getProperty().equals("userItemChange")){
01367 String newItemList = (String) event.getNewValue();
01368 String[] newItems = newItemList.split(PreferenceConstants.SEPARATOR, -1);
01369 String oldItemList = (String) event.getOldValue();
01370 String[] oldItems = oldItemList.split(PreferenceConstants.SEPARATOR, -1);
01371 IPreferenceStore store =Activator.getDefault().getPreferenceStore();
01372 String userItemPathList=store.getString(PreferenceConstants.USERITEM+"."+PreferenceConstants.CLASSPATH);
01373 String[] userItemPath=userItemPathList.split(PreferenceConstants.SEPARATOR, -1);
01374 String userItemVisibleList=store.getString(PreferenceConstants.USERITEM+"."+PreferenceConstants.VISIBLE);
01375 String[] userItemVisible=userItemVisibleList.split(PreferenceConstants.SEPARATOR, -1);
01376 Vector<Integer> delList = new Vector<Integer>();
01377 for(int i=0; i<oldItems.length; i++){
01378 boolean flg=false;
01379 for(String newItem : newItems){
01380 if(newItem.equals(oldItems[i])){
01381 flg=true;
01382 break;
01383 }
01384 }
01385 if(!flg && !oldItems[i].equals(""))
01386 delList.add(new Integer(i));
01387
01388 }
01389
01390 OrderedHashMap modes=(OrderedHashMap) getItemMap(GrxModeInfoItem.class);
01391 for(Integer i : delList){
01392 Class<? extends GrxBasePlugin> itemClass=null;
01393 try {
01394 itemClass = (Class<? extends GrxBasePlugin>) Class.forName(oldItems[i.intValue()], false, pluginLoader_);
01395 } catch (ClassNotFoundException e) {
01396 continue;
01397 }
01398 if(currentMode_.activeItemClassList_.contains(itemClass))
01399 removeItems((Class<? extends GrxBaseItem>) itemClass);
01400 Iterator<GrxModeInfoItem> it = modes.values().iterator();
01401 while (it.hasNext()) {
01402 GrxModeInfoItem mode = it.next();
01403 if(mode.activeItemClassList_.contains(itemClass))
01404 mode.activeItemClassList_.remove(itemClass);
01405 }
01406 }
01407 for(int i=0; i<newItems.length; i++){
01408 pluginLoader_.addURL(userItemPath[i]);
01409 Class<? extends GrxBasePlugin> itemClass=null;
01410 try {
01411 itemClass = (Class<? extends GrxBasePlugin>) Class.forName(newItems[i], false, pluginLoader_);
01412 } catch (ClassNotFoundException e) {
01413 continue;
01414 }
01415 Iterator<GrxModeInfoItem> it = modes.values().iterator();
01416 while (it.hasNext()) {
01417 GrxModeInfoItem mode = it.next();
01418 if(mode.activeItemClassList_.contains(itemClass)){
01419 PluginInfo pi = pinfoMap_.get(itemClass);
01420 pi.visible = userItemVisible[i].equals("true")? true : false;
01421 pinfoMap_.put(itemClass, pi);
01422 }else{
01423 if(pluginLoader_.existClass(newItems[i])){
01424 Class<? extends GrxBasePlugin> plugin=registerPlugin(newItems[i]);
01425 if (plugin != null) {
01426 PluginInfo pi = pinfoMap_.get(plugin);
01427 pi.visible = userItemVisible[i].equals("true")? true : false;
01428 pinfoMap_.put(plugin, pi);
01429 mode.addItemClassList(plugin);
01430 }
01431 }
01432 }
01433 }
01434 }
01435 }
01436 else if(event.getProperty().equals(PreferenceConstants.FONT_TABLE)){
01437 Activator.getDefault().updateTableFont();
01438 List<GrxBaseView> list = getViewList();
01439 for (GrxBaseView v : list){
01440 v.updateTableFont();
01441 }
01442 }
01443 else if(event.getProperty().equals(PreferenceConstants.FONT_EDITER)){
01444 Activator.getDefault().updateEditerFont();
01445 List<GrxBaseView> list = getViewList();
01446 for (GrxBaseView v : list){
01447 v.updateEditerFont();
01448 }
01449 }
01450 itemChange(currentMode_, CHANGE_MODE);
01451 }
01452
01453 @SuppressWarnings("unchecked")
01454 public void addPlugin(String className, String classPath){
01455 pluginLoader_.addURL(classPath);
01456 Class<? extends GrxBasePlugin> itemClass=null;
01457 try {
01458 itemClass = (Class<? extends GrxBasePlugin>) Class.forName(className, false, pluginLoader_);
01459 } catch (ClassNotFoundException e) {
01460 return;
01461 }
01462 OrderedHashMap modes=(OrderedHashMap) getItemMap(GrxModeInfoItem.class);
01463 Iterator<GrxModeInfoItem> it = modes.values().iterator();
01464 while (it.hasNext()) {
01465 GrxModeInfoItem mode = it.next();
01466 if(!mode.activeItemClassList_.contains(itemClass)){
01467 if(pluginLoader_.existClass(className)){
01468 Class<? extends GrxBasePlugin> plugin=registerPlugin(className);
01469 if (plugin != null) {
01470 PluginInfo pi = pinfoMap_.get(plugin);
01471 pi.visible = true;
01472 pinfoMap_.put(plugin, pi);
01473 mode.addItemClassList(plugin);
01474 }
01475 }
01476 }
01477 }
01478 itemChange(currentMode_, CHANGE_MODE);
01479 }
01480
01481 @SuppressWarnings("unchecked")
01482 public void deletePlugin(Class<? extends GrxBasePlugin> cls){
01483 OrderedHashMap modes=(OrderedHashMap) getItemMap(GrxModeInfoItem.class);
01484 if(currentMode_.activeItemClassList_.contains(cls)){
01485 removeItems((Class<? extends GrxBaseItem>) cls);
01486 Iterator<GrxModeInfoItem> it = modes.values().iterator();
01487 while (it.hasNext()) {
01488 GrxModeInfoItem mode = it.next();
01489 if(mode.activeItemClassList_.contains(cls))
01490 mode.activeItemClassList_.remove(cls);
01491 }
01492 }
01493 itemChange(currentMode_, CHANGE_MODE);
01494 }
01495
01496 public void dispose(){
01497 IPreferenceStore store =Activator.getDefault().getPreferenceStore();
01498 store.removePropertyChangeListener(this);
01499 }
01500
01501 public void loadInitialProject(){
01502 IPreferenceStore store =Activator.getDefault().getPreferenceStore();
01503 String initProjectFile = store.getString(PreferenceConstants.INITIALPROJECT);
01504 File f = new File(initProjectFile);
01505 if(f.exists() && f.isFile())
01506 currentProject_.load(f);
01507 }
01508
01509 public Properties getViewProperties(String name) {
01510 return currentProject_.getViewProperties(name);
01511 }
01512
01513 }