GrxTextItem.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  *  GrxTextItem.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.BufferedReader;
00022 import java.io.File;
00023 import java.io.FileNotFoundException;
00024 import java.io.FileReader;
00025 import java.io.FileWriter;
00026 import java.io.IOException;
00027 
00028 import org.eclipse.jface.action.Action;
00029 import org.eclipse.jface.dialogs.MessageDialog;
00030 import org.eclipse.swt.SWT;
00031 import org.eclipse.swt.widgets.FileDialog;
00032 
00033 import com.generalrobotix.ui.grxui.GrxUIPerspectiveFactory;
00034 import com.generalrobotix.ui.GrxBaseItem;
00035 import com.generalrobotix.ui.GrxPluginManager;
00036 import com.generalrobotix.ui.util.GrxDebugUtil;
00037 import com.generalrobotix.ui.util.MessageBundle;
00038 
00039 import org.eclipse.swt.widgets.Text;
00040 
00041 @SuppressWarnings("serial") //$NON-NLS-1$
00042 public class GrxTextItem extends GrxBaseItem {
00043         public static final String TITLE = "Text Item"; //$NON-NLS-1$
00044         public static final String FILE_EXTENSION = "txt"; //$NON-NLS-1$
00045 
00046         private long lastModified_ = 0;
00047         private String old = ""; //$NON-NLS-1$
00048         private int caretPosition_ = 0;
00049         
00050         public GrxTextItem(String name, GrxPluginManager manager) {
00051                 super(name, manager);
00052                 Action item = new Action(){
00053                         public String getText(){ return MessageBundle.get("GrxTextItem.menu.save"); } //$NON-NLS-1$
00054                         public void run(){ save(); }
00055                 };
00056                 setMenuItem(item);
00057                 item = new Action(){
00058                         public String getText(){ return MessageBundle.get("GrxTextItem.menu.saveAs"); } //$NON-NLS-1$
00059                         public void run(){ saveAs(); }
00060                 };
00061                 setMenuItem(item);
00062 
00063                 setExclusive(true);
00064         }
00065         
00066         public boolean create() {
00067                 file_ = new File(getDefaultDir()+File.separator+getName()+"."+getFileExtention()); //$NON-NLS-1$
00068                 setURL(file_.getPath());
00069                 
00070                 old=""; //$NON-NLS-1$
00071                 lastModified_ = file_.lastModified();
00072 //              undo_.discardAllEdits();
00073                 caretPosition_ = 0;
00074                 setValue(""); //$NON-NLS-1$
00075                 return true;
00076         }
00077 
00078         public boolean load(File f) {
00079                 String delimiter = Text.DELIMITER;
00080                 try {
00081                         BufferedReader b = new BufferedReader(new FileReader(f));
00082                         StringBuffer buf = new StringBuffer();
00083                         while (b.ready()) {
00084                                 buf.append(b.readLine() + delimiter);
00085                         }
00086                         setValue(buf.toString());
00087                 } catch (FileNotFoundException e) {
00088                         GrxDebugUtil.println("TextItem: File Not Found. ("+f.getName()+")"); //$NON-NLS-1$ //$NON-NLS-2$
00089                         return false;
00090                 } catch (IOException e) {
00091                         e.printStackTrace();
00092                 }
00093                 file_ = f;
00094                 
00095                 old = getValue();
00096                 lastModified_ = file_.lastModified();
00097 //              undo_.discardAllEdits();
00098                 caretPosition_ = 0;
00099                 return true;
00100         }
00101         
00102         public void rename(String newName) {
00103                 String p = file_.getParent();
00104                 super.rename(newName);
00105                 file_ = new File(p+File.separator+getName()+"."+getFileExtention()); //$NON-NLS-1$
00106                 setURL(file_.getPath());
00107         }
00108 
00109         public boolean save() {
00110                 try {
00111                         GrxDebugUtil.println("savefile:"+file_.getPath()); //$NON-NLS-1$
00112                         if (!file_.exists())
00113                                 saveAs();
00114                         FileWriter fw = new FileWriter(file_);
00115                         fw.write(getValue());
00116                         fw.close();
00117                         old = getValue();
00118 //                      undo_.discardAllEdits();
00119                         lastModified_ = file_.lastModified();
00120                 } catch (IOException e) {
00121                         e.printStackTrace();
00122                         return false;
00123                 }
00124                 return true;
00125         }
00126         
00127         public boolean saveAs() {
00128         FileDialog fdlg = new FileDialog(GrxUIPerspectiveFactory.getCurrentShell(), SWT.SAVE);
00129         fdlg.setFileName(getName()+"."+getFileExtention()); //$NON-NLS-1$
00130         fdlg.setFilterExtensions(new String[]{"*."+getFileExtention()}); //$NON-NLS-1$
00131         fdlg.setFilterPath(getDefaultDir().getAbsolutePath());
00132         final String fPath = fdlg.open();
00133         if (fPath != null) {
00134             File f = new File(fPath);
00135             if( f.exists() ){
00136                 if( !MessageDialog.openConfirm( GrxUIPerspectiveFactory.getCurrentShell(), MessageBundle.get("GrxTextItem.dialog.title.saveFile"), MessageBundle.get("GrxTextItem.dialog.message.saveFile"))) //$NON-NLS-1$ //$NON-NLS-2$
00137                     return false;
00138             }else{
00139                 try {
00140                     f.createNewFile();
00141                 } catch (IOException e) {
00142                     e.printStackTrace();
00143                 }
00144             }
00145             String newName = f.getName().split("[.]")[0]; //$NON-NLS-1$
00146             manager_.renamePlugin(this, newName);
00147             if (getName().equals(newName)) {
00148                 file_ = f;
00149                 setURL(f.getPath());
00150                 setDefaultDirectory(f.getParent());
00151                 return save();
00152             }
00153         }
00154         return false;
00155         }
00156         
00157         public String getValue() {
00158                 return (String)super.getValue();
00159         }
00160         
00161         public boolean isEdited() {
00162                 return !old.equals(getValue());
00163         }
00164 
00165         public boolean isModifiedExternally() {
00166                 if (lastModified_ < file_.lastModified()) {
00167                         lastModified_ = file_.lastModified();
00168                         return true;
00169                 }
00170                 return false;
00171         }
00172         
00173         public void reload() {
00174                 this.load(file_);
00175         }
00176         
00177         public void setCaretPosition(int pos) {
00178                 caretPosition_ = pos;
00179         }
00180         
00181         public int getCaretPositoin() {
00182                 return caretPosition_;
00183         }
00184 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:16