GrxTextItem.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
10 /*
11  * GrxTextItem.java
12  *
13  * Copyright (C) 2007 GeneralRobotix, Inc.
14  * All Rights Reserved
15  *
16  * @author Yuichiro Kawasumi (General Robotix, Inc.)
17  */
18 
19 package com.generalrobotix.ui.item;
20 
21 import java.io.BufferedReader;
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.FileReader;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 
28 import org.eclipse.jface.action.Action;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.widgets.FileDialog;
32 
38 
39 import org.eclipse.swt.widgets.Text;
40 
41 @SuppressWarnings("serial") //$NON-NLS-1$
42 public class GrxTextItem extends GrxBaseItem {
43  public static final String TITLE = "Text Item"; //$NON-NLS-1$
44  public static final String FILE_EXTENSION = "txt"; //$NON-NLS-1$
45 
46  private long lastModified_ = 0;
47  private String old = ""; //$NON-NLS-1$
48  private int caretPosition_ = 0;
49 
50  public GrxTextItem(String name, GrxPluginManager manager) {
51  super(name, manager);
52  Action item = new Action(){
53  public String getText(){ return MessageBundle.get("GrxTextItem.menu.save"); } //$NON-NLS-1$
54  public void run(){ save(); }
55  };
56  setMenuItem(item);
57  item = new Action(){
58  public String getText(){ return MessageBundle.get("GrxTextItem.menu.saveAs"); } //$NON-NLS-1$
59  public void run(){ saveAs(); }
60  };
61  setMenuItem(item);
62 
63  setExclusive(true);
64  }
65 
66  public boolean create() {
67  file_ = new File(getDefaultDir()+File.separator+getName()+"."+getFileExtention()); //$NON-NLS-1$
68  setURL(file_.getPath());
69 
70  old=""; //$NON-NLS-1$
71  lastModified_ = file_.lastModified();
72 // undo_.discardAllEdits();
73  caretPosition_ = 0;
74  setValue(""); //$NON-NLS-1$
75  return true;
76  }
77 
78  public boolean load(File f) {
79  String delimiter = Text.DELIMITER;
80  try {
81  BufferedReader b = new BufferedReader(new FileReader(f));
82  StringBuffer buf = new StringBuffer();
83  while (b.ready()) {
84  buf.append(b.readLine() + delimiter);
85  }
86  setValue(buf.toString());
87  } catch (FileNotFoundException e) {
88  GrxDebugUtil.println("TextItem: File Not Found. ("+f.getName()+")"); //$NON-NLS-1$ //$NON-NLS-2$
89  return false;
90  } catch (IOException e) {
91  e.printStackTrace();
92  }
93  file_ = f;
94 
95  old = getValue();
96  lastModified_ = file_.lastModified();
97 // undo_.discardAllEdits();
98  caretPosition_ = 0;
99  return true;
100  }
101 
102  public void rename(String newName) {
103  String p = file_.getParent();
104  super.rename(newName);
105  file_ = new File(p+File.separator+getName()+"."+getFileExtention()); //$NON-NLS-1$
106  setURL(file_.getPath());
107  }
108 
109  public boolean save() {
110  try {
111  GrxDebugUtil.println("savefile:"+file_.getPath()); //$NON-NLS-1$
112  if (!file_.exists())
113  saveAs();
114  FileWriter fw = new FileWriter(file_);
115  fw.write(getValue());
116  fw.close();
117  old = getValue();
118 // undo_.discardAllEdits();
119  lastModified_ = file_.lastModified();
120  } catch (IOException e) {
121  e.printStackTrace();
122  return false;
123  }
124  return true;
125  }
126 
127  public boolean saveAs() {
128  FileDialog fdlg = new FileDialog(GrxUIPerspectiveFactory.getCurrentShell(), SWT.SAVE);
129  fdlg.setFileName(getName()+"."+getFileExtention()); //$NON-NLS-1$
130  fdlg.setFilterExtensions(new String[]{"*."+getFileExtention()}); //$NON-NLS-1$
131  fdlg.setFilterPath(getDefaultDir().getAbsolutePath());
132  final String fPath = fdlg.open();
133  if (fPath != null) {
134  File f = new File(fPath);
135  if( f.exists() ){
136  if( !MessageDialog.openConfirm( GrxUIPerspectiveFactory.getCurrentShell(), MessageBundle.get("GrxTextItem.dialog.title.saveFile"), MessageBundle.get("GrxTextItem.dialog.message.saveFile"))) //$NON-NLS-1$ //$NON-NLS-2$
137  return false;
138  }else{
139  try {
140  f.createNewFile();
141  } catch (IOException e) {
142  e.printStackTrace();
143  }
144  }
145  String newName = f.getName().split("[.]")[0]; //$NON-NLS-1$
146  manager_.renamePlugin(this, newName);
147  if (getName().equals(newName)) {
148  file_ = f;
149  setURL(f.getPath());
150  setDefaultDirectory(f.getParent());
151  return save();
152  }
153  }
154  return false;
155  }
156 
157  public String getValue() {
158  return (String)super.getValue();
159  }
160 
161  public boolean isEdited() {
162  return !old.equals(getValue());
163  }
164 
165  public boolean isModifiedExternally() {
166  if (lastModified_ < file_.lastModified()) {
167  lastModified_ = file_.lastModified();
168  return true;
169  }
170  return false;
171  }
172 
173  public void reload() {
174  this.load(file_);
175  }
176 
177  public void setCaretPosition(int pos) {
178  caretPosition_ = pos;
179  }
180 
181  public int getCaretPositoin() {
182  return caretPosition_;
183  }
184 }
static final String get(String key)
#define null
our own NULL pointer
Definition: IceTypes.h:57
png_infop png_charpp name
Definition: png.h:2382
long b
Definition: jpegint.h:371
def run(tree, args)
png_bytep buf
Definition: png.h:2729
プラグイン管理クラス GrxUIの核になるクラス。プラグインのロード等の、初期化を実行する。 プラグインとそ...
GrxTextItem(String name, GrxPluginManager manager)
def getText(self, nodes=None)
org


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Sep 8 2022 02:24:03