GrxTextEditorView.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  * GrxTextEditorView.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.view;
20 
21 import org.eclipse.jface.action.Action;
22 import org.eclipse.jface.action.IToolBarManager;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.FocusEvent;
26 import org.eclipse.swt.events.FocusListener;
27 import org.eclipse.swt.events.ModifyEvent;
28 import org.eclipse.swt.events.ModifyListener;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Event;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Listener;
35 import org.eclipse.swt.custom.StyledText;
36 
44 
45 @SuppressWarnings("serial") //$NON-NLS-1$
46 public class GrxTextEditorView extends GrxBaseView {
47  private Label status_;
48  private StyledText area_;
49  private Action save_,saveAs_;
50  private int caretPos_ = -1;
51 
52  private GrxPythonScriptItem currentItem_ = null;
53 
54  public GrxTextEditorView(String name, GrxPluginManager manager_,
55  GrxBaseViewPart vp, Composite parent) {
56  super(name, manager_, vp, parent);
57  GridLayout layout = new GridLayout(1, true);
58  layout.marginHeight = 0;
59  layout.marginWidth = 0;
60  layout.horizontalSpacing = 0;
61  layout.verticalSpacing = 0;
62  composite_.setLayout(layout);
63 
64  area_ = new StyledText( composite_, SWT.MULTI|SWT.V_SCROLL|SWT.BORDER );
65 
66  area_.addModifyListener(new ModifyListener(){
67  public void modifyText(ModifyEvent e) {
68  if(currentItem_!=null)
69  currentItem_.setValue(area_.getText());
70  }
71  });
72 
73  area_.addFocusListener(new FocusListener() {
74  public void focusGained(FocusEvent e) {
75  if (currentItem_ == null)
76  return;
77  if (currentItem_.isModifiedExternally()) {
78  boolean state = MessageDialog.openQuestion(getParent().getShell(), MessageBundle.get("GrxTextEditorView.dialog.title.reload"), MessageBundle.get("GrxTextEditorView.dialog.message.reload")); //$NON-NLS-1$ //$NON-NLS-2$
79  if (state){
80  currentItem_.reload();
81  area_.setText(currentItem_.getValue());
82  }
83  }
84  }
85 
86  public void focusLost(FocusEvent e) {
87  }
88  });
89 
90  area_.addListener(SWT.Paint, new Listener() {
91  public void handleEvent(Event event) {
92  setPositionLabel();
93  }
94  });
95 
96  area_.addListener(SWT.MouseDown, new Listener() {
97  public void handleEvent(Event event) {
98  setPositionLabel();
99  }
100  });
101 
102  area_.addListener(SWT.KeyDown, new Listener() {
103  public void handleEvent(Event event) {
104  setPositionLabel();
105  }
106  });
107 
108  status_ = new Label(composite_, SWT.BORDER);
109 
110  GridData textData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
111  area_.setLayoutData(textData);
112 
113  GridData statusData = new GridData(GridData.FILL_HORIZONTAL);
114  statusData.verticalAlignment = SWT.END;
115  status_.setLayoutData(statusData);
116 
117 
118  IToolBarManager toolbar = vp.getViewSite().getActionBars().getToolBarManager();
119 
120  save_ = new Action() {
121  public void run() {
122  if (currentItem_ != null) {
123  currentItem_.setValue(area_.getText());
124  currentItem_.save();
125  //save_.setEnabled(false);
126  }
127  }
128  };
129  save_.setToolTipText( MessageBundle.get("GrxTextEditorView.text.save") ); //$NON-NLS-1$
130  save_.setImageDescriptor( Activator.getDefault().getDescriptor("save_edit.png") ); //$NON-NLS-1$
131  toolbar.add( save_ );
132 
133  saveAs_ = new Action() {
134  public void run() {
135  if (currentItem_ != null) {
136  currentItem_.setValue(area_.getText());
137  currentItem_.saveAs();
138  }
139  }
140  };
141  saveAs_.setToolTipText( MessageBundle.get("GrxTextEditorView.text.saveAs") ); //$NON-NLS-1$
142  saveAs_.setImageDescriptor( Activator.getDefault().getDescriptor("saveas_edit.png") ); //$NON-NLS-1$
143  toolbar.add( saveAs_ );
144  setScrollMinSize(SWT.DEFAULT,SWT.DEFAULT);
145 
146  setUp();
147  manager_.registerItemChangeListener(this, GrxPythonScriptItem.class);
148  updateEditerFont();
149  }
150 
151  public void setUp(){
152  currentItem_ = manager_.<GrxPythonScriptItem>getSelectedItem(GrxPythonScriptItem.class, null);
153  setTextItem(currentItem_);
154  }
155 
157  if(item != null){
158  area_.setText( (String)item.getValue() );
159  area_.setEnabled(true);
160  save_.setEnabled(true);
161  saveAs_.setEnabled(true);
162  setPositionLabel();
163  }else{
164  area_.setText(""); //$NON-NLS-1$
165  area_.setEnabled(false);
166  save_.setEnabled(false);
167  saveAs_.setEnabled(false);
168  status_.setText("");
169  }
170  }
171 
172  private void setPositionLabel(){
173  if(caretPos_ == area_.getCaretOffset())
174  return;
175  caretPos_ = area_.getCaretOffset();
176  int y = area_.getLineAtOffset(caretPos_);
177  String s = area_.getText();
178  int x = 0;
179  int ti = caretPos_;
180  while(0 < ti){
181  --ti;
182  char cha = s.charAt(ti);
183  if(cha == '\r' || cha == '\n'){
184  break;
185  }
186  ++x;
187  }
188  status_.setText(String.format("%6d:%d", y + 1, x + 1));
189  }
190 
191  public void registerItemChange(GrxBaseItem item, int event){
192  GrxPythonScriptItem textItem = (GrxPythonScriptItem)item;
193  switch(event){
195  currentItem_ = textItem;
196  setTextItem(textItem);
197  break;
200  if(currentItem_ == textItem){
201  currentItem_ = null;
202  setTextItem(null);
203  }
204  break;
205  default:
206  break;
207  }
208  }
209 
210  public void shutdown(){
211  manager_.removeItemChangeListener(this, GrxPythonScriptItem.class);
212  }
213 
214  public void updateEditerFont(){
215  area_.setFont(Activator.getDefault().getFont("preference_editer"));
216  }
217 }
static final String get(String key)
* x
Definition: IceUtils.h:98
state
void registerItemChange(GrxBaseItem item, int event)
#define null
our own NULL pointer
Definition: IceTypes.h:57
png_infop png_charpp name
Definition: png.h:2382
void registerItemChangeListener(GrxItemChangeListener view, Class<? extends GrxBaseItem > cls)
def run(tree, args)
GrxTextEditorView(String name, GrxPluginManager manager_, GrxBaseViewPart vp, Composite parent)
プラグイン管理クラス GrxUIの核になるクラス。プラグインのロード等の、初期化を実行する。 プラグインとそ...
org
* y
Definition: IceUtils.h:97


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