Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 package com.generalrobotix.ui.view;
00020
00021 import org.eclipse.jface.action.Action;
00022 import org.eclipse.jface.action.IToolBarManager;
00023 import org.eclipse.jface.dialogs.MessageDialog;
00024 import org.eclipse.swt.SWT;
00025 import org.eclipse.swt.events.FocusEvent;
00026 import org.eclipse.swt.events.FocusListener;
00027 import org.eclipse.swt.events.ModifyEvent;
00028 import org.eclipse.swt.events.ModifyListener;
00029 import org.eclipse.swt.layout.GridLayout;
00030 import org.eclipse.swt.layout.GridData;
00031 import org.eclipse.swt.widgets.Composite;
00032 import org.eclipse.swt.widgets.Event;
00033 import org.eclipse.swt.widgets.Label;
00034 import org.eclipse.swt.widgets.Listener;
00035 import org.eclipse.swt.custom.StyledText;
00036
00037 import com.generalrobotix.ui.GrxBaseItem;
00038 import com.generalrobotix.ui.GrxBaseView;
00039 import com.generalrobotix.ui.GrxBaseViewPart;
00040 import com.generalrobotix.ui.GrxPluginManager;
00041 import com.generalrobotix.ui.grxui.Activator;
00042 import com.generalrobotix.ui.item.GrxPythonScriptItem;
00043 import com.generalrobotix.ui.util.MessageBundle;
00044
00045 @SuppressWarnings("serial")
00046 public class GrxTextEditorView extends GrxBaseView {
00047 private Label status_;
00048 private StyledText area_;
00049 private Action save_,saveAs_;
00050 private int caretPos_ = -1;
00051
00052 private GrxPythonScriptItem currentItem_ = null;
00053
00054 public GrxTextEditorView(String name, GrxPluginManager manager_,
00055 GrxBaseViewPart vp, Composite parent) {
00056 super(name, manager_, vp, parent);
00057 GridLayout layout = new GridLayout(1, true);
00058 layout.marginHeight = 0;
00059 layout.marginWidth = 0;
00060 layout.horizontalSpacing = 0;
00061 layout.verticalSpacing = 0;
00062 composite_.setLayout(layout);
00063
00064 area_ = new StyledText( composite_, SWT.MULTI|SWT.V_SCROLL|SWT.BORDER );
00065
00066 area_.addModifyListener(new ModifyListener(){
00067 public void modifyText(ModifyEvent e) {
00068 if(currentItem_!=null)
00069 currentItem_.setValue(area_.getText());
00070 }
00071 });
00072
00073 area_.addFocusListener(new FocusListener() {
00074 public void focusGained(FocusEvent e) {
00075 if (currentItem_ == null)
00076 return;
00077 if (currentItem_.isModifiedExternally()) {
00078 boolean state = MessageDialog.openQuestion(getParent().getShell(), MessageBundle.get("GrxTextEditorView.dialog.title.reload"), MessageBundle.get("GrxTextEditorView.dialog.message.reload"));
00079 if (state){
00080 currentItem_.reload();
00081 area_.setText(currentItem_.getValue());
00082 }
00083 }
00084 }
00085
00086 public void focusLost(FocusEvent e) {
00087 }
00088 });
00089
00090 area_.addListener(SWT.Paint, new Listener() {
00091 public void handleEvent(Event event) {
00092 setPositionLabel();
00093 }
00094 });
00095
00096 area_.addListener(SWT.MouseDown, new Listener() {
00097 public void handleEvent(Event event) {
00098 setPositionLabel();
00099 }
00100 });
00101
00102 area_.addListener(SWT.KeyDown, new Listener() {
00103 public void handleEvent(Event event) {
00104 setPositionLabel();
00105 }
00106 });
00107
00108 status_ = new Label(composite_, SWT.BORDER);
00109
00110 GridData textData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
00111 area_.setLayoutData(textData);
00112
00113 GridData statusData = new GridData(GridData.FILL_HORIZONTAL);
00114 statusData.verticalAlignment = SWT.END;
00115 status_.setLayoutData(statusData);
00116
00117
00118 IToolBarManager toolbar = vp.getViewSite().getActionBars().getToolBarManager();
00119
00120 save_ = new Action() {
00121 public void run() {
00122 if (currentItem_ != null) {
00123 currentItem_.setValue(area_.getText());
00124 currentItem_.save();
00125
00126 }
00127 }
00128 };
00129 save_.setToolTipText( MessageBundle.get("GrxTextEditorView.text.save") );
00130 save_.setImageDescriptor( Activator.getDefault().getDescriptor("save_edit.png") );
00131 toolbar.add( save_ );
00132
00133 saveAs_ = new Action() {
00134 public void run() {
00135 if (currentItem_ != null) {
00136 currentItem_.setValue(area_.getText());
00137 currentItem_.saveAs();
00138 }
00139 }
00140 };
00141 saveAs_.setToolTipText( MessageBundle.get("GrxTextEditorView.text.saveAs") );
00142 saveAs_.setImageDescriptor( Activator.getDefault().getDescriptor("saveas_edit.png") );
00143 toolbar.add( saveAs_ );
00144 setScrollMinSize(SWT.DEFAULT,SWT.DEFAULT);
00145
00146 setUp();
00147 manager_.registerItemChangeListener(this, GrxPythonScriptItem.class);
00148 updateEditerFont();
00149 }
00150
00151 public void setUp(){
00152 currentItem_ = manager_.<GrxPythonScriptItem>getSelectedItem(GrxPythonScriptItem.class, null);
00153 setTextItem(currentItem_);
00154 }
00155
00156 private void setTextItem(GrxPythonScriptItem item){
00157 if(item != null){
00158 area_.setText( (String)item.getValue() );
00159 area_.setEnabled(true);
00160 save_.setEnabled(true);
00161 saveAs_.setEnabled(true);
00162 setPositionLabel();
00163 }else{
00164 area_.setText("");
00165 area_.setEnabled(false);
00166 save_.setEnabled(false);
00167 saveAs_.setEnabled(false);
00168 status_.setText("");
00169 }
00170 }
00171
00172 private void setPositionLabel(){
00173 if(caretPos_ == area_.getCaretOffset())
00174 return;
00175 caretPos_ = area_.getCaretOffset();
00176 int y = area_.getLineAtOffset(caretPos_);
00177 String s = area_.getText();
00178 int x = 0;
00179 int ti = caretPos_;
00180 while(0 < ti){
00181 --ti;
00182 char cha = s.charAt(ti);
00183 if(cha == '\r' || cha == '\n'){
00184 break;
00185 }
00186 ++x;
00187 }
00188 status_.setText(String.format("%6d:%d", y + 1, x + 1));
00189 }
00190
00191 public void registerItemChange(GrxBaseItem item, int event){
00192 GrxPythonScriptItem textItem = (GrxPythonScriptItem)item;
00193 switch(event){
00194 case GrxPluginManager.SELECTED_ITEM:
00195 currentItem_ = textItem;
00196 setTextItem(textItem);
00197 break;
00198 case GrxPluginManager.REMOVE_ITEM:
00199 case GrxPluginManager.NOTSELECTED_ITEM:
00200 if(currentItem_ == textItem){
00201 currentItem_ = null;
00202 setTextItem(null);
00203 }
00204 break;
00205 default:
00206 break;
00207 }
00208 }
00209
00210 public void shutdown(){
00211 manager_.removeItemChangeListener(this, GrxPythonScriptItem.class);
00212 }
00213
00214 public void updateEditerFont(){
00215 area_.setFont(Activator.getDefault().getFont("preference_editer"));
00216 }
00217 }