GrxProjectItem.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  *  GrxProjectItem.java
00012  *
00013  *  Copyright (C) 2007 GeneralRobotix, Inc.
00014  *  All Rights Reserved
00015  *
00016  *  @author Yuichiro Kawasumi (General Robotix, Inc.)
00017  */
00018 
00019 package com.generalrobotix.ui.item;
00020 
00021 import java.io.*;
00022 import java.lang.reflect.InvocationTargetException;
00023 import java.net.URL;
00024 import java.util.*;
00025 
00026 import javax.xml.parsers.DocumentBuilder;
00027 import javax.xml.parsers.DocumentBuilderFactory;
00028 import javax.xml.parsers.ParserConfigurationException;
00029 import javax.xml.transform.OutputKeys;
00030 import javax.xml.transform.Transformer;
00031 import javax.xml.transform.TransformerConfigurationException;
00032 import javax.xml.transform.TransformerException;
00033 import javax.xml.transform.TransformerFactory;
00034 import javax.xml.transform.dom.DOMSource;
00035 import javax.xml.transform.stream.StreamResult;
00036 
00037 import org.eclipse.core.runtime.IProgressMonitor;
00038 import org.eclipse.jface.action.Action;
00039 import org.eclipse.jface.dialogs.InputDialog;
00040 import org.eclipse.jface.dialogs.MessageDialog;
00041 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
00042 import org.eclipse.jface.operation.IRunnableWithProgress;
00043 import org.eclipse.jface.preference.IPreferenceStore;
00044 import org.eclipse.osgi.util.NLS;
00045 import org.eclipse.swt.SWT;
00046 import org.eclipse.swt.widgets.FileDialog;
00047 import org.eclipse.ui.IPerspectiveDescriptor;
00048 import org.eclipse.ui.IPerspectiveRegistry;
00049 import org.eclipse.ui.IWorkbench;
00050 import org.eclipse.ui.IWorkbenchPage;
00051 import org.eclipse.ui.IWorkbenchWindow;
00052 import org.eclipse.ui.PlatformUI;
00053 import org.w3c.dom.Document;
00054 import org.w3c.dom.Element;
00055 import org.w3c.dom.Node;
00056 import org.w3c.dom.NodeList;
00057 import org.xml.sax.InputSource;
00058 import org.xml.sax.SAXException;
00059 
00060 import com.generalrobotix.ui.grxui.Activator;
00061 import com.generalrobotix.ui.grxui.GrxUIPerspectiveFactory;
00062 import com.generalrobotix.ui.grxui.PreferenceConstants;
00063 import com.generalrobotix.ui.*;
00064 import com.generalrobotix.ui.view.Grx3DView;
00065 import com.generalrobotix.ui.util.GrxConfigBundle;
00066 import com.generalrobotix.ui.util.GrxCorbaUtil;
00067 import com.generalrobotix.ui.util.GrxDebugUtil;
00068 import com.generalrobotix.ui.util.GrxXmlUtil;
00069 import com.generalrobotix.ui.util.MessageBundle;
00070 import com.generalrobotix.ui.util.OrderedHashMap;
00071 import com.generalrobotix.ui.util.TwoInputDialog;
00072 
00073 @SuppressWarnings({ "unchecked", "serial" }) //$NON-NLS-1$ //$NON-NLS-2$
00074 public class GrxProjectItem extends GrxBaseItem {
00075         public static final String TITLE = "Project"; //$NON-NLS-1$
00076         public static final String DEFAULT_DIR = "/"; //$NON-NLS-1$
00077         public static final String FILE_EXTENSION = "xml"; //$NON-NLS-1$
00078 
00079         public static final int MENU_CREATE=0, MENU_RESTORE=1, MENU_LOAD=2, MENU_SAVE=3, MENU_SAVE_AS=4, MENU_IMPORT=5;
00080         private Vector<Action> menu_;
00081         
00082     private static final String MODE_TAG = "mode"; //$NON-NLS-1$
00083     private static final String WINCONF_TAG = "perspective"; //$NON-NLS-1$
00084 
00085         private Document doc_;
00086     private DocumentBuilder builder_;
00087     private Transformer transformer_;
00088     private HashMap<String, Properties> viewProperties_ = new HashMap<String, Properties>(); 
00089     
00090     private Map<String, ModeNodeInfo> modeInfoMap_ = new HashMap<String, ModeNodeInfo>();
00091     
00092     private class ModeNodeInfo {
00093         Element  root;
00094         List     propList;
00095         NodeList itemList;
00096         NodeList viewList;
00097         Element  windowConfig;
00098     }
00099     
00100         public GrxProjectItem(String name, GrxPluginManager manager) {
00101                 super(name, manager);
00102 //              setIcon(manager_.ROBOT_ICON);
00103                 DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
00104                 TransformerFactory tffactory = TransformerFactory.newInstance();
00105         
00106                 try {
00107                         builder_ = dbfactory.newDocumentBuilder();
00108                         transformer_ = tffactory.newTransformer();
00109             transformer_.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
00110             transformer_.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
00111             setDefaultDirectory();
00112                 } catch (ParserConfigurationException e) {
00113                         e.printStackTrace();
00114                 } catch (TransformerConfigurationException e) {
00115                         e.printStackTrace();
00116                 }
00117         }
00118 
00119         public boolean create() {
00120                 viewProperties_.clear();
00121                 clear();
00122                 setURL("");
00123                 for (int i=0; ; i++) {
00124                         File f = new File(getDefaultDir().getAbsolutePath()+"/"+"newproject"+i+".xml"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
00125                         if (!f.isFile()) {
00126                                 setName(f.getName().split("[.]")[0]); //$NON-NLS-1$
00127                                 break;
00128                         }
00129                 }
00130                         
00131                 doc_ = builder_.newDocument();
00132                 element_ = doc_.createElement("grxui"); //$NON-NLS-1$
00133                 element_.appendChild(doc_.createTextNode("\n")); //$NON-NLS-1$
00134                 doc_.appendChild(element_);
00135                 _updateModeInfo();
00136                 IWorkbenchPage page=null;
00137                 IWorkbench workbench = PlatformUI.getWorkbench();
00138         if( workbench != null){
00139                 IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
00140                 for(IWorkbenchWindow window : windows){
00141                         page = window.getActivePage();
00142                         if (page != null &&
00143                             page.getPerspective().getId().equals(GrxUIPerspectiveFactory.ID+ ".project") )
00144                         page.closePerspective(page.getPerspective(), false, false);
00145                 }
00146         }
00147         
00148         List<GrxBaseView> vl = manager_.getViewList();
00149                 for (int i=0; i<vl.size(); i++) 
00150                         if (vl.get(i) != null) vl.get(i).restoreProperties();
00151                 
00152                 return true;
00153         }
00154 
00155         private void _updateModeInfo() {
00156                 modeInfoMap_.clear();
00157 
00158                 NodeList modeList = doc_.getElementsByTagName(MODE_TAG);
00159                 for (int i=0; i<modeList.getLength(); i++) {
00160             ModeNodeInfo mi = new ModeNodeInfo();
00161                         mi.root = (Element)modeList.item(i);
00162             modeInfoMap_.put(mi.root.getAttribute("name"), mi); //$NON-NLS-1$
00163 
00164                         // property node 
00165                         NodeList propList = mi.root.getElementsByTagName(PROPERTY_TAG);
00166                         List<Element> elList = new ArrayList<Element>();
00167                         for (int j=0; j<propList.getLength(); j++) {
00168                                 if (propList.item(j).getParentNode() == mi.root) 
00169                                         elList.add((Element)propList.item(j));
00170                         }
00171                         mi.propList =  elList;
00172 
00173             // item node
00174                         mi.itemList = mi.root.getElementsByTagName(ITEM_TAG);
00175 
00176             // view node
00177                         mi.viewList = mi.root.getElementsByTagName(VIEW_TAG);
00178                         
00179             // window config element
00180                         NodeList wconfList = mi.root.getElementsByTagName(WINCONF_TAG);
00181                         if (wconfList.getLength() > 0)
00182                                 mi.windowConfig =  (Element)wconfList.item(0);
00183                         else
00184                                 mi.windowConfig = null;
00185                                 
00186                 }
00187         }
00188 
00189     private ModeNodeInfo _getModeNodeInfo(String mode) {
00190         ModeNodeInfo mi = modeInfoMap_.get(mode);
00191         if (mi == null) {
00192             mi = new ModeNodeInfo();
00193 
00194                     NodeList nodeList = doc_.getElementsByTagName(MODE_TAG);
00195                     for (int i=0; i<nodeList.getLength(); i++) {
00196                             Element e = (Element)nodeList.item(i);
00197                             if (e.getAttribute("name").equals(mode)) { //$NON-NLS-1$
00198                                     mi.root = e;
00199                     break;
00200                 }
00201                     }
00202                     if (mi.root == null) {
00203                             mi.root = doc_.createElement(MODE_TAG);
00204                             mi.root.setAttribute("name", mode); //$NON-NLS-1$
00205                             element_.appendChild(doc_.createTextNode(INDENT4));
00206                             element_.appendChild(mi.root);
00207                             element_.appendChild(doc_.createTextNode("\n")); //$NON-NLS-1$
00208             } 
00209 
00210                         _updateModeInfo();
00211         }
00212 
00213         return mi;
00214     }
00215         
00216     public void setWindowConfigElement(String mode, Element element){
00217         _getModeNodeInfo(mode).windowConfig = element;
00218     }
00219     
00220         public Element getWindowConfigElement(String mode) {
00221         return _getModeNodeInfo(mode).windowConfig; 
00222         }
00223    
00224         private void storeMode(String mode) {
00225                 Element modeEl = _getModeNodeInfo(mode).root;
00226                 NodeList list = modeEl.getChildNodes();
00227                 for (int i=list.getLength()-1; i>=0; i--)
00228                         modeEl.removeChild(list.item(i));
00229                 
00230                 modeEl.appendChild(doc_.createTextNode("\n")); //$NON-NLS-1$
00231                 
00232                 List<GrxBaseItem> itemList = manager_.getActiveItemList();
00233                 for (int i=0; i<itemList.size(); i++) {
00234                         GrxBaseItem item = itemList.get(i);
00235                         modeEl.appendChild(doc_.createTextNode(INDENT4+INDENT4));
00236                         item.setDocument(doc_);
00237                         modeEl.appendChild(item.storeProperties());
00238                         modeEl.appendChild(doc_.createTextNode("\n")); //$NON-NLS-1$
00239                 }
00240                 
00241                 List<GrxBaseView> viewList = manager_.getActiveViewList();
00242                 for (int i=0; i<viewList.size(); i++) {
00243                         GrxBaseView view = viewList.get(i);
00244                         if (view.propertyNames().hasMoreElements()) {
00245                                 view.setDocument(doc_);
00246                                 Element element = view.storeProperties();
00247                                 if(element!=null){
00248                                         modeEl.appendChild(doc_.createTextNode(INDENT4+INDENT4));
00249                                         modeEl.appendChild(element);
00250                                         modeEl.appendChild(doc_.createTextNode("\n")); //$NON-NLS-1$
00251                                 }
00252                         }
00253                 }
00254                 
00255                 modeEl.appendChild(doc_.createTextNode(INDENT4));
00256         }
00257         
00258         @SuppressWarnings("deprecation")
00259         private Element storePerspectiveConf(Element element){
00260                 IWorkbenchPage page=null;
00261                 IWorkbench workbench = PlatformUI.getWorkbench();
00262         if( workbench != null){
00263                 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
00264                 if (window != null){
00265                         page = window.getActivePage();
00266                         if (!page.getPerspective().getId().contains(GrxUIPerspectiveFactory.ID) ){
00267                                 return null;
00268                         }
00269                 }
00270         }
00271                 
00272         IPerspectiveRegistry perspectiveRegistry=workbench.getPerspectiveRegistry();
00273         IPerspectiveDescriptor orgPd=perspectiveRegistry.findPerspectiveWithId(GrxUIPerspectiveFactory.ID );
00274                 IPerspectiveDescriptor tempPd=perspectiveRegistry.findPerspectiveWithId(GrxUIPerspectiveFactory.ID + ".project");
00275                 if(tempPd!=null)
00276                         perspectiveRegistry.deletePerspective(tempPd);
00277                 tempPd = perspectiveRegistry.clonePerspective(GrxUIPerspectiveFactory.ID + ".project", getName(), orgPd);
00278                 page.savePerspectiveAs(tempPd);
00279                 page.setPerspective(orgPd);
00280                 page.setPerspective(tempPd);
00281                 IPreferenceStore store = workbench.getPreferenceStore();
00282                 String confString=store.getString(GrxUIPerspectiveFactory.ID +".project"+"_persp");
00283                 Document doc=null;
00284                 try {
00285                         doc = builder_.parse(new InputSource(new StringReader(confString)));
00286                         Element confElement =doc.getDocumentElement();
00287                         Node elementCopy = doc_.importNode(confElement, true);
00288                         element.appendChild(elementCopy);
00289                         return (Element) elementCopy;
00290                 } catch (SAXException e1) {
00291                         e1.printStackTrace();
00292                 } catch (IOException e1) {
00293                         e1.printStackTrace();
00294                 }
00295                 return null;
00296         }
00297         
00298         public Vector<Action> getMenu() {
00299 
00300                 if( menu_ == null ){
00301                         menu_ = new Vector<Action>();
00302 
00303                         // MENU_CREATE=0
00304                         menu_.add( new Action(){
00305                                 public String getText(){ return MessageBundle.get("GrxProjectItem.menu.CreateProject"); } //$NON-NLS-1$
00306                                 public void run(){
00307                                         boolean ans = MessageDialog.openConfirm( null, MessageBundle.get("GrxProjectItem.dialog.title.createProject"), MessageBundle.get("GrxProjectItem.dialog.message.createProject") ); //$NON-NLS-1$ //$NON-NLS-2$
00308                                         if ( ans ){
00309                                                 if(!checkModifiedModel())
00310                                 return;
00311                                                 manager_.refuseItemChange();
00312                                                 manager_.removeAllItems();
00313                                                 manager_.acceptItemChange();
00314                                         }else if ( ans == false )
00315                                                 return;
00316                                         create();
00317                                 }
00318                         } );
00319                         // MENU_RESTORE=1
00320                         menu_.add( new Action(){
00321                                 public String getText(){ return MessageBundle.get("GrxProjectItem.menu.restoreProject"); } //$NON-NLS-1$
00322                                 public void run(){
00323                                         manager_.refuseItemChange();
00324                                         manager_.removeAllItems();
00325                                         restoreProject();
00326                                         manager_.acceptItemChange();
00327                                 }
00328                         } );
00329                         // MENU_LOAD=2
00330                         menu_.add( new Action(){
00331                                 public String getText(){ return MessageBundle.get("GrxProjectItem.menu.loadProject"); } //$NON-NLS-1$
00332                                 public void run(){
00333                     if(!checkModifiedModel())
00334                         return;
00335                     load();
00336                                 }
00337                         } );
00338                         // MENU_SAVE=3
00339                         menu_.add( new Action(){
00340                                 public String getText(){ return MessageBundle.get("GrxProjectItem.menu.saveProject"); } //$NON-NLS-1$
00341                                 public void run(){
00342                                         save();
00343                                 }
00344                         } );
00345                         // MENU_SAVE_AS=4
00346                         menu_.add( new Action(){
00347                                 public String getText(){ return MessageBundle.get("GrxProjectItem.menu.saveProjectAs"); } //$NON-NLS-1$
00348                                 public void run(){
00349                                         saveAs();
00350                                 }
00351                         } );
00352                         // MENU_IMPORT=3
00353                         /*
00354                         menu_.add( new Action(){
00355                                 public String getText(){ return MessageBundle.get("GrxProjectItem.menu.ImportISE"); } //$NON-NLS-1$
00356                                 public void run(){
00357                                         importISEProject();
00358                                 }
00359                         } );
00360                         */
00361                         menu_.add( new Action(){
00362                                 public String getText(){ return MessageBundle.get("GrxProjectItem.menu.addItem"); } //$NON-NLS-1$
00363                                 public void run(){
00364                                         addItem();
00365                                 }
00366                         } );
00367 
00368                 }
00369                 
00370                 return menu_;
00371         }
00372 
00373     private boolean checkModifiedModel()
00374     {
00375         List<GrxModelItem> modelList = manager_.<GrxModelItem>getSelectedItemList(GrxModelItem.class);
00376         List<GrxModelItem> modifiedModels = new ArrayList<GrxModelItem>();
00377         for (int i=0; i<modelList.size(); i++) {
00378             GrxModelItem model = modelList.get(i);
00379             int ret=model.checkModifiedModel(false);
00380             if(ret==GrxModelItem.MODIFIED_NG){
00381                 for(GrxModelItem modifiedModel : modifiedModels){
00382                         modifiedModel.reload();
00383                 }
00384                 return false;
00385             }else if(ret==GrxModelItem.MODIFIED_OK){
00386                 modifiedModels.add(model);
00387             }
00388         }
00389         return true;
00390     }
00391 
00392         public void save(File f) {
00393                 if (f.exists()) {
00394                         if (!f.isFile())
00395                return;
00396                 }
00397                 
00398                 String mode = manager_.getCurrentModeName();
00399                 storeMode(mode);
00400 
00401                 setName(f.getName().split("[.]")[0]); //$NON-NLS-1$
00402         setURL(f.getAbsolutePath());
00403         
00404         if (MessageDialog.openQuestion(null, MessageBundle.get("GrxProjectItem.dialog.savewindow.title"), 
00405                         MessageBundle.get("GrxProjectItem.dialog.savewindow.message"))) { //$NON-NLS-1$
00406                         Element element = _getModeNodeInfo(mode).root;
00407                         Element windowConfigElement = storePerspectiveConf(element);
00408                         setWindowConfigElement(mode, windowConfigElement);
00409         }
00410                 
00411         if (f == null)
00412                         f = new File(getDefaultDir().getAbsolutePath()+"/"+getName()+".xml"); //$NON-NLS-1$ //$NON-NLS-2$
00413                 
00414                 if (!f.getAbsolutePath().endsWith(".xml")) //$NON-NLS-1$
00415                         f = new File(f.getAbsolutePath()+".xml"); //$NON-NLS-1$
00416                 
00417                 try {
00418                         DOMSource src = new DOMSource();
00419                         src.setNode(doc_);
00420                         StreamResult target = new StreamResult();
00421                         target.setOutputStream(new FileOutputStream(f));
00422                         transformer_.transform(src, target);
00423                         return ;
00424                 } catch (TransformerConfigurationException e) {
00425                         e.printStackTrace();
00426                 } catch (FileNotFoundException e) {
00427                         e.printStackTrace();
00428                 } catch (TransformerException e) {
00429                         e.printStackTrace();
00430                 }
00431                 MessageDialog.openError(null, MessageBundle.get("GrxProjectItem.dialog.saveError.title"), MessageBundle.get("GrxProjectItem.dialog.saveError.message"));
00432         }
00433         
00434         public void saveAs() {
00435                 String path = getURL(true);
00436                 if (path == null)
00437                         path = getDefaultDir().getAbsolutePath()+"/"+getName()+".xml"; //$NON-NLS-1$ //$NON-NLS-2$
00438                 
00439                 File initialFile = new File(path);
00440 
00441                 FileDialog fdlg = new FileDialog( GrxUIPerspectiveFactory.getCurrentShell(), SWT.SAVE);
00442                 String[] exts = { "*.xml" }, extNames={"GrxUI Project"}; //$NON-NLS-1$ //$NON-NLS-2$
00443                 fdlg.setFilterExtensions( exts );
00444                 fdlg.setFilterNames( extNames );
00445 
00446                 fdlg.setFilterPath( initialFile.getParent() );
00447                 fdlg.setFileName( initialFile.getName() );
00448                 
00449                 String fPath = fdlg.open();
00450                 if( fPath != null ) {
00451                         File f = new File(fPath);
00452                         if (f.exists() && f.isFile()){
00453                                 boolean ans = MessageDialog.openConfirm( null, MessageBundle.get("GrxProjectItem.dialog.title.saveProject"), //$NON-NLS-1$
00454                                                 MessageBundle.get("GrxProjectItem.dialog.message.saveProject0")+f.getName()+MessageBundle.get("GrxProjectItem.dialog.message.saveProject1") + //$NON-NLS-1$ //$NON-NLS-2$
00455                                 MessageBundle.get("GrxProjectItem.dialog.message.saveProject2") ); //$NON-NLS-1$
00456                                 if (ans == false)
00457                                         return;
00458                         }
00459                         System.setProperty("CURRENT_DIR", f.getParent());
00460                         save( f );
00461                         setDefaultDirectory(f.getParent());
00462                 }       
00463         }
00464         
00465         public void save(){
00466                 if (getURL(true) == null){
00467                         saveAs();
00468                 }else{
00469                         File f = new File(getURL(true));
00470                         if (f.exists()){
00471                                 save(f);
00472                         }else{
00473                                 saveAs();
00474                         }
00475                 }
00476         }
00477 
00481         public void load() {
00482                 FileDialog fdlg = new FileDialog( GrxUIPerspectiveFactory.getCurrentShell(), SWT.OPEN);
00483                 String[] fe = { "*.xml" }; //$NON-NLS-1$
00484                 fdlg.setFilterExtensions( fe );
00485         fdlg.setFilterPath(getDefaultDir().getAbsolutePath());
00486                 
00487                 String fPath = fdlg.open();
00488                 if( fPath != null ) {
00489                         File f = new File(fPath);
00490                         load(f);
00491                 }
00492         }
00493 
00499         public boolean load(File f) {
00500                 System.out.println( "[ProjectItem]@load ProjectFile load "+ f.toString()); //$NON-NLS-1$
00501                 
00502                 if (f == null || !f.isFile())
00503                         return false;
00504                 GrxSimulationItem simItem = manager_.<GrxSimulationItem>getSelectedItem(GrxSimulationItem.class, null);
00505                 if(simItem!=null && simItem.isSimulating())
00506                         simItem.stopSimulation();
00507                 
00508                 manager_.refuseItemChange();
00509                 manager_.removeAllItems();
00510                 manager_.focusedItem(manager_.getProject());
00511                 setName(f.getName().split("[.]")[0]); //$NON-NLS-1$
00512                 
00513                 String dir = f.getParent();
00514                 if(dir != null)
00515                         System.setProperty("CURRENT_DIR", dir);
00516                 
00517         try {
00518                 doc_ = builder_.parse(f);
00519                 element_ = doc_.getDocumentElement();
00520                 _updateModeInfo();
00521                 file_ = f;
00522                 setURL(f.getAbsolutePath());
00523         } catch (Exception e) {
00524                 GrxDebugUtil.printErr("project load:",e); //$NON-NLS-1$
00525                 file_ = null;
00526                 return false;
00527         }
00528                 
00529                 // register mode
00530         OrderedHashMap modes=(OrderedHashMap)manager_.getItemMap(GrxModeInfoItem.class);
00531                 Iterator<GrxModeInfoItem> it = modes.values().iterator();
00532                 NodeList list = doc_.getElementsByTagName(MODE_TAG);
00533                 for (int i=0; i<list.getLength(); i++) {
00534                         Element modeEl = (Element) list.item(i);
00535                         String modeName = modeEl.getAttribute("name"); //$NON-NLS-1$
00536                         if (modeName == null) 
00537                                 continue;
00538                         boolean found=false;
00539                     while (it.hasNext()) {
00540                         GrxModeInfoItem mode = it.next();
00541                         if(modeName.equals(mode.getName())){
00542                                 mode.setElement(modeEl);
00543                                 found=true;
00544                                 break;
00545                         }
00546                     }
00547                     if(!found){
00548                         GrxModeInfoItem item = (GrxModeInfoItem)manager_.createItem(GrxModeInfoItem.class, modeName);
00549                         if (item != null)  {
00550                                 item.setElement(modeEl);
00551                                 if (GrxXmlUtil.getBoolean(modeEl, "select", true)) { //$NON-NLS-1$
00552                                         manager_.setSelectedItem(item, true);
00553                                 }
00554                         }
00555                         manager_.setCurrentMode(item);
00556                         }
00557                 }
00558                 
00559                 setDefaultDirectory(f.getParent());
00560                 restoreProject();
00561                 manager_.acceptItemChange();
00562                 return true;
00563         }
00564         
00565         public void restoreProject() {
00566 
00567                 String mode = manager_.getCurrentModeName();
00568                 System.out.println("Restore Project (Mode:" +mode+")"); //$NON-NLS-1$ //$NON-NLS-2$
00569 
00570                 IRunnableWithProgress runnableProgress = new IRunnableWithProgress() {
00571                         public void run(IProgressMonitor monitor) throws InterruptedException {
00572                                 String mode = manager_.getCurrentModeName();
00573                                 monitor.beginTask("Restore Project (Mode:" +mode+")", 10 ); //$NON-NLS-1$ //$NON-NLS-2$
00574                                 restoreProject_work(mode, monitor);
00575                                 monitor.done();
00576                         }
00577                 };
00578                 ProgressMonitorDialog progressMonitorDlg = new ProgressMonitorDialog(GrxUIPerspectiveFactory.getCurrentShell());
00579                 try {
00580                         progressMonitorDlg.run(false,false, runnableProgress);
00581                         //ダイアログの影が残ってしまぁE策  //
00582                         Grx3DView view3d =  (Grx3DView)manager_.getView( Grx3DView.class, true );
00583                         if(view3d!=null){
00584                                 view3d.repaint();
00585                         }
00586                 } catch (InvocationTargetException e) {
00587                         e.printStackTrace();
00588                 } catch (InterruptedException e) {
00589                         e.printStackTrace();
00590                 }
00591         }
00592 
00593         private void restoreProject_work(String mode, IProgressMonitor monitor) {
00594                 //manager_.restoreProcess();
00595 
00596                 monitor.worked(1);
00597 
00598                 if (file_ == null || !file_.isFile())
00599                 {
00600                         return;
00601                 }
00602                 ModeNodeInfo minfo =  modeInfoMap_.get(mode);
00603 
00604         monitor.worked(1);
00605         
00606                 List propList = minfo.propList;
00607                 if (propList != null) {
00608                         for (int i=0; i<propList.size(); i++) {
00609                                 Element propEl = (Element)propList.get(i);
00610                                 String key = propEl.getAttribute("name"); //$NON-NLS-1$
00611                                 String val = propEl.getAttribute("value"); //$NON-NLS-1$
00612                                 setProperty(key, val);
00613                         }
00614                 }
00615 
00616                 monitor.worked(1);
00617 
00618                 if (minfo.itemList != null) {   
00619                         
00620                 //  古ぁEEロジェクトファイルに対応  //
00621                         //  ModeがSimulationでSimulationItemがなぁE合E自動的に作Eし、WorldStateItemのプロパティをSimulationItemに設定  //
00622                         if(mode.equals("Simulation") && !containSimulationItem(minfo.itemList)){
00623                                 Element simElement = doc_.createElement(ITEM_TAG);
00624                                 simElement.setAttribute("class", PreferenceConstants.SIMULATIONITEM);
00625                                 simElement.setAttribute("name", "simulationItem");
00626                                 simElement.setAttribute("select", "true");
00627                                 minfo.root.appendChild(simElement);
00628                                 minfo.itemList = minfo.root.getElementsByTagName(ITEM_TAG);
00629                                 for (int i=0; i<minfo.itemList.getLength(); i++) {
00630                                     Element itemElement = (Element)minfo.itemList.item(i);
00631                                     if(itemElement.getAttribute("class").equals(PreferenceConstants.WORLDSTATEITEM)){
00632                                         NodeList props = itemElement.getElementsByTagName(PROPERTY_TAG);
00633                                                 for (int j = 0; j < props.getLength(); ) {
00634                                                         Element propEl = (Element) props.item(j);
00635                                                         String key = propEl.getAttribute("name"); //$NON-NLS-1$
00636                                                         String val = propEl.getAttribute("value"); //$NON-NLS-1$
00637                                                         if(!key.equals("logTimeStep")){
00638                                                                 Element element = doc_.createElement(PROPERTY_TAG);
00639                                                                 element.setAttribute("name", key);
00640                                                                 element.setAttribute("value", val);
00641                                                                 simElement.appendChild(element);
00642                                                                 itemElement.removeChild(propEl);
00643                                                         }else
00644                                                                 j++;
00645                                                 }
00646                                     }
00647                                 }
00648                         }
00649                         
00650             List<GrxBaseItem> il = new ArrayList<GrxBaseItem>();
00651                         for (int i = 0; i < minfo.itemList.getLength(); i++) {
00652                                 GrxBaseItem p = (GrxBaseItem)_restorePlugin((Element) minfo.itemList.item(i));
00653                 if (p != null)
00654                      il.add(p);
00655             }
00656             
00657             // for a item that is exclusive selection reselect 
00658             for (int i=0; i<il.size(); i++) {
00659                             GrxBaseItem item = il.get(i);
00660                 boolean select = GrxXmlUtil.getBoolean(item.getElement(), "select", false); //$NON-NLS-1$
00661                             //manager_.setSelectedItem(item, select);
00662             }
00663                 }
00664 
00665                 monitor.worked(1);
00666                 
00667                 if (minfo.windowConfig != null) {
00668                         Document doc = builder_.newDocument();
00669                         Node nodeCopy = doc.importNode(minfo.windowConfig, true);
00670                         doc.appendChild(nodeCopy);
00671                         StringWriter sw = new StringWriter();
00672                         try {
00673                                 DOMSource src = new DOMSource();
00674                                 src.setNode(doc);
00675                                 StreamResult target = new StreamResult(sw);
00676                                 transformer_.transform(src, target);
00677                         } catch (TransformerConfigurationException e) {
00678                                 e.printStackTrace();
00679                         } catch (TransformerException e) {
00680                         e.printStackTrace();
00681                         }
00682                 
00683                         IWorkbenchPage page=null;
00684                         IWorkbench workbench = PlatformUI.getWorkbench();
00685                 if( workbench != null){
00686                         IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
00687                         if (window != null){
00688                                 page = window.getActivePage();
00689                                 if (!page.getPerspective().getId().contains(GrxUIPerspectiveFactory.ID) ){
00690                                         return;
00691                                 }
00692                         }
00693                 }
00694                 if (page!=null && page.getPerspective().getId().equals(GrxUIPerspectiveFactory.ID+ ".project") )
00695                         page.closePerspective(page.getPerspective(), false, false);
00696                 IPreferenceStore store = workbench.getPreferenceStore();
00697                         IPerspectiveRegistry perspectiveRegistry=workbench.getPerspectiveRegistry();
00698                         IPerspectiveDescriptor orgPd=perspectiveRegistry.findPerspectiveWithId(GrxUIPerspectiveFactory.ID );
00699                         IPerspectiveDescriptor tempPd=perspectiveRegistry.findPerspectiveWithId(GrxUIPerspectiveFactory.ID + ".project");
00700                         if(tempPd!=null)
00701                                 perspectiveRegistry.deletePerspective(tempPd);
00702                         tempPd = perspectiveRegistry.clonePerspective(GrxUIPerspectiveFactory.ID + ".project", getName(), orgPd);
00703                         store.setValue(GrxUIPerspectiveFactory.ID + ".project"+"_persp", sw.toString());
00704                         if(page!=null)
00705                                 page.setPerspective(tempPd);
00706                 }else{
00707                 IWorkbench workbench = PlatformUI.getWorkbench();
00708                 if( workbench != null){
00709                         IWorkbenchWindow[] windows = workbench.getWorkbenchWindows();
00710                         for(IWorkbenchWindow window : windows){
00711                                 IWorkbenchPage page = window.getActivePage();
00712                                 if (page != null &&
00713                                     page.getPerspective().getId().equals(GrxUIPerspectiveFactory.ID+ ".project") )
00714                                 page.closePerspective(page.getPerspective(), false, false);
00715                         }
00716                 }
00717                 }
00718                 
00719                 viewProperties_.clear();
00720                 if (minfo.viewList != null) {
00721                         for (int i = 0; i < minfo.viewList.getLength(); i++){
00722                                 Element element = (Element)minfo.viewList.item(i);
00723                                 NodeList props = element.getElementsByTagName(PROPERTY_TAG);
00724                                 Properties properties = new Properties();
00725                                 for (int j = 0; j < props.getLength(); j++) {
00726                                         Element propEl = (Element) props.item(j);
00727                                         String key = propEl.getAttribute("name"); //$NON-NLS-1$
00728                                         String val = propEl.getAttribute("value"); //$NON-NLS-1$
00729                                         properties.setProperty(key, val);
00730                                 }
00731                                 viewProperties_.put(element.getAttribute("name"), properties);
00732                         }
00733                 }
00734                 
00735                 List<GrxBaseView> vl = manager_.getViewList();
00736                 for (int i=0; i<vl.size(); i++) 
00737                         if (vl.get(i) != null) vl.get(i).restoreProperties();
00738                 
00739         }
00740         
00741         private GrxBasePlugin _restorePlugin(Element e) {
00742                 String iname = e.getAttribute("name"); //$NON-NLS-1$
00743                 if (iname == null || iname.length() == 0)
00744                         return null;
00745                 manager_.pluginLoader_.addURL(GrxXmlUtil.expandEnvVal(e.getAttribute("lib"))); //$NON-NLS-1$
00746                 Class cls = manager_.registerPlugin(e.getAttribute("class")); //$NON-NLS-1$
00747                 if (cls == null)
00748                         return null;
00749                 manager_.getMode().addItemClassList(cls);
00750                 
00751                 GrxBasePlugin plugin = null;
00752                 if (GrxBaseItem.class.isAssignableFrom(cls)) {
00753                         Class<? extends GrxBaseItem> icls = (Class<? extends GrxBaseItem>) cls;
00754                         String url = e.getAttribute("url"); //$NON-NLS-1$
00755                         if (!url.equals("")) //$NON-NLS-1$
00756                                 plugin = manager_.loadItem(icls, iname, url);
00757                         else
00758                                 plugin = manager_.createItem(icls, iname);
00759                         plugin = manager_.getItem(icls, iname);
00760                 }
00761         
00762         if (plugin != null) {
00763             plugin.setElement(e);
00764                         plugin.restoreProperties();
00765                         if (GrxBaseItem.class.isAssignableFrom(cls)) {
00766                                 manager_.itemChange((GrxBaseItem)plugin, GrxPluginManager.ADD_ITEM);
00767                         manager_.setSelectedItem((GrxBaseItem)plugin, true);
00768                         }
00769         }
00770 
00771         return plugin;
00772         }
00773                 
00774         private boolean containSimulationItem(NodeList list){
00775                 for (int i = 0; i < list.getLength(); i++) {
00776                         if(((Element) list.item(i)).getAttribute("class").equals(PreferenceConstants.SIMULATIONITEM))
00777                                 return true;
00778                 }
00779                 return false;
00780         }
00781         
00782         private static final File DEFAULT_ISE_PROJECT_DIR = new File("../ISE/Projects"); //$NON-NLS-1$
00783         public void importISEProject() {
00784 
00785                 FileDialog fDialog = new FileDialog(null,SWT.SAVE);
00786 
00787                 String [] exts = {"*.prj"}; //$NON-NLS-1$
00788                 String [] filterNames = {"ISE Project File(*.prj)"}; //$NON-NLS-1$
00789                 fDialog.setFilterExtensions(exts);
00790                 fDialog.setFilterNames(filterNames);
00791                 if( DEFAULT_ISE_PROJECT_DIR != null )
00792                         fDialog.setFilterPath( DEFAULT_ISE_PROJECT_DIR.getPath() );
00793 
00794                 String openPath = fDialog.open();
00795                 
00796                 if( openPath != null ) {
00797                         //manager_.processingWindow_.setTitle("Importing ISE Project");
00798                         //manager_.processingWindow_.setMessage(" importing ISE Project ...");
00799                         //manager_.processingWindow_.setVisible(true);
00800                         //Thread t = new Thread() {
00801                                 //public void run() {
00802                                         //File f = fc.getSelectedFile();
00803                                         File f = new File( openPath );
00804                                         setName(f.getName().split(".prj")[0]); //$NON-NLS-1$
00805                                         importISEProject(f);
00806                                         //manager_.processingWindow_.setVisible(false);
00807                                         //fc.setSelectedFile(null);
00808                                         storeMode(manager_.getCurrentModeName());
00809                                 //}
00810                         //};
00811                         //t.start();
00812                 }
00813         }
00814     
00815     public Properties getViewProperties(String viewName){
00816         return viewProperties_.get(viewName);
00817     }
00818         
00819         private static String ENVIRONMENT_NODE = "jp.go.aist.hrp.simulator.EnvironmentNode"; //$NON-NLS-1$
00820         private static String ROBOT_NODE = "jp.go.aist.hrp.simulator.RobotNode"; //$NON-NLS-1$
00821         private static String COLLISIONPAIR_NODE = "jp.go.aist.hrp.simulator.CollisionPairNode"; //$NON-NLS-1$
00822         private static String GRAPH_NODE = "jp.go.aist.hrp.simulator.GraphNode"; //$NON-NLS-1$
00823                         
00824         private static String WORLD_STATE_ITEM = "com.generalrobotix.ui.item.GrxWorldStateItem"; //$NON-NLS-1$
00825         private static String MODEL_ITEM = "com.generalrobotix.ui.item.GrxModelItem"; //$NON-NLS-1$
00826         private static String COLLISIONPAIR_ITEM = "com.generalrobotix.ui.item.GrxCollisionPairItem"; //$NON-NLS-1$
00827         private static String GRAPH_ITEM = "com.generalrobotix.ui.item.GrxGraphItem"; //$NON-NLS-1$
00828         
00829         public void importISEProject(File f) {
00830                 manager_.removeAllItems();
00831                 
00832                 GrxConfigBundle prop = null;
00833                 try {
00834                         prop = new GrxConfigBundle(f.getAbsolutePath());
00835                 } catch (FileNotFoundException e) {
00836                         e.printStackTrace();
00837                 } catch (IOException e) {
00838                         e.printStackTrace();
00839                 }
00840 
00841                 setProperty("nsHost", GrxCorbaUtil.nsHost()); //$NON-NLS-1$
00842                 Integer nsPort = new Integer(GrxCorbaUtil.nsPort());
00843                 setProperty("nsPort", nsPort.toString()); //$NON-NLS-1$
00844 
00845                 String pname = prop.getStr("Project.name", ""); //$NON-NLS-1$ //$NON-NLS-2$
00846                 Class cls = manager_.registerPlugin(WORLD_STATE_ITEM);
00847                 GrxBaseItem newItem = manager_.createItem((Class <? extends GrxBaseItem>)cls, pname);
00848                 newItem.setDbl("totalTime",   prop.getDbl("Project.totalTime", 20.0)); //$NON-NLS-1$ //$NON-NLS-2$
00849                 newItem.setDbl("timeStep",    prop.getDbl("Project.timeStep", 0.001)); //$NON-NLS-1$ //$NON-NLS-2$
00850                 newItem.setDbl("logTimeStep", prop.getDbl("Project.timeStep", 0.001)); //$NON-NLS-1$ //$NON-NLS-2$
00851                 newItem.setProperty("method", prop.getStr("Project.method")); //$NON-NLS-1$ //$NON-NLS-2$
00852                 manager_.itemChange(newItem, GrxPluginManager.ADD_ITEM);
00853         manager_.setSelectedItem(newItem, true);
00854 
00855                 for (int i = 0; i < prop.getInt("Project.num_object", 0); i++) { //$NON-NLS-1$
00856                         String header = "Object" + i + "."; //$NON-NLS-1$ //$NON-NLS-2$
00857                         String oName = prop.getStr(header + "name"); //$NON-NLS-1$
00858                         String cName = prop.getStr(header + "class"); //$NON-NLS-1$
00859                         if (oName == null || cName == null)
00860                                 continue;
00861                         
00862                         if (cName.equals(ENVIRONMENT_NODE) || cName.equals(ROBOT_NODE)) {
00863                                 cls = manager_.registerPlugin(MODEL_ITEM);
00864                                 try {
00865                                         URL url = new URL(prop.getStr(header + "url")); //$NON-NLS-1$
00866                                         newItem = manager_.loadItem((Class<? extends GrxBaseItem>)cls, oName, url.getPath());
00867                                         if(newItem!=null){
00868                                                 manager_.itemChange(newItem, GrxPluginManager.ADD_ITEM);
00869                                                 manager_.setSelectedItem(newItem, true);
00870                                         }
00871                                 } catch (Exception e) {
00872                                         e.printStackTrace();
00873                                 }
00874                                 if (newItem == null)
00875                                         continue;
00876 
00877                                 if (cName.equals(ENVIRONMENT_NODE)) {
00878                                         newItem.setProperty("isRobot", "false"); //$NON-NLS-1$ //$NON-NLS-2$
00879                                 } else if (cName.equals(ROBOT_NODE)) {
00880                                         newItem.setProperty("isRobot", "true"); //$NON-NLS-1$ //$NON-NLS-2$
00881                                         Enumeration e = prop.keys();
00882                                         while (e.hasMoreElements()) {
00883                                                 String key = (String)e.nextElement();
00884                                                 if (key.startsWith(header)) {
00885                                                         String newKey = key.substring(header.length());
00886                                                         if (key.endsWith(".angle")) { //$NON-NLS-1$
00887                                                                 newItem.setDbl(newKey, prop.getDbl(key, 0.0));
00888                                                         } else if (key.endsWith(".mode")) { //$NON-NLS-1$
00889                                                                 newItem.setProperty(newKey, prop.getStr(key, "Torque")); //$NON-NLS-1$
00890                                                         } else if (key.endsWith(".translation"))  { //$NON-NLS-1$
00891                                                                 newItem.setDblAry(newKey, 
00892                                                                         prop.getDblAry(key, new double[]{0.0, 0.0, 0.0}));
00893                                                         } else if (key.endsWith(".rotation"))  { //$NON-NLS-1$
00894                                                                 newItem.setDblAry(newKey, 
00895                                                                         prop.getDblAry(key, new double[]{0.0, 1.0, 0.0, 0.0}));
00896                                                         }
00897                                                 }
00898                                         }
00899                                         
00900                                         String controller = prop.getStr(header + "controller"); //$NON-NLS-1$
00901                                         controller = controller.replaceFirst("openhrp.", ""); //$NON-NLS-1$ //$NON-NLS-2$
00902                                         double controlTime = prop.getDbl(header + "controlTime", 0.001); //$NON-NLS-1$
00903                                         newItem.setProperty("controller", controller); //$NON-NLS-1$
00904                                         newItem.setProperty("controlTime", String.valueOf(controlTime)); //$NON-NLS-1$
00905 
00906                                         String imageProcessor = prop.getStr(header + "imageProcessor"); //$NON-NLS-1$
00907                                         if (imageProcessor != null) {
00908                                                 double imageProcessTime = prop.getDbl(header + "imageProcessTime", 0.001); //$NON-NLS-1$
00909                                                 newItem.setProperty("imageProcessor", imageProcessor); //$NON-NLS-1$
00910                                                 newItem.setProperty("imageProcessTime", String.valueOf(imageProcessTime)); //$NON-NLS-1$
00911                                         }
00912                                 }
00913                                 
00914                         } else if (cName.equals(COLLISIONPAIR_NODE)) {
00915                                 cls = manager_.registerPlugin(COLLISIONPAIR_ITEM); 
00916                                 newItem = manager_.createItem((Class<? extends GrxBaseItem>)cls, oName);
00917                                 newItem.setProperty("objectName1", prop.getStr(header + "objectName1")); //$NON-NLS-1$ //$NON-NLS-2$
00918                                 newItem.setProperty("jointName1",  prop.getStr(header + "jointName1")); //$NON-NLS-1$ //$NON-NLS-2$
00919                                 newItem.setProperty("objectName2", prop.getStr(header + "objectName2")); //$NON-NLS-1$ //$NON-NLS-2$
00920                                 newItem.setProperty("jointName2",  prop.getStr(header + "jointName2")); //$NON-NLS-1$ //$NON-NLS-2$
00921                                 newItem.setProperty("slidingFriction", prop.getStr(header + "slidingFriction", "0.5")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
00922                                 newItem.setProperty("staticFriction",  prop.getStr(header + "staticFriction", "0.5")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
00923                                 newItem.setProperty("cullingThresh",  prop.getStr(header + "cullingThresh", "0.01")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
00924                                 newItem.setProperty("Restitution",  prop.getStr(header + "Restitution", "0.0")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
00925                                 newItem.setProperty("sprintDamperModel", prop.getStr(header + "springDamplerModel", "false")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
00926                                 newItem.setProperty("springConstant", prop.getStr(header + "springConstant", "0.0 0.0 0.0 0.0 0.0 0.0")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
00927                                 newItem.setProperty("damperConstant", prop.getStr(header + "damperConstant", "0.0 0.0 0.0 0.0 0.0 0.0")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
00928 
00929                         } else if (cName.equals(GRAPH_NODE)) {
00930                                 cls = manager_.registerPlugin(GRAPH_ITEM);
00931                                 newItem = manager_.getItem((Class<? extends GrxBaseItem>)cls, null);
00932                                 if (newItem == null)
00933                                         newItem = manager_.createItem((Class<? extends GrxBaseItem>)cls, "GraphList1"); //$NON-NLS-1$
00934                                 String items = prop.getStr(header + "dataItems"); //$NON-NLS-1$
00935                                 newItem.setProperty(oName + ".dataItems", items); //$NON-NLS-1$
00936                                 String[] str = items.split(","); //$NON-NLS-1$
00937                                 String[] p = { 
00938                                         "object", "node", "attr", "index",  //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
00939                                         "numSibling", "legend", "color"  //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
00940                                 };
00941                                 for (int j = 0; j < str.length; j++) {
00942                                         for (int k = 0; k < p.length; k++) {
00943                                                 String key = str[j] + "." + p[k]; //$NON-NLS-1$
00944                                                 String value = prop.getStr(header + key);
00945                                                 if (value != null)
00946                                                         newItem.setProperty(oName + "." + key, value); //$NON-NLS-1$
00947                                         }
00948                                 }
00949                         }
00950                         newItem.restoreProperties();
00951                         manager_.setSelectedItem(newItem, true);
00952                 }
00953         }
00954         
00955         //
00956         private void setDefaultDirectory(){
00957                 String dir = Activator.getDefault().getPreferenceStore().getString("PROJECT_DIR"); //$NON-NLS-1$
00958         if(dir.equals("")) //$NON-NLS-1$
00959                 dir = System.getenv("PROJECT_DIR"); //$NON-NLS-1$
00960                 if( dir != null ){
00961                         setDefaultDirectory( dir );
00962                 }
00963         }
00964         
00965         private void addItem(){
00966                 TwoInputDialog dialog = new TwoInputDialog( null, MessageBundle.get("GrxProjectItem.dialog.addItem.title"), MessageBundle.get("GrxProjectItem.dialog.addItem.message0"),
00967                                 "", null, MessageBundle.get("GrxProjectItem.dialog.addItem.message1"),""  );
00968                 if(dialog.open()==InputDialog.OK){
00969                         String[] values = dialog.getValues();
00970                         manager_.addPlugin(values[0], values[1]);
00971                 }
00972         }
00973 }


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:54